diff options
author | Michael Brown <mcb30@ipxe.org> | 2021-05-06 13:17:35 +0100 |
---|---|---|
committer | Michael Brown <mcb30@ipxe.org> | 2021-05-08 15:34:19 +0100 |
commit | d093683d93ccfac4c76e72264ec3b0d8f0017b92 (patch) | |
tree | 7e752e4ffae9a5ef8ff0341e33d69da2acc693a1 /src/tests | |
parent | 5c9c8d2b9b78cf4e1f256fe6874855c1aee458f2 (diff) | |
download | ipxe-d093683d93ccfac4c76e72264ec3b0d8f0017b92.zip ipxe-d093683d93ccfac4c76e72264ec3b0d8f0017b92.tar.gz ipxe-d093683d93ccfac4c76e72264ec3b0d8f0017b92.tar.bz2 |
[zlib] Add support for zlib archive images
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/tests.c | 1 | ||||
-rw-r--r-- | src/tests/zlib_test.c | 133 |
2 files changed, 134 insertions, 0 deletions
diff --git a/src/tests/tests.c b/src/tests/tests.c index 2e812d6..f4cf041 100644 --- a/src/tests/tests.c +++ b/src/tests/tests.c @@ -73,3 +73,4 @@ REQUIRE_OBJECT ( bitops_test ); REQUIRE_OBJECT ( der_test ); REQUIRE_OBJECT ( pem_test ); REQUIRE_OBJECT ( ntlm_test ); +REQUIRE_OBJECT ( zlib_test ); diff --git a/src/tests/zlib_test.c b/src/tests/zlib_test.c new file mode 100644 index 0000000..df52d09 --- /dev/null +++ b/src/tests/zlib_test.c @@ -0,0 +1,133 @@ +/* + * Copyright (C) 2021 Michael Brown <mbrown@fensystems.co.uk>. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + * + * You can also choose to distribute this program under the terms of + * the Unmodified Binary Distribution Licence (as given in the file + * COPYING.UBDL), provided that you have satisfied its requirements. + */ + +FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); + +/** @file + * + * zlib image tests + * + */ + +/* Forcibly enable assertions */ +#undef NDEBUG + +#include <stdint.h> +#include <ipxe/image.h> +#include <ipxe/zlib.h> +#include <ipxe/test.h> + +/** A zlib test */ +struct zlib_test { + /** Compressed filename */ + const char *compressed_name; + /** Compressed data */ + const void *compressed; + /** Length of compressed data */ + size_t compressed_len; + /** Expected uncompressed name */ + const char *expected_name; + /** Expected uncompressed data */ + const void *expected; + /** Length of expected uncompressed data */ + size_t expected_len; +}; + +/** Define inline data */ +#define DATA(...) { __VA_ARGS__ } + +/** Define a zlib test */ +#define ZLIB( name, COMPRESSED, EXPECTED ) \ + static const uint8_t name ## _compressed[] = COMPRESSED; \ + static const uint8_t name ## _expected[] = EXPECTED; \ + static struct zlib_test name = { \ + .compressed_name = #name ".z", \ + .compressed = name ## _compressed, \ + .compressed_len = sizeof ( name ## _compressed ), \ + .expected_name = #name, \ + .expected = name ## _expected, \ + .expected_len = sizeof ( name ## _expected ), \ + }; + +/** "Hello world" */ +ZLIB ( hello_world, + DATA ( 0x78, 0x9c, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, + 0x2f, 0xca, 0x49, 0x01, 0x00, 0x18, 0xab, 0x04, 0x3d ), + DATA ( 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, + 0x64 ) ); + +/** + * Report zlib test result + * + * @v test zlib test + * @v file Test code file + * @v line Test code line + */ +static void zlib_okx ( struct zlib_test *test, const char *file, + unsigned int line ) { + struct image *image; + struct image *extracted; + + /* Construct compressed image */ + image = image_memory ( test->compressed_name, + virt_to_user ( test->compressed ), + test->compressed_len ); + okx ( image != NULL, file, line ); + okx ( image->len == test->compressed_len, file, line ); + + /* Check type detection */ + okx ( image->type == &zlib_image_type, file, line ); + + /* Extract archive image */ + okx ( image_extract ( image, NULL, &extracted ) == 0, file, line ); + + /* Verify extracted image content */ + okx ( extracted->len == test->expected_len, file, line ); + okx ( memcmp_user ( extracted->data, 0, + virt_to_user ( test->expected ), 0, + test->expected_len ) == 0, file, line ); + + /* Verify extracted image name */ + okx ( strcmp ( extracted->name, test->expected_name ) == 0, + file, line ); + + /* Unregister images */ + unregister_image ( extracted ); + unregister_image ( image ); +} +#define zlib_ok( test ) zlib_okx ( test, __FILE__, __LINE__ ) + +/** + * Perform zlib self-test + * + */ +static void zlib_test_exec ( void ) { + + zlib_ok ( &hello_world ); +} + +/** zlib self-test */ +struct self_test zlib_test __self_test = { + .name = "zlib", + .exec = zlib_test_exec, +}; |