iPXE
Data Structures | Defines | 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...

Defines

#define DATA(...)   { __VA_ARGS__ }
 Define inline data.
#define BASE16(name, DATA, ENCODED)
 Define a base16 test.
#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.
 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.


Define Documentation

#define DATA (   ...)    { __VA_ARGS__ }

Define inline data.

Definition at line 51 of file base16_test.c.

#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 a base16 test.

Definition at line 54 of file base16_test.c.

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

Definition at line 94 of file base16_test.c.

Referenced by base16_test_exec().

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

Definition at line 115 of file base16_test.c.

Referenced by base16_test_exec().


Function Documentation

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 
) [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.

References base16_encoded_len(), base16_test::data, base16_test::encoded, base16_test::len, len, okx, strcmp(), and strlen().

                                                    {
        size_t len = base16_encoded_len ( test->len );
        char buf[ len + 1 /* NUL */ ];
        size_t check_len;

        okx ( len == strlen ( test->encoded ), file, line );
        check_len = base16_encode ( test->data, test->len, buf, sizeof ( buf ));
        okx ( check_len == len, file, line );
        okx ( strcmp ( test->encoded, buf ) == 0, file, line );
}
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.

References base16_decoded_max_len(), base16_test::data, base16_test::encoded, base16_test::len, len, max_len, memcmp(), and okx.

                                                    {
        size_t max_len = base16_decoded_max_len ( test->encoded );
        uint8_t buf[max_len];
        int len;

        len = base16_decode ( test->encoded, buf, sizeof ( buf ) );
        okx ( len >= 0, file, line );
        okx ( ( size_t ) len <= max_len, file, line );
        okx ( ( size_t ) len == test->len, file, line );
        okx ( memcmp ( test->data, buf, len ) == 0, file, line );
}
static void base16_test_exec ( void  ) [static]

Perform Base16 self-tests.

Definition at line 121 of file base16_test.c.

References base16_decode_ok, and base16_encode_ok.

                                      {

        base16_encode_ok ( &empty_test );
        base16_decode_ok ( &empty_test );

        base16_encode_ok ( &hw_test );
        base16_decode_ok ( &hw_test );

        base16_encode_ok ( &random_test );
        base16_decode_ok ( &random_test );
}

Variable Documentation

Initial value:
 {
        .name = "base16",
        .exec = base16_test_exec,
}

Base16 self-test.

Definition at line 134 of file base16_test.c.