test_package.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 json
  29. import os
  30. import tempfile
  31. import unittest
  32. from zipfile import ZipFile
  33. import shutil
  34. from nordicsemi.dfu.package import Package
  35. class TestPackage(unittest.TestCase):
  36. def setUp(self):
  37. self.work_directory = tempfile.mkdtemp(prefix="nrf_dfu_tests_")
  38. def tearDown(self):
  39. shutil.rmtree(self.work_directory, ignore_errors=True)
  40. def test_generate_package_application(self):
  41. self.p = Package(
  42. dev_type=1,
  43. dev_rev=2,
  44. app_version=100,
  45. sd_req=[0x1000, 0xfffe],
  46. app_fw="firmwares/bar.hex"
  47. )
  48. pkg_name = "mypackage.zip"
  49. self.p.generate_package(pkg_name, preserve_work_directory=False)
  50. expected_zip_content = ["manifest.json", "bar.bin", "bar.dat"]
  51. with ZipFile(pkg_name, 'r') as pkg:
  52. infolist = pkg.infolist()
  53. for file_information in infolist:
  54. self.assertTrue(file_information.filename in expected_zip_content)
  55. self.assertGreater(file_information.file_size, 0)
  56. # Extract all and load json document to see if it is correct regarding to paths
  57. pkg.extractall(self.work_directory)
  58. with open(os.path.join(self.work_directory, 'manifest.json'), 'r') as f:
  59. _json = json.load(f)
  60. self.assertEqual(u'bar.bin', _json['manifest']['application']['bin_file'])
  61. self.assertEqual(u'bar.dat', _json['manifest']['application']['dat_file'])
  62. self.assertTrue(u'softdevice' not in _json['manifest'])
  63. self.assertTrue(u'softdevice_bootloader' not in _json['manifest'])
  64. self.assertTrue(u'bootloader' not in _json['manifest'])
  65. def test_generate_package_sd_bl(self):
  66. self.p = Package(dev_type=1,
  67. dev_rev=2,
  68. app_version=100,
  69. sd_req=[0x1000, 0xfffe],
  70. softdevice_fw="firmwares/foo.hex",
  71. bootloader_fw="firmwares/bar.hex")
  72. pkg_name = "mypackage.zip"
  73. self.p.generate_package(pkg_name, preserve_work_directory=False)
  74. expected_zip_content = ["manifest.json", "sd_bl.bin", "sd_bl.dat"]
  75. with ZipFile(pkg_name, 'r') as pkg:
  76. infolist = pkg.infolist()
  77. for file_information in infolist:
  78. self.assertTrue(file_information.filename in expected_zip_content)
  79. self.assertGreater(file_information.file_size, 0)
  80. # Extract all and load json document to see if it is correct regarding to paths
  81. pkg.extractall(self.work_directory)
  82. with open(os.path.join(self.work_directory, 'manifest.json'), 'r') as f:
  83. _json = json.load(f)
  84. self.assertEqual(u'sd_bl.bin', _json['manifest']['softdevice_bootloader']['bin_file'])
  85. self.assertEqual(u'sd_bl.dat', _json['manifest']['softdevice_bootloader']['dat_file'])
  86. def test_unpack_package_a(self):
  87. self.p = Package(dev_type=1,
  88. dev_rev=2,
  89. app_version=100,
  90. sd_req=[0x1000, 0xffff],
  91. softdevice_fw="firmwares/bar.hex",
  92. dfu_ver=0.6)
  93. pkg_name = os.path.join(self.work_directory, "mypackage.zip")
  94. self.p.generate_package(pkg_name, preserve_work_directory=False)
  95. unpacked_dir = os.path.join(self.work_directory, "unpacked")
  96. manifest = self.p.unpack_package(os.path.join(self.work_directory, pkg_name), unpacked_dir)
  97. self.assertIsNotNone(manifest)
  98. self.assertEqual(u'bar.bin', manifest.softdevice.bin_file)
  99. self.assertEqual(0, manifest.softdevice.init_packet_data.ext_packet_id)
  100. self.assertIsNotNone(manifest.softdevice.init_packet_data.firmware_crc16)
  101. def test_unpack_package_b(self):
  102. self.p = Package(dev_type=1,
  103. dev_rev=2,
  104. app_version=100,
  105. sd_req=[0x1000, 0xffff],
  106. softdevice_fw="firmwares/bar.hex",
  107. dfu_ver=0.7)
  108. pkg_name = os.path.join(self.work_directory, "mypackage.zip")
  109. self.p.generate_package(pkg_name, preserve_work_directory=False)
  110. unpacked_dir = os.path.join(self.work_directory, "unpacked")
  111. manifest = self.p.unpack_package(os.path.join(self.work_directory, pkg_name), unpacked_dir)
  112. self.assertIsNotNone(manifest)
  113. self.assertEqual(u'bar.bin', manifest.softdevice.bin_file)
  114. self.assertEqual(1, manifest.softdevice.init_packet_data.ext_packet_id)
  115. self.assertIsNone(manifest.softdevice.init_packet_data.firmware_crc16)
  116. self.assertIsNotNone(manifest.softdevice.init_packet_data.firmware_hash)
  117. def test_unpack_package_c(self):
  118. self.p = Package(dev_type=1,
  119. dev_rev=2,
  120. app_version=100,
  121. sd_req=[0x1000, 0xffff],
  122. softdevice_fw="firmwares/bar.hex",
  123. key_file="key.pem")
  124. pkg_name = os.path.join(self.work_directory, "mypackage.zip")
  125. self.p.generate_package(pkg_name, preserve_work_directory=False)
  126. unpacked_dir = os.path.join(self.work_directory, "unpacked")
  127. manifest = self.p.unpack_package(os.path.join(self.work_directory, pkg_name), unpacked_dir)
  128. self.assertIsNotNone(manifest)
  129. self.assertEqual(u'bar.bin', manifest.softdevice.bin_file)
  130. self.assertEqual(2, manifest.softdevice.init_packet_data.ext_packet_id)
  131. self.assertIsNone(manifest.softdevice.init_packet_data.firmware_crc16)
  132. self.assertIsNotNone(manifest.softdevice.init_packet_data.firmware_hash)
  133. self.assertIsNotNone(manifest.softdevice.init_packet_data.init_packet_ecds)
  134. self.assertEqual(manifest.dfu_version, 0.8)
  135. if __name__ == '__main__':
  136. unittest.main()