Greenbone Vulnerability Management Libraries 22.32.0
authutils_tests.c File Reference
#include "authutils.c"
#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>
#include <glib.h>
Include dependency graph for authutils_tests.c:

Go to the source code of this file.

Functions

 Describe (authutils)
 BeforeEach (authutils)
 AfterEach (authutils)
 Ensure (authutils, auth_method_name_returns_correct_strings)
 Ensure (authutils, auth_method_name_valid_returns_one_for_valid_names)
 Ensure (authutils, auth_method_name_valid_returns_zero_for_invalid_names)
 Ensure (authutils, gvm_auth_ldap_enabled_returns_one_when_enabled)
 Ensure (authutils, gvm_auth_radius_enabled_returns_one_when_enabled)
 Ensure (authutils, digest_hex_returns_correct_hex_string)
 Ensure (authutils, digest_hex_returns_null_for_invalid_algorithm)
 Ensure (authutils, get_md5_hash_from_string_returns_correct_hash)
 Ensure (authutils, get_md5_hash_from_string_handles_empty_string)
 Ensure (authutils, get_password_hashes_returns_valid_hash_pair)
 Ensure (authutils, gvm_authenticate_classic_succeeds_with_correct_password)
 Ensure (authutils, gvm_authenticate_classic_fails_with_incorrect_password)
 Ensure (authutils, gvm_authenticate_classic_fails_with_null_hash)
 Ensure (authutils, gvm_authenticate_classic_returns_error_for_invalid_hash_format)
 Ensure (authutils, gvm_auth_init_succeeds_on_first_call)
 Ensure (authutils, gvm_auth_init_fails_on_second_call)
int main (int argc, char **argv)

Function Documentation

◆ AfterEach()

AfterEach ( authutils )

Definition at line 19 of file authutils_tests.c.

20{
21}

◆ BeforeEach()

BeforeEach ( authutils )

Definition at line 13 of file authutils_tests.c.

14{
15 // Initialize auth for tests that need gcrypt functionality
17}
int gvm_auth_init(void)
Initializes Gcrypt.
Definition authutils.c:109

References gvm_auth_init().

Here is the call graph for this function:

◆ Describe()

Describe ( authutils )

◆ Ensure() [1/16]

Ensure ( authutils ,
auth_method_name_returns_correct_strings  )

Definition at line 25 of file authutils_tests.c.

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}
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

References auth_method_name(), AUTHENTICATION_METHOD_FILE, AUTHENTICATION_METHOD_LAST, AUTHENTICATION_METHOD_LDAP_CONNECT, and AUTHENTICATION_METHOD_RADIUS_CONNECT.

Here is the call graph for this function:

◆ Ensure() [2/16]

Ensure ( authutils ,
auth_method_name_valid_returns_one_for_valid_names  )

Definition at line 39 of file authutils_tests.c.

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}
int auth_method_name_valid(const gchar *name)
Check if name is a valid auth method name.
Definition authutils.c:91

References auth_method_name_valid().

Here is the call graph for this function:

◆ Ensure() [3/16]

Ensure ( authutils ,
auth_method_name_valid_returns_zero_for_invalid_names  )

Definition at line 46 of file authutils_tests.c.

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}

References auth_method_name_valid().

Here is the call graph for this function:

◆ Ensure() [4/16]

Ensure ( authutils ,
digest_hex_returns_correct_hex_string  )

Definition at line 76 of file authutils_tests.c.

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}
gchar * digest_hex(int gcrypt_algorithm, const guchar *digest)
Generate a hexadecimal representation of a message digest.
Definition authutils.c:175

References digest_hex().

Here is the call graph for this function:

◆ Ensure() [5/16]

Ensure ( authutils ,
digest_hex_returns_null_for_invalid_algorithm  )

Definition at line 86 of file authutils_tests.c.

