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
24FILE_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 */
41struct 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 */
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" */
73ZLIB ( 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 */
86static 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, test->compressed,
93 test->compressed_len );
94 okx ( image != NULL, file, line );
95 okx ( image->len == test->compressed_len, file, line );
96
97 /* Check type detection */
98 okx ( image->type == &zlib_image_type, file, line );
99
100 /* Extract archive image */
101 okx ( image_extract ( image, NULL, &extracted ) == 0, file, line );
102
103 /* Verify extracted image content */
104 okx ( extracted->len == test->expected_len, file, line );
105 okx ( memcmp ( extracted->data, test->expected,
106 test->expected_len ) == 0, file, line );
107
108 /* Verify extracted image name */
109 okx ( strcmp ( extracted->name, test->expected_name ) == 0,
110 file, line );
111
112 /* Unregister images */
113 unregister_image ( extracted );
115}
116#define zlib_ok( test ) zlib_okx ( test, __FILE__, __LINE__ )
117
118/**
119 * Perform zlib self-test
120 *
121 */
122static void zlib_test_exec ( void ) {
123
124 zlib_ok ( &hello_world );
125}
126
127/** zlib self-test */
129 .name = "zlib",
130 .exec = zlib_test_exec,
131};
#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
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.
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
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
A zlib test.
Definition zlib_test.c:41
size_t expected_len
Length of expected uncompressed data.
Definition zlib_test.c:53
const char * expected_name
Expected uncompressed name.
Definition zlib_test.c:49
size_t compressed_len
Length of compressed data.
Definition zlib_test.c:47
const char * compressed_name
Compressed filename.
Definition zlib_test.c:43
const void * expected
Expected uncompressed data.
Definition zlib_test.c:51
const void * compressed
Compressed data.
Definition zlib_test.c:45
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
zlib compressed images
#define ZLIB(name, COMPRESSED, EXPECTED)
Define a zlib test.
Definition zlib_test.c:60
#define zlib_ok(test)
Definition zlib_test.c:116
static void zlib_test_exec(void)
Perform zlib self-test.
Definition zlib_test.c:122
static void zlib_okx(struct zlib_test *test, const char *file, unsigned int line)
Report zlib test result.
Definition zlib_test.c:86