]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
auth: Introduce settings_simple_init() for mocking passdb in tests
authorMarkus Valentin <markus.valentin@open-xchange.com>
Tue, 12 Mar 2024 07:01:44 +0000 (08:01 +0100)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Wed, 12 Feb 2025 10:34:12 +0000 (12:34 +0200)
src/auth/test-auth.c
src/auth/test-auth.h
src/auth/test-mech.c
src/auth/test-mock.c

index 9025f1ea337d45d633edd44454575107df4c96f3..07a1bc65987136faa5a0d934edf29b08ca5f5f8c 100644 (file)
@@ -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"
 
 #include <time.h>
 
-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);
 }
index dd946a36cb51b663fc9f25f518b8d78b3c02eb38..a707b7a56eff87498fdf648471b783d10a0b631d 100644 (file)
@@ -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);
index 3445f30e12554f64fa7cfb51f74da5d4b31248bd..fdad66905cc4274e36d7422ab16a29fc4c6f3979 100644 (file)
@@ -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;
index 86d5bdc96915fe1217a9c8596322ecad104bc2fd..b0c1db4a022f4e16303d5e857b6435e6251ada2e 100644 (file)
@@ -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)