iPXE
cpio_test.c File Reference

CPIO self-tests. More...

#include <stdlib.h>
#include <string.h>
#include <ipxe/cpio.h>
#include <ipxe/test.h>

Go to the source code of this file.

Data Structures

struct  cpio_test
 A CPIO test. More...

Macros

#define CPIO_HEADER(mode, filesize, namesize, pname)
 Define an expected CPIO header.
#define PAD1   "\0"
 Define a one-byte padding.
#define PAD2   "\0\0"
 Define a two-byte padding.
#define PAD3   "\0\0\0"
 Define a three-byte padding.
#define PAD4   "\0\0\0\0"
 Define four-byte padding.
#define CPIO_TEST(NAME, LEN, CMDLINE, COUNT, EXPECTED)
 Define a CPIO test.
#define cpio_ok(test)

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static void cpio_okx (struct cpio_test *test, const char *file, unsigned int line)
 Report a CPIO test result.
 CPIO_TEST (no_cmdline, 42, NULL, 0, "")
 CPIO_TEST (empty_cmdline, 154, "", 0, "")
 CPIO_TEST (all_slashes, 64, "////", 0, "")
 CPIO_TEST (simple, 0x69, "wimboot", 1, CPIO_HEADER("000081a4", "00000069", "00000008", "wimboot" PAD3))
 CPIO_TEST (init_slash, 0x273, "/wimboot", 1, CPIO_HEADER("000081a4", "00000273", "00000009", "/wimboot" PAD2))
 CPIO_TEST (init_slashes, 0x94, "///initscript", 1, CPIO_HEADER("000081a4", "00000094", "0000000e", "///initscript" PAD1))
 CPIO_TEST (path, 0x341, "/usr/share/oem/config.ign", 1, CPIO_HEADER("000081a4", "00000341", "0000001a", "/usr/share/oem/config.ign" PAD1))
 CPIO_TEST (path_mkdir_0, 0x341, "/usr/share/oem/config.ign mkdir=0", 1, CPIO_HEADER("000081a4", "00000341", "0000001a", "/usr/share/oem/config.ign" PAD1))
 CPIO_TEST (path_mkdir_1, 0x341, "/usr/share/oem/config.ign mkdir=1", 2, CPIO_HEADER("000041ed", "00000000", "0000000f", "/usr/share/oem" PAD4) CPIO_HEADER("000081a4", "00000341", "0000001a", "/usr/share/oem/config.ign" PAD1))
 CPIO_TEST (path_mkdir_2, 0x341, "/usr/share/oem/config.ign mkdir=2", 3, CPIO_HEADER("000041ed", "00000000", "0000000b", "/usr/share" PAD4) CPIO_HEADER("000041ed", "00000000", "0000000f", "/usr/share/oem" PAD4) CPIO_HEADER("000081a4", "00000341", "0000001a", "/usr/share/oem/config.ign" PAD1))
 CPIO_TEST (path_mkdir_all, 0x341, "/usr/share/oem/config.ign mkdir=-1", 4, CPIO_HEADER("000041ed", "00000000", "00000005", "/usr" PAD2) CPIO_HEADER("000041ed", "00000000", "0000000b", "/usr/share" PAD4) CPIO_HEADER("000041ed", "00000000", "0000000f", "/usr/share/oem" PAD4) CPIO_HEADER("000081a4", "00000341", "0000001a", "/usr/share/oem/config.ign" PAD1))
 CPIO_TEST (dir, 0, "/opt/", 1, CPIO_HEADER("000041ed", "00000000", "00000005", "/opt" PAD2))
 CPIO_TEST (tree, 0, "/opt/oem/scripts/ mkdir=-1", 3, CPIO_HEADER("000041ed", "00000000", "00000005", "/opt" PAD2) CPIO_HEADER("000041ed", "00000000", "00000009", "/opt/oem" PAD2) CPIO_HEADER("000041ed", "00000000", "00000011", "/opt/oem/scripts" PAD2))
 CPIO_TEST (mode, 39, "/sbin/init mode=755", 1, CPIO_HEADER("000081ed", "00000027", "0000000b", "/sbin/init" PAD4))
 CPIO_TEST (chaos, 73, "///etc//init.d///runthings mode=700 mkdir=99", 3, CPIO_HEADER("000041ed", "00000000", "00000007", "///etc" PAD4) CPIO_HEADER("000041ed", "00000000", "0000000f", "///etc//init.d" PAD4) CPIO_HEADER("000081c0", "00000049", "0000001b", "///etc//init.d///runthings" PAD4))
