LCOV - code coverage report
Current view: top level - source4/torture/raw - seek.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 141 154 91.6 %
Date: 2024-04-21 15:09:00 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           6 : static bool test_seek(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
      48             : {
      49           1 :         union smb_seek io;
      50           1 :         union smb_fileinfo finfo;
      51           1 :         union smb_setfileinfo sfinfo;
      52           1 :         NTSTATUS status;
      53           6 :         bool ret = true;
      54           1 :         int fnum, fnum2;
      55           6 :         const char *fname = BASEDIR "\\test.txt";
      56           1 :         uint8_t c[2];
      57             : 
      58           6 :         if (!torture_setup_dir(cli, BASEDIR)) {
      59           0 :                 return false;
      60             :         }
      61             : 
      62           6 :         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
      63           6 :         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           6 :         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
      70           6 :         finfo.position_information.in.file.fnum = fnum;
      71             :         
      72           6 :         printf("Trying bad handle\n");
      73           6 :         io.lseek.in.file.fnum = fnum+1;
      74           6 :         io.lseek.in.mode = SEEK_MODE_START;
      75           6 :         io.lseek.in.offset = 0;
      76           6 :         status = smb_raw_seek(cli->tree, &io);
      77           6 :         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
      78             : 
      79           6 :         printf("Trying simple seek\n");
      80           6 :         io.lseek.in.file.fnum = fnum;
      81           6 :         io.lseek.in.mode = SEEK_MODE_START;
      82           6 :         io.lseek.in.offset = 17;
      83           6 :         status = smb_raw_seek(cli->tree, &io);
      84           6 :         CHECK_STATUS(status, NT_STATUS_OK);
      85           6 :         CHECK_VALUE(io.lseek.out.offset, 17);
      86           6 :         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
      87           6 :         CHECK_STATUS(status, NT_STATUS_OK);
      88           6 :         CHECK_VALUE(finfo.position_information.out.position, 0);
      89             :         
      90           6 :         printf("Trying relative seek\n");
      91           6 :         io.lseek.in.file.fnum = fnum;
      92           6 :         io.lseek.in.mode = SEEK_MODE_CURRENT;
      93           6 :         io.lseek.in.offset = -3;
      94           6 :         status = smb_raw_seek(cli->tree, &io);
      95           6 :         CHECK_STATUS(status, NT_STATUS_OK);
      96           6 :         CHECK_VALUE(io.lseek.out.offset, 14);
      97             : 
      98           6 :         printf("Trying end seek\n");
      99           6 :         io.lseek.in.file.fnum = fnum;
     100           6 :         io.lseek.in.mode = SEEK_MODE_END;
     101           6 :         io.lseek.in.offset = 0;
     102           6 :         status = smb_raw_seek(cli->tree, &io);
     103           6 :         CHECK_STATUS(status, NT_STATUS_OK);
     104           6 :         finfo.generic.level = RAW_FILEINFO_ALL_INFO;
     105           6 :         finfo.all_info.in.file.fnum = fnum;
     106           6 :         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
     107           6 :         CHECK_STATUS(status, NT_STATUS_OK);
     108           6 :         CHECK_VALUE(io.lseek.out.offset, finfo.all_info.out.size);
     109             : 
     110           6 :         printf("Trying max seek\n");
     111           6 :         io.lseek.in.file.fnum = fnum;
     112           6 :         io.lseek.in.mode = SEEK_MODE_START;
     113           6 :         io.lseek.in.offset = -1;
     114           6 :         status = smb_raw_seek(cli->tree, &io);
     115           6 :         CHECK_STATUS(status, NT_STATUS_OK);
     116           6 :         CHECK_VALUE(io.lseek.out.offset, 0xffffffff);
     117             : 
     118           6 :         printf("Testing position information change\n");
     119           6 :         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
     120           6 :         finfo.position_information.in.file.fnum = fnum;
     121           6 :         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
     122           6 :         CHECK_STATUS(status, NT_STATUS_OK);
     123           6 :         CHECK_VALUE(finfo.position_information.out.position, 0);
     124             : 
     125           6 :         printf("Trying max overflow\n");
     126           6 :         io.lseek.in.file.fnum = fnum;
     127           6 :         io.lseek.in.mode = SEEK_MODE_CURRENT;
     128           6 :         io.lseek.in.offset = 1000;
     129           6 :         status = smb_raw_seek(cli->tree, &io);
     130           6 :         CHECK_STATUS(status, NT_STATUS_OK);
     131           6 :         CHECK_VALUE(io.lseek.out.offset, 999);
     132             : 
     133           6 :         printf("Testing position information change\n");
     134           6 :         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
     135           6 :         finfo.position_information.in.file.fnum = fnum;
     136           6 :         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
     137           6 :         CHECK_STATUS(status, NT_STATUS_OK);
     138           6 :         CHECK_VALUE(finfo.position_information.out.position, 0);
     139             : 
     140           6 :         printf("trying write to update offset\n");
     141           6 :         ZERO_STRUCT(c);
     142           6 :         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           6 :         printf("Testing position information change\n");
     149           6 :         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
     150           6 :         finfo.position_information.in.file.fnum = fnum;
     151           6 :         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
     152           6 :         CHECK_STATUS(status, NT_STATUS_OK);
     153           6 :         CHECK_VALUE(finfo.position_information.out.position, 0);
     154             : 
     155           6 :         io.lseek.in.file.fnum = fnum;
     156           6 :         io.lseek.in.mode = SEEK_MODE_CURRENT;
     157           6 :         io.lseek.in.offset = 0;
     158           6 :         status = smb_raw_seek(cli->tree, &io);
     159           6 :         CHECK_STATUS(status, NT_STATUS_OK);
     160           6 :         CHECK_VALUE(io.lseek.out.offset, 2);
     161             :         
     162           6 :         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           6 :         printf("Testing position information change\n");
     169           6 :         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
     170           6 :         finfo.position_information.in.file.fnum = fnum;
     171           6 :         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
     172           6 :         CHECK_STATUS(status, NT_STATUS_OK);
     173           6 :         CHECK_VALUE(finfo.position_information.out.position, 1);
     174             : 
     175           6 :         status = smb_raw_seek(cli->tree, &io);
     176           6 :         CHECK_STATUS(status, NT_STATUS_OK);
     177           6 :         CHECK_VALUE(io.lseek.out.offset, 1);
     178             : 
     179           6 :         printf("Testing position information\n");
     180           6 :         fnum2 = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
     181           6 :         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           6 :         sfinfo.generic.level = RAW_SFILEINFO_POSITION_INFORMATION;
     187           6 :         sfinfo.position_information.in.file.fnum = fnum2;
     188           6 :         sfinfo.position_information.in.position = 25;
     189           6 :         status = smb_raw_setfileinfo(cli->tree, &sfinfo);
     190           6 :         CHECK_STATUS(status, NT_STATUS_OK);
     191             : 
     192           6 :         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
     193           6 :         finfo.position_information.in.file.fnum = fnum2;
     194           6 :         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
     195           6 :         CHECK_STATUS(status, NT_STATUS_OK);
     196           6 :         CHECK_VALUE(finfo.position_information.out.position, 25);
     197             : 
     198           6 :         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
     199           6 :         finfo.position_information.in.file.fnum = fnum;
     200           6 :         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
     201           6 :         CHECK_STATUS(status, NT_STATUS_OK);
     202           6 :         CHECK_VALUE(finfo.position_information.out.position, 1);
     203             : 
     204           6 :         printf("position_information via paths\n");
     205             : 
     206           6 :         sfinfo.generic.level = RAW_SFILEINFO_POSITION_INFORMATION;
     207           6 :         sfinfo.position_information.in.file.path = fname;
     208           6 :         sfinfo.position_information.in.position = 32;
     209           6 :         status = smb_raw_setpathinfo(cli->tree, &sfinfo);
     210           6 :         CHECK_STATUS(status, NT_STATUS_OK);
     211             : 
     212           6 :         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
     213           6 :         finfo.position_information.in.file.fnum = fnum2;
     214           6 :         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
     215           6 :         CHECK_STATUS(status, NT_STATUS_OK);
     216           6 :         CHECK_VALUE(finfo.position_information.out.position, 25);
     217             : 
     218           6 :         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
     219           6 :         finfo.position_information.in.file.path = fname;
     220           6 :         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
     221           6 :         CHECK_STATUS(status, NT_STATUS_OK);
     222           6 :         CHECK_VALUE(finfo.position_information.out.position, 0);
     223             :         
     224             : 
     225           6 : done:
     226           6 :         smb_raw_exit(cli->session);
     227           6 :         smbcli_deltree(cli->tree, BASEDIR);
     228           6 :         return ret;
     229             : }
     230             : 
     231             : 
     232             : /* 
     233             :    basic testing of seek calls
     234             : */
     235           6 : bool torture_raw_seek(struct torture_context *torture, struct smbcli_state *cli)
     236             : {
     237           6 :         bool ret = true;
     238             : 
     239           6 :         ret &= test_seek(cli, torture);
     240             : 
     241           6 :         return ret;
     242             : }

Generated by: LCOV version 1.14