Skip to content

Commit

Permalink
cmd_convert: Fix indentation count param handling
Browse files Browse the repository at this point in the history
Set appropriate type in argument parser; add value validation.
Add new tests.
Fix typos.

Ref: NCSDK-24441

Signed-off-by: Adam Szczygieł <[email protected]>
  • Loading branch information
adsz-nordic committed Dec 14, 2023
1 parent fdec88b commit 58bfe84
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
3 changes: 3 additions & 0 deletions suit_generator/cmd_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def add_arguments(parser):
cmd_convert_arg_parser.add_argument(
"--indentation-count",
required=False,
type=int,
default=KeyConverter.default_indentation_count,
help=f"Number of indentation characters to put at the beginning of array lines. "
f"Default: {KeyConverter.default_indentation_count}",
Expand Down Expand Up @@ -170,6 +171,8 @@ def _validate(self):
raise ValueError(f"Invalid length_name: {self._length_name}")
if self._columns_count <= 0:
raise ValueError(f"Invalid columns count: {self._columns_count}")
if self._indentation_count < 0:
raise ValueError(f"Invalid indentation count: {self._indentation_count}")

# Header and footer files can be empty
# no length and no const are boolean and both values are allowed
Expand Down
79 changes: 73 additions & 6 deletions tests/test_cmd_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@
};
"""

EXPECTED_GENERATED_FILE_INDENT_0 = """const uint8_t public_key[] = {
0xed, 0xd0, 0x9e, 0xa5, 0xec, 0xe4, 0xed, 0xbe, 0x6c, 0x08, 0xe7, 0x47,
0x09, 0x55, 0x9a, 0x38, 0x29, 0xc5, 0x31, 0x33, 0x22, 0x7b, 0xf4, 0xf0,
0x11, 0x6e, 0x8c, 0x05, 0x2d, 0x02, 0x0e, 0x0e, 0xc3, 0xe0, 0xd8, 0x37,
0xf4, 0xc2, 0x6f, 0xc1, 0x28, 0x80, 0x2f, 0x45, 0x38, 0x1a, 0x23, 0x2b,
0x6d, 0xd5, 0xda, 0x28, 0x60, 0x00, 0x5d, 0xab, 0xe2, 0xa0, 0x83, 0xdb,
0xef, 0x38, 0x55, 0x13
};
"""

EXPECTED_GENERATED_FILE_INDENT_8 = """const uint8_t public_key[] = {
0xed, 0xd0, 0x9e, 0xa5, 0xec, 0xe4, 0xed, 0xbe, 0x6c, 0x08, 0xe7, 0x47,
0x09, 0x55, 0x9a, 0x38, 0x29, 0xc5, 0x31, 0x33, 0x22, 0x7b, 0xf4, 0xf0,
0x11, 0x6e, 0x8c, 0x05, 0x2d, 0x02, 0x0e, 0x0e, 0xc3, 0xe0, 0xd8, 0x37,
0xf4, 0xc2, 0x6f, 0xc1, 0x28, 0x80, 0x2f, 0x45, 0x38, 0x1a, 0x23, 0x2b,
0x6d, 0xd5, 0xda, 0x28, 0x60, 0x00, 0x5d, 0xab, 0xe2, 0xa0, 0x83, 0xdb,
0xef, 0x38, 0x55, 0x13
};
"""


@pytest.fixture
def mocker_existing_file(mocker):
Expand Down Expand Up @@ -199,51 +219,66 @@ def test_validate_whitespace_output_file(mocker_private_key_file_nonempty):
def test_validate_array_type():
# GIVEN empty array variable type
# WHEN converter is created
# THEN it rases an exception
# THEN it raises an exception
with pytest.raises(ValueError):
KeyConverter(input_file="some_input_file", output_file="some_output_file", array_type="")


def test_validate_array_name():
# GIVEN empty array variable name
# WHEN converter is created
# THEN it rases an exception
# THEN it raises an exception
with pytest.raises(ValueError):
KeyConverter(input_file="some_input_file", output_file="some_output_file", array_name="")


def test_validate_length_type():
# GIVEN empty length variable type
# WHEN converter is created
# THEN it rases an exception
# THEN it raises an exception
with pytest.raises(ValueError):
KeyConverter(input_file="some_input_file", output_file="some_output_file", length_type="")


def test_validate_length_name():
# GIVEN empty length variable name
# WHEN converter is created
# THEN it rases an exception
# THEN it raises an exception
with pytest.raises(ValueError):
KeyConverter(input_file="some_input_file", output_file="some_output_file", length_name="")


def test_validate_columns_count_zero():
# GIVEN columns count set to zero
# WHEN converter is created
# THEN it rases an exception
# THEN it raises an exception
with pytest.raises(ValueError):
KeyConverter(input_file="some_input_file", output_file="some_output_file", columns_count=0)


def test_validate_columns_count_negative():
# GIVEN columns count set to negative value
# WHEN converter is created
# THEN it rases an exception
# THEN it raises an exception
with pytest.raises(ValueError):
KeyConverter(input_file="some_input_file", output_file="some_output_file", columns_count=-123)


def test_validate_negative_indentation_count(mocker_private_key_file_nonempty):
# GIVEN indentation count set to -4
# WHEN converter is created
# THEN it raises an exception
with pytest.raises(ValueError):
KeyConverter(
input_file="key_private.pem",
output_file="some_output_file",
array_name="public_key",
no_length=True,
columns_count=12,
indentation_count=-4,
)


def test_prepare_header_no_header_file(default_converter):
# GIVEN converter that has no header file name given
# WHEN header text is prepared
Expand Down Expand Up @@ -542,3 +577,35 @@ def test_file_contents_with_header_and_footer(mocker_header_key_and_footer):
contents
== HEADER_FILE_DATA_NON_EMPTY + "\n\n" + EXPECTED_GENERATED_FILE_ORIGINAL + "\n" + FOOTER_FILE_DATA_NON_EMPTY
)


def test_file_contents_indentation_0(mocker_private_key_file_nonempty):
# GIVEN converter with indentation count set to 0
converter = KeyConverter(
input_file="key_private.pem",
output_file="some_output_file",
array_name="public_key",
no_length=True,
columns_count=12,
indentation_count=0,
)
# WHEN file contents are generated
contents = converter.prepare_file_contents()
# THEN they match expected content
assert contents == EXPECTED_GENERATED_FILE_INDENT_0


def test_file_contents_indentation_8(mocker_private_key_file_nonempty):
# GIVEN converter with indentation count set to 8
converter = KeyConverter(
input_file="key_private.pem",
output_file="some_output_file",
array_name="public_key",
no_length=True,
columns_count=12,
indentation_count=8,
)
# WHEN file contents are generated
contents = converter.prepare_file_contents()
# THEN they match expected content
assert contents == EXPECTED_GENERATED_FILE_INDENT_8

0 comments on commit 58bfe84

Please sign in to comment.