|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2020 Kevin J. Walters |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | + |
| 23 | +import sys |
| 24 | +import os |
| 25 | + |
| 26 | +import unittest |
| 27 | +from unittest.mock import MagicMock |
| 28 | + |
| 29 | +verbose = int(os.getenv('TESTVERBOSE', '2')) |
| 30 | + |
| 31 | +# PYTHONPATH needs to be set to find adafruit_ble |
| 32 | + |
| 33 | +# Mocking library used by adafruit_ble |
| 34 | +sys.modules['_bleio'] = MagicMock() |
| 35 | + |
| 36 | +# Borrowing the dhalbert/tannewt technique from adafruit/Adafruit_CircuitPython_Motor |
| 37 | +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) |
| 38 | + |
| 39 | +# import what we are testing or will test in future |
| 40 | +# pylint: disable=unused-import,wrong-import-position |
| 41 | +from rps_advertisements import JoinGameAdvertisement, \ |
| 42 | + RpsEncDataAdvertisement, \ |
| 43 | + RpsKeyDataAdvertisement, \ |
| 44 | + RpsRoundEndAdvertisement |
| 45 | + |
| 46 | +# pylint: disable=line-too-long |
| 47 | + |
| 48 | +class Test_RpsEncDataAdvertisement(unittest.TestCase): |
| 49 | + |
| 50 | + def test_bytes_order(self): |
| 51 | + """Testing the order of data inside the manufacturer's field to ensure it follows the |
| 52 | + fields are set in. This is new behaviour to benefit prefix matching.""" |
| 53 | + |
| 54 | + rpsedad1 = RpsEncDataAdvertisement(enc_data=b"FIRST", round_no=33, sequence_number=17) |
| 55 | + |
| 56 | + # This checks value is not the old incorrect order |
| 57 | + self.assertNotEqual(bytes(rpsedad1), |
| 58 | + b"\x16\xff\x22\x08\x03\x03\x00\x11\nA\xfeFIRST\x00\x00\x00\x03C\xfe\x21", |
| 59 | + msg="Checking order of serialised data for" |
| 60 | + " ackless RpsEncDataAdvertisement does" |
| 61 | + " not follow previous incorrect order") |
| 62 | + |
| 63 | + # This check for correct order |
| 64 | + self.assertEqual(bytes(rpsedad1), |
| 65 | + b"\x16\xff\x22\x08\x0a\x41\xfeFIRST\x00\x00\x00\x03C\xfe\x21\x03\x03\x00\x11", |
| 66 | + msg="Checking order of serialised data for" |
| 67 | + " ackless RpsEncDataAdvertisement") |
| 68 | + |
| 69 | + rpsedad1.ack = 29 |
| 70 | + self.assertEqual(bytes(rpsedad1), |
| 71 | + b"\x1a\xff\x22\x08\nA\xfeFIRST\x00\x00\x00\x03C\xfe!\x03\x03\x00\x11\x03Q\xfe\x1d", |
| 72 | + msg="Checking order of serialised data for" |
| 73 | + " RpsEncDataAdvertisement with ack set post construction") |
| 74 | + |
| 75 | + |
| 76 | +class Test_RpsKeyDataAdvertisement(unittest.TestCase): |
| 77 | + |
| 78 | + def test_bytes_order(self): |
| 79 | + """Testing the order of data inside the manufacturer's field to ensure it follows the |
| 80 | + fields are set in. This is new behaviour to benefit prefix matching.""" |
| 81 | + |
| 82 | + rpskdad1 = RpsKeyDataAdvertisement(key_data=b"FIRST", round_no=33, sequence_number=17) |
| 83 | + |
| 84 | + # This checks value is not the old incorrect order |
| 85 | + self.assertNotEqual(bytes(rpskdad1), |
| 86 | + b"\x16\xff\x22\x08\x03\x03\x00\x11\nB\xfeFIRST\x00\x00\x00\x03C\xfe\x21", |
| 87 | + msg="Checking order of serialised data for" |
| 88 | + " ackless RpsKeyDataAdvertisement does" |
| 89 | + " not follow previous incorrect order") |
| 90 | + |
| 91 | + # This check for correct order |
| 92 | + self.assertEqual(bytes(rpskdad1), |
| 93 | + b"\x16\xff\x22\x08\x0a\x42\xfeFIRST\x00\x00\x00\x03C\xfe\x21\x03\x03\x00\x11", |
| 94 | + msg="Checking order of serialised data for" |
| 95 | + " ackless RpsKeyDataAdvertisement") |
| 96 | + |
| 97 | + rpskdad1.ack = 29 |
| 98 | + self.assertEqual(bytes(rpskdad1), |
| 99 | + b"\x1a\xff\x22\x08\nB\xfeFIRST\x00\x00\x00\x03C\xfe!\x03\x03\x00\x11\x03Q\xfe\x1d", |
| 100 | + msg="Checking order of serialised data for" |
| 101 | + " RpsKeyDataAdvertisement with ack set post construction") |
| 102 | + |
| 103 | + |
| 104 | +class Test_RpsRoundEndAdvertisement(unittest.TestCase): |
| 105 | + |
| 106 | + def test_bytes_order(self): |
| 107 | + """Testing the order of data inside the manufacturer's field to ensure it follows the |
| 108 | + fields are set in. This is new behaviour to benefit prefix matching.""" |
| 109 | + |
| 110 | + rpsread1 = RpsRoundEndAdvertisement(round_no=133, sequence_number=201) |
| 111 | + |
| 112 | + # This checks value is not the old incorrect order |
| 113 | + self.assertNotEqual(bytes(rpsread1), |
| 114 | + b"\x0b\xff\x22\x08\x03\x03\x00\xc9\x03C\xfe\x85", |
| 115 | + msg="Checking order of serialised data for" |
| 116 | + " ackless RpsRoundEndAdvertisement does" |
| 117 | + " not follow previous incorrect order") |
| 118 | + |
| 119 | + # This check for correct order |
| 120 | + self.assertEqual(bytes(rpsread1), |
| 121 | + b"\x0b\xff\x22\x08\x03C\xfe\x85\x03\x03\x00\xc9", |
| 122 | + msg="Checking order of serialised data for" |
| 123 | + " ackless RpsRoundEndAdvertisement") |
| 124 | + |
| 125 | + rpsread1.ack = 200 |
| 126 | + self.assertEqual(bytes(rpsread1), |
| 127 | + b"\x0f" b"\xff\x22\x08\x03C\xfe\x85\x03\x03\x00\xc9" b"\x03Q\xfe\xc8", |
| 128 | + msg="Checking order of serialised data for" |
| 129 | + " RpsRoundEndAdvertisement with ack set post construction") |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == '__main__': |
| 133 | + unittest.main(verbosity=verbose) |
0 commit comments