Debian Codegraph @

src/test/test-user-record.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-json.h"
4
5 #include "tests.h"
6 #include "user-record.h"
7
8 #define USER(ret, ...) \
9 ({ \
10 typeof(ret) _r = (ret); \
11 user_record_unref(*_r); \
12 ASSERT_OK(user_record_build((ret), SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR_STRING("disposition", "regular"), __VA_ARGS__))); \
13 0; \
14 })
15
16 TEST(self_changes) {
17 _cleanup_(user_record_unrefp) UserRecord *curr = NULL, *new = NULL;
18
19 /* not allowlisted */
20 USER(&curr,
21 SD_JSON_BUILD_PAIR_STRING("userName", "test"),
22 SD_JSON_BUILD_PAIR_UNSIGNED("notInHardCodedList", 11111));
23 USER(&new,
24 SD_JSON_BUILD_PAIR_STRING("userName", "test"),
25 SD_JSON_BUILD_PAIR_UNSIGNED("notInHardCodedList", 99999));
26 ASSERT_FALSE(user_record_self_changes_allowed(curr, new));
27
28 /* manually allowlisted */
29 USER(&curr,
30 SD_JSON_BUILD_PAIR_STRING("userName", "test"),
31 SD_JSON_BUILD_PAIR_UNSIGNED("notInHardCodedList", 11111),
32 SD_JSON_BUILD_PAIR_ARRAY("selfModifiableFields", SD_JSON_BUILD_STRING("notInHardCodedList")));
33 USER(&new,
34 SD_JSON_BUILD_PAIR_STRING("userName", "test"),
35 SD_JSON_BUILD_PAIR_ARRAY("selfModifiableFields", SD_JSON_BUILD_STRING("notInHardCodedList")),
36 /* change in order shouldn't affect things */
37 SD_JSON_BUILD_PAIR_UNSIGNED("notInHardCodedList", 99999));
38 ASSERT_TRUE(user_record_self_changes_allowed(curr, new));
39
40 /* default allowlisted */
41 USER(&curr,
42 SD_JSON_BUILD_PAIR_STRING("userName", "test"),
43 SD_JSON_BUILD_PAIR_STRING("realName", "Old Name"));
44 USER(&new,
45 SD_JSON_BUILD_PAIR_STRING("userName", "test"),
46 SD_JSON_BUILD_PAIR_STRING("realName", "New Name"));
47 ASSERT_TRUE(user_record_self_changes_allowed(curr, new));
48
49 /* introduced new default allowlisted */
50 USER(&curr,
51 SD_JSON_BUILD_PAIR_STRING("userName", "test"));
52 USER(&new,
53 SD_JSON_BUILD_PAIR_STRING("userName", "test"),
54 SD_JSON_BUILD_PAIR_STRING("realName", "New Name"));
55 ASSERT_TRUE(user_record_self_changes_allowed(curr, new));
56
57 /* introduced new not allowlisted */
58 USER(&curr,
59 SD_JSON_BUILD_PAIR_STRING("userName", "test"));
60 USER(&new,
61 SD_JSON_BUILD_PAIR_STRING("userName", "test"),
62 SD_JSON_BUILD_PAIR_UNSIGNED("notInHardCodedList", 99999));
63 ASSERT_FALSE(user_record_self_changes_allowed(curr, new));
64
65 /* privileged section: default allowlisted */
66 USER(&curr,
67 SD_JSON_BUILD_PAIR_STRING("userName", "test"),
68 SD_JSON_BUILD_PAIR_OBJECT("privileged",
69 SD_JSON_BUILD_PAIR_STRING("passwordHint", "Old Hint")));
70 USER(&new,
71 SD_JSON_BUILD_PAIR_STRING("userName", "test"),
72 SD_JSON_BUILD_PAIR_OBJECT("privileged",
73 SD_JSON_BUILD_PAIR_STRING("passwordHint", "New Hint")));
74 ASSERT_TRUE(user_record_self_changes_allowed(curr, new));
75
76 /* privileged section: not allowlisted */
77 USER(&curr,
78 SD_JSON_BUILD_PAIR_STRING("userName", "test"),
79 SD_JSON_BUILD_PAIR_OBJECT("privileged",
80 SD_JSON_BUILD_PAIR_UNSIGNED("notInHardCodedList", 11111)));
81 USER(&new,
82 SD_JSON_BUILD_PAIR_STRING("userName", "test"),
83 SD_JSON_BUILD_PAIR_OBJECT("privileged",
84 SD_JSON_BUILD_PAIR_UNSIGNED("notInHardCodedList", 99999)));
85 ASSERT_FALSE(user_record_self_changes_allowed(curr, new));
86
87 /* privileged section: manually allowlisted */
88 USER(&curr,
89 SD_JSON_BUILD_PAIR_STRING("userName", "test"),
90 SD_JSON_BUILD_PAIR_ARRAY("selfModifiablePrivileged", SD_JSON_BUILD_STRING("notInHardCodedList")),
91 SD_JSON_BUILD_PAIR_OBJECT("privileged",
92 SD_JSON_BUILD_PAIR_UNSIGNED("notInHardCodedList", 11111)));
93 USER(&new,
94 SD_JSON_BUILD_PAIR_STRING("userName", "test"),
95 SD_JSON_BUILD_PAIR_ARRAY("selfModifiablePrivileged", SD_JSON_BUILD_STRING("notInHardCodedList")),
96 SD_JSON_BUILD_PAIR_OBJECT("privileged",
97 SD_JSON_BUILD_PAIR_UNSIGNED("notInHardCodedList", 99999)));
98 ASSERT_TRUE(user_record_self_changes_allowed(curr, new));
99
100 /* birthDate is NOT self-modifiable (admin-only) */
101 USER(&curr,
102 SD_JSON_BUILD_PAIR_STRING("userName", "test"),
103 SD_JSON_BUILD_PAIR_STRING("birthDate", "1990-01-01"));
104 USER(&new,
105 SD_JSON_BUILD_PAIR_STRING("userName", "test"),
106 SD_JSON_BUILD_PAIR_STRING("birthDate", "1990-06-15"));
107 ASSERT_FALSE(user_record_self_changes_allowed(curr, new));
108 }
109
110 DEFINE_TEST_MAIN(LOG_INFO);