]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: Add tests for gid list ops 11199/head
authorDariusz Gadomski <dgadomski@gmail.com>
Wed, 8 Jan 2020 15:25:15 +0000 (16:25 +0100)
committerDariusz Gadomski <dgadomski@gmail.com>
Mon, 13 Jan 2020 09:29:20 +0000 (10:29 +0100)
src/test/test-user-util.c

index 47baacb518a19e94410123972ff17662eb355469..fde8336bf9b7a094773d10ec187691f9186d96a1 100644 (file)
@@ -4,6 +4,7 @@
 #include "format-util.h"
 #include "log.h"
 #include "macro.h"
+#include "memory-util.h"
 #include "path-util.h"
 #include "string-util.h"
 #include "user-util.h"
@@ -287,12 +288,41 @@ static void test_make_salt(void) {
 }
 
 static void test_in_gid(void) {
-
         assert(in_gid(getgid()) >= 0);
-        assert(in_gid(getegid()) >= 0);
+        assert(in_gid(getegid()) >= 0);        assert(in_gid(TTY_GID) == 0); /* The TTY gid is for owning ttys, it would be really really weird if we were in it. */
+}
+
+static void test_gid_lists_ops(void) {
+        static const gid_t l1[] = { 5, 10, 15, 20, 25};
+        static const gid_t l2[] = { 1, 2, 3, 15, 20, 25};
+        static const gid_t l3[] = { 5, 10, 15, 20, 25, 26, 27};
+        static const gid_t l4[] = { 25, 26, 20, 15, 5, 27, 10};
+
+        static const gid_t result1[] = {1, 2, 3, 5, 10, 15, 20, 25, 26, 27};
+        static const gid_t result2[] = {5, 10, 15, 20, 25, 26, 27};
+
+        _cleanup_free_ gid_t *gids = NULL;
+        _cleanup_free_ gid_t *res1 = NULL;
+        _cleanup_free_ gid_t *res2 = NULL;
+        _cleanup_free_ gid_t *res3 = NULL;
+        _cleanup_free_ gid_t *res4 = NULL;
+        int nresult;
+
+        nresult = merge_gid_lists(l2, ELEMENTSOF(l2), l3, ELEMENTSOF(l3), &res1);
+        assert_se(memcmp_nn(res1, nresult, result1, ELEMENTSOF(result1)) == 0);
+
+        nresult = merge_gid_lists(NULL, 0, l2, ELEMENTSOF(l2), &res2);
+        assert_se(memcmp_nn(res2, nresult, l2, ELEMENTSOF(l2)) == 0);
+
+        nresult = merge_gid_lists(l1, ELEMENTSOF(l1), l1, ELEMENTSOF(l1), &res3);
+        assert_se(memcmp_nn(l1, ELEMENTSOF(l1), res3, nresult) == 0);
+
+        nresult = merge_gid_lists(l1, ELEMENTSOF(l1), l4, ELEMENTSOF(l4), &res4);
+        assert_se(memcmp_nn(result2, ELEMENTSOF(result2), res4, nresult) == 0);
 
-        assert(in_gid(GID_INVALID) < 0);
-        assert(in_gid(TTY_GID) == 0); /* The TTY gid is for owning ttys, it would be really really weird if we were in it. */
+        nresult = getgroups_alloc(&gids);
+        assert_se(nresult >= 0 || nresult == -EINVAL || nresult == -ENOMEM);
+        assert_se(gids);
 }
 
 int main(int argc, char *argv[]) {
@@ -330,6 +360,7 @@ int main(int argc, char *argv[]) {
         test_make_salt();
 
         test_in_gid();
+        test_gid_lists_ops();
 
         return 0;
 }