Debian Codegraph @

tests/test_shadow.c

raw
1 #include "config.h"
2
3 #include <stdarg.h>
4 #include <stddef.h>
5 #include <setjmp.h>
6 #include <cmocka.h>
7
8 #include <shadow.h>
9 #include <stdint.h>
10 #include <unistd.h>
11
12 #include <crypt.h>
13
14 static void test_nwrap_getspent(void **state)
15 {
16 struct spwd *sp;
17 uint32_t i;
18
19 (void)state; /* unused */
20
21 setspent();
22
23 for (sp = getspent(), i = 0; sp != NULL; sp = getspent(), i++) {
24 if (i == 0) {
25 assert_string_equal(sp->sp_namp, "alice");
26 } else {
27 assert_string_equal(sp->sp_namp, "bob");
28 }
29 }
30
31 endspent();
32 }
33
34 static void test_nwrap_getspnam(void **state)
35 {
36 char *encrypted_password;
37 struct spwd *sp;
38
39 (void)state; /* unused */
40
41 sp = getspnam("alice");
42 assert_non_null(sp);
43
44 assert_string_equal(sp->sp_namp, "alice");
45
46 encrypted_password = crypt("secret", sp->sp_pwdp);
47 assert_non_null(encrypted_password);
48
49 assert_string_equal(encrypted_password, sp->sp_pwdp);
50
51 sp = getspnam("bob");
52 assert_non_null(sp);
53
54 assert_string_equal(sp->sp_namp, "bob");
55
56 encrypted_password = crypt("secret", sp->sp_pwdp);
57 assert_non_null(encrypted_password);
58
59 assert_string_equal(encrypted_password, sp->sp_pwdp);
60 }
61
62 int main(void) {
63 int rc;
64
65 const struct CMUnitTest tests[] = {
66 cmocka_unit_test(test_nwrap_getspent),
67 cmocka_unit_test(test_nwrap_getspnam),
68 };
69
70 rc = cmocka_run_group_tests(tests, NULL, NULL);
71
72 return rc;
73 }