LCOV - code coverage report
Current view: top level - test - test-cap-list.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 58 59 98.3 %
Date: 2019-08-23 13:36:53 Functions: 4 4 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 37 70 52.9 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : 
       3                 :            : #include <stdio.h>
       4                 :            : #include <sys/prctl.h>
       5                 :            : 
       6                 :            : #include "alloc-util.h"
       7                 :            : #include "cap-list.h"
       8                 :            : #include "capability-util.h"
       9                 :            : #include "parse-util.h"
      10                 :            : #include "string-util.h"
      11                 :            : #include "util.h"
      12                 :            : 
      13                 :            : /* verify the capability parser */
      14                 :          4 : static void test_cap_list(void) {
      15                 :            :         int i;
      16                 :            : 
      17         [ -  + ]:          4 :         assert_se(!capability_to_name(-1));
      18         [ -  + ]:          4 :         assert_se(!capability_to_name(capability_list_length()));
      19                 :            : 
      20         [ +  + ]:        156 :         for (i = 0; i < capability_list_length(); i++) {
      21                 :            :                 const char *n;
      22                 :            : 
      23         [ -  + ]:        152 :                 assert_se(n = capability_to_name(i));
      24         [ -  + ]:        152 :                 assert_se(capability_from_name(n) == i);
      25                 :        152 :                 printf("%s = %i\n", n, i);
      26                 :            :         }
      27                 :            : 
      28         [ -  + ]:          4 :         assert_se(capability_from_name("asdfbsd") == -EINVAL);
      29         [ -  + ]:          4 :         assert_se(capability_from_name("CAP_AUDIT_READ") == CAP_AUDIT_READ);
      30         [ -  + ]:          4 :         assert_se(capability_from_name("cap_audit_read") == CAP_AUDIT_READ);
      31         [ -  + ]:          4 :         assert_se(capability_from_name("cAp_aUdIt_rEAd") == CAP_AUDIT_READ);
      32         [ -  + ]:          4 :         assert_se(capability_from_name("0") == 0);
      33         [ -  + ]:          4 :         assert_se(capability_from_name("15") == 15);
      34         [ -  + ]:          4 :         assert_se(capability_from_name("-1") == -EINVAL);
      35                 :            : 
      36         [ +  + ]:        156 :         for (i = 0; i < capability_list_length(); i++) {
      37         [ +  - ]:        152 :                 _cleanup_cap_free_charp_ char *a = NULL;
      38                 :            :                 const char *b;
      39                 :            :                 unsigned u;
      40                 :            : 
      41         [ -  + ]:        152 :                 assert_se(a = cap_to_name(i));
      42                 :            : 
      43                 :            :                 /* quit the loop as soon as libcap starts returning
      44                 :            :                  * numeric ids, formatted as strings */
      45         [ -  + ]:        152 :                 if (safe_atou(a, &u) >= 0)
      46                 :          0 :                         break;
      47                 :            : 
      48         [ -  + ]:        152 :                 assert_se(b = capability_to_name(i));
      49                 :            : 
      50                 :        152 :                 printf("%s vs. %s\n", a, b);
      51                 :            : 
      52         [ -  + ]:        152 :                 assert_se(strcasecmp(a, b) == 0);
      53                 :            :         }
      54                 :          4 : }
      55                 :            : 
      56                 :         20 : static void test_capability_set_one(uint64_t c, const char *t) {
      57                 :         20 :         _cleanup_free_ char *t1 = NULL;
      58                 :         20 :         uint64_t c1, c_masked = c & ((UINT64_C(1) << capability_list_length()) - 1);
      59                 :            : 
      60         [ -  + ]:         20 :         assert_se(capability_set_to_string_alloc(c, &t1) == 0);
      61         [ -  + ]:         20 :         assert_se(streq(t1, t));
      62                 :            : 
      63         [ -  + ]:         20 :         assert_se(capability_set_from_string(t1, &c1) == 0);
      64         [ -  + ]:         20 :         assert_se(c1 == c_masked);
      65                 :            : 
      66                 :         20 :         free(t1);
      67         [ -  + ]:         20 :         assert_se(t1 = strjoin("'cap_chown cap_dac_override' \"cap_setgid cap_setuid\"", t,
      68                 :            :                                " hogehoge foobar 12345 3.14 -3 ", t));
      69         [ -  + ]:         20 :         assert_se(capability_set_from_string(t1, &c1) == 0);
      70         [ -  + ]:         20 :         assert_se(c1 == c_masked);
      71                 :         20 : }
      72                 :            : 
      73                 :          4 : static void test_capability_set(void) {
      74                 :            :         uint64_t c;
      75                 :            : 
      76         [ -  + ]:          4 :         assert_se(capability_set_from_string(NULL, &c) == 0);
      77         [ -  + ]:          4 :         assert_se(c == 0);
      78                 :            : 
      79         [ -  + ]:          4 :         assert_se(capability_set_from_string("", &c) == 0);
      80         [ -  + ]:          4 :         assert_se(c == 0);
      81                 :            : 
      82         [ -  + ]:          4 :         assert_se(capability_set_from_string("0", &c) == 0);
      83         [ -  + ]:          4 :         assert_se(c == UINT64_C(1));
      84                 :            : 
      85         [ -  + ]:          4 :         assert_se(capability_set_from_string("1", &c) == 0);
      86         [ -  + ]:          4 :         assert_se(c == UINT64_C(1) << 1);
      87                 :            : 
      88         [ -  + ]:          4 :         assert_se(capability_set_from_string("0 1 2 3", &c) == 0);
      89         [ -  + ]:          4 :         assert_se(c == (UINT64_C(1) << 4) - 1);
      90                 :            : 
      91                 :          4 :         test_capability_set_one(0, "");
      92                 :          4 :         test_capability_set_one(
      93                 :            :                 UINT64_C(1) << CAP_DAC_OVERRIDE,
      94                 :            :                 "cap_dac_override");
      95                 :          4 :         test_capability_set_one(
      96                 :            :                 UINT64_C(1) << CAP_DAC_OVERRIDE |
      97                 :          4 :                 UINT64_C(1) << capability_list_length(),
      98                 :            :                 "cap_dac_override");
      99                 :          4 :         test_capability_set_one(
     100                 :          4 :                 UINT64_C(1) << capability_list_length(), "");
     101                 :          4 :         test_capability_set_one(
     102                 :            :                 UINT64_C(1) << CAP_CHOWN |
     103                 :            :                 UINT64_C(1) << CAP_DAC_OVERRIDE |
     104                 :            :                 UINT64_C(1) << CAP_DAC_READ_SEARCH |
     105                 :            :                 UINT64_C(1) << CAP_FOWNER |
     106                 :            :                 UINT64_C(1) << CAP_SETGID |
     107                 :            :                 UINT64_C(1) << CAP_SETUID |
     108                 :            :                 UINT64_C(1) << CAP_SYS_PTRACE |
     109                 :            :                 UINT64_C(1) << CAP_SYS_ADMIN |
     110                 :            :                 UINT64_C(1) << CAP_AUDIT_CONTROL |
     111                 :            :                 UINT64_C(1) << CAP_MAC_OVERRIDE |
     112                 :            :                 UINT64_C(1) << CAP_SYSLOG |
     113                 :          4 :                 UINT64_C(1) << (capability_list_length() + 1),
     114                 :            :                 "cap_chown cap_dac_override cap_dac_read_search cap_fowner "
     115                 :            :                 "cap_setgid cap_setuid cap_sys_ptrace cap_sys_admin "
     116                 :            :                 "cap_audit_control cap_mac_override cap_syslog");
     117                 :          4 : }
     118                 :            : 
     119                 :          4 : int main(int argc, char *argv[]) {
     120                 :          4 :         test_cap_list();
     121                 :          4 :         test_capability_set();
     122                 :            : 
     123                 :          4 :         return 0;
     124                 :            : }

Generated by: LCOV version 1.14