test_base32hex.c 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*  Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <stdint.h>
#include <string.h>
20
#include <tap/basic.h>
21

22
#include "libknot/libknot.h"
23
#include "contrib/base32hex.h"
24
#include "contrib/openbsd/strlcpy.h"
25

26 27
#define BUF_LEN			256
#define MAX_BIN_DATA_LEN	((INT32_MAX / 8) * 5)
28 29 30

int main(int argc, char *argv[])
{
31
	plan(67);
32 33

	int32_t  ret;
34
	uint8_t  in[BUF_LEN], ref[BUF_LEN], out[BUF_LEN], out2[BUF_LEN], *out3;
35 36
	uint32_t in_len, ref_len;

37 38 39 40 41
	// 0. test invalid input
	ret = base32hex_encode(NULL, 0, out, BUF_LEN);
	ok(ret == KNOT_EINVAL, "base32hex_encode: NULL input buffer");
	ret = base32hex_encode(in, BUF_LEN, NULL, 0);
	ok(ret == KNOT_EINVAL, "base32hex_encode: NULL output buffer");
42
	ret = base32hex_encode(in, MAX_BIN_DATA_LEN + 1, out, BUF_LEN);
43
	ok(ret == KNOT_ERANGE, "base32hex_encode: input buffer too large");
44
	ret = base32hex_encode(in, BUF_LEN, out, BUF_LEN);
45
	ok(ret == KNOT_ERANGE, "base32hex_encode: output buffer too small");
46

47 48
	ret = base32hex_encode_alloc(NULL, 0, &out3);
	ok(ret == KNOT_EINVAL, "base32hex_encode_alloc: NULL input buffer");
49 50 51 52
	ret = base32hex_encode_alloc(in, MAX_BIN_DATA_LEN + 1, &out3);
	ok(ret == KNOT_ERANGE, "base32hex_encode_alloc: input buffer too large");
	ret = base32hex_encode_alloc(in, BUF_LEN, NULL);
	ok(ret == KNOT_EINVAL, "base32hex_encode_alloc: NULL output buffer");
53 54 55 56 57

	ret = base32hex_decode(NULL, 0, out, BUF_LEN);
	ok(ret == KNOT_EINVAL, "base32hex_decode: NULL input buffer");
	ret = base32hex_decode(in, BUF_LEN, NULL, 0);
	ok(ret == KNOT_EINVAL, "base32hex_decode: NULL output buffer");
58
	ret = base32hex_decode(in, UINT32_MAX, out, BUF_LEN);
59
	ok(ret == KNOT_ERANGE, "base32hex_decode: input buffer too large");
60
	ret = base32hex_decode(in, BUF_LEN, out, 0);
61
	ok(ret == KNOT_ERANGE, "base32hex_decode: output buffer too small");
62 63 64

	ret = base32hex_decode_alloc(NULL, 0, &out3);
	ok(ret == KNOT_EINVAL, "base32hex_decode_alloc: NULL input buffer");
65
	ret = base32hex_decode_alloc(in, UINT32_MAX, &out3);
66 67 68
	ok(ret == KNOT_ERANGE, "base32hex_decode_aloc: input buffer too large");
	ret = base32hex_decode_alloc(in, BUF_LEN, NULL);
	ok(ret == KNOT_EINVAL, "base32hex_decode_alloc: NULL output buffer");
69

70
	// 1. test vector -> ENC -> DEC
71
	strlcpy((char *)in, "", BUF_LEN);
72
	in_len = strlen((char *)in);
73
	strlcpy((char *)ref, "", BUF_LEN);
74 75 76 77
	ref_len = strlen((char *)ref);
	ret = base32hex_encode(in, in_len, out, BUF_LEN);
	ok(ret == ref_len, "1. test vector - ENC output length");
	if (ret < 0) {
78
		skip("Encode err");
79 80 81 82 83 84
	} else {
		ok(memcmp(out, ref, ret) == 0, "1. test vector - ENC output content");
	}
	ret = base32hex_decode(out, ret, out2, BUF_LEN);
	ok(ret == in_len, "1. test vector - DEC output length");
	if (ret < 0) {
85
		skip("Decode err");
86 87 88 89 90
	} else {
		ok(memcmp(out2, in, ret) == 0, "1. test vector - DEC output content");
	}

	// 2. test vector -> ENC -> DEC
91
	strlcpy((char *)in, "f", BUF_LEN);
92
	in_len = strlen((char *)in);
93
	strlcpy((char *)ref, "CO======", BUF_LEN);
94 95 96 97
	ref_len = strlen((char *)ref);
	ret = base32hex_encode(in, in_len, out, BUF_LEN);
	ok(ret == ref_len, "2. test vector - ENC output length");
	if (ret < 0) {
98
		skip("Encode err");
99 100 101 102 103 104
	} else {
		ok(memcmp(out, ref, ret) == 0, "2. test vector - ENC output content");
	}
	ret = base32hex_decode(out, ret, out2, BUF_LEN);
	ok(ret == in_len, "2. test vector - DEC output length");
	if (ret < 0) {
105
		skip("Decode err");
106 107 108 109 110
	} else {
		ok(memcmp(out2, in, ret) == 0, "2. test vector - DEC output content");
	}

	// 3. test vector -> ENC -> DEC
111
	strlcpy((char *)in, "fo", BUF_LEN);
112
	in_len = strlen((char *)in);
113
	strlcpy((char *)ref, "CPNG====", BUF_LEN);
114 115 116 117
	ref_len = strlen((char *)ref);
	ret = base32hex_encode(in, in_len, out, BUF_LEN);
	ok(ret == ref_len, "3. test vector - ENC output length");
	if (ret < 0) {
118
		skip("Encode err");
119 120 121 122 123 124
	} else {
		ok(memcmp(out, ref, ret) == 0, "3. test vector - ENC output content");
	}
	ret = base32hex_decode(out, ret, out2, BUF_LEN);
	ok(ret == in_len, "3. test vector - DEC output length");
	if (ret < 0) {
125
		skip("Decode err");
126 127 128 129 130
	} else {
		ok(memcmp(out2, in, ret) == 0, "3. test vector - DEC output content");
	}

	// 4. test vector -> ENC -> DEC
131
	strlcpy((char *)in, "foo", BUF_LEN);
132
	in_len = strlen((char *)in);
133
	strlcpy((char *)ref, "CPNMU===", BUF_LEN);
134 135 136 137
	ref_len = strlen((char *)ref);
	ret = base32hex_encode(in, in_len, out, BUF_LEN);
	ok(ret == ref_len, "4. test vector - ENC output length");
	if (ret < 0) {
138
		skip("Encode err");
139 140 141 142 143 144
	} else {
		ok(memcmp(out, ref, ret) == 0, "4. test vector - ENC output content");
	}
	ret = base32hex_decode(out, ret, out2, BUF_LEN);
	ok(ret == in_len, "4. test vector - DEC output length");
	if (ret < 0) {
145
		skip("Decode err");
146 147 148 149 150
	} else {
		ok(memcmp(out2, in, ret) == 0, "4. test vector - DEC output content");
	}

	// 5. test vector -> ENC -> DEC
151
	strlcpy((char *)in, "foob", BUF_LEN);
152
	in_len = strlen((char *)in);
153
	strlcpy((char *)ref, "CPNMUOG=", BUF_LEN);
154 155 156 157
	ref_len = strlen((char *)ref);
	ret = base32hex_encode(in, in_len, out, BUF_LEN);
	ok(ret == ref_len, "5. test vector - ENC output length");
	if (ret < 0) {
158
		skip("Encode err");
159 160 161 162 163 164
	} else {
		ok(memcmp(out, ref, ret) == 0, "5. test vector - ENC output content");
	}
	ret = base32hex_decode(out, ret, out2, BUF_LEN);
	ok(ret == in_len, "5. test vector - DEC output length");
	if (ret < 0) {
165
		skip("Decode err");
166 167 168 169 170
	} else {
		ok(memcmp(out2, in, ret) == 0, "5. test vector - DEC output content");
	}

	// 6. test vector -> ENC -> DEC
171
	strlcpy((char *)in, "fooba", BUF_LEN);
172
	in_len = strlen((char *)in);
173
	strlcpy((char *)ref, "CPNMUOJ1", BUF_LEN);
174 175 176 177
	ref_len = strlen((char *)ref);
	ret = base32hex_encode(in, in_len, out, BUF_LEN);
	ok(ret == ref_len, "6. test vector - ENC output length");
	if (ret < 0) {
178
		skip("Encode err");
179 180 181 182 183 184
	} else {
		ok(memcmp(out, ref, ret) == 0, "6. test vector - ENC output content");
	}
	ret = base32hex_decode(out, ret, out2, BUF_LEN);
	ok(ret == in_len, "6. test vector - DEC output length");
	if (ret < 0) {
185
		skip("Decode err");
186 187 188 189 190
	} else {
		ok(memcmp(out2, in, ret) == 0, "6. test vector - DEC output content");
	}

	// 7. test vector -> ENC -> DEC
191
	strlcpy((char *)in, "foobar", BUF_LEN);
192
	in_len = strlen((char *)in);
193
	strlcpy((char *)ref, "CPNMUOJ1E8======", BUF_LEN);
194 195 196 197
	ref_len = strlen((char *)ref);
	ret = base32hex_encode(in, in_len, out, BUF_LEN);
	ok(ret == ref_len, "7. test vector - ENC output length");
	if (ret < 0) {
198
		skip("Encode err");
199 200 201 202 203 204
	} else {
		ok(memcmp(out, ref, ret) == 0, "7. test vector - ENC output content");
	}
	ret = base32hex_decode(out, ret, out2, BUF_LEN);
	ok(ret == in_len, "7. test vector - DEC output length");
	if (ret < 0) {
205
		skip("Decode err");
206 207 208 209 210
	} else {
		ok(memcmp(out2, in, ret) == 0, "7. test vector - DEC output content");
	}

	// Bad paddings
211 212 213 214
	ret = base32hex_decode((uint8_t *)"AAAAAA==", 8, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ECHAR, "Bad padding length 2");
	ret = base32hex_decode((uint8_t *)"AAA=====", 8, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ECHAR, "Bad padding length 5");
215
	ret = base32hex_decode((uint8_t *)"A=======", 8, out, BUF_LEN);
216
	ok(ret == KNOT_BASE32HEX_ECHAR, "Bad padding length 7");
217
	ret = base32hex_decode((uint8_t *)"========", 8, out, BUF_LEN);
218
	ok(ret == KNOT_BASE32HEX_ECHAR, "Bad padding length 8");
219 220 221 222 223 224 225 226
	ret = base32hex_decode((uint8_t *)"AAAAA=A=", 8, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ECHAR, "Bad padding character on position 2");
	ret = base32hex_decode((uint8_t *)"AA=A====", 8, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ECHAR, "Bad padding character on position 5");
	ret = base32hex_decode((uint8_t *)"=A======", 8, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ECHAR, "Bad padding character on position 7");
	ret = base32hex_decode((uint8_t *)"CO======CO======", 16, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ECHAR, "Two octects with padding");
227 228

	// Bad data length
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
	ret = base32hex_decode((uint8_t *)"A", 1, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ESIZE, "Bad data length 1");
	ret = base32hex_decode((uint8_t *)"AA", 2, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ESIZE, "Bad data length 2");
	ret = base32hex_decode((uint8_t *)"AAA", 3, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ESIZE, "Bad data length 3");
	ret = base32hex_decode((uint8_t *)"AAAA", 4, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ESIZE, "Bad data length 4");
	ret = base32hex_decode((uint8_t *)"AAAAA", 5, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ESIZE, "Bad data length 5");
	ret = base32hex_decode((uint8_t *)"AAAAAA", 6, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ESIZE, "Bad data length 6");
	ret = base32hex_decode((uint8_t *)"AAAAAAA", 7, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ESIZE, "Bad data length 7");
	ret = base32hex_decode((uint8_t *)"AAAAAAAAA", 9, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ESIZE, "Bad data length 9");
245 246

	// Bad data character
247 248 249 250
	ret = base32hex_decode((uint8_t *)"AAAAAAA$", 8, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ECHAR, "Bad data character dollar");
	ret = base32hex_decode((uint8_t *)"AAAAAAA ", 8, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ECHAR, "Bad data character space");
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	ret = base32hex_decode((uint8_t *)"AAAAAA$A", 8, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ECHAR, "Bad data character dollar on position 7");
	ret = base32hex_decode((uint8_t *)"AAAAA$AA", 8, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ECHAR, "Bad data character dollar on position 6");
	ret = base32hex_decode((uint8_t *)"AAAA$AAA", 8, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ECHAR, "Bad data character dollar on position 5");
	ret = base32hex_decode((uint8_t *)"AAA$AAAA", 8, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ECHAR, "Bad data character dollar on position 4");
	ret = base32hex_decode((uint8_t *)"AA$AAAAA", 8, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ECHAR, "Bad data character dollar on position 3");
	ret = base32hex_decode((uint8_t *)"A$AAAAAA", 8, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ECHAR, "Bad data character dollar on position 2");
	ret = base32hex_decode((uint8_t *)"$AAAAAAA", 8, out, BUF_LEN);
	ok(ret == KNOT_BASE32HEX_ECHAR, "Bad data character dollar on position 1");

266 267
	return 0;
}