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

Defines

#define DATA(...)   { __VA_ARGS__ }
 Define inline data.
#define BASE64(name, DATA, ENCODED)
 Define a base64 test.
#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.
 BASE64 (hw_test, DATA( 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'),"SGVsbG8gd29ybGQ=")
 "Hello world" test
 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.
static void base64_encode_okx (struct base64_test *test, const char *file, unsigned int line)
 Report a base64 encoding test result.
static void base64_decode_okx (struct base64_test *test, const char *file, unsigned int line)
 Report a base64 decoding test result.
static void base64_test_exec (void)
 Perform Base64 self-tests.

Variables

struct self_test base64_test __self_test
 Base64 self-test.

Detailed Description

Base64 tests.

Test vectors generated using "base64 -w 0"

Definition in file base64_test.c.


Define Documentation

#define DATA (   ...)    { __VA_ARGS__ }

Define inline data.

Definition at line 53 of file base64_test.c.

#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,                                     \
        }

Define a base64 test.

Definition at line 56 of file base64_test.c.

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

Definition at line 97 of file base64_test.c.

Referenced by base64_test_exec().

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

Definition at line 118 of file base64_test.c.

Referenced by base64_test_exec().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
BASE64 ( empty_test  ,
DATA()  ,
""   
)

Empty data test.

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

"Hello world" test

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.

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.

References base64_encode(), base64_encoded_len(), base64_test::data, base64_test::encoded, base64_test::len, len, okx, strcmp(), and strlen().

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

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

References base64_decode(), base64_decoded_max_len(), base64_test::data, base64_test::encoded, base64_test::len, len, max_len, memcmp(), and okx.

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

        len = base64_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 base64_test_exec ( void  ) [static]

Perform Base64 self-tests.

Definition at line 124 of file base64_test.c.

References base64_decode_ok, and base64_encode_ok.

                                      {

        base64_encode_ok ( &empty_test );
        base64_decode_ok ( &empty_test );

        base64_encode_ok ( &hw_test );
        base64_decode_ok ( &hw_test );

        base64_encode_ok ( &random_test );
        base64_decode_ok ( &random_test );
}

Variable Documentation

Initial value:
 {
        .name = "base64",
        .exec = base64_test_exec,
}

Base64 self-test.

Definition at line 137 of file base64_test.c.