iPXE
zlib_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  * 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 */
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  */
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 );
117 }
118 #define zlib_ok( test ) zlib_okx ( test, __FILE__, __LINE__ )
119 
120 /**
121  * Perform zlib self-test
122  *
123  */
124 static void zlib_test_exec ( void ) {
125 
126  zlib_ok ( &hello_world );
127 }
128 
129 /** zlib self-test */
131  .name = "zlib",
132  .exec = zlib_test_exec,
133 };
userptr_t data
Raw file image.
Definition: image.h:41
const char * compressed_name
Compressed filename.
Definition: zlib_test.c:43
const void * compressed
Compressed data.
Definition: zlib_test.c:45
struct image_type * type
Image type, if known.
Definition: image.h:46
const char * expected_name
Expected uncompressed name.
Definition: zlib_test.c:49
struct image * image_memory(const char *name, userptr_t data, size_t len)
Create registered image from block of memory.
Definition: image.c:550
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
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
struct self_test zlib_test __self_test
zlib self-test
Definition: zlib_test.c:130
#define okx(success, file, line)
Report test result.
Definition: test.h:44
zlib compressed images
#define ZLIB(name, COMPRESSED, EXPECTED)
Define a zlib test.
Definition: zlib_test.c:60
int image_extract(struct image *image, const char *name, struct image **extracted)
Extract archive image.
Definition: archive.c:44
Executable images.
static void zlib_test_exec(void)
Perform zlib self-test.
Definition: zlib_test.c:124
#define zlib_ok(test)
Definition: zlib_test.c:118
size_t len
Length of raw file image.
Definition: image.h:43
size_t compressed_len
Length of compressed data.
Definition: zlib_test.c:47
#define DATA(...)
Define inline data.
Definition: zlib_test.c:57
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:303
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:173
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.
size_t expected_len
Length of expected uncompressed data.
Definition: zlib_test.c:53
static void zlib_okx(struct zlib_test *test, const char *file, unsigned int line)
Report zlib test result.
Definition: zlib_test.c:86
char * name
Name.
Definition: image.h:34
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
A zlib test.
Definition: zlib_test.c:41
static int test
Definition: epic100.c:73
const void * expected
Expected uncompressed data.
Definition: zlib_test.c:51