s3: Replace chain_reply
[Samba/gebeck_regimport.git] / source4 / torture / ndr / string.c
blobb682555dc423781849eaac4b541b4ce8211a035d
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"
7 static const char const *ascii = "ascii";
8 /* the following is equivalent to "kamelåså öäüÿéèóò" in latin1 */
9 static const char const 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 const 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 };
20 /* purely for convenience */
21 static int fl_ascii_null = LIBNDR_FLAG_STR_ASCII|LIBNDR_FLAG_STR_NULLTERM;
22 static int fl_utf8_null = LIBNDR_FLAG_STR_UTF8|LIBNDR_FLAG_STR_NULLTERM;
23 static int fl_raw8_null = LIBNDR_FLAG_STR_RAW8|LIBNDR_FLAG_STR_NULLTERM;
25 static bool
26 test_ndr_push_string (struct torture_context *tctx, const char *string,
27 int flags, enum ndr_err_code exp_ndr_err,
28 bool strcmp_pass)
30 TALLOC_CTX *mem_ctx;
31 struct ndr_push *ndr;
32 enum ndr_err_code err;
34 torture_comment(tctx,
35 "test_ndr_push_string %s flags 0x%x expecting "
36 "err 0x%x and strcmp %s\n", string, flags, exp_ndr_err,
37 strcmp_pass?"pass":"fail");
38 if (exp_ndr_err != NDR_ERR_SUCCESS) {
39 torture_comment(tctx, "(ignore any Conversion error) ");
42 mem_ctx = talloc_named (NULL, 0, "test_ndr_push_string");
43 ndr = talloc_zero (mem_ctx, struct ndr_push);
44 ndr_set_flags (&ndr->flags, flags);
46 err = ndr_push_string (ndr, NDR_SCALARS, string);
47 torture_assert(tctx, err == exp_ndr_err,
48 "ndr_push_string: unexpected return code");
50 if (exp_ndr_err == NDR_ERR_SUCCESS) {
51 torture_assert(tctx, ndr->data != NULL,
52 "ndr_push_string: succeeded but NULL data");
54 torture_assert(tctx,
55 strcmp_pass == !strcmp(string, (char *)ndr->data),
56 "ndr_push_string: post-push strcmp");
59 talloc_free(mem_ctx);
60 return true;
63 static bool
64 test_ndr_pull_string (struct torture_context *tctx, const char *string,
65 int flags, enum ndr_err_code exp_ndr_err,
66 bool strcmp_pass)
68 TALLOC_CTX *mem_ctx;
69 DATA_BLOB blob;
70 struct ndr_pull *ndr;
71 enum ndr_err_code err;
72 const char *result = NULL;
74 torture_comment(tctx,
75 "test_ndr_pull_string '%s' flags 0x%x expecting "
76 "err 0x%x and strcmp %s\n", string, flags, exp_ndr_err,
77 strcmp_pass?"pass":"fail");
78 if (exp_ndr_err != NDR_ERR_SUCCESS) {
79 torture_comment(tctx, "(ignore any Conversion error) ");
82 mem_ctx = talloc_named (NULL, 0, "test_ndr_pull_string");
84 blob = data_blob_string_const(string);
85 ndr = ndr_pull_init_blob(&blob, mem_ctx);
86 ndr_set_flags (&ndr->flags, flags);
88 err = ndr_pull_string (ndr, NDR_SCALARS, &result);
89 torture_assert(tctx, err == exp_ndr_err,
90 "ndr_pull_string: unexpected return code");
92 if (exp_ndr_err == NDR_ERR_SUCCESS) {
93 torture_assert(tctx, result != NULL,
94 "ndr_pull_string: NULL data");
95 torture_assert(tctx, strcmp_pass == !strcmp(string, result),
96 "ndr_pull_string: post-pull strcmp");
97 torture_assert(tctx, result != NULL,
98 "ndr_pull_string succeeded but result NULL");
101 talloc_free(mem_ctx);
102 return true;
105 static bool
106 torture_ndr_string(struct torture_context *torture)
108 const char *saved_dos_cp = talloc_strdup(torture, lpcfg_dos_charset(torture->lp_ctx));
110 torture_assert(torture,
111 test_ndr_push_string (torture, ascii, fl_ascii_null,
112 NDR_ERR_SUCCESS, true),
113 "test_ndr_push_string(ASCII, STR_ASCII|STR_NULL)");
114 torture_assert(torture,
115 test_ndr_push_string (torture, utf8, fl_utf8_null,
116 NDR_ERR_SUCCESS, true),
117 "test_ndr_push_string(UTF8, STR_UTF8|STR_NULL)");
118 torture_assert(torture,
119 test_ndr_push_string (torture, utf8, fl_raw8_null,
120 NDR_ERR_SUCCESS, true),
121 "test_ndr_push_string(UTF8, STR_RAW8|STR_NULL)");
122 torture_assert(torture,
123 test_ndr_push_string (torture, latin1, fl_raw8_null,
124 NDR_ERR_SUCCESS, true),
125 "test_ndr_push_string(LATIN1, STR_RAW8|STR_NULL)");
126 torture_assert(torture,
127 test_ndr_push_string (torture, utf8, fl_ascii_null,
128 NDR_ERR_CHARCNV, false),
129 "test_ndr_push_string(UTF8, STR_ASCII|STR_NULL)");
130 torture_assert(torture,
131 test_ndr_push_string (torture, latin1, fl_ascii_null,
132 NDR_ERR_CHARCNV, false),
133 "test_ndr_push_string(LATIN1, STR_ASCII|STR_NULL)");
136 torture_assert(torture,
137 test_ndr_pull_string (torture, ascii, fl_ascii_null,
138 NDR_ERR_SUCCESS, true),
139 "test_ndr_pull_string(ASCII, STR_ASCII|STR_NULL)");
140 torture_assert(torture,
141 test_ndr_pull_string (torture, utf8, fl_utf8_null,
142 NDR_ERR_SUCCESS, true),
143 "test_ndr_pull_string(UTF8, STR_UTF8|STR_NULL)");
144 torture_assert(torture,
145 test_ndr_pull_string (torture, utf8, fl_raw8_null,
146 NDR_ERR_SUCCESS, true),
147 "test_ndr_pull_string(UTF8, STR_RAW8|STR_NULL)");
148 torture_assert(torture,
149 test_ndr_pull_string (torture, latin1, fl_raw8_null,
150 NDR_ERR_SUCCESS, true),
151 "test_ndr_pull_string(LATIN1, STR_RAW8|STR_NULL)");
153 /* Depending on runtime config, the behavior of ndr_pull_string on
154 * incorrect combinations of strings and flags (latin1 with ASCII
155 * flags, for example) may differ; it may return NDR_ERR_CHARCNV, or
156 * it may return NDR_ERR_SUCCESS but with a string that has been
157 * mutilated, depending on the value of "dos charset". We test for
158 * both cases here. */
160 lpcfg_do_global_parameter(torture->lp_ctx, "dos charset", "ASCII");
161 reload_charcnv(torture->lp_ctx);
163 torture_assert(torture,
164 test_ndr_pull_string (torture, latin1, fl_ascii_null,
165 NDR_ERR_CHARCNV, false),
166 "test_ndr_pull_string(LATIN1, STR_ASCII|STR_NULL)");
167 torture_assert(torture,
168 test_ndr_pull_string (torture, utf8, fl_ascii_null,
169 NDR_ERR_CHARCNV, false),
170 "test_ndr_pull_string(UTF8, STR_ASCII|STR_NULL)");
172 lpcfg_do_global_parameter(torture->lp_ctx, "dos charset", "CP850");
173 reload_charcnv(torture->lp_ctx);
175 torture_assert(torture,
176 test_ndr_pull_string (torture, latin1, fl_ascii_null,
177 NDR_ERR_SUCCESS, false),
178 "test_ndr_pull_string(LATIN1, STR_ASCII|STR_NULL)");
179 torture_assert(torture,
180 test_ndr_pull_string (torture, utf8, fl_ascii_null,
181 NDR_ERR_SUCCESS, false),
182 "test_ndr_pull_string(UTF8, STR_ASCII|STR_NULL)");
184 lpcfg_do_global_parameter(torture->lp_ctx, "dos charset", saved_dos_cp);
185 reload_charcnv(torture->lp_ctx);
187 return true;
190 struct torture_suite *ndr_string_suite(TALLOC_CTX *ctx)
192 struct torture_suite *suite = torture_suite_create(ctx, "ndr_string");
194 torture_suite_add_simple_test(suite, "ndr_string", torture_ndr_string);
195 suite->description = talloc_strdup(suite, "NDR - string-conversion focused push/pull tests");
197 return suite;