first public release of samba4 code
[jra/samba/.git] / source4 / torture / raw / seek.c
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 #define CHECK_STATUS(status, correct) do { \
24         if (!NT_STATUS_EQUAL(status, correct)) { \
25                 printf("(%d) Incorrect status %s - should be %s\n", \
26                        __LINE__, nt_errstr(status), nt_errstr(correct)); \
27                 ret = False; \
28                 goto done; \
29         }} while (0)
30
31 #define CHECK_VALUE(v, correct) do { \
32         if ((v) != (correct)) { \
33                 printf("(%d) Incorrect value %s=%d - should be %d\n", \
34                        __LINE__, #v, v, correct); \
35                 ret = False; \
36                 goto done; \
37         }} while (0)
38
39 #define BASEDIR "\\testseek"
40
41 /*
42   test seek ops
43 */
44 static BOOL test_seek(struct cli_state *cli, TALLOC_CTX *mem_ctx)
45 {
46         struct smb_seek io;
47         union smb_fileinfo finfo;
48         NTSTATUS status;
49         BOOL ret = True;
50         int fnum;
51         const char *fname = BASEDIR "\\test.txt";
52
53         if (cli_deltree(cli, BASEDIR) == -1 ||
54             !cli_mkdir(cli, BASEDIR)) {
55                 printf("Unable to setup %s - %s\n", BASEDIR, cli_errstr(cli));
56                 return False;
57         }
58
59         fnum = create_complex_file(cli, mem_ctx, fname);
60         if (fnum == -1) {
61                 printf("Failed to open test.txt - %s\n", cli_errstr(cli));
62                 ret = False;
63                 goto done;
64         }
65
66         finfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
67         finfo.position_information.in.fnum = fnum;
68         
69         printf("Trying bad handle\n");
70         io.in.fnum = fnum+1;
71         io.in.mode = SEEK_MODE_START;
72         io.in.offset = 0;
73         status = smb_raw_seek(cli->tree, &io);
74         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
75
76         printf("Trying simple seek\n");
77         io.in.fnum = fnum;
78         io.in.mode = SEEK_MODE_START;
79         io.in.offset = 17;
80         status = smb_raw_seek(cli->tree, &io);
81         CHECK_STATUS(status, NT_STATUS_OK);
82         CHECK_VALUE(io.out.offset, 17);
83         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
84         CHECK_STATUS(status, NT_STATUS_OK);
85         CHECK_VALUE(finfo.position_information.out.position, 0);
86         
87         printf("Trying relative seek\n");
88         io.in.fnum = fnum;
89         io.in.mode = SEEK_MODE_CURRENT;
90         io.in.offset = -3;
91         status = smb_raw_seek(cli->tree, &io);
92         CHECK_STATUS(status, NT_STATUS_OK);
93         CHECK_VALUE(io.out.offset, 14);
94
95         printf("Trying end seek\n");
96         io.in.fnum = fnum;
97         io.in.mode = SEEK_MODE_END;
98         io.in.offset = 0;
99         status = smb_raw_seek(cli->tree, &io);
100         CHECK_STATUS(status, NT_STATUS_OK);
101         finfo.generic.level = RAW_FILEINFO_ALL_INFO;
102         finfo.all_info.in.fnum = fnum;
103         status = smb_raw_fileinfo(cli->tree, mem_ctx, &finfo);
104         CHECK_STATUS(status, NT_STATUS_OK);
105         CHECK_VALUE(io.out.offset, finfo.all_info.out.size);
106
107         printf("Trying max seek\n");
108         io.in.fnum = fnum;
109         io.in.mode = SEEK_MODE_START;
110         io.in.offset = -1;
111         status = smb_raw_seek(cli->tree, &io);
112         CHECK_STATUS(status, NT_STATUS_OK);
113         CHECK_VALUE(io.out.offset, 0xffffffff);
114
115         printf("Trying max overflow\n");
116         io.in.fnum = fnum;
117         io.in.mode = SEEK_MODE_CURRENT;
118         io.in.offset = 1000;
119         status = smb_raw_seek(cli->tree, &io);
120         CHECK_STATUS(status, NT_STATUS_OK);
121         CHECK_VALUE(io.out.offset, 999);
122
123 done:
124         smb_raw_exit(cli->session);
125         cli_deltree(cli, BASEDIR);
126         return ret;
127 }
128
129
130 /* 
131    basic testing of seek calls
132 */
133 BOOL torture_raw_seek(int dummy)
134 {
135         struct cli_state *cli;
136         BOOL ret = True;
137         TALLOC_CTX *mem_ctx;
138
139         if (!torture_open_connection(&cli)) {
140                 return False;
141         }
142
143         mem_ctx = talloc_init("torture_raw_seek");
144
145         if (!test_seek(cli, mem_ctx)) {
146                 ret = False;
147         }
148
149         torture_close_connection(cli);
150         talloc_destroy(mem_ctx);
151         return ret;
152 }