iPXE
Data Structures | Macros | Functions | Variables
base64_test.c File Reference

Base64 tests. More...

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

Go to the source code of this file.

Data Structures

struct  base64_test
 A Base64 test. More...
 

Macros

#define DATA(...)   { __VA_ARGS__ }
 Define inline data. More...
 
#define BASE64(name, DATA, ENCODED)
 Define a base64 test. More...
 
#define base64_encode_ok(test)   base64_encode_okx ( test, __FILE__, __LINE__ )
 
#define base64_decode_ok(test)   base64_decode_okx ( test, __FILE__, __LINE__ )
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
 BASE64 (empty_test, DATA(), "")
 Empty data test. More...
 
 BASE64 (hw_test, DATA( 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'), "SGVsbG8gd29ybGQ=")
 "Hello world" test More...
 
 BASE64 (random_test, DATA(0x36, 0x03, 0x84, 0xdc, 0x4e, 0x03, 0x46, 0xa0, 0xb5, 0x2d, 0x03, 0x6e, 0xd0, 0x56, 0xed, 0xa0, 0x37, 0x02, 0xac, 0xc6, 0x65, 0xd1), "NgOE3E4DRqC1LQNu0FbtoDcCrMZl0Q==")
 Random data test. More...
 
static void base64_encode_okx (struct base64_test *test, const char *file, unsigned int line)
 Report a base64 encoding test result. More...
 
static void base64_decode_okx (struct base64_test *test, const char *file, unsigned int line)
 Report a base64 decoding test result. More...
 
static void base64_test_exec (void)
 Perform Base64 self-tests. More...
 

Variables

struct self_test base64_test __self_test
 Base64 self-test. More...
 

Detailed Description

Base64 tests.

Test vectors generated using "base64 -w 0"

Definition in file base64_test.c.

Macro Definition Documentation

◆ DATA

#define DATA (   ...)    { __VA_ARGS__ }

Define inline data.

Definition at line 53 of file base64_test.c.

◆ BASE64

#define BASE64 (   name,
  DATA,
  ENCODED 
)
Value:
static const uint8_t name ## _data[] = DATA; \
static struct base64_test name = { \
.data = name ## _data, \
.len = sizeof ( name ## _data ), \
.encoded = ENCODED, \
}
const char * name
Definition: ath9k_hw.c:1984
A Base64 test.
Definition: base64_test.c:43
unsigned char uint8_t
Definition: stdint.h:10
const char * encoded
Base64-encoded data.
Definition: base64_test.c:49
#define DATA(...)
Define inline data.
Definition: base64_test.c:53

Define a base64 test.

Definition at line 56 of file base64_test.c.

◆ base64_encode_ok

#define base64_encode_ok (   test)    base64_encode_okx ( test, __FILE__, __LINE__ )

Definition at line 97 of file base64_test.c.

◆ base64_decode_ok

#define base64_decode_ok (   test)    base64_decode_okx ( test, __FILE__, __LINE__ )

Definition at line 118 of file base64_test.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ BASE64() [1/3]

BASE64 ( empty_test  ,
DATA()  ,
""   
)

Empty data test.

◆ BASE64() [2/3]

BASE64 ( hw_test  ,
DATA('H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd')   
)

"Hello world" test

◆ BASE64() [3/3]

BASE64 ( random_test  ,
DATA(0x36, 0x03, 0x84, 0xdc, 0x4e, 0x03, 0x46, 0xa0, 0xb5, 0x2d, 0x03, 0x6e, 0xd0, 0x56, 0xed, 0xa0, 0x37, 0x02, 0xac, 0xc6, 0x65, 0xd1)   
)

Random data test.

◆ base64_encode_okx()

static void base64_encode_okx ( struct base64_test test,
const char *  file,
unsigned int  line 
)
static

Report a base64 encoding test result.

Parameters
testBase64 test
fileTest code file
lineTest code line

Definition at line 86 of file base64_test.c.

87  {
88  size_t len = base64_encoded_len ( test->len );
89  char buf[ len + 1 /* NUL */ ];
90  size_t check_len;
91 
92  okx ( len == strlen ( test->encoded ), file, line );
93  check_len = base64_encode ( test->data, test->len, buf, sizeof ( buf ));
94  okx ( check_len == len, file, line );
95  okx ( strcmp ( test->encoded, buf ) == 0, file, line );
96 }
static size_t base64_encoded_len(size_t raw_len)
Calculate length of base64-encoded data.
Definition: base64.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:213
uint32_t len
Length.
Definition: ena.h:14
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:157
static int test
Definition: epic100.c:73
size_t base64_encode(const void *raw, size_t raw_len, char *data, size_t len)
Base64-encode data.
Definition: base64.c:51

References base64_encode(), base64_encoded_len(), len, okx, strcmp(), strlen(), and test.

◆ base64_decode_okx()

static void base64_decode_okx ( struct base64_test test,
const char *  file,
unsigned int  line 
)
static

Report a base64 decoding test result.

Parameters
testBase64 test
fileTest code file
lineTest code line

Definition at line 106 of file base64_test.c.

107  {
108  size_t max_len = base64_decoded_max_len ( test->encoded );
109  uint8_t buf[max_len];
110  int len;
111 
112  len = base64_decode ( test->encoded, buf, sizeof ( buf ) );
113  okx ( len >= 0, file, line );
114  okx ( ( size_t ) len <= max_len, file, line );
115  okx ( ( size_t ) len == test->len, file, line );
116  okx ( memcmp ( test->data, buf, len ) == 0, file, line );
117 }
static void size_t size_t max_len
Definition: entropy.h:153
int base64_decode(const char *encoded, void *data, size_t len)
Base64-decode string.
Definition: base64.c:91
static size_t base64_decoded_max_len(const char *encoded)
Calculate maximum length of base64-decoded string.
Definition: base64.h:34
#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:98
static int test
Definition: epic100.c:73

References base64_decode(), base64_decoded_max_len(), len, max_len, memcmp(), okx, and test.

◆ base64_test_exec()

static void base64_test_exec ( void  )
static

Perform Base64 self-tests.

Definition at line 124 of file base64_test.c.

124  {
125 
126  base64_encode_ok ( &empty_test );
127  base64_decode_ok ( &empty_test );
128 
129  base64_encode_ok ( &hw_test );
130  base64_decode_ok ( &hw_test );
131 
132  base64_encode_ok ( &random_test );
133  base64_decode_ok ( &random_test );
134 }
#define base64_decode_ok(test)
Definition: base64_test.c:118
#define base64_encode_ok(test)
Definition: base64_test.c:97

References base64_decode_ok, and base64_encode_ok.

Variable Documentation

◆ __self_test

struct self_test base64_test __self_test
Initial value:
= {
.name = "base64",
}
static void base64_test_exec(void)
Perform Base64 self-tests.
Definition: base64_test.c:124

Base64 self-test.

Definition at line 137 of file base64_test.c.