From 0bd9f6fe12e1e1d950091aebecbe13568229b59b Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 30 Apr 2021 18:37:15 +0200 Subject: [PATCH] tools: spiffsgen: fix length error, add test case --- .gitlab/ci/host-test.yml | 2 + components/spiffs/spiffsgen.py | 10 +-- components/spiffs/test_spiffsgen/__init__.py | 0 .../spiffs/test_spiffsgen/test_spiffsgen.py | 68 +++++++++++++++++++ tools/ci/executable-list.txt | 1 + 5 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 components/spiffs/test_spiffsgen/__init__.py create mode 100755 components/spiffs/test_spiffsgen/test_spiffsgen.py diff --git a/.gitlab/ci/host-test.yml b/.gitlab/ci/host-test.yml index 2408d3e26a..7a2f978dcc 100644 --- a/.gitlab/ci/host-test.yml +++ b/.gitlab/ci/host-test.yml @@ -110,6 +110,8 @@ test_spiffs_on_host: script: - cd components/spiffs/test_spiffs_host/ - make test + - cd ../test_spiffsgen + - ${IDF_PATH}/tools/ci/multirun_with_pyenv.sh ./test_spiffsgen.py test_multi_heap_on_host: extends: .host_test_template diff --git a/components/spiffs/spiffsgen.py b/components/spiffs/spiffsgen.py index c1eb259cc8..10884a87b2 100755 --- a/components/spiffs/spiffsgen.py +++ b/components/spiffs/spiffsgen.py @@ -49,7 +49,7 @@ SPIFFS_PAGE_IX_LEN = 2 # spiffs_page_ix SPIFFS_BLOCK_IX_LEN = 2 # spiffs_block_ix -class SpiffsBuildConfig(): +class SpiffsBuildConfig(object): def __init__(self, page_size, # type: int page_ix_len, # type: int @@ -115,7 +115,7 @@ class SpiffsFullError(RuntimeError): pass -class SpiffsPage(): +class SpiffsPage(object): _endianness_dict = { 'little': '<', 'big': '>' @@ -294,7 +294,7 @@ class SpiffsObjDataPage(SpiffsObjPageWithIdx): return img -class SpiffsBlock(): +class SpiffsBlock(object): def _reset(self): # type: () -> None self.cur_obj_index_span_ix = 0 self.cur_obj_data_span_ix = 0 @@ -392,7 +392,7 @@ class SpiffsBlock(): return img -class SpiffsFS(): +class SpiffsFS(object): def __init__(self, img_size, build_config): # type: (int, SpiffsBuildConfig) -> None if img_size % build_config.block_size != 0: raise RuntimeError('image size should be a multiple of block size') @@ -486,7 +486,7 @@ class SpiffsFS(): bix += 1 else: # Just fill remaining spaces FF's - all_blocks.append(b'\xFF' * (self.img_size - len(img))) + all_blocks.append(b'\xFF' * (self.img_size - len(all_blocks) * self.build_config.block_size)) img += b''.join([blk for blk in all_blocks]) return img diff --git a/components/spiffs/test_spiffsgen/__init__.py b/components/spiffs/test_spiffsgen/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/components/spiffs/test_spiffsgen/test_spiffsgen.py b/components/spiffs/test_spiffsgen/test_spiffsgen.py new file mode 100755 index 0000000000..df6a5f7745 --- /dev/null +++ b/components/spiffs/test_spiffsgen/test_spiffsgen.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python +import os +import sys +import unittest + +try: + import typing +except ImportError: + pass + +sys.path.append(os.path.join(os.path.dirname(__file__), '..')) +try: + import spiffsgen +except ImportError: + raise + + +class SpiffsgenTest(unittest.TestCase): + def test_configs(self): # type: () -> None + """Run spiffsgen with different configs, and check that + an image is generated (there is no exception), and the image size + is as expected. + """ + default_config = dict( + page_size=256, + page_ix_len=spiffsgen.SPIFFS_PAGE_IX_LEN, + block_size=4096, + block_ix_len=spiffsgen.SPIFFS_BLOCK_IX_LEN, + meta_len=4, + obj_name_len=32, + obj_id_len=spiffsgen.SPIFFS_BLOCK_IX_LEN, + span_ix_len=spiffsgen.SPIFFS_SPAN_IX_LEN, + packed=True, + aligned=True, + endianness='little', + use_magic=True, + use_magic_len=True, + aligned_obj_ix_tables=False + ) + + def make_config(**kwargs): # type: (typing.Any) -> spiffsgen.SpiffsBuildConfig + """Return SpiffsBuildConfig object with configuration set + by default_config plus any options overridden in kwargs. + """ + new_config = dict(default_config) + new_config.update(**kwargs) + return spiffsgen.SpiffsBuildConfig(**new_config) + + configs = [ + make_config(), + make_config(use_magic_len=False, use_magic=False, aligned_obj_ix_tables=True), + make_config(meta_len=4, obj_name_len=16), + make_config(block_size=8192), + make_config(page_size=512) + ] + + image_size = 64 * 1024 + for config in configs: + spiffs = spiffsgen.SpiffsFS(image_size, config) + spiffs.create_file('/test', __file__) + image = spiffs.to_binary() + self.assertEqual(len(image), image_size) + # Note: it would be nice to compile spiffs for host with the given + # config, and verify that the image is parsed correctly. + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/ci/executable-list.txt b/tools/ci/executable-list.txt index 5e293e3287..a94b5cde5f 100644 --- a/tools/ci/executable-list.txt +++ b/tools/ci/executable-list.txt @@ -17,6 +17,7 @@ components/partition_table/parttool.py components/partition_table/test_gen_esp32part_host/check_sizes_test.py components/partition_table/test_gen_esp32part_host/gen_esp32part_tests.py components/spiffs/spiffsgen.py +components/spiffs/test_spiffsgen/test_spiffsgen.py components/ulp/esp32ulp_mapgen.py docs/build_docs.py docs/check_lang_folder_sync.sh