static void cpio_test_exec (void)
 Perform CPIO self-test.

Variables

struct self_test cpio_test __self_test
 CPIO self-test.

Detailed Description

CPIO self-tests.

Definition in file cpio_test.c.

Macro Definition Documentation

◆ CPIO_HEADER

#define CPIO_HEADER ( mode,
filesize,
namesize,
pname )
Value:
"070701" "00000000" mode "00000000" "00000000" "00000001" \
"00000000" filesize "00000000" "00000000" "00000000" "00000000" \
namesize "00000000" pname
uint16_t mode
Acceleration mode.
Definition ena.h:15

Define an expected CPIO header.

Definition at line 57 of file cpio_test.c.

57#define CPIO_HEADER( mode, filesize, namesize, pname ) \
58 "070701" "00000000" mode "00000000" "00000000" "00000001" \
59 "00000000" filesize "00000000" "00000000" "00000000" "00000000" \
60 namesize "00000000" pname

Referenced by CPIO_TEST(), CPIO_TEST(), CPIO_TEST(), CPIO_TEST(), CPIO_TEST(), CPIO_TEST(), CPIO_TEST(), CPIO_TEST(), CPIO_TEST(), CPIO_TEST(), CPIO_TEST(), and CPIO_TEST().

◆ PAD1

#define PAD1   "\0"

Define a one-byte padding.

Definition at line 63 of file cpio_test.c.

Referenced by CPIO_TEST(), CPIO_TEST(), CPIO_TEST(), CPIO_TEST(), CPIO_TEST(), and CPIO_TEST().

◆ PAD2

#define PAD2   "\0\0"

Define a two-byte padding.

Definition at line 66 of file cpio_test.c.

Referenced by CPIO_TEST(), CPIO_TEST(), CPIO_TEST(), and CPIO_TEST().

◆ PAD3

#define PAD3   "\0\0\0"

Define a three-byte padding.

Definition at line 69 of file cpio_test.c.

Referenced by CPIO_TEST().

◆ PAD4

#define PAD4   "\0\0\0\0"

Define four-byte padding.

Definition at line 72 of file cpio_test.c.

Referenced by CPIO_TEST(), CPIO_TEST(), CPIO_TEST(), CPIO_TEST(), and CPIO_TEST().

◆ CPIO_TEST

