summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/tests/internal/base64.c
blob: 4375870fc2eda79000e4619288e2a38ca91df8a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <fluent-bit/flb_sds.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_base64.h>
#include <fluent-bit/flb_time.h>

#include "flb_tests_internal.h"

static void b64_basic_test_encode()
{
    char* data = "Hello world";
    char out[100];
    char* expect = "SGVsbG8gd29ybGQ=";
    size_t olen;
    out[16] = 'X';

    flb_base64_encode((unsigned char *) out, 100, &olen, (unsigned char *)data, 11);

    TEST_CHECK(strlen(out) == 16 && olen == 16);
    TEST_MSG("Base64 encode failed to output result of expected length");

    TEST_CHECK(strcmp(out, expect) == 0);
    TEST_MSG("Base64 encode failed to output result of expected value");

    TEST_CHECK(out[16] == 0);
    TEST_MSG("Base64 not null terminated");
    return;
}

static void b64_basic_test_decode()
{
    char* data = "SGVsbG8gd29ybGQ=";
    char out[100] = { 0 };
    char* expect = "Hello world";
    size_t olen;

    flb_base64_decode((unsigned char *) out, 100, &olen, (unsigned char *)data, 16);

    TEST_CHECK(strlen(out) == 11 && olen == 11);
    TEST_MSG("Base64 decode failed to output result of expected length");

    TEST_CHECK(strcmp(out, expect) == 0);
    TEST_MSG("Base64 decode failed to output result of expected value");
    return;
}

TEST_LIST = {
    { "b64_basic_test_encode" , b64_basic_test_encode },
    { "b64_basic_test_decode", b64_basic_test_decode },
    { 0 }
};