Line data Source code
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 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;
24 :
25 : static bool
26 12 : 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)
29 : {
30 : TALLOC_CTX *mem_ctx;
31 : struct ndr_push *ndr;
32 : enum ndr_err_code err;
33 :
34 12 : 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 12 : if (exp_ndr_err != NDR_ERR_SUCCESS) {
39 4 : torture_comment(tctx, "(ignore any Conversion error) ");
40 : }
41 :
42 12 : mem_ctx = talloc_named (NULL, 0, "test_ndr_push_string");
43 12 : ndr = talloc_zero (mem_ctx, struct ndr_push);
44 12 : ndr_set_flags (&ndr->flags, flags);
45 :
46 12 : err = ndr_push_string (ndr, NDR_SCALARS, string);
47 12 : torture_assert(tctx, err == exp_ndr_err,
48 : "ndr_push_string: unexpected return code");
49 :
50 12 : if (exp_ndr_err == NDR_ERR_SUCCESS) {
51 8 : torture_assert(tctx, ndr->data != NULL,
52 : "ndr_push_string: succeeded but NULL data");
53 :
54 8 : torture_assert(tctx,
55 : strcmp_pass == !strcmp(string, (char *)ndr->data),
56 : "ndr_push_string: post-push strcmp");
57 : }
58 :
59 12 : talloc_free(mem_ctx);
60 12 : return true;
61 : }
62 :
63 : static bool
64 16 : 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)
67 : {
68 : TALLOC_CTX *mem_ctx;
69 : DATA_BLOB blob;
70 : struct ndr_pull *ndr;
71 : enum ndr_err_code err;
72 16 : const char *result = NULL;
73 :
74 16 : 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 16 : if (exp_ndr_err != NDR_ERR_SUCCESS) {
79 4 : torture_comment(tctx, "(ignore any Conversion error) ");
80 : }
81 :
82 16 : mem_ctx = talloc_named (NULL, 0, "test_ndr_pull_string");
83 :
84 16 : blob = data_blob_string_const(string);
85 16 : ndr = ndr_pull_init_blob(&blob, mem_ctx);
86 16 : ndr_set_flags (&ndr->flags, flags);
87 :
88 16 : err = ndr_pull_string (ndr, NDR_SCALARS, &result);
89 16 : torture_assert(tctx, err == exp_ndr_err,
90 : "ndr_pull_string: unexpected return code");
91 :
92 16 : if (exp_ndr_err == NDR_ERR_SUCCESS) {
93 12 : torture_assert(tctx, result != NULL,
94 : "ndr_pull_string: NULL data");
95 12 : torture_assert(tctx, strcmp_pass == !strcmp(string, result),
96 : "ndr_pull_string: post-pull strcmp");
97 0 : torture_assert(tctx, result != NULL,
98 : "ndr_pull_string succeeded but result NULL");
99 : }
100 :
101 16 : talloc_free(mem_ctx);
102 16 : return true;
103 : }
104 :
105 : static bool
106 2 : torture_ndr_string(struct torture_context *torture)
107 : {
108 2 : const char *saved_dos_cp = talloc_strdup(torture, lpcfg_dos_charset(torture->lp_ctx));
109 :
110 2 : 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 2 : 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 2 : 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 2 : 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 2 : 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 2 : 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)");
134 :
135 :
136 2 : 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 2 : 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 2 : 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 2 : 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)");
152 :
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. */
159 :
160 2 : lpcfg_do_global_parameter(torture->lp_ctx, "dos charset", "ASCII");
161 2 : reload_charcnv(torture->lp_ctx);
162 :
163 2 : 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 2 : 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)");
171 :
172 2 : lpcfg_do_global_parameter(torture->lp_ctx, "dos charset", "CP850");
173 2 : reload_charcnv(torture->lp_ctx);
174 :
175 2 : 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 2 : 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)");
183 :
184 2 : lpcfg_do_global_parameter(torture->lp_ctx, "dos charset", saved_dos_cp);
185 2 : reload_charcnv(torture->lp_ctx);
186 :
187 2 : return true;
188 : }
189 :
190 2355 : struct torture_suite *ndr_string_suite(TALLOC_CTX *ctx)
191 : {
192 2355 : struct torture_suite *suite = torture_suite_create(ctx, "ndr_string");
193 :
194 2355 : torture_suite_add_simple_test(suite, "ndr_string", torture_ndr_string);
195 2355 : suite->description = talloc_strdup(suite, "NDR - string-conversion focused push/pull tests");
196 :
197 2355 : return suite;
198 : }
|