iPXE
Data Structures | Macros | Functions | Variables
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(...)   { __VA_ARGS__ }
 Define inline data. More...
 
#define BASE16(name, DATA, ENCODED)
 Define a base16 test. More...
 
#define base16_encode_ok(test)   base16_encode_okx ( test, __FILE__, __LINE__ )
 
#define base16_decode_ok(test)   base16_decode_okx ( test, __FILE__, __LINE__ )
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
 BASE16 (empty_test, DATA(), "")
 Empty data test. More...
 
 BASE16 (hw_test, DATA( 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'), "48656c6c6f20776f726c64")
 "Hello world" test More...
 
 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. More...
 
static void base16_encode_okx (struct base16_test *test, const char *file, unsigned int line)
 Report a base16 encoding test result. More...
 
static void base16_decode_okx (struct base16_test *test, const char *file, unsigned int line)
 Report a base16 decoding test result. More...
 
static void base16_test_exec (void)
 Perform Base16 self-tests. More...
 

Variables

struct self_test base16_test __self_test
 Base16 self-test. More...
 

Detailed Description

Base16 tests.

Definition in file base16_test.c.

Macro Definition Documentation

◆ DATA

#define DATA (   ...)    { __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, \
}
const char * name
Definition: ath9k_hw.c:1984
#define DATA(...)
Define inline data.
Definition: base16_test.c:51
const char * encoded
Base16-encoded data.
Definition: base16_test.c:47
A Base16 test.
Definition: base16_test.c:41
unsigned char uint8_t
Definition: stdint.h:10

Define a base16 test.

Definition at line 54 of file base16_test.c.

◆ base16_encode_ok

#define base16_encode_ok (   test)    base16_encode_okx ( test, __FILE__, __LINE__ )

Definition at line 94 of file base16_test.c.

◆ base16_decode_ok

#define base16_decode_ok (   test)    base16_decode_okx ( test, __FILE__, __LINE__ )

Definition at line 115 of file base16_test.c.

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

◆ 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.

◆ base16_encode_okx()

static 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:21
#define okx(success, file, line)
Report test result.
Definition: test.h:44
size_t strlen(const char *src)
Get length of string.
Definition: string.c:228
uint32_t len
Length.
Definition: ena.h:14
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:172
static int test
Definition: epic100.c:73

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

◆ base16_decode_okx()

static 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 void size_t size_t max_len
Definition: entropy.h:153
static size_t base16_decoded_max_len(const char *encoded)
Calculate maximum length of base16-decoded string.
Definition: base16.h:31
#define okx(success, file, line)
Report test result.
Definition: test.h:44
unsigned char uint8_t
Definition: stdint.h:10
uint32_t len
Length.
Definition: ena.h:14
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:113
static int test
Definition: epic100.c:73

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

◆ base16_test_exec()

static 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_encode_ok(test)
Definition: base16_test.c:94
#define base16_decode_ok(test)
Definition: base16_test.c:115

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.
Definition: base16_test.c:121

Base16 self-test.

Definition at line 134 of file base16_test.c.