Skip to content

Commit b0d3386

Browse files
committed
Adding tests for new Windows detection
1 parent 31081cd commit b0d3386

File tree

1 file changed

+76
-51
lines changed

1 file changed

+76
-51
lines changed

test/os_win7.py

Lines changed: 76 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import unittest
2121
import sys
2222
import os
23-
from mock import MagicMock, patch
23+
from mock import MagicMock, patch, call
2424

2525
# Mock the winreg and _winreg module for non-windows python
2626
_winreg = MagicMock()
@@ -29,30 +29,6 @@
2929

3030
from mbed_lstools.windows import MbedLsToolsWin7
3131

32-
def _mounted_drives_check(drives):
33-
"""! Function to generate fake 'dir <drive letter>' responses
34-
@param drives Array of paths that should be considered mounted
35-
@return Returns Function that accepts an array that would be passed
36-
to _run_cli_process (ex ['dir', '<drive letter>']). Result of
37-
function mimics the result of _run_cli_process.
38-
"""
39-
def cli(cmd):
40-
for drive in drives:
41-
if drive.startswith(cmd[1]):
42-
break
43-
else:
44-
return (None,
45-
'The system cannot find the path specified.',
46-
1)
47-
48-
return (('10/17/2017 12:27 PM <DIR> .\n'
49-
'10/17/2017 12:27 PM <DIR> ..'),
50-
None,
51-
0)
52-
53-
return cli
54-
55-
5632
class Win7TestCase(unittest.TestCase):
5733
""" Basic test cases checking trivial asserts
5834
"""
@@ -78,12 +54,33 @@ def test_os_supported(self):
7854
pass
7955

