test-base32.c revision 191153d1a5b0eb0c129139570e3aa5212f28d2ac
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2007-2017 Dovecot authors, see the included COPYING file */
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch#include "test-lib.h"
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch#include "str.h"
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch#include "base32.h"
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Boschstatic void test_base32_encode(void)
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch{
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch static const char *input[] = {
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "toedeledokie!!",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "bye bye world",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "hoeveel onzin kun je testen?????",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "c'est pas vrai! ",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "dit is het einde van deze test"
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch };
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch static const char *output[] = {
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "ORXWKZDFNRSWI33LNFSSCII=",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "MJ4WKIDCPFSSA53POJWGI===",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "NBXWK5TFMVWCA33OPJUW4IDLOVXCA2TFEB2GK43UMVXD6PZ7H47Q====",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "MMTWK43UEBYGC4ZAOZZGC2JBEA======",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "MRUXIIDJOMQGQZLUEBSWS3TEMUQHMYLOEBSGK6TFEB2GK43U"
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch };
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch string_t *str;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch unsigned int i;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_begin("base32_encode() with padding");
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch str = t_str_new(256);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch for (i = 0; i < N_ELEMENTS(input); i++) {
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch str_truncate(str, 0);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch base32_encode(TRUE, input[i], strlen(input[i]), str);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_assert(strcmp(output[i], str_c(str)) == 0);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch }
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_end();
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_begin("base32_encode() no padding");
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch str = t_str_new(256);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch for (i = 0; i < N_ELEMENTS(input); i++) {
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch const char *p = strchr(output[i], '=');
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch size_t len;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch if (p == NULL)
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch len = strlen(output[i]);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch else
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch len = (size_t)(p - output[i]);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch str_truncate(str, 0);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch base32_encode(FALSE, input[i], strlen(input[i]), str);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_assert(strncmp(output[i], str_c(str), len) == 0);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch }
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_end();
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch}
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Boschstatic void test_base32hex_encode(void)
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch{
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch static const char *input[] = {
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "toedeledokie!!",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "bye bye world",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "hoeveel onzin kun je testen?????",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "c'est pas vrai! ",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "dit is het einde van deze test"
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch };
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch static const char *output[] = {
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "EHNMAP35DHIM8RRBD5II288=",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "C9SMA832F5II0TRFE9M68===",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "D1NMATJ5CLM20RREF9KMS83BELN20QJ541Q6ASRKCLN3UFPV7SVG====",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "CCJMASRK41O62SP0EPP62Q9140======",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "CHKN8839ECG6GPBK41IMIRJ4CKG7COBE41I6AUJ541Q6ASRK"
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch };
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch string_t *str;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch unsigned int i;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_begin("base32hex_encode() with padding");
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch str = t_str_new(256);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch for (i = 0; i < N_ELEMENTS(input); i++) {
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch str_truncate(str, 0);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch base32hex_encode(TRUE, input[i], strlen(input[i]), str);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_assert(strcmp(output[i], str_c(str)) == 0);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch }
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_end();
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_begin("base32hex_encode() no padding");
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch str = t_str_new(256);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch for (i = 0; i < N_ELEMENTS(input); i++) {
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch const char *p = strchr(output[i], '=');
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch size_t len;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch if (p == NULL)
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch len = strlen(output[i]);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch else
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch len = (size_t)(p - output[i]);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch str_truncate(str, 0);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch base32hex_encode(FALSE, input[i], strlen(input[i]), str);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_assert(strncmp(output[i], str_c(str), len) == 0);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch }
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_end();
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch}
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Boschstruct test_base32_decode_output {
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch const char *text;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch int ret;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch unsigned int src_pos;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch};
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Boschstatic void test_base32_decode(void)
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch{
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch static const char *input[] = {
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "ORXWKZDFNRSWI33LNFSSCII=",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "MJ4WKIDCPFSSA53POJWGI===",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "NBXWK5TFMVWCA33OPJUW4IDLOVXCA2TFEB2GK43UMVXD6PZ7H47Q====",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "MMTWK43UEBYGC4ZAOZZGC2JBEA======",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "MRUXIIDJOMQGQZLUEBSWS3TEMUQHMYLOEBSGK6TFEB2GK43U"
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch };
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch static const struct test_base32_decode_output output[] = {
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch { "toedeledokie!!", 0, 24 },
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch { "bye bye world", 0, 24 },
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch { "hoeveel onzin kun je testen?????", 0, 56 },
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch { "c'est pas vrai! ", 0, 32 },
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch { "dit is het einde van deze test", 1, 48 },
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch };
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch string_t *str;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch unsigned int i;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch size_t src_pos;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch int ret;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_begin("base32_decode()");
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch str = t_str_new(256);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch for (i = 0; i < N_ELEMENTS(input); i++) {
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch str_truncate(str, 0);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch src_pos = 0;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch ret = base32_decode(input[i], strlen(input[i]), &src_pos, str);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_assert(output[i].ret == ret &&
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch strcmp(output[i].text, str_c(str)) == 0 &&
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch (src_pos == output[i].src_pos ||
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch (output[i].src_pos == UINT_MAX &&
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch src_pos == strlen(input[i]))));
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch }
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_end();
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch}
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Boschstatic void test_base32_random(void)
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch{
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch string_t *str, *dest;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch char buf[10];
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch unsigned int i, j, max;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch str = t_str_new(256);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch dest = t_str_new(256);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_begin("padded base32 encode/decode with random input");
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch for (i = 0; i < 1000; i++) {
191153d1a5b0eb0c129139570e3aa5212f28d2acJosef 'Jeff' Sipek max = i_rand_limit(sizeof(buf));
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch for (j = 0; j < max; j++)
62461eb609e1d852e027cf4e07d30d51288678a2Aki Tuomi buf[j] = i_rand();
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch str_truncate(str, 0);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch str_truncate(dest, 0);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch base32_encode(TRUE, buf, max, str);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_assert(base32_decode(str_data(str), str_len(str), NULL, dest) >= 0);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_assert(str_len(dest) == max &&
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch memcmp(buf, str_data(dest), max) == 0);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch }
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_end();
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_begin("padded base32hex encode/decode with random input");
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch for (i = 0; i < 1000; i++) {
191153d1a5b0eb0c129139570e3aa5212f28d2acJosef 'Jeff' Sipek max = i_rand_limit(sizeof(buf));
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch for (j = 0; j < max; j++)
62461eb609e1d852e027cf4e07d30d51288678a2Aki Tuomi buf[j] = i_rand();
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch str_truncate(str, 0);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch str_truncate(dest, 0);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch base32hex_encode(TRUE, buf, max, str);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_assert(base32hex_decode(str_data(str), str_len(str), NULL, dest) >= 0);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_assert(str_len(dest) == max &&
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch memcmp(buf, str_data(dest), max) == 0);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch }
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_end();
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch}
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Boschvoid test_base32(void)
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch{
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_base32_encode();
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_base32hex_encode();
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_base32_decode();
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_base32_random();
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch}