LCOV - code coverage report
Current view: top level - source3/torture - test_namemap_cache.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 82 125 65.6 %
Date: 2021-09-23 10:06:22 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Unix SMB/CIFS implementation.
       3             :  * namemap_cache.c
       4             :  * Copyright (C) Volker Lendecke 2017
       5             :  *
       6             :  * This program is free software; you can redistribute it and/or modify
       7             :  * it under the terms of the GNU General Public License as published by
       8             :  * the Free Software Foundation; either version 3 of the License, or
       9             :  * (at your option) any later version.
      10             :  *
      11             :  * This program is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  * GNU General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU General Public License
      17             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : #include "includes.h"
      21             : #include "torture/proto.h"
      22             : #include "lib/namemap_cache.h"
      23             : #include "libcli/security/dom_sid.h"
      24             : #include "lib/gencache.h"
      25             : 
      26             : static const struct dom_sid domsid = {
      27             :         1, 4, {0,0,0,0,0,5}, {21, 123, 456, 789}
      28             : };
      29             : 
      30           1 : static void namemap_cache1_fn1(const char *domain,
      31             :                                const char *name,
      32             :                                enum lsa_SidType type,
      33             :                                bool expired,
      34             :                                void *private_data)
      35             : {
      36           1 :         bool *p_ok = private_data;
      37             :         bool ok;
      38             : 
      39           1 :         ok = strequal(domain, "nt authority");
      40           1 :         ok &= strequal(name, "network");
      41           1 :         ok &= (type == SID_NAME_WKN_GRP);
      42             : 
      43           1 :         *p_ok = ok;
      44           1 : }
      45             : 
      46           1 : static void namemap_cache1_fn2(const struct dom_sid *sid,
      47             :                                enum lsa_SidType type,
      48             :                                bool expired,
      49             :                                void *private_data)
      50             : {
      51           1 :         bool *p_ok = private_data;
      52             :         bool ok;
      53             : 
      54           1 :         ok = dom_sid_equal(sid, &global_sid_Network);
      55           1 :         ok &= (type == SID_NAME_WKN_GRP);
      56             : 
      57           1 :         *p_ok = ok;
      58           1 : }
      59             : 
      60           1 : static void namemap_cache1_fn3(const char *domain,
      61             :                                const char *name,
      62             :                                enum lsa_SidType type,
      63             :                                bool expired,
      64             :                                void *private_data)
      65             : {
      66           1 :         bool *p_ok = private_data;
      67             :         bool ok;
      68             : 
      69           1 :         ok = strequal(domain, "");
      70           1 :         ok &= strequal(name, "everyone");
      71           1 :         ok &= (type == SID_NAME_WKN_GRP);
      72             : 
      73           1 :         *p_ok = ok;
      74           1 : }
      75             : 
      76           1 : static void namemap_cache1_fn4(const struct dom_sid *sid,
      77             :                                enum lsa_SidType type,
      78             :                                bool expired,
      79             :                                void *private_data)
      80             : {
      81           1 :         bool *p_ok = private_data;
      82             :         bool ok;
      83             : 
      84           1 :         ok = dom_sid_equal(sid, &global_sid_World);
      85           1 :         ok &= (type == SID_NAME_WKN_GRP);
      86             : 
      87           1 :         *p_ok = ok;
      88           1 : }
      89             : 
      90           1 : static void namemap_cache1_fn5(const char *domain,
      91             :                                const char *name,
      92             :                                enum lsa_SidType type,
      93             :                                bool expired,
      94             :                                void *private_data)
      95             : {
      96           1 :         bool *p_ok = private_data;
      97             :         bool ok;
      98             : 
      99           1 :         ok = strequal(domain, "samba-dom");
     100           1 :         ok &= strequal(name, "");
     101           1 :         ok &= (type == SID_NAME_DOMAIN);
     102             : 
     103           1 :         *p_ok = ok;
     104           1 : }
     105             : 
     106           1 : static void namemap_cache1_fn6(const struct dom_sid *sid,
     107             :                                enum lsa_SidType type,
     108             :                                bool expired,
     109             :                                void *private_data)
     110             : {
     111           1 :         bool *p_ok = private_data;
     112             :         bool ok;
     113             : 
     114           1 :         ok = dom_sid_equal(sid, &domsid);
     115           1 :         ok &= (type == SID_NAME_DOMAIN);
     116             : 
     117           1 :         *p_ok = ok;
     118           1 : }
     119             : 
     120           1 : bool run_local_namemap_cache1(int dummy)
     121             : {
     122             :         bool found;
     123             :         bool ok;
     124             : 
     125           1 :         ok = gencache_set("SID2NAME/S-1-5-2", "invalid", time(NULL)+60);
     126           1 :         if (!ok) {
     127           0 :                 fprintf(stderr, "gencache_set failed\n");
     128           0 :                 return false;
     129             :         }
     130             : 
     131           1 :         ok = namemap_cache_find_sid(&global_sid_Network, namemap_cache1_fn1,
     132             :                                     &found);
     133           1 :         if (ok) {
     134           0 :                 fprintf(stderr, "namemap_cache_find_sid parsed valid value\n");
     135           0 :                 return false;
     136             :         }
     137             : 
     138           1 :         ok = namemap_cache_set_sid2name(&global_sid_Network,
     139             :                                         "NT Authority", "Network",
     140             :                                         SID_NAME_WKN_GRP,
     141           1 :                                         time(NULL) + 60);
     142           1 :         if (!ok) {
     143           0 :                 fprintf(stderr, "namemap_cache_set_sid2name failed\n");
     144           0 :                 return false;
     145             :         }
     146             : 
     147           1 :         ok = namemap_cache_find_sid(&global_sid_Network, namemap_cache1_fn1,
     148             :                                     &found);
     149           1 :         if (!ok) {
     150           0 :                 fprintf(stderr, "namecache_find_sid failed\n");
     151           0 :                 return false;
     152             :         }
     153           1 :         if (!found) {
     154           0 :                 fprintf(stderr, "wrong values found\n");
     155           0 :                 return false;
     156             :         }
     157             : 
     158           1 :         ok = namemap_cache_set_name2sid("NT Authority", "Network",
     159             :                                         &global_sid_Network,
     160             :                                         SID_NAME_WKN_GRP,
     161           1 :                                         time(NULL) + 60);
     162           1 :         if (!ok) {
     163           0 :                 fprintf(stderr, "namemap_cache_set_name2sid failed\n");
     164           0 :                 return false;
     165             :         }
     166             : 
     167           1 :         ok = namemap_cache_find_name("nt authority", "network",
     168             :                                      namemap_cache1_fn2, &found);
     169           1 :         if (!ok) {
     170           0 :                 fprintf(stderr, "namecache_find_name failed\n");
     171           0 :                 return false;
     172             :         }
     173           1 :         if (!found) {
     174           0 :                 fprintf(stderr, "wrong values found\n");
     175           0 :                 return false;
     176             :         }
     177             : 
     178           1 :         ok = namemap_cache_find_name("foo", "bar", namemap_cache1_fn2, &found);
     179           1 :         if (ok) {
     180           0 :                 fprintf(stderr,
     181             :                         "namemap_cache_find_name succeeded unexpectedly\n");
     182           0 :                 return false;
     183             :         }
     184             : 
     185             :         /*
     186             :          * Test "" domain name
     187             :          */
     188             : 
     189           1 :         ok = namemap_cache_set_sid2name(&global_sid_World, "", "Everyone",
     190             :                                         SID_NAME_WKN_GRP,
     191           1 :                                         time(NULL) + 60);
     192           1 :         if (!ok) {
     193           0 :                 fprintf(stderr, "namemap_cache_set_sid2name failed\n");
     194           0 :                 return false;
     195             :         }
     196             : 
     197           1 :         ok = namemap_cache_find_sid(&global_sid_World, namemap_cache1_fn3,
     198             :                                     &found);
     199           1 :         if (!ok) {
     200           0 :                 fprintf(stderr, "namecache_find_sid failed\n");
     201           0 :                 return false;
     202             :         }
     203           1 :         if (!found) {
     204           0 :                 fprintf(stderr, "wrong values found\n");
     205           0 :                 return false;
     206             :         }
     207             : 
     208           1 :         ok = namemap_cache_set_name2sid("", "Everyone",
     209             :                                         &global_sid_World, SID_NAME_WKN_GRP,
     210           1 :                                         time(NULL) + 60);
     211           1 :         if (!ok) {
     212           0 :                 fprintf(stderr, "namemap_cache_set failed\n");
     213           0 :                 return false;
     214             :         }
     215             : 
     216           1 :         ok = namemap_cache_find_name("", "everyone",
     217             :                                      namemap_cache1_fn4, &found);
     218           1 :         if (!ok) {
     219           0 :                 fprintf(stderr, "namecache_find_name failed\n");
     220           0 :                 return false;
     221             :         }
     222           1 :         if (!found) {
     223           0 :                 fprintf(stderr, "wrong values found\n");
     224           0 :                 return false;
     225             :         }
     226             : 
     227             :         /*
     228             :          * Test domain only
     229             :          */
     230             : 
     231           1 :         ok = namemap_cache_set_sid2name(&domsid, "SAMBA-DOM", "",
     232             :                                         SID_NAME_DOMAIN,
     233           1 :                                         time(NULL) + 60);
     234           1 :         if (!ok) {
     235           0 :                 fprintf(stderr, "namemap_cache_set failed\n");
     236           0 :                 return false;
     237             :         }
     238             : 
     239           1 :         ok = namemap_cache_find_sid(&domsid, namemap_cache1_fn5,
     240             :                                     &found);
     241           1 :         if (!ok) {
     242           0 :                 fprintf(stderr, "namecache_find_sid failed\n");
     243           0 :                 return false;
     244             :         }
     245           1 :         if (!found) {
     246           0 :                 fprintf(stderr, "wrong values found\n");
     247           0 :                 return false;
     248             :         }
     249             : 
     250           1 :         ok = namemap_cache_set_name2sid("SAMBA-DOM", "",
     251             :                                         &domsid, SID_NAME_DOMAIN,
     252           1 :                                         time(NULL) + 60);
     253           1 :         if (!ok) {
     254           0 :                 fprintf(stderr, "namemap_cache_set failed\n");
     255           0 :                 return false;
     256             :         }
     257             : 
     258           1 :         ok = namemap_cache_find_name("samba-dom", "",
     259             :                                      namemap_cache1_fn6, &found);
     260           1 :         if (!ok) {
     261           0 :                 fprintf(stderr, "namecache_find_name failed\n");
     262           0 :                 return false;
     263             :         }
     264           1 :         if (!found) {
     265           0 :                 fprintf(stderr, "wrong values found\n");
     266           0 :                 return false;
     267             :         }
     268             : 
     269           0 :         return true;
     270             : }

Generated by: LCOV version 1.13