tools: spiffsgen: fix length error, add test case

This commit is contained in:
Ivan Grokhotkov
2021-04-30 18:37:15 +02:00
parent 9af485307e
commit 0bd9f6fe12
5 changed files with 76 additions and 5 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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()

View File

@@ -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