| 1 |
#include "includes.h" |
| 2 |
#include "torture/ndr/ndr.h" |
| 3 |
#include "torture/ndr/proto.h" |
| 4 |
#include "../lib/util/dlinklist.h" |
| 5 |
#include "param/param.h" |
| 6 |
|
| 7 |
static const char *ascii = "ascii"; |
| 8 |
/* the following is equivalent to "kamelåså öäüÿéèóò" in latin1 */ |
| 9 |
static const char latin1[] = { 0x6b, 0x61, 0x6d, 0x65, 0x6c, 0xe5, 0x73, |
| 10 |
0xe5, 0x20, 0xF6, 0xE4, 0xFC, 0xFF, 0xE9, |
| 11 |
0xE8, 0xF3, 0xF2, 0x00 }; |
| 12 |
/* the following is equivalent to "kamelåså ☺☺☺ öäüÿéèóò" in utf8 */ |
| 13 |
static const char utf8[] = { 0x6b, 0x61, 0x6d, 0x65, 0x6c, 0xc3, 0xa5, |
| 14 |
0x73, 0xc3, 0xa5, 0x20, 0xE2, 0x98, 0xBA, |
| 15 |
0xE2, 0x98, 0xBA, 0xE2, 0x98, 0xBA, 0x20, |
| 16 |
0xc3, 0xb6, 0xc3, 0xa4, 0xc3, 0xbc, 0xc3, |
| 17 |
0xbf, 0xc3, 0xa9, 0xc3, 0xa8, 0xc3, 0xb3, |
| 18 |
0xc3, 0xb2, 0x00 }; |
| 19 |
|
| 20 |
/* purely for convenience */ |
| 21 |
static const libndr_flags fl_ascii_null = LIBNDR_FLAG_STR_ASCII|LIBNDR_FLAG_STR_NULLTERM; |
| 22 |
static const libndr_flags fl_ascii_noterm = LIBNDR_FLAG_STR_ASCII|LIBNDR_FLAG_STR_NOTERM|LIBNDR_FLAG_REMAINING; |
| 23 |
static const libndr_flags fl_utf8_null = LIBNDR_FLAG_STR_UTF8|LIBNDR_FLAG_STR_NULLTERM; |
| 24 |
static const libndr_flags fl_raw8_null = LIBNDR_FLAG_STR_RAW8|LIBNDR_FLAG_STR_NULLTERM; |
| 25 |
|
| 26 |
static bool |
| 27 |
test_ndr_push_string (struct torture_context *tctx, const char *string, |
| 28 |
libndr_flags flags, enum ndr_err_code exp_ndr_err, |
| 29 |
bool strcmp_pass) |
| 30 |
{ |
| 31 |
TALLOC_CTX *mem_ctx; |
| 32 |
struct ndr_push *ndr; |
| 33 |
enum ndr_err_code err; |
| 34 |
|
| 35 |
torture_comment(tctx, |
| 36 |
"test_ndr_push_string %s flags 0x%"PRI_LIBNDR_FLAGS" expecting " |
| 37 |
"err 0x%x and strcmp %s\n", string, flags, exp_ndr_err, |
| 38 |
strcmp_pass?"pass":"fail"); |
| 39 |
if (exp_ndr_err != NDR_ERR_SUCCESS) { |
| 40 |
torture_comment(tctx, "(ignore any Conversion error) "); |
| 41 |
} |
| 42 |
|
| 43 |
mem_ctx = talloc_named (NULL, 0, "test_ndr_push_string"); |
| 44 |
ndr = ndr_push_init_ctx(mem_ctx); |
| 45 |
ndr_set_flags (&ndr->flags, flags); |
| 46 |
|
| 47 |
err = ndr_push_string (ndr, NDR_SCALARS, string); |
| 48 |
torture_assert_ndr_err_equal(tctx, err, exp_ndr_err, |
| 49 |
"ndr_push_string: unexpected return code"); |
| 50 |
|
| 51 |
if (exp_ndr_err == NDR_ERR_SUCCESS) { |
| 52 |
uint32_t expected_offset = strlen(string); |
| 53 |
|
| 54 |
if (flags & LIBNDR_FLAG_STR_NULLTERM) { |
| 55 |
expected_offset += 1; |
| 56 |
} |
| 57 |
|
| 58 |
torture_assert_int_equal(tctx, |
| 59 |
ndr->offset, expected_offset, |
| 60 |
"ndr_push_string: invalid length"); |
| 61 |
|
| 62 |
torture_assert(tctx, ndr->data != NULL, |
| 63 |
"ndr_push_string: succeeded but NULL data"); |
| 64 |
|
| 65 |
torture_assert(tctx, |
| 66 |
strcmp_pass == !strcmp(string, (char *)ndr->data), |
| 67 |
"ndr_push_string: post-push strcmp"); |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
talloc_free(mem_ctx); |
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
static bool |
| 76 |
test_ndr_pull_string (struct torture_context *tctx, const char *string, |
| 77 |
libndr_flags flags, enum ndr_err_code exp_ndr_err, |
| 78 |
bool strcmp_pass) |
| 79 |
{ |
| 80 |
TALLOC_CTX *mem_ctx; |
| 81 |
DATA_BLOB blob; |
| 82 |
struct ndr_pull *ndr; |
| 83 |
enum ndr_err_code err; |
| 84 |
const char *result = NULL; |
| 85 |
|
| 86 |
torture_comment(tctx, |
| 87 |
"test_ndr_pull_string '%s' flags 0x%"PRI_LIBNDR_FLAGS" expecting " |
| 88 |
"err 0x%x and strcmp %s\n", string, flags, exp_ndr_err, |
| 89 |
strcmp_pass?"pass":"fail"); |
| 90 |
if (exp_ndr_err != NDR_ERR_SUCCESS) { |
| 91 |
torture_comment(tctx, "(ignore any Conversion error) "); |
| 92 |
} |
| 93 |
|
| 94 |
mem_ctx = talloc_named (NULL, 0, "test_ndr_pull_string"); |
| 95 |
|
| 96 |
blob = data_blob_string_const(string); |
| 97 |
ndr = ndr_pull_init_blob(&blob, mem_ctx); |
| 98 |
torture_assert(mem_ctx, ndr, "ndr init failed"); |
| 99 |
ndr_set_flags (&ndr->flags, flags); |
| 100 |
|
| 101 |
err = ndr_pull_string (ndr, NDR_SCALARS, &result); |
| 102 |
torture_assert_ndr_err_equal(tctx, err, exp_ndr_err, |
| 103 |
"ndr_pull_string: unexpected return code"); |
| 104 |
|
| 105 |
if (exp_ndr_err == NDR_ERR_SUCCESS) { |
| 106 |
torture_assert(tctx, result != NULL, |
| 107 |
"ndr_pull_string: NULL data"); |
| 108 |
torture_assert(tctx, strcmp_pass == !strcmp(string, result), |
| 109 |
"ndr_pull_string: post-pull strcmp"); |
| 110 |
torture_assert(tctx, result != NULL, |
| 111 |
"ndr_pull_string succeeded but result NULL"); |
| 112 |
} |
| 113 |
|
| 114 |
talloc_free(mem_ctx); |
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
static bool |
| 119 |
torture_ndr_string(struct torture_context *torture) |
| 120 |
{ |
| 121 |
const char *saved_dos_cp = talloc_strdup(torture, lpcfg_dos_charset(torture->lp_ctx)); |
| 122 |
|
| 123 |
torture_assert(torture, |
| 124 |
test_ndr_push_string (torture, ascii, fl_ascii_null, |
| 125 |
NDR_ERR_SUCCESS, true), |
| 126 |
"test_ndr_push_string(ASCII, STR_ASCII|STR_NULL)"); |
| 127 |
torture_assert(torture, |
| 128 |
test_ndr_push_string (torture, ascii, fl_ascii_noterm, |
| 129 |
NDR_ERR_SUCCESS, true), |
| 130 |
"test_ndr_push_string(ASCII, STR_ASCII|STR_NOTERM|REMAINING)"); |
| 131 |
torture_assert(torture, |
| 132 |
test_ndr_push_string (torture, "", fl_ascii_null, |
| 133 |
NDR_ERR_SUCCESS, true), |
| 134 |
"test_ndr_push_string('', STR_ASCII|STR_NULL)"); |
| 135 |
torture_assert(torture, |
| 136 |
test_ndr_push_string (torture, "", fl_ascii_noterm, |
| 137 |
NDR_ERR_SUCCESS, true), |
| 138 |
"test_ndr_push_string('', STR_ASCII|STR_NOTERM|REMAINING)"); |
| 139 |
torture_assert(torture, |
| 140 |
test_ndr_push_string (torture, utf8, fl_utf8_null, |
| 141 |
NDR_ERR_SUCCESS, true), |
| 142 |
"test_ndr_push_string(UTF8, STR_UTF8|STR_NULL)"); |
| 143 |
torture_assert(torture, |
| 144 |
test_ndr_push_string (torture, utf8, fl_raw8_null, |
| 145 |
NDR_ERR_SUCCESS, true), |
| 146 |
"test_ndr_push_string(UTF8, STR_RAW8|STR_NULL)"); |
| 147 |
torture_assert(torture, |
| 148 |
test_ndr_push_string (torture, latin1, fl_raw8_null, |
| 149 |
NDR_ERR_SUCCESS, true), |
| 150 |
"test_ndr_push_string(LATIN1, STR_RAW8|STR_NULL)"); |
| 151 |
torture_assert(torture, |
| 152 |
test_ndr_push_string (torture, utf8, fl_ascii_null, |
| 153 |
NDR_ERR_CHARCNV, false), |
| 154 |
"test_ndr_push_string(UTF8, STR_ASCII|STR_NULL)"); |
| 155 |
torture_assert(torture, |
| 156 |
test_ndr_push_string (torture, latin1, fl_ascii_null, |
| 157 |
NDR_ERR_CHARCNV, false), |
| 158 |
"test_ndr_push_string(LATIN1, STR_ASCII|STR_NULL)"); |
| 159 |
|
| 160 |
|
| 161 |
torture_assert(torture, |
| 162 |
test_ndr_pull_string (torture, ascii, fl_ascii_null, |
| 163 |
NDR_ERR_SUCCESS, true), |
| 164 |
"test_ndr_pull_string(ASCII, STR_ASCII|STR_NULL)"); |
| 165 |
torture_assert(torture, |
| 166 |
test_ndr_pull_string (torture, utf8, fl_utf8_null, |
| 167 |
NDR_ERR_SUCCESS, true), |
| 168 |
"test_ndr_pull_string(UTF8, STR_UTF8|STR_NULL)"); |
| 169 |
torture_assert(torture, |
| 170 |
test_ndr_pull_string (torture, utf8, fl_raw8_null, |
| 171 |
NDR_ERR_SUCCESS, true), |
| 172 |
"test_ndr_pull_string(UTF8, STR_RAW8|STR_NULL)"); |
| 173 |
torture_assert(torture, |
| 174 |
test_ndr_pull_string (torture, latin1, fl_raw8_null, |
| 175 |
NDR_ERR_SUCCESS, true), |
| 176 |
"test_ndr_pull_string(LATIN1, STR_RAW8|STR_NULL)"); |
| 177 |
|
| 178 |
/* Depending on runtime config, the behavior of ndr_pull_string on |
| 179 |
* incorrect combinations of strings and flags (latin1 with ASCII |
| 180 |
* flags, for example) may differ; it may return NDR_ERR_CHARCNV, or |
| 181 |
* it may return NDR_ERR_SUCCESS but with a string that has been |
| 182 |
* mutilated, depending on the value of "dos charset". We test for |
| 183 |
* both cases here. */ |
| 184 |
|
| 185 |
lpcfg_do_global_parameter(torture->lp_ctx, "dos charset", "ASCII"); |
| 186 |
reload_charcnv(torture->lp_ctx); |
| 187 |
|
| 188 |
torture_assert(torture, |
| 189 |
test_ndr_pull_string (torture, latin1, fl_ascii_null, |
| 190 |
NDR_ERR_CHARCNV, false), |
| 191 |
"test_ndr_pull_string(LATIN1, STR_ASCII|STR_NULL)"); |
| 192 |
torture_assert(torture, |
| 193 |
test_ndr_pull_string (torture, utf8, fl_ascii_null, |
| 194 |
NDR_ERR_CHARCNV, false), |
| 195 |
"test_ndr_pull_string(UTF8, STR_ASCII|STR_NULL)"); |
| 196 |
|
| 197 |
lpcfg_do_global_parameter(torture->lp_ctx, "dos charset", "CP850"); |
| 198 |
reload_charcnv(torture->lp_ctx); |
| 199 |
|
| 200 |
torture_assert(torture, |
| 201 |
test_ndr_pull_string (torture, latin1, fl_ascii_null, |
| 202 |
NDR_ERR_SUCCESS, false), |
| 203 |
"test_ndr_pull_string(LATIN1, STR_ASCII|STR_NULL)"); |
| 204 |
torture_assert(torture, |
| 205 |
test_ndr_pull_string (torture, utf8, fl_ascii_null, |
| 206 |
NDR_ERR_SUCCESS, false), |
| 207 |
"test_ndr_pull_string(UTF8, STR_ASCII|STR_NULL)"); |
| 208 |
|
| 209 |
lpcfg_do_global_parameter(torture->lp_ctx, "dos charset", saved_dos_cp); |
| 210 |
reload_charcnv(torture->lp_ctx); |
| 211 |
|
| 212 |
return true; |
| 213 |
} |
| 214 |
|
| 215 |
struct torture_suite *ndr_string_suite(TALLOC_CTX *ctx) |
| 216 |
{ |
| 217 |
struct torture_suite *suite = torture_suite_create(ctx, "ndr_string"); |
| 218 |
|
| 219 |
torture_suite_add_simple_test(suite, "ndr_string", torture_ndr_string); |
| 220 |
suite->description = talloc_strdup(suite, "NDR - string-conversion focused push/pull tests"); |
| 221 |
|
| 222 |
return suite; |
| 223 |
} |