#define CPIO_TEST ( NAME,
LEN,
CMDLINE,
COUNT,
EXPECTED )
Value:
static const uint8_t NAME ## _expected[] = EXPECTED; \
static struct cpio_test NAME = { \
.name = #NAME, \
.len = LEN, \
.cmdline = CMDLINE, \
.expected = NAME ## _expected, \
.expected_len = ( sizeof ( NAME ## _expected ) \
- 1 /* NUL */ ), \
.expected_count = COUNT, \
};
unsigned char uint8_t
Definition stdint.h:10
#define EXPECTED(...)
Define inline expected result point.
A CPIO test.
Definition cpio_test.c:41
size_t expected_len
Length of expected CPIO headers.
Definition cpio_test.c:51
size_t len
Image length.
Definition cpio_test.c:45

Define a CPIO test.

Definition at line 75 of file cpio_test.c.

75#define CPIO_TEST( NAME, LEN, CMDLINE, COUNT, EXPECTED ) \
76 static const uint8_t NAME ## _expected[] = EXPECTED; \
77 static struct cpio_test NAME = { \
78 .name = #NAME, \
79 .len = LEN, \
80 .cmdline = CMDLINE, \
81 .expected = NAME ## _expected, \
82 .expected_len = ( sizeof ( NAME ## _expected ) \
83 - 1 /* NUL */ ), \
84 .expected_count = COUNT, \
85 };

◆ cpio_ok

#define cpio_ok ( test)
Value:
cpio_okx ( test, __FILE__, __LINE__ )
static void cpio_okx(struct cpio_test *test, const char *file, unsigned int line)
Report a CPIO test result.
Definition cpio_test.c:94
static int test
Definition epic100.c:73

Definition at line 162 of file cpio_test.c.

Referenced by cpio_test_exec().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ cpio_okx()

void cpio_okx ( struct cpio_test * test,
const char * file,
unsigned int line )
static

Report a CPIO test result.

Parameters
testCPIO test
fileTest code file
lineTest code line

Definition at line 94 of file cpio_test.c.

95 {
96 struct cpio_header cpio;
97 struct image *image;
99 size_t len;
100 size_t cpio_len;
101 unsigned int i;
102 unsigned int j;
103
104 DBGC ( test, "CPIO len %#zx cmdline \"%s\"\n",
105 test->len, test->cmdline );
106 DBGC2_HDA ( test, 0, test->expected, test->expected_len );
107
108 /* Sanity check */
109 okx ( ( test->expected_len % CPIO_ALIGN ) == 0, file, line );
110
111 /* Construct dummy image */
112 image = alloc_image ( NULL );
113 okx ( image != NULL, file, line );
114 okx ( image_set_name ( image, test->name ) == 0, file, line );
115 okx ( image_set_len ( image, test->len ) == 0, file, line );
116 okx ( image_set_cmdline ( image, test->cmdline ) == 0, file, line );
117
118 /* Calculate length of CPIO headers */
119 len = 0;
120 for ( i = 0 ; ( cpio_len = cpio_header ( image, i, &cpio ) ) ; i++ ) {
121 okx ( cpio_len >= sizeof ( cpio ), file, line );
122 len += ( cpio_len + cpio_pad_len ( cpio_len ) );
123 okx ( cpio_pad_len ( cpio_len ) > 0, file, line );
124 okx ( ( len % CPIO_ALIGN ) == 0, file, line );
125 }
126 okx ( i == test->expected_count, file, line );
127 okx ( len == test->expected_len, file, line );
128
129 /* Allocate space for CPIO headers */
130 data = zalloc ( len );
131 okx ( data != NULL, file, line );
132
133 /* Construct CPIO headers */
134 len = 0;
135 for ( i = 0 ; ( cpio_len = cpio_header ( image, i, &cpio ) ) ; i++ ) {
136 memcpy ( ( data + len ), &cpio, sizeof ( cpio ) );
137 memcpy ( ( data + len + sizeof ( cpio ) ), cpio_name ( image ),
138 ( cpio_len - sizeof ( cpio ) ) );
139 DBGC ( test, "CPIO hdr %d: ", i );
140 for ( j = 0 ; j < cpio_len ; j++ ) {
141 if ( ( j <= sizeof ( cpio ) && ! ( ( j + 2 ) % 8 ) ) )
142 DBGC ( test, " " );
143 DBGC ( test, "%c", data[ len + j ] );
144 }
145 DBGC ( test, "\n" );
146 len += ( cpio_len + cpio_pad_len ( cpio_len ) );
147 }
148 okx ( i == test->expected_count, file, line );
149 okx ( len == test->expected_len, file, line );
150
151 /* Verify constructed CPIO headers */
152 DBGC2_HDA ( test, 0, data, len );
153 okx ( memcmp ( data, test->expected, test->expected_len ) == 0,
154 file, line );
155
156 /* Free constructed headers */
157 free ( data );
158
159 /* Drop reference to dummy image */
160 image_put ( image );
161}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
size_t cpio_header(struct image *image, unsigned int index, struct cpio_header *cpio)
Construct CPIO header for image, if applicable.
Definition cpio.c:156
#define CPIO_ALIGN
CPIO header length alignment.
Definition cpio.h:62
static size_t cpio_pad_len(size_t len)
Get CPIO header zero-padding length.
Definition cpio.h:82
static const char * cpio_name(struct image *image)
Get CPIO image name.
Definition cpio.h:71
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
#define DBGC2_HDA(...)
Definition compiler.h:523
#define DBGC(...)
Definition compiler.h:505
struct image * alloc_image(struct uri *uri)
Allocate executable image.
Definition image.c:124
int image_set_cmdline(struct image *image, const char *cmdline)
Set image command line.
Definition image.c:226
int image_set_name(struct image *image, const char *name)
Set image name.
Definition image.c:181
int image_set_len(struct image *image, size_t len)
Set image length.
Definition image.c:245
static void image_put(struct image *image)
Decrement reference count on an image.
Definition image.h:250
void * memcpy(void *dest, const void *src, size_t len) __nonnull
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:662
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115
A CPIO archive header.
Definition cpio.h:21
An executable image.
Definition image.h:24
#define okx(success, file, line)
Report test result.
Definition test.h:44

References alloc_image(), CPIO_ALIGN, cpio_header(), cpio_name(), cpio_pad_len(), data, DBGC, DBGC2_HDA, free, image_put(), image_set_cmdline(), image_set_len(), image_set_name(), len, memcmp(), memcpy(), NULL, okx, test, and zalloc().

◆ CPIO_TEST() [1/15]

CPIO_TEST ( no_cmdline ,
42 ,
NULL ,
0 ,
""  )

References NULL.

◆ CPIO_TEST() [2/15]

CPIO_TEST ( empty_cmdline ,
154 ,
"" ,
0 ,
""  )

◆ CPIO_TEST() [3/15]

CPIO_TEST ( all_slashes ,
64  )

◆ CPIO_TEST() [4/15]

CPIO_TEST ( simple ,
0x69 ,
"wimboot" ,
1 ,
CPIO_HEADER("000081a4", "00000069", "00000008", "wimboot" PAD3)  )

References CPIO_HEADER, and PAD3.

◆ CPIO_TEST() [5/15]

CPIO_TEST ( init_slash ,
0x273 ,
"/wimboot" ,
1 ,
CPIO_HEADER("000081a4", "00000273", "00000009", "/wimboot" PAD2)  )

References CPIO_HEADER, and PAD2.

◆ CPIO_TEST() [6/15]

CPIO_TEST ( init_slashes ,
0x94  )

References CPIO_HEADER, and PAD1.

◆ CPIO_TEST() [7/15]

CPIO_TEST ( path ,
0x341 ,
"/usr/share/oem/config.ign" ,
1 ,
CPIO_HEADER("000081a4", "00000341", "0000001a", "/usr/share/oem/config.ign" PAD1)  )

References CPIO_HEADER, and PAD1.

◆ CPIO_TEST() [8/15]

CPIO_TEST ( path_mkdir_0 ,
0x341  )

References CPIO_HEADER, and PAD1.

◆ CPIO_TEST() [9/15]

CPIO_TEST ( path_mkdir_1 ,
0x341  )

References CPIO_HEADER, PAD1, and PAD4.

◆ CPIO_TEST() [10/15]

CPIO_TEST ( path_mkdir_2 ,
0x341  )

References CPIO_HEADER, PAD1, and PAD4.

◆ CPIO_TEST() [11/15]

CPIO_TEST ( path_mkdir_all ,
0x341  )

References CPIO_HEADER, PAD1, PAD2, and PAD4.

◆ CPIO_TEST() [12/15]

CPIO_TEST ( dir ,
0 ,
"/opt/" ,
1 ,
CPIO_HEADER("000041ed", "00000000", "00000005", "/opt" PAD2)  )

References CPIO_HEADER, and PAD2.

◆ CPIO_TEST() [13/15]

CPIO_TEST ( tree ,
0  )

References CPIO_HEADER, and PAD2.

◆ CPIO_TEST() [14/15]

CPIO_TEST ( mode ,
39  )

References CPIO_HEADER, mode, and PAD4.

◆ CPIO_TEST() [15/15]

CPIO_TEST ( chaos ,
73  )

References CPIO_HEADER, and PAD4.

◆ cpio_test_exec()

void cpio_test_exec ( void )
static

Perform CPIO self-test.

Definition at line 257 of file cpio_test.c.

257 {
258
259 cpio_ok ( &no_cmdline );
260 cpio_ok ( &empty_cmdline );
261 cpio_ok ( &all_slashes );
262 cpio_ok ( &simple );
263 cpio_ok ( &init_slash );
264 cpio_ok ( &init_slashes );
265 cpio_ok ( &path );
266 cpio_ok ( &path_mkdir_0 );
267 cpio_ok ( &path_mkdir_1 );
268 cpio_ok ( &path_mkdir_2 );
269 cpio_ok ( &path_mkdir_all );
270 cpio_ok ( &dir );
271 cpio_ok ( &tree );
272 cpio_ok ( &mode );
273 cpio_ok ( &chaos );
274}
#define cpio_ok(test)
Definition cpio_test.c:162

References cpio_ok, and mode.

Variable Documentation

◆ __self_test

struct self_test cpio_test __self_test
Initial value:
= {
.name = "cpio",
.exec = cpio_test_exec,
}
static void cpio_test_exec(void)
Perform CPIO self-test.
Definition cpio_test.c:257

CPIO self-test.

Definition at line 277 of file cpio_test.c.

277 {
278 .name = "cpio",
279 .exec = cpio_test_exec,
280};