LCOV - code coverage report
Current view: top level - source4/torture/libnet - libnet_share.c (source / functions) Hit Total Coverage
Test: coverage report for master 2b515b7d Lines: 96 126 76.2 %
Date: 2024-02-28 12:06:22 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    Test suite for libnet calls.
       4             : 
       5             :    Copyright (C) Gregory LEOCADIE <gleocadie@idealx.com> 2005
       6             :    Copyright (C) Rafal Szczesniak  2005
       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 "includes.h"
      23             : #include "torture/rpc/torture_rpc.h"
      24             : #include "libnet/libnet.h"
      25             : #include "lib/cmdline/cmdline.h"
      26             : #include "librpc/gen_ndr/ndr_srvsvc_c.h"
      27             : #include "torture/libnet/proto.h"
      28             : 
      29             : 
      30             : #define TEST_SHARENAME "libnetsharetest"
      31             : 
      32             : 
      33           5 : static void test_displayshares(struct torture_context *tctx,
      34             :                                struct libnet_ListShares s)
      35             : {
      36           0 :         int i, j;
      37             : 
      38           0 :         struct share_type {
      39             :                 enum srvsvc_ShareType type;
      40             :                 const char *desc;
      41           5 :         } share_types[] = {
      42             :                 { STYPE_DISKTREE, "STYPE_DISKTREE" },
      43             :                 { STYPE_DISKTREE_TEMPORARY, "STYPE_DISKTREE_TEMPORARY" },
      44             :                 { STYPE_DISKTREE_HIDDEN, "STYPE_DISKTREE_HIDDEN" },
      45             :                 { STYPE_PRINTQ, "STYPE_PRINTQ" },
      46             :                 { STYPE_PRINTQ_TEMPORARY, "STYPE_PRINTQ_TEMPORARY" },
      47             :                 { STYPE_PRINTQ_HIDDEN, "STYPE_PRINTQ_HIDDEN" },
      48             :                 { STYPE_DEVICE, "STYPE_DEVICE" },
      49             :                 { STYPE_DEVICE_TEMPORARY, "STYPE_DEVICE_TEMPORARY" },
      50             :                 { STYPE_DEVICE_HIDDEN, "STYPE_DEVICE_HIDDEN" },
      51             :                 { STYPE_IPC, "STYPE_IPC" },
      52             :                 { STYPE_IPC_TEMPORARY, "STYPE_IPC_TEMPORARY" },
      53             :                 { STYPE_IPC_HIDDEN, "STYPE_IPC_HIDDEN" }
      54             :         };
      55             : 
      56           5 :         switch (s.in.level) {
      57           1 :         case 0:
      58          25 :                 for (i = 0; i < s.out.ctr.ctr0->count; i++) {
      59          24 :                         struct srvsvc_NetShareInfo0 *info = &s.out.ctr.ctr0->array[i];
      60          24 :                         torture_comment(tctx, "\t[%d] %s\n", i, info->name);
      61             :                 }
      62           1 :                 break;
      63             : 
      64           1 :         case 1:
      65          25 :                 for (i = 0; i < s.out.ctr.ctr1->count; i++) {
      66          24 :                         struct srvsvc_NetShareInfo1 *info = &s.out.ctr.ctr1->array[i];
      67          50 :                         for (j = 0; j < ARRAY_SIZE(share_types); j++) {
      68          50 :                                 if (share_types[j].type == info->type) {
      69          24 :                                         torture_comment(tctx,
      70             :                                                         "\t[%d] %s (%s)\t%s\n",
      71             :                                                         i,
      72             :                                                         info->name,
      73             :                                                         info->comment,
      74             :                                                         share_types[j].desc);
      75          24 :                                         break;
      76             :                                 }
      77             :                         }
      78             :                 }
      79           1 :                 break;
      80             : 
      81           1 :         case 2:
      82          25 :                 for (i = 0; i < s.out.ctr.ctr2->count; i++) {
      83          24 :                         struct srvsvc_NetShareInfo2 *info = &s.out.ctr.ctr2->array[i];
      84          50 :                         for (j = 0; j < ARRAY_SIZE(share_types); j++) {
      85          50 :                                 if (share_types[j].type == info->type) {
      86          24 :                                         torture_comment(tctx,
      87             :                                                         "\t[%d] %s\t%s\n"
      88             :                                                         "\t    %s\n"
      89             :                                                         "\t    [perms=0x%08x, "
      90             :                                                         "max_usr=%d, "
      91             :                                                         "cur_usr=%d, "
      92             :                                                         "path=%s, "
      93             :                                                         "pass=%s]\n",
      94             :                                                         i,
      95             :                                                         info->name,
      96             :                                                         share_types[j].desc,
      97             :                                                         info->comment,
      98             :                                                         info->permissions,
      99             :                                                         info->max_users,
     100             :                                                         info->current_users,
     101             :                                                         info->path,
     102             :                                                         info->password);
     103          24 :                                         break;
     104             :                                 }
     105             :                         }
     106             :                 }
     107           1 :                 break;
     108             : 
     109           1 :         case 501:
     110          25 :                 for (i = 0; i < s.out.ctr.ctr501->count; i++) {
     111          24 :                         struct srvsvc_NetShareInfo501 *info = &s.out.ctr.ctr501->array[i];
     112          50 :                         for (j = 0; j < ARRAY_SIZE(share_types); j++) {
     113          50 :                                 if (share_types[j].type == info->type) {
     114          24 :                                         torture_comment(tctx,
     115             :                                                         "\t[%d] %s"
     116             :                                                         "\t%s "
     117             :                                                         "[csc_policy=0x%08x]\n"
     118             :                                                         "\t    %s\n",
     119             :                                                         i,
     120             :                                                         info->name,
     121             :                                                         share_types[j].desc,
     122             :                                                         info->csc_policy,
     123             :                                                         info->comment);
     124          24 :                                         break;
     125             :                                 }
     126             :                         }
     127             :                 }
     128           1 :                 break;
     129             : 
     130           1 :         case 502:
     131          25 :                 for (i = 0; i < s.out.ctr.ctr502->count; i++) {
     132          24 :                         struct srvsvc_NetShareInfo502 *info = &s.out.ctr.ctr502->array[i];
     133          50 :                         for (j = 0; j < ARRAY_SIZE(share_types); j++) {
     134          50 :                                 if (share_types[j].type == info->type) {
     135          24 :                                         torture_comment(tctx,
     136             :                                                         "\t[%d] %s\t%s\n"
     137             :                                                         "\t    %s\n"
     138             :                                                         "\t    [perms=0x%08x, "
     139             :                                                         "max_usr=%d, "
     140             :                                                         "cur_usr=%d, "
     141             :                                                         "path=%s, pass=%s]\n",
     142             :                                                         i,
     143             :                                                         info->name,
     144             :                                                         share_types[j].desc,
     145             :                                                         info->comment,
     146             :                                                         info->permissions,
     147             :                                                         info->max_users,
     148             :                                                         info->current_users,
     149             :                                                         info->path,
     150             :                                                         info->password);
     151          24 :                                         break;
     152             :                                 }
     153             :                         }
     154             :                 }
     155           1 :                 break;
     156             :         }
     157           5 : }
     158             : 
     159             : 
     160           1 : bool torture_listshares(struct torture_context *torture)
     161             : {
     162           0 :         struct libnet_ListShares share;
     163           0 :         NTSTATUS  status;
     164           1 :         uint32_t levels[] = { 0, 1, 2, 501, 502 };
     165           0 :         int i;
     166           1 :         bool ret = true;
     167           0 :         struct libnet_context* libnetctx;
     168           0 :         struct dcerpc_binding *binding;
     169           0 :         TALLOC_CTX *mem_ctx;
     170             : 
     171           1 :         mem_ctx = talloc_init("test_listshares");
     172           1 :         status = torture_rpc_binding(torture, &binding);
     173           1 :         if (!NT_STATUS_IS_OK(status)) {
     174           0 :                 ret = false;
     175           0 :                 goto done;
     176             :         }
     177             : 
     178           1 :         libnetctx = libnet_context_init(torture->ev, torture->lp_ctx);
     179           1 :         if (!libnetctx) {
     180           0 :                 torture_comment(torture, "Couldn't allocate libnet context\n");
     181           0 :                 ret = false;
     182           0 :                 goto done;
     183             :         }
     184             : 
     185           1 :         libnetctx->cred = samba_cmdline_get_creds();
     186             : 
     187           1 :         torture_comment(torture, "Testing libnet_ListShare\n");
     188             : 
     189           1 :         share.in.server_name = dcerpc_binding_get_string_option(binding, "host");
     190             : 
     191           6 :         for (i = 0; i < ARRAY_SIZE(levels); i++) {
     192           5 :                 share.in.level = levels[i];
     193           5 :                 torture_comment(torture, "Testing libnet_ListShare level %u\n", share.in.level);
     194             : 
     195           5 :                 status = libnet_ListShares(libnetctx, mem_ctx, &share);
     196           5 :                 if (!NT_STATUS_IS_OK(status)) {
     197           0 :                         torture_comment(torture, "libnet_ListShare level %u failed - %s\n", share.in.level, share.out.error_string);
     198           0 :                         ret = false;
     199           0 :                         goto done;
     200             :                 }
     201             : 
     202           5 :                 torture_comment(torture, "listing shares:\n");
     203           5 :                 test_displayshares(torture, share);
     204             :         }
     205             : 
     206           1 : done:
     207           1 :         talloc_free(mem_ctx);
     208           1 :         return ret;
     209             : }
     210             : 
     211             : 
     212           1 : static bool test_addshare(struct torture_context *tctx,
     213             :                           struct dcerpc_binding_handle *b, TALLOC_CTX *mem_ctx, const char *host,
     214             :                           const char* share)
     215             : {
     216           0 :         NTSTATUS status;
     217           0 :         struct srvsvc_NetShareAdd add;
     218           0 :         union srvsvc_NetShareInfo info;
     219           0 :         struct srvsvc_NetShareInfo2 i;
     220             : 
     221           1 :         ZERO_STRUCT(i);
     222           1 :         ZERO_STRUCT(info);
     223           1 :         ZERO_STRUCT(add);
     224             : 
     225           1 :         i.name         = share;
     226           1 :         i.type         = STYPE_DISKTREE;
     227           1 :         i.path         = "C:\\WINDOWS\\TEMP";
     228           1 :         i.max_users    = 5;
     229           1 :         i.comment      = "Comment to the test share";
     230           1 :         i.password     = NULL;
     231           1 :         i.permissions  = 0x0;
     232             : 
     233           1 :         info.info2 = &i;
     234             : 
     235           1 :         add.in.server_unc = host;
     236           1 :         add.in.level      = 2;
     237           1 :         add.in.info       = &info;
     238           1 :         add.in.parm_error = NULL;
     239             : 
     240           1 :         status = dcerpc_srvsvc_NetShareAdd_r(b, mem_ctx, &add);
     241           1 :         if (!NT_STATUS_IS_OK(status)) {
     242           0 :                 torture_comment(tctx, "Failed to add a new share\n");
     243           0 :                 return false;
     244             :         }
     245             : 
     246           1 :         torture_comment(tctx, "share added\n");
     247           1 :         return true;
     248             : }
     249             : 
     250             : 
     251           1 : bool torture_delshare(struct torture_context *torture)
     252             : {
     253           0 :         struct dcerpc_pipe *p;
     254           0 :         struct dcerpc_binding *binding;
     255           0 :         struct libnet_context* libnetctx;
     256           0 :         const char *host;
     257           0 :         NTSTATUS  status;
     258           1 :         bool ret = true;
     259           0 :         struct libnet_DelShare share;
     260             : 
     261           1 :         host = torture_setting_string(torture, "host", NULL);
     262           1 :         status = torture_rpc_binding(torture, &binding);
     263           1 :         torture_assert_ntstatus_ok(torture, status, "Failed to get binding");
     264             : 
     265           1 :         libnetctx = libnet_context_init(torture->ev, torture->lp_ctx);
     266           1 :         libnetctx->cred = samba_cmdline_get_creds();
     267             : 
     268           1 :         status = torture_rpc_connection(torture,
     269             :                                         &p,
     270             :                                         &ndr_table_srvsvc);
     271             : 
     272           1 :         torture_assert_ntstatus_ok(torture, status, "Failed to get rpc connection");
     273             : 
     274           1 :         if (!test_addshare(torture, p->binding_handle, torture, host, TEST_SHARENAME)) {
     275           0 :                 return false;
     276             :         }
     277             : 
     278           1 :         share.in.server_name    = dcerpc_binding_get_string_option(binding, "host");
     279           1 :         share.in.share_name     = TEST_SHARENAME;
     280             : 
     281           1 :         status = libnet_DelShare(libnetctx, torture, &share);
     282           1 :         torture_assert_ntstatus_ok(torture, status, "Failed to delete share");
     283             : 
     284           0 :         return ret;
     285             : }

Generated by: LCOV version 1.14