fdopen: don't rewind the file after creating the FILE* handle. Added
[wine] / dlls / ntdll / tests / rtlstr.c
1 /* Unit test suite for Rtl string functions
2  *
3  * Copyright 2002 Robert Shearman
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * NOTES
20  * We use function pointers here as there is no import library for NTDLL on
21  * windows.
22  */
23
24 #include "winbase.h"
25 #include "wine/test.h"
26 #include "winnt.h"
27 #include "winternl.h"
28
29 static UNICODE_STRING uni;
30 static STRING str;
31
32 /* Function ptrs for ntdll calls */
33 static HMODULE hntdll = 0;
34 static VOID (WINAPI *pRtlInitAnsiString)(PSTRING,LPCSTR);
35 static VOID (WINAPI *pRtlInitString)(PSTRING, LPCSTR);
36 static VOID (WINAPI *pRtlFreeAnsiString)(PSTRING);
37 static VOID (WINAPI *pRtlFreeOemString)(PSTRING);
38 static VOID (WINAPI *pRtlCopyString)(STRING *, const STRING *);
39 static VOID (WINAPI *pRtlInitUnicodeString)(PUNICODE_STRING,LPCWSTR);
40 static BOOLEAN (WINAPI *pRtlCreateUnicodeString)(PUNICODE_STRING,LPCWSTR);
41 static BOOLEAN (WINAPI *pRtlCreateUnicodeStringFromAsciiz)(PUNICODE_STRING,LPCSTR);
42 static VOID (WINAPI *pRtlFreeUnicodeString)(PUNICODE_STRING);
43 static VOID (WINAPI *pRtlCopyUnicodeString)(UNICODE_STRING *, const UNICODE_STRING *);
44 static VOID (WINAPI *pRtlEraseUnicodeString)(UNICODE_STRING *);
45 static LONG (WINAPI *pRtlCompareString)(const STRING *,const STRING *,BOOLEAN);
46 static LONG (WINAPI *pRtlCompareUnicodeString)(const UNICODE_STRING *,const UNICODE_STRING *,BOOLEAN);
47 static BOOLEAN (WINAPI *pRtlEqualString)(const STRING *,const STRING *,BOOLEAN);
48 static BOOLEAN (WINAPI *pRtlEqualUnicodeString)(const UNICODE_STRING *,const UNICODE_STRING *,BOOLEAN);
49 static BOOLEAN (WINAPI *pRtlPrefixString)(const STRING *, const STRING *, BOOLEAN);
50 static BOOLEAN (WINAPI *pRtlPrefixUnicodeString)(const UNICODE_STRING *, const UNICODE_STRING *, BOOLEAN);
51 static NTSTATUS (WINAPI *pRtlAnsiStringToUnicodeString)(PUNICODE_STRING, PCANSI_STRING, BOOLEAN);
52 static NTSTATUS (WINAPI *pRtlOemStringToUnicodeString)(PUNICODE_STRING, const STRING *, BOOLEAN);
53 static NTSTATUS (WINAPI *pRtlUnicodeStringToAnsiString)(STRING *, const UNICODE_STRING *, BOOLEAN);
54 static NTSTATUS (WINAPI *pRtlUnicodeStringToOemString)(STRING *, const UNICODE_STRING *, BOOLEAN);
55 static NTSTATUS (WINAPI *pRtlMultiByteToUnicodeN)(LPWSTR, DWORD, LPDWORD, LPCSTR, DWORD);
56 static NTSTATUS (WINAPI *pRtlOemToUnicodeN)(LPWSTR, DWORD, LPDWORD, LPCSTR, DWORD);
57 static NTSTATUS (WINAPI *pRtlUpperString)(STRING *, const STRING *);
58 static NTSTATUS (WINAPI *pRtlUpcaseUnicodeString)(UNICODE_STRING *, const UNICODE_STRING *, BOOLEAN);
59 static NTSTATUS (WINAPI *pRtlUpcaseUnicodeStringToAnsiString)(STRING *, const UNICODE_STRING *, BOOLEAN);
60 static NTSTATUS (WINAPI *pRtlUpcaseUnicodeStringToOemString)(STRING *, const UNICODE_STRING *, BOOLEAN);
61 static NTSTATUS (WINAPI *pRtlUpcaseUnicodeToMultiByteN)(LPSTR, DWORD, LPDWORD, LPCWSTR, DWORD);
62 static NTSTATUS (WINAPI *pRtlUpcaseUnicodeToOemN)(LPSTR, DWORD, LPDWORD, LPCWSTR, DWORD);
63 static UINT (WINAPI *pRtlOemToUnicodeSize)(const STRING *);
64 static DWORD (WINAPI *pRtlAnsiStringToUnicodeSize)(const STRING *);
65 static NTSTATUS (WINAPI *pRtlOemToUnicodeN)(LPWSTR, DWORD, LPDWORD, LPCSTR, DWORD);
66 static NTSTATUS (WINAPI *pRtlOemToUnicodeN)(LPWSTR, DWORD, LPDWORD, LPCSTR, DWORD);
67
68 /* more function pointers here */
69
70 static DWORD (WINAPI *pRtlIsTextUnicode)(LPVOID, DWORD, DWORD *);
71 static NTSTATUS (WINAPI *pRtlUnicodeStringToInteger)(const UNICODE_STRING *, int, int *);
72
73 static void InitFunctionPtrs()
74 {
75   hntdll = LoadLibraryA("ntdll.dll");
76   ok(hntdll != 0, "LoadLibrary failed");
77   if (hntdll)
78   {
79         pRtlInitAnsiString = (void *)GetProcAddress(hntdll, "RtlInitAnsiString");
80         pRtlInitString = (void *)GetProcAddress(hntdll, "RtlInitString");
81         pRtlInitUnicodeString = (void *)GetProcAddress(hntdll, "RtlInitUnicodeString");
82         pRtlCopyString = (void *)GetProcAddress(hntdll, "RtlCopyString");
83         pRtlCreateUnicodeString = (void *)GetProcAddress(hntdll, "RtlCreateUnicodeString");
84         pRtlUnicodeStringToInteger = (void *)GetProcAddress(hntdll, "RtlUnicodeStringToInteger");
85   }
86 }
87
88 static void test_RtlInitString(void)
89 {
90         static const char teststring[] = "Some Wild String";
91         str.Length = 0;
92         str.MaximumLength = 0;
93         str.Buffer = (void *)0xdeadbeef;
94         pRtlInitString(&str, teststring);
95         ok(str.Length == sizeof(teststring) - sizeof(char), "Length uninitialized");
96         ok(str.MaximumLength == sizeof(teststring), "MaximumLength uninitialized");
97         ok(strcmp(str.Buffer, "Some Wild String") == 0, "Buffer written to");
98 }
99
100 static void test_RtlInitUnicodeString(void)
101 {
102 #define STRINGW {'S','o','m','e',' ','W','i','l','d',' ','S','t','r','i','n','g',0}
103     static const WCHAR teststring[] = STRINGW;
104         static const WCHAR originalstring[] = STRINGW;
105 #undef STRINGW
106         uni.Length = 0;
107         uni.MaximumLength = 0;
108         uni.Buffer = (void *)0xdeadbeef;
109         pRtlInitUnicodeString(&uni, teststring);
110         ok(uni.Length == sizeof(teststring) - sizeof(WCHAR), "Length uninitialized");
111         ok(uni.MaximumLength == sizeof(teststring), "MaximumLength uninitialized");
112         ok(wcscmp(uni.Buffer, originalstring) == 0, "Buffer written to");
113 }
114
115 static void test_RtlCopyString(void)
116 {
117         static const char teststring[] = "Some Wild String";
118         static char deststring[] = "                    ";
119         STRING deststr;
120         pRtlInitString(&str, teststring);
121         pRtlInitString(&deststr, deststring);
122         pRtlCopyString(&deststr, &str);
123         ok(strncmp(str.Buffer, deststring, str.Length) == 0, "String not copied");
124 }
125
126 static void test_RtlUnicodeStringToInteger(void)
127 {
128         int dest = 0;
129         int i;
130         DWORD result;
131
132         /* keep these next two arrays in sync. or they'll be trouble! */
133         static const WCHAR stringwithint[][12] = {
134                 {'1','0','1','1','1','0','1','1','0','0',0},
135                 {'1','2','3','4','5','6','7',0},
136                 {'2','1','4','7','4','8','3','6','4','8',0},
137                 {'-','2','1','4','7','4','8','3','6','4','8',0},
138                 {'-','2','1','4',0},
139                 {' ','\n',' ','\r','2','1','4',0},
140                 {'+','2','1','4',' ','0',0},
141                 {' ','2','1','4','.','0','1',0},
142                 {'f','8','1',0},
143                 {'0','x','1','2','3','4','5',0},
144                 {'1','x','3','4',0}
145         };
146         static const int expectedresults[] = {
147                 1011101100,
148                 1234567,
149                 2147483648,
150                 2147483648,
151                 -214,
152                 214,
153                 214,
154                 214,
155                 0,
156                 0x12345,
157                 1
158         };
159         /* these are for stringwithint[0]: */
160         static const int expectedresultsbase[] = {
161                 748, /* base 2 */
162                 136610368, /* base 8 */
163                 1011101100, /* base 10 */
164                 286265600, /* base 16 */
165         };
166         
167         for (i = 0; i < sizeof(expectedresults) / sizeof(int); i++)
168         {
169                 dest = 0xdeadbeef;
170                 pRtlInitUnicodeString(&uni, stringwithint[i]);
171                 result = pRtlUnicodeStringToInteger(&uni, 0, &dest);
172                 ok(result == 0, "call failed: RtlUnicodeStringToInteger(\"%S\", %d, [out])", uni.Buffer, 0);
173                 ok(dest == expectedresults[i], "didn't return expected value (test %d): expected: %d, got: %d}", i, expectedresults[i], dest);
174         }
175         pRtlInitUnicodeString(&uni, stringwithint[0]);
176         result = pRtlUnicodeStringToInteger(&uni, 2, &dest);
177         ok(result == 0, "call failed: RtlUnicodeStringToInteger(\"%S\", %d, [out])", uni.Buffer, 2);
178         ok(dest == expectedresultsbase[0], "didn't return expected value: \"%S\"; expected: %d, got: %d}", uni.Buffer, expectedresultsbase[0], dest);
179         result = pRtlUnicodeStringToInteger(&uni, 8, &dest);
180         ok(result == 0, "call failed: RtlUnicodeStringToInteger(\"%S\", %d, [out])", uni.Buffer, 8);
181         ok(dest == expectedresultsbase[1], "didn't return expected value: \"%S\"; expected: %d, got: %d}", uni.Buffer, expectedresultsbase[1], dest);
182         result = pRtlUnicodeStringToInteger(&uni, 10, &dest);
183         ok(result == 0, "call failed: RtlUnicodeStringToInteger(\"%S\", %d, [out])", uni.Buffer, 10);
184         ok(dest == expectedresultsbase[2], "didn't return expected value: \"%S\"; expected: %d, got: %d}", uni.Buffer, expectedresultsbase[2], dest);
185         result = pRtlUnicodeStringToInteger(&uni, 16, &dest);
186         ok(result == 0, "call failed: RtlUnicodeStringToInteger(\"%S\", %d, [out])", uni.Buffer, 16);
187         ok(dest == expectedresultsbase[3], "didn't return expected value: \"%S\"; expected: %d, got: %d}", uni.Buffer, expectedresultsbase[3], dest);
188 }
189
190 START_TEST(rtlstr)
191 {
192   InitFunctionPtrs();
193
194   if (pRtlInitAnsiString)
195   {
196     test_RtlInitString();
197     test_RtlInitUnicodeString();
198     test_RtlCopyString();
199     test_RtlUnicodeStringToInteger();
200   }
201 }