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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
/* Copyright (C) CZ.NIC, z.s.p.o. and contributors
* SPDX-License-Identifier: GPL-2.0-or-later
* For more information, see <https://www.knot-dns.cz/>
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <tap/basic.h>
#include "libknot/libknot.h"
#include "contrib/base64.h"
#include "contrib/openbsd/strlcpy.h"
#define BUF_LEN 256
#define MAX_BIN_DATA_LEN ((INT32_MAX / 4) * 3)
int main(int argc, char *argv[])
{
plan(52);
int32_t ret;
uint8_t in[BUF_LEN], ref[BUF_LEN], out[BUF_LEN], out2[BUF_LEN], *out3;
uint32_t in_len, ref_len;
// 0. test invalid input
ret = knot_base64_encode(NULL, 0, out, BUF_LEN);
is_int(KNOT_EINVAL, ret, "knot_base64_encode: NULL input buffer");
ret = knot_base64_encode(in, BUF_LEN, NULL, 0);
is_int(KNOT_EINVAL, ret, "knot_base64_encode: NULL output buffer");
ret = knot_base64_encode(in, MAX_BIN_DATA_LEN + 1, out, BUF_LEN);
is_int(KNOT_ERANGE, ret, "knot_base64_encode: input buffer too large");
ret = knot_base64_encode(in, BUF_LEN, out, BUF_LEN);
is_int(KNOT_ESPACE, ret, "knot_base64_encode: output buffer too small");
ret = knot_base64_encode_alloc(NULL, 0, &out3);
is_int(KNOT_EINVAL, ret, "knot_base64_encode_alloc: NULL input buffer");
ret = knot_base64_encode_alloc(in, MAX_BIN_DATA_LEN + 1, &out3);
is_int(KNOT_ERANGE, ret, "knot_base64_encode_alloc: input buffer too large");
ret = knot_base64_encode_alloc(in, BUF_LEN, NULL);
is_int(KNOT_EINVAL, ret, "knot_base64_encode_alloc: NULL output buffer");
ret = knot_base64_decode(NULL, 0, out, BUF_LEN);
is_int(KNOT_EINVAL, ret, "knot_base64_decode: NULL input buffer");
ret = knot_base64_decode(in, BUF_LEN, NULL, 0);
is_int(KNOT_EINVAL, ret, "knot_base64_decode: NULL output buffer");
ret = knot_base64_decode(in, UINT32_MAX, out, BUF_LEN);
is_int(KNOT_ERANGE, ret, "knot_base64_decode: input buffer too large");
ret = knot_base64_decode(in, BUF_LEN, out, 0);
is_int(KNOT_ESPACE, ret, "knot_base64_decode: output buffer too small");
ret = knot_base64_decode_alloc(NULL, 0, &out3);
is_int(KNOT_EINVAL, ret, "knot_base64_decode_alloc: NULL input buffer");
ret = knot_base64_decode_alloc(in, UINT32_MAX, &out3);
is_int(KNOT_ERANGE, ret, "knot_base64_decode_aloc: input buffer too large");
ret = knot_base64_decode_alloc(in, BUF_LEN, NULL);
is_int(KNOT_EINVAL, ret, "knot_base64_decode_alloc: NULL output buffer");
// 1. test vector -> ENC -> DEC
strlcpy((char *)in, "", BUF_LEN);
in_len = strlen((char *)in);
strlcpy((char *)ref, "", BUF_LEN);
ref_len = strlen((char *)ref);
ret = knot_base64_encode(in, in_len, out, BUF_LEN);
ok(ret == ref_len, "1. test vector - ENC output length");
if (ret < 0) {
skip("Encode err");
} else {
ok(memcmp(out, ref, ret) == 0, "1. test vector - ENC output content");
}
ret = knot_base64_decode(out, ret, out2, BUF_LEN);
ok(ret == in_len, "1. test vector - DEC output length");
if (ret < 0) {
skip("Decode err");
} else {
ok(memcmp(out2, in, ret) == 0, "1. test vector - DEC output content");
}
// 2. test vector -> ENC -> DEC
strlcpy((char *)in, "f", BUF_LEN);
in_len = strlen((char *)in);
strlcpy((char *)ref, "Zg==", BUF_LEN);
ref_len = strlen((char *)ref);
ret = knot_base64_encode(in, in_len, out, BUF_LEN);
ok(ret == ref_len, "2. test vector - ENC output length");
if (ret < 0) {
skip("Encode err");
} else {
ok(memcmp(out, ref, ret) == 0, "2. test vector - ENC output content");
}
ret = knot_base64_decode(out, ret, out2, BUF_LEN);
ok(ret == in_len, "2. test vector - DEC output length");
if (ret < 0) {
skip("Decode err");
} else {
ok(memcmp(out2, in, ret) == 0, "2. test vector - DEC output content");
}
// 3. test vector -> ENC -> DEC
strlcpy((char *)in, "fo", BUF_LEN);
in_len = strlen((char *)in);
strlcpy((char *)ref, "Zm8=", BUF_LEN);
ref_len = strlen((char *)ref);
ret = knot_base64_encode(in, in_len, out, BUF_LEN);
ok(ret == ref_len, "3. test vector - ENC output length");
if (ret < 0) {
skip("Encode err");
} else {
ok(memcmp(out, ref, ret) == 0, "3. test vector - ENC output content");
}
ret = knot_base64_decode(out, ret, out2, BUF_LEN);
ok(ret == in_len, "3. test vector - DEC output length");
if (ret < 0) {
skip("Decode err");
} else {
ok(memcmp(out2, in, ret) == 0, "3. test vector - DEC output content");
}
// 4. test vector -> ENC -> DEC
strlcpy((char *)in, "foo", BUF_LEN);
in_len = strlen((char *)in);
strlcpy((char *)ref, "Zm9v", BUF_LEN);
ref_len = strlen((char *)ref);
ret = knot_base64_encode(in, in_len, out, BUF_LEN);
ok(ret == ref_len, "4. test vector - ENC output length");
if (ret < 0) {
skip("Encode err");
} else {
ok(memcmp(out, ref, ret) == 0, "4. test vector - ENC output content");
}
ret = knot_base64_decode(out, ret, out2, BUF_LEN);
ok(ret == in_len, "4. test vector - DEC output length");
if (ret < 0) {
skip("Decode err");
} else {
ok(memcmp(out2, in, ret) == 0, "4. test vector - DEC output content");
}
// 5. test vector -> ENC -> DEC
strlcpy((char *)in, "foob", BUF_LEN);
in_len = strlen((char *)in);
strlcpy((char *)ref, "Zm9vYg==", BUF_LEN);
ref_len = strlen((char *)ref);
ret = knot_base64_encode(in, in_len, out, BUF_LEN);
ok(ret == ref_len, "5. test vector - ENC output length");
if (ret < 0) {
skip("Encode err");
} else {
ok(memcmp(out, ref, ret) == 0, "5. test vector - ENC output content");
}
ret = knot_base64_decode(out, ret, out2, BUF_LEN);
ok(ret == in_len, "5. test vector - DEC output length");
if (ret < 0) {
skip("Decode err");
} else {
ok(memcmp(out2, in, ret) == 0, "5. test vector - DEC output content");
}
// 6. test vector -> ENC -> DEC
strlcpy((char *)in, "fooba", BUF_LEN);
in_len = strlen((char *)in);
strlcpy((char *)ref, "Zm9vYmE=", BUF_LEN);
ref_len = strlen((char *)ref);
ret = knot_base64_encode(in, in_len, out, BUF_LEN);
ok(ret == ref_len, "6. test vector - ENC output length");
if (ret < 0) {
skip("Encode err");
} else {
ok(memcmp(out, ref, ret) == 0, "6. test vector - ENC output content");
}
ret = knot_base64_decode(out, ret, out2, BUF_LEN);
ok(ret == in_len, "6. test vector - DEC output length");
if (ret < 0) {
skip("Decode err");
} else {
ok(memcmp(out2, in, ret) == 0, "6. test vector - DEC output content");
}
// 7. test vector -> ENC -> DEC
strlcpy((char *)in, "foobar", BUF_LEN);
in_len = strlen((char *)in);
strlcpy((char *)ref, "Zm9vYmFy", BUF_LEN);
ref_len = strlen((char *)ref);
ret = knot_base64_encode(in, in_len, out, BUF_LEN);
ok(ret == ref_len, "7. test vector - ENC output length");
if (ret < 0) {
skip("Encode err");
} else {
ok(memcmp(out, ref, ret) == 0, "7. test vector - ENC output content");
}
ret = knot_base64_decode(out, ret, out2, BUF_LEN);
ok(ret == in_len, "7. test vector - DEC output length");
if (ret < 0) {
skip("Decode err");
} else {
ok(memcmp(out2, in, ret) == 0, "7. test vector - DEC output content");
}
// Bad paddings
ret = knot_base64_decode((uint8_t *)"A===", 4, out, BUF_LEN);
ok(ret == KNOT_BASE64_ECHAR, "Bad padding length 3");
ret = knot_base64_decode((uint8_t *)"====", 4, out, BUF_LEN);
ok(ret == KNOT_BASE64_ECHAR, "Bad padding length 4");
ret = knot_base64_decode((uint8_t *)"AA=A", 4, out, BUF_LEN);
ok(ret == KNOT_BASE64_ECHAR, "Bad padding character on position 2");
ret = knot_base64_decode((uint8_t *)"Zg==Zg==", 8, out, BUF_LEN);
ok(ret == KNOT_BASE64_ECHAR, "Two quartets with padding");
// Bad data length
ret = knot_base64_decode((uint8_t *)"A", 1, out, BUF_LEN);
ok(ret == KNOT_BASE64_ESIZE, "Bad data length 1");
ret = knot_base64_decode((uint8_t *)"AA", 2, out, BUF_LEN);
ok(ret == KNOT_BASE64_ESIZE, "Bad data length 2");
ret = knot_base64_decode((uint8_t *)"AAA", 3, out, BUF_LEN);
ok(ret == KNOT_BASE64_ESIZE, "Bad data length 3");
ret = knot_base64_decode((uint8_t *)"AAAAA", 5, out, BUF_LEN);
ok(ret == KNOT_BASE64_ESIZE, "Bad data length 5");
// Bad data character
ret = knot_base64_decode((uint8_t *)"AAA$", 4, out, BUF_LEN);
ok(ret == KNOT_BASE64_ECHAR, "Bad data character dollar");
ret = knot_base64_decode((uint8_t *)"AAA ", 4, out, BUF_LEN);
ok(ret == KNOT_BASE64_ECHAR, "Bad data character space");
return 0;
}
|