From c6cecb744b53561efd329309af7d02a3f9979ed1 Mon Sep 17 00:00:00 2001 From: Dariusz Gadomski Date: Wed, 8 Jan 2020 16:25:15 +0100 Subject: [PATCH] test: Add tests for gid list ops --- src/test/test-user-util.c | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/src/test/test-user-util.c b/src/test/test-user-util.c index 47baacb518a..fde8336bf9b 100644 --- a/src/test/test-user-util.c +++ b/src/test/test-user-util.c @@ -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; } -- 2.47.3