bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen#include "lib.h"
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen#include "str.h"
bdd36cfdba3ff66d25570a9ff568d69e1eb543cfTimo Sirainen#include "auth-request.h"
5566faf0b48f0b77b0134f34130bdc7842c844ebTimo Sirainen#include "auth-cache.h"
e9d9f9071b12a687564c6817f3984cc1cd56d866Timo Sirainen#include "test-common.h"
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainenconst struct var_expand_table auth_request_var_expand_static_tab[] = {
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen /* these 3 must be in this order */
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen { 'u', NULL, "user" },
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen { 'n', NULL, "username" },
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen { 'd', NULL, "domain" },
120ab73a12c9a752d112db80cd467eea514493c6Timo Sirainen
120ab73a12c9a752d112db80cd467eea514493c6Timo Sirainen { 'a', NULL, NULL },
120ab73a12c9a752d112db80cd467eea514493c6Timo Sirainen { '\0', NULL, "longb" },
120ab73a12c9a752d112db80cd467eea514493c6Timo Sirainen { 'c', NULL, "longc" },
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen { '\0', NULL, NULL }
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen};
e9d9f9071b12a687564c6817f3984cc1cd56d866Timo Sirainen
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainenint t_auth_request_var_expand(const char *str,
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen const struct auth_request *auth_request ATTR_UNUSED,
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen auth_request_escape_func_t *escape_func ATTR_UNUSED,
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen const char **value_r, const char **error_r)
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen{
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen string_t *dest = t_str_new(128);
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen int ret = var_expand(dest, str, auth_request_var_expand_static_tab, error_r);
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen *value_r = str_c(dest);
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen return ret;
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen}
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainenstatic void test_auth_cache_parse_key(void)
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen{
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen static const struct {
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen const char *in, *out;
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen } tests[] = {
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen { "%n@%d", "%u" },
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen { "%{username}@%{domain}", "%u" },
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen { "%n%d%u", "%u" },
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen { "%n", "%n" },
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen { "%d", "%d" },
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen { "%a%b%u", "%u\t%a\t%b" },
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen { "foo%5.5Mabar", "%a" },
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen { "foo%5.5M{longb}bar", "%{longb}" },
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen { "foo%5.5Mcbar", "%c" },
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen { "foo%5.5M{longc}bar", "%c" },
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen { "%a%b", "%a\t%b" },
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen { "%a%{longb}%a", "%a\t%{longb}" },
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen { "%{longc}%c", "%c" },
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen { "%c%a%{longc}%c", "%a\t%c" },
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen { "%a%{env:foo}%{env:foo}%a", "%a\t%{env:foo}\t%{env:foo}" }
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen };
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen const char *cache_key;
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen unsigned int i;
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen test_begin("auth cache parse key");
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen for (i = 0; i < N_ELEMENTS(tests); i++) {
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen cache_key = auth_cache_parse_key(pool_datastack_create(),
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen tests[i].in);
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen test_assert(strcmp(cache_key, tests[i].out) == 0);
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen }
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen test_end();
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen}
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainenint main(void)
5566faf0b48f0b77b0134f34130bdc7842c844ebTimo Sirainen{
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen static void (*const test_functions[])(void) = {
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen test_auth_cache_parse_key,
e76073ebaf90fa29abfdc364873acf78983949aaTimo Sirainen NULL
e9d9f9071b12a687564c6817f3984cc1cd56d866Timo Sirainen };
e9d9f9071b12a687564c6817f3984cc1cd56d866Timo Sirainen return test_run(test_functions);
e9d9f9071b12a687564c6817f3984cc1cd56d866Timo Sirainen}
e9d9f9071b12a687564c6817f3984cc1cd56d866Timo Sirainen