8056
def test_empty_reg(self):
81-
_winreg.QueryInfoKey.return_value = (0, 0)
82-
self.lstool.find_candidates()
83-
_winreg.OpenKey.assert_called_with(_winreg.HKEY_LOCAL_MACHINE,
84-
'SYSTEM\MountedDevices')
85-
_winreg.QueryInfoKey.assert_called_with(_winreg.OpenKey.return_value)
86-
pass
57+
value_dict = {
58+
(None, 'SYSTEM\\MountedDevices'): [
59+
('\\DosDevices\\F:',
60+
u'_??_USBSTOR#Disk&Ven_MBED&Prod_VFS&Rev_0.1#0240000032044e4500367009997b00086781000097969900&0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}'.encode('utf-16le')),
61+
('\\DosDevices\\D:',
62+
u'_??_USBSTOR#Disk&Ven_SEGGER&Prod_MSD_Volume&Rev_1.00#8&1b8e102b&0&000440035522&0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}'.encode('utf-16le'))],
63+
((None, 'SYSTEM\\CurrentControlSet'), 'Services\\usbccgp\\Enum'): [
64+
('Count', 0),
65+
('NextInstance', 0)
66+
]
67+
}
68+
key_dict = {
69+
(None, 'SYSTEM\\CurrentControlSet'): ['Services\\usbccgp\\Enum'],
70+
(None, 'SYSTEM\\CurrentControlSet'): ['Enum\\USB'],
71+
((None, 'SYSTEM\\CurrentControlSet'), 'Enum\\USB'):
72+
['ROOT_HUB30', 'VID_0416&PID_511E', 'VID_0416&PID_511E&MI_00',
73+
'VID_8087&PID_0A2B', 'Vid_80EE&Pid_CAFE']
74+
}
75+
self.setUpRegistry(value_dict, key_dict)
76+
candidates = self.lstool.find_candidates()
77+
self.assertEqual(_winreg.OpenKey.mock_calls, [
78+
call(_winreg.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet'),
79+
call((_winreg.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet'), 'Enum\\USB'),
80+
call((_winreg.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet'), 'Services\\usbccgp\\Enum'),
81+
call((_winreg.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet'), 'Services\\mbedComposite\\Enum')
82+
])
83+
self.assertEqual(candidates, [])
8784

8885
def assertNoRegMut(self):
8986
"""Assert that the registry was not mutated in this test"""
@@ -128,20 +125,45 @@ def query_info_key(key):
128125
len(value_dict.get(key, [])))
129126
_winreg.QueryInfoKey.side_effect = query_info_key
130127

131-
def test_one_nucleo_dev(self):
128+
def test_one_dev(self):
132129
value_dict = {
133-
(None, 'SYSTEM\MountedDevices'): [
130+
(None, 'SYSTEM\\MountedDevices'): [
131+
('\\DosDevices\\C:', u'NOT A VALID MBED DRIVE'.encode('utf-16le')),
134132
('\\DosDevices\\F:',
135-
u'_??_USBSTOR#Disk&Ven_MBED&Prod_VFS&Rev_0.1#0240000032044e4500367009997b00086781000097969900&0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}'.encode('utf-16le')),
136-
('\\DosDevices\\D:',
137-
u'_??_USBSTOR#Disk&Ven_SEGGER&Prod_MSD_Volume&Rev_1.00#8&1b8e102b&0&000440035522&0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}'.encode('utf-16le'))],
138-
((((((None, 'SYSTEM\CurrentControlSet\Enum'), 'USB'),
139-
'VID_0416&PID_511E'), '000440035522'), 'Device Parameters'), 'PortName'): ('COM7', None)
133+
u'_??_USBSTOR#Disk&Ven_MBED&Prod_VFS&Rev_0.1#11010000442031204c364141303031313431303397969903&0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}'.encode('utf-16le'))
134+
],
135+
((None, 'SYSTEM\\CurrentControlSet'), 'Services\\usbccgp\\Enum'): [
136+
('0', 'USB\\VID_5986&PID_0706\\5&31ac2c0b&0&8'),
137+
('Count', 2),
138+
('NextInstance', 2)
139+
],
140+
((None, 'SYSTEM\\CurrentControlSet'), 'Services\\mbedComposite\\Enum'): [
141+
('0', 'USB\\VID_0D28&PID_0204\\11010000442031204c364141303031313431303397969903'),
142+
('Count', 1),
143+
('NextInstance', 1)
144+
],
145+
((((None, 'SYSTEM\\CurrentControlSet'), 'Enum\\USB'), 'VID_5986&PID_0706\\5&31ac2c0b&0&8'), 'ParentIdPrefix'): ('6&3beba67&0', None),
146+
((((None, 'SYSTEM\\CurrentControlSet'), 'Enum\\USB'), 'VID_5986&PID_0706&MI_00\\6&3beba67&0&0000'),
147+
'Service'): ('SPUVCbv', None),
148+
(((None, 'SYSTEM\\CurrentControlSet'), 'Enum\\USB'),
149+
'VID_0D28&PID_0204\\11010000442031204c364141303031313431303397969903'): [],
150+
((((None, 'SYSTEM\\CurrentControlSet'), 'Enum\\USB'),
151+
'VID_0D28&PID_0204&MI_00\\11010000442031204c364141303031313431303397969903'),
152+
'Service'): ('USBSTOR', None),
153+
((((None, 'SYSTEM\\CurrentControlSet'), 'Enum\\USB'),
154+
'VID_0D28&PID_0204&MI_01\\11010000442031204c364141303031313431303397969903'),
155+
'Device Parameters'): [('PortName', 'COM7')],
156+
(((((None, 'SYSTEM\\CurrentControlSet'), 'Enum\\USB'),
157+
'VID_0D28&PID_0204&MI_01\\11010000442031204c364141303031313431303397969903'),
158+
'Device Parameters'), 'PortName'): ('COM7', None),
159+
((((None, 'SYSTEM\\CurrentControlSet'), 'Enum\\USB'),
160+
'VID_0D28&PID_0204&MI_03\\11010000442031204c364141303031313431303397969903'),
161+
'Service'): ('HidUsb', None)
140162
}
141163
key_dict = {
142-
(None, 'SYSTEM\CurrentControlSet\Enum'): ['USB'],
143-
((((None, 'SYSTEM\CurrentControlSet\Enum'), 'USB'), 'VID_0416&PID_511E'), '000440035522'): ['Device Parameters', 'Properties'],
144-
((None, 'SYSTEM\CurrentControlSet\Enum'), 'USB'):
164+
(None, 'SYSTEM\\CurrentControlSet'): ['Services\\usbccgp\\Enum', 'Services\\mbedComposite\\Enum'],
165+
(None, 'SYSTEM\\CurrentControlSet'): ['Enum\\USB'],
166+
((None, 'SYSTEM\\CurrentControlSet'), 'Enum\\USB'):
145167
['ROOT_HUB30', 'VID_0416&PID_511E', 'VID_0416&PID_511E&MI_00',
146168
'VID_0416&PID_511E&MI_01', 'VID_046D&PID_C03D',
147169
'VID_046D&PID_C313', 'VID_046D&PID_C313&MI_00',
@@ -160,27 +182,30 @@ def test_one_nucleo_dev(self):
160182
'VID_195D&PID_2047&MI_02', 'VID_1A40&PID_0101', 'VID_1FD2&PID_5003',
161183
'VID_1FD2&PID_5003&MI_00', 'VID_1FD2&PID_5003&MI_01',
162184
'VID_413C&PID_2107', 'VID_5986&PID_0706', 'VID_5986&PID_0706&MI_00',
163-
'VID_8087&PID_0A2B', 'Vid_80EE&Pid_CAFE']
185+
'VID_8087&PID_0A2B', 'Vid_80EE&Pid_CAFE'],
186+
(((None, 'SYSTEM\\CurrentControlSet'), 'Enum\\USB'),
187+
'VID_5986&PID_0706&MI_00'): ['6&3beba67&0&0000'],
188+
(((None, 'SYSTEM\\CurrentControlSet'), 'Enum\\USB'),
189+
'VID_0D28&PID_0204&MI_00'): ['11010000442031204c364141303031313431303397969903'],
190+
(((None, 'SYSTEM\\CurrentControlSet'), 'Enum\\USB'),
191+
'VID_0D28&PID_0204&MI_01'): ['11010000442031204c364141303031313431303397969903'],
192+
((((None, 'SYSTEM\\CurrentControlSet'), 'Enum\\USB'),
193+
'VID_0D28&PID_0204&MI_01'), '11010000442031204c364141303031313431303397969903'): ['Device Parameters'],
194+
(((None, 'SYSTEM\\CurrentControlSet'), 'Enum\\USB'),
195+
'VID_0D28&PID_0204&MI_03'): ['11010000442031204c364141303031313431303397969903']
164196
}
165197
self.setUpRegistry(value_dict, key_dict)
166198

167199
with patch('mbed_lstools.windows.MbedLsToolsWin7._run_cli_process') as _cliproc:
168200
expected_info = {
169-
'mount_point': u'D:',
201+
'mount_point': u'F:',
170202
'serial_port': 'COM7',
171-
'target_id_usb_id': u'000440035522'
203+
'target_id_usb_id': u'11010000442031204c364141303031313431303397969903'
172204
}
173205

174-
_cliproc.side_effect = _mounted_drives_check(['C:', 'D:', 'F:', 'Z:'])
175206
devices = self.lstool.find_candidates()
176207
self.assertIn(expected_info, devices)
177208
self.assertNoRegMut()
178209

179-
_cliproc.side_effect = _mounted_drives_check(['C:', 'F:', 'Z:'])
180-
devices = self.lstool.find_candidates()
181-
self.assertNotIn(expected_info, devices)
182-
self.assertNoRegMut()
183-
184-
185210
if __name__ == '__main__':
186211
unittest.main()

0 commit comments

Comments
 (0)