]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-uid-range.c
udev-builtin-blkid: Use _cleanup_blkid_free_probe_ to free probe (#6108)
[thirdparty/systemd.git] / src / test / test-uid-range.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <stddef.h>
21
22 #include "alloc-util.h"
23 #include "uid-range.h"
24 #include "user-util.h"
25 #include "util.h"
26
27 int main(int argc, char *argv[]) {
28 _cleanup_free_ UidRange *p = NULL;
29 unsigned n = 0;
30 uid_t search;
31
32 assert_se(uid_range_add_str(&p, &n, "500-999") >= 0);
33 assert_se(n == 1);
34 assert_se(p[0].start == 500);
35 assert_se(p[0].nr == 500);
36
37 assert_se(!uid_range_contains(p, n, 499));
38 assert_se(uid_range_contains(p, n, 500));
39 assert_se(uid_range_contains(p, n, 999));
40 assert_se(!uid_range_contains(p, n, 1000));
41
42 search = UID_INVALID;
43 assert_se(uid_range_next_lower(p, n, &search));
44 assert_se(search == 999);
45 assert_se(uid_range_next_lower(p, n, &search));
46 assert_se(search == 998);
47 search = 501;
48 assert_se(uid_range_next_lower(p, n, &search));
49 assert_se(search == 500);
50 assert_se(uid_range_next_lower(p, n, &search) == -EBUSY);
51
52 assert_se(uid_range_add_str(&p, &n, "1000") >= 0);
53 assert_se(n == 1);
54 assert_se(p[0].start == 500);
55 assert_se(p[0].nr == 501);
56
57 assert_se(uid_range_add_str(&p, &n, "30-40") >= 0);
58 assert_se(n == 2);
59 assert_se(p[0].start == 30);
60 assert_se(p[0].nr == 11);
61 assert_se(p[1].start == 500);
62 assert_se(p[1].nr == 501);
63
64 assert_se(uid_range_add_str(&p, &n, "60-70") >= 0);
65 assert_se(n == 3);
66 assert_se(p[0].start == 30);
67 assert_se(p[0].nr == 11);
68 assert_se(p[1].start == 60);
69 assert_se(p[1].nr == 11);
70 assert_se(p[2].start == 500);
71 assert_se(p[2].nr == 501);
72
73 assert_se(uid_range_add_str(&p, &n, "20-2000") >= 0);
74 assert_se(n == 1);
75 assert_se(p[0].start == 20);
76 assert_se(p[0].nr == 1981);
77
78 assert_se(uid_range_add_str(&p, &n, "2002") >= 0);
79 assert_se(n == 2);
80 assert_se(p[0].start == 20);
81 assert_se(p[0].nr == 1981);
82 assert_se(p[1].start == 2002);
83 assert_se(p[1].nr == 1);
84
85 assert_se(uid_range_add_str(&p, &n, "2001") >= 0);
86 assert_se(n == 1);
87 assert_se(p[0].start == 20);
88 assert_se(p[0].nr == 1983);
89
90 return 0;
91 }