| 1 |
/* |
| 2 |
Unix SMB/CIFS implementation. |
| 3 |
|
| 4 |
SMB torture tester - charset test routines |
| 5 |
|
| 6 |
Copyright (C) Andrew Tridgell 2001 |
| 7 |
|
| 8 |
This program is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "includes.h" |
| 23 |
#include "libcli/smb2/smb2.h" |
| 24 |
#include "libcli/smb2/smb2_calls.h" |
| 25 |
#include "torture/torture.h" |
| 26 |
#include "torture/smb2/proto.h" |
| 27 |
#include "libcli/libcli.h" |
| 28 |
#include "torture/util.h" |
| 29 |
#include "param/param.h" |
| 30 |
|
| 31 |
#define BASEDIR "chartest" |
| 32 |
|
| 33 |
/* |
| 34 |
open a file using a set of unicode code points for the name |
| 35 |
|
| 36 |
the prefix BASEDIR is added before the name |
| 37 |
*/ |
| 38 |
static NTSTATUS unicode_open(struct torture_context *tctx, |
| 39 |
struct smb2_tree *tree, |
| 40 |
TALLOC_CTX *mem_ctx, |
| 41 |
uint32_t create_disposition, |
| 42 |
const uint32_t *u_name, |
| 43 |
size_t u_name_len) |
| 44 |
{ |
| 45 |
struct smb2_create io = {0}; |
| 46 |
char *fname = NULL; |
| 47 |
char *fname2 = NULL; |
| 48 |
char *ucs_name = NULL; |
| 49 |
size_t i; |
| 50 |
NTSTATUS status; |
| 51 |
|
| 52 |
ucs_name = talloc_size(mem_ctx, (1+u_name_len)*2); |
| 53 |
if (!ucs_name) { |
| 54 |
torture_comment(tctx, "Failed to create UCS2 Name - talloc() failure\n"); |
| 55 |
return NT_STATUS_NO_MEMORY; |
| 56 |
} |
| 57 |
|
| 58 |
for (i=0;i<u_name_len;i++) { |
| 59 |
SSVAL(ucs_name, i*2, u_name[i]); |
| 60 |
} |
| 61 |
SSVAL(ucs_name, i*2, 0); |
| 62 |
|
| 63 |
if (!convert_string_talloc_handle(ucs_name, lpcfg_iconv_handle(tctx->lp_ctx), CH_UTF16, CH_UNIX, ucs_name, (1+u_name_len)*2, (void **)&fname, &i)) { |
| 64 |
torture_comment(tctx, "Failed to convert UCS2 Name into unix - convert_string_talloc() failure\n"); |
| 65 |
talloc_free(ucs_name); |
| 66 |
return NT_STATUS_NO_MEMORY; |
| 67 |
} |
| 68 |
|
| 69 |
fname2 = talloc_asprintf(ucs_name, "%s\\%s", BASEDIR, fname); |
| 70 |
if (!fname2) { |
| 71 |
talloc_free(ucs_name); |
| 72 |
torture_comment(tctx, "Failed to create fname - talloc() failure\n"); |
| 73 |
return NT_STATUS_NO_MEMORY; |
| 74 |
} |
| 75 |
|
| 76 |
io.in.create_flags = NTCREATEX_FLAGS_EXTENDED; |
| 77 |
io.in.desired_access = SEC_RIGHTS_FILE_ALL; |
| 78 |
io.in.file_attributes = FILE_ATTRIBUTE_NORMAL; |
| 79 |
io.in.share_access = NTCREATEX_SHARE_ACCESS_NONE; |
| 80 |
io.in.create_options = 0; |
| 81 |
io.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS; |
| 82 |
io.in.security_flags = 0; |
| 83 |
io.in.fname = fname2; |
| 84 |
io.in.create_disposition = create_disposition; |
| 85 |
|
| 86 |
status = smb2_create(tree, tctx, &io); |
| 87 |
if (!NT_STATUS_IS_OK(status)) { |
| 88 |
talloc_free(ucs_name); |
| 89 |
return status; |
| 90 |
} |
| 91 |
|
| 92 |
smb2_util_close(tree, io.out.file.handle); |
| 93 |
talloc_free(ucs_name); |
| 94 |
return NT_STATUS_OK; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
/* |
| 99 |
see if the server recognises composed characters |
| 100 |
*/ |
| 101 |
static bool test_composed(struct torture_context *tctx, |
| 102 |
struct smb2_tree *tree) |
| 103 |
{ |
| 104 |
const uint32_t name1[] = {0x61, 0x308}; |
| 105 |
const uint32_t name2[] = {0xe4}; |
| 106 |
NTSTATUS status; |
| 107 |
bool ret = true; |
| 108 |
|
| 109 |
ret = smb2_util_setup_dir(tctx, tree, BASEDIR); |
| 110 |
torture_assert_goto(tctx, ret, ret, done, "setting up basedir"); |
| 111 |
|
| 112 |
status = unicode_open(tctx, tree, tctx, |
| 113 |
NTCREATEX_DISP_CREATE, name1, 2); |
| 114 |
torture_assert_ntstatus_ok_goto(tctx, status, ret, done, |
| 115 |
"Failed to create composed name"); |
| 116 |
|
| 117 |
status = unicode_open(tctx, tree, tctx, |
| 118 |
NTCREATEX_DISP_CREATE, name2, 1); |
| 119 |
torture_assert_ntstatus_ok_goto(tctx, status, ret, done, |
| 120 |
"Failed to create accented character"); |
| 121 |
|
| 122 |
done: |
| 123 |
smb2_deltree(tree, BASEDIR); |
| 124 |
return ret; |
| 125 |
} |
| 126 |
|
| 127 |
/* |
| 128 |
see if the server recognises a naked diacritical |
| 129 |
*/ |
| 130 |
static bool test_diacritical(struct torture_context *tctx, |
| 131 |
struct smb2_tree *tree) |
| 132 |
{ |
| 133 |
const uint32_t name1[] = {0x308}; |
| 134 |
const uint32_t name2[] = {0x308, 0x308}; |
| 135 |
NTSTATUS status; |
| 136 |
bool ret = true; |
| 137 |
|
| 138 |
ret = smb2_util_setup_dir(tctx, tree, BASEDIR); |
| 139 |
torture_assert_goto(tctx, ret, ret, done, "setting up basedir"); |
| 140 |
|
| 141 |
status = unicode_open(tctx, tree, tctx, |
| 142 |
NTCREATEX_DISP_CREATE, name1, 1); |
| 143 |
torture_assert_ntstatus_ok_goto(tctx, status, ret, done, |
| 144 |
"Failed to create naked diacritical"); |
| 145 |
|
| 146 |
/* try a double diacritical */ |
| 147 |
status = unicode_open(tctx, tree, tctx, |
| 148 |
NTCREATEX_DISP_CREATE, name2, 2); |
| 149 |
torture_assert_ntstatus_ok_goto(tctx, status, ret, done, |
| 150 |
"Failed to create double " |
| 151 |
"naked diacritical"); |
| 152 |
|
| 153 |
done: |
| 154 |
smb2_deltree(tree, BASEDIR); |
| 155 |
return ret; |
| 156 |
} |
| 157 |
|
| 158 |
/* |
| 159 |
see if the server recognises a partial surrogate pair |
| 160 |
*/ |
| 161 |
static bool test_surrogate(struct torture_context *tctx, |
| 162 |
struct smb2_tree *tree) |
| 163 |
{ |
| 164 |
const uint32_t name1[] = {0xd800}; |
| 165 |
const uint32_t name2[] = {0xdc00}; |
| 166 |
const uint32_t name3[] = {0xd800, 0xdc00}; |
| 167 |
NTSTATUS status; |
| 168 |
bool ret = true; |
| 169 |
|
| 170 |
ret = smb2_util_setup_dir(tctx, tree, BASEDIR); |
| 171 |
torture_assert_goto(tctx, ret, ret, done, "setting up basedir"); |
| 172 |
|
| 173 |
status = unicode_open(tctx, tree, tctx, NTCREATEX_DISP_CREATE, name1, 1); |
| 174 |
torture_assert_ntstatus_ok_goto(tctx, status, ret, done, |
| 175 |
"Failed to create partial surrogate 1"); |
| 176 |
|
| 177 |
status = unicode_open(tctx, tree, tctx, NTCREATEX_DISP_CREATE, name2, 1); |
| 178 |
torture_assert_ntstatus_ok_goto(tctx, status, ret, done, |
| 179 |
"Failed to create partial surrogate 2"); |
| 180 |
|
| 181 |
status = unicode_open(tctx, tree, tctx, NTCREATEX_DISP_CREATE, name3, 2); |
| 182 |
torture_assert_ntstatus_ok_goto(tctx, status, ret, done, |
| 183 |
"Failed to create full surrogate"); |
| 184 |
|
| 185 |
done: |
| 186 |
smb2_deltree(tree, BASEDIR); |
| 187 |
return true; |
| 188 |
} |
| 189 |
|
| 190 |
/* |
| 191 |
see if the server recognises wide-a characters |
| 192 |
*/ |
| 193 |
static bool test_widea(struct torture_context *tctx, |
| 194 |
struct smb2_tree *tree) |
| 195 |
{ |
| 196 |
const uint32_t name1[] = {'a'}; |
| 197 |
const uint32_t name2[] = {0xff41}; |
| 198 |
const uint32_t name3[] = {0xff21}; |
| 199 |
NTSTATUS status; |
| 200 |
bool ret = true; |
| 201 |
|
| 202 |
ret = smb2_util_setup_dir(tctx, tree, BASEDIR); |
| 203 |
torture_assert_goto(tctx, ret, ret, done, "setting up basedir"); |
| 204 |
|
| 205 |
status = unicode_open(tctx, tree, tctx, NTCREATEX_DISP_CREATE, name1, 1); |
| 206 |
torture_assert_ntstatus_ok_goto(tctx, status, ret, done, |
| 207 |
"Failed to create 'a'"); |
| 208 |
|
| 209 |
status = unicode_open(tctx, tree, tctx, NTCREATEX_DISP_CREATE, name2, 1); |
| 210 |
torture_assert_ntstatus_ok_goto(tctx, status, ret, done, |
| 211 |
"Failed to create wide-a"); |
| 212 |
|
| 213 |
status = unicode_open(tctx, tree, tctx, NTCREATEX_DISP_CREATE, name3, 1); |
| 214 |
torture_assert_ntstatus_equal_goto(tctx, |
| 215 |
status, |
| 216 |
NT_STATUS_OBJECT_NAME_COLLISION, |
| 217 |
ret, done, |
| 218 |
"Failed to create wide-A"); |
| 219 |
|
| 220 |
done: |
| 221 |
smb2_deltree(tree, BASEDIR); |
| 222 |
return ret; |
| 223 |
} |
| 224 |
|
| 225 |
struct torture_suite *torture_smb2_charset(TALLOC_CTX *mem_ctx) |
| 226 |
{ |
| 227 |
struct torture_suite *suite = torture_suite_create(mem_ctx, "charset"); |
| 228 |
|
| 229 |
torture_suite_add_1smb2_test(suite, "Testing composite character (a umlaut)", test_composed); |
| 230 |
torture_suite_add_1smb2_test(suite, "Testing naked diacritical (umlaut)", test_diacritical); |
| 231 |
torture_suite_add_1smb2_test(suite, "Testing partial surrogate", test_surrogate); |
| 232 |
torture_suite_add_1smb2_test(suite, "Testing wide-a", test_widea); |
| 233 |
|
| 234 |
return suite; |
| 235 |
} |