From: Markus Valentin Date: Tue, 12 Mar 2024 07:01:44 +0000 (+0100) Subject: auth: Introduce settings_simple_init() for mocking passdb in tests X-Git-Tag: 2.4.1~924 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=abed12cf9d48602700b26262be8a88a5125cbc43;p=thirdparty%2Fdovecot%2Fcore.git auth: Introduce settings_simple_init() for mocking passdb in tests --- diff --git a/src/auth/test-auth.c b/src/auth/test-auth.c index 9025f1ea33..07a1bc6598 100644 --- a/src/auth/test-auth.c +++ b/src/auth/test-auth.c @@ -3,6 +3,7 @@ #include "test-auth.h" #include "ostream.h" #include "auth-common.h" +#include "settings.h" #include "settings-parser.h" #include "auth-settings.h" #include "auth-token.h" @@ -16,11 +17,33 @@ #include -struct auth_settings test_auth_set; -static struct mechanisms_register *mech_reg; - #define TEST_OAUTH2_CONFIG_FILE "test-oauth2-config" +static const char *const settings[] = { + "base_dir", ".", + "auth_mechanisms", "plain", + "auth_username_chars", "", + "auth_username_format", "", + /* For tests of digest-md5. */ + "auth_realms", "example.com", + /* For tests of mech-anonymous. */ + "auth_anonymous_username", "anonuser", + /* For oauth2 tests */ + "auth_oauth2_config_file", TEST_OAUTH2_CONFIG_FILE, + + "passdb", "mock1 mock2", + "passdb/mock1/name", "mock1", + "passdb/mock1/driver", "mock", + "passdb/mock2/name", "mock1", + "passdb/mock2/driver", "mock", + "passdb/mock2/master", "yes", + + NULL +}; + +static struct mechanisms_register *mech_reg; +static struct settings_simple simple_set; + void test_auth_init(void) { const char *const protocols[] = {NULL}; @@ -33,36 +56,9 @@ void test_auth_init(void) test_assert(o_stream_finish(os) == 1); o_stream_unref(&os); - /* Copy default settings */ - test_auth_set = *(const struct auth_settings *)auth_setting_parser_info.defaults; - test_auth_set.pool = pool_alloconly_create("test settings", 128); - test_auth_set.base_dir = "."; - p_array_init(&test_auth_set.mechanisms, test_auth_set.pool, 1); - const char *plain = "plain"; - array_push_back(&test_auth_set.mechanisms, &plain); - global_auth_settings = &test_auth_set; - memset((&test_auth_set)->username_chars_map, 1, - sizeof((&test_auth_set)->username_chars_map)); - test_auth_set.username_format = ""; - - p_array_init(&test_auth_set.parsed_passdbs, test_auth_set.pool, 2); - struct auth_passdb_settings *mock_set = t_new(struct auth_passdb_settings, 1); - *mock_set = mock_passdb_set; - const struct auth_passdb_settings *const_mock_set = mock_set; - array_push_back(&test_auth_set.parsed_passdbs, &const_mock_set); - mock_set = p_new(test_auth_set.pool, struct auth_passdb_settings, 1); - *mock_set = mock_passdb_set; - mock_set->master = TRUE; - const_mock_set = mock_set; - array_push_back(&test_auth_set.parsed_passdbs, &const_mock_set); - p_array_init(&test_auth_set.parsed_userdbs, test_auth_set.pool, 1); - - /* For tests of digest-md5. */ - test_auth_set.realms_arr = t_strsplit_spaces("example.com ", " "); - /* For tests of mech-anonymous. */ - test_auth_set.anonymous_username = "anonuser"; - /* For oauth2 tests */ - test_auth_set.oauth2_config_file = TEST_OAUTH2_CONFIG_FILE; + settings_simple_init(&simple_set, settings); + global_auth_settings = settings_get_or_fatal(simple_set.event, + &auth_setting_parser_info); mech_init(global_auth_settings); mech_reg = mech_register_init(global_auth_settings); @@ -72,7 +68,7 @@ void test_auth_init(void) password_schemes_init(); password_schemes_allow_weak(TRUE); - auths_preinit(NULL, &test_auth_set, mech_reg, protocols); + auths_preinit(simple_set.event, global_auth_settings, mech_reg, protocols); auths_init(); auth_token_init(); @@ -94,7 +90,8 @@ void test_auth_deinit(void) mech_deinit(global_auth_settings); mech_register_deinit(&mech_reg); auths_free(); - pool_unref(&test_auth_set.pool); + settings_free(global_auth_settings); + settings_simple_deinit(&simple_set); i_unlink_if_exists("auth-token-secret.dat"); i_unlink_if_exists(TEST_OAUTH2_CONFIG_FILE); } diff --git a/src/auth/test-auth.h b/src/auth/test-auth.h index dd946a36cb..a707b7a56e 100644 --- a/src/auth/test-auth.h +++ b/src/auth/test-auth.h @@ -10,9 +10,6 @@ struct auth_passdb; -extern struct auth_passdb_settings mock_passdb_set; -extern struct auth_settings test_auth_set; - void test_auth_request_var_expand(void); void test_auth_request_fields(void); void test_db_dict_parse_cache_key(void); diff --git a/src/auth/test-mech.c b/src/auth/test-mech.c index 3445f30e12..fdad66905c 100644 --- a/src/auth/test-mech.c +++ b/src/auth/test-mech.c @@ -87,15 +87,19 @@ static void test_mech_prepare_request(struct auth_request **request_r, unsigned int running_test, const struct test_case *test_case) { - test_auth_set.ssl_username_from_cert = test_case->set_cert_username; struct auth *auth = auth_default_protocol(); struct auth_request *request = auth_request_new(mech, NULL); + struct auth_settings *new_set = + p_memdup(request->pool, global_auth_settings, + sizeof(*global_auth_settings)); + new_set->ssl_username_from_cert = test_case->set_cert_username; + request->handler = handler; request->id = running_test+1; request->mech_password = NULL; request->state = AUTH_REQUEST_STATE_NEW; - request->set = global_auth_settings; + request->set = new_set; request->protocol_set = global_auth_settings; request->connect_uid = running_test; request->passdb = auth->passdbs; diff --git a/src/auth/test-mock.c b/src/auth/test-mock.c index 86d5bdc969..b0c1db4a02 100644 --- a/src/auth/test-mock.c +++ b/src/auth/test-mock.c @@ -43,20 +43,6 @@ static struct passdb_module_interface mock_interface = { .lookup_credentials = passdb_mock_lookup_credentials, }; -struct auth_passdb_settings mock_passdb_set = { - .name = "mock", - .driver = "mock", - .args = "", - .mechanisms = ARRAY_INIT, - .username_filter = "", - .skip = "never", - .result_success = "return-ok", - .result_failure = "continue", - .result_internalfail = "continue", - .deny = FALSE, - .master = FALSE, -}; - void passdb_mock_mod_init(void) { if (mock_passdb_mod != NULL)