LCOV - code coverage report
Current view: top level - source4/torture/raw - mux.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 186 196 94.9 %
Date: 2021-09-23 10:06:22 Functions: 4 7 57.1 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             :    basic raw test suite for multiplexing
       4             :    Copyright (C) Andrew Tridgell 2003
       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 "system/filesys.h"
      22             : #include "libcli/raw/libcliraw.h"
      23             : #include "libcli/raw/raw_proto.h"
      24             : #include "libcli/libcli.h"
      25             : #include "torture/util.h"
      26             : #include "torture/raw/proto.h"
      27             : 
      28             : #define BASEDIR "\\test_mux"
      29             : 
      30             : /*
      31             :   test the delayed reply to a open that leads to a sharing violation
      32             : */
      33           1 : static bool test_mux_open(struct torture_context *tctx, struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
      34             : {
      35             :         union smb_open io;
      36             :         NTSTATUS status;
      37             :         int fnum1, fnum2;
      38           1 :         bool ret = true;
      39             :         struct smbcli_request *req1, *req2;
      40             :         struct timeval tv;
      41             :         double d;
      42             : 
      43           1 :         torture_comment(tctx, "Testing multiplexed open/open/close\n");
      44             : 
      45           1 :         torture_comment(tctx, "send first open\n");
      46           1 :         io.generic.level = RAW_OPEN_NTCREATEX;
      47           1 :         io.ntcreatex.in.root_fid.fnum = 0;
      48           1 :         io.ntcreatex.in.flags = 0;
      49           1 :         io.ntcreatex.in.access_mask = SEC_FILE_READ_DATA;
      50           1 :         io.ntcreatex.in.create_options = 0;
      51           1 :         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
      52           1 :         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ;
      53           1 :         io.ntcreatex.in.alloc_size = 0;
      54           1 :         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
      55           1 :         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
      56           1 :         io.ntcreatex.in.security_flags = 0;
      57           1 :         io.ntcreatex.in.fname = BASEDIR "\\open.dat";
      58           1 :         status = smb_raw_open(cli->tree, mem_ctx, &io);
      59           1 :         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OK, "send first open");
      60           1 :         fnum1 = io.ntcreatex.out.file.fnum;
      61             : 
      62           1 :         torture_comment(tctx, "send 2nd open, non-conflicting\n");
      63           1 :         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
      64           1 :         status = smb_raw_open(cli->tree, mem_ctx, &io);
      65           1 :         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OK, "send 2nd open, non-conflicting");
      66           1 :         fnum2 = io.ntcreatex.out.file.fnum;
      67             : 
      68           1 :         tv = timeval_current();
      69             : 
      70           1 :         torture_comment(tctx, "send 3rd open, conflicting\n");
      71           1 :         io.ntcreatex.in.share_access = 0;
      72           1 :         status = smb_raw_open(cli->tree, mem_ctx, &io);
      73           1 :         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_SHARING_VIOLATION, "send 3rd open, conflicting");
      74             : 
      75           1 :         d = timeval_elapsed(&tv);
      76           1 :         if (d < 0.5 || d > 1.5) {
      77           1 :                 torture_comment(tctx, "bad timeout for conflict - %.2f should be 1.0\n", d);
      78             :         } else {
      79           0 :                 torture_comment(tctx, "open delay %.2f\n", d);
      80             :         }
      81             : 
      82           1 :         torture_comment(tctx, "send async open, conflicting\n");
      83           1 :         tv = timeval_current();
      84           1 :         req1 = smb_raw_open_send(cli->tree, &io);
      85             : 
      86           1 :         torture_comment(tctx, "send 2nd async open, conflicting\n");
      87           1 :         tv = timeval_current();
      88           1 :         req2 = smb_raw_open_send(cli->tree, &io);
      89             :         
      90           1 :         torture_comment(tctx, "close first sync open\n");
      91           1 :         smbcli_close(cli->tree, fnum1);
      92             : 
      93           1 :         torture_comment(tctx, "cancel 2nd async open (should be ignored)\n");
      94           1 :         smb_raw_ntcancel(req2);
      95             : 
      96           1 :         d = timeval_elapsed(&tv);
      97           1 :         if (d > 0.25) {
      98           0 :                 torture_comment(tctx, "bad timeout after cancel - %.2f should be <0.25\n", d);
      99           0 :                 torture_assert(tctx, d <= 0.25, "bad timeout after cancel");
     100             :         }
     101             : 
     102           1 :         torture_comment(tctx, "close the 2nd sync open\n");
     103           1 :         smbcli_close(cli->tree, fnum2);
     104             : 
     105           1 :         torture_comment(tctx, "see if the 1st async open now succeeded\n");
     106           1 :         status = smb_raw_open_recv(req1, mem_ctx, &io);
     107           1 :         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OK, "see if the 1st async open now succeeded");
     108             : 
     109           1 :         d = timeval_elapsed(&tv);
     110           1 :         if (d > 0.25) {
     111           0 :                 torture_comment(tctx, "bad timeout for async conflict - %.2f should be <0.25\n", d);
     112           0 :                 torture_assert(tctx, d <= 0.25, "bad timeout for async conflict");
     113             :         } else {
     114           1 :                 torture_comment(tctx, "async open delay %.2f\n", d);
     115             :         }
     116             : 
     117           1 :         torture_comment(tctx, "2nd async open should have timed out\n");
     118           1 :         status = smb_raw_open_recv(req2, mem_ctx, &io);
     119           1 :         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_SHARING_VIOLATION, "2nd async open should have timed out");
     120           1 :         d = timeval_elapsed(&tv);
     121           1 :         if (d < 0.8) {
     122           1 :                 torture_comment(tctx, "bad timeout for async conflict - %.2f should be 1.0\n", d);
     123             :         }
     124             : 
     125           1 :         torture_comment(tctx, "close the 1st async open\n");
     126           1 :         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
     127             : 
     128           1 :         return ret;
     129             : }
     130             : 
     131             : 
     132             : /*
     133             :   test a write that hits a byte range lock and send the close after the write
     134             : */
     135           1 : static bool test_mux_write(struct torture_context *tctx, struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
     136             : {
     137             :         union smb_write io;
     138             :         NTSTATUS status;
     139             :         int fnum;
     140           1 :         bool ret = true;
     141             :         struct smbcli_request *req;
     142             : 
     143           1 :         torture_comment(tctx, "Testing multiplexed lock/write/close\n");
     144             : 
     145           1 :         fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE);
     146           1 :         if (fnum == -1) {
     147           0 :                 torture_comment(tctx, "open failed in mux_write - %s\n", smbcli_errstr(cli->tree));
     148           0 :                 torture_assert(tctx, fnum != -1, "open failed in mux_write");
     149             :         }
     150             : 
     151           1 :         cli->session->pid = 1;
     152             : 
     153           1 :         status = smbcli_lock(cli->tree, fnum, 0, 4, 0, WRITE_LOCK);
     154             : 
     155             :         /* lock a range */
     156           1 :         if (NT_STATUS_IS_ERR(status)) {
     157           0 :                 torture_assert_ntstatus_ok(tctx, status, "lock failed in mux_write");
     158             :         }
     159             : 
     160           1 :         cli->session->pid = 2;
     161             : 
     162             :         /* send an async write */
     163           1 :         io.generic.level = RAW_WRITE_WRITEX;
     164           1 :         io.writex.in.file.fnum = fnum;
     165           1 :         io.writex.in.offset = 0;
     166           1 :         io.writex.in.wmode = 0;
     167           1 :         io.writex.in.remaining = 0;
     168           1 :         io.writex.in.count = 4;
     169           1 :         io.writex.in.data = (const uint8_t *)&fnum; 
     170           1 :         req = smb_raw_write_send(cli->tree, &io);
     171             : 
     172             :         /* unlock the range */
     173           1 :         cli->session->pid = 1;
     174           1 :         smbcli_unlock(cli->tree, fnum, 0, 4);
     175             : 
     176             :         /* and recv the async write reply */
     177           1 :         status = smb_raw_write_recv(req, &io);
     178           1 :         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_FILE_LOCK_CONFLICT, "recv the async write reply");
     179             : 
     180           1 :         smbcli_close(cli->tree, fnum);
     181             : 
     182           1 :         return ret;
     183             : }
     184             : 
     185             : 
     186             : /*
     187             :   test a lock that conflicts with an existing lock
     188             : */
     189           1 : static bool test_mux_lock(struct torture_context *tctx, struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
     190             : {
     191             :         union smb_lock io;
     192             :         NTSTATUS status;
     193             :         int fnum;
     194           1 :         bool ret = true;
     195             :         struct smbcli_request *req;
     196             :         struct smb_lock_entry lock[1];
     197             :         struct timeval t;
     198             : 
     199           1 :         torture_comment(tctx, "TESTING MULTIPLEXED LOCK/LOCK/UNLOCK\n");
     200             : 
     201           1 :         fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE);
     202           1 :         if (fnum == -1) {
     203           0 :                 torture_comment(tctx, "open failed in mux_lock - %s\n", smbcli_errstr(cli->tree));
     204           0 :                 torture_assert(tctx, fnum != -1, "open failed in mux_lock");
     205             :         }
     206             : 
     207           1 :         torture_comment(tctx, "establishing a lock\n");
     208           1 :         io.lockx.level = RAW_LOCK_LOCKX;
     209           1 :         io.lockx.in.file.fnum = fnum;
     210           1 :         io.lockx.in.mode = 0;
     211           1 :         io.lockx.in.timeout = 0;
     212           1 :         io.lockx.in.lock_cnt = 1;
     213           1 :         io.lockx.in.ulock_cnt = 0;
     214           1 :         lock[0].pid = 1;
     215           1 :         lock[0].offset = 0;
     216           1 :         lock[0].count = 4;
     217           1 :         io.lockx.in.locks = &lock[0];
     218             : 
     219           1 :         status = smb_raw_lock(cli->tree, &io);
     220           1 :         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OK, "establishing a lock");
     221             : 
     222           1 :         torture_comment(tctx, "the second lock will conflict with the first\n");
     223           1 :         lock[0].pid = 2;
     224           1 :         io.lockx.in.timeout = 1000;
     225           1 :         status = smb_raw_lock(cli->tree, &io);
     226           1 :         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_FILE_LOCK_CONFLICT, "the second lock will conflict with the first");
     227             : 
     228           1 :         torture_comment(tctx, "this will too, but we'll unlock while waiting\n");
     229           1 :         t = timeval_current();
     230           1 :         req = smb_raw_lock_send(cli->tree, &io);
     231             : 
     232           1 :         torture_comment(tctx, "unlock the first range\n");
     233           1 :         lock[0].pid = 1;
     234           1 :         io.lockx.in.ulock_cnt = 1;
     235           1 :         io.lockx.in.lock_cnt = 0;
     236           1 :         io.lockx.in.timeout = 0;
     237           1 :         status = smb_raw_lock(cli->tree, &io);
     238           1 :         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OK, "unlock the first range");
     239             : 
     240           1 :         torture_comment(tctx, "recv the async reply\n");
     241           1 :         status = smbcli_request_simple_recv(req);
     242           1 :         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OK, "recv the async reply");
     243             : 
     244           1 :         torture_comment(tctx, "async lock took %.2f msec\n", timeval_elapsed(&t) * 1000);
     245           1 :         torture_assert(tctx, timeval_elapsed(&t) <= 0.1, "failed to trigger early lock retry\n");
     246             : 
     247           1 :         torture_comment(tctx, "reopening with an exit\n");
     248           1 :         smb_raw_exit(cli->session);
     249           1 :         fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE);
     250             : 
     251           1 :         torture_comment(tctx, "Now trying with a cancel\n");
     252             : 
     253           1 :         io.lockx.level = RAW_LOCK_LOCKX;
     254           1 :         io.lockx.in.file.fnum = fnum;
     255           1 :         io.lockx.in.mode = 0;
     256           1 :         io.lockx.in.timeout = 0;
     257           1 :         io.lockx.in.lock_cnt = 1;
     258           1 :         io.lockx.in.ulock_cnt = 0;
     259           1 :         lock[0].pid = 1;
     260           1 :         lock[0].offset = 0;
     261           1 :         lock[0].count = 4;
     262           1 :         io.lockx.in.locks = &lock[0];
     263             : 
     264           1 :         status = smb_raw_lock(cli->tree, &io);
     265           1 :         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OK, "Now trying with a cancel");
     266             : 
     267           1 :         lock[0].pid = 2;
     268           1 :         io.lockx.in.timeout = 1000;
     269           1 :         status = smb_raw_lock(cli->tree, &io);
     270           1 :         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_FILE_LOCK_CONFLICT, "Now trying with a cancel pid 2");
     271             : 
     272           1 :         req = smb_raw_lock_send(cli->tree, &io);
     273             : 
     274             :         /* cancel the blocking lock */
     275           1 :         smb_raw_ntcancel(req);
     276             : 
     277           1 :         torture_comment(tctx, "sending 2nd cancel\n");
     278             :         /* the 2nd cancel is totally harmless, but tests the server trying to 
     279             :            cancel an already cancelled request */
     280           1 :         smb_raw_ntcancel(req);
     281             : 
     282           1 :         torture_comment(tctx, "sent 2nd cancel\n");
     283             : 
     284           1 :         lock[0].pid = 1;
     285           1 :         io.lockx.in.ulock_cnt = 1;
     286           1 :         io.lockx.in.lock_cnt = 0;
     287           1 :         io.lockx.in.timeout = 0;
     288           1 :         status = smb_raw_lock(cli->tree, &io);
     289           1 :         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OK, "clear lock");
     290             : 
     291           1 :         status = smbcli_request_simple_recv(req);
     292           1 :         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_FILE_LOCK_CONFLICT, "recv 2nd cancel");
     293             : 
     294           1 :         torture_comment(tctx, "cancel a lock using exit to close file\n");
     295           1 :         lock[0].pid = 1;
     296           1 :         io.lockx.in.ulock_cnt = 0;
     297           1 :         io.lockx.in.lock_cnt = 1;
     298           1 :         io.lockx.in.timeout = 1000;
     299             : 
     300           1 :         status = smb_raw_lock(cli->tree, &io);
     301           1 :         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_OK, "cancel a lock using exit to close file");
     302             : 
     303           1 :         t = timeval_current();
     304           1 :         lock[0].pid = 2;
     305           1 :         req = smb_raw_lock_send(cli->tree, &io);
     306             : 
     307           1 :         smb_raw_exit(cli->session);
     308           1 :         smb_raw_exit(cli->session);
     309           1 :         smb_raw_exit(cli->session);
     310           1 :         smb_raw_exit(cli->session);
     311             : 
     312           1 :         torture_comment(tctx, "recv the async reply\n");
     313           1 :         status = smbcli_request_simple_recv(req);
     314           1 :         torture_assert_ntstatus_equal(tctx, status, NT_STATUS_RANGE_NOT_LOCKED, "recv the async reply");
     315           1 :         torture_comment(tctx, "async lock exit took %.2f msec\n", timeval_elapsed(&t) * 1000);
     316           1 :         torture_assert(tctx, timeval_elapsed(&t) <= 0.1, "failed to trigger early lock failure\n");
     317             : 
     318           1 :         return ret;
     319             : }
     320             : 
     321             : 
     322             : 
     323             : /* 
     324             :    basic testing of multiplexing notify
     325             : */
     326           1 : bool torture_raw_mux(struct torture_context *torture, struct smbcli_state *cli)
     327             : {
     328           1 :         bool ret = true;
     329             :         TALLOC_CTX *frame;
     330             : 
     331           1 :         torture_assert(torture, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
     332           1 :         frame = talloc_stackframe();
     333             : 
     334           1 :         ret &= test_mux_open(torture, cli, frame);
     335           1 :         ret &= test_mux_write(torture, cli, frame);
     336           1 :         ret &= test_mux_lock(torture, cli, frame);
     337             : 
     338           1 :         smb_raw_exit(cli->session);
     339           1 :         smbcli_deltree(cli->tree, BASEDIR);
     340           1 :         TALLOC_FREE(frame);
     341           1 :         return ret;
     342             : }

Generated by: LCOV version 1.13