LCOV - code coverage report
Current view: top level - source3/modules - test_vfs_posixacl.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 71 78 91.0 %
Date: 2021-09-23 10:06:22 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  Unix SMB/CIFS implementation.
       3             :  *
       4             :  *  Unit test for vfs_posixacl
       5             :  *
       6             :  *  Copyright (C) Christof Schmitt 2020
       7             :  *
       8             :  *  This program is free software; you can redistribute it and/or modify
       9             :  *  it under the terms of the GNU General Public License as published by
      10             :  *  the Free Software Foundation; either version 3 of the License, or
      11             :  *  (at your option) any later version.
      12             :  *
      13             :  *  This program is distributed in the hope that it will be useful,
      14             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :  *  GNU General Public License for more details.
      17             :  *
      18             :  *  You should have received a copy of the GNU General Public License
      19             :  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
      20             :  */
      21             : 
      22             : #include "vfs_posixacl.c"
      23             : #include <cmocka.h>
      24             : 
      25           3 : static void smb_acl_add_entry(struct smb_acl_t * smb_acl,
      26             :                               SMB_ACL_TAG_T tag, uint32_t id,
      27             :                               bool read, bool write, bool execute)
      28             : {
      29             :         int ret;
      30           3 :         struct smb_acl_entry *smb_acl_entry = NULL;
      31           3 :         SMB_ACL_PERMSET_T smb_permset = NULL;
      32             : 
      33           3 :         ret = sys_acl_create_entry(&smb_acl, &smb_acl_entry);
      34           3 :         assert_int_equal(ret, 0);
      35             : 
      36           3 :         ret = sys_acl_set_tag_type(smb_acl_entry, tag);
      37           3 :         assert_int_equal(ret, 0);
      38             : 
      39           3 :         if (tag == SMB_ACL_USER || tag == SMB_ACL_GROUP) {
      40           0 :                 ret = sys_acl_set_qualifier(smb_acl_entry, &id);
      41           0 :                 assert_int_equal(ret, 0);
      42             :         }
      43             : 
      44           3 :         ret = sys_acl_get_permset(smb_acl_entry, &smb_permset);
      45           3 :         assert_int_equal(ret, 0);
      46             : 
      47           3 :         if (read) {
      48           1 :                 ret = sys_acl_add_perm(smb_permset, SMB_ACL_READ);
      49           1 :                 assert_int_equal(ret, 0);
      50             :         }
      51             : 
      52           3 :         if (write) {
      53           1 :                 ret = sys_acl_add_perm(smb_permset, SMB_ACL_WRITE);
      54           1 :                 assert_int_equal(ret, 0);
      55             :         }
      56             : 
      57           3 :         if (execute) {
      58           1 :                 ret = sys_acl_add_perm(smb_permset, SMB_ACL_EXECUTE);
      59           1 :                 assert_int_equal(ret, 0);
      60             :         }
      61             : 
      62           3 :         ret = sys_acl_set_permset(smb_acl_entry, smb_permset);
      63           3 :         assert_int_equal(ret, 0);
      64           3 : }
      65             : 
      66           3 : static void acl_check_entry(acl_entry_t acl_entry, SMB_ACL_TAG_T tag,
      67             :                             uint32_t id,
      68             :                             bool read, bool write, bool execute)
      69             : {
      70             :         int ret;
      71           3 :         acl_permset_t acl_permset = NULL;
      72             :         acl_tag_t acl_tag;
      73             : 
      74           3 :         ret = acl_get_permset(acl_entry, &acl_permset);
      75           3 :         assert_int_equal(ret, 0);
      76             : 
      77           3 :         ret = acl_get_tag_type(acl_entry, &acl_tag);
      78           3 :         assert_int_equal(ret, 0);
      79           3 :         assert_int_equal(acl_tag, tag);
      80             : 
      81           3 :         if (tag == ACL_USER || tag == ACL_GROUP) {
      82             :                 uint32_t *id_p;
      83             : 
      84           0 :                 id_p = acl_get_qualifier(acl_entry);
      85           0 :                 assert_non_null(id_p);
      86           0 :                 assert_int_equal(*id_p, id);
      87             :         }
      88             : 
      89             : #ifdef HAVE_ACL_GET_PERM_NP
      90             :         ret = acl_get_perm_np(acl_permset, ACL_READ);
      91             : #else
      92           3 :         ret = acl_get_perm(acl_permset, ACL_READ);
      93             : #endif
      94           3 :         assert_int_equal(ret, read ? 1 : 0);
      95             : 
      96             : #ifdef HAVE_ACL_GET_PERM_NP
      97             :         ret = acl_get_perm_np(acl_permset, ACL_WRITE);
      98             : #else
      99           3 :         ret = acl_get_perm(acl_permset, ACL_WRITE);
     100             : #endif
     101           3 :         assert_int_equal(ret, write ? 1 : 0);
     102             : 
     103             : #ifdef HAVE_ACL_GET_PERM_NP
     104             :         ret = acl_get_perm_np(acl_permset, ACL_EXECUTE);
     105             : #else
     106           3 :         ret = acl_get_perm(acl_permset, ACL_EXECUTE);
     107             : #endif
     108           3 :         assert_int_equal(ret, execute ? 1 : 0);
     109           3 : }
     110             : 
     111           1 : static void test_smb_acl_to_posix_simple_acl(void **state)
     112             : {
     113           1 :         TALLOC_CTX *mem_ctx = talloc_stackframe();
     114           1 :         struct smb_acl_t *smb_acl = NULL;
     115           1 :         acl_t acl = NULL;
     116           1 :         acl_entry_t acl_entry = NULL;
     117             :         int ret;
     118             : 
     119           1 :         smb_acl = sys_acl_init(mem_ctx);
     120           1 :         assert_non_null(smb_acl);
     121             : 
     122           1 :         smb_acl_add_entry(smb_acl, SMB_ACL_USER_OBJ, 0, false, true, false);
     123           1 :         smb_acl_add_entry(smb_acl, SMB_ACL_GROUP_OBJ, 0, true, false, false);
     124           1 :         smb_acl_add_entry(smb_acl, SMB_ACL_OTHER, 0, false, false, true);
     125             : 
     126           1 :         acl = smb_acl_to_posix(smb_acl);
     127           1 :         assert_non_null(acl);
     128             : 
     129           1 :         ret = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry);
     130           1 :         assert_int_equal(ret, 1);
     131           1 :         acl_check_entry(acl_entry, ACL_USER_OBJ, 0, false, true, false);
     132             : 
     133           1 :         ret = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
     134           1 :         assert_int_equal(ret, 1);
     135           1 :         acl_check_entry(acl_entry, ACL_GROUP_OBJ, 0, true, false, false);
     136             : 
     137           1 :         ret = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
     138           1 :         assert_int_equal(ret, 1);
     139           1 :         acl_check_entry(acl_entry, ACL_OTHER, 0, false, false, true);
     140             : 
     141           1 :         ret = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry);
     142           1 :         assert_int_equal(ret, 0);
     143             : 
     144           1 :         ret = acl_free(acl);
     145           1 :         assert_int_equal(ret, 0);
     146             : 
     147           1 :         TALLOC_FREE(mem_ctx);
     148           1 : }
     149             : 
     150           1 : int main(int argc, char **argv)
     151             : {
     152           1 :         const struct CMUnitTest tests[] = {
     153             :                 cmocka_unit_test(test_smb_acl_to_posix_simple_acl),
     154             :         };
     155             : 
     156           1 :         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
     157             : 
     158           1 :         if (argc != 2) {
     159           0 :                 print_error("Usage: %s smb.conf\n", argv[0]);
     160           0 :                 exit(1);
     161             :         }
     162             : 
     163             :         /*
     164             :          * Initialize enough of the Samba internals to have the
     165             :          * mappings tests work.
     166             :          */
     167           1 :         talloc_stackframe();
     168           1 :         lp_load_global(argv[1]);
     169             : 
     170           1 :         return cmocka_run_group_tests(tests, NULL, NULL);
     171             : }

Generated by: LCOV version 1.13