Skip to content

Commit 2f46e6f

Browse files
flichtenheldcron2
authored andcommitted
test_user_pass: Check fatal errors for empty username/password
Required a fix to mock_msg to make tests of M_FATAL possible at all. This also tests some cases which arguably should throw a fatal error but do not. v2: - Suppress LeakSanitizer errors for fatal error tests. Due to aborting the function, the memory will not be cleaned up, but that is expected. v3: - Disable assert tests with MSVC. Does not seem to catch the error correctly. - Rebase on top of parallel-tests series to get AM_TESTS_ENVIRONMENT. v8: - Update srcdir handling according to master. v10: - Update mock_msg.c fatal handling to be compatible with NO_CMOCKA. Change-Id: Icabc8acf75638c86c8c395e9ffecba7a7226cd97 Signed-off-by: Frank Lichtenheld <[email protected]>
1 parent 8550948 commit 2f46e6f

File tree

7 files changed

+112
-3
lines changed

7 files changed

+112
-3
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ if (BUILD_TESTING)
729729
add_test(${test_name} ${test_name})
730730
# for compat with autotools make check
731731
set_tests_properties(${test_name} PROPERTIES
732-
ENVIRONMENT "srcdir=${_UT_SOURCE_DIR}")
732+
ENVIRONMENT "srcdir=${_UT_SOURCE_DIR};LSAN_OPTIONS=suppressions=${_UT_SOURCE_DIR}/input/leak_suppr.txt")
733733
endif ()
734734
add_executable(${test_name}
735735
tests/unit_tests/openvpn/${test_name}.c

tests/unit_tests/openvpn/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ EXTRA_DIST = input
44

55
AM_TESTSUITE_SUMMARY_HEADER = ' for $(PACKAGE_STRING) Unit-Tests'
66

7+
AM_TESTS_ENVIRONMENT = export LSAN_OPTIONS=suppressions=$(srcdir)/input/leak_suppr.txt;
8+
79
test_binaries = argv_testdriver buffer_testdriver crypto_testdriver packet_id_testdriver auth_token_testdriver \
810
ncp_testdriver misc_testdriver options_parse_testdriver pkt_testdriver ssl_testdriver \
911
user_pass_testdriver push_update_msg_testdriver provider_testdriver socket_testdriver
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
3+
(contains \t\n\t\n)

tests/unit_tests/openvpn/input/empty.txt

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
leak:_assertions$

tests/unit_tests/openvpn/mock_msg.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
msglvl_t x_debug_level = 0; /* Default to (almost) no debugging output */
4242
msglvl_t print_x_debug_level = 0;
4343

44-
bool fatal_error_triggered = false;
4544

4645
char mock_msg_buf[MOCK_MSG_BUF];
4746

@@ -75,7 +74,6 @@ x_msg_va(const msglvl_t flags, const char *format, va_list arglist)
7574
{
7675
if (flags & M_FATAL)
7776
{
78-
fatal_error_triggered = true;
7977
printf("FATAL ERROR:");
8078
}
8179
CLEAR(mock_msg_buf);
@@ -86,6 +84,12 @@ x_msg_va(const msglvl_t flags, const char *format, va_list arglist)
8684
printf("%s", mock_msg_buf);
8785
printf("\n");
8886
}
87+
#ifndef NO_CMOCKA
88+
if (flags & M_FATAL)
89+
{
90+
mock_assert(false, "FATAL ERROR", __FILE__, __LINE__);
91+
}
92+
#endif
8993
}
9094

9195
void

tests/unit_tests/openvpn/test_user_pass.c

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,16 @@ test_get_user_pass_inline_creds(void **state)
180180

181181
reset_user_pass(&up);
182182

183+
/*FIXME: query_user_exec() called even though nothing queued */
184+
will_return(query_user_exec_builtin, true);
185+
/*FIXME: silently removes control characters but does not error out */
186+
assert_true(get_user_pass_cr(&up, "\t\n\t", "UT", flags, NULL));
187+
assert_true(up.defined);
188+
assert_string_equal(up.username, "");
189+
assert_string_equal(up.password, "");
190+
191+
reset_user_pass(&up);
192+
183193
expect_string(query_user_exec_builtin, query_user[i].prompt, "Enter UT Password:");
184194
will_return(query_user_exec_builtin, "cpassword");
185195
will_return(query_user_exec_builtin, true);
@@ -212,6 +222,25 @@ test_get_user_pass_inline_creds(void **state)
212222
assert_string_equal(up.password, "cpassword");
213223
}
214224

