From 297a01b67e5405c80b12a83ac8c12511841918a0 Mon Sep 17 00:00:00 2001 From: Yuval Peress Date: Tue, 8 Jun 2021 23:50:54 -0600 Subject: zmake: test final file output size Zephyr's build allows the image to consume the full flash size (CONFIG_FLASH_SIZE) since it doesn't assume anything about how chromium lays out the images (using RO/RW). This means that in systems with 512Kb of flash space, images taking up more than 256Kb will successfully build (even though the final image will be larger than 512Kb). Add a check in the output packers for the final size. This test ignores by default the .elf extension. BRANCH=none BUG=b:190435084 TEST=Added unit tests TEST=build brya Signed-off-by: Yuval Peress Change-Id: I94f1657d0ff44d79920ae5e8e7f11edf1580de05 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2948169 Reviewed-by: Denis Brockus --- zephyr/zmake/tests/test_packers.py | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 zephyr/zmake/tests/test_packers.py (limited to 'zephyr/zmake/tests') diff --git a/zephyr/zmake/tests/test_packers.py b/zephyr/zmake/tests/test_packers.py new file mode 100644 index 0000000000..21361a925f --- /dev/null +++ b/zephyr/zmake/tests/test_packers.py @@ -0,0 +1,51 @@ +# Copyright 2021 The Chromium OS Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import hypothesis +import hypothesis.strategies as st +import pathlib +import pytest +import tempfile +import unittest.mock as mock + +import zmake.output_packers as packers + +# Strategies for use with hypothesis +absolute_path = st.from_regex(regex=r"\A/[\w/]*\Z") + +@hypothesis.given(absolute_path) +@hypothesis.settings(deadline=60000) +def test_file_size_unbounded(path): + packer = packers.BasePacker(project=None) + packer._is_size_bound = mock.Mock(name='_is_size_bound', return_value=False) + file = pathlib.Path(path) / 'zephyr.bin' + assert packer._check_packed_file_size(file=file, dirs={}) == file + packer._is_size_bound.assert_called_once_with(file) + +@hypothesis.given(st.binary(min_size=5, max_size=100)) +@hypothesis.settings(deadline=60000) +def test_file_size_in_bounds(data): + packer = packers.BasePacker(project=None) + packer._is_size_bound = mock.Mock(name='_is_size_bound', return_value=True) + packer._get_max_image_bytes = mock.Mock(name='_get_max_image_bytes', + return_value=100) + with tempfile.TemporaryDirectory() as temp_dir_name: + file = pathlib.Path(temp_dir_name) / 'zephyr.bin' + with open(file, 'wb') as f: + f.write(data) + assert packer._check_packed_file_size(file=file, dirs={}) == file + +@hypothesis.given(st.binary(min_size=101, max_size=200)) +@hypothesis.settings(deadline=60000) +def test_file_size_out_of_bounds(data): + packer = packers.BasePacker(project=None) + packer._is_size_bound = mock.Mock(name='_is_size_bound', return_value=True) + packer._get_max_image_bytes = mock.Mock(name='_get_max_image_bytes', + return_value=100) + with tempfile.TemporaryDirectory() as temp_dir_name: + file = pathlib.Path(temp_dir_name) / 'zephyr.bin' + with open(file, 'wb') as f: + f.write(data) + with pytest.raises(RuntimeError): + packer._check_packed_file_size(file=file, dirs={}) \ No newline at end of file -- cgit v1.2.1