server: Add ARM64 support.
[wine] / dlls / ntdll / tests / rtlstr.c
1 /* Unit test suite for Rtl string functions
2  *
3  * Copyright 2002 Robert Shearman
4  * Copyright 2003 Thomas Mertes
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * NOTES
21  * We use function pointers here as there is no import library for NTDLL on
22  * windows.
23  */
24
25 #include <stdlib.h>
26
27 #define INITGUID
28
29 #include "ntdll_test.h"
30 #include "winnls.h"
31 #include "guiddef.h"
32
33 /* Function ptrs for ntdll calls */
34 static HMODULE hntdll = 0;
35 static NTSTATUS (WINAPI *pRtlAnsiStringToUnicodeString)(PUNICODE_STRING, PCANSI_STRING, BOOLEAN);
36 static NTSTATUS (WINAPI *pRtlAppendAsciizToString)(STRING *, LPCSTR);
37 static NTSTATUS (WINAPI *pRtlAppendStringToString)(STRING *, const STRING *);
38 static NTSTATUS (WINAPI *pRtlAppendUnicodeStringToString)(UNICODE_STRING *, const UNICODE_STRING *);
39 static NTSTATUS (WINAPI *pRtlAppendUnicodeToString)(UNICODE_STRING *, LPCWSTR);
40 static NTSTATUS (WINAPI *pRtlCharToInteger)(PCSZ, ULONG, int *);
41 static VOID     (WINAPI *pRtlCopyString)(STRING *, const STRING *);
42 static BOOLEAN  (WINAPI *pRtlCreateUnicodeString)(PUNICODE_STRING, LPCWSTR);
43 static BOOLEAN  (WINAPI *pRtlCreateUnicodeStringFromAsciiz)(PUNICODE_STRING, LPCSTR);
44 static NTSTATUS (WINAPI *pRtlDowncaseUnicodeString)(UNICODE_STRING *, const UNICODE_STRING *, BOOLEAN);
45 static NTSTATUS (WINAPI *pRtlDuplicateUnicodeString)(int, UNICODE_STRING *, UNICODE_STRING *);
46 static BOOLEAN  (WINAPI *pRtlEqualUnicodeString)(const UNICODE_STRING *, const UNICODE_STRING *, BOOLEAN);
47 static NTSTATUS (WINAPI *pRtlFindCharInUnicodeString)(int, const UNICODE_STRING *, const UNICODE_STRING *, USHORT *);
48 static VOID     (WINAPI *pRtlFreeAnsiString)(PSTRING);
49 static VOID     (WINAPI *pRtlFreeUnicodeString)(PUNICODE_STRING);
50 static VOID     (WINAPI *pRtlInitAnsiString)(PSTRING, LPCSTR);
51 static VOID     (WINAPI *pRtlInitString)(PSTRING, LPCSTR);
52 static VOID     (WINAPI *pRtlInitUnicodeString)(PUNICODE_STRING, LPCWSTR);
53 static NTSTATUS (WINAPI *pRtlInitUnicodeStringEx)(PUNICODE_STRING, LPCWSTR);
54 static NTSTATUS (WINAPI *pRtlIntegerToChar)(ULONG, ULONG, ULONG, PCHAR);
55 static NTSTATUS (WINAPI *pRtlIntegerToUnicodeString)(ULONG, ULONG, UNICODE_STRING *);
56 static NTSTATUS (WINAPI *pRtlMultiAppendUnicodeStringBuffer)(UNICODE_STRING *, LONG, UNICODE_STRING *);
57 static NTSTATUS (WINAPI *pRtlUnicodeStringToAnsiString)(STRING *, const UNICODE_STRING *, BOOLEAN);
58 static NTSTATUS (WINAPI *pRtlUnicodeStringToInteger)(const UNICODE_STRING *, int, int *);
59 static WCHAR    (WINAPI *pRtlUpcaseUnicodeChar)(WCHAR);
60 static NTSTATUS (WINAPI *pRtlUpcaseUnicodeString)(UNICODE_STRING *, const UNICODE_STRING *, BOOLEAN);
61 static CHAR     (WINAPI *pRtlUpperChar)(CHAR);
62 static NTSTATUS (WINAPI *pRtlUpperString)(STRING *, const STRING *);
63 static NTSTATUS (WINAPI *pRtlValidateUnicodeString)(LONG, UNICODE_STRING *);
64 static NTSTATUS (WINAPI *pRtlGUIDFromString)(const UNICODE_STRING*,GUID*);
65 static NTSTATUS (WINAPI *pRtlStringFromGUID)(const GUID*, UNICODE_STRING*);
66 static BOOLEAN (WINAPI *pRtlIsTextUnicode)(LPVOID, INT, INT *);
67
68 /*static VOID (WINAPI *pRtlFreeOemString)(PSTRING);*/
69 /*static VOID (WINAPI *pRtlCopyUnicodeString)(UNICODE_STRING *, const UNICODE_STRING *);*/
70 /*static VOID (WINAPI *pRtlEraseUnicodeString)(UNICODE_STRING *);*/
71 /*static LONG (WINAPI *pRtlCompareString)(const STRING *,const STRING *,BOOLEAN);*/
72 /*static LONG (WINAPI *pRtlCompareUnicodeString)(const UNICODE_STRING *,const UNICODE_STRING *,BOOLEAN);*/
73 /*static BOOLEAN (WINAPI *pRtlEqualString)(const STRING *,const STRING *,BOOLEAN);*/
74 /*static BOOLEAN (WINAPI *pRtlPrefixString)(const STRING *, const STRING *, BOOLEAN);*/
75 /*static BOOLEAN (WINAPI *pRtlPrefixUnicodeString)(const UNICODE_STRING *, const UNICODE_STRING *, BOOLEAN);*/
76 /*static NTSTATUS (WINAPI *pRtlOemStringToUnicodeString)(PUNICODE_STRING, const STRING *, BOOLEAN);*/
77 /*static NTSTATUS (WINAPI *pRtlUnicodeStringToOemString)(STRING *, const UNICODE_STRING *, BOOLEAN);*/
78 /*static NTSTATUS (WINAPI *pRtlMultiByteToUnicodeN)(LPWSTR, DWORD, LPDWORD, LPCSTR, DWORD);*/
79 /*static NTSTATUS (WINAPI *pRtlOemToUnicodeN)(LPWSTR, DWORD, LPDWORD, LPCSTR, DWORD);*/
80 /*static NTSTATUS (WINAPI *pRtlUpcaseUnicodeStringToAnsiString)(STRING *, const UNICODE_STRING *, BOOLEAN);*/
81 /*static NTSTATUS (WINAPI *pRtlUpcaseUnicodeStringToOemString)(STRING *, const UNICODE_STRING *, BOOLEAN);*/
82 /*static NTSTATUS (WINAPI *pRtlUpcaseUnicodeToMultiByteN)(LPSTR, DWORD, LPDWORD, LPCWSTR, DWORD);*/
83 /*static NTSTATUS (WINAPI *pRtlUpcaseUnicodeToOemN)(LPSTR, DWORD, LPDWORD, LPCWSTR, DWORD);*/
84 /*static UINT (WINAPI *pRtlOemToUnicodeSize)(const STRING *);*/
85 /*static DWORD (WINAPI *pRtlAnsiStringToUnicodeSize)(const STRING *);*/
86
87
88 static WCHAR* AtoW( const char* p )
89 {
90     WCHAR* buffer;
91     DWORD len = MultiByteToWideChar( CP_ACP, 0, p, -1, NULL, 0 );
92     buffer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
93     MultiByteToWideChar( CP_ACP, 0, p, -1, buffer, len );
94     return buffer;
95 }
96
97
98 static void InitFunctionPtrs(void)
99 {
100     hntdll = LoadLibraryA("ntdll.dll");
101     ok(hntdll != 0, "LoadLibrary failed\n");
102     if (hntdll) {
103         pRtlAnsiStringToUnicodeString = (void *)GetProcAddress(hntdll, "RtlAnsiStringToUnicodeString");
104         pRtlAppendAsciizToString = (void *)GetProcAddress(hntdll, "RtlAppendAsciizToString");
105         pRtlAppendStringToString = (void *)GetProcAddress(hntdll, "RtlAppendStringToString");
106         pRtlAppendUnicodeStringToString = (void *)GetProcAddress(hntdll, "RtlAppendUnicodeStringToString");
107         pRtlAppendUnicodeToString = (void *)GetProcAddress(hntdll, "RtlAppendUnicodeToString");
108         pRtlCharToInteger = (void *)GetProcAddress(hntdll, "RtlCharToInteger");
109         pRtlCopyString = (void *)GetProcAddress(hntdll, "RtlCopyString");
110         pRtlCreateUnicodeString = (void *)GetProcAddress(hntdll, "RtlCreateUnicodeString");
111         pRtlCreateUnicodeStringFromAsciiz = (void *)GetProcAddress(hntdll, "RtlCreateUnicodeStringFromAsciiz");
112         pRtlDowncaseUnicodeString = (void *)GetProcAddress(hntdll, "RtlDowncaseUnicodeString");
113         pRtlDuplicateUnicodeString = (void *)GetProcAddress(hntdll, "RtlDuplicateUnicodeString");
114         pRtlEqualUnicodeString = (void *)GetProcAddress(hntdll, "RtlEqualUnicodeString");
115         pRtlFindCharInUnicodeString = (void *)GetProcAddress(hntdll, "RtlFindCharInUnicodeString");
116         pRtlFreeAnsiString = (void *)GetProcAddress(hntdll, "RtlFreeAnsiString");
117         pRtlFreeUnicodeString = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
118         pRtlInitAnsiString = (void *)GetProcAddress(hntdll, "RtlInitAnsiString");
119         pRtlInitString = (void *)GetProcAddress(hntdll, "RtlInitString");
120         pRtlInitUnicodeString = (void *)GetProcAddress(hntdll, "RtlInitUnicodeString");
121         pRtlInitUnicodeStringEx = (void *)GetProcAddress(hntdll, "RtlInitUnicodeStringEx");
122         pRtlIntegerToChar = (void *)GetProcAddress(hntdll, "RtlIntegerToChar");
123         pRtlIntegerToUnicodeString = (void *)GetProcAddress(hntdll, "RtlIntegerToUnicodeString");
124         pRtlMultiAppendUnicodeStringBuffer = (void *)GetProcAddress(hntdll, "RtlMultiAppendUnicodeStringBuffer");
125         pRtlUnicodeStringToAnsiString = (void *)GetProcAddress(hntdll, "RtlUnicodeStringToAnsiString");
126         pRtlUnicodeStringToInteger = (void *)GetProcAddress(hntdll, "RtlUnicodeStringToInteger");
127         pRtlUpcaseUnicodeChar = (void *)GetProcAddress(hntdll, "RtlUpcaseUnicodeChar");
128         pRtlUpcaseUnicodeString = (void *)GetProcAddress(hntdll, "RtlUpcaseUnicodeString");
129         pRtlUpperChar = (void *)GetProcAddress(hntdll, "RtlUpperChar");
130         pRtlUpperString = (void *)GetProcAddress(hntdll, "RtlUpperString");
131         pRtlValidateUnicodeString = (void *)GetProcAddress(hntdll, "RtlValidateUnicodeString");
132         pRtlGUIDFromString = (void *)GetProcAddress(hntdll, "RtlGUIDFromString");
133         pRtlStringFromGUID = (void *)GetProcAddress(hntdll, "RtlStringFromGUID");
134         pRtlIsTextUnicode = (void *)GetProcAddress(hntdll, "RtlIsTextUnicode");
135     }
136 }
137
138
139 static void test_RtlInitString(void)
140 {
141     static const char teststring[] = "Some Wild String";
142     STRING str;
143
144     str.Length = 0;
145     str.MaximumLength = 0;
146     str.Buffer = (void *)0xdeadbeef;
147     pRtlInitString(&str, teststring);
148     ok(str.Length == sizeof(teststring) - sizeof(char), "Length uninitialized\n");
149     ok(str.MaximumLength == sizeof(teststring), "MaximumLength uninitialized\n");
150     ok(str.Buffer == teststring, "Buffer not equal to teststring\n");
151     ok(strcmp(str.Buffer, "Some Wild String") == 0, "Buffer written to\n");
152     pRtlInitString(&str, NULL);
153     ok(str.Length == 0, "Length uninitialized\n");
154     ok(str.MaximumLength == 0, "MaximumLength uninitialized\n");
155     ok(str.Buffer == NULL, "Buffer not equal to NULL\n");
156 /*  pRtlInitString(NULL, teststring); */
157 }
158
159
160 static void test_RtlInitUnicodeString(void)
161 {
162 #define STRINGW {'S','o','m','e',' ','W','i','l','d',' ','S','t','r','i','n','g',0}
163     static const WCHAR teststring[] = STRINGW;
164     static const WCHAR originalstring[] = STRINGW;
165 #undef STRINGW
166     UNICODE_STRING uni;
167
168     uni.Length = 0;
169     uni.MaximumLength = 0;
170     uni.Buffer = (void *)0xdeadbeef;
171     pRtlInitUnicodeString(&uni, teststring);
172     ok(uni.Length == sizeof(teststring) - sizeof(WCHAR), "Length uninitialized\n");
173     ok(uni.MaximumLength == sizeof(teststring), "MaximumLength uninitialized\n");
174     ok(uni.Buffer == teststring, "Buffer not equal to teststring\n");
175     ok(lstrcmpW(uni.Buffer, originalstring) == 0, "Buffer written to\n");
176     pRtlInitUnicodeString(&uni, NULL);
177     ok(uni.Length == 0, "Length uninitialized\n");
178     ok(uni.MaximumLength == 0, "MaximumLength uninitialized\n");
179     ok(uni.Buffer == NULL, "Buffer not equal to NULL\n");
180 /*  pRtlInitUnicodeString(NULL, teststring); */
181 }
182
183
184 #define TESTSTRING2_LEN 1000000
185 /* #define TESTSTRING2_LEN 32766 */
186
187
188 static void test_RtlInitUnicodeStringEx(void)
189 {
190     static const WCHAR teststring[] = {'S','o','m','e',' ','W','i','l','d',' ','S','t','r','i','n','g',0};
191     WCHAR *teststring2;
192     UNICODE_STRING uni;
193     NTSTATUS result;
194
195     teststring2 = HeapAlloc(GetProcessHeap(), 0, (TESTSTRING2_LEN + 1) * sizeof(WCHAR));
196     memset(teststring2, 'X', TESTSTRING2_LEN * sizeof(WCHAR));
197     teststring2[TESTSTRING2_LEN] = '\0';
198
199     uni.Length = 12345;
200     uni.MaximumLength = 12345;
201     uni.Buffer = (void *) 0xdeadbeef;
202     result = pRtlInitUnicodeStringEx(&uni, teststring);
203     ok(result == STATUS_SUCCESS,
204        "pRtlInitUnicodeStringEx(&uni, 0) returns %x, expected 0\n",
205        result);
206     ok(uni.Length == 32,
207        "pRtlInitUnicodeStringEx(&uni, 0) sets Length to %u, expected %u\n",
208        uni.Length, 32);
209     ok(uni.MaximumLength == 34,
210        "pRtlInitUnicodeStringEx(&uni, 0) sets MaximumLength to %u, expected %u\n",
211        uni.MaximumLength, 34);
212     ok(uni.Buffer == teststring,
213        "pRtlInitUnicodeStringEx(&uni, 0) sets Buffer to %p, expected %p\n",
214        uni.Buffer, teststring);
215
216     uni.Length = 12345;
217     uni.MaximumLength = 12345;
218     uni.Buffer = (void *) 0xdeadbeef;
219     pRtlInitUnicodeString(&uni, teststring);
220     ok(uni.Length == 32,
221        "pRtlInitUnicodeString(&uni, 0) sets Length to %u, expected %u\n",
222        uni.Length, 32);
223     ok(uni.MaximumLength == 34,
224        "pRtlInitUnicodeString(&uni, 0) sets MaximumLength to %u, expected %u\n",
225        uni.MaximumLength, 34);
226     ok(uni.Buffer == teststring,
227        "pRtlInitUnicodeString(&uni, 0) sets Buffer to %p, expected %p\n",
228        uni.Buffer, teststring);
229
230     uni.Length = 12345;
231     uni.MaximumLength = 12345;
232     uni.Buffer = (void *) 0xdeadbeef;
233     result = pRtlInitUnicodeStringEx(&uni, teststring2);
234     ok(result == STATUS_NAME_TOO_LONG,
235        "pRtlInitUnicodeStringEx(&uni, 0) returns %x, expected %x\n",
236        result, STATUS_NAME_TOO_LONG);
237     ok(uni.Length == 12345 ||
238        uni.Length == 0, /* win2k3 */
239        "pRtlInitUnicodeStringEx(&uni, 0) sets Length to %u, expected 12345 or 0\n",
240        uni.Length);
241     ok(uni.MaximumLength == 12345 ||
242        uni.MaximumLength == 0, /* win2k3 */
243        "pRtlInitUnicodeStringEx(&uni, 0) sets MaximumLength to %u, expected 12345 or 0\n",
244        uni.MaximumLength);
245     ok(uni.Buffer == (void *) 0xdeadbeef ||
246        uni.Buffer == teststring2, /* win2k3 */
247        "pRtlInitUnicodeStringEx(&uni, 0) sets Buffer to %p, expected %x or %p\n",
248        uni.Buffer, 0xdeadbeef, teststring2);
249
250     uni.Length = 12345;
251     uni.MaximumLength = 12345;
252     uni.Buffer = (void *) 0xdeadbeef;
253     pRtlInitUnicodeString(&uni, teststring2);
254     ok(uni.Length == 33920 /* <= Win2000 */ || uni.Length == 65532 /* >= Win XP */,
255        "pRtlInitUnicodeString(&uni, 0) sets Length to %u, expected %u\n",
256        uni.Length, 65532);
257     ok(uni.MaximumLength == 33922 /* <= Win2000 */ || uni.MaximumLength == 65534 /* >= Win XP */,
258        "pRtlInitUnicodeString(&uni, 0) sets MaximumLength to %u, expected %u\n",
259        uni.MaximumLength, 65534);
260     ok(uni.Buffer == teststring2,
261        "pRtlInitUnicodeString(&uni, 0) sets Buffer to %p, expected %p\n",
262        uni.Buffer, teststring2);
263     ok(memcmp(uni.Buffer, teststring2, (TESTSTRING2_LEN + 1) * sizeof(WCHAR)) == 0,
264        "pRtlInitUnicodeString(&uni, 0) changes Buffer\n");
265
266     uni.Length = 12345;
267     uni.MaximumLength = 12345;
268     uni.Buffer = (void *) 0xdeadbeef;
269     result = pRtlInitUnicodeStringEx(&uni, 0);
270     ok(result == STATUS_SUCCESS,
271        "pRtlInitUnicodeStringEx(&uni, 0) returns %x, expected 0\n",
272        result);
273     ok(uni.Length == 0,
274        "pRtlInitUnicodeStringEx(&uni, 0) sets Length to %u, expected %u\n",
275        uni.Length, 0);
276     ok(uni.MaximumLength == 0,
277        "pRtlInitUnicodeStringEx(&uni, 0) sets MaximumLength to %u, expected %u\n",
278        uni.MaximumLength, 0);
279     ok(uni.Buffer == NULL,
280        "pRtlInitUnicodeStringEx(&uni, 0) sets Buffer to %p, expected %p\n",
281        uni.Buffer, NULL);
282
283     uni.Length = 12345;
284     uni.MaximumLength = 12345;
285     uni.Buffer = (void *) 0xdeadbeef;
286     pRtlInitUnicodeString(&uni, 0);
287     ok(uni.Length == 0,
288        "pRtlInitUnicodeString(&uni, 0) sets Length to %u, expected %u\n",
289        uni.Length, 0);
290     ok(uni.MaximumLength == 0,
291        "pRtlInitUnicodeString(&uni, 0) sets MaximumLength to %u, expected %u\n",
292        uni.MaximumLength, 0);
293     ok(uni.Buffer == NULL,
294        "pRtlInitUnicodeString(&uni, 0) sets Buffer to %p, expected %p\n",
295        uni.Buffer, NULL);
296
297     HeapFree(GetProcessHeap(), 0, teststring2);
298 }
299
300
301 typedef struct {
302     int add_nul;
303     int source_Length;
304     int source_MaximumLength;
305     int source_buf_size;
306     const char *source_buf;
307     int dest_Length;
308     int dest_MaximumLength;
309     int dest_buf_size;
310     const char *dest_buf;
311     int res_Length;
312     int res_MaximumLength;
313     int res_buf_size;
314     const char *res_buf;
315     NTSTATUS result;
316 } dupl_ustr_t;
317
318 static const dupl_ustr_t dupl_ustr[] = {
319     { 0, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 32, 32, 32, "This is a string",     STATUS_SUCCESS},
320     { 0, 32, 32, 32, "This is a string", 40, 42, 42, "--------------------", 32, 32, 32, "This is a string",     STATUS_SUCCESS},
321     { 0, 32, 30, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
322     { 0, 32, 34, 34, "This is a string", 40, 42, 42, NULL,                   32, 32, 32, "This is a string",     STATUS_SUCCESS},
323     { 0, 32, 32, 32, "This is a string", 40, 42, 42, NULL,                   32, 32, 32, "This is a string",     STATUS_SUCCESS},
324     { 0, 32, 30, 34, "This is a string", 40, 42, 42, NULL,                   40, 42,  0, NULL,                   STATUS_INVALID_PARAMETER},
325     { 1, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 32, 34, 34, "This is a string",     STATUS_SUCCESS},
326     { 1, 32, 32, 32, "This is a string", 40, 42, 42, "--------------------", 32, 34, 34, "This is a string",     STATUS_SUCCESS},
327     { 1, 32, 30, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
328     { 1, 32, 34, 34, "This is a string", 40, 42, 42, NULL,                   32, 34, 34, "This is a string",     STATUS_SUCCESS},
329     { 1, 32, 32, 32, "This is a string", 40, 42, 42, NULL,                   32, 34, 34, "This is a string",     STATUS_SUCCESS},
330     { 1, 32, 30, 34, "This is a string", 40, 42, 42, NULL,                   40, 42,  0, NULL,                   STATUS_INVALID_PARAMETER},
331     { 2, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
332     { 2, 32, 32, 32, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
333     { 2, 32, 30, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
334     { 2, 32, 34, 34, "This is a string", 40, 42, 42, NULL,                   40, 42,  0, NULL,                   STATUS_INVALID_PARAMETER},
335     { 2, 32, 32, 32, "This is a string", 40, 42, 42, NULL,                   40, 42,  0, NULL,                   STATUS_INVALID_PARAMETER},
336     { 2, 32, 30, 34, "This is a string", 40, 42, 42, NULL,                   40, 42,  0, NULL,                   STATUS_INVALID_PARAMETER},
337     { 3, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 32, 34, 34, "This is a string",     STATUS_SUCCESS},
338     { 3, 32, 32, 32, "This is a string", 40, 42, 42, "--------------------", 32, 34, 34, "This is a string",     STATUS_SUCCESS},
339     { 3, 32, 30, 32, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
340     { 3, 32, 34, 34, "This is a string", 40, 42, 42, NULL,                   32, 34, 34, "This is a string",     STATUS_SUCCESS},
341     { 3, 32, 32, 32, "This is a string", 40, 42, 42, NULL,                   32, 34, 34, "This is a string",     STATUS_SUCCESS},
342     { 3, 32, 30, 32, "This is a string", 40, 42, 42, NULL,                   40, 42,  0, NULL,                   STATUS_INVALID_PARAMETER},
343     { 4, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
344     { 5, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
345     { 6, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
346     { 7, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
347     { 8, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
348     { 9, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
349     {10, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
350     {11, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
351     {12, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
352     {13, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
353     {14, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
354     {15, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
355     {16, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
356     {-1, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
357     {-5, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
358     {-9, 32, 34, 34, "This is a string", 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
359     { 0,  0,  2,  2, "",                 40, 42, 42, "--------------------",  0,  0,  0, NULL,                   STATUS_SUCCESS},
360     { 0,  0,  0,  0, "",                 40, 42, 42, "--------------------",  0,  0,  0, NULL,                   STATUS_SUCCESS},
361     { 0,  0,  2,  2, "",                 40, 42, 42, NULL,                    0,  0,  0, NULL,                   STATUS_SUCCESS},
362     { 0,  0,  0,  0, "",                 40, 42, 42, NULL,                    0,  0,  0, NULL,                   STATUS_SUCCESS},
363     { 0,  0,  2,  2, NULL,               40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
364     { 0,  0,  0,  0, NULL,               40, 42, 42, "--------------------",  0,  0,  0, NULL,                   STATUS_SUCCESS},
365     { 0,  0,  2,  2, NULL,               40, 42, 42, NULL,                   40, 42,  0, NULL,                   STATUS_INVALID_PARAMETER},
366     { 0,  0,  0,  0, NULL,               40, 42, 42, NULL,                    0,  0,  0, NULL,                   STATUS_SUCCESS},
367     { 1,  0,  2,  2, "",                 40, 42, 42, "--------------------",  0,  0,  0, NULL,                   STATUS_SUCCESS},
368     { 1,  0,  0,  0, "",                 40, 42, 42, "--------------------",  0,  0,  0, NULL,                   STATUS_SUCCESS},
369     { 1,  0,  2,  2, "",                 40, 42, 42, NULL,                    0,  0,  0, NULL,                   STATUS_SUCCESS},
370     { 1,  0,  0,  0, "",                 40, 42, 42, NULL,                    0,  0,  0, NULL,                   STATUS_SUCCESS},
371     { 1,  0,  2,  2, NULL,               40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
372     { 1,  0,  0,  0, NULL,               40, 42, 42, "--------------------",  0,  0,  0, NULL,                   STATUS_SUCCESS},
373     { 1,  0,  2,  2, NULL,               40, 42, 42, NULL,                   40, 42,  0, NULL,                   STATUS_INVALID_PARAMETER},
374     { 1,  0,  0,  0, NULL,               40, 42, 42, NULL,                    0,  0,  0, NULL,                   STATUS_SUCCESS},
375     { 2,  0,  2,  2, "",                 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
376     { 2,  0,  0,  0, "",                 40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
377     { 2,  0,  2,  2, "",                 40, 42, 42, NULL,                   40, 42,  0, NULL,                   STATUS_INVALID_PARAMETER},
378     { 2,  0,  0,  0, "",                 40, 42, 42, NULL,                   40, 42,  0, NULL,                   STATUS_INVALID_PARAMETER},
379     { 2,  0,  2,  2, NULL,               40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
380     { 2,  0,  0,  0, NULL,               40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
381     { 2,  0,  2,  2, NULL,               40, 42, 42, NULL,                   40, 42,  0, NULL,                   STATUS_INVALID_PARAMETER},
382     { 2,  0,  0,  0, NULL,               40, 42, 42, NULL,                   40, 42,  0, NULL,                   STATUS_INVALID_PARAMETER},
383     { 3,  0,  2,  2, "",                 40, 42, 42, "--------------------",  0,  2,  2, "",                     STATUS_SUCCESS},
384     { 3,  0,  0,  0, "",                 40, 42, 42, "--------------------",  0,  2,  2, "",                     STATUS_SUCCESS},
385     { 3,  0,  2,  2, "",                 40, 42, 42, NULL,                    0,  2,  2, "",                     STATUS_SUCCESS},
386     { 3,  0,  0,  0, "",                 40, 42, 42, NULL,                    0,  2,  2, "",                     STATUS_SUCCESS},
387     { 3,  0,  2,  2, NULL,               40, 42, 42, "--------------------", 40, 42, 42, "--------------------", STATUS_INVALID_PARAMETER},
388     { 3,  0,  0,  0, NULL,               40, 42, 42, "--------------------",  0,  2,  2, "",                     STATUS_SUCCESS},
389     { 3,  0,  2,  2, NULL,               40, 42, 42, NULL,                   40, 42,  0, NULL,                   STATUS_INVALID_PARAMETER},
390     { 3,  0,  0,  0, NULL,               40, 42, 42, NULL,                    0,  2,  2, "",                     STATUS_SUCCESS},
391 };
392 #define NB_DUPL_USTR (sizeof(dupl_ustr)/sizeof(*dupl_ustr))
393
394
395 static void test_RtlDuplicateUnicodeString(void)
396 {
397     size_t pos;
398     WCHAR source_buf[257];
399     WCHAR dest_buf[257];
400     WCHAR res_buf[257];
401     UNICODE_STRING source_str;
402     UNICODE_STRING dest_str;
403     UNICODE_STRING res_str;
404     CHAR dest_ansi_buf[257];
405     STRING dest_ansi_str;
406     NTSTATUS result;
407     unsigned int test_num;
408
409     for (test_num = 0; test_num < NB_DUPL_USTR; test_num++) {
410         source_str.Length        = dupl_ustr[test_num].source_Length;
411         source_str.MaximumLength = dupl_ustr[test_num].source_MaximumLength;
412         if (dupl_ustr[test_num].source_buf != NULL) {
413             for (pos = 0; pos < dupl_ustr[test_num].source_buf_size/sizeof(WCHAR); pos++) {
414                 source_buf[pos] = dupl_ustr[test_num].source_buf[pos];
415             }
416             source_str.Buffer = source_buf;
417         } else {
418             source_str.Buffer = NULL;
419         }
420         dest_str.Length        = dupl_ustr[test_num].dest_Length;
421         dest_str.MaximumLength = dupl_ustr[test_num].dest_MaximumLength;
422         if (dupl_ustr[test_num].dest_buf != NULL) {
423             for (pos = 0; pos < dupl_ustr[test_num].dest_buf_size/sizeof(WCHAR); pos++) {
424                 dest_buf[pos] = dupl_ustr[test_num].dest_buf[pos];
425             }
426             dest_str.Buffer = dest_buf;
427         } else {
428             dest_str.Buffer = NULL;
429         }
430         res_str.Length        = dupl_ustr[test_num].res_Length;
431         res_str.MaximumLength = dupl_ustr[test_num].res_MaximumLength;
432         if (dupl_ustr[test_num].res_buf != NULL) {
433             for (pos = 0; pos < dupl_ustr[test_num].res_buf_size/sizeof(WCHAR); pos++) {
434                 res_buf[pos] = dupl_ustr[test_num].res_buf[pos];
435             }
436             res_str.Buffer = res_buf;
437         } else {
438             res_str.Buffer = NULL;
439         }
440         result = pRtlDuplicateUnicodeString(dupl_ustr[test_num].add_nul, &source_str, &dest_str);
441         dest_ansi_str.Length = dest_str.Length / sizeof(WCHAR);
442         dest_ansi_str.MaximumLength = dest_ansi_str.Length + 1;
443         for (pos = 0; pos < dest_ansi_str.Length; pos++) {
444             dest_ansi_buf[pos] = (char)dest_buf[pos];
445         }
446         dest_ansi_buf[dest_ansi_str.Length] = '\0';
447         dest_ansi_str.Buffer = dest_ansi_buf;
448         ok(result == dupl_ustr[test_num].result,
449            "(test %d): RtlDuplicateUnicodeString(%d, source, dest) has result %x, expected %x\n",
450            test_num, dupl_ustr[test_num].add_nul, result, dupl_ustr[test_num].result);
451         ok(dest_str.Length == dupl_ustr[test_num].res_Length,
452            "(test %d): RtlDuplicateUnicodeString(%d, source, dest) destination has Length %d, expected %d\n",
453            test_num, dupl_ustr[test_num].add_nul, dest_str.Length, dupl_ustr[test_num].res_Length);
454         ok(dest_str.MaximumLength == dupl_ustr[test_num].res_MaximumLength,
455            "(test %d): RtlDuplicateUnicodeString(%d, source, dest) destination has MaximumLength %d, expected %d\n",
456            test_num, dupl_ustr[test_num].add_nul, dest_str.MaximumLength, dupl_ustr[test_num].res_MaximumLength);
457         if (result == STATUS_INVALID_PARAMETER) {
458             ok((dest_str.Buffer == NULL && res_str.Buffer == NULL) ||
459                dest_str.Buffer == dest_buf,
460                "(test %d): RtlDuplicateUnicodeString(%d, source, dest) destination buffer changed %p expected %p\n",
461                test_num, dupl_ustr[test_num].add_nul, dest_str.Buffer, dest_buf);
462         } else {
463             ok(dest_str.Buffer != dest_buf,
464                "(test %d): RtlDuplicateUnicodeString(%d, source, dest) has destination buffer unchanged %p\n",
465                test_num, dupl_ustr[test_num].add_nul, dest_str.Buffer);
466         }
467         if (dest_str.Buffer != NULL && dupl_ustr[test_num].res_buf != NULL) {
468             ok(memcmp(dest_str.Buffer, res_str.Buffer, dupl_ustr[test_num].res_buf_size) == 0,
469                "(test %d): RtlDuplicateUnicodeString(%d, source, dest) has destination \"%s\" expected \"%s\"\n",
470                test_num, dupl_ustr[test_num].add_nul, dest_ansi_str.Buffer, dupl_ustr[test_num].res_buf);
471             if(result == STATUS_SUCCESS) pRtlFreeUnicodeString(&dest_str);
472         } else {
473             ok(dest_str.Buffer == NULL && dupl_ustr[test_num].res_buf == NULL,
474                "(test %d): RtlDuplicateUnicodeString(%d, source, dest) has destination %p expected %p\n",
475                test_num, dupl_ustr[test_num].add_nul, dest_str.Buffer, dupl_ustr[test_num].res_buf);
476         }
477     }
478 }
479
480
481 static void test_RtlCopyString(void)
482 {
483     static const char teststring[] = "Some Wild String";
484     char deststring[] = "                    ";
485     STRING str;
486     STRING deststr;
487
488     pRtlInitString(&str, teststring);
489     pRtlInitString(&deststr, deststring);
490     pRtlCopyString(&deststr, &str);
491     ok(strncmp(str.Buffer, deststring, str.Length) == 0, "String not copied\n");
492 }
493
494
495 static void test_RtlUpperChar(void)
496 {
497     int ch;
498     int upper_ch;
499     int expected_upper_ch;
500     int byte_ch;
501
502     for (ch = -1; ch <= 1024; ch++) {
503         upper_ch = pRtlUpperChar(ch);
504         byte_ch = ch & 0xff;
505         if (byte_ch >= 'a' && byte_ch <= 'z') {
506             expected_upper_ch = (CHAR) (byte_ch - 'a' + 'A');
507         } else {
508             expected_upper_ch = (CHAR) byte_ch;
509         }
510         ok(upper_ch == expected_upper_ch,
511            "RtlUpperChar('%c'[=0x%x]) has result '%c'[=0x%x], expected '%c'[=0x%x]\n",
512            ch, ch, upper_ch, upper_ch, expected_upper_ch, expected_upper_ch);
513     }
514 }
515
516
517 static void test_RtlUpperString(void)
518 {
519     int i;
520     CHAR ch;
521     CHAR upper_ch;
522     char ascii_buf[257];
523     char result_buf[257];
524     char upper_buf[257];
525     STRING ascii_str;
526     STRING result_str;
527     STRING upper_str;
528
529     for (i = 0; i <= 255; i++) {
530         ch = (CHAR) i;
531         if (ch >= 'a' && ch <= 'z') {
532             upper_ch = ch - 'a' + 'A';
533         } else {
534             upper_ch = ch;
535         }
536         ascii_buf[i] = ch;
537         result_buf[i] = '\0';
538         upper_buf[i] = upper_ch;
539     }
540     ascii_buf[i] = '\0';
541     result_buf[i] = '\0';
542     upper_buf[i] = '\0';
543     ascii_str.Length = 256;
544     ascii_str.MaximumLength = 256;
545     ascii_str.Buffer = ascii_buf;
546     result_str.Length = 256;
547     result_str.MaximumLength = 256;
548     result_str.Buffer = result_buf;
549     upper_str.Length = 256;
550     upper_str.MaximumLength = 256;
551     upper_str.Buffer = upper_buf;
552
553     pRtlUpperString(&result_str, &ascii_str);
554     ok(memcmp(result_str.Buffer, upper_str.Buffer, 256) == 0,
555        "RtlUpperString does not work as expected\n");
556 }
557
558
559 static void test_RtlUpcaseUnicodeChar(void)
560 {
561     int i;
562     WCHAR ch;
563     WCHAR upper_ch;
564     WCHAR expected_upper_ch;
565
566     for (i = 0; i <= 255; i++) {
567         ch = (WCHAR) i;
568         upper_ch = pRtlUpcaseUnicodeChar(ch);
569         if (ch >= 'a' && ch <= 'z') {
570             expected_upper_ch = ch - 'a' + 'A';
571         } else if (ch >= 0xe0 && ch <= 0xfe && ch != 0xf7) {
572             expected_upper_ch = ch - 0x20;
573         } else if (ch == 0xff) {
574             expected_upper_ch = 0x178;
575         } else {
576             expected_upper_ch = ch;
577         }
578         ok(upper_ch == expected_upper_ch,
579            "RtlUpcaseUnicodeChar('%c'[=0x%x]) has result '%c'[=0x%x], expected: '%c'[=0x%x]\n",
580            ch, ch, upper_ch, upper_ch, expected_upper_ch, expected_upper_ch);
581     }
582 }
583
584
585 static void test_RtlUpcaseUnicodeString(void)
586 {
587     int i;
588     WCHAR ch;
589     WCHAR upper_ch;
590     WCHAR ascii_buf[257];
591     WCHAR result_buf[257];
592     WCHAR upper_buf[257];
593     UNICODE_STRING ascii_str;
594     UNICODE_STRING result_str;
595     UNICODE_STRING upper_str;
596
597     for (i = 0; i <= 255; i++) {
598         ch = (WCHAR) i;
599         if (ch >= 'a' && ch <= 'z') {
600             upper_ch = ch - 'a' + 'A';
601         } else if (ch >= 0xe0 && ch <= 0xfe && ch != 0xf7) {
602             upper_ch = ch - 0x20;
603         } else if (ch == 0xff) {
604             upper_ch = 0x178;
605         } else {
606             upper_ch = ch;
607         }
608         ascii_buf[i] = ch;
609         result_buf[i] = '\0';
610         upper_buf[i] = upper_ch;
611     }
612     ascii_buf[i] = '\0';
613     result_buf[i] = '\0';
614     upper_buf[i] = '\0';
615     ascii_str.Length = 512;
616     ascii_str.MaximumLength = 512;
617     ascii_str.Buffer = ascii_buf;
618     result_str.Length = 512;
619     result_str.MaximumLength = 512;
620     result_str.Buffer = result_buf;
621     upper_str.Length = 512;
622     upper_str.MaximumLength = 512;
623     upper_str.Buffer = upper_buf;
624
625     pRtlUpcaseUnicodeString(&result_str, &ascii_str, 0);
626     for (i = 0; i <= 255; i++) {
627         ok(result_str.Buffer[i] == upper_str.Buffer[i],
628            "RtlUpcaseUnicodeString works wrong: '%c'[=0x%x] is converted to '%c'[=0x%x], expected: '%c'[=0x%x]\n",
629            ascii_str.Buffer[i], ascii_str.Buffer[i],
630            result_str.Buffer[i], result_str.Buffer[i],
631            upper_str.Buffer[i], upper_str.Buffer[i]);
632     }
633 }
634
635
636 static void test_RtlDowncaseUnicodeString(void)
637 {
638     int i;
639     WCHAR ch;
640     WCHAR lower_ch;
641     WCHAR source_buf[1025];
642     WCHAR result_buf[1025];
643     WCHAR lower_buf[1025];
644     UNICODE_STRING source_str;
645     UNICODE_STRING result_str;
646     UNICODE_STRING lower_str;
647
648     for (i = 0; i < 1024; i++) {
649         ch = (WCHAR) i;
650         if (ch >= 'A' && ch <= 'Z') {
651             lower_ch = ch - 'A' + 'a';
652         } else if (ch >= 0xc0 && ch <= 0xde && ch != 0xd7) {
653             lower_ch = ch + 0x20;
654         } else if (ch >= 0x391 && ch <= 0x3ab && ch != 0x3a2) {
655             lower_ch = ch + 0x20;
656         } else {
657             switch (ch) {
658                 case 0x178: lower_ch = 0xff; break;
659                 case 0x181: lower_ch = 0x253; break;
660                 case 0x186: lower_ch = 0x254; break;
661                 case 0x189: lower_ch = 0x256; break;
662                 case 0x18a: lower_ch = 0x257; break;
663                 case 0x18e: lower_ch = 0x1dd; break;
664                 case 0x18f: lower_ch = 0x259; break;
665                 case 0x190: lower_ch = 0x25b; break;
666                 case 0x193: lower_ch = 0x260; break;
667                 case 0x194: lower_ch = 0x263; break;
668                 case 0x196: lower_ch = 0x269; break;
669                 case 0x197: lower_ch = 0x268; break;
670                 case 0x19c: lower_ch = 0x26f; break;
671                 case 0x19d: lower_ch = 0x272; break;
672                 case 0x19f: lower_ch = 0x275; break;
673                 case 0x1a9: lower_ch = 0x283; break;
674                 case 0x1ae: lower_ch = 0x288; break;
675                 case 0x1b1: lower_ch = 0x28a; break;
676                 case 0x1b2: lower_ch = 0x28b; break;
677                 case 0x1b7: lower_ch = 0x292; break;
678                 case 0x1c4: lower_ch = 0x1c6; break;
679                 case 0x1c7: lower_ch = 0x1c9; break;
680                 case 0x1ca: lower_ch = 0x1cc; break;
681                 case 0x1f1: lower_ch = 0x1f3; break;
682                 case 0x386: lower_ch = 0x3ac; break;
683                 case 0x388: lower_ch = 0x3ad; break;
684                 case 0x389: lower_ch = 0x3ae; break;
685                 case 0x38a: lower_ch = 0x3af; break;
686                 case 0x38c: lower_ch = 0x3cc; break;
687                 case 0x38e: lower_ch = 0x3cd; break;
688                 case 0x38f: lower_ch = 0x3ce; break;
689                 default: lower_ch = ch; break;
690             } /* switch */
691         }
692         source_buf[i] = ch;
693         result_buf[i] = '\0';
694         lower_buf[i] = lower_ch;
695     }
696     source_buf[i] = '\0';
697     result_buf[i] = '\0';
698     lower_buf[i] = '\0';
699     source_str.Length = 2048;
700     source_str.MaximumLength = 2048;
701     source_str.Buffer = source_buf;
702     result_str.Length = 2048;
703     result_str.MaximumLength = 2048;
704     result_str.Buffer = result_buf;
705     lower_str.Length = 2048;
706     lower_str.MaximumLength = 2048;
707     lower_str.Buffer = lower_buf;
708
709     pRtlDowncaseUnicodeString(&result_str, &source_str, 0);
710     for (i = 0; i <= 1024; i++) {
711         ok(result_str.Buffer[i] == lower_str.Buffer[i] || result_str.Buffer[i] == source_str.Buffer[i] + 1,
712            "RtlDowncaseUnicodeString works wrong: '%c'[=0x%x] is converted to '%c'[=0x%x], expected: '%c'[=0x%x]\n",
713            source_str.Buffer[i], source_str.Buffer[i],
714            result_str.Buffer[i], result_str.Buffer[i],
715            lower_str.Buffer[i], lower_str.Buffer[i]);
716     }
717 }
718
719
720 typedef struct {
721     int ansi_Length;
722     int ansi_MaximumLength;
723     int ansi_buf_size;
724     const char *ansi_buf;
725     int uni_Length;
726     int uni_MaximumLength;
727     int uni_buf_size;
728     const char *uni_buf;
729     BOOLEAN doalloc;
730     int res_Length;
731     int res_MaximumLength;
732     int res_buf_size;
733     const char *res_buf;
734     NTSTATUS result;
735 } ustr2astr_t;
736
737 static const ustr2astr_t ustr2astr[] = {
738     { 10, 12, 12, "------------",  0,  0,  0, "",       TRUE,  0, 1, 1, "",       STATUS_SUCCESS},
739     { 10, 12, 12, "------------", 12, 12, 12, "abcdef", TRUE,  6, 7, 7, "abcdef", STATUS_SUCCESS},
740     {  0,  2, 12, "------------", 12, 12, 12, "abcdef", TRUE,  6, 7, 7, "abcdef", STATUS_SUCCESS},
741     { 10, 12, 12, NULL,           12, 12, 12, "abcdef", TRUE,  6, 7, 7, "abcdef", STATUS_SUCCESS},
742     {  0,  0, 12, "------------", 12, 12, 12, "abcdef", FALSE, 6, 0, 0, "",       STATUS_BUFFER_OVERFLOW},
743     {  0,  1, 12, "------------", 12, 12, 12, "abcdef", FALSE, 0, 1, 1, "",       STATUS_BUFFER_OVERFLOW},
744     {  0,  2, 12, "------------", 12, 12, 12, "abcdef", FALSE, 1, 2, 2, "a",      STATUS_BUFFER_OVERFLOW},
745     {  0,  3, 12, "------------", 12, 12, 12, "abcdef", FALSE, 2, 3, 3, "ab",     STATUS_BUFFER_OVERFLOW},
746     {  0,  5, 12, "------------", 12, 12, 12, "abcdef", FALSE, 4, 5, 5, "abcd",   STATUS_BUFFER_OVERFLOW},
747     {  8,  5, 12, "------------", 12, 12, 12, "abcdef", FALSE, 4, 5, 5, "abcd",   STATUS_BUFFER_OVERFLOW},
748     {  8,  6, 12, "------------", 12, 12, 12, "abcdef", FALSE, 5, 6, 6, "abcde",  STATUS_BUFFER_OVERFLOW},
749     {  8,  7, 12, "------------", 12, 12, 12, "abcdef", FALSE, 6, 7, 7, "abcdef", STATUS_SUCCESS},
750     {  8,  7, 12, "------------",  0, 12, 12,  NULL,    FALSE, 0, 7, 0, "",       STATUS_SUCCESS},
751 #if 0
752     /* crashes on Japanese and Chinese XP */
753     {  0,  0, 12, NULL,           10, 10, 12,  NULL,    FALSE, 5, 0, 0, NULL,     STATUS_BUFFER_OVERFLOW},
754 #endif
755 };
756 #define NB_USTR2ASTR (sizeof(ustr2astr)/sizeof(*ustr2astr))
757
758
759 static void test_RtlUnicodeStringToAnsiString(void)
760 {
761     size_t pos;
762     CHAR ansi_buf[257];
763     WCHAR uni_buf[257];
764     STRING ansi_str;
765     UNICODE_STRING uni_str;
766     NTSTATUS result;
767     unsigned int test_num;
768
769     for (test_num = 0; test_num < NB_USTR2ASTR; test_num++) {
770         ansi_str.Length        = ustr2astr[test_num].ansi_Length;
771         ansi_str.MaximumLength = ustr2astr[test_num].ansi_MaximumLength;
772         if (ustr2astr[test_num].ansi_buf != NULL) {
773             memcpy(ansi_buf, ustr2astr[test_num].ansi_buf, ustr2astr[test_num].ansi_buf_size);
774             ansi_buf[ustr2astr[test_num].ansi_buf_size] = '\0';
775             ansi_str.Buffer = ansi_buf;
776         } else {
777             ansi_str.Buffer = NULL;
778         }
779         uni_str.Length        = ustr2astr[test_num].uni_Length;
780         uni_str.MaximumLength = ustr2astr[test_num].uni_MaximumLength;
781         if (ustr2astr[test_num].uni_buf != NULL) {
782             for (pos = 0; pos < ustr2astr[test_num].uni_buf_size/sizeof(WCHAR); pos++) {
783                 uni_buf[pos] = ustr2astr[test_num].uni_buf[pos];
784             }
785             uni_str.Buffer = uni_buf;
786         } else {
787             uni_str.Buffer = NULL;
788         }
789         result = pRtlUnicodeStringToAnsiString(&ansi_str, &uni_str, ustr2astr[test_num].doalloc);
790         ok(result == ustr2astr[test_num].result,
791            "(test %d): RtlUnicodeStringToAnsiString(ansi, uni, %d) has result %x, expected %x\n",
792            test_num, ustr2astr[test_num].doalloc, result, ustr2astr[test_num].result);
793         ok(ansi_str.Length == ustr2astr[test_num].res_Length,
794            "(test %d): RtlUnicodeStringToAnsiString(ansi, uni, %d) ansi has Length %d, expected %d\n",
795            test_num, ustr2astr[test_num].doalloc, ansi_str.Length, ustr2astr[test_num].res_Length);
796         ok(ansi_str.MaximumLength == ustr2astr[test_num].res_MaximumLength,
797            "(test %d): RtlUnicodeStringToAnsiString(ansi, uni, %d) ansi has MaximumLength %d, expected %d\n",
798            test_num, ustr2astr[test_num].doalloc, ansi_str.MaximumLength, ustr2astr[test_num].res_MaximumLength);
799         ok(memcmp(ansi_str.Buffer, ustr2astr[test_num].res_buf, ustr2astr[test_num].res_buf_size) == 0,
800            "(test %d): RtlUnicodeStringToAnsiString(ansi, uni, %d) has ansi \"%s\" expected \"%s\"\n",
801            test_num, ustr2astr[test_num].doalloc, ansi_str.Buffer, ustr2astr[test_num].res_buf);
802         if(result == STATUS_SUCCESS && ustr2astr[test_num].doalloc)
803             pRtlFreeAnsiString(&ansi_str);
804     }
805 }
806
807
808 typedef struct {
809     int dest_Length;
810     int dest_MaximumLength;
811     int dest_buf_size;
812     const char *dest_buf;
813     const char *src;
814     int res_Length;
815     int res_MaximumLength;
816     int res_buf_size;
817     const char *res_buf;
818     NTSTATUS result;
819 } app_asc2str_t;
820
821 static const app_asc2str_t app_asc2str[] = {
822     { 5, 12, 15,  "TestS01234abcde", "tring", 10, 12, 15,  "TestStringabcde", STATUS_SUCCESS},
823     { 5, 11, 15,  "TestS01234abcde", "tring", 10, 11, 15,  "TestStringabcde", STATUS_SUCCESS},
824     { 5, 10, 15,  "TestS01234abcde", "tring", 10, 10, 15,  "TestStringabcde", STATUS_SUCCESS},
825     { 5,  9, 15,  "TestS01234abcde", "tring",  5,  9, 15,  "TestS01234abcde", STATUS_BUFFER_TOO_SMALL},
826     { 5,  0, 15,  "TestS01234abcde", "tring",  5,  0, 15,  "TestS01234abcde", STATUS_BUFFER_TOO_SMALL},
827     { 5, 14, 15,  "TestS01234abcde", "tring", 10, 14, 15,  "TestStringabcde", STATUS_SUCCESS},
828     { 5, 14, 15,  "TestS01234abcde",    NULL,  5, 14, 15,  "TestS01234abcde", STATUS_SUCCESS},
829     { 5, 14, 15,               NULL,    NULL,  5, 14, 15,               NULL, STATUS_SUCCESS},
830     { 5, 12, 15, "Tst\0S01234abcde", "tr\0i",  7, 12, 15, "Tst\0Str234abcde", STATUS_SUCCESS},
831 };
832 #define NB_APP_ASC2STR (sizeof(app_asc2str)/sizeof(*app_asc2str))
833
834
835 static void test_RtlAppendAsciizToString(void)
836 {
837     CHAR dest_buf[257];
838     STRING dest_str;
839     NTSTATUS result;
840     unsigned int test_num;
841
842     for (test_num = 0; test_num < NB_APP_ASC2STR; test_num++) {
843         dest_str.Length        = app_asc2str[test_num].dest_Length;
844         dest_str.MaximumLength = app_asc2str[test_num].dest_MaximumLength;
845         if (app_asc2str[test_num].dest_buf != NULL) {
846             memcpy(dest_buf, app_asc2str[test_num].dest_buf, app_asc2str[test_num].dest_buf_size);
847             dest_buf[app_asc2str[test_num].dest_buf_size] = '\0';
848             dest_str.Buffer = dest_buf;
849         } else {
850             dest_str.Buffer = NULL;
851         }
852         result = pRtlAppendAsciizToString(&dest_str, app_asc2str[test_num].src);
853         ok(result == app_asc2str[test_num].result,
854            "(test %d): RtlAppendAsciizToString(dest, src) has result %x, expected %x\n",
855            test_num, result, app_asc2str[test_num].result);
856         ok(dest_str.Length == app_asc2str[test_num].res_Length,
857            "(test %d): RtlAppendAsciizToString(dest, src) dest has Length %d, expected %d\n",
858            test_num, dest_str.Length, app_asc2str[test_num].res_Length);
859         ok(dest_str.MaximumLength == app_asc2str[test_num].res_MaximumLength,
860            "(test %d): RtlAppendAsciizToString(dest, src) dest has MaximumLength %d, expected %d\n",
861            test_num, dest_str.MaximumLength, app_asc2str[test_num].res_MaximumLength);
862         if (dest_str.Buffer == dest_buf) {
863             ok(memcmp(dest_buf, app_asc2str[test_num].res_buf, app_asc2str[test_num].res_buf_size) == 0,
864                "(test %d): RtlAppendAsciizToString(dest, src) has dest \"%s\" expected \"%s\"\n",
865                test_num, dest_buf, app_asc2str[test_num].res_buf);
866         } else {
867             ok(dest_str.Buffer == app_asc2str[test_num].res_buf,
868                "(test %d): RtlAppendAsciizToString(dest, src) dest has Buffer %p expected %p\n",
869                test_num, dest_str.Buffer, app_asc2str[test_num].res_buf);
870         }
871     }
872 }
873
874
875 typedef struct {
876     int dest_Length;
877     int dest_MaximumLength;
878     int dest_buf_size;
879     const char *dest_buf;
880     int src_Length;
881     int src_MaximumLength;
882     int src_buf_size;
883     const char *src_buf;
884     int res_Length;
885     int res_MaximumLength;
886     int res_buf_size;
887     const char *res_buf;
888     NTSTATUS result;
889 } app_str2str_t;
890
891 static const app_str2str_t app_str2str[] = {
892     { 5, 12, 15,  "TestS01234abcde", 5, 5, 7, "tringZY", 10, 12, 15,   "TestStringabcde", STATUS_SUCCESS},
893     { 5, 11, 15,  "TestS01234abcde", 5, 5, 7, "tringZY", 10, 11, 15,   "TestStringabcde", STATUS_SUCCESS},
894     { 5, 10, 15,  "TestS01234abcde", 5, 5, 7, "tringZY", 10, 10, 15,   "TestStringabcde", STATUS_SUCCESS},
895     { 5,  9, 15,  "TestS01234abcde", 5, 5, 7, "tringZY",  5,  9, 15,   "TestS01234abcde", STATUS_BUFFER_TOO_SMALL},
896     { 5,  0, 15,  "TestS01234abcde", 0, 0, 7, "tringZY",  5,  0, 15,   "TestS01234abcde", STATUS_SUCCESS},
897     { 5, 14, 15,  "TestS01234abcde", 0, 0, 7, "tringZY",  5, 14, 15,   "TestS01234abcde", STATUS_SUCCESS},
898     { 5, 14, 15,  "TestS01234abcde", 0, 0, 7,      NULL,  5, 14, 15,   "TestS01234abcde", STATUS_SUCCESS},
899     { 5, 14, 15,               NULL, 0, 0, 7,      NULL,  5, 14, 15,                NULL, STATUS_SUCCESS},
900     { 5, 12, 15, "Tst\0S01234abcde", 4, 4, 7, "tr\0iZY",  9, 12, 15, "Tst\0Str\0i4abcde", STATUS_SUCCESS},
901 };
902 #define NB_APP_STR2STR (sizeof(app_str2str)/sizeof(*app_str2str))
903
904
905 static void test_RtlAppendStringToString(void)
906 {
907     CHAR dest_buf[257];
908     CHAR src_buf[257];
909     STRING dest_str;
910     STRING src_str;
911     NTSTATUS result;
912     unsigned int test_num;
913
914     for (test_num = 0; test_num < NB_APP_STR2STR; test_num++) {
915         dest_str.Length        = app_str2str[test_num].dest_Length;
916         dest_str.MaximumLength = app_str2str[test_num].dest_MaximumLength;
917         if (app_str2str[test_num].dest_buf != NULL) {
918             memcpy(dest_buf, app_str2str[test_num].dest_buf, app_str2str[test_num].dest_buf_size);
919             dest_buf[app_str2str[test_num].dest_buf_size] = '\0';
920             dest_str.Buffer = dest_buf;
921         } else {
922             dest_str.Buffer = NULL;
923         }
924         src_str.Length         = app_str2str[test_num].src_Length;
925         src_str.MaximumLength  = app_str2str[test_num].src_MaximumLength;
926         if (app_str2str[test_num].src_buf != NULL) {
927             memcpy(src_buf, app_str2str[test_num].src_buf, app_str2str[test_num].src_buf_size);
928             src_buf[app_str2str[test_num].src_buf_size] = '\0';
929             src_str.Buffer = src_buf;
930         } else {
931             src_str.Buffer = NULL;
932         }
933         result = pRtlAppendStringToString(&dest_str, &src_str);
934         ok(result == app_str2str[test_num].result,
935            "(test %d): RtlAppendStringToString(dest, src) has result %x, expected %x\n",
936            test_num, result, app_str2str[test_num].result);
937         ok(dest_str.Length == app_str2str[test_num].res_Length,
938            "(test %d): RtlAppendStringToString(dest, src) dest has Length %d, expected %d\n",
939            test_num, dest_str.Length, app_str2str[test_num].res_Length);
940         ok(dest_str.MaximumLength == app_str2str[test_num].res_MaximumLength,
941            "(test %d): RtlAppendStringToString(dest, src) dest has MaximumLength %d, expected %d\n",
942            test_num, dest_str.MaximumLength, app_str2str[test_num].res_MaximumLength);
943         if (dest_str.Buffer == dest_buf) {
944             ok(memcmp(dest_buf, app_str2str[test_num].res_buf, app_str2str[test_num].res_buf_size) == 0,
945                "(test %d): RtlAppendStringToString(dest, src) has dest \"%s\" expected \"%s\"\n",
946                test_num, dest_buf, app_str2str[test_num].res_buf);
947         } else {
948             ok(dest_str.Buffer == app_str2str[test_num].res_buf,
949                "(test %d): RtlAppendStringToString(dest, src) dest has Buffer %p expected %p\n",
950                test_num, dest_str.Buffer, app_str2str[test_num].res_buf);
951         }
952     }
953 }
954
955
956 typedef struct {
957     int dest_Length;
958     int dest_MaximumLength;
959     int dest_buf_size;
960     const char *dest_buf;
961     const char *src;
962     int res_Length;
963     int res_MaximumLength;
964     int res_buf_size;
965     const char *res_buf;
966     NTSTATUS result;
967 } app_uni2str_t;
968
969 static const app_uni2str_t app_uni2str[] = {
970     { 4, 12, 14,     "Fake0123abcdef",    "Ustr\0",  8, 12, 14,  "FakeUstr\0\0cdef", STATUS_SUCCESS},
971     { 4, 11, 14,     "Fake0123abcdef",    "Ustr\0",  8, 11, 14,  "FakeUstr\0\0cdef", STATUS_SUCCESS},
972     { 4, 10, 14,     "Fake0123abcdef",    "Ustr\0",  8, 10, 14,  "FakeUstr\0\0cdef", STATUS_SUCCESS},
973 /* In the following test the native function writes beyond MaximumLength
974  *  { 4,  9, 14,     "Fake0123abcdef",    "Ustr\0",  8,  9, 14,    "FakeUstrabcdef", STATUS_SUCCESS},
975  */
976     { 4,  8, 14,     "Fake0123abcdef",    "Ustr\0",  8,  8, 14,    "FakeUstrabcdef", STATUS_SUCCESS},
977     { 4,  7, 14,     "Fake0123abcdef",    "Ustr\0",  4,  7, 14,    "Fake0123abcdef", STATUS_BUFFER_TOO_SMALL},
978     { 4,  0, 14,     "Fake0123abcdef",    "Ustr\0",  4,  0, 14,    "Fake0123abcdef", STATUS_BUFFER_TOO_SMALL},
979     { 4, 14, 14,     "Fake0123abcdef",    "Ustr\0",  8, 14, 14,  "FakeUstr\0\0cdef", STATUS_SUCCESS},
980     { 4, 14, 14,     "Fake0123abcdef",        NULL,  4, 14, 14,    "Fake0123abcdef", STATUS_SUCCESS},
981     { 4, 14, 14,                 NULL,        NULL,  4, 14, 14,                NULL, STATUS_SUCCESS},
982     { 4, 14, 14,     "Fake0123abcdef", "U\0stri\0", 10, 14, 14, "FakeU\0stri\0\0ef", STATUS_SUCCESS},
983     { 6, 14, 16, "Te\0\0stabcdefghij",  "St\0\0ri",  8, 14, 16, "Te\0\0stSt\0\0efghij", STATUS_SUCCESS},
984 };
985 #define NB_APP_UNI2STR (sizeof(app_uni2str)/sizeof(*app_uni2str))
986
987
988 static void test_RtlAppendUnicodeToString(void)
989 {
990     WCHAR dest_buf[257];
991     UNICODE_STRING dest_str;
992     NTSTATUS result;
993     unsigned int test_num;
994
995     for (test_num = 0; test_num < NB_APP_UNI2STR; test_num++) {
996         dest_str.Length        = app_uni2str[test_num].dest_Length;
997         dest_str.MaximumLength = app_uni2str[test_num].dest_MaximumLength;
998         if (app_uni2str[test_num].dest_buf != NULL) {
999             memcpy(dest_buf, app_uni2str[test_num].dest_buf, app_uni2str[test_num].dest_buf_size);
1000             dest_buf[app_uni2str[test_num].dest_buf_size/sizeof(WCHAR)] = '\0';
1001             dest_str.Buffer = dest_buf;
1002         } else {
1003             dest_str.Buffer = NULL;
1004         }
1005         result = pRtlAppendUnicodeToString(&dest_str, (LPCWSTR) app_uni2str[test_num].src);
1006         ok(result == app_uni2str[test_num].result,
1007            "(test %d): RtlAppendUnicodeToString(dest, src) has result %x, expected %x\n",
1008            test_num, result, app_uni2str[test_num].result);
1009         ok(dest_str.Length == app_uni2str[test_num].res_Length,
1010            "(test %d): RtlAppendUnicodeToString(dest, src) dest has Length %d, expected %d\n",
1011            test_num, dest_str.Length, app_uni2str[test_num].res_Length);
1012         ok(dest_str.MaximumLength == app_uni2str[test_num].res_MaximumLength,
1013            "(test %d): RtlAppendUnicodeToString(dest, src) dest has MaximumLength %d, expected %d\n",
1014            test_num, dest_str.MaximumLength, app_uni2str[test_num].res_MaximumLength);
1015         if (dest_str.Buffer == dest_buf) {
1016             ok(memcmp(dest_buf, app_uni2str[test_num].res_buf, app_uni2str[test_num].res_buf_size) == 0,
1017                "(test %d): RtlAppendUnicodeToString(dest, src) has dest \"%s\" expected \"%s\"\n",
1018                test_num, (char *) dest_buf, app_uni2str[test_num].res_buf);
1019         } else {
1020             ok(dest_str.Buffer == (WCHAR *) app_uni2str[test_num].res_buf,
1021                "(test %d): RtlAppendUnicodeToString(dest, src) dest has Buffer %p expected %p\n",
1022                test_num, dest_str.Buffer, app_uni2str[test_num].res_buf);
1023         }
1024     }
1025 }
1026
1027
1028 typedef struct {
1029     int dest_Length;
1030     int dest_MaximumLength;
1031     int dest_buf_size;
1032     const char *dest_buf;
1033     int src_Length;
1034     int src_MaximumLength;
1035     int src_buf_size;
1036     const char *src_buf;
1037     int res_Length;
1038     int res_MaximumLength;
1039     int res_buf_size;
1040     const char *res_buf;
1041     NTSTATUS result;
1042 } app_ustr2str_t;
1043
1044 static const app_ustr2str_t app_ustr2str[] = {
1045     { 4, 12, 14,     "Fake0123abcdef", 4, 6, 8,   "UstrZYXW",  8, 12, 14,   "FakeUstr\0\0cdef", STATUS_SUCCESS},
1046     { 4, 11, 14,     "Fake0123abcdef", 4, 6, 8,   "UstrZYXW",  8, 11, 14,   "FakeUstr\0\0cdef", STATUS_SUCCESS},
1047     { 4, 10, 14,     "Fake0123abcdef", 4, 6, 8,   "UstrZYXW",  8, 10, 14,   "FakeUstr\0\0cdef", STATUS_SUCCESS},
1048 /* In the following test the native function writes beyond MaximumLength 
1049  *  { 4,  9, 14,     "Fake0123abcdef", 4, 6, 8,   "UstrZYXW",  8,  9, 14,     "FakeUstrabcdef", STATUS_SUCCESS},
1050  */
1051     { 4,  8, 14,     "Fake0123abcdef", 4, 6, 8,   "UstrZYXW",  8,  8, 14,     "FakeUstrabcdef", STATUS_SUCCESS},
1052     { 4,  7, 14,     "Fake0123abcdef", 4, 6, 8,   "UstrZYXW",  4,  7, 14,     "Fake0123abcdef", STATUS_BUFFER_TOO_SMALL},
1053     { 4,  0, 14,     "Fake0123abcdef", 0, 0, 8,   "UstrZYXW",  4,  0, 14,     "Fake0123abcdef", STATUS_SUCCESS},
1054     { 4, 14, 14,     "Fake0123abcdef", 0, 0, 8,   "UstrZYXW",  4, 14, 14,     "Fake0123abcdef", STATUS_SUCCESS},
1055     { 4, 14, 14,     "Fake0123abcdef", 0, 0, 8,         NULL,  4, 14, 14,     "Fake0123abcdef", STATUS_SUCCESS},
1056     { 4, 14, 14,                 NULL, 0, 0, 8,         NULL,  4, 14, 14,                 NULL, STATUS_SUCCESS},
1057     { 6, 14, 16, "Te\0\0stabcdefghij", 6, 8, 8, "St\0\0riZY", 12, 14, 16, "Te\0\0stSt\0\0ri\0\0ij", STATUS_SUCCESS},
1058 };
1059 #define NB_APP_USTR2STR (sizeof(app_ustr2str)/sizeof(*app_ustr2str))
1060
1061
1062 static void test_RtlAppendUnicodeStringToString(void)
1063 {
1064     WCHAR dest_buf[257];
1065     WCHAR src_buf[257];
1066     UNICODE_STRING dest_str;
1067     UNICODE_STRING src_str;
1068     NTSTATUS result;
1069     unsigned int test_num;
1070
1071     for (test_num = 0; test_num < NB_APP_USTR2STR; test_num++) {
1072         dest_str.Length        = app_ustr2str[test_num].dest_Length;
1073         dest_str.MaximumLength = app_ustr2str[test_num].dest_MaximumLength;
1074         if (app_ustr2str[test_num].dest_buf != NULL) {
1075             memcpy(dest_buf, app_ustr2str[test_num].dest_buf, app_ustr2str[test_num].dest_buf_size);
1076             dest_buf[app_ustr2str[test_num].dest_buf_size/sizeof(WCHAR)] = '\0';
1077             dest_str.Buffer = dest_buf;
1078         } else {
1079             dest_str.Buffer = NULL;
1080         }
1081         src_str.Length         = app_ustr2str[test_num].src_Length;
1082         src_str.MaximumLength  = app_ustr2str[test_num].src_MaximumLength;
1083         if (app_ustr2str[test_num].src_buf != NULL) {
1084             memcpy(src_buf, app_ustr2str[test_num].src_buf, app_ustr2str[test_num].src_buf_size);
1085             src_buf[app_ustr2str[test_num].src_buf_size/sizeof(WCHAR)] = '\0';
1086             src_str.Buffer = src_buf;
1087         } else {
1088             src_str.Buffer = NULL;
1089         }
1090         result = pRtlAppendUnicodeStringToString(&dest_str, &src_str);
1091         ok(result == app_ustr2str[test_num].result,
1092            "(test %d): RtlAppendStringToString(dest, src) has result %x, expected %x\n",
1093            test_num, result, app_ustr2str[test_num].result);
1094         ok(dest_str.Length == app_ustr2str[test_num].res_Length,
1095            "(test %d): RtlAppendStringToString(dest, src) dest has Length %d, expected %d\n",
1096            test_num, dest_str.Length, app_ustr2str[test_num].res_Length);
1097         ok(dest_str.MaximumLength == app_ustr2str[test_num].res_MaximumLength,
1098            "(test %d): RtlAppendStringToString(dest, src) dest has MaximumLength %d, expected %d\n",
1099            test_num, dest_str.MaximumLength, app_ustr2str[test_num].res_MaximumLength);
1100         if (dest_str.Buffer == dest_buf) {
1101             ok(memcmp(dest_buf, app_ustr2str[test_num].res_buf, app_ustr2str[test_num].res_buf_size) == 0,
1102                "(test %d): RtlAppendStringToString(dest, src) has dest \"%s\" expected \"%s\"\n",
1103                test_num, (char *) dest_buf, app_ustr2str[test_num].res_buf);
1104         } else {
1105             ok(dest_str.Buffer == (WCHAR *) app_ustr2str[test_num].res_buf,
1106                "(test %d): RtlAppendStringToString(dest, src) dest has Buffer %p expected %p\n",
1107                test_num, dest_str.Buffer, app_ustr2str[test_num].res_buf);
1108         }
1109     }
1110 }
1111
1112
1113 typedef struct {
1114     int flags;
1115     const char *main_str;
1116     const char *search_chars;
1117     USHORT pos;
1118     NTSTATUS result;
1119 } find_ch_in_ustr_t;
1120
1121 static const find_ch_in_ustr_t find_ch_in_ustr[] = {
1122     { 0, "Some Wild String",           "S",       2, STATUS_SUCCESS},
1123     { 0, "This is a String",           "String",  6, STATUS_SUCCESS},
1124     { 1, "This is a String",           "String", 30, STATUS_SUCCESS},
1125     { 2, "This is a String",           "String",  2, STATUS_SUCCESS},
1126     { 3, "This is a String",           "String", 18, STATUS_SUCCESS},
1127     { 0, "This is a String",           "Wild",    6, STATUS_SUCCESS},
1128     { 1, "This is a String",           "Wild",   26, STATUS_SUCCESS},
1129     { 2, "This is a String",           "Wild",    2, STATUS_SUCCESS},
1130     { 3, "This is a String",           "Wild",   30, STATUS_SUCCESS},
1131     { 0, "abcdefghijklmnopqrstuvwxyz", "",        0, STATUS_NOT_FOUND},
1132     { 0, "abcdefghijklmnopqrstuvwxyz", "123",     0, STATUS_NOT_FOUND},
1133     { 0, "abcdefghijklmnopqrstuvwxyz", "a",       2, STATUS_SUCCESS},
1134     { 0, "abcdefghijklmnopqrstuvwxyz", "12a34",   2, STATUS_SUCCESS},
1135     { 0, "abcdefghijklmnopqrstuvwxyz", "12b34",   4, STATUS_SUCCESS},
1136     { 0, "abcdefghijklmnopqrstuvwxyz", "12y34",  50, STATUS_SUCCESS},
1137     { 0, "abcdefghijklmnopqrstuvwxyz", "12z34",  52, STATUS_SUCCESS},
1138     { 0, "abcdefghijklmnopqrstuvwxyz", "rvz",    36, STATUS_SUCCESS},
1139     { 0, "abcdefghijklmmlkjihgfedcba", "egik",   10, STATUS_SUCCESS},
1140     { 1, "abcdefghijklmnopqrstuvwxyz", "",        0, STATUS_NOT_FOUND},
1141     { 1, "abcdefghijklmnopqrstuvwxyz", "rvz",    50, STATUS_SUCCESS},
1142     { 1, "abcdefghijklmnopqrstuvwxyz", "ravy",   48, STATUS_SUCCESS},
1143     { 1, "abcdefghijklmnopqrstuvwxyz", "raxv",   46, STATUS_SUCCESS},
1144     { 2, "abcdefghijklmnopqrstuvwxyz", "",        2, STATUS_SUCCESS},
1145     { 2, "abcdefghijklmnopqrstuvwxyz", "rvz",     2, STATUS_SUCCESS},
1146     { 2, "abcdefghijklmnopqrstuvwxyz", "vaz",     4, STATUS_SUCCESS},
1147     { 2, "abcdefghijklmnopqrstuvwxyz", "ravbz",   6, STATUS_SUCCESS},
1148     { 3, "abcdefghijklmnopqrstuvwxyz", "",       50, STATUS_SUCCESS},
1149     { 3, "abcdefghijklmnopqrstuvwxyz", "123",    50, STATUS_SUCCESS},
1150     { 3, "abcdefghijklmnopqrstuvwxyz", "ahp",    50, STATUS_SUCCESS},
1151     { 3, "abcdefghijklmnopqrstuvwxyz", "rvz",    48, STATUS_SUCCESS},
1152     { 0, NULL,                         "abc",     0, STATUS_NOT_FOUND},
1153     { 1, NULL,                         "abc",     0, STATUS_NOT_FOUND},
1154     { 2, NULL,                         "abc",     0, STATUS_NOT_FOUND},
1155     { 3, NULL,                         "abc",     0, STATUS_NOT_FOUND},
1156     { 0, "abcdefghijklmnopqrstuvwxyz", NULL,      0, STATUS_NOT_FOUND},
1157     { 1, "abcdefghijklmnopqrstuvwxyz", NULL,      0, STATUS_NOT_FOUND},
1158     { 2, "abcdefghijklmnopqrstuvwxyz", NULL,      2, STATUS_SUCCESS},
1159     { 3, "abcdefghijklmnopqrstuvwxyz", NULL,     50, STATUS_SUCCESS},
1160     { 0, NULL,                         NULL,      0, STATUS_NOT_FOUND},
1161     { 1, NULL,                         NULL,      0, STATUS_NOT_FOUND},
1162     { 2, NULL,                         NULL,      0, STATUS_NOT_FOUND},
1163     { 3, NULL,                         NULL,      0, STATUS_NOT_FOUND},
1164     { 0, "abcdabcdabcdabcdabcdabcd",   "abcd",    2, STATUS_SUCCESS},
1165     { 1, "abcdabcdabcdabcdabcdabcd",   "abcd",   46, STATUS_SUCCESS},
1166     { 2, "abcdabcdabcdabcdabcdabcd",   "abcd",    0, STATUS_NOT_FOUND},
1167     { 3, "abcdabcdabcdabcdabcdabcd",   "abcd",    0, STATUS_NOT_FOUND},
1168 };
1169 #define NB_FIND_CH_IN_USTR (sizeof(find_ch_in_ustr)/sizeof(*find_ch_in_ustr))
1170
1171
1172 static void test_RtlFindCharInUnicodeString(void)
1173 {
1174     WCHAR main_str_buf[257];
1175     WCHAR search_chars_buf[257];
1176     UNICODE_STRING main_str;
1177     UNICODE_STRING search_chars;
1178     USHORT pos;
1179     NTSTATUS result;
1180     unsigned int idx;
1181     unsigned int test_num;
1182
1183     for (test_num = 0; test_num < NB_FIND_CH_IN_USTR; test_num++) {
1184         if (find_ch_in_ustr[test_num].main_str != NULL) {
1185             main_str.Length        = strlen(find_ch_in_ustr[test_num].main_str) * sizeof(WCHAR);
1186             main_str.MaximumLength = main_str.Length + sizeof(WCHAR);
1187             for (idx = 0; idx < main_str.Length / sizeof(WCHAR); idx++) {
1188                 main_str_buf[idx] = find_ch_in_ustr[test_num].main_str[idx];
1189             }
1190             main_str.Buffer = main_str_buf;
1191         } else {
1192             main_str.Length        = 0;
1193             main_str.MaximumLength = 0;
1194             main_str.Buffer        = NULL;
1195         }
1196         if (find_ch_in_ustr[test_num].search_chars != NULL) {
1197             search_chars.Length        = strlen(find_ch_in_ustr[test_num].search_chars) * sizeof(WCHAR);
1198             search_chars.MaximumLength = search_chars.Length + sizeof(WCHAR);
1199             for (idx = 0; idx < search_chars.Length / sizeof(WCHAR); idx++) {
1200                 search_chars_buf[idx] = find_ch_in_ustr[test_num].search_chars[idx];
1201             }
1202             search_chars.Buffer = search_chars_buf;
1203         } else {
1204             search_chars.Length        = 0;
1205             search_chars.MaximumLength = 0;
1206             search_chars.Buffer        = NULL;
1207         }
1208         pos = 12345;
1209         result = pRtlFindCharInUnicodeString(find_ch_in_ustr[test_num].flags, &main_str, &search_chars, &pos);
1210         ok(result == find_ch_in_ustr[test_num].result,
1211            "(test %d): RtlFindCharInUnicodeString(%d, %s, %s, [out]) has result %x, expected %x\n",
1212            test_num, find_ch_in_ustr[test_num].flags,
1213            find_ch_in_ustr[test_num].main_str, find_ch_in_ustr[test_num].search_chars,
1214            result, find_ch_in_ustr[test_num].result);
1215         ok(pos == find_ch_in_ustr[test_num].pos,
1216            "(test %d): RtlFindCharInUnicodeString(%d, %s, %s, [out]) assigns %d to pos, expected %d\n",
1217            test_num, find_ch_in_ustr[test_num].flags,
1218            find_ch_in_ustr[test_num].main_str, find_ch_in_ustr[test_num].search_chars,
1219            pos, find_ch_in_ustr[test_num].pos);
1220     }
1221 }
1222
1223
1224 typedef struct {
1225     int base;
1226     const char *str;
1227     int value;
1228     NTSTATUS result, alternative;
1229 } str2int_t;
1230
1231 static const str2int_t str2int[] = {
1232     { 0, "1011101100",   1011101100, STATUS_SUCCESS},
1233     { 0, "1234567",         1234567, STATUS_SUCCESS},
1234     { 0, "-214",               -214, STATUS_SUCCESS},
1235     { 0, "+214",                214, STATUS_SUCCESS}, /* The + sign is allowed also */
1236     { 0, "--214",                 0, STATUS_SUCCESS}, /* Do not accept more than one sign */
1237     { 0, "-+214",                 0, STATUS_SUCCESS},
1238     { 0, "++214",                 0, STATUS_SUCCESS},
1239     { 0, "+-214",                 0, STATUS_SUCCESS},
1240     { 0, "\001\002\003\00411",   11, STATUS_SUCCESS}, /* whitespace char  1 to  4 */
1241     { 0, "\005\006\007\01012",   12, STATUS_SUCCESS}, /* whitespace char  5 to  8 */
1242     { 0, "\011\012\013\01413",   13, STATUS_SUCCESS}, /* whitespace char  9 to 12 */
1243     { 0, "\015\016\017\02014",   14, STATUS_SUCCESS}, /* whitespace char 13 to 16 */
1244     { 0, "\021\022\023\02415",   15, STATUS_SUCCESS}, /* whitespace char 17 to 20 */
1245     { 0, "\025\026\027\03016",   16, STATUS_SUCCESS}, /* whitespace char 21 to 24 */
1246     { 0, "\031\032\033\03417",   17, STATUS_SUCCESS}, /* whitespace char 25 to 28 */
1247     { 0, "\035\036\037\04018",   18, STATUS_SUCCESS}, /* whitespace char 29 to 32 */
1248     { 0, " \n \r \t214",        214, STATUS_SUCCESS},
1249     { 0, " \n \r \t+214",       214, STATUS_SUCCESS}, /* Signs can be used after whitespace */
1250     { 0, " \n \r \t-214",      -214, STATUS_SUCCESS},
1251     { 0, "+214 0",              214, STATUS_SUCCESS}, /* Space terminates the number */
1252     { 0, " 214.01",             214, STATUS_SUCCESS}, /* Decimal point not accepted */
1253     { 0, " 214,01",             214, STATUS_SUCCESS}, /* Decimal comma not accepted */
1254     { 0, "f81",                   0, STATUS_SUCCESS},
1255     { 0, "0x12345",         0x12345, STATUS_SUCCESS}, /* Hex */
1256     { 0, "00x12345",              0, STATUS_SUCCESS},
1257     { 0, "0xx12345",              0, STATUS_SUCCESS},
1258     { 0, "1x34",                  1, STATUS_SUCCESS},
1259     { 0, "-9999999999", -1410065407, STATUS_SUCCESS}, /* Big negative integer */
1260     { 0, "-2147483649",  2147483647, STATUS_SUCCESS}, /* Too small to fit in 32 Bits */
1261     { 0, "-2147483648", 0x80000000L, STATUS_SUCCESS}, /* Smallest negative integer */
1262     { 0, "-2147483647", -2147483647, STATUS_SUCCESS},
1263     { 0, "-1",                   -1, STATUS_SUCCESS},
1264     { 0, "0",                     0, STATUS_SUCCESS},
1265     { 0, "1",                     1, STATUS_SUCCESS},
1266     { 0, "2147483646",   2147483646, STATUS_SUCCESS},
1267     { 0, "2147483647",   2147483647, STATUS_SUCCESS}, /* Largest signed positive integer */
1268     { 0, "2147483648",  0x80000000L, STATUS_SUCCESS}, /* Positive int equal to smallest negative int */
1269     { 0, "2147483649",  -2147483647, STATUS_SUCCESS},
1270     { 0, "4294967294",           -2, STATUS_SUCCESS},
1271     { 0, "4294967295",           -1, STATUS_SUCCESS}, /* Largest unsigned integer */
1272     { 0, "4294967296",            0, STATUS_SUCCESS}, /* Too big to fit in 32 Bits */
1273     { 0, "9999999999",   1410065407, STATUS_SUCCESS}, /* Big positive integer */
1274     { 0, "056789",            56789, STATUS_SUCCESS}, /* Leading zero and still decimal */
1275     { 0, "b1011101100",           0, STATUS_SUCCESS}, /* Binary (b-notation) */
1276     { 0, "-b1011101100",          0, STATUS_SUCCESS}, /* Negative Binary (b-notation) */
1277     { 0, "b10123456789",          0, STATUS_SUCCESS}, /* Binary with nonbinary digits (2-9) */
1278     { 0, "0b1011101100",        748, STATUS_SUCCESS}, /* Binary (0b-notation) */
1279     { 0, "-0b1011101100",      -748, STATUS_SUCCESS}, /* Negative binary (0b-notation) */
1280     { 0, "0b10123456789",         5, STATUS_SUCCESS}, /* Binary with nonbinary digits (2-9) */
1281     { 0, "-0b10123456789",       -5, STATUS_SUCCESS}, /* Negative binary with nonbinary digits (2-9) */
1282     { 0, "0b1",                   1, STATUS_SUCCESS}, /* one digit binary */
1283     { 0, "0b2",                   0, STATUS_SUCCESS}, /* empty binary */
1284     { 0, "0b",                    0, STATUS_SUCCESS}, /* empty binary */
1285     { 0, "o1234567",              0, STATUS_SUCCESS}, /* Octal (o-notation) */
1286     { 0, "-o1234567",             0, STATUS_SUCCESS}, /* Negative Octal (o-notation) */
1287     { 0, "o56789",                0, STATUS_SUCCESS}, /* Octal with nonoctal digits (8 and 9) */
1288     { 0, "0o1234567",      01234567, STATUS_SUCCESS}, /* Octal (0o-notation) */
1289     { 0, "-0o1234567",    -01234567, STATUS_SUCCESS}, /* Negative octal (0o-notation) */
1290     { 0, "0o56789",            0567, STATUS_SUCCESS}, /* Octal with nonoctal digits (8 and 9) */
1291     { 0, "-0o56789",          -0567, STATUS_SUCCESS}, /* Negative octal with nonoctal digits (8 and 9) */
1292     { 0, "0o7",                   7, STATUS_SUCCESS}, /* one digit octal */
1293     { 0, "0o8",                   0, STATUS_SUCCESS}, /* empty octal */
1294     { 0, "0o",                    0, STATUS_SUCCESS}, /* empty octal */
1295     { 0, "0d1011101100",          0, STATUS_SUCCESS}, /* explicit decimal with 0d */
1296     { 0, "x89abcdef",             0, STATUS_SUCCESS}, /* Hex with lower case digits a-f (x-notation) */
1297     { 0, "xFEDCBA00",             0, STATUS_SUCCESS}, /* Hex with upper case digits A-F (x-notation) */
1298     { 0, "-xFEDCBA00",            0, STATUS_SUCCESS}, /* Negative Hexadecimal (x-notation) */
1299     { 0, "0x89abcdef",   0x89abcdef, STATUS_SUCCESS}, /* Hex with lower case digits a-f (0x-notation) */
1300     { 0, "0xFEDCBA00",   0xFEDCBA00, STATUS_SUCCESS}, /* Hex with upper case digits A-F (0x-notation) */
1301     { 0, "-0xFEDCBA00",    19088896, STATUS_SUCCESS}, /* Negative Hexadecimal (0x-notation) */
1302     { 0, "0xabcdefgh",     0xabcdef, STATUS_SUCCESS}, /* Hex with illegal lower case digits (g-z) */
1303     { 0, "0xABCDEFGH",     0xABCDEF, STATUS_SUCCESS}, /* Hex with illegal upper case digits (G-Z) */
1304     { 0, "0xF",                 0xf, STATUS_SUCCESS}, /* one digit hexadecimal */
1305     { 0, "0xG",                   0, STATUS_SUCCESS}, /* empty hexadecimal */
1306     { 0, "0x",                    0, STATUS_SUCCESS}, /* empty hexadecimal */
1307     { 0, "",                      0, STATUS_SUCCESS, STATUS_INVALID_PARAMETER}, /* empty string */
1308     { 2, "1011101100",          748, STATUS_SUCCESS},
1309     { 2, "-1011101100",        -748, STATUS_SUCCESS},
1310     { 2, "2",                     0, STATUS_SUCCESS},
1311     { 2, "0b1011101100",          0, STATUS_SUCCESS},
1312     { 2, "0o1011101100",          0, STATUS_SUCCESS},
1313     { 2, "0d1011101100",          0, STATUS_SUCCESS},
1314     { 2, "0x1011101100",          0, STATUS_SUCCESS},
1315     { 2, "",                      0, STATUS_SUCCESS, STATUS_INVALID_PARAMETER}, /* empty string */
1316     { 8, "1011101100",    136610368, STATUS_SUCCESS},
1317     { 8, "-1011101100",  -136610368, STATUS_SUCCESS},
1318     { 8, "8",                     0, STATUS_SUCCESS},
1319     { 8, "0b1011101100",          0, STATUS_SUCCESS},
1320     { 8, "0o1011101100",          0, STATUS_SUCCESS},
1321     { 8, "0d1011101100",          0, STATUS_SUCCESS},
1322     { 8, "0x1011101100",          0, STATUS_SUCCESS},
1323     { 8, "",                      0, STATUS_SUCCESS, STATUS_INVALID_PARAMETER}, /* empty string */
1324     {10, "1011101100",   1011101100, STATUS_SUCCESS},
1325     {10, "-1011101100", -1011101100, STATUS_SUCCESS},
1326     {10, "0b1011101100",          0, STATUS_SUCCESS},
1327     {10, "0o1011101100",          0, STATUS_SUCCESS},
1328     {10, "0d1011101100",          0, STATUS_SUCCESS},
1329     {10, "0x1011101100",          0, STATUS_SUCCESS},
1330     {10, "o12345",                0, STATUS_SUCCESS}, /* Octal although base is 10 */
1331     {10, "",                      0, STATUS_SUCCESS, STATUS_INVALID_PARAMETER}, /* empty string */
1332     {16, "1011101100",    286265600, STATUS_SUCCESS},
1333     {16, "-1011101100",  -286265600, STATUS_SUCCESS},
1334     {16, "G",                     0, STATUS_SUCCESS},
1335     {16, "g",                     0, STATUS_SUCCESS},
1336     {16, "0b1011101100",  286265600, STATUS_SUCCESS},
1337     {16, "0o1011101100",          0, STATUS_SUCCESS},
1338     {16, "0d1011101100",  286265600, STATUS_SUCCESS},
1339     {16, "0x1011101100",          0, STATUS_SUCCESS},
1340     {16, "",                      0, STATUS_SUCCESS, STATUS_INVALID_PARAMETER}, /* empty string */
1341     {20, "0",                     0, STATUS_INVALID_PARAMETER}, /* illegal base */
1342     {-8, "0",                     0, STATUS_INVALID_PARAMETER}, /* Negative base */
1343 /*    { 0, NULL,                    0, STATUS_SUCCESS}, */ /* NULL as string */
1344 };
1345 #define NB_STR2INT (sizeof(str2int)/sizeof(*str2int))
1346
1347
1348 static void test_RtlUnicodeStringToInteger(void)
1349 {
1350     unsigned int test_num;
1351     int value;
1352     NTSTATUS result;
1353     WCHAR *wstr;
1354     UNICODE_STRING uni;
1355
1356     for (test_num = 0; test_num < NB_STR2INT; test_num++) {
1357         wstr = AtoW(str2int[test_num].str);
1358         value = 0xdeadbeef;
1359         pRtlInitUnicodeString(&uni, wstr);
1360         result = pRtlUnicodeStringToInteger(&uni, str2int[test_num].base, &value);
1361         ok(result == str2int[test_num].result ||
1362            (str2int[test_num].alternative && result == str2int[test_num].alternative),
1363            "(test %d): RtlUnicodeStringToInteger(\"%s\", %d, [out]) has result %x, expected: %x (%x)\n",
1364            test_num, str2int[test_num].str, str2int[test_num].base, result,
1365            str2int[test_num].result, str2int[test_num].alternative);
1366         if (result == STATUS_SUCCESS)
1367             ok(value == str2int[test_num].value ||
1368                broken(str2int[test_num].str[0] == '\0' && str2int[test_num].base == 16), /* nt4 */
1369                "(test %d): RtlUnicodeStringToInteger(\"%s\", %d, [out]) assigns value %d, expected: %d\n",
1370                test_num, str2int[test_num].str, str2int[test_num].base, value, str2int[test_num].value);
1371         else
1372             ok(value == 0xdeadbeef || value == 0 /* vista */,
1373                "(test %d): RtlUnicodeStringToInteger(\"%s\", %d, [out]) assigns value %d, expected 0 or deadbeef\n",
1374                test_num, str2int[test_num].str, str2int[test_num].base, value);
1375         HeapFree(GetProcessHeap(), 0, wstr);
1376     }
1377
1378     wstr = AtoW(str2int[1].str);
1379     pRtlInitUnicodeString(&uni, wstr);
1380     result = pRtlUnicodeStringToInteger(&uni, str2int[1].base, NULL);
1381     ok(result == STATUS_ACCESS_VIOLATION,
1382        "call failed: RtlUnicodeStringToInteger(\"%s\", %d, NULL) has result %x\n",
1383        str2int[1].str, str2int[1].base, result);
1384     result = pRtlUnicodeStringToInteger(&uni, 20, NULL);
1385     ok(result == STATUS_INVALID_PARAMETER || result == STATUS_ACCESS_VIOLATION,
1386        "call failed: RtlUnicodeStringToInteger(\"%s\", 20, NULL) has result %x\n",
1387        str2int[1].str, result);
1388
1389     uni.Length = 10; /* Make Length shorter (5 WCHARS instead of 7) */
1390     result = pRtlUnicodeStringToInteger(&uni, str2int[1].base, &value);
1391     ok(result == STATUS_SUCCESS,
1392        "call failed: RtlUnicodeStringToInteger(\"12345\", %d, [out]) has result %x\n",
1393        str2int[1].base, result);
1394     ok(value == 12345,
1395        "didn't return expected value (test a): expected: %d, got: %d\n",
1396        12345, value);
1397
1398     uni.Length = 5; /* Use odd Length (2.5 WCHARS) */
1399     result = pRtlUnicodeStringToInteger(&uni, str2int[1].base, &value);
1400     ok(result == STATUS_SUCCESS || result == STATUS_INVALID_PARAMETER /* vista */,
1401        "call failed: RtlUnicodeStringToInteger(\"12\", %d, [out]) has result %x\n",
1402        str2int[1].base, result);
1403     if (result == STATUS_SUCCESS)
1404         ok(value == 12, "didn't return expected value (test b): expected: %d, got: %d\n", 12, value);
1405
1406     uni.Length = 2;
1407     result = pRtlUnicodeStringToInteger(&uni, str2int[1].base, &value);
1408     ok(result == STATUS_SUCCESS,
1409        "call failed: RtlUnicodeStringToInteger(\"1\", %d, [out]) has result %x\n",
1410        str2int[1].base, result);
1411     ok(value == 1,
1412        "didn't return expected value (test c): expected: %d, got: %d\n",
1413        1, value);
1414     /* w2k: uni.Length = 0 returns value 11234567 instead of 0 */
1415     HeapFree(GetProcessHeap(), 0, wstr);
1416 }
1417
1418
1419 static void test_RtlCharToInteger(void)
1420 {
1421     unsigned int test_num;
1422     int value;
1423     NTSTATUS result;
1424
1425     for (test_num = 0; test_num < NB_STR2INT; test_num++) {
1426         /* w2k skips a leading '\0' and processes the string after */
1427         if (str2int[test_num].str[0] != '\0') {
1428             value = 0xdeadbeef;
1429             result = pRtlCharToInteger(str2int[test_num].str, str2int[test_num].base, &value);
1430             ok(result == str2int[test_num].result ||
1431                (str2int[test_num].alternative && result == str2int[test_num].alternative),
1432                "(test %d): call failed: RtlCharToInteger(\"%s\", %d, [out]) has result %x, expected: %x (%x)\n",
1433                test_num, str2int[test_num].str, str2int[test_num].base, result,
1434                str2int[test_num].result, str2int[test_num].alternative);
1435             if (result == STATUS_SUCCESS)
1436                 ok(value == str2int[test_num].value,
1437                    "(test %d): call failed: RtlCharToInteger(\"%s\", %d, [out]) assigns value %d, expected: %d\n",
1438                    test_num, str2int[test_num].str, str2int[test_num].base, value, str2int[test_num].value);
1439             else
1440                 ok(value == 0 || value == 0xdeadbeef,
1441                    "(test %d): call failed: RtlCharToInteger(\"%s\", %d, [out]) assigns value %d, expected 0 or deadbeef\n",
1442                    test_num, str2int[test_num].str, str2int[test_num].base, value);
1443         }
1444     }
1445
1446     result = pRtlCharToInteger(str2int[1].str, str2int[1].base, NULL);
1447     ok(result == STATUS_ACCESS_VIOLATION,
1448        "call failed: RtlCharToInteger(\"%s\", %d, NULL) has result %x\n",
1449        str2int[1].str, str2int[1].base, result);
1450
1451     result = pRtlCharToInteger(str2int[1].str, 20, NULL);
1452     ok(result == STATUS_INVALID_PARAMETER,
1453        "call failed: RtlCharToInteger(\"%s\", 20, NULL) has result %x\n",
1454        str2int[1].str, result);
1455 }
1456
1457
1458 #define STRI_BUFFER_LENGTH 35
1459
1460 typedef struct {
1461     int base;
1462     ULONG value;
1463     USHORT Length;
1464     USHORT MaximumLength;
1465     const char *Buffer;
1466     NTSTATUS result;
1467 } int2str_t;
1468
1469 static const int2str_t int2str[] = {
1470     {10,          123,  3, 11, "123\0-------------------------------", STATUS_SUCCESS},
1471
1472     { 0,  0x80000000U, 10, 11, "2147483648\0------------------------", STATUS_SUCCESS}, /* min signed int */
1473     { 0,  -2147483647, 10, 11, "2147483649\0------------------------", STATUS_SUCCESS},
1474     { 0,           -2, 10, 11, "4294967294\0------------------------", STATUS_SUCCESS},
1475     { 0,           -1, 10, 11, "4294967295\0------------------------", STATUS_SUCCESS},
1476     { 0,            0,  1, 11, "0\0---------------------------------", STATUS_SUCCESS},
1477     { 0,            1,  1, 11, "1\0---------------------------------", STATUS_SUCCESS},
1478     { 0,           12,  2, 11, "12\0--------------------------------", STATUS_SUCCESS},
1479     { 0,          123,  3, 11, "123\0-------------------------------", STATUS_SUCCESS},
1480     { 0,         1234,  4, 11, "1234\0------------------------------", STATUS_SUCCESS},
1481     { 0,        12345,  5, 11, "12345\0-----------------------------", STATUS_SUCCESS},
1482     { 0,       123456,  6, 11, "123456\0----------------------------", STATUS_SUCCESS},
1483     { 0,      1234567,  7, 11, "1234567\0---------------------------", STATUS_SUCCESS},
1484     { 0,     12345678,  8, 11, "12345678\0--------------------------", STATUS_SUCCESS},
1485     { 0,    123456789,  9, 11, "123456789\0-------------------------", STATUS_SUCCESS},
1486     { 0,   2147483646, 10, 11, "2147483646\0------------------------", STATUS_SUCCESS},
1487     { 0,   2147483647, 10, 11, "2147483647\0------------------------", STATUS_SUCCESS}, /* max signed int */
1488     { 0,  2147483648U, 10, 11, "2147483648\0------------------------", STATUS_SUCCESS}, /* uint = -max int */
1489     { 0,  2147483649U, 10, 11, "2147483649\0------------------------", STATUS_SUCCESS},
1490     { 0,  4294967294U, 10, 11, "4294967294\0------------------------", STATUS_SUCCESS},
1491     { 0,  4294967295U, 10, 11, "4294967295\0------------------------", STATUS_SUCCESS}, /* max unsigned int */
1492
1493     { 2,  0x80000000U, 32, 33, "10000000000000000000000000000000\0--", STATUS_SUCCESS}, /* min signed int */
1494     { 2,  -2147483647, 32, 33, "10000000000000000000000000000001\0--", STATUS_SUCCESS},
1495     { 2,           -2, 32, 33, "11111111111111111111111111111110\0--", STATUS_SUCCESS},
1496     { 2,           -1, 32, 33, "11111111111111111111111111111111\0--", STATUS_SUCCESS},
1497     { 2,            0,  1, 33, "0\0---------------------------------", STATUS_SUCCESS},
1498     { 2,            1,  1, 33, "1\0---------------------------------", STATUS_SUCCESS},
1499     { 2,           10,  4, 33, "1010\0------------------------------", STATUS_SUCCESS},
1500     { 2,          100,  7, 33, "1100100\0---------------------------", STATUS_SUCCESS},
1501     { 2,         1000, 10, 33, "1111101000\0------------------------", STATUS_SUCCESS},
1502     { 2,        10000, 14, 33, "10011100010000\0--------------------", STATUS_SUCCESS},
1503     { 2,        32767, 15, 33, "111111111111111\0-------------------", STATUS_SUCCESS},
1504 /*  { 2,        32768, 16, 33, "1000000000000000\0------------------", STATUS_SUCCESS}, broken on windows */
1505 /*  { 2,        65535, 16, 33, "1111111111111111\0------------------", STATUS_SUCCESS}, broken on windows */
1506     { 2,        65536, 17, 33, "10000000000000000\0-----------------", STATUS_SUCCESS},
1507     { 2,       100000, 17, 33, "11000011010100000\0-----------------", STATUS_SUCCESS},
1508     { 2,      1000000, 20, 33, "11110100001001000000\0--------------", STATUS_SUCCESS},
1509     { 2,     10000000, 24, 33, "100110001001011010000000\0----------", STATUS_SUCCESS},
1510     { 2,    100000000, 27, 33, "101111101011110000100000000\0-------", STATUS_SUCCESS},
1511     { 2,   1000000000, 30, 33, "111011100110101100101000000000\0----", STATUS_SUCCESS},
1512     { 2,   1073741823, 30, 33, "111111111111111111111111111111\0----", STATUS_SUCCESS},
1513     { 2,   2147483646, 31, 33, "1111111111111111111111111111110\0---", STATUS_SUCCESS},
1514     { 2,   2147483647, 31, 33, "1111111111111111111111111111111\0---", STATUS_SUCCESS}, /* max signed int */
1515     { 2,  2147483648U, 32, 33, "10000000000000000000000000000000\0--", STATUS_SUCCESS}, /* uint = -max int */
1516     { 2,  2147483649U, 32, 33, "10000000000000000000000000000001\0--", STATUS_SUCCESS},
1517     { 2,  4294967294U, 32, 33, "11111111111111111111111111111110\0--", STATUS_SUCCESS},
1518     { 2,  4294967295U, 32, 33, "11111111111111111111111111111111\0--", STATUS_SUCCESS}, /* max unsigned int */
1519
1520     { 8,  0x80000000U, 11, 12, "20000000000\0-----------------------", STATUS_SUCCESS}, /* min signed int */
1521     { 8,  -2147483647, 11, 12, "20000000001\0-----------------------", STATUS_SUCCESS},
1522     { 8,           -2, 11, 12, "37777777776\0-----------------------", STATUS_SUCCESS},
1523     { 8,           -1, 11, 12, "37777777777\0-----------------------", STATUS_SUCCESS},
1524     { 8,            0,  1, 12, "0\0---------------------------------", STATUS_SUCCESS},
1525     { 8,            1,  1, 12, "1\0---------------------------------", STATUS_SUCCESS},
1526     { 8,   2147483646, 11, 12, "17777777776\0-----------------------", STATUS_SUCCESS},
1527     { 8,   2147483647, 11, 12, "17777777777\0-----------------------", STATUS_SUCCESS}, /* max signed int */
1528     { 8,  2147483648U, 11, 12, "20000000000\0-----------------------", STATUS_SUCCESS}, /* uint = -max int */
1529     { 8,  2147483649U, 11, 12, "20000000001\0-----------------------", STATUS_SUCCESS},
1530     { 8,  4294967294U, 11, 12, "37777777776\0-----------------------", STATUS_SUCCESS},
1531     { 8,  4294967295U, 11, 12, "37777777777\0-----------------------", STATUS_SUCCESS}, /* max unsigned int */
1532
1533     {10,  0x80000000U, 10, 11, "2147483648\0------------------------", STATUS_SUCCESS}, /* min signed int */
1534     {10,  -2147483647, 10, 11, "2147483649\0------------------------", STATUS_SUCCESS},
1535     {10,           -2, 10, 11, "4294967294\0------------------------", STATUS_SUCCESS},
1536     {10,           -1, 10, 11, "4294967295\0------------------------", STATUS_SUCCESS},
1537     {10,            0,  1, 11, "0\0---------------------------------", STATUS_SUCCESS},
1538     {10,            1,  1, 11, "1\0---------------------------------", STATUS_SUCCESS},
1539     {10,   2147483646, 10, 11, "2147483646\0------------------------", STATUS_SUCCESS},
1540     {10,   2147483647, 10, 11, "2147483647\0------------------------", STATUS_SUCCESS}, /* max signed int */
1541     {10,  2147483648U, 10, 11, "2147483648\0------------------------", STATUS_SUCCESS}, /* uint = -max int */
1542     {10,  2147483649U, 10, 11, "2147483649\0------------------------", STATUS_SUCCESS},
1543     {10,  4294967294U, 10, 11, "4294967294\0------------------------", STATUS_SUCCESS},
1544     {10,  4294967295U, 10, 11, "4294967295\0------------------------", STATUS_SUCCESS}, /* max unsigned int */
1545
1546     {16,  0x80000000U,  8,  9, "80000000\0--------------------------", STATUS_SUCCESS}, /* min signed int */
1547     {16,  -2147483647,  8,  9, "80000001\0--------------------------", STATUS_SUCCESS},
1548     {16,           -2,  8,  9, "FFFFFFFE\0--------------------------", STATUS_SUCCESS},
1549     {16,           -1,  8,  9, "FFFFFFFF\0--------------------------", STATUS_SUCCESS},
1550     {16,            0,  1,  9, "0\0---------------------------------", STATUS_SUCCESS},
1551     {16,            1,  1,  9, "1\0---------------------------------", STATUS_SUCCESS},
1552     {16,   2147483646,  8,  9, "7FFFFFFE\0--------------------------", STATUS_SUCCESS},
1553     {16,   2147483647,  8,  9, "7FFFFFFF\0--------------------------", STATUS_SUCCESS}, /* max signed int */
1554     {16,  2147483648U,  8,  9, "80000000\0--------------------------", STATUS_SUCCESS}, /* uint = -max int */
1555     {16,  2147483649U,  8,  9, "80000001\0--------------------------", STATUS_SUCCESS},
1556     {16,  4294967294U,  8,  9, "FFFFFFFE\0--------------------------", STATUS_SUCCESS},
1557     {16,  4294967295U,  8,  9, "FFFFFFFF\0--------------------------", STATUS_SUCCESS}, /* max unsigned int */
1558
1559 /*  { 2,        32768, 16, 17, "1000000000000000\0------------------", STATUS_SUCCESS}, broken on windows */
1560 /*  { 2,        32768, 16, 16, "1000000000000000-------------------",  STATUS_SUCCESS}, broken on windows */
1561     { 2,        65536, 17, 18, "10000000000000000\0-----------------", STATUS_SUCCESS},
1562     { 2,        65536, 17, 17, "10000000000000000------------------",  STATUS_SUCCESS},
1563     { 2,       131072, 18, 19, "100000000000000000\0----------------", STATUS_SUCCESS},
1564     { 2,       131072, 18, 18, "100000000000000000-----------------",  STATUS_SUCCESS},
1565     {16,   0xffffffff,  8,  9, "FFFFFFFF\0--------------------------", STATUS_SUCCESS},
1566     {16,   0xffffffff,  8,  8, "FFFFFFFF---------------------------",  STATUS_SUCCESS}, /* No \0 term */
1567     {16,   0xffffffff,  8,  7, "-----------------------------------",  STATUS_BUFFER_OVERFLOW}, /* Too short */
1568     {16,          0xa,  1,  2, "A\0---------------------------------", STATUS_SUCCESS},
1569     {16,          0xa,  1,  1, "A----------------------------------",  STATUS_SUCCESS}, /* No \0 term */
1570     {16,            0,  1,  0, "-----------------------------------",  STATUS_BUFFER_OVERFLOW},
1571     {20,   0xdeadbeef,  0,  9, "-----------------------------------",  STATUS_INVALID_PARAMETER}, /* ill. base */
1572     {-8,     07654321,  0, 12, "-----------------------------------",  STATUS_INVALID_PARAMETER}, /* neg. base */
1573 };
1574 #define NB_INT2STR (sizeof(int2str)/sizeof(*int2str))
1575
1576
1577 static void one_RtlIntegerToUnicodeString_test(int test_num, const int2str_t *int2str)
1578 {
1579     int pos;
1580     WCHAR expected_str_Buffer[STRI_BUFFER_LENGTH + 1];
1581     UNICODE_STRING expected_unicode_string;
1582     STRING expected_ansi_str;
1583     WCHAR str_Buffer[STRI_BUFFER_LENGTH + 1];
1584     UNICODE_STRING unicode_string;
1585     STRING ansi_str;
1586     NTSTATUS result;
1587
1588     for (pos = 0; pos < STRI_BUFFER_LENGTH; pos++) {
1589         expected_str_Buffer[pos] = int2str->Buffer[pos];
1590     }
1591     expected_unicode_string.Length = int2str->Length * sizeof(WCHAR);
1592     expected_unicode_string.MaximumLength = int2str->MaximumLength * sizeof(WCHAR);
1593     expected_unicode_string.Buffer = expected_str_Buffer;
1594     pRtlUnicodeStringToAnsiString(&expected_ansi_str, &expected_unicode_string, 1);
1595
1596     for (pos = 0; pos < STRI_BUFFER_LENGTH; pos++) {
1597         str_Buffer[pos] = '-';
1598     }
1599     unicode_string.Length = 0;
1600     unicode_string.MaximumLength = int2str->MaximumLength * sizeof(WCHAR);
1601     unicode_string.Buffer = str_Buffer;
1602
1603     result = pRtlIntegerToUnicodeString(int2str->value, int2str->base, &unicode_string);
1604     pRtlUnicodeStringToAnsiString(&ansi_str, &unicode_string, 1);
1605     if (result == STATUS_BUFFER_OVERFLOW) {
1606         /* On BUFFER_OVERFLOW the string Buffer should be unchanged */
1607         for (pos = 0; pos < STRI_BUFFER_LENGTH; pos++) {
1608             expected_str_Buffer[pos] = '-';
1609         }
1610         /* w2k: The native function has two reasons for BUFFER_OVERFLOW: */
1611         /* If the value is too large to convert: The Length is unchanged */
1612         /* If str is too small to hold the string: Set str->Length to the length */
1613         /* the string would have (which can be larger than the MaximumLength). */
1614         /* To allow all this in the tests we do the following: */
1615         if (expected_unicode_string.Length > 32 && unicode_string.Length == 0) {
1616             /* The value is too large to convert only triggered when testing native */
1617             expected_unicode_string.Length = 0;
1618         }
1619     } else {
1620         ok(result == int2str->result,
1621            "(test %d): RtlIntegerToUnicodeString(%u, %d, [out]) has result %x, expected: %x\n",
1622            test_num, int2str->value, int2str->base, result, int2str->result);
1623         if (result == STATUS_SUCCESS) {
1624             ok(unicode_string.Buffer[unicode_string.Length/sizeof(WCHAR)] == '\0',
1625                "(test %d): RtlIntegerToUnicodeString(%u, %d, [out]) string \"%s\" is not NULL terminated\n",
1626                test_num, int2str->value, int2str->base, ansi_str.Buffer);
1627         }
1628     }
1629     ok(memcmp(unicode_string.Buffer, expected_unicode_string.Buffer, STRI_BUFFER_LENGTH * sizeof(WCHAR)) == 0,
1630        "(test %d): RtlIntegerToUnicodeString(%u, %d, [out]) assigns string \"%s\", expected: \"%s\"\n",
1631        test_num, int2str->value, int2str->base, ansi_str.Buffer, expected_ansi_str.Buffer);
1632     ok(unicode_string.Length == expected_unicode_string.Length,
1633        "(test %d): RtlIntegerToUnicodeString(%u, %d, [out]) string has Length %d, expected: %d\n",
1634        test_num, int2str->value, int2str->base, unicode_string.Length, expected_unicode_string.Length);
1635     ok(unicode_string.MaximumLength == expected_unicode_string.MaximumLength,
1636        "(test %d): RtlIntegerToUnicodeString(%u, %d, [out]) string has MaximumLength %d, expected: %d\n",
1637        test_num, int2str->value, int2str->base, unicode_string.MaximumLength, expected_unicode_string.MaximumLength);
1638     pRtlFreeAnsiString(&expected_ansi_str);
1639     pRtlFreeAnsiString(&ansi_str);
1640 }
1641
1642
1643 static void test_RtlIntegerToUnicodeString(void)
1644 {
1645     size_t test_num;
1646
1647     for (test_num = 0; test_num < NB_INT2STR; test_num++)
1648         one_RtlIntegerToUnicodeString_test(test_num, &int2str[test_num]);
1649 }
1650
1651
1652 static void one_RtlIntegerToChar_test(int test_num, const int2str_t *int2str)
1653 {
1654     NTSTATUS result;
1655     char dest_str[STRI_BUFFER_LENGTH + 1];
1656
1657     memset(dest_str, '-', STRI_BUFFER_LENGTH);
1658     dest_str[STRI_BUFFER_LENGTH] = '\0';
1659     result = pRtlIntegerToChar(int2str->value, int2str->base, int2str->MaximumLength, dest_str);
1660     ok(result == int2str->result,
1661        "(test %d): RtlIntegerToChar(%u, %d, %d, [out]) has result %x, expected: %x\n",
1662        test_num, int2str->value, int2str->base, int2str->MaximumLength, result, int2str->result);
1663     ok(memcmp(dest_str, int2str->Buffer, STRI_BUFFER_LENGTH) == 0,
1664        "(test %d): RtlIntegerToChar(%u, %d, %d, [out]) assigns string \"%s\", expected: \"%s\"\n",
1665        test_num, int2str->value, int2str->base, int2str->MaximumLength, dest_str, int2str->Buffer);
1666 }
1667
1668
1669 static void test_RtlIntegerToChar(void)
1670 {
1671     NTSTATUS result;
1672     size_t test_num;
1673
1674     for (test_num = 0; test_num < NB_INT2STR; test_num++)
1675       one_RtlIntegerToChar_test(test_num, &int2str[test_num]);
1676
1677     result = pRtlIntegerToChar(int2str[0].value, 20, int2str[0].MaximumLength, NULL);
1678     ok(result == STATUS_INVALID_PARAMETER,
1679        "(test a): RtlIntegerToChar(%u, %d, %d, NULL) has result %x, expected: %x\n",
1680        int2str[0].value, 20, int2str[0].MaximumLength, result, STATUS_INVALID_PARAMETER);
1681
1682     result = pRtlIntegerToChar(int2str[0].value, 20, 0, NULL);
1683     ok(result == STATUS_INVALID_PARAMETER,
1684        "(test b): RtlIntegerToChar(%u, %d, %d, NULL) has result %x, expected: %x\n",
1685        int2str[0].value, 20, 0, result, STATUS_INVALID_PARAMETER);
1686
1687     result = pRtlIntegerToChar(int2str[0].value, int2str[0].base, 0, NULL);
1688     ok(result == STATUS_BUFFER_OVERFLOW,
1689        "(test c): RtlIntegerToChar(%u, %d, %d, NULL) has result %x, expected: %x\n",
1690        int2str[0].value, int2str[0].base, 0, result, STATUS_BUFFER_OVERFLOW);
1691
1692     result = pRtlIntegerToChar(int2str[0].value, int2str[0].base, int2str[0].MaximumLength, NULL);
1693     ok(result == STATUS_ACCESS_VIOLATION,
1694        "(test d): RtlIntegerToChar(%u, %d, %d, NULL) has result %x, expected: %x\n",
1695        int2str[0].value, int2str[0].base, int2str[0].MaximumLength, result, STATUS_ACCESS_VIOLATION);
1696 }
1697
1698 static void test_RtlIsTextUnicode(void)
1699 {
1700     char ascii[] = "A simple string";
1701     char false_positive[] = {0x41, 0x0a, 0x0d, 0x1d};
1702     WCHAR false_negative = 0x0d0a;
1703     WCHAR unicode[] = {'A',' ','U','n','i','c','o','d','e',' ','s','t','r','i','n','g',0};
1704     WCHAR unicode_no_controls[] = {'A','U','n','i','c','o','d','e','s','t','r','i','n','g',0};
1705     /* String with both byte-reversed and standard Unicode control characters. */
1706     WCHAR mixed_controls[] = {'\t',0x9000,0x0d00,'\n',0};
1707     WCHAR *be_unicode;
1708     WCHAR *be_unicode_no_controls;
1709     BOOLEAN res;
1710     int flags;
1711     int i;
1712
1713     ok(!pRtlIsTextUnicode(ascii, sizeof(ascii), NULL), "ASCII text detected as Unicode\n");
1714
1715     res = pRtlIsTextUnicode(unicode, sizeof(unicode), NULL);
1716     ok(res ||
1717        broken(res == FALSE), /* NT4 */
1718        "Text should be Unicode\n");
1719
1720     ok(!pRtlIsTextUnicode(unicode, sizeof(unicode) - 1, NULL), "Text should be Unicode\n");
1721
1722     flags =  IS_TEXT_UNICODE_UNICODE_MASK;
1723     ok(pRtlIsTextUnicode(unicode, sizeof(unicode), &flags), "Text should not pass a Unicode\n");
1724     ok(flags == (IS_TEXT_UNICODE_STATISTICS | IS_TEXT_UNICODE_CONTROLS),
1725        "Expected flags 0x6, obtained %x\n", flags);
1726
1727     flags =  IS_TEXT_UNICODE_REVERSE_MASK;
1728     ok(!pRtlIsTextUnicode(unicode, sizeof(unicode), &flags), "Text should not pass reverse Unicode tests\n");
1729     ok(flags == 0, "Expected flags 0, obtained %x\n", flags);
1730
1731     flags = IS_TEXT_UNICODE_ODD_LENGTH;
1732     ok(!pRtlIsTextUnicode(unicode, sizeof(unicode) - 1, &flags), "Odd length test should have passed\n");
1733     ok(flags == IS_TEXT_UNICODE_ODD_LENGTH, "Expected flags 0x200, obtained %x\n", flags);
1734
1735     be_unicode = HeapAlloc(GetProcessHeap(), 0, sizeof(unicode) + sizeof(WCHAR));
1736     be_unicode[0] = 0xfffe;
1737     for (i = 0; i < sizeof(unicode)/sizeof(unicode[0]); i++)
1738     {
1739         be_unicode[i + 1] = (unicode[i] >> 8) | ((unicode[i] & 0xff) << 8);
1740     }
1741     ok(!pRtlIsTextUnicode(be_unicode, sizeof(unicode) + 2, NULL), "Reverse endian should not be Unicode\n");
1742     ok(!pRtlIsTextUnicode(&be_unicode[1], sizeof(unicode), NULL), "Reverse endian should not be Unicode\n");
1743
1744     flags = IS_TEXT_UNICODE_REVERSE_MASK;
1745     ok(!pRtlIsTextUnicode(&be_unicode[1], sizeof(unicode), &flags), "Reverse endian should be Unicode\n");
1746     todo_wine
1747     ok(flags == (IS_TEXT_UNICODE_REVERSE_ASCII16 | IS_TEXT_UNICODE_REVERSE_STATISTICS | IS_TEXT_UNICODE_REVERSE_CONTROLS),
1748        "Expected flags 0x70, obtained %x\n", flags);
1749
1750     flags = IS_TEXT_UNICODE_REVERSE_MASK;
1751     ok(!pRtlIsTextUnicode(be_unicode, sizeof(unicode) + 2, &flags), "Reverse endian should be Unicode\n");
1752     ok(flags == (IS_TEXT_UNICODE_REVERSE_CONTROLS | IS_TEXT_UNICODE_REVERSE_SIGNATURE),
1753        "Expected flags 0xc0, obtained %x\n", flags);
1754
1755     /* build byte reversed unicode string with no control chars */
1756     be_unicode_no_controls = HeapAlloc(GetProcessHeap(), 0, sizeof(unicode) + sizeof(WCHAR));
1757     ok(be_unicode_no_controls != NULL, "Expected HeapAlloc to succeed.\n");
1758     be_unicode_no_controls[0] = 0xfffe;
1759     for (i = 0; i < sizeof(unicode_no_controls)/sizeof(unicode_no_controls[0]); i++)
1760         be_unicode_no_controls[i + 1] = (unicode_no_controls[i] >> 8) | ((unicode_no_controls[i] & 0xff) << 8);
1761
1762
1763     /* The following tests verify that the tests for */
1764     /* IS_TEXT_UNICODE_CONTROLS and IS_TEXT_UNICODE_REVERSE_CONTROLS */
1765     /* are not mutually exclusive. Regardless of whether the strings */
1766     /* contain an indication of endianness, the tests are still */
1767     /* run if the flag is passed to (Rtl)IsTextUnicode. */
1768
1769     /* Test IS_TEXT_UNICODE_CONTROLS flag */
1770     flags = IS_TEXT_UNICODE_CONTROLS;
1771     ok(!pRtlIsTextUnicode(unicode_no_controls, sizeof(unicode_no_controls), &flags), "Test should not pass on Unicode string lacking control characters.\n");
1772     ok(flags == 0, "Expected flags 0x0, obtained %x\n", flags);
1773
1774     flags = IS_TEXT_UNICODE_CONTROLS;
1775     ok(!pRtlIsTextUnicode(be_unicode_no_controls, sizeof(unicode_no_controls), &flags), "Test should not pass on byte-reversed Unicode string lacking control characters.\n");
1776     ok(flags == 0, "Expected flags 0x0, obtained %x\n", flags);
1777
1778     flags = IS_TEXT_UNICODE_CONTROLS;
1779     ok(pRtlIsTextUnicode(unicode, sizeof(unicode), &flags), "Test should pass on Unicode string lacking control characters.\n");
1780     ok(flags == IS_TEXT_UNICODE_CONTROLS, "Expected flags 0x04, obtained %x\n", flags);
1781
1782     flags = IS_TEXT_UNICODE_CONTROLS;
1783     ok(!pRtlIsTextUnicode(be_unicode_no_controls, sizeof(unicode_no_controls) + 2, &flags),
1784             "Test should not pass with standard Unicode string.\n");
1785     ok(flags == 0, "Expected flags 0x0, obtained %x\n", flags);
1786
1787     flags = IS_TEXT_UNICODE_CONTROLS;
1788     ok(pRtlIsTextUnicode(mixed_controls, sizeof(mixed_controls), &flags), "Test should pass on a string containing control characters.\n");
1789     ok(flags == IS_TEXT_UNICODE_CONTROLS, "Expected flags 0x04, obtained %x\n", flags);
1790
1791     /* Test IS_TEXT_UNICODE_REVERSE_CONTROLS flag */
1792     flags = IS_TEXT_UNICODE_REVERSE_CONTROLS;
1793     ok(!pRtlIsTextUnicode(be_unicode_no_controls, sizeof(unicode_no_controls), &flags), "Test should not pass on Unicode string lacking control characters.\n");
1794     ok(flags == 0, "Expected flags 0x0, obtained %x\n", flags);
1795
1796     flags = IS_TEXT_UNICODE_REVERSE_CONTROLS;
1797     ok(!pRtlIsTextUnicode(unicode_no_controls, sizeof(unicode_no_controls), &flags), "Test should not pass on Unicode string lacking control characters.\n");
1798     ok(flags == 0, "Expected flags 0x0, obtained %x\n", flags);
1799
1800     flags = IS_TEXT_UNICODE_REVERSE_CONTROLS;
1801     ok(!pRtlIsTextUnicode(unicode, sizeof(unicode), &flags), "Test should not pass on Unicode string lacking control characters.\n");
1802     ok(flags == 0, "Expected flags 0x0, obtained %x\n", flags);
1803
1804     flags = IS_TEXT_UNICODE_REVERSE_CONTROLS;
1805     ok(!pRtlIsTextUnicode(be_unicode, sizeof(unicode) + 2, &flags),
1806         "Test should pass with byte-reversed Unicode string containing control characters.\n");
1807     ok(flags == IS_TEXT_UNICODE_REVERSE_CONTROLS, "Expected flags 0x40, obtained %x\n", flags);
1808
1809     flags = IS_TEXT_UNICODE_REVERSE_CONTROLS;
1810     ok(!pRtlIsTextUnicode(mixed_controls, sizeof(mixed_controls), &flags), "Test should pass on a string containing byte-reversed control characters.\n");
1811     ok(flags == IS_TEXT_UNICODE_REVERSE_CONTROLS, "Expected flags 0x40, obtained %x\n", flags);
1812
1813     /* Test with flags for both byte-reverse and standard Unicode characters */
1814     flags = IS_TEXT_UNICODE_CONTROLS | IS_TEXT_UNICODE_REVERSE_CONTROLS;
1815     ok(!pRtlIsTextUnicode(mixed_controls, sizeof(mixed_controls), &flags), "Test should pass on string containing both byte-reversed and standard control characters.\n");
1816     ok(flags == (IS_TEXT_UNICODE_CONTROLS | IS_TEXT_UNICODE_REVERSE_CONTROLS), "Expected flags 0x44, obtained %x\n", flags);
1817
1818     flags = IS_TEXT_UNICODE_STATISTICS;
1819     todo_wine ok(pRtlIsTextUnicode(false_positive, sizeof(false_positive), &flags), "Test should pass on false positive.\n");
1820
1821     ok(!pRtlIsTextUnicode(&false_negative, sizeof(false_negative), NULL), "Test should fail on 0x0d0a (MALAYALAM LETTER UU).\n");
1822
1823     HeapFree(GetProcessHeap(), 0, be_unicode);
1824     HeapFree(GetProcessHeap(), 0, be_unicode_no_controls);
1825 }
1826
1827 static const WCHAR szGuid[] = { '{','0','1','0','2','0','3','0','4','-',
1828   '0','5','0','6','-'  ,'0','7','0','8','-','0','9','0','A','-',
1829   '0','B','0','C','0','D','0','E','0','F','0','A','}','\0' };
1830 static const WCHAR szGuid2[] = { '{','0','1','0','2','0','3','0','4','-',
1831   '0','5','0','6','-'  ,'0','7','0','8','-','0','9','0','A','-',
1832   '0','B','0','C','0','D','0','E','0','F','0','A',']','\0' };
1833 DEFINE_GUID(IID_Endianness, 0x01020304, 0x0506, 0x0708, 0x09, 0x0A, 0x0B,
1834             0x0C, 0x0D, 0x0E, 0x0F, 0x0A);
1835
1836 static void test_RtlGUIDFromString(void)
1837 {
1838   GUID guid;
1839   UNICODE_STRING str;
1840   NTSTATUS ret;
1841
1842   str.Length = str.MaximumLength = sizeof(szGuid) - sizeof(WCHAR);
1843   str.Buffer = (LPWSTR)szGuid;
1844
1845   ret = pRtlGUIDFromString(&str, &guid);
1846   ok(ret == 0, "expected ret=0, got 0x%0x\n", ret);
1847   ok(IsEqualGUID(&guid, &IID_Endianness), "Endianness broken\n");
1848
1849   str.Length = str.MaximumLength = sizeof(szGuid2) - sizeof(WCHAR);
1850   str.Buffer = (LPWSTR)szGuid2;
1851
1852   ret = pRtlGUIDFromString(&str, &guid);
1853   ok(ret, "expected ret!=0\n");
1854 }
1855
1856 static void test_RtlStringFromGUID(void)
1857 {
1858   UNICODE_STRING str;
1859   NTSTATUS ret;
1860
1861   str.Length = str.MaximumLength = 0;
1862   str.Buffer = NULL;
1863
1864   ret = pRtlStringFromGUID(&IID_Endianness, &str);
1865   ok(ret == 0, "expected ret=0, got 0x%0x\n", ret);
1866   ok(str.Buffer && !lstrcmpiW(str.Buffer, szGuid), "Endianness broken\n");
1867   pRtlFreeUnicodeString(&str);
1868 }
1869
1870 START_TEST(rtlstr)
1871 {
1872     InitFunctionPtrs();
1873     if (pRtlInitAnsiString) {
1874         test_RtlInitString();
1875         test_RtlInitUnicodeString();
1876         test_RtlCopyString();
1877         test_RtlUnicodeStringToInteger();
1878         test_RtlCharToInteger();
1879         test_RtlIntegerToUnicodeString();
1880         test_RtlIntegerToChar();
1881         test_RtlUpperChar();
1882         test_RtlUpperString();
1883         test_RtlUnicodeStringToAnsiString();
1884         test_RtlAppendAsciizToString();
1885         test_RtlAppendStringToString();
1886         test_RtlAppendUnicodeToString();
1887         test_RtlAppendUnicodeStringToString();
1888     }
1889
1890     if (pRtlInitUnicodeStringEx)
1891         test_RtlInitUnicodeStringEx();
1892     if (pRtlDuplicateUnicodeString)
1893         test_RtlDuplicateUnicodeString();
1894     if (pRtlFindCharInUnicodeString)
1895         test_RtlFindCharInUnicodeString();
1896     if (pRtlGUIDFromString)
1897         test_RtlGUIDFromString();
1898     if (pRtlStringFromGUID)
1899         test_RtlStringFromGUID();
1900     if (pRtlIsTextUnicode)
1901         test_RtlIsTextUnicode();
1902     if(0)
1903     {
1904         test_RtlUpcaseUnicodeChar();
1905         test_RtlUpcaseUnicodeString();
1906         test_RtlDowncaseUnicodeString();
1907     }
1908 }