s3:torture: add SMB2-BASIC
[samba.git] / source3 / torture / test_smb2.c
1 /*
2    Unix SMB/CIFS implementation.
3    Initial test for the smb2 client lib
4    Copyright (C) Volker Lendecke 2011
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 "torture/proto.h"
22 #include "client.h"
23 #include "libsmb/smb2cli.h"
24 #include "libcli/security/security.h"
25
26 extern fstring host, workgroup, share, password, username, myname;
27
28 bool run_smb2_basic(int dummy)
29 {
30         struct cli_state *cli;
31         NTSTATUS status;
32         uint64_t fid_persistent, fid_volatile;
33         const char *hello = "Hello, world\n";
34         uint8_t *result;
35         uint32_t nread;
36         uint8_t *dir_data;
37         uint32_t dir_data_length;
38
39         printf("Starting SMB2-BASIC\n");
40
41         if (!torture_init_connection(&cli)) {
42                 return false;
43         }
44         cli->smb2.pid = 0xFEFF;
45
46         status = smb2cli_negprot(cli);
47         if (!NT_STATUS_IS_OK(status)) {
48                 printf("smb2cli_negprot returned %s\n", nt_errstr(status));
49                 return false;
50         }
51
52         status = smb2cli_sesssetup(cli, username, workgroup, password);
53         if (!NT_STATUS_IS_OK(status)) {
54                 printf("smb2cli_sesssetup returned %s\n", nt_errstr(status));
55                 return false;
56         }
57
58         status = smb2cli_tcon(cli, share);
59         if (!NT_STATUS_IS_OK(status)) {
60                 printf("smb2cli_tcon returned %s\n", nt_errstr(status));
61                 return false;
62         }
63
64         status = smb2cli_create(
65                 cli, "test.txt", SMB2_OPLOCK_LEVEL_NONE, 0,
66                 MAXIMUM_ALLOWED_ACCESS, FILE_ATTRIBUTE_NORMAL,
67                 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
68                 FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE, NULL,
69                 &fid_persistent, &fid_volatile);
70         if (!NT_STATUS_IS_OK(status)) {
71                 printf("smb2cli_create returned %s\n", nt_errstr(status));
72                 return false;
73         }
74
75         status = smb2cli_write(cli, strlen(hello), 0, fid_persistent,
76                                fid_volatile, 0, 0, (const uint8_t *)hello);
77         if (!NT_STATUS_IS_OK(status)) {
78                 printf("smb2cli_write returned %s\n", nt_errstr(status));
79                 return false;
80         }
81
82         status = smb2cli_read(cli, 0x10000, 0, fid_persistent,
83                                fid_volatile, 2, 0,
84                                talloc_tos(), &result, &nread);
85         if (!NT_STATUS_IS_OK(status)) {
86                 printf("smb2cli_read returned %s\n", nt_errstr(status));
87                 return false;
88         }
89
90         if (nread != strlen(hello)) {
91                 printf("smb2cli_read returned %d bytes, expected %d\n",
92                        (int)nread, (int)strlen(hello));
93                 return false;
94         }
95
96         if (memcmp(hello, result, nread) != 0) {
97                 printf("smb2cli_read returned '%s', expected '%s'\n",
98                        result, hello);
99                 return false;
100         }
101
102         status = smb2cli_close(cli, 0, fid_persistent, fid_volatile);
103         if (!NT_STATUS_IS_OK(status)) {
104                 printf("smb2cli_close returned %s\n", nt_errstr(status));
105                 return false;
106         }
107
108         status = smb2cli_create(
109                 cli, "", SMB2_OPLOCK_LEVEL_NONE, 0,
110                 MAXIMUM_ALLOWED_ACCESS, FILE_ATTRIBUTE_DIRECTORY, 0,
111                 FILE_OPEN, 0, NULL, &fid_persistent, &fid_volatile);
112         if (!NT_STATUS_IS_OK(status)) {
113                 printf("smb2cli_create returned %s\n", nt_errstr(status));
114                 return false;
115         }
116
117         status = smb2cli_query_directory(
118                 cli, 1, 0, 0, fid_persistent, fid_volatile, "*", 0xffff,
119                 talloc_tos(), &dir_data, &dir_data_length);
120
121         if (!NT_STATUS_IS_OK(status)) {
122                 printf("smb2cli_query_directory returned %s\n", nt_errstr(status));
123                 return false;
124         }
125
126         status = smb2cli_close(cli, 0, fid_persistent, fid_volatile);
127         if (!NT_STATUS_IS_OK(status)) {
128                 printf("smb2cli_close returned %s\n", nt_errstr(status));
129                 return false;
130         }
131
132         return true;
133 }