test-util: move RUN_UNPRIVILEGED definitions to kdbus-util.h
authorDjalal Harouni <tixxdz@opendz.org>
Mon, 13 Oct 2014 22:27:07 +0000 (23:27 +0100)
committerDjalal Harouni <tixxdz@opendz.org>
Tue, 14 Oct 2014 19:50:26 +0000 (20:50 +0100)
Will be used by other tests

Signed-off-by: Djalal Harouni <tixxdz@opendz.org>
test/kdbus-util.h
test/test-policy-priv.c

index ac1af23e30b910cec881034207313e8834bf20fd..9e48e72d2d8d2881f3b2d97eec8ac68ae0bc8239 100644 (file)
@@ -32,6 +32,9 @@
 
 #define POOL_SIZE (16 * 1024LU * 1024LU)
 
+#define UNPRIV_UID 65534
+#define UNPRIV_GID 65534
+
 /* Dump as user of process, useful for user namespace testing */
 #define SUID_DUMP_USER 1
 
@@ -41,6 +44,48 @@ extern int kdbus_util_verbose;
        if (kdbus_util_verbose) \
                printf(X)
 
+#define RUN_UNPRIVILEGED(child_uid, child_gid, _child_, _parent_) ({   \
+               pid_t pid, rpid;                                        \
+               int ret;                                                \
+                                                                       \
+               pid = fork();                                           \
+               if (pid == 0) {                                         \
+                       ret = drop_privileges(child_uid, child_gid);    \
+                       if (ret < 0)                                    \
+                               _exit(ret);                             \
+                                                                       \
+                       _child_;                                        \
+                       _exit(0);                                       \
+               } else if (pid > 0) {                                   \
+                       _parent_;                                       \
+                       rpid = waitpid(pid, &ret, 0);                   \
+                       ASSERT_RETURN(rpid == pid);                     \
+                       ASSERT_RETURN(WIFEXITED(ret));                  \
+                       ASSERT_RETURN(WEXITSTATUS(ret) == 0);           \
+                       ret = TEST_OK;                                  \
+               } else {                                                \
+                       ret = pid;                                      \
+               }                                                       \
+                                                                       \
+               ret;                                                    \
+       })
+
+#define RUN_UNPRIVILEGED_CONN(_var_, _bus_, _code_)                    \
+       RUN_UNPRIVILEGED(UNPRIV_UID, UNPRIV_GID, ({                     \
+               struct kdbus_conn *_var_;                               \
+               _var_ = kdbus_hello(_bus_, 0, NULL, 0);                 \
+               ASSERT_EXIT(_var_);                                     \
+               _code_;                                                 \
+               kdbus_conn_free(_var_);                                 \
+       }), ({ 0; }))
+
+/* Enums for parent if it should drop privs or not */
+enum kdbus_drop_parent {
+       DO_NOT_DROP,
+       DROP_SAME_UNPRIV,
+       DROP_OTHER_UNPRIV,
+};
+
 struct kdbus_conn {
        int fd;
        uint64_t id;
index 92a2347c30d216a171301f25707fde3a90a8f34d..cf73f72909997300824c4b95ef61b2f152712e94 100644 (file)
 #include "kdbus-util.h"
 #include "kdbus-enum.h"
 
-#define UNPRIV_UID 65534
-#define UNPRIV_GID 65534
-
-enum kdbus_drop_user {
-       DO_NOT_DROP,
-       DROP_SAME_UNPRIV_USER,
-       DROP_OTHER_UNPRIV_USER,
-};
-
-#define RUN_UNPRIVILEGED(child_uid, child_gid, _child_, _parent_) ({   \
-               pid_t pid, rpid;                                        \
-               int ret;                                                \
-                                                                       \
-               pid = fork();                                           \
-               if (pid == 0) {                                         \
-                       ret = drop_privileges(child_uid, child_gid);    \
-                       if (ret < 0)                                    \
-                               _exit(ret);                             \
-                                                                       \
-                       _child_;                                        \
-                       _exit(0);                                       \
-               } else if (pid > 0) {                                   \
-                       _parent_;                                       \
-                       rpid = waitpid(pid, &ret, 0);                   \
-                       ASSERT_RETURN(rpid == pid);                     \
-                       ASSERT_RETURN(WIFEXITED(ret));                  \
-                       ASSERT_RETURN(WEXITSTATUS(ret) == 0);           \
-                       ret = TEST_OK;                                  \
-               } else {                                                \
-                       ret = pid;                                      \
-               }                                                       \
-                                                                       \
-               ret;                                                    \
-       })
-
-#define RUN_UNPRIVILEGED_CONN(_var_, _bus_, _code_)                    \
-       RUN_UNPRIVILEGED(UNPRIV_UID, UNPRIV_GID, ({                     \
-               struct kdbus_conn *_var_;                               \
-               _var_ = kdbus_hello(_bus_, 0, NULL, 0);                 \
-               ASSERT_EXIT(_var_);                                     \
-               _code_;                                                 \
-               kdbus_conn_free(_var_);                                 \
-       }), ({ 0; }))
-
 static int test_policy_priv_by_id(const char *bus,
                                  struct kdbus_conn *conn_dst,
                                  bool drop_second_user,
@@ -101,7 +57,7 @@ static int test_policy_priv_by_broadcast(const char *bus,
        uint64_t expected_cookie = time(NULL) ^ 0xdeadbeef;
 
        /* Drop to another unprivileged user other than UNPRIV_UID */
-       if (drop_second_user == DROP_OTHER_UNPRIV_USER) {
+       if (drop_second_user == DROP_OTHER_UNPRIV) {
                second_uid = UNPRIV_UID - 1;
                second_gid = UNPRIV_GID - 1;
        }
@@ -289,7 +245,7 @@ static int test_priv_before_policy_upload(struct kdbus_test_env *env)
         */
 
        ret = test_policy_priv_by_broadcast(env->buspath, NULL,
-                                           DROP_SAME_UNPRIV_USER,
+                                           DROP_SAME_UNPRIV,
                                            EXIT_SUCCESS, EXIT_SUCCESS);
        ASSERT_RETURN(ret == 0);
 
@@ -301,7 +257,7 @@ static int test_priv_before_policy_upload(struct kdbus_test_env *env)
         */
 
        ret = test_policy_priv_by_broadcast(env->buspath, NULL,
-                                           DROP_OTHER_UNPRIV_USER,
+                                           DROP_OTHER_UNPRIV,
                                            -ETIMEDOUT, -ETIMEDOUT);
        ASSERT_RETURN(ret == 0);
 
@@ -370,7 +326,7 @@ static int test_broadcast_after_policy_upload(struct kdbus_test_env *env)
         */
 
        ret = test_policy_priv_by_broadcast(env->buspath, NULL,
-                                           DROP_SAME_UNPRIV_USER,
+                                           DROP_SAME_UNPRIV,
                                            EXIT_SUCCESS, EXIT_SUCCESS);
        ASSERT_RETURN(ret == 0);
 
@@ -557,7 +513,7 @@ static int test_broadcast_after_policy_upload(struct kdbus_test_env *env)
         */
 
        ret = test_policy_priv_by_broadcast(env->buspath, NULL,
-                                           DROP_OTHER_UNPRIV_USER,
+                                           DROP_OTHER_UNPRIV,
                                            -ETIMEDOUT, -ETIMEDOUT);
        ASSERT_RETURN(ret == 0);