test_spiffsgen.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python
  2. # SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
  3. # SPDX-License-Identifier: Apache-2.0
  4. import os
  5. import sys
  6. import unittest
  7. try:
  8. import typing
  9. except ImportError:
  10. pass
  11. sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
  12. try:
  13. import spiffsgen
  14. except ImportError:
  15. raise
  16. class SpiffsgenTest(unittest.TestCase):
  17. def test_configs(self): # type: () -> None
  18. """Run spiffsgen with different configs, and check that
  19. an image is generated (there is no exception), and the image size
  20. is as expected.
  21. """
  22. default_config = dict(
  23. page_size=256,
  24. page_ix_len=spiffsgen.SPIFFS_PAGE_IX_LEN,
  25. block_size=4096,
  26. block_ix_len=spiffsgen.SPIFFS_BLOCK_IX_LEN,
  27. meta_len=4,
  28. obj_name_len=32,
  29. obj_id_len=spiffsgen.SPIFFS_BLOCK_IX_LEN,
  30. span_ix_len=spiffsgen.SPIFFS_SPAN_IX_LEN,
  31. packed=True,
  32. aligned=True,
  33. endianness='little',
  34. use_magic=True,
  35. use_magic_len=True,
  36. aligned_obj_ix_tables=False
  37. )
  38. def make_config(**kwargs): # type: (typing.Any) -> spiffsgen.SpiffsBuildConfig
  39. """Return SpiffsBuildConfig object with configuration set
  40. by default_config plus any options overridden in kwargs.
  41. """
  42. new_config = dict(default_config)
  43. new_config.update(**kwargs)
  44. return spiffsgen.SpiffsBuildConfig(**new_config)
  45. configs = [
  46. make_config(),
  47. make_config(use_magic_len=False, use_magic=False, aligned_obj_ix_tables=True),
  48. make_config(meta_len=4, obj_name_len=16),
  49. make_config(block_size=8192),
  50. make_config(page_size=512)
  51. ]
  52. image_size = 64 * 1024
  53. for config in configs:
  54. spiffs = spiffsgen.SpiffsFS(image_size, config)
  55. spiffs.create_file('/test', __file__)
  56. image = spiffs.to_binary()
  57. self.assertEqual(len(image), image_size)
  58. # Note: it would be nice to compile spiffs for host with the given
  59. # config, and verify that the image is parsed correctly.
  60. if __name__ == '__main__':
  61. unittest.main()