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