Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */ 2 : 3 : #include <stddef.h> 4 : 5 : #include "alloc-util.h" 6 : #include "uid-range.h" 7 : #include "user-util.h" 8 : #include "util.h" 9 : 10 1 : int main(int argc, char *argv[]) { 11 2 : _cleanup_free_ UidRange *p = NULL; 12 1 : unsigned n = 0; 13 : uid_t search; 14 : 15 1 : assert_se(uid_range_add_str(&p, &n, "500-999") >= 0); 16 1 : assert_se(n == 1); 17 1 : assert_se(p[0].start == 500); 18 1 : assert_se(p[0].nr == 500); 19 : 20 1 : assert_se(!uid_range_contains(p, n, 499)); 21 1 : assert_se(uid_range_contains(p, n, 500)); 22 1 : assert_se(uid_range_contains(p, n, 999)); 23 1 : assert_se(!uid_range_contains(p, n, 1000)); 24 : 25 1 : search = UID_INVALID; 26 1 : assert_se(uid_range_next_lower(p, n, &search)); 27 1 : assert_se(search == 999); 28 1 : assert_se(uid_range_next_lower(p, n, &search)); 29 1 : assert_se(search == 998); 30 1 : search = 501; 31 1 : assert_se(uid_range_next_lower(p, n, &search)); 32 1 : assert_se(search == 500); 33 1 : assert_se(uid_range_next_lower(p, n, &search) == -EBUSY); 34 : 35 1 : assert_se(uid_range_add_str(&p, &n, "1000") >= 0); 36 1 : assert_se(n == 1); 37 1 : assert_se(p[0].start == 500); 38 1 : assert_se(p[0].nr == 501); 39 : 40 1 : assert_se(uid_range_add_str(&p, &n, "30-40") >= 0); 41 1 : assert_se(n == 2); 42 1 : assert_se(p[0].start == 30); 43 1 : assert_se(p[0].nr == 11); 44 1 : assert_se(p[1].start == 500); 45 1 : assert_se(p[1].nr == 501); 46 : 47 1 : assert_se(uid_range_add_str(&p, &n, "60-70") >= 0); 48 1 : assert_se(n == 3); 49 1 : assert_se(p[0].start == 30); 50 1 : assert_se(p[0].nr == 11); 51 1 : assert_se(p[1].start == 60); 52 1 : assert_se(p[1].nr == 11); 53 1 : assert_se(p[2].start == 500); 54 1 : assert_se(p[2].nr == 501); 55 : 56 1 : assert_se(uid_range_add_str(&p, &n, "20-2000") >= 0); 57 1 : assert_se(n == 1); 58 1 : assert_se(p[0].start == 20); 59 1 : assert_se(p[0].nr == 1981); 60 : 61 1 : assert_se(uid_range_add_str(&p, &n, "2002") >= 0); 62 1 : assert_se(n == 2); 63 1 : assert_se(p[0].start == 20); 64 1 : assert_se(p[0].nr == 1981); 65 1 : assert_se(p[1].start == 2002); 66 1 : assert_se(p[1].nr == 1); 67 : 68 1 : assert_se(uid_range_add_str(&p, &n, "2001") >= 0); 69 1 : assert_se(n == 1); 70 1 : assert_se(p[0].start == 20); 71 1 : assert_se(p[0].nr == 1983); 72 : 73 1 : return 0; 74 : }