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