test-base32.c revision 191153d1a5b0eb0c129139570e3aa5212f28d2ac
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2007-2017 Dovecot authors, see the included COPYING file */
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Boschstatic void test_base32_encode(void)
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 static const char *output[] = {
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "ORXWKZDFNRSWI33LNFSSCII=",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "MJ4WKIDCPFSSA53POJWGI===",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "NBXWK5TFMVWCA33OPJUW4IDLOVXCA2TFEB2GK43UMVXD6PZ7H47Q====",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "MMTWK43UEBYGC4ZAOZZGC2JBEA======",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "MRUXIIDJOMQGQZLUEBSWS3TEMUQHMYLOEBSGK6TFEB2GK43U"
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch unsigned int i;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch base32_encode(TRUE, input[i], strlen(input[i]), str);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_assert(strcmp(output[i], str_c(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 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 static const char *output[] = {
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "EHNMAP35DHIM8RRBD5II288=",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "C9SMA832F5II0TRFE9M68===",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "D1NMATJ5CLM20RREF9KMS83BELN20QJ541Q6ASRKCLN3UFPV7SVG====",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "CCJMASRK41O62SP0EPP62Q9140======",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "CHKN8839ECG6GPBK41IMIRJ4CKG7COBE41I6AUJ541Q6ASRK"
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch unsigned int i;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_begin("base32hex_encode() with padding");
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch base32hex_encode(TRUE, input[i], strlen(input[i]), str);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_assert(strcmp(output[i], str_c(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 unsigned int src_pos;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Boschstatic void test_base32_decode(void)
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch static const char *input[] = {
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "ORXWKZDFNRSWI33LNFSSCII=",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "MJ4WKIDCPFSSA53POJWGI===",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "NBXWK5TFMVWCA33OPJUW4IDLOVXCA2TFEB2GK43UMVXD6PZ7H47Q====",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "MMTWK43UEBYGC4ZAOZZGC2JBEA======",
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch "MRUXIIDJOMQGQZLUEBSWS3TEMUQHMYLOEBSGK6TFEB2GK43U"
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch static const struct test_base32_decode_output output[] = {
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch { "hoeveel onzin kun je testen?????", 0, 56 },
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch unsigned int i;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch ret = base32_decode(input[i], strlen(input[i]), &src_pos, str);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Boschstatic void test_base32_random(void)
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch unsigned int i, j, max;
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_begin("padded base32 encode/decode with random input");
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch for (i = 0; i < 1000; i++) {
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch for (j = 0; j < max; j++)
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_assert(base32_decode(str_data(str), str_len(str), NULL, dest) >= 0);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch test_begin("padded base32hex encode/decode with random input");
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch for (i = 0; i < 1000; i++) {
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch for (j = 0; j < max; j++)