Greenbone Vulnerability Management Libraries 22.32.0
authutils_tests.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6#include "authutils.c"
7
8#include <cgreen/cgreen.h>
9#include <cgreen/mocks.h>
10#include <glib.h>
11
12Describe (authutils);
13BeforeEach (authutils)
14{
15 // Initialize auth for tests that need gcrypt functionality
17}
18
19AfterEach (authutils)
20{
21}
22
23/* auth_method_name */
24
25Ensure (authutils, auth_method_name_returns_correct_strings)
26{
28 is_equal_to_string ("file"));
30 is_equal_to_string ("ldap_connect"));
32 is_equal_to_string ("radius_connect"));
34 is_equal_to_string ("ERROR"));
35}
36
37/* auth_method_name_valid */
38
39Ensure (authutils, auth_method_name_valid_returns_one_for_valid_names)
40{
41 assert_that (auth_method_name_valid ("file"), is_equal_to (1));
42 assert_that (auth_method_name_valid ("ldap_connect"), is_equal_to (1));
43 assert_that (auth_method_name_valid ("radius_connect"), is_equal_to (1));
44}
45
46Ensure (authutils, auth_method_name_valid_returns_zero_for_invalid_names)
47{
48 assert_that (auth_method_name_valid ("invalid_method"), is_equal_to (0));
49 assert_that (auth_method_name_valid (NULL), is_equal_to (0));
50}
51
52/* gvm_auth_ldap_enabled */
53
54Ensure (authutils, gvm_auth_ldap_enabled_returns_one_when_enabled)
55{
56#ifdef ENABLE_LDAP_AUTH
57 assert_that (gvm_auth_ldap_enabled (), is_equal_to (1));
58#else
59 assert_that (gvm_auth_ldap_enabled (), is_equal_to (0));
60#endif
61}
62
63/* gvm_auth_radius_enabled */
64
65Ensure (authutils, gvm_auth_radius_enabled_returns_one_when_enabled)
66{
67#ifdef ENABLE_RADIUS_AUTH
68 assert_that (gvm_auth_radius_enabled (), is_equal_to (1));
69#else
70 assert_that (gvm_auth_radius_enabled (), is_equal_to (0));
71#endif
72}
73
74/* digest_hex */
75
76Ensure (authutils, digest_hex_returns_correct_hex_string)
77{
78 guchar digest[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
79 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
80 gchar *hex = digest_hex (GCRY_MD_MD5, digest);
81 assert_that (hex, is_not_null);
82 assert_that (hex, is_equal_to_string ("000102030405060708090a0b0c0d0e0f"));
83 g_free (hex);
84}
85
86Ensure (authutils, digest_hex_returns_null_for_invalid_algorithm)
87{
88 guchar digest[] = {0x00, 0x01, 0x02};
89 gchar *hex = digest_hex (9999, digest); // Invalid algorithm
90 assert_that (hex, is_null);
91}
92
93/* get_md5_hash_from_string */
94
95Ensure (authutils, get_md5_hash_from_string_returns_correct_hash)
96{
97 gchar *hash = get_md5_hash_from_string ("test");
98 assert_that (hash, is_not_null);
99 // MD5 hash of "test" is "098f6bcd4621d373cade4e832627b4f6"
100 assert_that (hash, is_equal_to_string ("098f6bcd4621d373cade4e832627b4f6"));
101 g_free (hash);
102}
103
104Ensure (authutils, get_md5_hash_from_string_handles_empty_string)
105{
106 gchar *hash = get_md5_hash_from_string ("");
107 assert_that (hash, is_not_null);
108 // MD5 hash of "" is "d41d8cd98f00b204e9800998ecf8427e"
109 assert_that (hash, is_equal_to_string ("d41d8cd98f00b204e9800998ecf8427e"));
110 g_free (hash);
111}
112
113/* get_password_hashes */
114
115Ensure (authutils, get_password_hashes_returns_valid_hash_pair)
116{
117 gchar *hashes = get_password_hashes ("password");
118 assert_that (hashes, is_not_null);
119
120 // Should contain two MD5 hashes separated by a space
121 gchar **split = g_strsplit (hashes, " ", 2);
122 assert_that (split, is_not_null);
123 assert_that (split[0], is_not_null);
124 assert_that (split[1], is_not_null);
125 assert_that (split[2], is_null); // Should only have two elements
126
127 // Both should be 32 characters (MD5 hex representation)
128 assert_that (strlen (split[0]), is_equal_to (32));
129 assert_that (strlen (split[1]), is_equal_to (32));
130
131 g_strfreev (split);
132 g_free (hashes);
133}
134
135/* gvm_authenticate_classic */
136
137Ensure (authutils, gvm_authenticate_classic_succeeds_with_correct_password)
138{
139 // Generate a valid hash for testing
140 gchar *hashes = get_password_hashes ("password");
141 assert_that (hashes, is_not_null);
142
143 int result = gvm_authenticate_classic ("user", "password", hashes);
144 assert_that (result, is_equal_to (0)); // Success
145
146 g_free (hashes);
147}
148
149Ensure (authutils, gvm_authenticate_classic_fails_with_incorrect_password)
150{
151 // Generate a valid hash for testing
152 gchar *hashes = get_password_hashes ("password");
153 assert_that (hashes, is_not_null);
154
155 int result = gvm_authenticate_classic ("user", "wrongpassword", hashes);
156 assert_that (result, is_equal_to (1)); // Failure
157
158 g_free (hashes);
159}
160
161Ensure (authutils, gvm_authenticate_classic_fails_with_null_hash)
162{
163 int result = gvm_authenticate_classic ("user", "password", NULL);
164 assert_that (result, is_equal_to (1)); // Failure
165}
166
167Ensure (authutils,
168 gvm_authenticate_classic_returns_error_for_invalid_hash_format)
169{
170 int result = gvm_authenticate_classic ("user", "password", "invalid");
171 assert_that (result, is_equal_to (-1)); // Error
172}
173
174/* gvm_auth_init */
175
176Ensure (authutils, gvm_auth_init_succeeds_on_first_call)
177{
178 // For this test, we need to reset the initialized flag
179 // This is a special case where we test the init function itself
180 initialized = FALSE;
181 int result = gvm_auth_init ();
182 assert_that (result, is_equal_to (0)); // Success
183}
184
185Ensure (authutils, gvm_auth_init_fails_on_second_call)
186{
187 // For this test, we need to reset the initialized flag
188 // This is a special case where we test the init function itself
189 initialized = FALSE;
190
191 // First call
192 gvm_auth_init ();
193
194 // Second call should return error
195 int result = gvm_auth_init ();
196 assert_that (result, is_equal_to (-1)); // Error
197}
198
199/* Test suite. */
200
201int
202main (int argc, char **argv)
203{
204 int ret;
205 TestSuite *suite;
206
207 suite = create_test_suite ();
208
209 add_test_with_context (suite, authutils,
210 auth_method_name_returns_correct_strings);
211 add_test_with_context (suite, authutils,
212 auth_method_name_valid_returns_one_for_valid_names);
213 add_test_with_context (suite, authutils,
214 auth_method_name_valid_returns_zero_for_invalid_names);
215 add_test_with_context (suite, authutils,
216 gvm_auth_ldap_enabled_returns_one_when_enabled);
217 add_test_with_context (suite, authutils,
218 gvm_auth_radius_enabled_returns_one_when_enabled);
219 add_test_with_context (suite, authutils,
220 digest_hex_returns_correct_hex_string);
221 add_test_with_context (suite, authutils,
222 digest_hex_returns_null_for_invalid_algorithm);
223 add_test_with_context (suite, authutils,
224 get_md5_hash_from_string_returns_correct_hash);
225 add_test_with_context (suite, authutils,
226 get_md5_hash_from_string_handles_empty_string);
227 add_test_with_context (suite, authutils,
228 get_password_hashes_returns_valid_hash_pair);
229 add_test_with_context (
230 suite, authutils, gvm_authenticate_classic_succeeds_with_correct_password);
231 add_test_with_context (
232 suite, authutils, gvm_authenticate_classic_fails_with_incorrect_password);
233 add_test_with_context (suite, authutils,
234 gvm_authenticate_classic_fails_with_null_hash);
235 add_test_with_context (
236 suite, authutils,
237 gvm_authenticate_classic_returns_error_for_invalid_hash_format);
238 add_test_with_context (suite, authutils,
239 gvm_auth_init_succeeds_on_first_call);
240 add_test_with_context (suite, authutils, gvm_auth_init_fails_on_second_call);
241
242 if (argc > 1)
243 ret = run_single_test (suite, argv[1], create_text_reporter ());
244 else
245 ret = run_test_suite (suite, create_text_reporter ());
246
247 destroy_test_suite (suite);
248
249 return ret;
250}
Authentication mechanism(s).
int auth_method_name_valid(const gchar *name)
Check if name is a valid auth method name.
Definition authutils.c:91
int gvm_auth_radius_enabled(void)
Return whether libraries has been compiled with RADIUS support.
Definition authutils.c:56
gchar * get_md5_hash_from_string(const gchar *string)
Calculate the MD5 hash value for a given string.
Definition authutils.c:249
int gvm_authenticate_classic(const gchar *username, const gchar *password, const gchar *hash_arg)
Authenticate a credential pair against user file contents.
Definition authutils.c:274
int gvm_auth_init(void)
Initializes Gcrypt.
Definition authutils.c:109
gchar * digest_hex(int gcrypt_algorithm, const guchar *digest)
Generate a hexadecimal representation of a message digest.
Definition authutils.c:175
gchar * get_password_hashes(const gchar *password)
Generate a pair of md5 hashes to be used in the "auth/hash" file for the user.
Definition authutils.c:210
static gboolean initialized
Flag whether the config file was read.
Definition authutils.c:33
int gvm_auth_ldap_enabled(void)
Return whether libraries has been compiled with LDAP support.
Definition authutils.c:41
const gchar * auth_method_name(auth_method_t method)
Return name of auth_method_t.
Definition authutils.c:76
@ AUTHENTICATION_METHOD_FILE
Definition authutils.h:23
@ AUTHENTICATION_METHOD_LDAP_CONNECT
Definition authutils.h:24
@ AUTHENTICATION_METHOD_LAST
Definition authutils.h:26
@ AUTHENTICATION_METHOD_RADIUS_CONNECT
Definition authutils.h:25
int main(int argc, char **argv)
AfterEach(authutils)
BeforeEach(authutils)
Ensure(authutils, auth_method_name_returns_correct_strings)
Describe(authutils)