87{
88 guchar digest[] = {0x00, 0x01, 0x02};
89 gchar *hex = digest_hex (9999, digest); // Invalid algorithm
90 assert_that (hex, is_null);
91}

References digest_hex().

Here is the call graph for this function:

◆ Ensure() [6/16]

Ensure ( authutils ,
get_md5_hash_from_string_handles_empty_string  )

Definition at line 104 of file authutils_tests.c.

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}
gchar * get_md5_hash_from_string(const gchar *string)
Calculate the MD5 hash value for a given string.
Definition authutils.c:249

References get_md5_hash_from_string().

Here is the call graph for this function:

◆ Ensure() [7/16]

Ensure ( authutils ,
get_md5_hash_from_string_returns_correct_hash  )

Definition at line 95 of file authutils_tests.c.

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}

References get_md5_hash_from_string().

Here is the call graph for this function:

◆ Ensure() [8/16]

Ensure ( authutils ,
get_password_hashes_returns_valid_hash_pair  )

Definition at line 115 of file authutils_tests.c.

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}
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

References get_password_hashes().

Here is the call graph for this function:

◆ Ensure() [9/16]

Ensure ( authutils ,
gvm_auth_init_fails_on_second_call  )

Definition at line 185 of file authutils_tests.c.

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}
static gboolean initialized
Flag whether the config file was read.
Definition authutils.c:33

References gvm_auth_init(), and initialized.

Here is the call graph for this function:

◆ Ensure() [10/16]

Ensure ( authutils ,
gvm_auth_init_succeeds_on_first_call  )

Definition at line 176 of file authutils_tests.c.

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}

References gvm_auth_init(), and initialized.

Here is the call graph for this function:

◆ Ensure() [11/16]

Ensure ( authutils ,
gvm_auth_ldap_enabled_returns_one_when_enabled  )

Definition at line 54 of file authutils_tests.c.

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}
int gvm_auth_ldap_enabled(void)
Return whether libraries has been compiled with LDAP support.
Definition authutils.c:41

References gvm_auth_ldap_enabled().

Here is the call graph for this function:

◆ Ensure() [12/16]

Ensure ( authutils ,
gvm_auth_radius_enabled_returns_one_when_enabled  )

Definition at line 65 of file authutils_tests.c.

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}
int gvm_auth_radius_enabled(void)
Return whether libraries has been compiled with RADIUS support.
Definition authutils.c:56

References gvm_auth_radius_enabled().

Here is the call graph for this function:

◆ Ensure() [13/16]

Ensure ( authutils ,
gvm_authenticate_classic_fails_with_incorrect_password  )

Definition at line 149 of file authutils_tests.c.

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}
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

References get_password_hashes(), and gvm_authenticate_classic().

Here is the call graph for this function:

◆ Ensure() [14/16]

Ensure ( authutils ,
gvm_authenticate_classic_fails_with_null_hash  )

Definition at line 161 of file authutils_tests.c.

162{
163 int result = gvm_authenticate_classic ("user", "password", NULL);
164 assert_that (result, is_equal_to (1)); // Failure
165}

References gvm_authenticate_classic().

Here is the call graph for this function:

◆ Ensure() [15/16]

Ensure ( authutils ,
gvm_authenticate_classic_returns_error_for_invalid_hash_format  )

Definition at line 167 of file authutils_tests.c.

169{
170 int result = gvm_authenticate_classic ("user", "password", "invalid");
171 assert_that (result, is_equal_to (-1)); // Error
172}

References gvm_authenticate_classic().

Here is the call graph for this function:

◆ Ensure() [16/16]

Ensure ( authutils ,
gvm_authenticate_classic_succeeds_with_correct_password  )

Definition at line 137 of file authutils_tests.c.

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}

References get_password_hashes(), and gvm_authenticate_classic().

Here is the call graph for this function:

◆ main()

int main ( int argc,
char ** argv )

Definition at line 202 of file authutils_tests.c.

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}