valeriyvdovin / rpms / systemd

Forked from rpms/systemd 4 years ago
Clone

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

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