test_nrfhex.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 os
  29. import unittest
  30. import nordicsemi.dfu.nrfhex as nrfhex
  31. import nordicsemi.dfu.intelhex as intelhex
  32. class TestnRFHex(unittest.TestCase):
  33. def setUp(self):
  34. script_abspath = os.path.abspath(__file__)
  35. script_dirname = os.path.dirname(script_abspath)
  36. os.chdir(script_dirname)
  37. def comparefiles(self, actual, wanted):
  38. actualfile = intelhex.IntelHex()
  39. actualfile.loadfile(actual, format="bin")
  40. wantedfile = intelhex.IntelHex()
  41. wantedfile.loadfile(wanted, format="bin")
  42. self.assertEqual(actualfile.minaddr(), wantedfile.minaddr())
  43. self.assertEqual(actualfile.maxaddr(), wantedfile.maxaddr())
  44. minaddress = actualfile.minaddr()
  45. maxaddress = actualfile.maxaddr()
  46. length = maxaddress - minaddress
  47. actualfile_data = actualfile.gets(minaddress, length)
  48. wantedfile_data = wantedfile.gets(minaddress, length)
  49. self.assertEqual(actualfile_data, wantedfile_data)
  50. def test_tobinfile_single_file_without_uicr_content(self):
  51. nrf = nrfhex.nRFHex("firmwares/bar.hex")
  52. nrf.tobinfile("firmwares/bar.bin")
  53. self.comparefiles("firmwares/bar.bin", "firmwares/bar_wanted.bin")
  54. def test_tobinfile_single_file_with_uicr_content(self):
  55. nrf = nrfhex.nRFHex("firmwares/foo.hex")
  56. nrf.tobinfile("firmwares/foo.bin")
  57. self.comparefiles("firmwares/foo.bin", "firmwares/foo_wanted.bin")
  58. def test_tobinfile_single_bin_file(self):
  59. nrf = nrfhex.nRFHex("firmwares/bar_wanted.bin")
  60. nrf.tobinfile("firmwares/bar.bin")
  61. self.comparefiles("firmwares/bar.bin", "firmwares/bar_wanted.bin")
  62. def test_tobinfile_two_hex_files(self):
  63. nrf = nrfhex.nRFHex("firmwares/foo.hex", "firmwares/bar.hex")
  64. nrf.tobinfile("firmwares/foobar.bin")
  65. self.comparefiles("firmwares/foobar.bin", "firmwares/foobar_wanted.bin")
  66. def test_tobinfile_one_hex_one_bin(self):
  67. nrf = nrfhex.nRFHex("firmwares/foo_wanted.bin", "firmwares/bar.hex")
  68. nrf.tobinfile("firmwares/foobar.bin")
  69. self.comparefiles("firmwares/foobar.bin", "firmwares/foobar_wanted.bin")
  70. def test_tobinfile_one_bin_one_hex(self):
  71. nrf = nrfhex.nRFHex("firmwares/foo.hex", "firmwares/bar_wanted.bin")
  72. nrf.tobinfile("firmwares/foobar.bin")
  73. self.comparefiles("firmwares/foobar.bin", "firmwares/foobar_wanted.bin")
  74. def test_tobinfile_two_bin(self):
  75. nrf = nrfhex.nRFHex("firmwares/foo_wanted.bin", "firmwares/bar_wanted.bin")
  76. nrf.tobinfile("firmwares/foobar.bin")
  77. self.comparefiles("firmwares/foobar.bin", "firmwares/foobar_wanted.bin")
  78. def test_sizes(self):
  79. nrf = nrfhex.nRFHex("firmwares/foo.hex", "firmwares/bar.hex")
  80. self.assertEqual(nrf.get_mbr_end_address(), 0x1000)
  81. self.assertEqual(nrf.minaddr(), 0x1000)
  82. self.assertEqual(nrf.size(), 73152)
  83. self.assertEqual(nrf.bootloadersize(), 13192)
  84. nrf = nrfhex.nRFHex("firmwares/s132_nrf52_mini.hex")
  85. self.assertEqual(nrf.get_mbr_end_address(), 0x3000)
  86. self.assertEqual(nrf.minaddr(), 0x3000)
  87. self.assertEqual(nrf.size(), 12288)
  88. self.assertEqual(nrf.bootloadersize(), 0)
  89. def test_get_softdevice_variant(self):
  90. nrf = nrfhex.nRFHex("firmwares/foo.hex")
  91. self.assertEqual(nrf.get_softdevice_variant(), "unknown")
  92. nrf = nrfhex.nRFHex("firmwares/s130_nrf51_mini.hex")
  93. self.assertEqual(nrf.get_softdevice_variant(), "s1x0")
  94. nrf = nrfhex.nRFHex("firmwares/s132_nrf52_mini.hex")
  95. self.assertEqual(nrf.get_softdevice_variant(), "s132")
  96. if __name__ == '__main__':
  97. unittest.main()