LCOV - code coverage report
Current view: top level - source4/torture/raw - seek.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 135 148 91.2 %
Date: 2024-06-13 04:01:37 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             :    seek test suite
       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/libcli.h"
      23             : #include "torture/util.h"
      24             : #include "torture/raw/proto.h"
      25             : 
      26             : #define CHECK_STATUS(status, correct) do { \
      27             :         if (!NT_STATUS_EQUAL(status, correct)) { \
      28             :                 printf("(%d) Incorrect status %s - should be %s\n", \
      29             :                        __LINE__, nt_errstr(status), nt_errstr(correct)); \
      30             :                 ret = false; \
      31             :                 goto done; \
      32             :         }} while (0)
      33             : 
      34             : #define CHECK_VALUE(v, correct) do { \
      35             :         if ((v) != (correct)) { \
      36             :                 printf("(%d) Incorrect value %s=%d - should be %d\n", \
      37             :                        __LINE__, #v, (int)v, (int)correct); \
      38             :                 ret = false; \
      39             :                 goto done; \
      40             :         }} while (0)
      41             : 
      42             : #define BASEDIR "\\testseek"
      43             : 
      44             : /*
      45             :   test seek ops
      46             : */
      47           1 : static bool test_seek(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
      48             : {
      49             :         union smb_seek io;
      50             :         union smb_fileinfo finfo;
      51             :         union smb_setfileinfo sfinfo;
      52             :         NTSTATUS status;
      53           1 :         bool ret = true;
      54             :         int fnum, fnum2;
      55           1 :         const char *fname = BASEDIR "\\test.txt";
      56             :         uint8_t c[2];
      57             : 
      58           1 :         if (!torture_setup_dir(cli, BASEDIR)) {
      59           0 :                 return false;
      60             :         }
      61             : 
      62           1 :         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
      63           1 :         if (fnum == -1) {
      64           0 :                 printf("Failed to open test.txt - %s\n", smbcli_errstr(cli->tree));
      65           0 :                 ret = false;
      66           0 :                 goto done;
      67             :         }
      68             : 
      69           1 :         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
      70           1 :         finfo.position_information.in.file.fnum = fnum;
      71             :         
      72           1 :         printf("Trying bad handle\n");
      73           1 :         io.lseek.in.file.fnum = fnum+1;
      74           1 :         io.lseek.in.mode = SEEK_MODE_START;
      75           1 :         io.lseek.in.offset = 0;
      76           1 :         status = smb_raw_seek(cli->tree, &io);
      77           1 :         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
      78             : 
      79           1 :         printf("Trying simple seek\n");
      80           1 :         io.lseek.in.file.fnum = fnum;
      81           1 :         io.lseek.in.mode = SEEK_MODE_START;
      82           1 :         io.lseek.in.offset = 17;
      83           1 :         status = smb_raw_seek(cli->tree, &io);
      84           1 :         CHECK_STATUS(status, NT_STATUS_OK);
      85           1 :         CHECK_VALUE(io.lseek.out.offset, 17);
      86           1 :         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
      87           1 :         CHECK_STATUS(status, NT_STATUS_OK);
      88           1 :         CHECK_VALUE(finfo.position_information.out.position, 0);
      89             :         
      90           1 :         printf("Trying relative seek\n");
      91           1 :         io.lseek.in.file.fnum = fnum;
      92           1 :         io.lseek.in.mode = SEEK_MODE_CURRENT;
      93           1 :         io.lseek.in.offset = -3;
      94           1 :         status = smb_raw_seek(cli->tree, &io);
      95           1 :         CHECK_STATUS(status, NT_STATUS_OK);
      96           1 :         CHECK_VALUE(io.lseek.out.offset, 14);
      97             : 
      98           1 :         printf("Trying end seek\n");
      99           1 :         io.lseek.in.file.fnum = fnum;
     100           1 :         io.lseek.in.mode = SEEK_MODE_END;
     101           1 :         io.lseek.in.offset = 0;
     102           1 :         status = smb_raw_seek(cli->tree, &io);
     103           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     104           1 :         finfo.generic.level = RAW_FILEINFO_ALL_INFO;
     105           1 :         finfo.all_info.in.file.fnum = fnum;
     106           1 :         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
     107           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     108           1 :         CHECK_VALUE(io.lseek.out.offset, finfo.all_info.out.size);
     109             : 
     110           1 :         printf("Trying max seek\n");
     111           1 :         io.lseek.in.file.fnum = fnum;
     112           1 :         io.lseek.in.mode = SEEK_MODE_START;
     113           1 :         io.lseek.in.offset = -1;
     114           1 :         status = smb_raw_seek(cli->tree, &io);
     115           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     116           1 :         CHECK_VALUE(io.lseek.out.offset, 0xffffffff);
     117             : 
     118           1 :         printf("Testing position information change\n");
     119           1 :         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
     120           1 :         finfo.position_information.in.file.fnum = fnum;
     121           1 :         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
     122           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     123           1 :         CHECK_VALUE(finfo.position_information.out.position, 0);
     124             : 
     125           1 :         printf("Trying max overflow\n");
     126           1 :         io.lseek.in.file.fnum = fnum;
     127           1 :         io.lseek.in.mode = SEEK_MODE_CURRENT;
     128           1 :         io.lseek.in.offset = 1000;
     129           1 :         status = smb_raw_seek(cli->tree, &io);
     130           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     131           1 :         CHECK_VALUE(io.lseek.out.offset, 999);
     132             : 
     133           1 :         printf("Testing position information change\n");
     134           1 :         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
     135           1 :         finfo.position_information.in.file.fnum = fnum;
     136           1 :         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
     137           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     138           1 :         CHECK_VALUE(finfo.position_information.out.position, 0);
     139             : 
     140           1 :         printf("trying write to update offset\n");
     141           1 :         ZERO_STRUCT(c);
     142           1 :         if (smbcli_write(cli->tree, fnum, 0, c, 0, 2) != 2) {
     143           0 :                 printf("Write failed - %s\n", smbcli_errstr(cli->tree));
     144           0 :                 ret = false;
     145           0 :                 goto done;              
     146             :         }
     147             : 
     148           1 :         printf("Testing position information change\n");
     149           1 :         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
     150           1 :         finfo.position_information.in.file.fnum = fnum;
     151           1 :         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
     152           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     153           1 :         CHECK_VALUE(finfo.position_information.out.position, 0);
     154             : 
     155           1 :         io.lseek.in.file.fnum = fnum;
     156           1 :         io.lseek.in.mode = SEEK_MODE_CURRENT;
     157           1 :         io.lseek.in.offset = 0;
     158           1 :         status = smb_raw_seek(cli->tree, &io);
     159           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     160           1 :         CHECK_VALUE(io.lseek.out.offset, 2);
     161             :         
     162           1 :         if (smbcli_read(cli->tree, fnum, c, 0, 1) != 1) {
     163           0 :                 printf("Read failed - %s\n", smbcli_errstr(cli->tree));
     164           0 :                 ret = false;
     165           0 :                 goto done;              
     166             :         }
     167             : 
     168           1 :         printf("Testing position information change\n");
     169           1 :         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
     170           1 :         finfo.position_information.in.file.fnum = fnum;
     171           1 :         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
     172           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     173           1 :         CHECK_VALUE(finfo.position_information.out.position, 1);
     174             : 
     175           1 :         status = smb_raw_seek(cli->tree, &io);
     176           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     177           1 :         CHECK_VALUE(io.lseek.out.offset, 1);
     178             : 
     179           1 :         printf("Testing position information\n");
     180           1 :         fnum2 = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
     181           1 :         if (fnum2 == -1) {
     182           0 :                 printf("2nd open failed - %s\n", smbcli_errstr(cli->tree));
     183           0 :                 ret = false;
     184           0 :                 goto done;
     185             :         }
     186           1 :         sfinfo.generic.level = RAW_SFILEINFO_POSITION_INFORMATION;
     187           1 :         sfinfo.position_information.in.file.fnum = fnum2;
     188           1 :         sfinfo.position_information.in.position = 25;
     189           1 :         status = smb_raw_setfileinfo(cli->tree, &sfinfo);
     190           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     191             : 
     192           1 :         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
     193           1 :         finfo.position_information.in.file.fnum = fnum2;
     194           1 :         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
     195           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     196           1 :         CHECK_VALUE(finfo.position_information.out.position, 25);
     197             : 
     198           1 :         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
     199           1 :         finfo.position_information.in.file.fnum = fnum;
     200           1 :         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
     201           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     202           1 :         CHECK_VALUE(finfo.position_information.out.position, 1);
     203             : 
     204           1 :         printf("position_information via paths\n");
     205             : 
     206           1 :         sfinfo.generic.level = RAW_SFILEINFO_POSITION_INFORMATION;
     207           1 :         sfinfo.position_information.in.file.path = fname;
     208           1 :         sfinfo.position_information.in.position = 32;
     209           1 :         status = smb_raw_setpathinfo(cli->tree, &sfinfo);
     210           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     211             : 
     212           1 :         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
     213           1 :         finfo.position_information.in.file.fnum = fnum2;
     214           1 :         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
     215           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     216           1 :         CHECK_VALUE(finfo.position_information.out.position, 25);
     217             : 
     218           1 :         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
     219           1 :         finfo.position_information.in.file.path = fname;
     220           1 :         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
     221           1 :         CHECK_STATUS(status, NT_STATUS_OK);
     222           1 :         CHECK_VALUE(finfo.position_information.out.position, 0);
     223             :         
     224             : 
     225           2 : done:
     226           1 :         smb_raw_exit(cli->session);
     227           1 :         smbcli_deltree(cli->tree, BASEDIR);
     228           1 :         return ret;
     229             : }
     230             : 
     231             : 
     232             : /* 
     233             :    basic testing of seek calls
     234             : */
     235           1 : bool torture_raw_seek(struct torture_context *torture, struct smbcli_state *cli)
     236             : {
     237           1 :         bool ret = true;
     238             : 
     239           1 :         ret &= test_seek(cli, torture);
     240             : 
     241           1 :         return ret;
     242             : }

Generated by: LCOV version 1.13