1#!/usr/bin/python2
2# Copyright 2017 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import unittest
7
8import power_cycle_usb_util
9
10
11class PowerCycleUsbUtilTest(unittest.TestCase):
12    """Unittest for the parse functions within power_cycle_usb_util.py."""
13
14    VID = '0001'
15    PID = '0001'
16    BUS = 1
17    DEV = 2
18
19    LSUSB_DEVICE_OUTPUT = 'Bus 001 Device 002:  ID 0001:0001\n'
20    LSUSB_DEVICE_OUTPUT_NONE = ''
21    LSUSB_DEVICE_OUTPUT_MULTI = ('Bus 001 Device 002:  ID 0001:0001\n'
22                                 'Bus 001 Device 002:  ID 0001:0001\n')
23
24    LSUSB_TREE_OUTPUT = \
25    ('/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 5000M\n'
26     '    |__ Port 3: Dev 2, If 0, Class=Hub, Driver=hub/4p, 5000M\n'
27     '/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/11p, 480M\n'
28     '    |__ Port 2: Dev 52, If 0, Class=Hub, Driver=hub/4p, 480M\n'
29     '        |__ Port 1: Dev 2, If 0, Class=Human Interface Device,'
30     'Driver=usbhid, 12M\n'
31     '        |__ Port 3: Dev 54, If 0, Class=Vendor Specific Class,'
32     'Driver=udl, 480M\n'
33     '    |__ Port 3: Dev 3, If 0, Class=Hub, Driver=hub/4p, 480M\n'
34     '    |__ Port 4: Dev 4, If 0, Class=Wireless, Driver=btusb, 12M\n'
35     '    |__ Port 4: Dev 4, If 1, Class=Wireless, Driver=btusb, 12M\n')
36
37    def test_get_bus_dev_id(self):
38        want = (self.BUS, self.DEV)
39        want_none = (None, None)
40        want_multi = (None, None)
41
42        bus, dev = power_cycle_usb_util.get_bus_dev_id(
43            self.LSUSB_DEVICE_OUTPUT, self.VID, self.PID)
44        self.assertEqual((bus, dev), want)
45        bus, dev = power_cycle_usb_util.get_bus_dev_id(
46            self.LSUSB_DEVICE_OUTPUT_NONE, self.VID, self.PID)
47        self.assertEqual((bus, dev), want_none)
48        bus, dev = power_cycle_usb_util.get_bus_dev_id(
49            self.LSUSB_DEVICE_OUTPUT_MULTI, self.VID, self.PID)
50        self.assertEqual((bus, dev), want_multi)
51
52    def test_get_port_number(self):
53        want = 2
54
55        port = power_cycle_usb_util.get_port_number(
56            self.LSUSB_TREE_OUTPUT, self.BUS, self.DEV)
57        self.assertEqual(port, want)
58
59
60if __name__ == '__main__':
61  unittest.main()
62