From f449f278dd3c70e479a035f50a9bb817a9b433ba Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 17:24:08 +0200 Subject: Adding upstream version 3.2.6. Signed-off-by: Daniel Baumann --- tests/contrib/test_base32hex.c | 267 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 267 insertions(+) create mode 100644 tests/contrib/test_base32hex.c (limited to 'tests/contrib/test_base32hex.c') diff --git a/tests/contrib/test_base32hex.c b/tests/contrib/test_base32hex.c new file mode 100644 index 0000000..541667c --- /dev/null +++ b/tests/contrib/test_base32hex.c @@ -0,0 +1,267 @@ +/* Copyright (C) 2020 CZ.NIC, z.s.p.o. + + 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 . + */ + +#include +#include +#include +#include + +#include "libknot/libknot.h" +#include "contrib/base32hex.h" +#include "contrib/openbsd/strlcpy.h" + +#define BUF_LEN 256 +#define MAX_BIN_DATA_LEN ((INT32_MAX / 8) * 5) + +int main(int argc, char *argv[]) +{ + plan(67); + + 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_base32hex_encode(NULL, 0, out, BUF_LEN); + is_int(KNOT_EINVAL, ret, "knot_base32hex_encode: NULL input buffer"); + ret = knot_base32hex_encode(in, BUF_LEN, NULL, 0); + is_int(KNOT_EINVAL, ret, "knot_base32hex_encode: NULL output buffer"); + ret = knot_base32hex_encode(in, MAX_BIN_DATA_LEN + 1, out, BUF_LEN); + is_int(KNOT_ERANGE, ret, "knot_base32hex_encode: input buffer too large"); + ret = knot_base32hex_encode(in, BUF_LEN, out, BUF_LEN); + is_int(KNOT_ERANGE, ret, "knot_base32hex_encode: output buffer too small"); + + ret = knot_base32hex_encode_alloc(NULL, 0, &out3); + is_int(KNOT_EINVAL, ret, "knot_base32hex_encode_alloc: NULL input buffer"); + ret = knot_base32hex_encode_alloc(in, MAX_BIN_DATA_LEN + 1, &out3); + is_int(KNOT_ERANGE, ret, "knot_base32hex_encode_alloc: input buffer too large"); + ret = knot_base32hex_encode_alloc(in, BUF_LEN, NULL); + is_int(KNOT_EINVAL, ret, "knot_base32hex_encode_alloc: NULL output buffer"); + + ret = knot_base32hex_decode(NULL, 0, out, BUF_LEN); + is_int(KNOT_EINVAL, ret, "knot_base32hex_decode: NULL input buffer"); + ret = knot_base32hex_decode(in, BUF_LEN, NULL, 0); + is_int(KNOT_EINVAL, ret, "knot_base32hex_decode: NULL output buffer"); + ret = knot_base32hex_decode(in, UINT32_MAX, out, BUF_LEN); + is_int(KNOT_ERANGE, ret, "knot_base32hex_decode: input buffer too large"); + ret = knot_base32hex_decode(in, BUF_LEN, out, 0); + is_int(KNOT_ERANGE, ret, "knot_base32hex_decode: output buffer too small"); + + ret = knot_base32hex_decode_alloc(NULL, 0, &out3); + is_int(KNOT_EINVAL, ret, "knot_base32hex_decode_alloc: NULL input buffer"); + ret = knot_base32hex_decode_alloc(in, UINT32_MAX, &out3); + is_int(KNOT_ERANGE, ret, "knot_base32hex_decode_aloc: input buffer too large"); + ret = knot_base32hex_decode_alloc(in, BUF_LEN, NULL); + is_int(KNOT_EINVAL, ret, "knot_base32hex_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_base32hex_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_base32hex_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, "co======", BUF_LEN); + ref_len = strlen((char *)ref); + ret = knot_base32hex_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_base32hex_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, "cpng====", BUF_LEN); + ref_len = strlen((char *)ref); + ret = knot_base32hex_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_base32hex_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, "cpnmu===", BUF_LEN); + ref_len = strlen((char *)ref); + ret = knot_base32hex_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_base32hex_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, "cpnmuog=", BUF_LEN); + ref_len = strlen((char *)ref); + ret = knot_base32hex_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_base32hex_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, "cpnmuoj1", BUF_LEN); + ref_len = strlen((char *)ref); + ret = knot_base32hex_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_base32hex_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, "cpnmuoj1e8======", BUF_LEN); + ref_len = strlen((char *)ref); + ret = knot_base32hex_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_base32hex_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_base32hex_decode((uint8_t *)"AAAAAA==", 8, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ECHAR, "Bad padding length 2"); + ret = knot_base32hex_decode((uint8_t *)"AAA=====", 8, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ECHAR, "Bad padding length 5"); + ret = knot_base32hex_decode((uint8_t *)"A=======", 8, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ECHAR, "Bad padding length 7"); + ret = knot_base32hex_decode((uint8_t *)"========", 8, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ECHAR, "Bad padding length 8"); + ret = knot_base32hex_decode((uint8_t *)"AAAAA=A=", 8, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ECHAR, "Bad padding character on position 2"); + ret = knot_base32hex_decode((uint8_t *)"AA=A====", 8, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ECHAR, "Bad padding character on position 5"); + ret = knot_base32hex_decode((uint8_t *)"=A======", 8, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ECHAR, "Bad padding character on position 7"); + ret = knot_base32hex_decode((uint8_t *)"CO======CO======", 16, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ECHAR, "Two octects with padding"); + + // Bad data length + ret = knot_base32hex_decode((uint8_t *)"A", 1, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ESIZE, "Bad data length 1"); + ret = knot_base32hex_decode((uint8_t *)"AA", 2, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ESIZE, "Bad data length 2"); + ret = knot_base32hex_decode((uint8_t *)"AAA", 3, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ESIZE, "Bad data length 3"); + ret = knot_base32hex_decode((uint8_t *)"AAAA", 4, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ESIZE, "Bad data length 4"); + ret = knot_base32hex_decode((uint8_t *)"AAAAA", 5, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ESIZE, "Bad data length 5"); + ret = knot_base32hex_decode((uint8_t *)"AAAAAA", 6, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ESIZE, "Bad data length 6"); + ret = knot_base32hex_decode((uint8_t *)"AAAAAAA", 7, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ESIZE, "Bad data length 7"); + ret = knot_base32hex_decode((uint8_t *)"AAAAAAAAA", 9, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ESIZE, "Bad data length 9"); + + // Bad data character + ret = knot_base32hex_decode((uint8_t *)"AAAAAAA$", 8, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ECHAR, "Bad data character dollar"); + ret = knot_base32hex_decode((uint8_t *)"AAAAAAA ", 8, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ECHAR, "Bad data character space"); + ret = knot_base32hex_decode((uint8_t *)"AAAAAA$A", 8, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ECHAR, "Bad data character dollar on position 7"); + ret = knot_base32hex_decode((uint8_t *)"AAAAA$AA", 8, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ECHAR, "Bad data character dollar on position 6"); + ret = knot_base32hex_decode((uint8_t *)"AAAA$AAA", 8, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ECHAR, "Bad data character dollar on position 5"); + ret = knot_base32hex_decode((uint8_t *)"AAA$AAAA", 8, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ECHAR, "Bad data character dollar on position 4"); + ret = knot_base32hex_decode((uint8_t *)"AA$AAAAA", 8, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ECHAR, "Bad data character dollar on position 3"); + ret = knot_base32hex_decode((uint8_t *)"A$AAAAAA", 8, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ECHAR, "Bad data character dollar on position 2"); + ret = knot_base32hex_decode((uint8_t *)"$AAAAAAA", 8, out, BUF_LEN); + ok(ret == KNOT_BASE32HEX_ECHAR, "Bad data character dollar on position 1"); + + return 0; +} -- cgit v1.2.3