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