|
| 1 | +""" |
| 2 | +mbed SDK |
| 3 | +Copyright (c) 2017 ARM Limited |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +""" |
| 17 | + |
| 18 | +import unittest |
| 19 | +from mock import patch |
| 20 | +from tools.detect_targets import get_interface_version |
| 21 | + |
| 22 | + |
| 23 | +class MbedLsToolsMock(): |
| 24 | + """ |
| 25 | + Mock of mbedls tools |
| 26 | + """ |
| 27 | + |
| 28 | + def __init__(self, test_type): |
| 29 | + self.interface_test_type = test_type |
| 30 | + |
| 31 | + def get_details_txt(self, mount_point): |
| 32 | + return self.details_txt_types[self.interface_test_type]; |
| 33 | + |
| 34 | + # Static details.txt types. |
| 35 | + details_txt_types = { |
| 36 | + 'details_valid_interface_version' : { |
| 37 | + 'Unique ID': '0226000029164e45002f0012706e0006f301000097969900', |
| 38 | + 'HIF ID': '97969900', |
| 39 | + 'Auto Reset': '0', |
| 40 | + 'Automation allowed': '0', |
| 41 | + 'Daplink Mode': 'Interface', |
| 42 | + 'Interface Version': '0240', |
| 43 | + 'Git SHA': 'c765cbb590f57598756683254ca38b211693ae5e', |
| 44 | + 'Local Mods': '0', |
| 45 | + 'USB Interfaces': 'MSD, CDC, HID', |
| 46 | + 'Interface CRC': '0x26764ebf' |
| 47 | + }, |
| 48 | + 'details_valid_version' : { |
| 49 | + 'Version': '0226', |
| 50 | + 'Build': 'Aug 24 2015 17:06:30', |
| 51 | + 'Git Commit SHA': '27a236b9fe39c674a703c5c89655fbd26b8e27e1', |
| 52 | + 'Git Local mods': 'Yes' |
| 53 | + }, |
| 54 | + 'details_missing_interface_version' : { |
| 55 | + 'Unique ID': '0226000033514e450044500585d4001de981000097969900', |
| 56 | + 'HIC ID': '97969900', |
| 57 | + 'Auto Reset': '0', |
| 58 | + 'Automation allowed': '0', |
| 59 | + 'Overflow detection': '0', |
| 60 | + 'Daplink Mode': 'Interface', |
| 61 | + 'Git SHA': 'b403a07e3696cee1e116d44cbdd64446e056ce38', |
| 62 | + 'Local Mods': '0', |
| 63 | + 'USB Interfaces': 'MSD, CDC, HID', |
| 64 | + 'Interface CRC': '0x4d98bf7e', |
| 65 | + 'Remount count': '0' |
| 66 | + }, |
| 67 | + 'details_invalid_none' : None |
| 68 | + } |
| 69 | + |
| 70 | +""" |
| 71 | +Tests for detect_targets.py |
| 72 | +""" |
| 73 | + |
| 74 | +class DetectTargetsTest(unittest.TestCase): |
| 75 | + """ |
| 76 | + Test cases for Detect Target functionality |
| 77 | + """ |
| 78 | + |
| 79 | + def setUp(self): |
| 80 | + """ |
| 81 | + Called before each test case |
| 82 | +
|
| 83 | + :return: |
| 84 | + """ |
| 85 | + self.missing_mount_point = None |
| 86 | + self.mount_point = "D:" |
| 87 | + |
| 88 | + def tearDown(self): |
| 89 | + """ |
| 90 | + Nothing to tear down. |
| 91 | + Called after each test case |
| 92 | +
|
| 93 | + :return: |
| 94 | + """ |
| 95 | + pass |
| 96 | + |
| 97 | + @patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_valid_interface_version')) |
| 98 | + def test_interface_version_valid(self, mbed_lstools_mock): |
| 99 | + """ |
| 100 | + Test that checks function returns correctly when given a valid Interface Version |
| 101 | + |
| 102 | + :param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock |
| 103 | + :return |
| 104 | + """ |
| 105 | + |
| 106 | + interface_version = get_interface_version(self.mount_point) |
| 107 | + assert interface_version == '0240' |
| 108 | + |
| 109 | + @patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_valid_version')) |
| 110 | + def test_version_valid(self, mbed_lstools_mock): |
| 111 | + """ |
| 112 | + Test that checks function returns correctly when given a valid Version |
| 113 | + |
| 114 | + :param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock |
| 115 | + :return |
| 116 | + """ |
| 117 | + |
| 118 | + interface_version = get_interface_version(self.mount_point) |
| 119 | + assert interface_version == '0226' |
| 120 | + |
| 121 | + @patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_missing_interface_version')) |
| 122 | + def test_interface_version_missing_interface_version(self, mbed_lstools_mock): |
| 123 | + """ |
| 124 | + Test that checks function returns correctly when DETAILS.txt is present |
| 125 | + but an interface version is not listed. |
| 126 | + |
| 127 | + :param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock |
| 128 | + :return |
| 129 | + """ |
| 130 | + |
| 131 | + interface_version = get_interface_version(self.mount_point) |
| 132 | + assert interface_version == 'unknown' |
| 133 | + |
| 134 | + @patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_invalid_none')) |
| 135 | + def test_version_none(self, mbed_lstools_mock): |
| 136 | + """ |
| 137 | + Test that checks function returns correctly when a valid mount point is supplied |
| 138 | + but DETAILS.txt is not present. |
| 139 | + |
| 140 | + :param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock |
| 141 | + :return |
| 142 | + """ |
| 143 | + |
| 144 | + interface_version = get_interface_version(self.mount_point) |
| 145 | + assert interface_version == 'unknown' |
| 146 | + |
| 147 | + @patch("mbed_lstools.create", return_value=MbedLsToolsMock('details_invalid_none')) |
| 148 | + def test_interface_version_missing_mount_point(self, mbed_lstools_mock): |
| 149 | + """ |
| 150 | + Test that checks function returns correctly when no mount point is supplied. |
| 151 | + |
| 152 | + :param mbed_lstools_mock: Mocks Mbed LS tools with MbedLsToolsMock |
| 153 | + :return |
| 154 | + """ |
| 155 | + |
| 156 | + interface_version = get_interface_version(self.missing_mount_point) |
| 157 | + assert interface_version == 'unknown' |
| 158 | + |
| 159 | +if __name__ == '__main__': |
| 160 | + unittest.main() |
0 commit comments