e96caa6a044ade2b629b8de51920f8a520ec12ce
[uid_wrapper.git] / tests / test_uid.c
1 #include "config.h"
2
3 #include <stdarg.h>
4 #include <stddef.h>
5 #include <setjmp.h>
6 #include <cmocka.h>
7
8 #include <sys/types.h>
9 #include <unistd.h>
10
11 #include <pwd.h>
12
13 static void test_uwrap_seteuid(void **state)
14 {
15         int rc;
16         uid_t u;
17
18         (void) state; /* unused */
19
20         rc = seteuid(-1);
21         assert_int_equal(rc, -1);
22
23         rc = seteuid(0);
24         assert_int_equal(rc, 0);
25
26         u = geteuid();
27         assert_int_equal(u, 0);
28
29         rc = setuid(42);
30         assert_int_equal(rc, 0);
31
32         u = getuid();
33         assert_int_equal(u, 42);
34 }
35
36 #ifdef HAVE_SETREUID
37 static void test_uwrap_setreuid(void **state)
38 {
39         int rc;
40         uid_t u;
41
42         (void) state; /* unused */
43
44         rc = setreuid(1, 2);
45         assert_int_equal(rc, 0);
46
47         u = getuid();
48         assert_int_equal(u, 1);
49
50         u = geteuid();
51         assert_int_equal(u, 2);
52 }
53 #endif
54
55 #ifdef HAVE_SETRESUID
56 static void test_uwrap_setresuid(void **state)
57 {
58         int rc;
59         uid_t u;
60
61         (void) state; /* unused */
62
63         rc = setresuid(1, 2, -1);
64         assert_int_equal(rc, 0);
65
66         u = getuid();
67         assert_int_equal(u, 1);
68
69         u = geteuid();
70         assert_int_equal(u, 2);
71 }
72 #endif
73
74 #ifdef HAVE_GETRESUID
75 static void test_uwrap_getresuid(void **state)
76 {
77         int rc;
78         uid_t ru, eu, su;
79
80         (void) state; /* unused */
81
82         rc = setresuid(1, 2, -1);
83         assert_int_equal(rc, 0);
84
85         ru = getuid();
86         assert_int_equal(ru, 1);
87
88         eu = geteuid();
89         assert_int_equal(eu, 2);
90
91         rc = getresuid(&ru, &eu, &su);
92         assert_int_equal(ru, 1);
93         assert_int_equal(eu, 2);
94 }
95 #endif
96
97 int main(void) {
98         int rc;
99
100         const struct CMUnitTest uwrap_tests[] = {
101                 cmocka_unit_test(test_uwrap_seteuid),
102 #ifdef HAVE_SETREUID
103                 cmocka_unit_test(test_uwrap_setreuid),
104 #endif
105 #ifdef HAVE_SETRESUID
106                 cmocka_unit_test(test_uwrap_setresuid),
107 #endif
108 #ifdef HAVE_GETRESUID
109                 cmocka_unit_test(test_uwrap_getresuid),
110 #endif
111         };
112
113         rc = cmocka_run_group_tests(uwrap_tests, NULL, NULL);
114
115         return rc;
116 }