bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2017-2018 Dovecot authors, see the included COPYING file */
b5ab29780f74cf88212a547ebbe3b6bc0cb867c5Stephan Bosch
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi#include "lib.h"
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi#include "test-common.h"
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi#include "str.h"
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi#include "var-expand.h"
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi#include "randgen.h"
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi#include "dcrypt.h"
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomistruct module;
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomiextern void var_expand_crypt_init(struct module *module);
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomiextern void var_expand_crypt_deinit(void);
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomistatic void test_var_expand_crypt(void)
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi{
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi struct var_expand_table table[] = {
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi { '\0', "98b3b40a48ca40f998b3b40a48ca40f9", "iv" },
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi { '\0', "cc2981c8f38aea59cc2981c8f38aea59", "key" },
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi { '\0', "46b58741763fe22598014be26331a082", "encrypted_noiv" },
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi { '\0', "98b3b40a48ca40f998b3b40a48ca40f9$46b58741763fe22598014be26331a082$", "encrypted" },
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi { '\0', "hello, world", "decrypted" },
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi { '\0', NULL, "encrypted2" },
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi { '\0', NULL, NULL }
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi };
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi static struct {
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi const char *input;
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi const char *output;
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi int expect_ret;
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi } test_cases[] = {
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi { "%{encrypt;algo=null:decrypted}", "", -1 },
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi { "%{encrypt;algo=aes-128-cbc,iv=98b3b40a48ca40f998b3b40a48ca40f9,key=cc2981c8f38aea59cc2981c8f38aea59:decrypted}", "98b3b40a48ca40f998b3b40a48ca40f9$46b58741763fe22598014be26331a082$", 1 },
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi { "%{encrypt;noiv=yes,algo=aes-128-cbc,iv=98b3b40a48ca40f998b3b40a48ca40f9,key=cc2981c8f38aea59cc2981c8f38aea59:decrypted}", "46b58741763fe22598014be26331a082", 1 },
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi { "%{encrypt;algo=aes-128-cbc,iv=%{iv},key=%{key}:decrypted}", "98b3b40a48ca40f998b3b40a48ca40f9$46b58741763fe22598014be26331a082$", 1 },
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi { "%{decrypt;algo=null:encrypted}", "", -1 },
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi { "%{decrypt;algo=aes-128-cbc,key=%{key}:encrypted}", "hello, world", 1 },
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi { "%{decrypt;algo=aes-128-cbc,iv=%{iv},key=%{key}:encrypted_noiv}", "hello, world", 1 },
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi { "%{decrypt;algo=aes-128-cbc,iv=98b3b40a48ca40f998b3b40a48ca40f9,key=cc2981c8f38aea59cc2981c8f38aea59:encrypted_noiv}", "hello, world", 1 },
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi };
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi unsigned int i;
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi test_begin("var_expand_crypt");
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi var_expand_crypt_init(NULL);
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi for(i=0; i < N_ELEMENTS(test_cases); i++) T_BEGIN {
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi const char *error;
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi string_t *dest = t_str_new(32);
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi int ret = var_expand(dest, test_cases[i].input, table, &error);
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi if (ret < 0) {
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi if (test_cases[i].expect_ret == -1)
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi i_info("Expected: var_expand(%s): %s", test_cases[i].input, error);
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi else
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi i_error("var_expand(%s): %s", test_cases[i].input, error);
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi }
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi test_assert_idx(strcmp(str_c(dest), test_cases[i].output)==0, i);
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi test_assert_idx(ret == test_cases[i].expect_ret, i);
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi } T_END;
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi test_end();
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi test_begin("var_expand_crypt_random");
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi string_t *input = t_str_new(32);
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi string_t *output = t_str_new(32);
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi for(i=0;i<1000;i++) {
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi const char *error;
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi str_truncate(input, 0);
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi str_truncate(output, 0);
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi test_assert_idx(var_expand(input, "%{encrypt;algo=aes-128-cbc,key=%{key}:decrypted}", table, &error) == 1, i);
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi table[5].value = str_c(input);
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi test_assert_idx(var_expand(output, "%{decrypt;algo=aes-128-cbc,key=%{key}:encrypted2}", table, &error) == 1, i);
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi test_assert_idx(strcmp(str_c(output), table[4].value)==0, i);
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi };
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi var_expand_crypt_deinit();
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi test_end();
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi}
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomiint main(void)
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi{
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi int ret = 0;
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi static void (*const test_functions[])(void) = {
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi test_var_expand_crypt,
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi NULL
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi };
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi struct dcrypt_settings set = {
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi .module_dir = DCRYPT_BUILD_DIR"/.libs"
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi };
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi if (!dcrypt_initialize(NULL, &set, NULL))
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi return 0;
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi ret = test_run(test_functions);
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi dcrypt_deinitialize();
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi return ret;
205f29a013ee4aa95e9be993ebf261a9d9bc251bAki Tuomi}