iPXE
base16_test.c File Reference

Base16 tests. More...

#include <stdint.h>
#include <string.h>
#include <ipxe/base16.h>
#include <ipxe/test.h>

Go to the source code of this file.

Data Structures

struct  base16_test
 A Base16 test. More...

Macros

#define DATA(...)
 Define inline data.
#define BASE16(name, DATA, ENCODED)
 Define a base16 test.
#define base16_encode_ok(test)
#define base16_decode_ok(test)

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 BASE16 (empty_test, DATA(), "")
 Empty data test.
 BASE16 (hw_test, DATA( 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'), "48656c6c6f20776f726c64")
 "Hello world" test
 BASE16 (random_test, DATA(0x8b, 0x1a, 0xa2, 0x6c, 0xa9, 0x38, 0x43, 0xb8, 0x81, 0xf8, 0x30, 0x44, 0xb2, 0x32, 0x6e, 0x82, 0xfe, 0x0f, 0x84, 0x91), "8b1aa26ca93843b881f83044b2326e82fe0f8491")
 Random data test.
static void base16_encode_okx (struct base16_test *test, const char *file, unsigned int line)
 Report a base16 encoding test result.
static void base16_decode_okx (struct base16_test *test, const char *file, unsigned int line)
 Report a base16 decoding test result.
static void base16_test_exec (void)
 Perform Base16 self-tests.

Variables

struct self_test base16_test __self_test
 Base16 self-test.

Detailed Description

Base16 tests.

Definition in file base16_test.c.

Macro Definition Documentation

◆ DATA

#define DATA ( ...)
Value:
{ __VA_ARGS__ }

Define inline data.

Definition at line 51 of file base16_test.c.

◆ BASE16

#define BASE16 ( name,
DATA,
ENCODED )
Value:
static const uint8_t name ## _data[] = DATA; \
static struct base16_test name = { \
.data = name ## _data, \
.len = sizeof ( name ## _data ), \
.encoded = ENCODED, \
}
#define DATA(...)
Define inline data.
Definition acpi_test.c:74
unsigned char uint8_t
Definition stdint.h:10
const char * name
Definition ath9k_hw.c:1986
A Base16 test.
Definition base16_test.c:41

Define a base16 test.

Definition at line 54 of file base16_test.c.

54#define BASE16( name, DATA, ENCODED ) \
55 static const uint8_t name ## _data[] = DATA; \
56 static struct base16_test name = { \
57 .data = name ## _data, \
58 .len = sizeof ( name ## _data ), \
59 .encoded = ENCODED, \
60 }

◆ base16_encode_ok

#define base16_encode_ok ( test)
Value:
base16_encode_okx ( test, __FILE__, __LINE__ )
static void base16_encode_okx(struct base16_test *test, const char *file, unsigned int line)
Report a base16 encoding test result.
Definition base16_test.c:83
static int test
Definition epic100.c:73

Definition at line 94 of file base16_test.c.

Referenced by base16_test_exec().

◆ base16_decode_ok

#define base16_decode_ok ( test)
Value:
base16_decode_okx ( test, __FILE__, __LINE__ )
static void base16_decode_okx(struct base16_test *test, const char *file, unsigned int line)
Report a base16 decoding test result.

Definition at line 115 of file base16_test.c.

Referenced by base16_test_exec().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ BASE16() [1/3]

BASE16 ( empty_test ,
DATA() ,
""  )

Empty data test.

◆ BASE16() [2/3]

BASE16 ( hw_test ,
DATA('H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd') ,
"48656c6c6f20776f726c64"  )

"Hello world" test

References DATA.

◆ BASE16() [3/3]

BASE16 ( random_test ,
DATA(0x8b, 0x1a, 0xa2, 0x6c, 0xa9, 0x38, 0x43, 0xb8, 0x81, 0xf8, 0x30, 0x44, 0xb2, 0x32, 0x6e, 0x82, 0xfe, 0x0f, 0x84, 0x91) ,
"8b1aa26ca93843b881f83044b2326e82fe0f8491"  )

Random data test.

References DATA.

◆ base16_encode_okx()

void base16_encode_okx ( struct base16_test * test,
const char * file,
unsigned int line )
static

Report a base16 encoding test result.

Parameters
testBase16 test
fileTest code file
lineTest code line

Definition at line 83 of file base16_test.c.

84 {
85 size_t len = base16_encoded_len ( test->len );
86 char buf[ len + 1 /* NUL */ ];
87 size_t check_len;
88
89 okx ( len == strlen ( test->encoded ), file, line );
90 check_len = base16_encode ( test->data, test->len, buf, sizeof ( buf ));
91 okx ( check_len == len, file, line );
92 okx ( strcmp ( test->encoded, buf ) == 0, file, line );
93}
static size_t base16_encoded_len(size_t raw_len)
Calculate length of base16-encoded data.
Definition base16.h:25
ring len
Length.
Definition dwmac.h:226
int strcmp(const char *first, const char *second)
Compare strings.
Definition string.c:174
size_t strlen(const char *src)
Get length of string.
Definition string.c:244
#define okx(success, file, line)
Report test result.
Definition test.h:44

References base16_encoded_len(), len, okx, strcmp(), strlen(), and test.

◆ base16_decode_okx()

void base16_decode_okx ( struct base16_test * test,
const char * file,
unsigned int line )
static

Report a base16 decoding test result.

Parameters
testBase16 test
fileTest code file
lineTest code line

Definition at line 103 of file base16_test.c.

104 {
105 size_t max_len = base16_decoded_max_len ( test->encoded );
106 uint8_t buf[max_len];
107 int len;
108
109 len = base16_decode ( test->encoded, buf, sizeof ( buf ) );
110 okx ( len >= 0, file, line );
111 okx ( ( size_t ) len <= max_len, file, line );
112 okx ( ( size_t ) len == test->len, file, line );
113 okx ( memcmp ( test->data, buf, len ) == 0, file, line );
114}
static size_t base16_decoded_max_len(const char *encoded)
Calculate maximum length of base16-decoded string.
Definition base16.h:35
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115

References base16_decoded_max_len(), len, memcmp(), okx, and test.

◆ base16_test_exec()

void base16_test_exec ( void )
static

Perform Base16 self-tests.

Definition at line 121 of file base16_test.c.

121 {
122
123 base16_encode_ok ( &empty_test );
124 base16_decode_ok ( &empty_test );
125
126 base16_encode_ok ( &hw_test );
127 base16_decode_ok ( &hw_test );
128
129 base16_encode_ok ( &random_test );
130 base16_decode_ok ( &random_test );
131}
#define base16_decode_ok(test)
#define base16_encode_ok(test)
Definition base16_test.c:94

References base16_decode_ok, and base16_encode_ok.

Variable Documentation

◆ __self_test

struct self_test base16_test __self_test
Initial value:
= {
.name = "base16",
}
static void base16_test_exec(void)
Perform Base16 self-tests.

Base16 self-test.

Definition at line 134 of file base16_test.c.

134 {
135 .name = "base16",
136 .exec = base16_test_exec,
137};