test_mkdfu.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2020 Espressif Systems (Shanghai) CO LTD
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. from __future__ import unicode_literals
  18. import filecmp
  19. import os
  20. import shutil
  21. import sys
  22. import tempfile
  23. import time
  24. import unittest
  25. import pexpect
  26. current_dir = os.path.dirname(os.path.realpath(__file__))
  27. mkdfu_path = os.path.join(current_dir, '..', 'mkdfu.py')
  28. class TestHelloWorldExample(unittest.TestCase):
  29. def common_test(self, add_args):
  30. with tempfile.NamedTemporaryFile(delete=False) as f:
  31. self.addCleanup(os.unlink, f.name)
  32. cmd = ' '.join([sys.executable, mkdfu_path, 'write',
  33. '-o', f.name,
  34. '--pid', '2',
  35. add_args])
  36. p = pexpect.spawn(cmd, timeout=10)
  37. self.addCleanup(p.terminate, force=True)
  38. p.expect_exact(['Adding 1/bootloader.bin at 0x1000',
  39. 'Adding 1/partition-table.bin at 0x8000',
  40. 'Adding 1/hello-world.bin at 0x10000',
  41. '"{}" has been written. You may proceed with DFU flashing.'.format(f.name)])
  42. # Need to wait for the process to end because the output file is closed when mkdfu exits.
  43. # Do non-blocking wait instead of the blocking p.wait():
  44. for _ in range(10):
  45. if not p.isalive():
  46. break
  47. time.sleep(0.5)
  48. else:
  49. p.terminate()
  50. self.assertTrue(filecmp.cmp(f.name, os.path.join(current_dir, '1','dfu.bin')), 'Output files are different')
  51. def test_with_json(self):
  52. self.common_test(' '.join(['--json', os.path.join(current_dir, '1', 'flasher_args.json')]))
  53. def test_without_json(self):
  54. self.common_test(' '.join(['0x1000', os.path.join(current_dir, '1', '1.bin'),
  55. '0x8000', os.path.join(current_dir, '1', '2.bin'),
  56. '0x10000', os.path.join(current_dir, '1', '3.bin')
  57. ]))
  58. def test_filenames(self):
  59. temp_dir = tempfile.mkdtemp(prefix='very_long_directory_name' * 8)
  60. self.addCleanup(shutil.rmtree, temp_dir, ignore_errors=True)
  61. with tempfile.NamedTemporaryFile(dir=temp_dir, delete=False) as f:
  62. output = f.name
  63. with tempfile.NamedTemporaryFile(prefix='ľščťžýáíéěř\u0420\u043e\u0441\u0441\u0438\u044f',
  64. dir=temp_dir,
  65. delete=False) as f:
  66. bootloader = f.name
  67. shutil.copyfile(os.path.join(current_dir, '1', '1.bin'), bootloader)
  68. cmd = ' '.join([sys.executable, mkdfu_path, 'write',
  69. '-o', output,
  70. '--pid', '2',
  71. ' '.join(['0x1000', bootloader,
  72. '0x8000', os.path.join(current_dir, '1', '2.bin'),
  73. '0x10000', os.path.join(current_dir, '1', '3.bin')
  74. ])
  75. ])
  76. p = pexpect.spawn(cmd, timeout=10, encoding='utf-8')
  77. self.addCleanup(p.terminate, force=True)
  78. p.expect_exact(['Adding {} at 0x1000'.format(bootloader),
  79. 'Adding 1/2.bin at 0x8000',
  80. 'Adding 1/3.bin at 0x10000',
  81. '"{}" has been written. You may proceed with DFU flashing.'.format(output)])
  82. if __name__ == '__main__':
  83. unittest.main()