view src/auth/test-auth-cache.c @ 22713:cb108f786fb4

Updated copyright notices to include the year 2018.
author Stephan Bosch <stephan.bosch@dovecot.fi>
date Mon, 01 Jan 2018 22:42:08 +0100
parents 2e2563132d5f
children
line wrap: on
line source

/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "str.h"
#include "auth-request.h"
#include "auth-cache.h"
#include "test-common.h"

const struct var_expand_table auth_request_var_expand_static_tab[] = {
	/* these 3 must be in this order */
	{ 'u', NULL, "user" },
	{ 'n', NULL, "username" },
	{ 'd', NULL, "domain" },

	{ 'a', NULL, NULL },
	{ '\0', NULL, "longb" },
	{ 'c', NULL, "longc" },
	{ '\0', NULL, NULL }
};

const char *
t_auth_request_var_expand(const char *str,
			  const struct auth_request *auth_request ATTR_UNUSED,
			  auth_request_escape_func_t *escape_func ATTR_UNUSED)
{
	string_t *dest = t_str_new(128);
	var_expand(dest, str, auth_request_var_expand_static_tab);
	return str_c(dest);
}

static void test_auth_cache_parse_key(void)
{
	struct {
		const char *in, *out;
	} tests[] = {
		{ "%n@%d", "%u" },
		{ "%{username}@%{domain}", "%u" },
		{ "%n%d%u", "%u" },
		{ "%n", "%n" },
		{ "%d", "%d" },
		{ "%a%b%u", "%u\t%a\t%b" },

		{ "foo%5.5Mabar", "%a" },
		{ "foo%5.5M{longb}bar", "%{longb}" },
		{ "foo%5.5Mcbar", "%c" },
		{ "foo%5.5M{longc}bar", "%c" },
		{ "%a%b", "%a\t%b" },
		{ "%a%{longb}%a", "%a\t%{longb}" },
		{ "%{longc}%c", "%c" },
		{ "%c%a%{longc}%c", "%a\t%c" },
		{ "%a%{env:foo}%{env:foo}%a", "%a\t%{env:foo}\t%{env:foo}" }
	};
	const char *cache_key;
	unsigned int i;

	test_begin("auth cache parse key");

	for (i = 0; i < N_ELEMENTS(tests); i++) {
		cache_key = auth_cache_parse_key(pool_datastack_create(),
						 tests[i].in);
		test_assert(strcmp(cache_key, tests[i].out) == 0);
	}
	test_end();
}

int main(void)
{
	static void (*test_functions[])(void) = {
		test_auth_cache_parse_key,
		NULL
	};
	return test_run(test_functions);
}