Skip to content
Permalink
Browse files

file2hex.py: new --gzip-mtime option that defaults to zero + test

This makes the output of file2hex.py deterministic by default while also
letting the user set the mtime in the gzip header manually if desired.

Use the option without any argument to restore the previous behavior
that sets the current (and obviously changing) "now" timestamp.

To test:  ./sanitycheck --tag gen_inc_file

Signed-off-by: Marc Herbert <marc.herbert@intel.com>
  • Loading branch information...
marc-hb authored and nashif committed Mar 21, 2019
1 parent 28a5657 commit 3061c92aafc7a313717224b23da6d4cb24210d49
@@ -24,6 +24,12 @@ def parse_args():
parser.add_argument("-f", "--file", required=True, help="Input file")
parser.add_argument("-g", "--gzip", action="store_true",
help="Compress the file using gzip before output")
parser.add_argument("-t", "--gzip-mtime", type=int, default=0,
nargs='?', const=None,
help="""mtime seconds in the gzip header.
Defaults to zero to keep builds deterministic. For
current date and time (= "now") use this option
without any value.""")
args = parser.parse_args()


@@ -44,6 +50,7 @@ def main():
with io.BytesIO() as content:
with open(args.file, 'rb') as fg:
with gzip.GzipFile(fileobj=content, mode='w',
mtime=args.gzip_mtime,
compresslevel=9) as gz_obj:
gz_obj.write(fg.read())

@@ -14,3 +14,5 @@ set(source_file src/file.bin)

generate_inc_file_for_target(app ${source_file} ${gen_dir}/file.bin.inc)
generate_inc_file_for_target(app ${source_file} ${gen_dir}/file.bin.gz.inc --gzip)
generate_inc_file_for_target(app ${source_file} ${gen_dir}/file.bin.mtime.gz.inc
--gzip --gzip-mtime=42)
@@ -8,6 +8,7 @@
#include <stdbool.h>
#include <ztest.h>

/* Don't confuse doxygen with missing files */
/**
* @cond INTERNAL_HIDDEN
*/
@@ -17,16 +18,26 @@ static const unsigned char inc_file[] = {
#include <file.bin.inc>
};

static const unsigned char gz_inc_file[] = {
static const unsigned char no_mtime_gz_inc_file[] = {
#include <file.bin.gz.inc>
};

static const unsigned char mtime_gz_inc_file[] = {
#include <file.bin.mtime.gz.inc>
};

/**
* @endcond
*/

static const unsigned char mtime_zero[4] = { 0, 0, 0, 0 };

/* Must match the --gzip-mtime=test_val passed to
* generate_inc_file_for_target() */
static const unsigned char mtime_test_val[4] = { 42, 0, 0, 0 };

static const unsigned char compressed_inc_file[] = {
0x1f, 0x8b, 0x08, 0x00, 0xb0, 0xd6, 0xb8, 0x59,
0x1f, 0x8b, 0x08, 0x00, 0xDD, 0xDD, 0xDD, 0xDD,
0x02, 0xff, 0x01, 0x00, 0x01, 0xff, 0xfe, 0x00,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
@@ -74,18 +85,21 @@ static void test_gen_inc_file(void)
}
}

static void test_gen_gz_inc_file(void)

static void do_test_gen_gz_inc_file(const unsigned char gz_inc_file[],
const unsigned char mtime[4])
{
int i;

zassert_equal(sizeof(gz_inc_file), sizeof(compressed_inc_file),
"Invalid compressed file size");

for (i = 0; i < sizeof(inc_file); i++) {
if (i == 4 || i == 5 || i == 6 || i == 7) {
/* Skip the modification time field (4 bytes) in
* gzip header as that can change.
/* Modification time field (4 bytes) in
* the gzip header.
*/
if (mtime != NULL) /* NULL arg = random "now" */
zassert_equal(gz_inc_file[i], mtime[i-4],
"Invalid mtime in inc file");

continue;
}

@@ -94,11 +108,28 @@ static void test_gen_gz_inc_file(void)
}
}

static void test_gen_gz_inc_file_no_mtime(void)
{
zassert_equal(sizeof(no_mtime_gz_inc_file), sizeof(compressed_inc_file),
"Invalid compressed file size");

do_test_gen_gz_inc_file(no_mtime_gz_inc_file, mtime_zero);
}

static void test_gen_gz_inc_file_mtime_arg(void)
{
zassert_equal(sizeof(mtime_gz_inc_file), sizeof(compressed_inc_file),
"Invalid compressed file size");

do_test_gen_gz_inc_file(mtime_gz_inc_file, mtime_test_val);
}

void test_main(void)
{
ztest_test_suite(gen_inc_file_test,
ztest_unit_test(test_gen_inc_file),
ztest_unit_test(test_gen_gz_inc_file)
ztest_unit_test(test_gen_gz_inc_file_no_mtime),
ztest_unit_test(test_gen_gz_inc_file_mtime_arg)
);

ztest_run_test_suite(gen_inc_file_test);

0 comments on commit 3061c92

Please sign in to comment.
You can’t perform that action at this time.