s4-torture: remove unchecked read from smb2 create
[Samba/wip.git] / source3 / torture / test_smb2.c
blobc66d1df4cca79da9ef9b974a984d62adfeed98d7
1 /*
2 Unix SMB/CIFS implementation.
3 Initial test for the smb2 client lib
4 Copyright (C) Volker Lendecke 2011
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.
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.
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/>.
20 #include "includes.h"
21 #include "torture/proto.h"
22 #include "client.h"
23 #include "libsmb/smb2cli.h"
24 #include "libcli/security/security.h"
26 extern fstring host, workgroup, share, password, username, myname;
28 bool run_smb2_basic(int dummy)
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;
39 printf("Starting SMB2-BASIC\n");
41 if (!torture_init_connection(&cli)) {
42 return false;
44 cli->smb2.pid = 0xFEFF;
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;
52 status = smb2cli_sesssetup_ntlmssp(cli, username, workgroup, password);
53 if (!NT_STATUS_IS_OK(status)) {
54 printf("smb2cli_sesssetup returned %s\n", nt_errstr(status));
55 return false;
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;
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;
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;
82 status = smb2cli_flush(cli, fid_persistent, fid_volatile);
83 if (!NT_STATUS_IS_OK(status)) {
84 printf("smb2cli_flush returned %s\n", nt_errstr(status));
85 return false;
88 status = smb2cli_read(cli, 0x10000, 0, fid_persistent,
89 fid_volatile, 2, 0,
90 talloc_tos(), &result, &nread);
91 if (!NT_STATUS_IS_OK(status)) {
92 printf("smb2cli_read returned %s\n", nt_errstr(status));
93 return false;
96 if (nread != strlen(hello)) {
97 printf("smb2cli_read returned %d bytes, expected %d\n",
98 (int)nread, (int)strlen(hello));
99 return false;
102 if (memcmp(hello, result, nread) != 0) {
103 printf("smb2cli_read returned '%s', expected '%s'\n",
104 result, hello);
105 return false;
108 status = smb2cli_close(cli, 0, fid_persistent, fid_volatile);
109 if (!NT_STATUS_IS_OK(status)) {
110 printf("smb2cli_close returned %s\n", nt_errstr(status));
111 return false;
114 status = smb2cli_create(
115 cli, "", SMB2_OPLOCK_LEVEL_NONE, 0,
116 MAXIMUM_ALLOWED_ACCESS, FILE_ATTRIBUTE_DIRECTORY, 0,
117 FILE_OPEN, 0, NULL, &fid_persistent, &fid_volatile);
118 if (!NT_STATUS_IS_OK(status)) {
119 printf("smb2cli_create returned %s\n", nt_errstr(status));
120 return false;
123 status = smb2cli_query_directory(
124 cli, 1, 0, 0, fid_persistent, fid_volatile, "*", 0xffff,
125 talloc_tos(), &dir_data, &dir_data_length);
127 if (!NT_STATUS_IS_OK(status)) {
128 printf("smb2cli_query_directory returned %s\n", nt_errstr(status));
129 return false;
132 status = smb2cli_close(cli, 0, fid_persistent, fid_volatile);
133 if (!NT_STATUS_IS_OK(status)) {
134 printf("smb2cli_close returned %s\n", nt_errstr(status));
135 return false;
138 return true;