225+
/* NOTE: expect_assert_failure does not seem to work with MSVC */
226+
#ifndef _MSC_VER
227+
/* NOTE: leaks gc memory */
228+
static void
229+
test_get_user_pass_inline_creds_assertions(void **state)
230+
{
231+
struct user_pass up = { 0 };
232+
reset_user_pass(&up);
233+
unsigned int flags = GET_USER_PASS_INLINE_CREDS;
234+
235+
reset_user_pass(&up);
236+
237+
/*FIXME: query_user_exec() called even though nothing queued */
238+
/*FIXME? username error thrown very late in stdin handling */
239+
will_return(query_user_exec_builtin, true);
240+
expect_assert_failure(get_user_pass_cr(&up, "\nipassword\n", "UT", flags, NULL));
241+
}
242+
#endif
243+
215244
static void
216245
test_get_user_pass_authfile_stdin(void **state)
217246
{
@@ -239,8 +268,39 @@ test_get_user_pass_authfile_stdin(void **state)
239268
assert_true(up.defined);
240269
assert_string_equal(up.username, "user");
241270
assert_string_equal(up.password, "cpassword");
271+
272+
reset_user_pass(&up);
273+
274+
flags |= GET_USER_PASS_PASSWORD_ONLY;
275+
expect_string(query_user_exec_builtin, query_user[i].prompt, "Enter UT Password:");
276+
will_return(query_user_exec_builtin, "");
277+
will_return(query_user_exec_builtin, true);
278+
/*FIXME? does not error out on empty password */
279+
assert_true(get_user_pass_cr(&up, "stdin", "UT", flags, NULL));
280+
assert_true(up.defined);
281+
assert_string_equal(up.username, "user");
282+
assert_string_equal(up.password, "");
242283
}
243284

285+
/* NOTE: expect_assert_failure does not seem to work with MSVC */
286+
#ifndef _MSC_VER
287+
/* NOTE: leaks gc memory */
288+
static void
289+
test_get_user_pass_authfile_stdin_assertions(void **state)
290+
{
291+
struct user_pass up = { 0 };
292+
reset_user_pass(&up);
293+
unsigned int flags = 0;
294+
295+
expect_string(query_user_exec_builtin, query_user[i].prompt, "Enter UT Username:");
296+
expect_string(query_user_exec_builtin, query_user[i].prompt, "Enter UT Password:");
297+
will_return(query_user_exec_builtin, "");
298+
will_return(query_user_exec_builtin, "cpassword");
299+
will_return(query_user_exec_builtin, true);
300+
expect_assert_failure(get_user_pass_cr(&up, "stdin", "UT", flags, NULL));
301+
}
302+
#endif
303+
244304
static void
245305
test_get_user_pass_authfile_file(void **state)
246306
{
@@ -260,6 +320,17 @@ test_get_user_pass_authfile_file(void **state)
260320

261321
reset_user_pass(&up);
262322

323+
openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/appears_empty.txt");
324+
/*FIXME: query_user_exec() called even though nothing queued */
325+
will_return(query_user_exec_builtin, true);
326+
/*FIXME? does not error out */
327+
assert_true(get_user_pass_cr(&up, authfile, "UT", flags, NULL));
328+
assert_true(up.defined);
329+
assert_string_equal(up.username, "");
330+
assert_string_equal(up.password, "");
331+
332+
reset_user_pass(&up);
333+
263334
openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/user_only.txt");
264335
expect_string(query_user_exec_builtin, query_user[i].prompt, "Enter UT Password:");
265336
will_return(query_user_exec_builtin, "cpassword");
@@ -361,6 +432,29 @@ test_get_user_pass_static_challenge(void **state)
361432
}
362433
#endif /* ENABLE_MANAGEMENT */
363434

435+
/* NOTE: expect_assert_failure does not seem to work with MSVC */
436+
#ifndef _MSC_VER
437+
/* NOTE: leaks gc memory */
438+
static void
439+
test_get_user_pass_authfile_file_assertions(void **state)
440+
{
441+
struct user_pass up = { 0 };
442+
reset_user_pass(&up);
443+
unsigned int flags = 0;
444+
445+
char authfile[PATH_MAX] = { 0 };
446+
447+
openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/empty.txt");
448+
expect_assert_failure(get_user_pass_cr(&up, authfile, "UT", flags, NULL));
449+
450+
reset_user_pass(&up);
451+
452+
flags |= GET_USER_PASS_PASSWORD_ONLY;
453+
openvpn_test_get_srcdir_dir(authfile, PATH_MAX, "input/empty.txt");
454+
expect_assert_failure(get_user_pass_cr(&up, authfile, "UT", flags, NULL));
455+
}
456+
#endif /* ifndef _MSC_VER */
457+
364458
const struct CMUnitTest user_pass_tests[] = {
365459
cmocka_unit_test(test_get_user_pass_defined),
366460
cmocka_unit_test(test_get_user_pass_needok),
@@ -371,6 +465,11 @@ const struct CMUnitTest user_pass_tests[] = {
371465
cmocka_unit_test(test_get_user_pass_dynamic_challenge),
372466
cmocka_unit_test(test_get_user_pass_static_challenge),
373467
#endif /* ENABLE_MANAGEMENT */
468+
#ifndef _MSC_VER
469+
cmocka_unit_test(test_get_user_pass_inline_creds_assertions),
470+
cmocka_unit_test(test_get_user_pass_authfile_stdin_assertions),
471+
cmocka_unit_test(test_get_user_pass_authfile_file_assertions),
472+
#endif
374473
};
375474

376475
int

0 commit comments

Comments
 (0)