| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. | 2 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 # pylint: disable=protected-access | 6 # pylint: disable=protected-access |
| 7 | 7 |
| 8 """ | 8 """ |
| 9 Unit tests for the contents of find_usb_devices.py. | 9 Unit tests for the contents of find_usb_devices.py. |
| 10 | 10 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 3: Device 100 "My Test HUB" #hub 2 | 26 3: Device 100 "My Test HUB" #hub 2 |
| 27 3:4: Device 101 "My Test Internal HUB" #internal section of hub 2 | 27 3:4: Device 101 "My Test Internal HUB" #internal section of hub 2 |
| 28 3:4:4: Device 102 "battor_p1_h2_t4" #physical port 1 on hub 2, on ttyusb4 | 28 3:4:4: Device 102 "battor_p1_h2_t4" #physical port 1 on hub 2, on ttyusb4 |
| 29 """ | 29 """ |
| 30 | 30 |
| 31 import logging | 31 import logging |
| 32 import unittest | 32 import unittest |
| 33 | 33 |
| 34 from devil import devil_env | 34 from devil import devil_env |
| 35 from devil.utils import find_usb_devices | 35 from devil.utils import find_usb_devices |
| 36 from devil.utils import usb_hubs |
| 37 from devil.utils import lsusb |
| 36 with devil_env.SysPath(devil_env.PYMOCK_PATH): | 38 with devil_env.SysPath(devil_env.PYMOCK_PATH): |
| 37 import mock # pylint: disable=import-error | 39 import mock # pylint: disable=import-error |
| 38 | 40 |
| 39 # Output of lsusb.lsusb(). | 41 # Output of lsusb.lsusb(). |
| 40 # We just test that the dictionary is working by creating an | 42 # We just test that the dictionary is working by creating an |
| 41 # "ID number" equal to (bus_num*1000)+device_num and seeing if | 43 # "ID number" equal to (bus_num*1000)+device_num and seeing if |
| 42 # it is picked up correctly. Also we test the description | 44 # it is picked up correctly. Also we test the description |
| 43 | 45 |
| 44 DEVLIST = [(1, 11, 'foo'), | 46 DEVLIST = [(1, 11, 'foo'), |
| 45 (1, 12, 'bar'), | 47 (1, 12, 'bar'), |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 S: SerialNumber=Battor2 | 87 S: SerialNumber=Battor2 |
| 86 T: Bus=02 Lev=00 Prnt=23 Port=03 Cnt=00 Dev#= 25 Spd=000 MxCh=00 | 88 T: Bus=02 Lev=00 Prnt=23 Port=03 Cnt=00 Dev#= 25 Spd=000 MxCh=00 |
| 87 S: SerialNumber=Battor3 | 89 S: SerialNumber=Battor3 |
| 88 T: Bus=02 Lev=00 Prnt=23 Port=02 Cnt=00 Dev#= 26 Spd=000 MxCh=00 | 90 T: Bus=02 Lev=00 Prnt=23 Port=02 Cnt=00 Dev#= 26 Spd=000 MxCh=00 |
| 89 | 91 |
| 90 T: Bus=02 Lev=00 Prnt=00 Port=02 Cnt=00 Dev#=100 Spd=000 MxCh=00 | 92 T: Bus=02 Lev=00 Prnt=00 Port=02 Cnt=00 Dev#=100 Spd=000 MxCh=00 |
| 91 T: Bus=02 Lev=00 Prnt=100 Port=03 Cnt=00 Dev#=101 Spd=000 MxCh=00 | 93 T: Bus=02 Lev=00 Prnt=100 Port=03 Cnt=00 Dev#=101 Spd=000 MxCh=00 |
| 92 T: Bus=02 Lev=00 Prnt=101 Port=03 Cnt=00 Dev#=102 Spd=000 MxCh=00 | 94 T: Bus=02 Lev=00 Prnt=101 Port=03 Cnt=00 Dev#=102 Spd=000 MxCh=00 |
| 93 ''' | 95 ''' |
| 94 | 96 |
| 97 RAW_LSUSB_OUTPUT = ''' |
| 98 Bus 001 Device 011: FAST foo |
| 99 Bus 001 Device 012: FAST bar |
| 100 Bus 001 Device 013: baz |
| 101 Bus 002 Device 011: quux |
| 102 Bus 002 Device 020: My Test HUB |
| 103 Bus 002 Device 021: Future Technology Devices International battor_p7_h1_t0 |
| 104 Bus 002 Device 022: Future Technology Devices International battor_p5_h1_t1 |
| 105 Bus 002 Device 023: My Test Internal HUB |
| 106 Bus 002 Device 024: Future Technology Devices International battor_p3_h1_t2 |
| 107 Bus 002 Device 025: Future Technology Devices International battor_p1_h1_t3 |
| 108 Bus 002 Device 026: Not a Battery Monitor |
| 109 Bus 002 Device 100: My Test HUB |
| 110 Bus 002 Device 101: My Test Internal HUB |
| 111 Bus 002 Device 102: Future Technology Devices International battor_p1_h1_t4 |
| 112 ''' |
| 113 |
| 95 LIST_TTY_OUTPUT = ''' | 114 LIST_TTY_OUTPUT = ''' |
| 96 ttyUSB0 | 115 ttyUSB0 |
| 97 Something-else-0 | 116 Something-else-0 |
| 98 ttyUSB1 | 117 ttyUSB1 |
| 99 ttyUSB2 | 118 ttyUSB2 |
| 100 Something-else-1 | 119 Something-else-1 |
| 101 ttyUSB3 | 120 ttyUSB3 |
| 102 ttyUSB4 | 121 ttyUSB4 |
| 103 Something-else-2 | 122 Something-else-2 |
| 104 ttyUSB5 | 123 ttyUSB5 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 with another 4-port hub connected on port 4. | 184 with another 4-port hub connected on port 4. |
| 166 """ | 185 """ |
| 167 if not isinstance(node, find_usb_devices.USBDeviceNode): | 186 if not isinstance(node, find_usb_devices.USBDeviceNode): |
| 168 return False | 187 return False |
| 169 if 'Test HUB' not in node.desc: | 188 if 'Test HUB' not in node.desc: |
| 170 return False | 189 return False |
| 171 if not node.HasPort(4): | 190 if not node.HasPort(4): |
| 172 return False | 191 return False |
| 173 return 'Test Internal HUB' in node.PortToDevice(4).desc | 192 return 'Test Internal HUB' in node.PortToDevice(4).desc |
| 174 | 193 |
| 175 TEST_HUB = find_usb_devices.HubType(isTestHub, | 194 TEST_HUB = usb_hubs.HubType(isTestHub, |
| 176 {1:7, | 195 {1:7, |
| 177 2:6, | 196 2:6, |
| 178 3:5, | 197 3:5, |
| 179 4:{1:4, 2:3, 3:2, 4:1}}) | 198 4:{1:4, 2:3, 3:2, 4:1}}) |
| 180 | 199 |
| 181 class USBScriptTest(unittest.TestCase): | 200 class USBScriptTest(unittest.TestCase): |
| 182 def setUp(self): | 201 def setUp(self): |
| 183 find_usb_devices._GetTtyUSBInfo = mock.Mock( | 202 find_usb_devices._GetTtyUSBInfo = mock.Mock( |
| 184 side_effect=lambda x: UDEVADM_OUTPUT_DICT[x]) | 203 side_effect=lambda x: UDEVADM_OUTPUT_DICT[x]) |
| 185 find_usb_devices._GetParsedLSUSBOutput = mock.Mock( | 204 find_usb_devices._GetParsedLSUSBOutput = mock.Mock( |
| 186 return_value=LSUSB_OUTPUT) | 205 return_value=LSUSB_OUTPUT) |
| 187 find_usb_devices._GetUSBDevicesOutput = mock.Mock( | 206 find_usb_devices._GetUSBDevicesOutput = mock.Mock( |
| 188 return_value=USB_DEVICES_OUTPUT) | 207 return_value=USB_DEVICES_OUTPUT) |
| 189 find_usb_devices._GetCommList = mock.Mock( | 208 find_usb_devices._GetCommList = mock.Mock( |
| 190 return_value=LIST_TTY_OUTPUT) | 209 return_value=LIST_TTY_OUTPUT) |
| 210 lsusb.raw_lsusb = mock.Mock( |
| 211 return_value=RAW_LSUSB_OUTPUT) |
| 191 | 212 |
| 192 def testIsBattor(self): | 213 def testIsBattor(self): |
| 193 bd = find_usb_devices.GetBusNumberToDeviceTreeMap() | 214 bd = find_usb_devices.GetBusNumberToDeviceTreeMap() |
| 194 self.assertTrue(find_usb_devices.IsBattor('ttyUSB3', bd)) | 215 self.assertTrue(find_usb_devices.IsBattor('ttyUSB3', bd)) |
| 195 self.assertFalse(find_usb_devices.IsBattor('ttyUSB5', bd)) | 216 self.assertFalse(find_usb_devices.IsBattor('ttyUSB5', bd)) |
| 196 | 217 |
| 197 def testGetBattors(self): | 218 def testGetBattors(self): |
| 198 bd = find_usb_devices.GetBusNumberToDeviceTreeMap() | 219 bd = find_usb_devices.GetBusNumberToDeviceTreeMap() |
| 199 self.assertEquals(find_usb_devices.GetBattorList(bd), | 220 self.assertEquals(find_usb_devices.GetBattorList(bd), |
| 200 ['ttyUSB0', 'ttyUSB1', 'ttyUSB2', | 221 ['ttyUSB0', 'ttyUSB1', 'ttyUSB2', |
| (...skipping 21 matching lines...) Expand all Loading... |
| 222 | 243 |
| 223 def testGetSerialMapping(self): | 244 def testGetSerialMapping(self): |
| 224 pp = find_usb_devices.GetAllPhysicalPortToSerialMaps([TEST_HUB]) | 245 pp = find_usb_devices.GetAllPhysicalPortToSerialMaps([TEST_HUB]) |
| 225 result = list(pp) | 246 result = list(pp) |
| 226 self.assertEquals(result[0], {7:'Battor0', | 247 self.assertEquals(result[0], {7:'Battor0', |
| 227 5:'Battor1', | 248 5:'Battor1', |
| 228 3:'Battor2', | 249 3:'Battor2', |
| 229 1:'Battor3'}) | 250 1:'Battor3'}) |
| 230 self.assertEquals(result[1], {}) | 251 self.assertEquals(result[1], {}) |
| 231 | 252 |
| 253 def testFastDeviceDescriptions(self): |
| 254 bd = find_usb_devices.GetBusNumberToDeviceTreeMap() |
| 255 dev_foo = bd[1].FindDeviceNumber(11) |
| 256 dev_bar = bd[1].FindDeviceNumber(12) |
| 257 dev_battor_p7_h1_t0 = bd[2].FindDeviceNumber(21) |
| 258 self.assertEquals(dev_foo.desc, 'FAST foo') |
| 259 self.assertEquals(dev_bar.desc, 'FAST bar') |
| 260 self.assertEquals(dev_battor_p7_h1_t0.desc, |
| 261 'Future Technology Devices International battor_p7_h1_t0') |
| 262 |
| 232 def testDeviceDescriptions(self): | 263 def testDeviceDescriptions(self): |
| 233 bd = find_usb_devices.GetBusNumberToDeviceTreeMap() | 264 bd = find_usb_devices.GetBusNumberToDeviceTreeMap(fast=False) |
| 234 dev_foo = bd[1].FindDeviceNumber(11) | 265 dev_foo = bd[1].FindDeviceNumber(11) |
| 235 dev_bar = bd[1].FindDeviceNumber(12) | 266 dev_bar = bd[1].FindDeviceNumber(12) |
| 236 dev_battor_p7_h1_t0 = bd[2].FindDeviceNumber(21) | 267 dev_battor_p7_h1_t0 = bd[2].FindDeviceNumber(21) |
| 237 self.assertEquals(dev_foo.desc, 'foo') | 268 self.assertEquals(dev_foo.desc, 'foo') |
| 238 self.assertEquals(dev_bar.desc, 'bar') | 269 self.assertEquals(dev_bar.desc, 'bar') |
| 239 self.assertEquals(dev_battor_p7_h1_t0.desc, | 270 self.assertEquals(dev_battor_p7_h1_t0.desc, |
| 240 'Future Technology Devices International battor_p7_h1_t0') | 271 'Future Technology Devices International battor_p7_h1_t0') |
| 241 | 272 |
| 242 def testDeviceInformation(self): | 273 def testDeviceInformation(self): |
| 243 bd = find_usb_devices.GetBusNumberToDeviceTreeMap() | 274 bd = find_usb_devices.GetBusNumberToDeviceTreeMap(fast=False) |
| 244 dev_foo = bd[1].FindDeviceNumber(11) | 275 dev_foo = bd[1].FindDeviceNumber(11) |
| 245 dev_bar = bd[1].FindDeviceNumber(12) | 276 dev_bar = bd[1].FindDeviceNumber(12) |
| 246 dev_battor_p7_h1_t0 = bd[2].FindDeviceNumber(21) | 277 dev_battor_p7_h1_t0 = bd[2].FindDeviceNumber(21) |
| 247 self.assertEquals(dev_foo.info['id'], 1011) | 278 self.assertEquals(dev_foo.info['id'], 1011) |
| 248 self.assertEquals(dev_bar.info['id'], 1012) | 279 self.assertEquals(dev_bar.info['id'], 1012) |
| 249 self.assertEquals(dev_battor_p7_h1_t0.info['id'], 2021) | 280 self.assertEquals(dev_battor_p7_h1_t0.info['id'], 2021) |
| 250 | 281 |
| 251 def testSerialNumber(self): | 282 def testSerialNumber(self): |
| 252 bd = find_usb_devices.GetBusNumberToDeviceTreeMap() | 283 bd = find_usb_devices.GetBusNumberToDeviceTreeMap(fast=False) |
| 253 dev_foo = bd[1].FindDeviceNumber(11) | 284 dev_foo = bd[1].FindDeviceNumber(11) |
| 254 dev_bar = bd[1].FindDeviceNumber(12) | 285 dev_bar = bd[1].FindDeviceNumber(12) |
| 255 dev_battor_p7_h1_t0 = bd[2].FindDeviceNumber(21) | 286 dev_battor_p7_h1_t0 = bd[2].FindDeviceNumber(21) |
| 256 self.assertEquals(dev_foo.serial, 'FooSerial') | 287 self.assertEquals(dev_foo.serial, 'FooSerial') |
| 257 self.assertEquals(dev_bar.serial, 'BarSerial') | 288 self.assertEquals(dev_bar.serial, 'BarSerial') |
| 258 self.assertEquals(dev_battor_p7_h1_t0.serial, 'Battor0') | 289 self.assertEquals(dev_battor_p7_h1_t0.serial, 'Battor0') |
| 259 | 290 |
| 291 def testBattorDictMapping(self): |
| 292 map_dict = {'Phone1':'Battor1', 'Phone2':'Battor2', 'Phone3':'Battor3'} |
| 293 a1 = find_usb_devices.GetBattorPathFromPhoneSerial('Phone1', |
| 294 serial_map=map_dict) |
| 295 a2 = find_usb_devices.GetBattorPathFromPhoneSerial('Phone2', |
| 296 serial_map=map_dict) |
| 297 a3 = find_usb_devices.GetBattorPathFromPhoneSerial('Phone3', |
| 298 serial_map=map_dict) |
| 299 self.assertEquals(a1, '/dev/ttyUSB1') |
| 300 self.assertEquals(a2, '/dev/ttyUSB2') |
| 301 self.assertEquals(a3, '/dev/ttyUSB3') |
| 302 |
| 303 def testReadSerialMapFile(self): |
| 304 map_dict = find_usb_devices.ReadSerialMapFile( |
| 305 'testdata/test_serial_map.json') |
| 306 self.assertEquals(len(map_dict.keys()), 3) |
| 307 self.assertEquals(map_dict['Phone1'], 'Battor1') |
| 308 self.assertEquals(map_dict['Phone2'], 'Battor2') |
| 309 self.assertEquals(map_dict['Phone3'], 'Battor3') |
| 310 |
| 311 original_PPTSM = find_usb_devices.GetAllPhysicalPortToSerialMaps |
| 312 original_PPTTM = find_usb_devices.GetAllPhysicalPortToTTYMaps |
| 313 original_GBL = find_usb_devices.GetBattorList |
| 314 original_GBNDM = find_usb_devices.GetBusNumberToDeviceTreeMap |
| 315 original_IB = find_usb_devices.IsBattor |
| 316 original_GBSM = find_usb_devices.GetBattorSerialNumbers |
| 317 |
| 318 def setup_battor_test(serial, tty, battor, bser=None): |
| 319 serial_mapper = mock.Mock(return_value=serial) |
| 320 tty_mapper = mock.Mock(return_value=tty) |
| 321 battor_lister = mock.Mock(return_value=battor) |
| 322 devtree = mock.Mock(return_value=None) |
| 323 is_battor = mock.Mock(side_effect=lambda x, y: x in battor) |
| 324 battor_serials = mock.Mock(return_value=bser) |
| 325 find_usb_devices.GetAllPhysicalPortToSerialMaps = serial_mapper |
| 326 find_usb_devices.GetAllPhysicalPortToTTYMaps = tty_mapper |
| 327 find_usb_devices.GetBattorList = battor_lister |
| 328 find_usb_devices.GetBusNumberToDeviceTreeMap = devtree |
| 329 find_usb_devices.IsBattor = is_battor |
| 330 find_usb_devices.GetBattorSerialNumbers = battor_serials |
| 331 |
| 332 class BattorMappingTest(unittest.TestCase): |
| 333 def tearDown(self): |
| 334 find_usb_devices.GetAllPhysicalPortToSerialMaps = original_PPTSM |
| 335 find_usb_devices.GetAllPhysicalPortToTTYMaps = original_PPTTM |
| 336 find_usb_devices.GetBattorList = original_GBL |
| 337 find_usb_devices.GetBusNumberToDeviceTreeMap = original_GBNDM |
| 338 find_usb_devices.IsBattor = original_IB |
| 339 find_usb_devices.GetBattorSerialNumbers = original_GBSM |
| 340 |
| 341 def test_generate_serial_map(self): |
| 342 setup_battor_test([{1:'Phn1', 2:'Phn2', 3:'Phn3'}, |
| 343 {1:'Bat1', 2:'Bat2', 3:'Bat3'}], |
| 344 [{}, |
| 345 {1:'ttyUSB0', 2:'ttyUSB1', 3:'ttyUSB2'}], |
| 346 ['ttyUSB0', 'ttyUSB1', 'ttyUSB2'], |
| 347 ['Bat1', 'Bat2', 'Bat3']) |
| 348 result = find_usb_devices.GenerateSerialMap() |
| 349 self.assertEqual(len(result), 3) |
| 350 self.assertEqual(result['Phn1'], 'Bat1') |
| 351 self.assertEqual(result['Phn2'], 'Bat2') |
| 352 self.assertEqual(result['Phn3'], 'Bat3') |
| 353 |
| 354 |
| 260 if __name__ == "__main__": | 355 if __name__ == "__main__": |
| 261 logging.getLogger().setLevel(logging.DEBUG) | 356 logging.getLogger().setLevel(logging.DEBUG) |
| 262 unittest.main(verbosity=2) | 357 unittest.main(verbosity=2) |
| OLD | NEW |