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 
24 FILE_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 <ipxe/image.h>
37 #include <ipxe/gzip.h>
38 #include <ipxe/test.h>
39 
40 /** A gzip test */
41 struct gzip_test {
42  /** Compressed filename */
43  const char *compressed_name;
44  /** Compressed data */
45  const void *compressed;
46  /** Length of compressed data */
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 gzip test */
60 #define GZIP( name, COMPRESSED, EXPECTED ) \
61  static const uint8_t name ## _compressed[] = COMPRESSED; \
62  static const uint8_t name ## _expected[] = EXPECTED; \
63  static struct gzip_test name = { \
64  .compressed_name = #name ".gz", \
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 GZIP ( hello_world,
74  DATA ( 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
75  0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca,
76  0x49, 0x01, 0x00, 0x52, 0x9e, 0xd6, 0x8b, 0x0b, 0x00, 0x00,
77  0x00 ),
78  DATA ( 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c,
79  0x64 ) );
80 
81 /** "Hello filename" */
82 GZIP ( hello_filename,
83  DATA ( 0x1f, 0x8b, 0x08, 0x08, 0xeb, 0x5b, 0x96, 0x60, 0x00, 0x03,
84  0x68, 0x77, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xf3, 0x48, 0xcd,
85  0xc9, 0xc9, 0x57, 0x48, 0xcb, 0xcc, 0x49, 0xcd, 0x4b, 0xcc,
86  0x4d, 0x05, 0x00, 0x69, 0x37, 0x25, 0x3c, 0x0e, 0x00, 0x00,
87  0x00 ),
88  DATA ( 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x66, 0x69, 0x6c, 0x65,
89  0x6e, 0x61, 0x6d, 0x65 ) );
90 
91 /** "Hello assorted headers" */
92 GZIP ( hello_headers,
93  DATA ( 0x1f, 0x8b, 0x08, 0x1c, 0x11, 0x5c, 0x96, 0x60, 0x00, 0x03,
94  0x05, 0x00, 0x41, 0x70, 0x01, 0x00, 0x0d, 0x68, 0x77, 0x2e,
95  0x74, 0x78, 0x74, 0x00, 0x2f, 0x2f, 0x77, 0x68, 0x79, 0x3f,
96  0x00, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x48, 0x2c, 0x2e,
97  0xce, 0x2f, 0x2a, 0x49, 0x4d, 0x51, 0xc8, 0x48, 0x4d, 0x4c,
98  0x49, 0x2d, 0x2a, 0x06, 0x00, 0x59, 0xa4, 0x19, 0x61, 0x16,
99  0x00, 0x00, 0x00 ),
100  DATA ( 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x61, 0x73, 0x73, 0x6f,
101  0x72, 0x74, 0x65, 0x64, 0x20, 0x68, 0x65, 0x61, 0x64, 0x65,
102  0x72, 0x73 ) );
103 
104 /**
105  * Report gzip test result
106  *
107  * @v test gzip test
108  * @v file Test code file
109  * @v line Test code line
110  */
111 static void gzip_okx ( struct gzip_test *test, const char *file,
112  unsigned int line ) {
113  struct image *image;
114  struct image *extracted;
115 
116  /* Construct compressed image */
117  image = image_memory ( test->compressed_name,
118  virt_to_user ( 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_user ( extracted->data, 0,
132  virt_to_user ( test->expected ), 0,
133  test->expected_len ) == 0, file, line );
134 
135  /* Verify extracted image name */
136  okx ( strcmp ( extracted->name, test->expected_name ) == 0,
137  file, line );
138 
139  /* Unregister images */
140  unregister_image ( extracted );
142 }
143 #define gzip_ok( test ) gzip_okx ( test, __FILE__, __LINE__ )
144 
145 /**
146  * Perform gzip self-test
147  *
148  */
149 static void gzip_test_exec ( void ) {
150 
151  gzip_ok ( &hello_world );
152  gzip_ok ( &hello_filename );
153  gzip_ok ( &hello_headers );
154 }
155 
156 /** gzip self-test */
158  .name = "gzip",
159  .exec = gzip_test_exec,
160 };
userptr_t data
Raw file image.
Definition: image.h:41
struct image_type * type
Image type, if known.
Definition: image.h:46
struct image * image_memory(const char *name, userptr_t data, size_t len)
Create registered image from block of memory.
Definition: image.c:569
Self-test infrastructure.
const char * name
Test set name.
Definition: test.h:17
An executable image.
Definition: image.h:24
A self-test set.
Definition: test.h:15
struct self_test gzip_test __self_test
gzip self-test
Definition: gzip_test.c:157
const char * compressed_name
Compressed filename.
Definition: gzip_test.c:43
const void * compressed
Compressed data.
Definition: gzip_test.c:45
#define okx(success, file, line)
Report test result.
Definition: test.h:44
gzip compressed images
int image_extract(struct image *image, const char *name, struct image **extracted)
Extract archive image.
Definition: archive.c:44
Executable images.
A gzip test.
Definition: gzip_test.c:41
const void * expected
Expected uncompressed data.
Definition: gzip_test.c:51
size_t len
Length of raw file image.
Definition: image.h:43
static void gzip_test_exec(void)
Perform gzip self-test.
Definition: gzip_test.c:149
size_t compressed_len
Length of compressed data.
Definition: gzip_test.c:47
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define gzip_ok(test)
Definition: gzip_test.c:143
#define GZIP(name, COMPRESSED, EXPECTED)
Define a gzip test.
Definition: gzip_test.c:60
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:322
const char * expected_name
Expected uncompressed name.
Definition: gzip_test.c:49
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:173
#define DATA(...)
Define inline data.
Definition: gzip_test.c:57
int memcmp_user(userptr_t first, off_t first_off, userptr_t second, off_t second_off, size_t len)
Compare data between user buffers.
userptr_t virt_to_user(volatile const void *addr)
Convert virtual address to user pointer.
static void gzip_okx(struct gzip_test *test, const char *file, unsigned int line)
Report gzip test result.
Definition: gzip_test.c:111
char * name
Name.
Definition: image.h:34
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
size_t expected_len
Length of expected uncompressed data.
Definition: gzip_test.c:53
static int test
Definition: epic100.c:73