1% Regression tests for isotpscanner 2~ vcan_socket needs_root linux not_pypy automotive_comm scanner 3 4 5+ Configuration 6~ conf 7 8= Imports 9with open(scapy_path("test/contrib/automotive/interface_mockup.py")) as f: 10 exec(f.read()) 11 12ISOTPSocket = ISOTPSoftSocket 13 14from unittest.mock import patch 15 16+ Usage tests 17 18= Test wrong usage 19 20result = subprocess.Popen([sys.executable, "scapy/tools/automotive/isotpscanner.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) 21 22std_out, std_err = result.communicate() 23if result.returncode: 24 print(std_out) 25 print(std_err) 26 27assert result.returncode != 0 28 29expected_output = plain_str(b'usage:') 30assert expected_output in plain_str(std_err) 31 32 33= Test show help 34 35result = subprocess.Popen([sys.executable, "scapy/tools/automotive/isotpscanner.py", "--help"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) 36std_out, _ = result.communicate() 37expected_output = plain_str(b'Scan for open ISOTP-Sockets.') 38 39assert result.wait() == 0 40assert expected_output in plain_str(std_out) 41 42 43= Test Python2 call 44 45result = subprocess.Popen([sys.executable, "scapy/tools/automotive/isotpscanner.py", "-i", "socketcan", "-c", iface0, "-s", "0x600", "-e", "0x600", "-v", "-n", "0", "-t", "0"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) 46returncode = result.wait() 47expected_output = plain_str(b'Start scan') 48std_out, std_err = result.communicate() 49 50assert returncode == 0 51assert expected_output in plain_str(std_out) 52 53= Test Python2 call with one python-can arg 54 55result = subprocess.Popen([sys.executable, "scapy/tools/automotive/isotpscanner.py", "-i", "socketcan", "-c", iface0, "-a", "bitrate=500000", "-s", "0x600", "-e", "0x600", "-v", "-n", "0", "-t", "0"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) 56returncode = result.wait() 57expected_output = plain_str(b'Start scan') 58std_out, std_err = result.communicate() 59 60assert returncode == 0 61assert expected_output in plain_str(std_out) 62 63 64= Test Python2 call with multiple python-can args 65 66result = subprocess.Popen([sys.executable, "scapy/tools/automotive/isotpscanner.py", "-i", "socketcan", "-c", iface0, "-a", "bitrate=500000 receive_own_messages=True", "-s", "0x600", "-e", "0x600", "-v", "-n", "0", "-t", "0"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) 67returncode = result.wait() 68expected_output = plain_str(b'Start scan') 69std_out, std_err = result.communicate() 70 71assert returncode == 0 72assert expected_output in plain_str(std_out) 73 74= Test Python2 call with multiple python-can args 2 75 76result = subprocess.Popen([sys.executable, "scapy/tools/automotive/isotpscanner.py", "-i", "socketcan", "-c", iface0, "--python-can_args", "bitrate=500000 receive_own_messages=True", "-s", "0x600", "-e", "0x600", "-v", "-n", "0", "-t", "0"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) 77returncode = result.wait() 78expected_output = plain_str(b'Start scan') 79std_out, std_err = result.communicate() 80 81assert returncode == 0 82assert expected_output in plain_str(std_out) 83 84 85+ Scan tests 86 87= Test standard scan 88 89exit_if_no_isotp_module() 90 91drain_bus(iface0) 92 93recv_result = subprocess.Popen(("isotprecv -s 700 -d 600 -l " + iface0).split()) 94result = subprocess.Popen([sys.executable, "scapy/tools/automotive/isotpscanner.py"] + can_socket_string_list + ["-s", "0x600", "-e", "0x600"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) 95returncode1 = result.wait() 96std_out1, std_err1 = result.communicate() 97recv_result.terminate() 98 99print(std_out1) 100print(std_err1) 101 102assert returncode1 == 0 103 104expected_output = [b'0x600', b'0x700'] 105for out in expected_output: 106 assert plain_str(out) in plain_str(std_out1) 107 108 109= Test extended scan 110 111def isotp_scan(sock, # type: SuperSocket 112 scan_range=range(0x7ff + 1), # type: Iterable[int] 113 extended_addressing=False, # type: bool 114 extended_scan_range=range(0x100), # type: Iterable[int] 115 noise_listen_time=2, # type: int 116 sniff_time=0.1, # type: float 117 output_format=None, # type: Optional[str] 118 can_interface=None, # type: Optional[str] 119 extended_can_id=False, # type: bool 120 verbose=False # type: bool 121 ): 122 # type: (...) -> Union[str, List[SuperSocket]] 123 assert sock is not None 124 assert 0x601 in scan_range 125 assert 0x602 not in scan_range 126 assert extended_addressing == True 127 assert 0 in extended_scan_range 128 assert 0xff in extended_scan_range 129 assert output_format == "text" 130 assert verbose == False 131 assert extended_can_id == False 132 assert "vcan0" in can_interface 133 return "Success" 134 135testargs = ["scapy/tools/automotive/isotpscanner.py"] + can_socket_string_list + ["-s", "0x601", "-e", "0x601", "-x"] 136with patch.object(sys, "argv", testargs), patch.object(scapy.contrib.isotp, "isotp_scan", isotp_scan): 137 from scapy.tools.automotive.isotpscanner import main 138 main() 139 140 141= Test scan with piso flag 142 143def isotp_scan(sock, # type: SuperSocket 144 scan_range=range(0x7ff + 1), # type: Iterable[int] 145 extended_addressing=False, # type: bool 146 extended_scan_range=range(0x100), # type: Iterable[int] 147 noise_listen_time=2, # type: int 148 sniff_time=0.1, # type: float 149 output_format=None, # type: Optional[str] 150 can_interface=None, # type: Optional[str] 151 extended_can_id=False, # type: bool 152 verbose=False # type: bool 153 ): 154 # type: (...) -> Union[str, List[SuperSocket]] 155 assert sock is not None 156 assert 0x601 in scan_range 157 assert 0x602 not in scan_range 158 assert extended_addressing == True 159 assert 0 in extended_scan_range 160 assert 0xff in extended_scan_range 161 assert output_format == "code" 162 assert verbose == False 163 assert extended_can_id == False 164 assert "vcan0" in can_interface 165 return "Success" 166 167testargs = ["scapy/tools/automotive/isotpscanner.py"] + can_socket_string_list + ["-s", "0x601", "-e", "0x601", "-x", "-C"] 168with patch.object(sys, "argv", testargs), patch.object(scapy.contrib.isotp, "isotp_scan", isotp_scan): 169 from scapy.tools.automotive.isotpscanner import main 170 main() 171 172 173+ Cleanup 174 175= Delete vcan interfaces 176 177assert cleanup_interfaces() 178