|
3 | 3 | #include <unistd.h> |
4 | 4 | #include <sys/types.h> |
5 | 5 |
|
| 6 | +#include "fd-util.h" |
| 7 | +#include "fileio.h" |
6 | 8 | #include "format-util.h" |
| 9 | +#include "fs-util.h" |
| 10 | +#include "tmpfile-util.h" |
7 | 11 | #include "tests.h" |
8 | 12 | #include "user-record.h" |
9 | 13 |
|
| 14 | +static void test_read_login_defs(const char *path) { |
| 15 | + log_info("/* %s(\"%s\") */", __func__, path ?: "<custom>"); |
| 16 | + |
| 17 | + _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-user-record.XXXXXX"; |
| 18 | + _cleanup_fclose_ FILE *f = NULL; |
| 19 | + if (!path) { |
| 20 | + assert_se(fmkostemp_safe(name, "r+", &f) == 0); |
| 21 | + fprintf(f, |
| 22 | + "SYS_UID_MIN "UID_FMT"\n" |
| 23 | + "SYS_UID_MAX "UID_FMT"\n" |
| 24 | + "SYS_GID_MIN "GID_FMT"\n" |
| 25 | + "SYS_GID_MAX "GID_FMT"\n", |
| 26 | + SYSTEM_ALLOC_UID_MIN + 5, |
| 27 | + SYSTEM_UID_MAX + 5, |
| 28 | + SYSTEM_ALLOC_GID_MIN + 5, |
| 29 | + SYSTEM_GID_MAX + 5); |
| 30 | + assert_se(fflush_and_check(f) >= 0); |
| 31 | + } |
| 32 | + |
| 33 | + UGIDAllocationRange defs; |
| 34 | + assert_se(read_login_defs(&defs, path ?: name, NULL) >= 0); |
| 35 | + |
| 36 | + log_info("system_alloc_uid_min="UID_FMT, defs.system_alloc_uid_min); |
| 37 | + log_info("system_uid_max="UID_FMT, defs.system_uid_max); |
| 38 | + log_info("system_alloc_gid_min="GID_FMT, defs.system_alloc_gid_min); |
| 39 | + log_info("system_gid_max="GID_FMT, defs.system_gid_max); |
| 40 | + |
| 41 | + if (!path) { |
| 42 | + uid_t offset = ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES ? 5 : 0; |
| 43 | + assert_se(defs.system_alloc_uid_min == SYSTEM_ALLOC_UID_MIN + offset); |
| 44 | + assert_se(defs.system_uid_max == SYSTEM_UID_MAX + offset); |
| 45 | + assert_se(defs.system_alloc_gid_min == SYSTEM_ALLOC_GID_MIN + offset); |
| 46 | + assert_se(defs.system_gid_max == SYSTEM_GID_MAX + offset); |
| 47 | + } else if (streq(path, "/dev/null")) { |
| 48 | + assert_se(defs.system_alloc_uid_min == SYSTEM_ALLOC_UID_MIN); |
| 49 | + assert_se(defs.system_uid_max == SYSTEM_UID_MAX); |
| 50 | + assert_se(defs.system_alloc_gid_min == SYSTEM_ALLOC_GID_MIN); |
| 51 | + assert_se(defs.system_gid_max == SYSTEM_GID_MAX); |
| 52 | + } |
| 53 | +} |
| 54 | + |
10 | 55 | static void test_acquire_ugid_allocation_range(void) { |
11 | 56 | log_info("/* %s */", __func__); |
12 | 57 |
|
@@ -48,6 +93,9 @@ static void test_gid_is_system(void) { |
48 | 93 | int main(int argc, char *argv[]) { |
49 | 94 | test_setup_logging(LOG_DEBUG); |
50 | 95 |
|
| 96 | + test_read_login_defs("/dev/null"); |
| 97 | + test_read_login_defs("/etc/login.defs"); |
| 98 | + test_read_login_defs(NULL); |
51 | 99 | test_acquire_ugid_allocation_range(); |
52 | 100 | test_uid_is_system(); |
53 | 101 | test_gid_is_system(); |
|
0 commit comments