LCOV - code coverage report
Current view: top level - test - test-selinux.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 55 56 98.2 %
Date: 2019-08-22 15:41:25 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <sys/stat.h>
       4             : 
       5             : #include "alloc-util.h"
       6             : #include "fd-util.h"
       7             : #include "log.h"
       8             : #include "selinux-util.h"
       9             : #include "string-util.h"
      10             : #include "tests.h"
      11             : #include "time-util.h"
      12             : #include "util.h"
      13             : 
      14           1 : static void test_testing(void) {
      15             :         bool b;
      16             : 
      17           1 :         log_info("============ %s ==========", __func__);
      18             : 
      19           1 :         b = mac_selinux_use();
      20           1 :         log_info("mac_selinux_use → %s", yes_no(b));
      21             : 
      22           1 :         b = mac_selinux_use();
      23           1 :         log_info("mac_selinux_use → %s", yes_no(b));
      24             : 
      25           1 :         mac_selinux_retest();
      26             : 
      27           1 :         b = mac_selinux_use();
      28           1 :         log_info("mac_selinux_use → %s", yes_no(b));
      29             : 
      30           1 :         b = mac_selinux_use();
      31           1 :         log_info("mac_selinux_use → %s", yes_no(b));
      32           1 : }
      33             : 
      34           1 : static void test_loading(void) {
      35             :         usec_t n1, n2;
      36             :         int r;
      37             : 
      38           1 :         log_info("============ %s ==========", __func__);
      39             : 
      40           1 :         n1 = now(CLOCK_MONOTONIC);
      41           1 :         r = mac_selinux_init();
      42           1 :         n2 = now(CLOCK_MONOTONIC);
      43           1 :         log_info_errno(r, "mac_selinux_init → %d %.2fs (%m)", r, (n2 - n1)/1e6);
      44           1 : }
      45             : 
      46           1 : static void test_cleanup(void) {
      47             :         usec_t n1, n2;
      48             : 
      49           1 :         log_info("============ %s ==========", __func__);
      50             : 
      51           1 :         n1 = now(CLOCK_MONOTONIC);
      52           1 :         mac_selinux_finish();
      53           1 :         n2 = now(CLOCK_MONOTONIC);
      54           1 :         log_info("mac_selinux_finish → %.2fs", (n2 - n1)/1e6);
      55           1 : }
      56             : 
      57           1 : static void test_misc(const char* fname) {
      58           1 :         _cleanup_(mac_selinux_freep) char *label = NULL, *label2 = NULL, *label3 = NULL;
      59             :         int r;
      60           2 :         _cleanup_close_ int fd = -1;
      61             : 
      62           1 :         log_info("============ %s ==========", __func__);
      63             : 
      64           1 :         r = mac_selinux_get_our_label(&label);
      65           1 :         log_info_errno(r, "mac_selinux_get_our_label → %d, \"%s\" (%m)",
      66             :                        r, strnull(label));
      67             : 
      68           1 :         r = mac_selinux_get_create_label_from_exe(fname, &label2);
      69           1 :         log_info_errno(r, "mac_selinux_create_label_from_exe → %d, \"%s\" (%m)",
      70             :                        r, strnull(label2));
      71             : 
      72           1 :         fd = socket(AF_INET, SOCK_DGRAM, 0);
      73           1 :         assert_se(fd >= 0);
      74             : 
      75           1 :         r = mac_selinux_get_child_mls_label(fd, fname, label2, &label3);
      76           1 :         log_info_errno(r, "mac_selinux_get_child_mls_label → %d, \"%s\" (%m)",
      77             :                        r, strnull(label3));
      78           1 : }
      79             : 
      80           1 : static void test_create_file_prepare(const char* fname) {
      81             :         int r;
      82             : 
      83           1 :         log_info("============ %s ==========", __func__);
      84             : 
      85           1 :         r = mac_selinux_create_file_prepare(fname, S_IRWXU);
      86           1 :         log_info_errno(r, "mac_selinux_create_file_prepare → %d (%m)", r);
      87             : 
      88           1 :         mac_selinux_create_file_clear();
      89           1 : }
      90             : 
      91           1 : int main(int argc, char **argv) {
      92           1 :         const char *path = SYSTEMD_BINARY_PATH;
      93           1 :         if (argc >= 2)
      94           0 :                 path = argv[1];
      95             : 
      96           1 :         test_setup_logging(LOG_DEBUG);
      97             : 
      98           1 :         test_testing();
      99           1 :         test_loading();
     100           1 :         test_misc(path);
     101           1 :         test_create_file_prepare(path);
     102           1 :         test_cleanup();
     103             : 
     104           1 :         return 0;
     105             : }

Generated by: LCOV version 1.14