iPXE
gzip_test.c
Go to the documentation of this file.
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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26/** @file
27 *
28 * gzip image tests
29 *
30 */
31
32/* Forcibly enable assertions */
33#undef NDEBUG
34
35#include <stdint.h>
36#include <string.h>
37#include <ipxe/image.h>
38#include <ipxe/gzip.h>
39#include <ipxe/test.h>
40
41/** A gzip test */
42struct gzip_test {
43 /** Compressed filename */
44 const char *compressed_name;
45 /** Compressed data */
46 const void *compressed;
47 /** Length of compressed data */
49 /** Expected uncompressed name */
50 const char *expected_name;
51 /** Expected uncompressed data */
52 const void *expected;
53 /** Length of expected uncompressed data */
55};
56
57/** Define inline data */
58#define DATA(...) { __VA_ARGS__ }
59
60/** Define a gzip test */
61#define GZIP( name, COMPRESSED, EXPECTED ) \
62 static const uint8_t name ## _compressed[] = COMPRESSED; \
63 static const uint8_t name ## _expected[] = EXPECTED; \
64 static struct gzip_test name = { \
65 .compressed_name = #name ".gz", \
66 .compressed = name ## _compressed, \
67 .compressed_len = sizeof ( name ## _compressed ), \
68 .expected_name = #name, \
69 .expected = name ## _expected, \
70 .expected_len = sizeof ( name ## _expected ), \
71 };
72
73/** "Hello world" */
74GZIP ( hello_world,
75 DATA ( 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
76 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca,
77 0x49, 0x01, 0x00, 0x52, 0x9e, 0xd6, 0x8b, 0x0b, 0x00, 0x00,
78 0x00 ),
79 DATA ( 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c,
80 0x64 ) );
81
82/** "Hello filename" */
83GZIP ( hello_filename,
84 DATA ( 0x1f, 0x8b, 0x08, 0x08, 0xeb, 0x5b, 0x96, 0x60, 0x00, 0x03,
85 0x68, 0x77, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xf3, 0x48, 0xcd,
86 0xc9, 0xc9, 0x57, 0x48, 0xcb, 0xcc, 0x49, 0xcd, 0x4b, 0xcc,
87 0x4d, 0x05, 0x00, 0x69, 0x37, 0x25, 0x3c, 0x0e, 0x00, 0x00,
88 0x00 ),
89 DATA ( 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x66, 0x69, 0x6c, 0x65,
90 0x6e, 0x61, 0x6d, 0x65 ) );
91
92/** "Hello assorted headers" */
93GZIP ( hello_headers,
94 DATA ( 0x1f, 0x8b, 0x08, 0x1c, 0x11, 0x5c, 0x96, 0x60, 0x00, 0x03,
95 0x05, 0x00, 0x41, 0x70, 0x01, 0x00, 0x0d, 0x68, 0x77, 0x2e,
96 0x74, 0x78, 0x74, 0x00, 0x2f, 0x2f, 0x77, 0x68, 0x79, 0x3f,
97 0x00, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x48, 0x2c, 0x2e,
98 0xce, 0x2f, 0x2a, 0x49, 0x4d, 0x51, 0xc8, 0x48, 0x4d, 0x4c,
99 0x49, 0x2d, 0x2a, 0x06, 0x00, 0x59, 0xa4, 0x19, 0x61, 0x16,
100 0x00, 0x00, 0x00 ),
101 DATA ( 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x61, 0x73, 0x73, 0x6f,
102 0x72, 0x74, 0x65, 0x64, 0x20, 0x68, 0x65, 0x61, 0x64, 0x65,
103 0x72, 0x73 ) );
104
105/**
106 * Report gzip test result
107 *
108 * @v test gzip test
109 * @v file Test code file
110 * @v line Test code line
111 */
112static void gzip_okx ( struct gzip_test *test, const char *file,
113 unsigned int line ) {
114 struct image *image;
115 struct image *extracted;
116
117 /* Construct compressed image */
118 image = image_memory ( test->compressed_name, test->compressed,
119 test->compressed_len );
120 okx ( image != NULL, file, line );
121 okx ( image->len == test->compressed_len, file, line );
122
123 /* Check type detection */
124 okx ( image->type == &gzip_image_type, file, line );
125
126 /* Extract archive image */
127 okx ( image_extract ( image, NULL, &extracted ) == 0, file, line );
128
129 /* Verify extracted image content */
130 okx ( extracted->len == test->expected_len, file, line );
131 okx ( memcmp ( extracted->data, test->expected,
132 test->expected_len ) == 0, file, line );
133
134 /* Verify extracted image name */
135 okx ( strcmp ( extracted->name, test->expected_name ) == 0,
136 file, line );
137
138 /* Unregister images */
139 unregister_image ( extracted );
141}
142#define gzip_ok( test ) gzip_okx ( test, __FILE__, __LINE__ )
143
144/**
145 * Perform gzip self-test
146 *
147 */
148static void gzip_test_exec ( void ) {
149
150 gzip_ok ( &hello_world );
151 gzip_ok ( &hello_filename );
152 gzip_ok ( &hello_headers );
153}
154
155/** gzip self-test */
157 .name = "gzip",
158 .exec = gzip_test_exec,
159};
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
#define DATA(...)
Define inline data.
Definition acpi_test.c:74
int image_extract(struct image *image, const char *name, struct image **extracted)
Extract archive image.
Definition archive.c:45
static int test
Definition epic100.c:73
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
gzip compressed images
static void gzip_okx(struct gzip_test *test, const char *file, unsigned int line)
Report gzip test result.
Definition gzip_test.c:112
static void gzip_test_exec(void)
Perform gzip self-test.
Definition gzip_test.c:148
#define GZIP(name, COMPRESSED, EXPECTED)
Define a gzip test.
Definition gzip_test.c:61
#define gzip_ok(test)
Definition gzip_test.c:142
void unregister_image(struct image *image)
Unregister executable image.
Definition image.c:358
struct image * image_memory(const char *name, const void *data, size_t len)
Create registered image from block of memory.
Definition image.c:609
Executable images.
String functions.
int strcmp(const char *first, const char *second)
Compare strings.
Definition string.c:174
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115
A gzip test.
Definition gzip_test.c:42
const char * compressed_name
Compressed filename.
Definition gzip_test.c:44
const void * compressed
Compressed data.
Definition gzip_test.c:46
const char * expected_name
Expected uncompressed name.
Definition gzip_test.c:50
size_t expected_len
Length of expected uncompressed data.
Definition gzip_test.c:54
const void * expected
Expected uncompressed data.
Definition gzip_test.c:52
size_t compressed_len
Length of compressed data.
Definition gzip_test.c:48
An executable image.
Definition image.h:24
struct image_type * type
Image type, if known.
Definition image.h:59
const void * data
Read-only data.
Definition image.h:51
char * name
Name.
Definition image.h:38
size_t len
Length of raw file image.
Definition image.h:56
A self-test set.
Definition test.h:15
Self-test infrastructure.
#define okx(success, file, line)
Report test result.
Definition test.h:44
#define __self_test
Declare a self-test.
Definition test.h:32