Best Syzkaller code snippet using isolated.Forward
iptables_test.go
Source:iptables_test.go
...124// - hook does nothing if container is not in bridge or user mode125// - iptables are correctly injected on at hook execution126func (s *IptablesTestSuite) TestIptablesHookRunPostRun() {127 // Store the state of the namespace network128 BaseForwardRule, _ := s.iptablesDriver.List("filter", s.forwardChain)129 BasePostRoutingRules, _ := s.iptablesDriver.List("nat", s.postroutingChain)130 BasePreRoutingRules, _ := s.iptablesDriver.List("nat", s.preroutingChain)131 // Injection should not be executed if the network is not in bridge mode132 info := &mesos.TaskInfo{}133 assert.Nil(s.T(), s.hook.RunPostRun(s.c, info, s.frameworkInfo, ""))134 forwardRules, _ := s.iptablesDriver.List("filter", s.forwardChain)135 postRoutingRules, _ := s.iptablesDriver.List("nat", s.postroutingChain)136 preRoutingRules, _ := s.iptablesDriver.List("nat", s.preroutingChain)137 assert.Equal(138 s.T(),139 BaseForwardRule,140 forwardRules,141 )142 assert.Equal(143 s.T(),144 BasePostRoutingRules,145 postRoutingRules,146 )147 assert.Equal(148 s.T(),149 BasePreRoutingRules,150 preRoutingRules,151 )152 // Now test for each table and chain that rule are correctly inserted,153 // next to the previous network states154 assert.Nil(s.T(), s.hook.RunPostRun(s.c, s.taskInfo, s.frameworkInfo, ""))155 forwardRules, _ = s.iptablesDriver.List("filter", s.forwardChain)156 assert.Subset(157 s.T(),158 forwardRules,159 append(160 BaseForwardRule,161 []string{162 "-A " + s.forwardChain + " -d 172.0.2.1/32 ! -i docker0 -o docker0 -p tcp -m tcp --dport 80 -j ACCEPT",163 "-A " + s.forwardChain + " -s 172.0.2.1/32 -i docker0 ! -o docker0 -p tcp -m tcp --sport 80 -j ACCEPT",164 "-A " + s.forwardChain + " -d 172.0.2.1/32 ! -i docker0 -o docker0 -p udp -m udp --dport 10000 -j ACCEPT",165 "-A " + s.forwardChain + " -s 172.0.2.1/32 -i docker0 ! -o docker0 -p udp -m udp --sport 10000 -j ACCEPT",166 "-A " + s.forwardChain + " -d 172.0.3.1/32 ! -i docker0 -o docker0 -p tcp -m tcp --dport 80 -j ACCEPT",167 "-A " + s.forwardChain + " -s 172.0.3.1/32 -i docker0 ! -o docker0 -p tcp -m tcp --sport 80 -j ACCEPT",168 "-A " + s.forwardChain + " -d 172.0.3.1/32 ! -i docker0 -o docker0 -p udp -m udp --dport 10000 -j ACCEPT",169 "-A " + s.forwardChain + " -s 172.0.3.1/32 -i docker0 ! -o docker0 -p udp -m udp --sport 10000 -j ACCEPT",170 }...,171 ),172 )173 postRoutingRules, _ = s.iptablesDriver.List("nat", s.postroutingChain)174 assert.Subset(175 s.T(),176 postRoutingRules,177 append(178 BasePostRoutingRules,179 []string{180 "-A " + s.postroutingChain + " -s 172.0.2.1/32 ! -o docker0 -j MASQUERADE",181 "-A " + s.postroutingChain + " -s 172.0.2.1/32 -d 172.0.2.1/32 -p tcp -m tcp --dport 80 -j MASQUERADE",182 "-A " + s.postroutingChain + " -s 172.0.2.1/32 -d 172.0.2.1/32 -p udp -m udp --dport 10000 -j MASQUERADE",183 "-A " + s.postroutingChain + " -s 172.0.3.1/32 ! -o docker0 -j MASQUERADE",184 "-A " + s.postroutingChain + " -s 172.0.3.1/32 -d 172.0.3.1/32 -p tcp -m tcp --dport 80 -j MASQUERADE",185 "-A " + s.postroutingChain + " -s 172.0.3.1/32 -d 172.0.3.1/32 -p udp -m udp --dport 10000 -j MASQUERADE",186 }...,187 ),188 )189 preRoutingRules, _ = s.iptablesDriver.List("nat", s.preroutingChain)190 assert.Subset(191 s.T(),192 preRoutingRules,193 append(194 BasePreRoutingRules,195 []string{196 "-A " + s.preroutingChain + " ! -i docker0 -p tcp -m tcp --dport 32000 -j DNAT --to-destination 172.0.2.1:80",197 "-A " + s.preroutingChain + " ! -i docker0 -p udp -m udp --dport 33000 -j DNAT --to-destination 172.0.2.1:10000",198 "-A " + s.preroutingChain + " ! -i docker0 -p tcp -m tcp --dport 32000 -j DNAT --to-destination 172.0.3.1:80",199 "-A " + s.preroutingChain + " ! -i docker0 -p udp -m udp --dport 33000 -j DNAT --to-destination 172.0.3.1:10000",200 }...,201 ),202 )203}204// Check that :205// - hook does nothing when container is not in bridge or user mode206// - iptables are correctly removed at task hook execution207func (s *IptablesTestSuite) TestIptablesHookRunPreStop() {208 // Store the state of the namespace network209 referenceForwardRule, _ := s.iptablesDriver.List("filter", s.forwardChain)210 referencePostRoutingRules, _ := s.iptablesDriver.List("nat", s.postroutingChain)211 referencePreRoutingRules, _ := s.iptablesDriver.List("nat", s.preroutingChain)212 // Removing should not be executed if the network is not in bridge mode213 info := &mesos.TaskInfo{}214 assert.Nil(s.T(), s.hook.RunPreStop(s.c, info, s.frameworkInfo, ""))215 // Execute insert iptables hook to insert iptables216 s.hook.RunPostRun(s.c, s.taskInfo, s.frameworkInfo, "")217 // Execute remove iptables hook to remove inserted iptables218 assert.Nil(s.T(), s.hook.RunPreStop(s.c, s.taskInfo, s.frameworkInfo, ""))219 forwardRules, _ := s.iptablesDriver.List("filter", s.forwardChain)220 postRoutingRules, _ := s.iptablesDriver.List("nat", s.postroutingChain)221 preRoutingRules, _ := s.iptablesDriver.List("nat", s.preroutingChain)222 assert.Equal(223 s.T(),224 forwardRules,225 referenceForwardRule,226 )227 assert.Equal(228 s.T(),229 postRoutingRules,230 referencePostRoutingRules,231 )232 assert.Equal(233 s.T(),234 preRoutingRules,235 referencePreRoutingRules,236 )237}238func TestIptablesHookSuite(t *testing.T) {239 suite.Run(t, new(IptablesTestSuite))...
isolated.go
Source:isolated.go
...94 inst.ssh("rm -rf '" + filepath.Join(inst.cfg.TargetDir, "*") + "'")95 closeInst = nil96 return inst, nil97}98func (inst *instance) Forward(port int) (string, error) {99 if inst.forwardPort != 0 {100 return "", fmt.Errorf("isolated: Forward port already set")101 }102 if port == 0 {103 return "", fmt.Errorf("isolated: Forward port is zero")104 }105 inst.forwardPort = port106 return fmt.Sprintf("127.0.0.1:%v", port), nil107}108func (inst *instance) ssh(command string) error {109 if inst.debug {110 log.Logf(0, "executing ssh %+v", command)111 }112 rpipe, wpipe, err := osutil.LongPipe()113 if err != nil {114 return err115 }116 // TODO(dvyukov): who is closing rpipe?117 args := append(vmimpl.SSHArgs(inst.debug, inst.sshKey, inst.targetPort),118 inst.sshUser+"@"+inst.targetAddr, command)119 if inst.debug {120 log.Logf(0, "running command: ssh %#v", args)121 }122 cmd := osutil.Command("ssh", args...)123 cmd.Stdout = wpipe124 cmd.Stderr = wpipe125 if err := cmd.Start(); err != nil {126 wpipe.Close()127 return err128 }129 wpipe.Close()130 done := make(chan bool)131 go func() {132 select {133 case <-time.After(time.Second * 30):134 if inst.debug {135 log.Logf(0, "ssh hanged")136 }137 cmd.Process.Kill()138 case <-done:139 }140 }()141 if err := cmd.Wait(); err != nil {142 close(done)143 out, _ := ioutil.ReadAll(rpipe)144 if inst.debug {145 log.Logf(0, "ssh failed: %v\n%s", err, out)146 }147 return fmt.Errorf("ssh %+v failed: %v\n%s", args, err, out)148 }149 close(done)150 if inst.debug {151 log.Logf(0, "ssh returned")152 }153 return nil154}155func (inst *instance) repair() error {156 log.Logf(2, "isolated: trying to ssh")157 if err := inst.waitForSSH(30 * time.Minute); err == nil {158 if inst.cfg.TargetReboot {159 log.Logf(2, "isolated: trying to reboot")160 inst.ssh("reboot") // reboot will return an error, ignore it161 if err := inst.waitForReboot(5 * 60); err != nil {162 log.Logf(2, "isolated: machine did not reboot")163 return err164 }165 log.Logf(2, "isolated: rebooted wait for comeback")166 if err := inst.waitForSSH(30 * time.Minute); err != nil {167 log.Logf(2, "isolated: machine did not comeback")168 return err169 }170 log.Logf(2, "isolated: reboot succeeded")171 } else {172 log.Logf(2, "isolated: ssh succeeded")173 }174 } else {175 log.Logf(2, "isolated: ssh failed")176 return fmt.Errorf("SSH failed")177 }178 return nil179}180func (inst *instance) waitForSSH(timeout time.Duration) error {181 return vmimpl.WaitForSSH(inst.debug, timeout, inst.targetAddr, inst.sshKey, inst.sshUser,182 inst.os, inst.targetPort, nil)183}184func (inst *instance) waitForReboot(timeout int) error {185 var err error186 start := time.Now()187 for {188 if !vmimpl.SleepInterruptible(time.Second) {189 return fmt.Errorf("shutdown in progress")190 }191 // If it fails, then the reboot started192 if err = inst.ssh("pwd"); err != nil {193 return nil194 }195 if time.Since(start).Seconds() > float64(timeout) {196 break197 }198 }199 return fmt.Errorf("isolated: the machine did not reboot on repair")200}201func (inst *instance) Close() {202 close(inst.closed)203}204func (inst *instance) Copy(hostSrc string) (string, error) {205 baseName := filepath.Base(hostSrc)206 vmDst := filepath.Join(inst.cfg.TargetDir, baseName)207 inst.ssh("pkill -9 '" + baseName + "'; rm -f '" + vmDst + "'")208 args := append(vmimpl.SCPArgs(inst.debug, inst.sshKey, inst.targetPort),209 hostSrc, inst.sshUser+"@"+inst.targetAddr+":"+vmDst)210 cmd := osutil.Command("scp", args...)211 if inst.debug {212 log.Logf(0, "running command: scp %#v", args)213 cmd.Stdout = os.Stdout214 cmd.Stderr = os.Stdout215 }216 if err := cmd.Start(); err != nil {217 return "", err218 }219 done := make(chan bool)220 go func() {221 select {222 case <-time.After(3 * time.Minute):223 cmd.Process.Kill()224 case <-done:225 }226 }()227 err := cmd.Wait()228 close(done)229 if err != nil {230 return "", err231 }232 return vmDst, nil233}234func (inst *instance) Run(timeout time.Duration, stop <-chan bool, command string) (235 <-chan []byte, <-chan error, error) {236 args := append(vmimpl.SSHArgs(inst.debug, inst.sshKey, inst.targetPort), inst.sshUser+"@"+inst.targetAddr)237 dmesg, err := vmimpl.OpenRemoteConsole("ssh", args...)238 if err != nil {239 return nil, nil, err240 }241 rpipe, wpipe, err := osutil.LongPipe()242 if err != nil {243 dmesg.Close()244 return nil, nil, err245 }246 args = vmimpl.SSHArgs(inst.debug, inst.sshKey, inst.targetPort)247 // Forward target port as part of the ssh connection (reverse proxy)248 if inst.forwardPort != 0 {249 proxy := fmt.Sprintf("%v:127.0.0.1:%v", inst.forwardPort, inst.forwardPort)250 args = append(args, "-R", proxy)251 }252 args = append(args, inst.sshUser+"@"+inst.targetAddr, "cd "+inst.cfg.TargetDir+" && exec "+command)253 log.Logf(0, "running command: ssh %#v", args)254 if inst.debug {255 log.Logf(0, "running command: ssh %#v", args)256 }257 cmd := osutil.Command("ssh", args...)258 cmd.Stdout = wpipe259 cmd.Stderr = wpipe260 if err := cmd.Start(); err != nil {261 dmesg.Close()...
Forward
Using AI Code Generation
1import "fmt"2func main(){3 fmt.Println(isolated.Forward(5))4}5import "fmt"6func main(){7 fmt.Println(isolated.Backward(5))8}9In this way, we can use the isolated class as an interface in our code. But we can’t use the Forward() and Backward() methods of the isolated class directly in the code. We have to
Forward
Using AI Code Generation
1import (2func main() {3 i.Forward()4}5import (6func main() {7 i.Forward()8}9import (10func main() {11 i.Forward()12}13import (14func main() {15 i.Forward()16}17import (18func main() {19 i.Forward()20}21import (22func main() {23 i.Forward()24}25import (26func main() {27 i.Forward()28}29import (30func main() {31 i.Forward()32}33import (34func main() {35 i.Forward()36}37import (38func main() {39 i.Forward()40}41import (42func main() {43 i.Forward()44}
Forward
Using AI Code Generation
1func main() {2 i := isolated.New()3 i.Forward()4}5func main() {6 i := isolated.New()7 i.Forward()8}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!