valeriyvdovin / rpms / systemd

Forked from rpms/systemd 4 years ago
Clone

Blame SOURCES/0464-test-add-test-for-capability-bounding-set-parsing.patch

a19bc6
From cac429e0a75667c021782210045c8e365f5cc8b0 Mon Sep 17 00:00:00 2001
a19bc6
From: Evgeny Vereshchagin <evvers@ya.ru>
a19bc6
Date: Thu, 29 Oct 2015 14:12:22 +0300
a19bc6
Subject: [PATCH] test: add test for capability bounding set parsing
a19bc6
a19bc6
Cherry-picked from: a8107a54
a19bc6
Resolves: #1387398
a19bc6
---
a19bc6
 src/test/test-unit-file.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
a19bc6
 1 file changed, 45 insertions(+)
a19bc6
a19bc6
diff --git a/src/test/test-unit-file.c b/src/test/test-unit-file.c
a19bc6
index 0384305..0f00a8f 100644
a19bc6
--- a/src/test/test-unit-file.c
a19bc6
+++ b/src/test/test-unit-file.c
a19bc6
@@ -24,6 +24,7 @@
a19bc6
 #include <stdio.h>
a19bc6
 #include <stddef.h>
a19bc6
 #include <string.h>
a19bc6
+#include <sys/capability.h>
a19bc6
 #include <unistd.h>
a19bc6
 #include <fcntl.h>
a19bc6
 
a19bc6
@@ -545,6 +546,9 @@ static void test_install_printf(void) {
a19bc6
         expect(i4, "%U", "0");
a19bc6
 }
a19bc6
 
a19bc6
+static uint64_t make_cap(int cap) {
a19bc6
+        return ((uint64_t) 1ULL << (uint64_t) cap);
a19bc6
+}
a19bc6
 
a19bc6
 static void test_config_parse_rlimit(void) {
a19bc6
         struct rlimit * rl[_RLIMIT_MAX] = {};
a19bc6
@@ -661,6 +665,46 @@ static void test_config_parse_rlimit(void) {
a19bc6
         free(rl[RLIMIT_RTTIME]);
a19bc6
 }
a19bc6
 
a19bc6
+static void test_config_parse_bounding_set(void) {
a19bc6
+        /* int config_parse_bounding_set(
a19bc6
+                 const char *unit,
a19bc6
+                 const char *filename,
a19bc6
+                 unsigned line,
a19bc6
+                 const char *section,
a19bc6
+                 unsigned section_line,
a19bc6
+                 const char *lvalue,
a19bc6
+                 int ltype,
a19bc6
+                 const char *rvalue,
a19bc6
+                 void *data,
a19bc6
+                 void *userdata) */
a19bc6
+        int r;
a19bc6
+        uint64_t capability_bounding_set_drop = 0;
a19bc6
+
a19bc6
+        r = config_parse_bounding_set(NULL, "fake", 1, "section", 1,
a19bc6
+                              "CapabilityBoundingSet", 0, "CAP_NET_RAW",
a19bc6
+                              &capability_bounding_set_drop, NULL);
a19bc6
+        assert_se(r >= 0);
a19bc6
+        assert_se(capability_bounding_set_drop == ~make_cap(CAP_NET_RAW));
a19bc6
+
a19bc6
+        r = config_parse_bounding_set(NULL, "fake", 1, "section", 1,
a19bc6
+                              "CapabilityBoundingSet", 0, "CAP_NET_ADMIN",
a19bc6
+                              &capability_bounding_set_drop, NULL);
a19bc6
+        assert_se(r >= 0);
a19bc6
+        assert_se(capability_bounding_set_drop == ~(make_cap(CAP_NET_RAW) | make_cap(CAP_NET_ADMIN)));
a19bc6
+
a19bc6
+        r = config_parse_bounding_set(NULL, "fake", 1, "section", 1,
a19bc6
+                              "CapabilityBoundingSet", 0, "",
a19bc6
+                              &capability_bounding_set_drop, NULL);
a19bc6
+        assert_se(r >= 0);
a19bc6
+        assert_se(capability_bounding_set_drop == ~((uint64_t) 0ULL));
a19bc6
+
a19bc6
+        r = config_parse_bounding_set(NULL, "fake", 1, "section", 1,
a19bc6
+                              "CapabilityBoundingSet", 0, "~",
a19bc6
+                              &capability_bounding_set_drop, NULL);
a19bc6
+        assert_se(r >= 0);
a19bc6
+        assert_se(capability_bounding_set_drop == (uint64_t) 0ULL);
a19bc6
+}
a19bc6
+
a19bc6
 int main(int argc, char *argv[]) {
a19bc6
         int r;
a19bc6
 
a19bc6
@@ -670,6 +714,7 @@ int main(int argc, char *argv[]) {
a19bc6
         r = test_unit_file_get_set();
a19bc6
         test_config_parse_exec();
a19bc6
         test_config_parse_rlimit();
a19bc6
+        test_config_parse_bounding_set();
a19bc6
         test_load_env_file_1();
a19bc6
         test_load_env_file_2();
a19bc6
         test_load_env_file_3();