1 /*
2 * Copyright (C) 2021 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 /** @file
27 *
28 * zlib image tests
29 *
30 */
31
32 /* Forcibly enable assertions */
33 #undef NDEBUG
34
35 #include <stdint.h>
36 #include <ipxe/image.h>
37 #include <ipxe/zlib.h>
38 #include <ipxe/test.h>
39
40 /** A zlib test */
41 struct zlib_test {
42 /** Compressed filename */
43 const char *compressed_name;
44 /** Compressed data */
45 const void *compressed;
46 /** Length of compressed data */
47 size_t compressed_len;
48 /** Expected uncompressed name */
49 const char *expected_name;
50 /** Expected uncompressed data */
51 const void *expected;
52 /** Length of expected uncompressed data */
53 size_t expected_len;
54 };
55
56 /** Define inline data */
57 #define DATA(...) { __VA_ARGS__ }
58
59 /** Define a zlib test */
60 #define ZLIB( name, COMPRESSED, EXPECTED ) \
61 static const uint8_t name ## _compressed[] = COMPRESSED; \
62 static const uint8_t name ## _expected[] = EXPECTED; \
63 static struct zlib_test name = { \
64 .compressed_name = #name ".z", \
65 .compressed = name ## _compressed, \
66 .compressed_len = sizeof ( name ## _compressed ), \
67 .expected_name = #name, \
68 .expected = name ## _expected, \
69 .expected_len = sizeof ( name ## _expected ), \
70 };
71
72 /** "Hello world" */
73 ZLIB ( hello_world,
74 DATA ( 0x78, 0x9c, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf,
75 0x2f, 0xca, 0x49, 0x01, 0x00, 0x18, 0xab, 0x04, 0x3d ),
76 DATA ( 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c,
77 0x64 ) );
78
79 /**
80 * Report zlib test result
81 *
82 * @v test zlib test
83 * @v file Test code file
84 * @v line Test code line
85 */
zlib_okx(struct zlib_test * test,const char * file,unsigned int line)86 static void zlib_okx ( struct zlib_test *test, const char *file,
87 unsigned int line ) {
88 struct image *image;
89 struct image *extracted;
90
91 /* Construct compressed image */
92 image = image_memory ( test->compressed_name,
93 virt_to_user ( test->compressed ),
94 test->compressed_len );
95 okx ( image != NULL, file, line );
96 okx ( image->len == test->compressed_len, file, line );
97
98 /* Check type detection */
99 okx ( image->type == &zlib_image_type, file, line );
100
101 /* Extract archive image */
102 okx ( image_extract ( image, NULL, &extracted ) == 0, file, line );
103
104 /* Verify extracted image content */
105 okx ( extracted->len == test->expected_len, file, line );
106 okx ( memcmp_user ( extracted->data, 0,
107 virt_to_user ( test->expected ), 0,
108 test->expected_len ) == 0, file, line );
109
110 /* Verify extracted image name */
111 okx ( strcmp ( extracted->name, test->expected_name ) == 0,
112 file, line );
113
114 /* Unregister images */
115 unregister_image ( extracted );
116 unregister_image ( image );
117 }
118 #define zlib_ok( test ) zlib_okx ( test, __FILE__, __LINE__ )
119
120 /**
121 * Perform zlib self-test
122 *
123 */
zlib_test_exec(void)124 static void zlib_test_exec ( void ) {
125
126 zlib_ok ( &hello_world );
127 }
128
129 /** zlib self-test */
130 struct self_test zlib_test __self_test = {
131 .name = "zlib",
132 .exec = zlib_test_exec,
133 };
134