genpkg_generate_dfu_package_steps.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 logging
  30. import os
  31. from zipfile import ZipFile
  32. from behave import given, then, when
  33. from click.testing import CliRunner
  34. from nordicsemi.__main__ import cli, int_as_text_to_int
  35. from common_steps import get_resources_path
  36. logger = logging.getLogger(__file__)
  37. @given(u'the user wants to generate a DFU package with application {application}, bootloader {bootloader} and SoftDevice {softdevice} with name {package}')
  38. def step_impl(context, application, bootloader, softdevice, package):
  39. runner = CliRunner()
  40. context.runner = runner
  41. args = ['dfu', 'genpkg']
  42. if application != u'not_set':
  43. args.extend(['--application', os.path.join(get_resources_path(), application)])
  44. context.application = application
  45. else:
  46. context.application = None
  47. if bootloader != u'not_set':
  48. args.extend(['--bootloader', os.path.join(get_resources_path(), bootloader)])
  49. context.bootloader = bootloader
  50. else:
  51. context.bootloader = None
  52. if softdevice != u'not_set':
  53. args.extend(['--softdevice', os.path.join(get_resources_path(), softdevice)])
  54. context.softdevice = softdevice
  55. else:
  56. context.softdevice = None
  57. args.append(package)
  58. context.args = args
  59. @given(u'with option --application-version {app_ver}')
  60. def step_impl(context, app_ver):
  61. context.application_version = None
  62. if app_ver == u'not_set':
  63. context.application_version = 0xFFFFFFFF
  64. elif app_ver == u'none':
  65. context.args.extend(['--application-version', 'None'])
  66. else:
  67. context.args.extend(['--application-version', app_ver])
  68. context.application_version = int_as_text_to_int(app_ver)
  69. @given(u'with option --dev-revision {dev_rev}')
  70. def step_impl(context, dev_rev):
  71. context.dev_revision = None
  72. if dev_rev == u'not_set':
  73. context.dev_revision = 0xFFFF
  74. elif dev_rev == u'none':
  75. context.args.extend(['--dev-revision', 'None'])
  76. else:
  77. context.args.extend(['--dev-revision', dev_rev])
  78. context.dev_revision = int_as_text_to_int(dev_rev)
  79. @given(u'with option --dev-type {dev_type}')
  80. def step_impl(context, dev_type):
  81. context.dev_type = None
  82. if dev_type == u'not_set':
  83. context.dev_type = 0xFFFF
  84. elif dev_type == u'none':
  85. context.args.extend(['--dev-type', 'None'])
  86. else:
  87. context.args.extend(['--dev-type', dev_type])
  88. context.dev_type = int_as_text_to_int(dev_type)
  89. @given(u'with option --dfu-ver {dfu_ver}')
  90. def step_impl(context, dfu_ver):
  91. context.firmware_hash = None
  92. context.ext_packet_id = None
  93. context.init_packet_ecds = None
  94. if dfu_ver == u'not_set':
  95. context.dfu_ver = 0.5
  96. context.ext_packet_id = 0
  97. else:
  98. if dfu_ver == 0.5:
  99. pass
  100. elif dfu_ver == 0.6:
  101. context.ext_packet_id = 0
  102. elif dfu_ver == 0.7:
  103. context.ext_packet_id = 1
  104. context.firmware_hash = 'exists'
  105. elif dfu_ver == 0.8:
  106. context.ext_packet_id = 2
  107. context.firmware_hash = 'exists'
  108. context.init_packet_ecds = 'exists'
  109. context.args.extend(['--dfu-ver', dfu_ver])
  110. context.dfu_ver = float(dfu_ver)
  111. @given(u'with option --sd-req {sd_reqs}')
  112. def step_impl(context, sd_reqs):
  113. context.sd_req = None
  114. if sd_reqs == u'not_set':
  115. context.sd_req = [0xFFFE]
  116. elif sd_reqs == u'none':
  117. context.args.extend(['--sd-req', 'None'])
  118. else:
  119. context.args.extend(['--sd-req', sd_reqs])
  120. sd_reqs = sd_reqs.split(",")
  121. sd_reqs_value = []
  122. for sd_req in sd_reqs:
  123. sd_reqs_value.append(int_as_text_to_int(sd_req))
  124. context.sd_req = sd_reqs_value
  125. @given(u'with option --key-file {pem_file}')
  126. def step_impl(context, pem_file):
  127. if pem_file != u'not_set':
  128. context.args.extend(['--key-file', os.path.join(get_resources_path(), pem_file)])
  129. context.dfu_ver = 0.8
  130. @when(u'user press enter')
  131. def step_impl(context):
  132. pass
  133. @then(u'the generated DFU package {package} contains correct data')
  134. def step_impl(context, package):
  135. with context.runner.isolated_filesystem():
  136. pkg_full_name = os.path.join(os.getcwd(), package)
  137. logger.debug("Package full name %s", pkg_full_name)
  138. result = context.runner.invoke(cli, context.args)
  139. logger.debug("exit_code: %s, output: \'%s\'", result.exit_code, result.output)
  140. assert result.exit_code == 0
  141. with ZipFile(pkg_full_name, 'r') as pkg:
  142. infolist = pkg.infolist()
  143. expected_zip_content = ["manifest.json"]
  144. if context.bootloader and context.softdevice:
  145. expected_zip_content.append("sd_bl.bin")
  146. expected_zip_content.append("sd_bl.dat")
  147. elif context.bootloader:
  148. expected_zip_content.append(context.bootloader.split(".")[0] + ".bin")
  149. expected_zip_content.append(context.bootloader.split(".")[0] + ".dat")
  150. elif context.softdevice:
  151. expected_zip_content.append(context.softdevice.split(".")[0] + ".bin")
  152. expected_zip_content.append(context.softdevice.split(".")[0] + ".dat")
  153. if context.application:
  154. expected_zip_content.append(context.application.split(".")[0] + '.bin')
  155. expected_zip_content.append(context.application.split(".")[0] + '.dat')
  156. for file_information in infolist:
  157. assert file_information.filename in expected_zip_content
  158. assert file_information.file_size > 0
  159. # Extract all and load json document to see if it is correct regarding to paths
  160. pkg.extractall()
  161. with open('manifest.json', 'r') as f:
  162. _json = json.load(f)
  163. if context.dfu_ver:
  164. assert 'dfu_version' in _json['manifest']
  165. assert _json['manifest']['dfu_version'] == context.dfu_ver
  166. if context.bootloader and context.softdevice:
  167. data = _json['manifest']['softdevice_bootloader']['init_packet_data']
  168. assert_init_packet_data(context, data)
  169. elif context.bootloader:
  170. data = _json['manifest']['bootloader']['init_packet_data']
  171. assert_init_packet_data(context, data)
  172. elif context.softdevice:
  173. data = _json['manifest']['softdevice']['init_packet_data']
  174. assert_init_packet_data(context, data)
  175. if context.application:
  176. data = _json['manifest']['application']['init_packet_data']
  177. assert_init_packet_data(context, data)
  178. def assert_init_packet_data(context, data):
  179. if context.application_version:
  180. assert 'application_version' in data
  181. assert data['application_version'] == context.application_version
  182. if context.dev_revision:
  183. assert 'device_revision' in data
  184. assert data['device_revision'] == context.dev_revision
  185. if context.dev_type:
  186. assert 'device_type' in data
  187. assert data['device_type'] == context.dev_type
  188. if context.sd_req:
  189. assert 'softdevice_req' in data
  190. assert data['softdevice_req'] == context.sd_req
  191. if context.ext_packet_id:
  192. assert 'ext_packet_id' in data
  193. assert data['ext_packet_id'] == context.ext_packet_id
  194. if context.firmware_hash:
  195. assert 'firmware_hash' in data
  196. if context.init_packet_ecds:
  197. assert 'init_packet_ecds' in data