def check_capture_fifo(self, cmd=None): if not config.canMkfifo(): self.skipTest('Test requires OS fifo support.') if cmd == config.cmd_wireshark and not config.canDisplay(): self.skipTest('Test requires a display.') self.assertIsNotNone(cmd) capture_file = os.path.join(config.capture_dir, 'dhcp.pcap') testout_file = self.filename_from_id(testout_pcap) fifo_file = self.filename_from_id('testout.fifo') try: # If a previous test left its fifo laying around, e.g. from a failure, remove it. os.unlink(fifo_file) except: pass os.mkfifo(fifo_file) slow_dhcp_cmd = subprocesstest.cat_dhcp_command('slow') fifo_proc = self.startProcess( ('{0} > {1}'.format(slow_dhcp_cmd, fifo_file)), shell=True) capture_proc = self.runProcess( subprocesstest.capture_command( cmd, '-i', fifo_file, '-p', '-w', testout_file, '-a', 'duration:{}'.format(capture_duration), )) fifo_proc.kill() self.assertTrue(os.path.isfile(testout_file)) capture_returncode = capture_proc.returncode self.assertEqual(capture_returncode, 0) if (capture_returncode == 0): self.checkPacketCount(8)
def check_capture_stdin(self, cmd=None): # Similar to suite_io.check_io_4_packets. if cmd == config.cmd_wireshark and not config.canDisplay(): self.skipTest('Test requires a display.') self.assertIsNotNone(cmd) capture_file = os.path.join(config.capture_dir, 'dhcp.pcap') testout_file = self.filename_from_id(testout_pcap) slow_dhcp_cmd = subprocesstest.cat_dhcp_command('slow') capture_cmd = subprocesstest.capture_command( cmd, '-i', '-', '-w', testout_file, '-a', 'duration:{}'.format(capture_duration), shell=True) if cmd == config.cmd_wireshark: capture_cmd += ' -o console.log.level:127' pipe_proc = self.runProcess(slow_dhcp_cmd + ' | ' + capture_cmd, shell=True) pipe_returncode = pipe_proc.returncode self.assertEqual(pipe_returncode, 0) if cmd == config.cmd_wireshark: self.assertTrue(self.grepOutput('Wireshark is up and ready to go'), 'No startup message.') self.assertTrue(self.grepOutput('Capture started'), 'No capture start message.') self.assertTrue(self.grepOutput('Capture stopped'), 'No capture stop message.') self.assertTrue(os.path.isfile(testout_file)) if (pipe_returncode == 0): self.checkPacketCount(8)
def check_capture_stdin(self, cmd=None): # Similar to suite_io.check_io_4_packets. if cmd == config.cmd_wireshark and not config.canDisplay(): self.skipTest('Test requires a display.') self.assertIsNotNone(cmd) capture_file = os.path.join(config.capture_dir, 'dhcp.pcap') testout_file = self.filename_from_id(testout_pcap) slow_dhcp_cmd = subprocesstest.cat_dhcp_command('slow') capture_cmd = subprocesstest.capture_command(cmd, '-i', '-', '-w', testout_file, '-a', 'duration:{}'.format(capture_duration), shell=True ) if cmd == config.cmd_wireshark: capture_cmd += ' -o console.log.level:127' pipe_proc = self.runProcess(slow_dhcp_cmd + ' | ' + capture_cmd, env=capture_env, shell=True) pipe_returncode = pipe_proc.returncode self.assertEqual(pipe_returncode, 0) if cmd == config.cmd_wireshark: self.assertTrue(self.grepOutput('Wireshark is up and ready to go'), 'No startup message.') self.assertTrue(self.grepOutput('Capture started'), 'No capture start message.') self.assertTrue(self.grepOutput('Capture stopped'), 'No capture stop message.') self.assertTrue(os.path.isfile(testout_file)) if (pipe_returncode == 0): self.checkPacketCount(8)
def check_capture_fifo(self, cmd=None): if not config.canMkfifo(): self.skipTest('Test requires OS fifo support.') if cmd == config.cmd_wireshark and not config.canDisplay(): self.skipTest('Test requires a display.') self.assertIsNotNone(cmd) capture_file = os.path.join(config.capture_dir, 'dhcp.pcap') testout_file = self.filename_from_id(testout_pcap) fifo_file = self.filename_from_id('testout.fifo') try: # If a previous test left its fifo laying around, e.g. from a failure, remove it. os.unlink(fifo_file) except: pass os.mkfifo(fifo_file) slow_dhcp_cmd = subprocesstest.cat_dhcp_command('slow') fifo_proc = self.startProcess( ('{0} > {1}'.format(slow_dhcp_cmd, fifo_file)), shell=True) capture_proc = self.runProcess(subprocesstest.capture_command(cmd, '-i', fifo_file, '-p', '-w', testout_file, '-a', 'duration:{}'.format(capture_duration), ), env=capture_env ) fifo_proc.kill() self.assertTrue(os.path.isfile(testout_file)) capture_returncode = capture_proc.returncode self.assertEqual(capture_returncode, 0) if (capture_returncode == 0): self.checkPacketCount(8)
def check_capture_read_filter(self, cmd=None): if not config.canCapture(): self.skipTest('Test requires capture privileges and an interface.') if cmd == config.cmd_wireshark and not config.canDisplay(): self.skipTest('Test requires a display.') if not config.args_ping: self.skipTest('Your platform ({}) does not have a defined ping command.'.format(sys.platform)) self.assertIsNotNone(cmd) ping_procs = start_pinging(self) testout_file = self.filename_from_id(testout_pcap) capture_proc = self.runProcess(subprocesstest.capture_command(cmd, '-i', config.capture_interface, '-p', '-w', testout_file, '-2', '-R', 'dcerpc.cn_call_id==123456', # Something unlikely. '-c', '10', '-a', 'duration:{}'.format(capture_duration), '-f', 'icmp || icmp6', ), env=capture_env ) capture_returncode = capture_proc.returncode stop_pinging(ping_procs) self.assertEqual(capture_returncode, 0) if (capture_returncode == 0): self.checkPacketCount(0)
def check_capture_read_filter(self, cmd=None): if not config.canCapture(): self.skipTest('Test requires capture privileges and an interface.') if cmd == config.cmd_wireshark and not config.canDisplay(): self.skipTest('Test requires a display.') if not config.args_ping: self.skipTest('Your platform ({}) does not have a defined ping command.'.format(sys.platform)) self.assertIsNotNone(cmd) ping_procs = start_pinging(self) testout_file = self.filename_from_id(testout_pcap) capture_proc = self.runProcess(capture_command(cmd, '-i', config.capture_interface, '-p', '-w', testout_file, '-2', '-R', 'dcerpc.cn_call_id==123456', # Something unlikely. '-c', '10', '-a', 'duration:{}'.format(capture_duration), '-f', 'icmp || icmp6', ), env=capture_env ) capture_returncode = capture_proc.returncode stop_pinging(ping_procs) self.cleanup_files.append(testout_file) self.assertEqual(capture_returncode, 0) if (capture_returncode == 0): check_testout_num_packets(self, 0)
def check_io_4_packets(self, cmd=None, from_stdin=False, to_stdout=False): # Test direct->direct, stdin->direct, and direct->stdout file I/O. # Similar to suite_capture.check_capture_10_packets and # suite_capture.check_capture_stdin. if cmd == config.cmd_wireshark and not config.canDisplay(): self.skipTest('Test requires a display.') self.assertIsNotNone(cmd) capture_file = os.path.join(config.capture_dir, 'dhcp.pcap') testout_file = self.filename_from_id(testout_pcap) if from_stdin and to_stdout: # XXX If we support this, should we bother with separate stdin->direct # and direct->stdout tests? self.fail('Stdin and stdout not supported in the same test.') elif from_stdin: # cat -B "${CAPTURE_DIR}dhcp.pcap" | $DUT -r - -w ./testout.pcap 2>./testout.txt cat_dhcp_cmd = subprocesstest.cat_dhcp_command('cat') stdin_cmd = '{0} | "{1}" -r - -w "{2}"'.format(cat_dhcp_cmd, cmd, testout_file) io_proc = self.runProcess(stdin_cmd, shell=True) elif to_stdout: # $DUT -r "${CAPTURE_DIR}dhcp.pcap" -w - > ./testout.pcap 2>./testout.txt stdout_cmd = '"{0}" -r "{1}" -w - > "{2}"'.format(cmd, capture_file, testout_file) io_proc = self.runProcess(stdout_cmd, shell=True) else: # direct->direct # $DUT -r "${CAPTURE_DIR}dhcp.pcap" -w ./testout.pcap > ./testout.txt 2>&1 capture_file = os.path.join(config.capture_dir, 'dhcp.pcap') io_proc = self.runProcess(subprocesstest.capture_command(cmd, '-r', capture_file, '-w', testout_file, )) io_returncode = io_proc.returncode self.assertEqual(io_returncode, 0) self.assertTrue(os.path.isfile(testout_file)) if (io_returncode == 0): self.checkPacketCount(4)
def check_capture_snapshot_len(self, cmd=None): if not config.canCapture(): self.skipTest('Test requires capture privileges and an interface.') if cmd == config.cmd_wireshark and not config.canDisplay(): self.skipTest('Test requires a display.') if not config.args_ping: self.skipTest('Your platform ({}) does not have a defined ping command.'.format(sys.platform)) self.assertIsNotNone(cmd) ping_procs = start_pinging(self) testout_file = self.filename_from_id(testout_pcap) capture_proc = self.runProcess(subprocesstest.capture_command(cmd, '-i', config.capture_interface, '-p', '-w', testout_file, '-s', str(snapshot_len), '-a', 'duration:{}'.format(capture_duration), '-f', 'icmp || icmp6', )) capture_returncode = capture_proc.returncode stop_pinging(ping_procs) self.assertEqual(capture_returncode, 0) self.assertTrue(os.path.isfile(testout_file)) # Use tshark to filter out all packets larger than 68 bytes. testout2_file = self.filename_from_id('testout2.pcap') filter_proc = self.runProcess((config.cmd_tshark, '-r', testout_file, '-w', testout2_file, '-Y', 'frame.cap_len>{}'.format(snapshot_len), )) filter_returncode = filter_proc.returncode self.assertEqual(capture_returncode, 0) if (capture_returncode == 0): self.checkPacketCount(0, cap_file=testout2_file)
def check_capture_10_packets(self, cmd=None, to_stdout=False): # Similar to suite_io.check_io_4_packets. if not config.canCapture(): self.skipTest('Test requires capture privileges and an interface.') if cmd == config.cmd_wireshark and not config.canDisplay(): self.skipTest('Test requires a display.') if not config.args_ping: self.skipTest( 'Your platform ({}) does not have a defined ping command.'.format( sys.platform)) self.assertIsNotNone(cmd) testout_file = self.filename_from_id(testout_pcap) ping_procs = start_pinging(self) if to_stdout: capture_proc = self.runProcess(subprocesstest.capture_command( cmd, '-i', '"{}"'.format(config.capture_interface), '-p', '-w', '-', '-c', '10', '-a', 'duration:{}'.format(capture_duration), '-f', '"icmp || icmp6"', '>', testout_file, shell=True), env=capture_env, shell=True) else: capture_proc = self.runProcess(subprocesstest.capture_command( cmd, '-i', config.capture_interface, '-p', '-w', testout_file, '-c', '10', '-a', 'duration:{}'.format(capture_duration), '-f', 'icmp || icmp6', ), env=capture_env) capture_returncode = capture_proc.returncode stop_pinging(ping_procs) if capture_returncode != 0: self.log_fd.write('{} -D output:\n'.format(cmd)) self.runProcess((cmd, '-D')) self.assertEqual(capture_returncode, 0) if (capture_returncode == 0): self.checkPacketCount(10)
def check_capture_snapshot_len(self, cmd=None): if not config.canCapture(): self.skipTest('Test requires capture privileges and an interface.') if cmd == config.cmd_wireshark and not config.canDisplay(): self.skipTest('Test requires a display.') if not config.args_ping: self.skipTest( 'Your platform ({}) does not have a defined ping command.'.format( sys.platform)) self.assertIsNotNone(cmd) ping_procs = start_pinging(self) testout_file = self.filename_from_id(testout_pcap) capture_proc = self.runProcess( subprocesstest.capture_command( cmd, '-i', config.capture_interface, '-p', '-w', testout_file, '-s', str(snapshot_len), '-a', 'duration:{}'.format(capture_duration), '-f', 'icmp || icmp6', )) capture_returncode = capture_proc.returncode stop_pinging(ping_procs) self.assertEqual(capture_returncode, 0) self.assertTrue(os.path.isfile(testout_file)) # Use tshark to filter out all packets larger than 68 bytes. testout2_file = self.filename_from_id('testout2.pcap') filter_proc = self.runProcess(( config.cmd_tshark, '-r', testout_file, '-w', testout2_file, '-Y', 'frame.cap_len>{}'.format(snapshot_len), )) filter_returncode = filter_proc.returncode self.assertEqual(capture_returncode, 0) if (capture_returncode == 0): self.checkPacketCount(0, cap_file=testout2_file)
def check_capture_10_packets(self, cmd=None, to_stdout=False): # Similar to suite_io.check_io_4_packets. if not config.canCapture(): self.skipTest('Test requires capture privileges and an interface.') if cmd == config.cmd_wireshark and not config.canDisplay(): self.skipTest('Test requires a display.') if not config.args_ping: self.skipTest('Your platform ({}) does not have a defined ping command.'.format(sys.platform)) self.assertIsNotNone(cmd) testout_file = self.filename_from_id(testout_pcap) ping_procs = start_pinging(self) if to_stdout: capture_proc = self.runProcess(subprocesstest.capture_command(cmd, '-i', '"{}"'.format(config.capture_interface), '-p', '-w', '-', '-c', '10', '-a', 'duration:{}'.format(capture_duration), '-f', '"icmp || icmp6"', '>', testout_file, shell=True ), env=capture_env, shell=True ) else: capture_proc = self.runProcess(subprocesstest.capture_command(cmd, '-i', config.capture_interface, '-p', '-w', testout_file, '-c', '10', '-a', 'duration:{}'.format(capture_duration), '-f', 'icmp || icmp6', ), env=capture_env ) capture_returncode = capture_proc.returncode stop_pinging(ping_procs) if capture_returncode != 0: self.log_fd.write('{} -D output:\n'.format(cmd)) self.runProcess((cmd, '-D')) self.assertEqual(capture_returncode, 0) if (capture_returncode == 0): self.checkPacketCount(10)
def check_io_4_packets(self, cmd=None, from_stdin=False, to_stdout=False): # Test direct->direct, stdin->direct, and direct->stdout file I/O. # Similar to suite_capture.check_capture_10_packets and # suite_capture.check_capture_stdin. if cmd == config.cmd_wireshark and not config.canDisplay(): self.skipTest('Test requires a display.') self.assertIsNotNone(cmd) capture_file = os.path.join(config.capture_dir, 'dhcp.pcap') testout_file = self.filename_from_id(testout_pcap) if from_stdin and to_stdout: # XXX If we support this, should we bother with separate stdin->direct # and direct->stdout tests? self.fail('Stdin and stdout not supported in the same test.') elif from_stdin: # cat -B "${CAPTURE_DIR}dhcp.pcap" | $DUT -r - -w ./testout.pcap 2>./testout.txt cat_dhcp_cmd = subprocesstest.cat_dhcp_command('cat') stdin_cmd = '{0} | "{1}" -r - -w "{2}"'.format(cat_dhcp_cmd, cmd, testout_file) io_proc = self.runProcess(stdin_cmd, shell=True) elif to_stdout: # $DUT -r "${CAPTURE_DIR}dhcp.pcap" -w - > ./testout.pcap 2>./testout.txt stdout_cmd = '"{0}" -r "{1}" -w - > "{2}"'.format( cmd, capture_file, testout_file) io_proc = self.runProcess(stdout_cmd, shell=True) else: # direct->direct # $DUT -r "${CAPTURE_DIR}dhcp.pcap" -w ./testout.pcap > ./testout.txt 2>&1 capture_file = os.path.join(config.capture_dir, 'dhcp.pcap') io_proc = self.runProcess( subprocesstest.capture_command( cmd, '-r', capture_file, '-w', testout_file, )) io_returncode = io_proc.returncode self.assertEqual(io_returncode, 0) self.assertTrue(os.path.isfile(testout_file)) if (io_returncode == 0): self.checkPacketCount(4)