bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen
332e3fd09754ad85800a736292a0ef8dfdca4c9eAki Tuomi#include "test-auth.h"
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen#include "array.h"
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen#include "db-dict.h"
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen
332e3fd09754ad85800a736292a0ef8dfdca4c9eAki Tuomivoid test_db_dict_parse_cache_key(void)
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen{
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen struct db_dict_key keys[] = {
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen { "key0", "%d and %n", NULL, NULL, 0 },
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen { "key1", "%{foo}%r%{bar}", NULL, NULL, 0 },
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen { "key2", "%{test1}/path", NULL, NULL, 0 },
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen { "key3", "path2/%{test2}", NULL, NULL, 0 },
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen { "key4", "%{plop}", NULL, NULL, 0 },
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen { "key5", "%{unused}", NULL, NULL, 0 }
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen };
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen struct db_dict_field fields[] = {
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen { "name1", "hello %{dict:key0} %l and %{dict:key1}" },
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen { "name2", "%{dict:key2} also %{extra} plus" }
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen };
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen const struct db_dict_key *objects[] = {
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen &keys[3], &keys[4]
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen };
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen buffer_t keybuf, fieldbuf, objectbuf;
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen ARRAY_TYPE(db_dict_key) keyarr;
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen ARRAY_TYPE(db_dict_field) fieldarr;
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen ARRAY_TYPE(db_dict_key_p) objectarr;
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen test_begin("db dict parse cache key");
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen buffer_create_from_const_data(&keybuf, keys, sizeof(keys));
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen buffer_create_from_const_data(&fieldbuf, fields, sizeof(fields));
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen buffer_create_from_const_data(&objectbuf, objects, sizeof(objects));
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen array_create_from_buffer(&keyarr, &keybuf, sizeof(keys[0]));
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen array_create_from_buffer(&fieldarr, &fieldbuf, sizeof(fields[0]));
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen array_create_from_buffer(&objectarr, &objectbuf, sizeof(objects[0]));
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen test_assert(strcmp(db_dict_parse_cache_key(&keyarr, &fieldarr, &objectarr),
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen "\t%d and %n\t%l\t%{foo}%r%{bar}\t%{test1}/path\t%{extra}\tpath2/%{test2}\t%{plop}") == 0);
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen test_end();
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen}