test_init_packet.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Copyright (c) 2015, Nordic Semiconductor
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are met:
  6. #
  7. # * Redistributions of source code must retain the above copyright notice, this
  8. # list of conditions and the following disclaimer.
  9. #
  10. # * Redistributions in binary form must reproduce the above copyright notice,
  11. # this list of conditions and the following disclaimer in the documentation
  12. # and/or other materials provided with the distribution.
  13. #
  14. # * Neither the name of Nordic Semiconductor ASA nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  22. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. import unittest
  29. from nordicsemi.dfu.init_packet import *
  30. class TestInitPacket(unittest.TestCase):
  31. def setUp(self):
  32. pass
  33. def test_generate_packet_a(self):
  34. init_packet_vars = {
  35. PacketField.DEVICE_TYPE: 1,
  36. PacketField.DEVICE_REVISION: 2,
  37. PacketField.APP_VERSION: 3,
  38. PacketField.REQUIRED_SOFTDEVICES_ARRAY: [1111, 2222, 3333, 4444],
  39. PacketField.NORDIC_PROPRIETARY_OPT_DATA_EXT_PACKET_ID: 2,
  40. PacketField.NORDIC_PROPRIETARY_OPT_DATA_FIRMWARE_HASH:
  41. "\xc9\xd3\xbfi\xf2\x1e\x88\xa01\x1e\r\xd2BSa\x12\xf8BW\x9b\xef&Z$\xbd\x02U\xfdD?u\x9e",
  42. PacketField.NORDIC_PROPRIETARY_OPT_DATA_INIT_PACKET_ECDS:
  43. '1\xd7B8\x129\xaa\xc3\xe6\x8b\xe2\x01\xd11\x17\x01\x00\xae\x1e\x04\xf9~q\xcd\xbfv"\xdan\xc0f2\xd49' +
  44. '\xdc\xc7\xf8\xae\x16VV\x17\x90\xa3\x96\xadxPa\x0bs\xfe\xbdi]\xb2\x95\x81\x99\xe4\xb0\xcf\xe9\xda'
  45. }
  46. ip = Packet(init_packet_vars)
  47. data = ip.generate_packet()
  48. self.assertEqual(data, ("\x01\x00" # Device type
  49. "\x02\x00" # Device revision
  50. "\x03\x00\x00\x00" # App version
  51. "\x04\x00" # Softdevice array length
  52. "\x57\x04" # Softdevice entry #1
  53. "\xae\x08" # Softdevice entry #2
  54. "\x05\x0d" # Softdevice entry #3
  55. "\x5c\x11" # Softdevice entry #4
  56. "\x02\x00\x00\x00" # ext packet id
  57. "\xc9\xd3\xbfi\xf2\x1e\x88\xa01\x1e\r\xd2BSa\x12" # Firmware hash, part one
  58. "\xf8BW\x9b\xef&Z$\xbd\x02U\xfdD?u\x9e" # Firmware hash, part two
  59. '1\xd7B8\x129\xaa\xc3\xe6\x8b\xe2\x01\xd11\x17\x01' # Init packet ECDS, part 1
  60. '\x00\xae\x1e\x04\xf9~q\xcd\xbfv"\xdan\xc0f2\xd49' # Init packet ECDS, part 2
  61. '\xdc\xc7\xf8\xae\x16VV\x17\x90\xa3\x96\xadxPa\x0b' # Init packet ECDS, part 3
  62. 's\xfe\xbdi]\xb2\x95\x81\x99\xe4\xb0\xcf\xe9\xda' # Init packet ECDS, part 4
  63. )
  64. )
  65. def test_generate_packet_b(self):
  66. init_packet_vars = {
  67. PacketField.DEVICE_TYPE: 1,
  68. PacketField.DEVICE_REVISION: 2,
  69. PacketField.APP_VERSION: 0xffeeffee,
  70. PacketField.REQUIRED_SOFTDEVICES_ARRAY: [1111, 2222, 3333],
  71. PacketField.NORDIC_PROPRIETARY_OPT_DATA_EXT_PACKET_ID: 1,
  72. PacketField.NORDIC_PROPRIETARY_OPT_DATA_FIRMWARE_HASH:
  73. "\xc9\xd3\xbfi\xf2\x1e\x88\xa01\x1e\r\xd2BSa\x12\xf8BW\x9b\xef&Z$\xbd\x02U\xfdD?u\x9e"
  74. }
  75. ip = Packet(init_packet_vars)
  76. data = ip.generate_packet()
  77. self.assertEqual(data, ("\x01\x00" # Device type
  78. "\x02\x00" # Device revision
  79. "\xee\xff\xee\xff" # App version
  80. "\x03\x00" # Softdevice array length
  81. "\x57\x04" # Softdevice entry #1
  82. "\xae\x08" # Softdevice entry #2
  83. "\x05\x0d" # Softdevice entry #3
  84. "\x01\x00\x00\x00" # ext packet id
  85. "\xc9\xd3\xbfi\xf2\x1e\x88\xa01\x1e\r\xd2BSa\x12" # Firmware hash, part one
  86. "\xf8BW\x9b\xef&Z$\xbd\x02U\xfdD?u\x9e" # Firmware hash, part two
  87. )
  88. )
  89. def test_generate_packet_c(self):
  90. init_packet_vars = {
  91. PacketField.DEVICE_TYPE: 1,
  92. PacketField.DEVICE_REVISION: 2,
  93. PacketField.APP_VERSION: 0xffeeffee,
  94. PacketField.REQUIRED_SOFTDEVICES_ARRAY: [1111, 2222, 3333],
  95. PacketField.NORDIC_PROPRIETARY_OPT_DATA_EXT_PACKET_ID: 0,
  96. PacketField.NORDIC_PROPRIETARY_OPT_DATA_FIRMWARE_CRC16: 0xfaae
  97. }
  98. ip = Packet(init_packet_vars)
  99. data = ip.generate_packet()
  100. self.assertEqual(data, ("\x01\x00" # Device type
  101. "\x02\x00" # Device revision
  102. "\xee\xff\xee\xff" # App version
  103. "\x03\x00" # Softdevice array length
  104. "\x57\x04" # Softdevice entry #1
  105. "\xae\x08" # Softdevice entry #2
  106. "\x05\x0d" # Softdevice entry #3
  107. "\x00\x00\x00\x00" # ext packet id
  108. "\xae\xfa" # CRC-16 checksum for firmware
  109. )
  110. )
  111. if __name__ == '__main__':
  112. unittest.main()