LCOV - code coverage report
Current view: top level - test - test-namespace.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 6 79 7.6 %
Date: 2019-08-22 15:41:25 Functions: 1 3 33.3 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <sys/socket.h>
       4             : #include <sys/stat.h>
       5             : 
       6             : #include "alloc-util.h"
       7             : #include "fd-util.h"
       8             : #include "namespace.h"
       9             : #include "process-util.h"
      10             : #include "string-util.h"
      11             : #include "tests.h"
      12             : #include "util.h"
      13             : 
      14           0 : static void test_tmpdir(const char *id, const char *A, const char *B) {
      15           0 :         _cleanup_free_ char *a, *b;
      16             :         struct stat x, y;
      17             :         char *c, *d;
      18             : 
      19           0 :         assert_se(setup_tmp_dirs(id, &a, &b) == 0);
      20           0 :         assert_se(startswith(a, A));
      21           0 :         assert_se(startswith(b, B));
      22             : 
      23           0 :         assert_se(stat(a, &x) >= 0);
      24           0 :         assert_se(stat(b, &y) >= 0);
      25             : 
      26           0 :         assert_se(S_ISDIR(x.st_mode));
      27           0 :         assert_se(S_ISDIR(y.st_mode));
      28             : 
      29           0 :         assert_se((x.st_mode & 01777) == 0700);
      30           0 :         assert_se((y.st_mode & 01777) == 0700);
      31             : 
      32           0 :         c = strjoina(a, "/tmp");
      33           0 :         d = strjoina(b, "/tmp");
      34             : 
      35           0 :         assert_se(stat(c, &x) >= 0);
      36           0 :         assert_se(stat(d, &y) >= 0);
      37             : 
      38           0 :         assert_se(S_ISDIR(x.st_mode));
      39           0 :         assert_se(S_ISDIR(y.st_mode));
      40             : 
      41           0 :         assert_se((x.st_mode & 01777) == 01777);
      42           0 :         assert_se((y.st_mode & 01777) == 01777);
      43             : 
      44           0 :         assert_se(rmdir(c) >= 0);
      45           0 :         assert_se(rmdir(d) >= 0);
      46             : 
      47           0 :         assert_se(rmdir(a) >= 0);
      48           0 :         assert_se(rmdir(b) >= 0);
      49           0 : }
      50             : 
      51           0 : static int test_netns(void) {
      52           0 :         _cleanup_close_pair_ int s[2] = { -1, -1 };
      53             :         pid_t pid1, pid2, pid3;
      54           0 :         int r, n = 0;
      55             :         siginfo_t si;
      56             : 
      57           0 :         if (geteuid() > 0)
      58           0 :                 return log_tests_skipped("not root");
      59             : 
      60           0 :         assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, s) >= 0);
      61             : 
      62           0 :         pid1 = fork();
      63           0 :         assert_se(pid1 >= 0);
      64             : 
      65           0 :         if (pid1 == 0) {
      66           0 :                 r = setup_netns(s);
      67           0 :                 assert_se(r >= 0);
      68           0 :                 _exit(r);
      69             :         }
      70             : 
      71           0 :         pid2 = fork();
      72           0 :         assert_se(pid2 >= 0);
      73             : 
      74           0 :         if (pid2 == 0) {
      75           0 :                 r = setup_netns(s);
      76           0 :                 assert_se(r >= 0);
      77           0 :                 exit(r);
      78             :         }
      79             : 
      80           0 :         pid3 = fork();
      81           0 :         assert_se(pid3 >= 0);
      82             : 
      83           0 :         if (pid3 == 0) {
      84           0 :                 r = setup_netns(s);
      85           0 :                 assert_se(r >= 0);
      86           0 :                 exit(r);
      87             :         }
      88             : 
      89           0 :         r = wait_for_terminate(pid1, &si);
      90           0 :         assert_se(r >= 0);
      91           0 :         assert_se(si.si_code == CLD_EXITED);
      92           0 :         n += si.si_status;
      93             : 
      94           0 :         r = wait_for_terminate(pid2, &si);
      95           0 :         assert_se(r >= 0);
      96           0 :         assert_se(si.si_code == CLD_EXITED);
      97           0 :         n += si.si_status;
      98             : 
      99           0 :         r = wait_for_terminate(pid3, &si);
     100           0 :         assert_se(r >= 0);
     101           0 :         assert_se(si.si_code == CLD_EXITED);
     102           0 :         n += si.si_status;
     103             : 
     104           0 :         assert_se(n == 1);
     105           0 :         return EXIT_SUCCESS;
     106             : }
     107             : 
     108           1 : int main(int argc, char *argv[]) {
     109             :         sd_id128_t bid;
     110             :         char boot_id[SD_ID128_STRING_MAX];
     111           1 :         _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *zz = NULL;
     112             : 
     113           1 :         test_setup_logging(LOG_INFO);
     114             : 
     115           1 :         if (!have_namespaces()) {
     116           1 :                 log_tests_skipped("Don't have namespace support");
     117           1 :                 return EXIT_TEST_SKIP;
     118             :         }
     119             : 
     120           0 :         assert_se(sd_id128_get_boot(&bid) >= 0);
     121           0 :         sd_id128_to_string(bid, boot_id);
     122             : 
     123           0 :         x = strjoin("/tmp/systemd-private-", boot_id, "-abcd.service-");
     124           0 :         y = strjoin("/var/tmp/systemd-private-", boot_id, "-abcd.service-");
     125           0 :         assert_se(x && y);
     126             : 
     127           0 :         test_tmpdir("abcd.service", x, y);
     128             : 
     129           0 :         z = strjoin("/tmp/systemd-private-", boot_id, "-sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device-");
     130           0 :         zz = strjoin("/var/tmp/systemd-private-", boot_id, "-sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device-");
     131             : 
     132           0 :         assert_se(z && zz);
     133             : 
     134           0 :         test_tmpdir("sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device", z, zz);
     135             : 
     136           0 :         return test_netns();
     137             : }

Generated by: LCOV version 1.14