ntdll: Initialize critical section debug info with correct pointer value.
[wine.git] / dlls / ntdll / tests / rtl.c
blob62662cb438451e6371060d1577936fdcdefa64c3
1 /* Unit test suite for Rtl* API functions
3 * Copyright 2003 Thomas Mertes
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 * NOTES
20 * We use function pointers here as there is no import library for NTDLL on
21 * windows.
24 #include <stdlib.h>
26 #include "ntdll_test.h"
27 #include "in6addr.h"
28 #include "inaddr.h"
30 #ifndef __WINE_WINTERNL_H
32 typedef struct _RTL_HANDLE
34 struct _RTL_HANDLE * Next;
35 } RTL_HANDLE;
37 typedef struct _RTL_HANDLE_TABLE
39 ULONG MaxHandleCount;
40 ULONG HandleSize;
41 ULONG Unused[2];
42 PVOID NextFree;
43 PVOID FirstHandle;
44 PVOID ReservedMemory;
45 PVOID MaxHandle;
46 } RTL_HANDLE_TABLE;
48 #endif
50 /* avoid #include <winsock2.h> */
51 #undef htons
52 #ifdef WORDS_BIGENDIAN
53 #define htons(s) ((USHORT)(s))
54 #else /* WORDS_BIGENDIAN */
55 static inline USHORT __my_ushort_swap(USHORT s)
57 return (s >> 8) | (s << 8);
59 #define htons(s) __my_ushort_swap(s)
60 #endif /* WORDS_BIGENDIAN */
64 /* Function ptrs for ntdll calls */
65 static HMODULE hntdll = 0;
66 static SIZE_T (WINAPI *pRtlCompareMemory)(LPCVOID,LPCVOID,SIZE_T);
67 static SIZE_T (WINAPI *pRtlCompareMemoryUlong)(PULONG, SIZE_T, ULONG);
68 static NTSTATUS (WINAPI *pRtlDeleteTimer)(HANDLE, HANDLE, HANDLE);
69 static VOID (WINAPI *pRtlMoveMemory)(LPVOID,LPCVOID,SIZE_T);
70 static VOID (WINAPI *pRtlFillMemory)(LPVOID,SIZE_T,BYTE);
71 static VOID (WINAPI *pRtlFillMemoryUlong)(LPVOID,SIZE_T,ULONG);
72 static VOID (WINAPI *pRtlZeroMemory)(LPVOID,SIZE_T);
73 static ULONGLONG (WINAPIV *pRtlUlonglongByteSwap)(ULONGLONG source);
74 static ULONG (WINAPI *pRtlUniform)(PULONG);
75 static ULONG (WINAPI *pRtlRandom)(PULONG);
76 static BOOLEAN (WINAPI *pRtlAreAllAccessesGranted)(ACCESS_MASK, ACCESS_MASK);
77 static BOOLEAN (WINAPI *pRtlAreAnyAccessesGranted)(ACCESS_MASK, ACCESS_MASK);
78 static DWORD (WINAPI *pRtlComputeCrc32)(DWORD,const BYTE*,INT);
79 static void (WINAPI * pRtlInitializeHandleTable)(ULONG, ULONG, RTL_HANDLE_TABLE *);
80 static BOOLEAN (WINAPI * pRtlIsValidIndexHandle)(const RTL_HANDLE_TABLE *, ULONG, RTL_HANDLE **);
81 static NTSTATUS (WINAPI * pRtlDestroyHandleTable)(RTL_HANDLE_TABLE *);
82 static RTL_HANDLE * (WINAPI * pRtlAllocateHandle)(RTL_HANDLE_TABLE *, ULONG *);
83 static BOOLEAN (WINAPI * pRtlFreeHandle)(RTL_HANDLE_TABLE *, RTL_HANDLE *);
84 static NTSTATUS (WINAPI *pRtlAllocateAndInitializeSid)(PSID_IDENTIFIER_AUTHORITY,BYTE,DWORD,DWORD,DWORD,DWORD,DWORD,DWORD,DWORD,DWORD,PSID*);
85 static NTSTATUS (WINAPI *pRtlFreeSid)(PSID);
86 static DWORD (WINAPI *pRtlGetThreadErrorMode)(void);
87 static NTSTATUS (WINAPI *pRtlSetThreadErrorMode)(DWORD, LPDWORD);
88 static IMAGE_BASE_RELOCATION *(WINAPI *pLdrProcessRelocationBlock)(void*,UINT,USHORT*,INT_PTR);
89 static CHAR * (WINAPI *pRtlIpv4AddressToStringA)(const IN_ADDR *, LPSTR);
90 static NTSTATUS (WINAPI *pRtlIpv4AddressToStringExA)(const IN_ADDR *, USHORT, LPSTR, PULONG);
91 static NTSTATUS (WINAPI *pRtlIpv4StringToAddressA)(PCSTR, BOOLEAN, PCSTR *, IN_ADDR *);
92 static NTSTATUS (WINAPI *pRtlIpv4StringToAddressExA)(PCSTR, BOOLEAN, IN_ADDR *, PUSHORT);
93 static CHAR * (WINAPI *pRtlIpv6AddressToStringA)(struct in6_addr *, PSTR);
94 static NTSTATUS (WINAPI *pRtlIpv6AddressToStringExA)(struct in6_addr *, ULONG, USHORT, PCHAR, PULONG);
95 static NTSTATUS (WINAPI *pRtlIpv6StringToAddressA)(PCSTR, PCSTR *, struct in6_addr *);
96 static NTSTATUS (WINAPI *pRtlIpv6StringToAddressW)(PCWSTR, PCWSTR *, struct in6_addr *);
97 static NTSTATUS (WINAPI *pRtlIpv6StringToAddressExA)(PCSTR, struct in6_addr *, PULONG, PUSHORT);
98 static NTSTATUS (WINAPI *pRtlIpv6StringToAddressExW)(PCWSTR, struct in6_addr *, PULONG, PUSHORT);
99 static NTSTATUS (WINAPI *pLdrAddRefDll)(ULONG, HMODULE);
100 static NTSTATUS (WINAPI *pLdrLockLoaderLock)(ULONG, ULONG*, ULONG_PTR*);
101 static NTSTATUS (WINAPI *pLdrUnlockLoaderLock)(ULONG, ULONG_PTR);
102 static NTSTATUS (WINAPI *pRtlMultiByteToUnicodeN)(LPWSTR, DWORD, LPDWORD, LPCSTR, DWORD);
103 static NTSTATUS (WINAPI *pRtlGetCompressionWorkSpaceSize)(USHORT, PULONG, PULONG);
104 static NTSTATUS (WINAPI *pRtlDecompressBuffer)(USHORT, PUCHAR, ULONG, const UCHAR*, ULONG, PULONG);
105 static NTSTATUS (WINAPI *pRtlDecompressFragment)(USHORT, PUCHAR, ULONG, const UCHAR*, ULONG, ULONG, PULONG, PVOID);
106 static NTSTATUS (WINAPI *pRtlCompressBuffer)(USHORT, const UCHAR*, ULONG, PUCHAR, ULONG, ULONG, PULONG, PVOID);
107 static BOOL (WINAPI *pRtlIsCriticalSectionLocked)(CRITICAL_SECTION *);
108 static BOOL (WINAPI *pRtlIsCriticalSectionLockedByThread)(CRITICAL_SECTION *);
109 static NTSTATUS (WINAPI *pRtlInitializeCriticalSectionEx)(CRITICAL_SECTION *, ULONG, ULONG);
110 static NTSTATUS (WINAPI *pLdrEnumerateLoadedModules)(void *, void *, void *);
111 static NTSTATUS (WINAPI *pRtlMakeSelfRelativeSD)(PSECURITY_DESCRIPTOR,PSECURITY_DESCRIPTOR,LPDWORD);
112 static NTSTATUS (WINAPI *pRtlAbsoluteToSelfRelativeSD)(PSECURITY_DESCRIPTOR,PSECURITY_DESCRIPTOR,PULONG);
113 static NTSTATUS (WINAPI *pLdrRegisterDllNotification)(ULONG, PLDR_DLL_NOTIFICATION_FUNCTION, void *, void **);
114 static NTSTATUS (WINAPI *pLdrUnregisterDllNotification)(void *);
116 static HMODULE hkernel32 = 0;
117 static BOOL (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
120 #define LEN 16
121 static const char* src_src = "This is a test!"; /* 16 bytes long, incl NUL */
122 static WCHAR ws2_32dllW[] = {'w','s','2','_','3','2','.','d','l','l',0};
123 static WCHAR nsidllW[] = {'n','s','i','.','d','l','l',0};
124 static WCHAR wintrustdllW[] = {'w','i','n','t','r','u','s','t','.','d','l','l',0};
125 static WCHAR crypt32dllW[] = {'c','r','y','p','t','3','2','.','d','l','l',0};
126 static ULONG src_aligned_block[4];
127 static ULONG dest_aligned_block[32];
128 static const char *src = (const char*)src_aligned_block;
129 static char* dest = (char*)dest_aligned_block;
130 const WCHAR *expected_dll = nsidllW;
132 static void InitFunctionPtrs(void)
134 hntdll = LoadLibraryA("ntdll.dll");
135 ok(hntdll != 0, "LoadLibrary failed\n");
136 if (hntdll) {
137 pRtlCompareMemory = (void *)GetProcAddress(hntdll, "RtlCompareMemory");
138 pRtlCompareMemoryUlong = (void *)GetProcAddress(hntdll, "RtlCompareMemoryUlong");
139 pRtlDeleteTimer = (void *)GetProcAddress(hntdll, "RtlDeleteTimer");
140 pRtlMoveMemory = (void *)GetProcAddress(hntdll, "RtlMoveMemory");
141 pRtlFillMemory = (void *)GetProcAddress(hntdll, "RtlFillMemory");
142 pRtlFillMemoryUlong = (void *)GetProcAddress(hntdll, "RtlFillMemoryUlong");
143 pRtlZeroMemory = (void *)GetProcAddress(hntdll, "RtlZeroMemory");
144 pRtlUlonglongByteSwap = (void *)GetProcAddress(hntdll, "RtlUlonglongByteSwap");
145 pRtlUniform = (void *)GetProcAddress(hntdll, "RtlUniform");
146 pRtlRandom = (void *)GetProcAddress(hntdll, "RtlRandom");
147 pRtlAreAllAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAllAccessesGranted");
148 pRtlAreAnyAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAnyAccessesGranted");
149 pRtlComputeCrc32 = (void *)GetProcAddress(hntdll, "RtlComputeCrc32");
150 pRtlInitializeHandleTable = (void *)GetProcAddress(hntdll, "RtlInitializeHandleTable");
151 pRtlIsValidIndexHandle = (void *)GetProcAddress(hntdll, "RtlIsValidIndexHandle");
152 pRtlDestroyHandleTable = (void *)GetProcAddress(hntdll, "RtlDestroyHandleTable");
153 pRtlAllocateHandle = (void *)GetProcAddress(hntdll, "RtlAllocateHandle");
154 pRtlFreeHandle = (void *)GetProcAddress(hntdll, "RtlFreeHandle");
155 pRtlAllocateAndInitializeSid = (void *)GetProcAddress(hntdll, "RtlAllocateAndInitializeSid");
156 pRtlFreeSid = (void *)GetProcAddress(hntdll, "RtlFreeSid");
157 pRtlGetThreadErrorMode = (void *)GetProcAddress(hntdll, "RtlGetThreadErrorMode");
158 pRtlSetThreadErrorMode = (void *)GetProcAddress(hntdll, "RtlSetThreadErrorMode");
159 pLdrProcessRelocationBlock = (void *)GetProcAddress(hntdll, "LdrProcessRelocationBlock");
160 pRtlIpv4AddressToStringA = (void *)GetProcAddress(hntdll, "RtlIpv4AddressToStringA");
161 pRtlIpv4AddressToStringExA = (void *)GetProcAddress(hntdll, "RtlIpv4AddressToStringExA");
162 pRtlIpv4StringToAddressA = (void *)GetProcAddress(hntdll, "RtlIpv4StringToAddressA");
163 pRtlIpv4StringToAddressExA = (void *)GetProcAddress(hntdll, "RtlIpv4StringToAddressExA");
164 pRtlIpv6AddressToStringA = (void *)GetProcAddress(hntdll, "RtlIpv6AddressToStringA");
165 pRtlIpv6AddressToStringExA = (void *)GetProcAddress(hntdll, "RtlIpv6AddressToStringExA");
166 pRtlIpv6StringToAddressA = (void *)GetProcAddress(hntdll, "RtlIpv6StringToAddressA");
167 pRtlIpv6StringToAddressW = (void *)GetProcAddress(hntdll, "RtlIpv6StringToAddressW");
168 pRtlIpv6StringToAddressExA = (void *)GetProcAddress(hntdll, "RtlIpv6StringToAddressExA");
169 pRtlIpv6StringToAddressExW = (void *)GetProcAddress(hntdll, "RtlIpv6StringToAddressExW");
170 pLdrAddRefDll = (void *)GetProcAddress(hntdll, "LdrAddRefDll");
171 pLdrLockLoaderLock = (void *)GetProcAddress(hntdll, "LdrLockLoaderLock");
172 pLdrUnlockLoaderLock = (void *)GetProcAddress(hntdll, "LdrUnlockLoaderLock");
173 pRtlMultiByteToUnicodeN = (void *)GetProcAddress(hntdll, "RtlMultiByteToUnicodeN");
174 pRtlGetCompressionWorkSpaceSize = (void *)GetProcAddress(hntdll, "RtlGetCompressionWorkSpaceSize");
175 pRtlDecompressBuffer = (void *)GetProcAddress(hntdll, "RtlDecompressBuffer");
176 pRtlDecompressFragment = (void *)GetProcAddress(hntdll, "RtlDecompressFragment");
177 pRtlCompressBuffer = (void *)GetProcAddress(hntdll, "RtlCompressBuffer");
178 pRtlIsCriticalSectionLocked = (void *)GetProcAddress(hntdll, "RtlIsCriticalSectionLocked");
179 pRtlIsCriticalSectionLockedByThread = (void *)GetProcAddress(hntdll, "RtlIsCriticalSectionLockedByThread");
180 pRtlInitializeCriticalSectionEx = (void *)GetProcAddress(hntdll, "RtlInitializeCriticalSectionEx");
181 pLdrEnumerateLoadedModules = (void *)GetProcAddress(hntdll, "LdrEnumerateLoadedModules");
182 pRtlMakeSelfRelativeSD = (void *)GetProcAddress(hntdll, "RtlMakeSelfRelativeSD");
183 pRtlAbsoluteToSelfRelativeSD = (void *)GetProcAddress(hntdll, "RtlAbsoluteToSelfRelativeSD");
184 pLdrRegisterDllNotification = (void *)GetProcAddress(hntdll, "LdrRegisterDllNotification");
185 pLdrUnregisterDllNotification = (void *)GetProcAddress(hntdll, "LdrUnregisterDllNotification");
187 hkernel32 = LoadLibraryA("kernel32.dll");
188 ok(hkernel32 != 0, "LoadLibrary failed\n");
189 if (hkernel32) {
190 pIsWow64Process = (void *)GetProcAddress(hkernel32, "IsWow64Process");
192 strcpy((char*)src_aligned_block, src_src);
193 ok(strlen(src) == 15, "Source must be 16 bytes long!\n");
196 #define COMP(str1,str2,cmplen,len) size = pRtlCompareMemory(str1, str2, cmplen); \
197 ok(size == len, "Expected %ld, got %ld\n", size, (SIZE_T)len)
199 static void test_RtlCompareMemory(void)
201 SIZE_T size;
203 if (!pRtlCompareMemory)
205 win_skip("RtlCompareMemory is not available\n");
206 return;
209 strcpy(dest, src);
211 COMP(src,src,0,0);
212 COMP(src,src,LEN,LEN);
213 dest[0] = 'x';
214 COMP(src,dest,LEN,0);
217 static void test_RtlCompareMemoryUlong(void)
219 ULONG a[10];
220 ULONG result;
222 if (!pRtlCompareMemoryUlong)
224 win_skip("RtlCompareMemoryUlong is not available\n");
225 return;
228 a[0]= 0x0123;
229 a[1]= 0x4567;
230 a[2]= 0x89ab;
231 a[3]= 0xcdef;
232 result = pRtlCompareMemoryUlong(a, 0, 0x0123);
233 ok(result == 0, "RtlCompareMemoryUlong(%p, 0, 0x0123) returns %u, expected 0\n", a, result);
234 result = pRtlCompareMemoryUlong(a, 3, 0x0123);
235 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %u, expected 0\n", a, result);
236 result = pRtlCompareMemoryUlong(a, 4, 0x0123);
237 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %u, expected 4\n", a, result);
238 result = pRtlCompareMemoryUlong(a, 5, 0x0123);
239 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %u, expected 4\n", a, result);
240 result = pRtlCompareMemoryUlong(a, 7, 0x0123);
241 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %u, expected 4\n", a, result);
242 result = pRtlCompareMemoryUlong(a, 8, 0x0123);
243 ok(result == 4, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %u, expected 4\n", a, result);
244 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
245 ok(result == 4, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %u, expected 4\n", a, result);
246 result = pRtlCompareMemoryUlong(a, 4, 0x0127);
247 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x0127) returns %u, expected 0\n", a, result);
248 result = pRtlCompareMemoryUlong(a, 4, 0x7123);
249 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x7123) returns %u, expected 0\n", a, result);
250 result = pRtlCompareMemoryUlong(a, 16, 0x4567);
251 ok(result == 0, "RtlCompareMemoryUlong(%p, 16, 0x4567) returns %u, expected 0\n", a, result);
253 a[1]= 0x0123;
254 result = pRtlCompareMemoryUlong(a, 3, 0x0123);
255 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %u, expected 0\n", a, result);
256 result = pRtlCompareMemoryUlong(a, 4, 0x0123);
257 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %u, expected 4\n", a, result);
258 result = pRtlCompareMemoryUlong(a, 5, 0x0123);
259 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %u, expected 4\n", a, result);
260 result = pRtlCompareMemoryUlong(a, 7, 0x0123);
261 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %u, expected 4\n", a, result);
262 result = pRtlCompareMemoryUlong(a, 8, 0x0123);
263 ok(result == 8, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %u, expected 8\n", a, result);
264 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
265 ok(result == 8, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %u, expected 8\n", a, result);
268 #define COPY(len) memset(dest,0,sizeof(dest_aligned_block)); pRtlMoveMemory(dest, src, len)
269 #define CMP(str) ok(strcmp(dest,str) == 0, "Expected '%s', got '%s'\n", str, dest)
271 static void test_RtlMoveMemory(void)
273 if (!pRtlMoveMemory)
275 win_skip("RtlMoveMemory is not available\n");
276 return;
279 /* Length should be in bytes and not rounded. Use strcmp to ensure we
280 * didn't write past the end (it checks for the final NUL left by memset)
282 COPY(0); CMP("");
283 COPY(1); CMP("T");
284 COPY(2); CMP("Th");
285 COPY(3); CMP("Thi");
286 COPY(4); CMP("This");
287 COPY(5); CMP("This ");
288 COPY(6); CMP("This i");
289 COPY(7); CMP("This is");
290 COPY(8); CMP("This is ");
291 COPY(9); CMP("This is a");
293 /* Overlapping */
294 strcpy(dest, src); pRtlMoveMemory(dest, dest + 1, strlen(src) - 1);
295 CMP("his is a test!!");
296 strcpy(dest, src); pRtlMoveMemory(dest + 1, dest, strlen(src));
297 CMP("TThis is a test!");
300 #define FILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemory(dest,len,'x')
302 static void test_RtlFillMemory(void)
304 if (!pRtlFillMemory)
306 win_skip("RtlFillMemory is not available\n");
307 return;
310 /* Length should be in bytes and not rounded. Use strcmp to ensure we
311 * didn't write past the end (the remainder of the string should match)
313 FILL(0); CMP("This is a test!");
314 FILL(1); CMP("xhis is a test!");
315 FILL(2); CMP("xxis is a test!");
316 FILL(3); CMP("xxxs is a test!");
317 FILL(4); CMP("xxxx is a test!");
318 FILL(5); CMP("xxxxxis a test!");
319 FILL(6); CMP("xxxxxxs a test!");
320 FILL(7); CMP("xxxxxxx a test!");
321 FILL(8); CMP("xxxxxxxxa test!");
322 FILL(9); CMP("xxxxxxxxx test!");
325 #define LFILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemoryUlong(dest,len,val)
327 static void test_RtlFillMemoryUlong(void)
329 ULONG val = ('x' << 24) | ('x' << 16) | ('x' << 8) | 'x';
330 if (!pRtlFillMemoryUlong)
332 win_skip("RtlFillMemoryUlong is not available\n");
333 return;
336 /* Length should be in bytes and not rounded. Use strcmp to ensure we
337 * didn't write past the end (the remainder of the string should match)
339 LFILL(0); CMP("This is a test!");
340 LFILL(1); CMP("This is a test!");
341 LFILL(2); CMP("This is a test!");
342 LFILL(3); CMP("This is a test!");
343 LFILL(4); CMP("xxxx is a test!");
344 LFILL(5); CMP("xxxx is a test!");
345 LFILL(6); CMP("xxxx is a test!");
346 LFILL(7); CMP("xxxx is a test!");
347 LFILL(8); CMP("xxxxxxxxa test!");
348 LFILL(9); CMP("xxxxxxxxa test!");
351 #define ZERO(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlZeroMemory(dest,len)
352 #define MCMP(str) ok(memcmp(dest,str,LEN) == 0, "Memcmp failed\n")
354 static void test_RtlZeroMemory(void)
356 if (!pRtlZeroMemory)
358 win_skip("RtlZeroMemory is not available\n");
359 return;
362 /* Length should be in bytes and not rounded. */
363 ZERO(0); MCMP("This is a test!");
364 ZERO(1); MCMP("\0his is a test!");
365 ZERO(2); MCMP("\0\0is is a test!");
366 ZERO(3); MCMP("\0\0\0s is a test!");
367 ZERO(4); MCMP("\0\0\0\0 is a test!");
368 ZERO(5); MCMP("\0\0\0\0\0is a test!");
369 ZERO(6); MCMP("\0\0\0\0\0\0s a test!");
370 ZERO(7); MCMP("\0\0\0\0\0\0\0 a test!");
371 ZERO(8); MCMP("\0\0\0\0\0\0\0\0a test!");
372 ZERO(9); MCMP("\0\0\0\0\0\0\0\0\0 test!");
375 static void test_RtlUlonglongByteSwap(void)
377 ULONGLONG result;
379 if ( !pRtlUlonglongByteSwap )
381 win_skip("RtlUlonglongByteSwap is not available\n");
382 return;
385 if ( pRtlUlonglongByteSwap( 0 ) != 0 )
387 win_skip("Broken RtlUlonglongByteSwap in win2k\n");
388 return;
391 result = pRtlUlonglongByteSwap( ((ULONGLONG)0x76543210 << 32) | 0x87654321 );
392 ok( (((ULONGLONG)0x21436587 << 32) | 0x10325476) == result,
393 "RtlUlonglongByteSwap(0x7654321087654321) returns 0x%s, expected 0x2143658710325476\n",
394 wine_dbgstr_longlong(result));
398 static void test_RtlUniform(void)
400 ULONGLONG num;
401 ULONG seed;
402 ULONG seed_bak;
403 ULONG expected;
404 ULONG result;
406 if (!pRtlUniform)
408 win_skip("RtlUniform is not available\n");
409 return;
413 * According to the documentation RtlUniform is using D.H. Lehmer's 1948
414 * algorithm. This algorithm is:
416 * seed = (seed * const_1 + const_2) % const_3;
418 * According to the documentation the random number is distributed over
419 * [0..MAXLONG]. Therefore const_3 is MAXLONG + 1:
421 * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
423 * Because MAXLONG is 0x7fffffff (and MAXLONG + 1 is 0x80000000) the
424 * algorithm can be expressed without division as:
426 * seed = (seed * const_1 + const_2) & MAXLONG;
428 * To find out const_2 we just call RtlUniform with seed set to 0:
430 seed = 0;
431 expected = 0x7fffffc3;
432 result = pRtlUniform(&seed);
433 ok(result == expected,
434 "RtlUniform(&seed (seed == 0)) returns %x, expected %x\n",
435 result, expected);
437 * The algorithm is now:
439 * seed = (seed * const_1 + 0x7fffffc3) & MAXLONG;
441 * To find out const_1 we can use:
443 * const_1 = RtlUniform(1) - 0x7fffffc3;
445 * If that does not work a search loop can try all possible values of
446 * const_1 and compare to the result to RtlUniform(1).
447 * This way we find out that const_1 is 0xffffffed.
449 * For seed = 1 the const_2 is 0x7fffffc4:
451 seed = 1;
452 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
453 result = pRtlUniform(&seed);
454 ok(result == expected,
455 "RtlUniform(&seed (seed == 1)) returns %x, expected %x\n",
456 result, expected);
458 * For seed = 2 the const_2 is 0x7fffffc3:
460 seed = 2;
461 expected = seed * 0xffffffed + 0x7fffffc3;
462 result = pRtlUniform(&seed);
465 * Windows Vista uses different algorithms, so skip the rest of the tests
466 * until that is figured out. Trace output for the failures is about 10.5 MB!
469 if (result == 0x7fffff9f) {
470 skip("Most likely running on Windows Vista which uses a different algorithm\n");
471 return;
474 ok(result == expected,
475 "RtlUniform(&seed (seed == 2)) returns %x, expected %x\n",
476 result, expected);
479 * More tests show that if seed is odd the result must be incremented by 1:
481 seed = 3;
482 expected = seed * 0xffffffed + 0x7fffffc3 + (seed & 1);
483 result = pRtlUniform(&seed);
484 ok(result == expected,
485 "RtlUniform(&seed (seed == 3)) returns %x, expected %x\n",
486 result, expected);
488 seed = 0x6bca1aa;
489 expected = seed * 0xffffffed + 0x7fffffc3;
490 result = pRtlUniform(&seed);
491 ok(result == expected,
492 "RtlUniform(&seed (seed == 0x6bca1aa)) returns %x, expected %x\n",
493 result, expected);
495 seed = 0x6bca1ab;
496 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
497 result = pRtlUniform(&seed);
498 ok(result == expected,
499 "RtlUniform(&seed (seed == 0x6bca1ab)) returns %x, expected %x\n",
500 result, expected);
502 * When seed is 0x6bca1ac there is an exception:
504 seed = 0x6bca1ac;
505 expected = seed * 0xffffffed + 0x7fffffc3 + 2;
506 result = pRtlUniform(&seed);
507 ok(result == expected,
508 "RtlUniform(&seed (seed == 0x6bca1ac)) returns %x, expected %x\n",
509 result, expected);
511 * Note that up to here const_3 is not used
512 * (the highest bit of the result is not set).
514 * Starting with 0x6bca1ad: If seed is even the result must be incremented by 1:
516 seed = 0x6bca1ad;
517 expected = (seed * 0xffffffed + 0x7fffffc3) & MAXLONG;
518 result = pRtlUniform(&seed);
519 ok(result == expected,
520 "RtlUniform(&seed (seed == 0x6bca1ad)) returns %x, expected %x\n",
521 result, expected);
523 seed = 0x6bca1ae;
524 expected = (seed * 0xffffffed + 0x7fffffc3 + 1) & MAXLONG;
525 result = pRtlUniform(&seed);
526 ok(result == expected,
527 "RtlUniform(&seed (seed == 0x6bca1ae)) returns %x, expected %x\n",
528 result, expected);
530 * There are several ranges where for odd or even seed the result must be
531 * incremented by 1. You can see this ranges in the following test.
533 * For a full test use one of the following loop heads:
535 * for (num = 0; num <= 0xffffffff; num++) {
536 * seed = num;
537 * ...
539 * seed = 0;
540 * for (num = 0; num <= 0xffffffff; num++) {
541 * ...
543 seed = 0;
544 for (num = 0; num <= 100000; num++) {
546 expected = seed * 0xffffffed + 0x7fffffc3;
547 if (seed < 0x6bca1ac) {
548 expected = expected + (seed & 1);
549 } else if (seed == 0x6bca1ac) {
550 expected = (expected + 2) & MAXLONG;
551 } else if (seed < 0xd79435c) {
552 expected = (expected + (~seed & 1)) & MAXLONG;
553 } else if (seed < 0x1435e50b) {
554 expected = expected + (seed & 1);
555 } else if (seed < 0x1af286ba) {
556 expected = (expected + (~seed & 1)) & MAXLONG;
557 } else if (seed < 0x21af2869) {
558 expected = expected + (seed & 1);
559 } else if (seed < 0x286bca18) {
560 expected = (expected + (~seed & 1)) & MAXLONG;
561 } else if (seed < 0x2f286bc7) {
562 expected = expected + (seed & 1);
563 } else if (seed < 0x35e50d77) {
564 expected = (expected + (~seed & 1)) & MAXLONG;
565 } else if (seed < 0x3ca1af26) {
566 expected = expected + (seed & 1);
567 } else if (seed < 0x435e50d5) {
568 expected = (expected + (~seed & 1)) & MAXLONG;
569 } else if (seed < 0x4a1af284) {
570 expected = expected + (seed & 1);
571 } else if (seed < 0x50d79433) {
572 expected = (expected + (~seed & 1)) & MAXLONG;
573 } else if (seed < 0x579435e2) {
574 expected = expected + (seed & 1);
575 } else if (seed < 0x5e50d792) {
576 expected = (expected + (~seed & 1)) & MAXLONG;
577 } else if (seed < 0x650d7941) {
578 expected = expected + (seed & 1);
579 } else if (seed < 0x6bca1af0) {
580 expected = (expected + (~seed & 1)) & MAXLONG;
581 } else if (seed < 0x7286bc9f) {
582 expected = expected + (seed & 1);
583 } else if (seed < 0x79435e4e) {
584 expected = (expected + (~seed & 1)) & MAXLONG;
585 } else if (seed < 0x7ffffffd) {
586 expected = expected + (seed & 1);
587 } else if (seed < 0x86bca1ac) {
588 expected = (expected + (~seed & 1)) & MAXLONG;
589 } else if (seed == 0x86bca1ac) {
590 expected = (expected + 1) & MAXLONG;
591 } else if (seed < 0x8d79435c) {
592 expected = expected + (seed & 1);
593 } else if (seed < 0x9435e50b) {
594 expected = (expected + (~seed & 1)) & MAXLONG;
595 } else if (seed < 0x9af286ba) {
596 expected = expected + (seed & 1);
597 } else if (seed < 0xa1af2869) {
598 expected = (expected + (~seed & 1)) & MAXLONG;
599 } else if (seed < 0xa86bca18) {
600 expected = expected + (seed & 1);
601 } else if (seed < 0xaf286bc7) {
602 expected = (expected + (~seed & 1)) & MAXLONG;
603 } else if (seed == 0xaf286bc7) {
604 expected = (expected + 2) & MAXLONG;
605 } else if (seed < 0xb5e50d77) {
606 expected = expected + (seed & 1);
607 } else if (seed < 0xbca1af26) {
608 expected = (expected + (~seed & 1)) & MAXLONG;
609 } else if (seed < 0xc35e50d5) {
610 expected = expected + (seed & 1);
611 } else if (seed < 0xca1af284) {
612 expected = (expected + (~seed & 1)) & MAXLONG;
613 } else if (seed < 0xd0d79433) {
614 expected = expected + (seed & 1);
615 } else if (seed < 0xd79435e2) {
616 expected = (expected + (~seed & 1)) & MAXLONG;
617 } else if (seed < 0xde50d792) {
618 expected = expected + (seed & 1);
619 } else if (seed < 0xe50d7941) {
620 expected = (expected + (~seed & 1)) & MAXLONG;
621 } else if (seed < 0xebca1af0) {
622 expected = expected + (seed & 1);
623 } else if (seed < 0xf286bc9f) {
624 expected = (expected + (~seed & 1)) & MAXLONG;
625 } else if (seed < 0xf9435e4e) {
626 expected = expected + (seed & 1);
627 } else if (seed < 0xfffffffd) {
628 expected = (expected + (~seed & 1)) & MAXLONG;
629 } else {
630 expected = expected + (seed & 1);
631 } /* if */
632 seed_bak = seed;
633 result = pRtlUniform(&seed);
634 ok(result == expected,
635 "test: 0x%s RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
636 wine_dbgstr_longlong(num), seed_bak, result, expected);
637 ok(seed == expected,
638 "test: 0x%s RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
639 wine_dbgstr_longlong(num), seed_bak, result, expected);
640 } /* for */
642 * Further investigation shows: In the different regions the highest bit
643 * is set or cleared when even or odd seeds need an increment by 1.
644 * This leads to a simplified algorithm:
646 * seed = seed * 0xffffffed + 0x7fffffc3;
647 * if (seed == 0xffffffff || seed == 0x7ffffffe) {
648 * seed = (seed + 2) & MAXLONG;
649 * } else if (seed == 0x7fffffff) {
650 * seed = 0;
651 * } else if ((seed & 0x80000000) == 0) {
652 * seed = seed + (~seed & 1);
653 * } else {
654 * seed = (seed + (seed & 1)) & MAXLONG;
657 * This is also the algorithm used for RtlUniform of wine (see dlls/ntdll/rtl.c).
659 * Now comes the funny part:
660 * It took me one weekend, to find the complicated algorithm and one day more,
661 * to find the simplified algorithm. Several weeks later I found out: The value
662 * MAXLONG (=0x7fffffff) is never returned, neither with the native function
663 * nor with the simplified algorithm. In reality the native function and our
664 * function return a random number distributed over [0..MAXLONG-1]. Note
665 * that this is different from what native documentation states [0..MAXLONG].
666 * Expressed with D.H. Lehmer's 1948 algorithm it looks like:
668 * seed = (seed * const_1 + const_2) % MAXLONG;
670 * Further investigations show that the real algorithm is:
672 * seed = (seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
674 * This is checked with the test below:
676 seed = 0;
677 for (num = 0; num <= 100000; num++) {
678 expected = (seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
679 seed_bak = seed;
680 result = pRtlUniform(&seed);
681 ok(result == expected,
682 "test: 0x%s RtlUniform(&seed (seed == %x)) returns %x, expected %x\n",
683 wine_dbgstr_longlong(num), seed_bak, result, expected);
684 ok(seed == expected,
685 "test: 0x%s RtlUniform(&seed (seed == %x)) sets seed to %x, expected %x\n",
686 wine_dbgstr_longlong(num), seed_bak, result, expected);
687 } /* for */
689 * More tests show that RtlUniform does not return 0x7ffffffd for seed values
690 * in the range [0..MAXLONG-1]. Additionally 2 is returned twice. This shows
691 * that there is more than one cycle of generated randon numbers ...
696 static void test_RtlRandom(void)
698 int i, j;
699 ULONG seed;
700 ULONG res[512];
702 if (!pRtlRandom)
704 win_skip("RtlRandom is not available\n");
705 return;
708 seed = 0;
709 for (i = 0; i < ARRAY_SIZE(res); i++)
711 res[i] = pRtlRandom(&seed);
712 ok(seed != res[i], "%i: seed is same as res %x\n", i, seed);
713 for (j = 0; j < i; j++)
714 ok(res[i] != res[j], "res[%i] (%x) is same as res[%i] (%x)\n", j, res[j], i, res[i]);
719 typedef struct {
720 ACCESS_MASK GrantedAccess;
721 ACCESS_MASK DesiredAccess;
722 BOOLEAN result;
723 } all_accesses_t;
725 static const all_accesses_t all_accesses[] = {
726 {0xFEDCBA76, 0xFEDCBA76, 1},
727 {0x00000000, 0xFEDCBA76, 0},
728 {0xFEDCBA76, 0x00000000, 1},
729 {0x00000000, 0x00000000, 1},
730 {0xFEDCBA76, 0xFEDCBA70, 1},
731 {0xFEDCBA70, 0xFEDCBA76, 0},
732 {0xFEDCBA76, 0xFEDC8A76, 1},
733 {0xFEDC8A76, 0xFEDCBA76, 0},
734 {0xFEDCBA76, 0xC8C4B242, 1},
735 {0xC8C4B242, 0xFEDCBA76, 0},
739 static void test_RtlAreAllAccessesGranted(void)
741 unsigned int test_num;
742 BOOLEAN result;
744 if (!pRtlAreAllAccessesGranted)
746 win_skip("RtlAreAllAccessesGranted is not available\n");
747 return;
750 for (test_num = 0; test_num < ARRAY_SIZE(all_accesses); test_num++) {
751 result = pRtlAreAllAccessesGranted(all_accesses[test_num].GrantedAccess,
752 all_accesses[test_num].DesiredAccess);
753 ok(all_accesses[test_num].result == result,
754 "(test %d): RtlAreAllAccessesGranted(%08x, %08x) returns %d, expected %d\n",
755 test_num, all_accesses[test_num].GrantedAccess,
756 all_accesses[test_num].DesiredAccess,
757 result, all_accesses[test_num].result);
758 } /* for */
762 typedef struct {
763 ACCESS_MASK GrantedAccess;
764 ACCESS_MASK DesiredAccess;
765 BOOLEAN result;
766 } any_accesses_t;
768 static const any_accesses_t any_accesses[] = {
769 {0xFEDCBA76, 0xFEDCBA76, 1},
770 {0x00000000, 0xFEDCBA76, 0},
771 {0xFEDCBA76, 0x00000000, 0},
772 {0x00000000, 0x00000000, 0},
773 {0xFEDCBA76, 0x01234589, 0},
774 {0x00040000, 0xFEDCBA76, 1},
775 {0x00040000, 0xFED8BA76, 0},
776 {0xFEDCBA76, 0x00040000, 1},
777 {0xFED8BA76, 0x00040000, 0},
781 static void test_RtlAreAnyAccessesGranted(void)
783 unsigned int test_num;
784 BOOLEAN result;
786 if (!pRtlAreAnyAccessesGranted)
788 win_skip("RtlAreAnyAccessesGranted is not available\n");
789 return;
792 for (test_num = 0; test_num < ARRAY_SIZE(any_accesses); test_num++) {
793 result = pRtlAreAnyAccessesGranted(any_accesses[test_num].GrantedAccess,
794 any_accesses[test_num].DesiredAccess);
795 ok(any_accesses[test_num].result == result,
796 "(test %d): RtlAreAnyAccessesGranted(%08x, %08x) returns %d, expected %d\n",
797 test_num, any_accesses[test_num].GrantedAccess,
798 any_accesses[test_num].DesiredAccess,
799 result, any_accesses[test_num].result);
800 } /* for */
803 static void test_RtlComputeCrc32(void)
805 DWORD crc = 0;
807 if (!pRtlComputeCrc32)
809 win_skip("RtlComputeCrc32 is not available\n");
810 return;
813 crc = pRtlComputeCrc32(crc, (const BYTE *)src, LEN);
814 ok(crc == 0x40861dc2,"Expected 0x40861dc2, got %8x\n", crc);
818 typedef struct MY_HANDLE
820 RTL_HANDLE RtlHandle;
821 void * MyValue;
822 } MY_HANDLE;
824 static inline void RtlpMakeHandleAllocated(RTL_HANDLE * Handle)
826 ULONG_PTR *AllocatedBit = (ULONG_PTR *)(&Handle->Next);
827 *AllocatedBit = *AllocatedBit | 1;
830 static void test_HandleTables(void)
832 BOOLEAN result;
833 NTSTATUS status;
834 ULONG Index;
835 MY_HANDLE * MyHandle;
836 RTL_HANDLE_TABLE HandleTable;
838 if (!pRtlInitializeHandleTable)
840 win_skip("RtlInitializeHandleTable is not available\n");
841 return;
844 pRtlInitializeHandleTable(0x3FFF, sizeof(MY_HANDLE), &HandleTable);
845 MyHandle = (MY_HANDLE *)pRtlAllocateHandle(&HandleTable, &Index);
846 ok(MyHandle != NULL, "RtlAllocateHandle failed\n");
847 RtlpMakeHandleAllocated(&MyHandle->RtlHandle);
848 MyHandle = NULL;
849 result = pRtlIsValidIndexHandle(&HandleTable, Index, (RTL_HANDLE **)&MyHandle);
850 ok(result, "Handle %p wasn't valid\n", MyHandle);
851 result = pRtlFreeHandle(&HandleTable, &MyHandle->RtlHandle);
852 ok(result, "Couldn't free handle %p\n", MyHandle);
853 status = pRtlDestroyHandleTable(&HandleTable);
854 ok(status == STATUS_SUCCESS, "RtlDestroyHandleTable failed with error 0x%08x\n", status);
857 static void test_RtlAllocateAndInitializeSid(void)
859 NTSTATUS ret;
860 SID_IDENTIFIER_AUTHORITY sia = {{ 1, 2, 3, 4, 5, 6 }};
861 PSID psid;
863 if (!pRtlAllocateAndInitializeSid)
865 win_skip("RtlAllocateAndInitializeSid is not available\n");
866 return;
869 ret = pRtlAllocateAndInitializeSid(&sia, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
870 ok(!ret, "RtlAllocateAndInitializeSid error %08x\n", ret);
871 ret = pRtlFreeSid(psid);
872 ok(!ret, "RtlFreeSid error %08x\n", ret);
874 /* these tests crash on XP */
875 if (0)
877 pRtlAllocateAndInitializeSid(NULL, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
878 pRtlAllocateAndInitializeSid(&sia, 0, 1, 2, 3, 4, 5, 6, 7, 8, NULL);
881 ret = pRtlAllocateAndInitializeSid(&sia, 9, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
882 ok(ret == STATUS_INVALID_SID, "wrong error %08x\n", ret);
885 static void test_RtlDeleteTimer(void)
887 NTSTATUS ret;
889 if (!pRtlDeleteTimer)
891 win_skip("RtlDeleteTimer is not available\n");
892 return;
895 ret = pRtlDeleteTimer(NULL, NULL, NULL);
896 ok(ret == STATUS_INVALID_PARAMETER_1 ||
897 ret == STATUS_INVALID_PARAMETER, /* W2K */
898 "expected STATUS_INVALID_PARAMETER_1 or STATUS_INVALID_PARAMETER, got %x\n", ret);
901 static void test_RtlThreadErrorMode(void)
903 DWORD oldmode;
904 BOOL is_wow64;
905 DWORD mode;
906 NTSTATUS status;
908 if (!pRtlGetThreadErrorMode || !pRtlSetThreadErrorMode)
910 win_skip("RtlGetThreadErrorMode and/or RtlSetThreadErrorMode not available\n");
911 return;
914 if (!pIsWow64Process || !pIsWow64Process(GetCurrentProcess(), &is_wow64))
915 is_wow64 = FALSE;
917 oldmode = pRtlGetThreadErrorMode();
919 status = pRtlSetThreadErrorMode(0x70, &mode);
920 ok(status == STATUS_SUCCESS ||
921 status == STATUS_WAIT_1, /* Vista */
922 "RtlSetThreadErrorMode failed with error 0x%08x\n", status);
923 ok(mode == oldmode,
924 "RtlSetThreadErrorMode returned mode 0x%x, expected 0x%x\n",
925 mode, oldmode);
926 ok(pRtlGetThreadErrorMode() == 0x70,
927 "RtlGetThreadErrorMode returned 0x%x, expected 0x%x\n", mode, 0x70);
928 if (!is_wow64)
930 ok(NtCurrentTeb()->HardErrorDisabled == 0x70,
931 "The TEB contains 0x%x, expected 0x%x\n",
932 NtCurrentTeb()->HardErrorDisabled, 0x70);
935 status = pRtlSetThreadErrorMode(0, &mode);
936 ok(status == STATUS_SUCCESS ||
937 status == STATUS_WAIT_1, /* Vista */
938 "RtlSetThreadErrorMode failed with error 0x%08x\n", status);
939 ok(mode == 0x70,
940 "RtlSetThreadErrorMode returned mode 0x%x, expected 0x%x\n",
941 mode, 0x70);
942 ok(pRtlGetThreadErrorMode() == 0,
943 "RtlGetThreadErrorMode returned 0x%x, expected 0x%x\n", mode, 0);
944 if (!is_wow64)
946 ok(NtCurrentTeb()->HardErrorDisabled == 0,
947 "The TEB contains 0x%x, expected 0x%x\n",
948 NtCurrentTeb()->HardErrorDisabled, 0);
951 for (mode = 1; mode; mode <<= 1)
953 status = pRtlSetThreadErrorMode(mode, NULL);
954 if (mode & 0x70)
955 ok(status == STATUS_SUCCESS ||
956 status == STATUS_WAIT_1, /* Vista */
957 "RtlSetThreadErrorMode(%x,NULL) failed with error 0x%08x\n",
958 mode, status);
959 else
960 ok(status == STATUS_INVALID_PARAMETER_1,
961 "RtlSetThreadErrorMode(%x,NULL) returns 0x%08x, "
962 "expected STATUS_INVALID_PARAMETER_1\n",
963 mode, status);
966 pRtlSetThreadErrorMode(oldmode, NULL);
969 static void test_LdrProcessRelocationBlock(void)
971 IMAGE_BASE_RELOCATION *ret;
972 USHORT reloc;
973 DWORD addr32;
974 SHORT addr16;
976 if(!pLdrProcessRelocationBlock) {
977 win_skip("LdrProcessRelocationBlock not available\n");
978 return;
981 addr32 = 0x50005;
982 reloc = IMAGE_REL_BASED_HIGHLOW<<12;
983 ret = pLdrProcessRelocationBlock(&addr32, 1, &reloc, 0x500050);
984 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
985 ok(addr32 == 0x550055, "addr32 = %x, expected 0x550055\n", addr32);
987 addr16 = 0x505;
988 reloc = IMAGE_REL_BASED_HIGH<<12;
989 ret = pLdrProcessRelocationBlock(&addr16, 1, &reloc, 0x500060);
990 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
991 ok(addr16 == 0x555, "addr16 = %x, expected 0x555\n", addr16);
993 addr16 = 0x505;
994 reloc = IMAGE_REL_BASED_LOW<<12;
995 ret = pLdrProcessRelocationBlock(&addr16, 1, &reloc, 0x500060);
996 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
997 ok(addr16 == 0x565, "addr16 = %x, expected 0x565\n", addr16);
1000 static void test_RtlIpv4AddressToString(void)
1002 CHAR buffer[20];
1003 CHAR *res;
1004 IN_ADDR ip;
1005 DWORD_PTR len;
1007 if (!pRtlIpv4AddressToStringA)
1009 win_skip("RtlIpv4AddressToStringA not available\n");
1010 return;
1013 ip.S_un.S_un_b.s_b1 = 1;
1014 ip.S_un.S_un_b.s_b2 = 2;
1015 ip.S_un.S_un_b.s_b3 = 3;
1016 ip.S_un.S_un_b.s_b4 = 4;
1018 memset(buffer, '#', sizeof(buffer) - 1);
1019 buffer[sizeof(buffer) -1] = 0;
1020 res = pRtlIpv4AddressToStringA(&ip, buffer);
1021 len = strlen(buffer);
1022 ok(res == (buffer + len), "got %p with '%s' (expected %p)\n", res, buffer, buffer + len);
1024 res = pRtlIpv4AddressToStringA(&ip, NULL);
1025 ok( (res == (char *)~0) ||
1026 broken(res == (char *)len), /* XP and w2003 */
1027 "got %p (expected ~0)\n", res);
1029 if (0) {
1030 /* this crashes in windows */
1031 memset(buffer, '#', sizeof(buffer) - 1);
1032 buffer[sizeof(buffer) -1] = 0;
1033 res = pRtlIpv4AddressToStringA(NULL, buffer);
1034 trace("got %p with '%s'\n", res, buffer);
1037 if (0) {
1038 /* this crashes in windows */
1039 res = pRtlIpv4AddressToStringA(NULL, NULL);
1040 trace("got %p\n", res);
1044 static void test_RtlIpv4AddressToStringEx(void)
1046 CHAR ip_1234[] = "1.2.3.4";
1047 CHAR ip_1234_80[] = "1.2.3.4:80";
1048 LPSTR expect;
1049 CHAR buffer[30];
1050 NTSTATUS res;
1051 IN_ADDR ip;
1052 ULONG size;
1053 DWORD used;
1054 USHORT port;
1056 if (!pRtlIpv4AddressToStringExA)
1058 win_skip("RtlIpv4AddressToStringExA not available\n");
1059 return;
1062 ip.S_un.S_un_b.s_b1 = 1;
1063 ip.S_un.S_un_b.s_b2 = 2;
1064 ip.S_un.S_un_b.s_b3 = 3;
1065 ip.S_un.S_un_b.s_b4 = 4;
1067 port = htons(80);
1068 expect = ip_1234_80;
1070 size = sizeof(buffer);
1071 memset(buffer, '#', sizeof(buffer) - 1);
1072 buffer[sizeof(buffer) -1] = 0;
1073 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1074 used = strlen(buffer);
1075 ok( (res == STATUS_SUCCESS) &&
1076 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1077 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1079 size = used + 1;
1080 memset(buffer, '#', sizeof(buffer) - 1);
1081 buffer[sizeof(buffer) -1] = 0;
1082 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1083 ok( (res == STATUS_SUCCESS) &&
1084 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1085 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1087 size = used;
1088 memset(buffer, '#', sizeof(buffer) - 1);
1089 buffer[sizeof(buffer) -1] = 0;
1090 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1091 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1092 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1093 res, size, buffer, used + 1);
1095 size = used - 1;
1096 memset(buffer, '#', sizeof(buffer) - 1);
1097 buffer[sizeof(buffer) -1] = 0;
1098 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1099 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1100 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1101 res, size, buffer, used + 1);
1104 /* to get only the ip, use 0 as port */
1105 port = 0;
1106 expect = ip_1234;
1108 size = sizeof(buffer);
1109 memset(buffer, '#', sizeof(buffer) - 1);
1110 buffer[sizeof(buffer) -1] = 0;
1111 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1112 used = strlen(buffer);
1113 ok( (res == STATUS_SUCCESS) &&
1114 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1115 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1117 size = used + 1;
1118 memset(buffer, '#', sizeof(buffer) - 1);
1119 buffer[sizeof(buffer) -1] = 0;
1120 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1121 ok( (res == STATUS_SUCCESS) &&
1122 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
1123 "got 0x%x and size %d with '%s'\n", res, size, buffer);
1125 size = used;
1126 memset(buffer, '#', sizeof(buffer) - 1);
1127 buffer[sizeof(buffer) -1] = 0;
1128 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1129 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1130 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1131 res, size, buffer, used + 1);
1133 size = used - 1;
1134 memset(buffer, '#', sizeof(buffer) - 1);
1135 buffer[sizeof(buffer) -1] = 0;
1136 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
1137 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
1138 "got 0x%x and %d with '%s' (expected STATUS_INVALID_PARAMETER and %d)\n",
1139 res, size, buffer, used + 1);
1142 /* parameters are checked */
1143 memset(buffer, '#', sizeof(buffer) - 1);
1144 buffer[sizeof(buffer) -1] = 0;
1145 res = pRtlIpv4AddressToStringExA(&ip, 0, buffer, NULL);
1146 ok(res == STATUS_INVALID_PARAMETER,
1147 "got 0x%x with '%s' (expected STATUS_INVALID_PARAMETER)\n", res, buffer);
1149 size = sizeof(buffer);
1150 res = pRtlIpv4AddressToStringExA(&ip, 0, NULL, &size);
1151 ok( res == STATUS_INVALID_PARAMETER,
1152 "got 0x%x and size %d (expected STATUS_INVALID_PARAMETER)\n", res, size);
1154 size = sizeof(buffer);
1155 memset(buffer, '#', sizeof(buffer) - 1);
1156 buffer[sizeof(buffer) -1] = 0;
1157 res = pRtlIpv4AddressToStringExA(NULL, 0, buffer, &size);
1158 ok( res == STATUS_INVALID_PARAMETER,
1159 "got 0x%x and size %d with '%s' (expected STATUS_INVALID_PARAMETER)\n",
1160 res, size, buffer);
1163 static struct
1165 PCSTR address;
1166 NTSTATUS res;
1167 int terminator_offset;
1168 int ip[4];
1169 enum { normal_4, strict_diff_4 = 1, ex_fail_4 = 2 } flags;
1170 NTSTATUS res_strict;
1171 int terminator_offset_strict;
1172 int ip_strict[4];
1173 } ipv4_tests[] =
1175 { "", STATUS_INVALID_PARAMETER, 0, { -1 } },
1176 { " ", STATUS_INVALID_PARAMETER, 0, { -1 } },
1177 { "1.1.1.1", STATUS_SUCCESS, 7, { 1, 1, 1, 1 } },
1178 { "0.0.0.0", STATUS_SUCCESS, 7, { 0, 0, 0, 0 } },
1179 { "255.255.255.255", STATUS_SUCCESS, 15, { 255, 255, 255, 255 } },
1180 { "255.255.255.255:123", STATUS_SUCCESS, 15, { 255, 255, 255, 255 } },
1181 { "255.255.255.256", STATUS_INVALID_PARAMETER, 15, { -1 } },
1182 { "255.255.255.4294967295", STATUS_INVALID_PARAMETER, 22, { -1 } },
1183 { "255.255.255.4294967296", STATUS_INVALID_PARAMETER, 21, { -1 } },
1184 { "255.255.255.4294967297", STATUS_INVALID_PARAMETER, 21, { -1 } },
1185 { "a", STATUS_INVALID_PARAMETER, 0, { -1 } },
1186 { "1.1.1.0xaA", STATUS_SUCCESS, 10, { 1, 1, 1, 170 }, strict_diff_4,
1187 STATUS_INVALID_PARAMETER, 8, { -1 } },
1188 { "1.1.1.0XaA", STATUS_SUCCESS, 10, { 1, 1, 1, 170 }, strict_diff_4,
1189 STATUS_INVALID_PARAMETER, 8, { -1 } },
1190 { "1.1.1.0x", STATUS_INVALID_PARAMETER, 8, { -1 } },
1191 { "1.1.1.0xff", STATUS_SUCCESS, 10, { 1, 1, 1, 255 }, strict_diff_4,
1192 STATUS_INVALID_PARAMETER, 8, { -1 } },
1193 { "1.1.1.0x100", STATUS_INVALID_PARAMETER, 11, { -1 }, strict_diff_4,
1194 STATUS_INVALID_PARAMETER, 8, { -1 } },
1195 { "1.1.1.0xffffffff", STATUS_INVALID_PARAMETER, 16, { -1 }, strict_diff_4,
1196 STATUS_INVALID_PARAMETER, 8, { -1 } },
1197 { "1.1.1.0x100000000", STATUS_INVALID_PARAMETER, 16, { -1, 0, 0, 0 }, strict_diff_4,
1198 STATUS_INVALID_PARAMETER, 8, { -1 } },
1199 { "1.1.1.010", STATUS_SUCCESS, 9, { 1, 1, 1, 8 }, strict_diff_4,
1200 STATUS_INVALID_PARAMETER, 7, { -1 } },
1201 { "1.1.1.00", STATUS_SUCCESS, 8, { 1, 1, 1, 0 }, strict_diff_4,
1202 STATUS_INVALID_PARAMETER, 7, { -1 } },
1203 { "1.1.1.007", STATUS_SUCCESS, 9, { 1, 1, 1, 7 }, strict_diff_4,
1204 STATUS_INVALID_PARAMETER, 7, { -1 } },
1205 { "1.1.1.08", STATUS_INVALID_PARAMETER, 7, { -1 } },
1206 { "1.1.1.008", STATUS_SUCCESS, 8, { 1, 1, 1, 0 }, strict_diff_4 | ex_fail_4,
1207 STATUS_INVALID_PARAMETER, 7, { -1 } },
1208 { "1.1.1.0a", STATUS_SUCCESS, 7, { 1, 1, 1, 0 }, ex_fail_4 },
1209 { "1.1.1.0o10", STATUS_SUCCESS, 7, { 1, 1, 1, 0 }, ex_fail_4 },
1210 { "1.1.1.0b10", STATUS_SUCCESS, 7, { 1, 1, 1, 0 }, ex_fail_4 },
1211 { "1.1.1.-2", STATUS_INVALID_PARAMETER, 6, { -1 } },
1212 { "1", STATUS_SUCCESS, 1, { 0, 0, 0, 1 }, strict_diff_4,
1213 STATUS_INVALID_PARAMETER, 1, { -1 } },
1214 { "-1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1215 { "203569230", STATUS_SUCCESS, 9, { 12, 34, 56, 78 }, strict_diff_4,
1216 STATUS_INVALID_PARAMETER, 9, { -1 } },
1217 { "1.223756", STATUS_SUCCESS, 8, { 1, 3, 106, 12 }, strict_diff_4,
1218 STATUS_INVALID_PARAMETER, 8, { -1 } },
1219 { "3.4.756", STATUS_SUCCESS, 7, { 3, 4, 2, 244 }, strict_diff_4,
1220 STATUS_INVALID_PARAMETER, 7, { -1 } },
1221 { "3.4.756.1", STATUS_INVALID_PARAMETER, 9, { -1 } },
1222 { "3.4.65536", STATUS_INVALID_PARAMETER, 9, { -1 } },
1223 { "3.4.5.6.7", STATUS_INVALID_PARAMETER, 7, { -1 } },
1224 { "3.4.5.+6", STATUS_INVALID_PARAMETER, 6, { -1 } },
1225 { " 3.4.5.6", STATUS_INVALID_PARAMETER, 0, { -1 } },
1226 { "\t3.4.5.6", STATUS_INVALID_PARAMETER, 0, { -1 } },
1227 { "3.4.5.6 ", STATUS_SUCCESS, 7, { 3, 4, 5, 6 }, ex_fail_4 },
1228 { "3. 4.5.6", STATUS_INVALID_PARAMETER, 2, { -1 } },
1229 { ".", STATUS_INVALID_PARAMETER, 1, { -1 } },
1230 { "..", STATUS_INVALID_PARAMETER, 1, { -1 } },
1231 { "1.", STATUS_INVALID_PARAMETER, 2, { -1 } },
1232 { "1..", STATUS_INVALID_PARAMETER, 3, { -1 } },
1233 { ".1", STATUS_INVALID_PARAMETER, 1, { -1 } },
1234 { ".1.", STATUS_INVALID_PARAMETER, 1, { -1 } },
1235 { ".1.2.3", STATUS_INVALID_PARAMETER, 1, { -1 } },
1236 { "0.1.2.3", STATUS_SUCCESS, 7, { 0, 1, 2, 3 } },
1237 { "0.1.2.3.", STATUS_INVALID_PARAMETER, 7, { -1 } },
1238 { "[0.1.2.3]", STATUS_INVALID_PARAMETER, 0, { -1 } },
1239 { "::1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1240 { ":1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1243 static void init_ip4(IN_ADDR* addr, const int src[4])
1245 if (!src || src[0] == -1)
1247 addr->S_un.S_addr = 0xabababab;
1249 else
1251 addr->S_un.S_un_b.s_b1 = src[0];
1252 addr->S_un.S_un_b.s_b2 = src[1];
1253 addr->S_un.S_un_b.s_b3 = src[2];
1254 addr->S_un.S_un_b.s_b4 = src[3];
1258 static void test_RtlIpv4StringToAddress(void)
1260 NTSTATUS res;
1261 IN_ADDR ip, expected_ip;
1262 PCSTR terminator;
1263 CHAR dummy;
1264 int i;
1266 if (!pRtlIpv4StringToAddressA)
1268 skip("RtlIpv4StringToAddress not available\n");
1269 return;
1272 if (0)
1274 /* leaving either parameter NULL crashes on Windows */
1275 res = pRtlIpv4StringToAddressA(NULL, FALSE, &terminator, &ip);
1276 res = pRtlIpv4StringToAddressA("1.1.1.1", FALSE, NULL, &ip);
1277 res = pRtlIpv4StringToAddressA("1.1.1.1", FALSE, &terminator, NULL);
1278 /* same for the wide char version */
1280 res = pRtlIpv4StringToAddressW(NULL, FALSE, &terminatorW, &ip);
1281 res = pRtlIpv4StringToAddressW(L"1.1.1.1", FALSE, NULL, &ip);
1282 res = pRtlIpv4StringToAddressW(L"1.1.1.1", FALSE, &terminatorW, NULL);
1286 for (i = 0; i < ARRAY_SIZE(ipv4_tests); i++)
1288 /* non-strict */
1289 terminator = &dummy;
1290 ip.S_un.S_addr = 0xabababab;
1291 res = pRtlIpv4StringToAddressA(ipv4_tests[i].address, FALSE, &terminator, &ip);
1292 ok(res == ipv4_tests[i].res,
1293 "[%s] res = 0x%08x, expected 0x%08x\n",
1294 ipv4_tests[i].address, res, ipv4_tests[i].res);
1295 ok(terminator == ipv4_tests[i].address + ipv4_tests[i].terminator_offset,
1296 "[%s] terminator = %p, expected %p\n",
1297 ipv4_tests[i].address, terminator, ipv4_tests[i].address + ipv4_tests[i].terminator_offset);
1299 init_ip4(&expected_ip, ipv4_tests[i].ip);
1300 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr,
1301 "[%s] ip = %08x, expected %08x\n",
1302 ipv4_tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1304 if (!(ipv4_tests[i].flags & strict_diff_4))
1306 ipv4_tests[i].res_strict = ipv4_tests[i].res;
1307 ipv4_tests[i].terminator_offset_strict = ipv4_tests[i].terminator_offset;
1308 ipv4_tests[i].ip_strict[0] = ipv4_tests[i].ip[0];
1309 ipv4_tests[i].ip_strict[1] = ipv4_tests[i].ip[1];
1310 ipv4_tests[i].ip_strict[2] = ipv4_tests[i].ip[2];
1311 ipv4_tests[i].ip_strict[3] = ipv4_tests[i].ip[3];
1313 /* strict */
1314 terminator = &dummy;
1315 ip.S_un.S_addr = 0xabababab;
1316 res = pRtlIpv4StringToAddressA(ipv4_tests[i].address, TRUE, &terminator, &ip);
1317 ok(res == ipv4_tests[i].res_strict,
1318 "[%s] res = 0x%08x, expected 0x%08x\n",
1319 ipv4_tests[i].address, res, ipv4_tests[i].res_strict);
1320 ok(terminator == ipv4_tests[i].address + ipv4_tests[i].terminator_offset_strict,
1321 "[%s] terminator = %p, expected %p\n",
1322 ipv4_tests[i].address, terminator, ipv4_tests[i].address + ipv4_tests[i].terminator_offset_strict);
1324 init_ip4(&expected_ip, ipv4_tests[i].ip_strict);
1325 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr,
1326 "[%s] ip = %08x, expected %08x\n",
1327 ipv4_tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1331 static void test_RtlIpv4StringToAddressEx(void)
1333 NTSTATUS res;
1334 IN_ADDR ip, expected_ip;
1335 USHORT port;
1336 static const struct
1338 PCSTR address;
1339 NTSTATUS res;
1340 int ip[4];
1341 USHORT port;
1342 } ipv4_ex_tests[] =
1344 { "", STATUS_INVALID_PARAMETER, { -1 }, 0xdead },
1345 { " ", STATUS_INVALID_PARAMETER, { -1 }, 0xdead },
1346 { "1.1.1.1:", STATUS_INVALID_PARAMETER, { 1, 1, 1, 1 }, 0xdead },
1347 { "1.1.1.1+", STATUS_INVALID_PARAMETER, { 1, 1, 1, 1 }, 0xdead },
1348 { "1.1.1.1:1", STATUS_SUCCESS, { 1, 1, 1, 1 }, 0x100 },
1349 { "256.1.1.1:1", STATUS_INVALID_PARAMETER, { -1 }, 0xdead },
1350 { "-1.1.1.1:1", STATUS_INVALID_PARAMETER, { -1 }, 0xdead },
1351 { "0.0.0.0:0", STATUS_INVALID_PARAMETER, { 0, 0, 0, 0 }, 0xdead },
1352 { "0.0.0.0:1", STATUS_SUCCESS, { 0, 0, 0, 0 }, 0x100 },
1353 { "1.2.3.4:65535", STATUS_SUCCESS, { 1, 2, 3, 4 }, 65535 },
1354 { "1.2.3.4:65536", STATUS_INVALID_PARAMETER, { 1, 2, 3, 4 }, 0xdead },
1355 { "1.2.3.4:0xffff", STATUS_SUCCESS, { 1, 2, 3, 4 }, 65535 },
1356 { "1.2.3.4:0XfFfF", STATUS_SUCCESS, { 1, 2, 3, 4 }, 65535 },
1357 { "1.2.3.4:011064", STATUS_SUCCESS, { 1, 2, 3, 4 }, 0x3412 },
1358 { "1.2.3.4:1234a", STATUS_INVALID_PARAMETER, { 1, 2, 3, 4 }, 0xdead },
1359 { "1.2.3.4:1234+", STATUS_INVALID_PARAMETER, { 1, 2, 3, 4 }, 0xdead },
1360 { "1.2.3.4: 1234", STATUS_INVALID_PARAMETER, { 1, 2, 3, 4 }, 0xdead },
1361 { "1.2.3.4:\t1234", STATUS_INVALID_PARAMETER, { 1, 2, 3, 4 }, 0xdead },
1363 unsigned int i;
1364 BOOLEAN strict;
1366 if (!pRtlIpv4StringToAddressExA)
1368 skip("RtlIpv4StringToAddressEx not available\n");
1369 return;
1372 /* do not crash, and do not touch the ip / port. */
1373 ip.S_un.S_addr = 0xabababab;
1374 port = 0xdead;
1375 res = pRtlIpv4StringToAddressExA(NULL, FALSE, &ip, &port);
1376 ok(res == STATUS_INVALID_PARAMETER, "[null address] res = 0x%08x, expected 0x%08x\n",
1377 res, STATUS_INVALID_PARAMETER);
1378 ok(ip.S_un.S_addr == 0xabababab, "RtlIpv4StringToAddressExA should not touch the ip!, ip == %x\n", ip.S_un.S_addr);
1379 ok(port == 0xdead, "RtlIpv4StringToAddressExA should not touch the port!, port == %x\n", port);
1381 port = 0xdead;
1382 res = pRtlIpv4StringToAddressExA("1.1.1.1", FALSE, NULL, &port);
1383 ok(res == STATUS_INVALID_PARAMETER, "[null ip] res = 0x%08x, expected 0x%08x\n",
1384 res, STATUS_INVALID_PARAMETER);
1385 ok(port == 0xdead, "RtlIpv4StringToAddressExA should not touch the port!, port == %x\n", port);
1387 ip.S_un.S_addr = 0xabababab;
1388 port = 0xdead;
1389 res = pRtlIpv4StringToAddressExA("1.1.1.1", FALSE, &ip, NULL);
1390 ok(res == STATUS_INVALID_PARAMETER, "[null port] res = 0x%08x, expected 0x%08x\n",
1391 res, STATUS_INVALID_PARAMETER);
1392 ok(ip.S_un.S_addr == 0xabababab, "RtlIpv4StringToAddressExA should not touch the ip!, ip == %x\n", ip.S_un.S_addr);
1393 ok(port == 0xdead, "RtlIpv4StringToAddressExA should not touch the port!, port == %x\n", port);
1395 /* first we run the non-ex testcases on the ex function */
1396 for (i = 0; i < ARRAY_SIZE(ipv4_tests); i++)
1398 NTSTATUS expect_res = (ipv4_tests[i].flags & ex_fail_4) ? STATUS_INVALID_PARAMETER : ipv4_tests[i].res;
1400 /* non-strict */
1401 port = 0xdead;
1402 ip.S_un.S_addr = 0xabababab;
1403 res = pRtlIpv4StringToAddressExA(ipv4_tests[i].address, FALSE, &ip, &port);
1404 ok(res == expect_res, "[%s] res = 0x%08x, expected 0x%08x\n",
1405 ipv4_tests[i].address, res, expect_res);
1407 init_ip4(&expected_ip, ipv4_tests[i].ip);
1408 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr, "[%s] ip = %08x, expected %08x\n",
1409 ipv4_tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1411 if (!(ipv4_tests[i].flags & strict_diff_4))
1413 ipv4_tests[i].res_strict = ipv4_tests[i].res;
1414 ipv4_tests[i].terminator_offset_strict = ipv4_tests[i].terminator_offset;
1415 ipv4_tests[i].ip_strict[0] = ipv4_tests[i].ip[0];
1416 ipv4_tests[i].ip_strict[1] = ipv4_tests[i].ip[1];
1417 ipv4_tests[i].ip_strict[2] = ipv4_tests[i].ip[2];
1418 ipv4_tests[i].ip_strict[3] = ipv4_tests[i].ip[3];
1420 /* strict */
1421 expect_res = (ipv4_tests[i].flags & ex_fail_4) ? STATUS_INVALID_PARAMETER : ipv4_tests[i].res_strict;
1422 port = 0xdead;
1423 ip.S_un.S_addr = 0xabababab;
1424 res = pRtlIpv4StringToAddressExA(ipv4_tests[i].address, TRUE, &ip, &port);
1425 ok(res == expect_res, "[%s] res = 0x%08x, expected 0x%08x\n",
1426 ipv4_tests[i].address, res, expect_res);
1428 init_ip4(&expected_ip, ipv4_tests[i].ip_strict);
1429 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr, "[%s] ip = %08x, expected %08x\n",
1430 ipv4_tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1434 for (i = 0; i < ARRAY_SIZE(ipv4_ex_tests); i++)
1436 /* Strict is only relevant for the ip address, so make sure that it does not influence the port */
1437 for (strict = 0; strict < 2; strict++)
1439 ip.S_un.S_addr = 0xabababab;
1440 port = 0xdead;
1441 res = pRtlIpv4StringToAddressExA(ipv4_ex_tests[i].address, strict, &ip, &port);
1442 ok(res == ipv4_ex_tests[i].res, "[%s] res = 0x%08x, expected 0x%08x\n",
1443 ipv4_ex_tests[i].address, res, ipv4_ex_tests[i].res);
1445 init_ip4(&expected_ip, ipv4_ex_tests[i].ip);
1446 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr, "[%s] ip = %08x, expected %08x\n",
1447 ipv4_ex_tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1448 ok(port == ipv4_ex_tests[i].port, "[%s] port = %u, expected %u\n",
1449 ipv4_ex_tests[i].address, port, ipv4_ex_tests[i].port);
1454 /* ipv6 addresses based on the set from https://github.com/beaugunderson/javascript-ipv6/tree/master/test/data */
1455 static const struct
1457 PCSTR address;
1458 NTSTATUS res;
1459 int terminator_offset;
1460 int ip[8];
1461 /* win_broken: older versions of windows do not handle this correctly
1462 ex_fail: Ex function does need the string to be terminated, non-Ex does not.
1463 ex_skip: test doesn't make sense for Ex (f.e. it's invalid for non-Ex but valid for Ex) */
1464 enum { normal_6, win_broken_6 = 1, ex_fail_6 = 2, ex_skip_6 = 4 } flags;
1465 } ipv6_tests[] =
1467 { "0000:0000:0000:0000:0000:0000:0000:0000", STATUS_SUCCESS, 39,
1468 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1469 { "0000:0000:0000:0000:0000:0000:0000:0001", STATUS_SUCCESS, 39,
1470 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1471 { "0:0:0:0:0:0:0:0", STATUS_SUCCESS, 15,
1472 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1473 { "0:0:0:0:0:0:0:1", STATUS_SUCCESS, 15,
1474 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1475 { "0:0:0:0:0:0:0::", STATUS_SUCCESS, 13,
1476 { 0, 0, 0, 0, 0, 0, 0, 0 }, win_broken_6 },
1477 { "0:0:0:0:0:0:13.1.68.3", STATUS_SUCCESS, 21,
1478 { 0, 0, 0, 0, 0, 0, 0x10d, 0x344 } },
1479 { "0:0:0:0:0:0::", STATUS_SUCCESS, 13,
1480 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1481 { "0:0:0:0:0::", STATUS_SUCCESS, 11,
1482 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1483 { "0:0:0:0:0:FFFF:129.144.52.38", STATUS_SUCCESS, 28,
1484 { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
1485 { "0::", STATUS_SUCCESS, 3,
1486 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1487 { "0:1:2:3:4:5:6:7", STATUS_SUCCESS, 15,
1488 { 0, 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700 } },
1489 { "1080:0:0:0:8:800:200c:417a", STATUS_SUCCESS, 26,
1490 { 0x8010, 0, 0, 0, 0x800, 0x8, 0x0c20, 0x7a41 } },
1491 { "0:a:b:c:d:e:f::", STATUS_SUCCESS, 13,
1492 { 0, 0xa00, 0xb00, 0xc00, 0xd00, 0xe00, 0xf00, 0 }, win_broken_6 },
1493 { "1111:2222:3333:4444:5555:6666:123.123.123.123", STATUS_SUCCESS, 45,
1494 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1495 { "1111:2222:3333:4444:5555:6666:7777:8888", STATUS_SUCCESS, 39,
1496 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 } },
1497 { "1111:2222:3333:4444:0x5555:6666:7777:8888", STATUS_INVALID_PARAMETER, 21,
1498 { 0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab } },
1499 { "1111:2222:3333:4444:x555:6666:7777:8888", STATUS_INVALID_PARAMETER, 20,
1500 { 0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab } },
1501 { "1111:2222:3333:4444:0r5555:6666:7777:8888", STATUS_INVALID_PARAMETER, 21,
1502 { 0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab } },
1503 { "1111:2222:3333:4444:r5555:6666:7777:8888", STATUS_INVALID_PARAMETER, 20,
1504 { 0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab } },
1505 { "1111:2222:3333:4444:5555:6666:7777::", STATUS_SUCCESS, 34,
1506 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0 }, win_broken_6 },
1507 { "1111:2222:3333:4444:5555:6666::", STATUS_SUCCESS, 31,
1508 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0, 0 } },
1509 { "1111:2222:3333:4444:5555:6666::8888", STATUS_SUCCESS, 35,
1510 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0, 0x8888 } },
1511 { "1111:2222:3333:4444:5555::", STATUS_SUCCESS, 26,
1512 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0 } },
1513 { "1111:2222:3333:4444:5555::123.123.123.123", STATUS_SUCCESS, 41,
1514 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0x7b7b, 0x7b7b } },
1515 { "1111:2222:3333:4444:5555::0x1.123.123.123", STATUS_SUCCESS, 27,
1516 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x100 }, ex_fail_6 },
1517 { "1111:2222:3333:4444:5555::0x88", STATUS_SUCCESS, 27,
1518 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8800 }, ex_fail_6 },
1519 { "1111:2222:3333:4444:5555::0X88", STATUS_SUCCESS, 27,
1520 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8800 }, ex_fail_6 },
1521 { "1111:2222:3333:4444:5555::0X", STATUS_SUCCESS, 27,
1522 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0 }, ex_fail_6 },
1523 { "1111:2222:3333:4444:5555::0X88:7777", STATUS_SUCCESS, 27,
1524 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8800 }, ex_fail_6 },
1525 { "1111:2222:3333:4444:5555::0x8888", STATUS_SUCCESS, 27,
1526 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8888 }, ex_fail_6 },
1527 { "1111:2222:3333:4444:5555::08888", STATUS_INVALID_PARAMETER, 31,
1528 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0xabab, 0xabab, 0xabab } },
1529 { "1111:2222:3333:4444:5555::fffff", STATUS_INVALID_PARAMETER, 31,
1530 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0xabab, 0xabab, 0xabab } },
1531 { "1111:2222:3333:4444::fffff", STATUS_INVALID_PARAMETER, 26,
1532 { 0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab } },
1533 { "1111:2222:3333::fffff", STATUS_INVALID_PARAMETER, 21,
1534 { 0x1111, 0x2222, 0x3333, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1535 { "1111:2222:3333:4444:5555::7777:8888", STATUS_SUCCESS, 35,
1536 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0x7777, 0x8888 } },
1537 { "1111:2222:3333:4444:5555::8888", STATUS_SUCCESS, 30,
1538 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8888 } },
1539 { "1111::", STATUS_SUCCESS, 6,
1540 { 0x1111, 0, 0, 0, 0, 0, 0, 0 } },
1541 { "1111::123.123.123.123", STATUS_SUCCESS, 21,
1542 { 0x1111, 0, 0, 0, 0, 0, 0x7b7b, 0x7b7b } },
1543 { "1111::3333:4444:5555:6666:123.123.123.123", STATUS_SUCCESS, 41,
1544 { 0x1111, 0, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1545 { "1111::3333:4444:5555:6666:7777:8888", STATUS_SUCCESS, 35,
1546 { 0x1111, 0, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 } },
1547 { "1111::4444:5555:6666:123.123.123.123", STATUS_SUCCESS, 36,
1548 { 0x1111, 0, 0, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1549 { "1111::4444:5555:6666:7777:8888", STATUS_SUCCESS, 30,
1550 { 0x1111, 0, 0, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 } },
1551 { "1111::5555:6666:123.123.123.123", STATUS_SUCCESS, 31,
1552 { 0x1111, 0, 0, 0, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1553 { "1111::5555:6666:7777:8888", STATUS_SUCCESS, 25,
1554 { 0x1111, 0, 0, 0, 0x5555, 0x6666, 0x7777, 0x8888 } },
1555 { "1111::6666:123.123.123.123", STATUS_SUCCESS, 26,
1556 { 0x1111, 0, 0, 0, 0, 0x6666, 0x7b7b, 0x7b7b } },
1557 { "1111::6666:7777:8888", STATUS_SUCCESS, 20,
1558 { 0x1111, 0, 0, 0, 0, 0x6666, 0x7777, 0x8888 } },
1559 { "1111::7777:8888", STATUS_SUCCESS, 15,
1560 { 0x1111, 0, 0, 0, 0, 0, 0x7777, 0x8888 } },
1561 { "1111::8888", STATUS_SUCCESS, 10,
1562 { 0x1111, 0, 0, 0, 0, 0, 0, 0x8888 } },
1563 { "1:2:3:4:5:6:1.2.3.4", STATUS_SUCCESS, 19,
1564 { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x201, 0x403 } },
1565 { "1:2:3:4:5:6:7:8", STATUS_SUCCESS, 15,
1566 { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700, 0x800 } },
1567 { "1:2:3:4:5:6::", STATUS_SUCCESS, 13,
1568 { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0, 0 } },
1569 { "1:2:3:4:5:6::8", STATUS_SUCCESS, 14,
1570 { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0, 0x800 } },
1571 { "2001:0000:1234:0000:0000:C1C0:ABCD:0876", STATUS_SUCCESS, 39,
1572 { 0x120, 0, 0x3412, 0, 0, 0xc0c1, 0xcdab, 0x7608 } },
1573 { "2001:0000:4136:e378:8000:63bf:3fff:fdd2", STATUS_SUCCESS, 39,
1574 { 0x120, 0, 0x3641, 0x78e3, 0x80, 0xbf63, 0xff3f, 0xd2fd } },
1575 { "2001:0db8:0:0:0:0:1428:57ab", STATUS_SUCCESS, 27,
1576 { 0x120, 0xb80d, 0, 0, 0, 0, 0x2814, 0xab57 } },
1577 { "2001:0db8:1234:ffff:ffff:ffff:ffff:ffff", STATUS_SUCCESS, 39,
1578 { 0x120, 0xb80d, 0x3412, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff } },
1579 { "2001::CE49:7601:2CAD:DFFF:7C94:FFFE", STATUS_SUCCESS, 35,
1580 { 0x120, 0, 0x49ce, 0x176, 0xad2c, 0xffdf, 0x947c, 0xfeff } },
1581 { "2001:db8:85a3::8a2e:370:7334", STATUS_SUCCESS, 28,
1582 { 0x120, 0xb80d, 0xa385, 0, 0, 0x2e8a, 0x7003, 0x3473 } },
1583 { "3ffe:0b00:0000:0000:0001:0000:0000:000a", STATUS_SUCCESS, 39,
1584 { 0xfe3f, 0xb, 0, 0, 0x100, 0, 0, 0xa00 } },
1585 { "::", STATUS_SUCCESS, 2,
1586 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1587 { "::%16", STATUS_SUCCESS, 2,
1588 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1589 { "::/16", STATUS_SUCCESS, 2,
1590 { 0, 0, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1591 { "::0", STATUS_SUCCESS, 3,
1592 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1593 { "::0:0", STATUS_SUCCESS, 5,
1594 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1595 { "::0:0:0", STATUS_SUCCESS, 7,
1596 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1597 { "::0:0:0:0", STATUS_SUCCESS, 9,
1598 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1599 { "::0:0:0:0:0", STATUS_SUCCESS, 11,
1600 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1601 { "::0:0:0:0:0:0", STATUS_SUCCESS, 13,
1602 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1603 /* this one and the next one are incorrectly parsed by windows,
1604 it adds one zero too many in front, cutting off the last digit. */
1605 { "::0:0:0:0:0:0:0", STATUS_SUCCESS, 13,
1606 { 0, 0, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1607 { "::0:a:b:c:d:e:f", STATUS_SUCCESS, 13,
1608 { 0, 0, 0, 0xa00, 0xb00, 0xc00, 0xd00, 0xe00 }, ex_fail_6 },
1609 { "::123.123.123.123", STATUS_SUCCESS, 17,
1610 { 0, 0, 0, 0, 0, 0, 0x7b7b, 0x7b7b } },
1611 { "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", STATUS_SUCCESS, 39,
1612 { 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff } },
1614 { "':10.0.0.1", STATUS_INVALID_PARAMETER, 0,
1615 { -1 } },
1616 { "-1", STATUS_INVALID_PARAMETER, 0,
1617 { -1 } },
1618 { "02001:0000:1234:0000:0000:C1C0:ABCD:0876", STATUS_INVALID_PARAMETER, -1,
1619 { -1 } },
1620 { "2001:00000:1234:0000:0000:C1C0:ABCD:0876", STATUS_INVALID_PARAMETER, -1,
1621 { 0x120, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1622 { "2001:0000:01234:0000:0000:C1C0:ABCD:0876", STATUS_INVALID_PARAMETER, -1,
1623 { 0x120, 0, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1624 { "1.2.3.4", STATUS_INVALID_PARAMETER, 7,
1625 { 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1626 { "1.2.3.4:1111::5555", STATUS_INVALID_PARAMETER, 7,
1627 { 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1628 { "1.2.3.4::5555", STATUS_INVALID_PARAMETER, 7,
1629 { 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1630 { "11112222:3333:4444:5555:6666:1.2.3.4", STATUS_INVALID_PARAMETER, -1,
1631 { -1 } },
1632 { "11112222:3333:4444:5555:6666:7777:8888", STATUS_INVALID_PARAMETER, -1,
1633 { -1 } },
1634 { "1111", STATUS_INVALID_PARAMETER, 4,
1635 { -1 } },
1636 { "1111:22223333:4444:5555:6666:1.2.3.4", STATUS_INVALID_PARAMETER, -1,
1637 { 0x1111, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1638 { "1111:22223333:4444:5555:6666:7777:8888", STATUS_INVALID_PARAMETER, -1,
1639 { 0x1111, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1640 { "1111:2222:", STATUS_INVALID_PARAMETER, 10,
1641 { 0x1111, 0x2222, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1642 { "1111:2222:1.2.3.4", STATUS_INVALID_PARAMETER, 17,
1643 { 0x1111, 0x2222, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab } },
1644 { "1111:2222:3333", STATUS_INVALID_PARAMETER, 14,
1645 { 0x1111, 0x2222, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1646 { "1111:2222:3333:4444:5555:6666:7777:1.2.3.4", STATUS_SUCCESS, 36,
1647 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x100 }, ex_fail_6 },
1648 { "1111:2222:3333:4444:5555:6666:7777:8888:", STATUS_SUCCESS, 39,
1649 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 }, ex_fail_6 },
1650 { "1111:2222:3333:4444:5555:6666:7777:8888:1.2.3.4",STATUS_SUCCESS, 39,
1651 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 }, ex_fail_6 },
1652 { "1111:2222:3333:4444:5555:6666:7777:8888:9999", STATUS_SUCCESS, 39,
1653 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 }, ex_fail_6 },
1654 { "1111:2222:::", STATUS_SUCCESS, 11,
1655 { 0x1111, 0x2222, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1656 { "1111::5555:", STATUS_INVALID_PARAMETER, 11,
1657 { 0x1111, 0x5555, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1658 { "1111::3333:4444:5555:6666:7777::", STATUS_SUCCESS, 30,
1659 { 0x1111, 0, 0, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777 }, ex_fail_6 },
1660 { "1111:2222:::4444:5555:6666:1.2.3.4", STATUS_SUCCESS, 11,
1661 { 0x1111, 0x2222, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1662 { "1111::3333::5555:6666:1.2.3.4", STATUS_SUCCESS, 10,
1663 { 0x1111, 0, 0, 0, 0, 0, 0, 0x3333 }, ex_fail_6 },
1664 { "12345::6:7:8", STATUS_INVALID_PARAMETER, -1,
1665 { -1 } },
1666 { "1::1.2.256.4", STATUS_INVALID_PARAMETER, -1,
1667 { 0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1668 { "1::1.2.3.256", STATUS_INVALID_PARAMETER, 12,
1669 { 0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1670 { "1::1.2.3.300", STATUS_INVALID_PARAMETER, 12,
1671 { 0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1672 { "1::1.2::1", STATUS_INVALID_PARAMETER, 6,
1673 { 0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1674 { "1::1.2.3.4::1", STATUS_SUCCESS, 10,
1675 { 0x100, 0, 0, 0, 0, 0, 0x201, 0x403 }, ex_fail_6 },
1676 { "1::1.", STATUS_INVALID_PARAMETER, 5,
1677 { 0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1678 { "1::1.2", STATUS_INVALID_PARAMETER, 6,
1679 { 0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1680 { "1::1.2.", STATUS_INVALID_PARAMETER, 7,
1681 { 0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1682 { "1::1.2.3", STATUS_INVALID_PARAMETER, 8,
1683 { 0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1684 { "1::1.2.3.", STATUS_INVALID_PARAMETER, 9,
1685 { 0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1686 { "1::1.2.3.4", STATUS_SUCCESS, 10,
1687 { 0x100, 0, 0, 0, 0, 0, 0x201, 0x403 } },
1688 { "1::1.2.3.900", STATUS_INVALID_PARAMETER, 12,
1689 { 0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1690 { "1::1.2.300.4", STATUS_INVALID_PARAMETER, -1,
1691 { 0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1692 { "1::1.256.3.4", STATUS_INVALID_PARAMETER, -1,
1693 { 0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1694 { "1::256.2.3.4", STATUS_INVALID_PARAMETER, -1,
1695 { 0x100, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1696 { "1::2::3", STATUS_SUCCESS, 4,
1697 { 0x100, 0, 0, 0, 0, 0, 0, 0x200 }, ex_fail_6 },
1698 { "2001:0000:1234: 0000:0000:C1C0:ABCD:0876", STATUS_INVALID_PARAMETER, 15,
1699 { 0x120, 0, 0x3412, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1700 { "2001:0000:1234:0000:0000:C1C0:ABCD:0876 0", STATUS_SUCCESS, 39,
1701 { 0x120, 0, 0x3412, 0, 0, 0xc0c1, 0xcdab, 0x7608 }, ex_fail_6 },
1702 { "2001:1:1:1:1:1:255Z255X255Y255", STATUS_INVALID_PARAMETER, 18,
1703 { 0x120, 0x100, 0x100, 0x100, 0x100, 0x100, 0xabab, 0xabab } },
1704 { "2001::FFD3::57ab", STATUS_SUCCESS, 10,
1705 { 0x120, 0, 0, 0, 0, 0, 0, 0xd3ff }, ex_fail_6 },
1706 { ":", STATUS_INVALID_PARAMETER, 0,
1707 { -1 } },
1708 { ":1111:2222:3333:4444:5555:6666:1.2.3.4", STATUS_INVALID_PARAMETER, 0,
1709 { -1 } },
1710 { ":1111:2222:3333:4444:5555:6666:7777:8888", STATUS_INVALID_PARAMETER, 0,
1711 { -1 } },
1712 { ":1111::", STATUS_INVALID_PARAMETER, 0,
1713 { -1 } },
1714 { "::-1", STATUS_SUCCESS, 2,
1715 { 0, 0, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1716 { "::.", STATUS_SUCCESS, 2,
1717 { 0, 0, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1718 { "::..", STATUS_SUCCESS, 2,
1719 { 0, 0, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1720 { "::...", STATUS_SUCCESS, 2,
1721 { 0, 0, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1722 { "XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:1.2.3.4", STATUS_INVALID_PARAMETER, 0,
1723 { -1 } },
1724 { "[::]", STATUS_INVALID_PARAMETER, 0,
1725 { -1 }, ex_skip_6 },
1728 static void init_ip6(IN6_ADDR* addr, const int src[8])
1730 unsigned int j;
1731 if (!src || src[0] == -1)
1733 for (j = 0; j < 8; ++j)
1734 addr->s6_words[j] = 0xabab;
1736 else
1738 for (j = 0; j < 8; ++j)
1739 addr->s6_words[j] = src[j];
1743 static void test_RtlIpv6AddressToString(void)
1745 CHAR buffer[50];
1746 LPCSTR result;
1747 IN6_ADDR ip;
1748 DWORD_PTR len;
1749 struct
1751 PCSTR address;
1752 int ip[8];
1753 } tests[] =
1755 /* ipv4 addresses & ISATAP addresses */
1756 { "::13.1.68.3", { 0, 0, 0, 0, 0, 0, 0x10d, 0x344 } },
1757 { "::ffff:13.1.68.3", { 0, 0, 0, 0, 0, 0xffff, 0x10d, 0x344 } },
1758 { "::feff:d01:4403", { 0, 0, 0, 0, 0, 0xfffe, 0x10d, 0x344 } },
1759 { "::fffe:d01:4403", { 0, 0, 0, 0, 0, 0xfeff, 0x10d, 0x344 } },
1760 { "::100:d01:4403", { 0, 0, 0, 0, 0, 1, 0x10d, 0x344 } },
1761 { "::1:d01:4403", { 0, 0, 0, 0, 0, 0x100, 0x10d, 0x344 } },
1762 { "::ffff:0:4403", { 0, 0, 0, 0, 0, 0xffff, 0, 0x344 } },
1763 { "::ffff:13.1.0.0", { 0, 0, 0, 0, 0, 0xffff, 0x10d, 0 } },
1764 { "::ffff:0:0", { 0, 0, 0, 0, 0, 0xffff, 0, 0 } },
1765 { "::ffff:0:13.1.68.3", { 0, 0, 0, 0, 0xffff, 0, 0x10d, 0x344 } },
1766 { "::ffff:ffff:d01:4403", { 0, 0, 0, 0, 0xffff, 0xffff, 0x10d, 0x344 } },
1767 { "::ffff:0:0:d01:4403", { 0, 0, 0, 0xffff, 0, 0, 0x10d, 0x344 } },
1768 { "::ffff:255.255.255.255", { 0, 0, 0, 0, 0, 0xffff, 0xffff, 0xffff } },
1769 { "::ffff:129.144.52.38", { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
1770 { "::5efe:129.144.52.38", { 0, 0, 0, 0, 0, 0xfe5e, 0x9081, 0x2634 } },
1771 { "1111:2222:3333:4444:0:5efe:129.144.52.38", { 0x1111, 0x2222, 0x3333, 0x4444, 0, 0xfe5e, 0x9081, 0x2634 } },
1772 { "1111:2222:3333::5efe:129.144.52.38", { 0x1111, 0x2222, 0x3333, 0, 0, 0xfe5e, 0x9081, 0x2634 } },
1773 { "1111:2222::5efe:129.144.52.38", { 0x1111, 0x2222, 0, 0, 0, 0xfe5e, 0x9081, 0x2634 } },
1774 { "1111::5efe:129.144.52.38", { 0x1111, 0, 0, 0, 0, 0xfe5e, 0x9081, 0x2634 } },
1775 { "::200:5efe:129.144.52.38", { 0, 0, 0, 0, 2, 0xfe5e, 0x9081, 0x2634 } },
1776 { "::100:5efe:8190:3426", { 0, 0, 0, 0, 1, 0xfe5e, 0x9081, 0x2634 } },
1777 /* 'normal' addresses */
1778 { "::1", { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1779 { "0:1:2:3:4:5:6:7", { 0, 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700 } },
1780 { "1080::8:800:200c:417a", { 0x8010, 0, 0, 0, 0x800, 0x8, 0x0c20, 0x7a41 } },
1781 { "1111:2222:3333:4444:5555:6666:7b7b:7b7b", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1782 { "1111:2222:3333:4444:5555:6666:7777:8888", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 } },
1783 { "1111:2222:3333:4444:5555:6666::", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0, 0 } },
1784 { "1111:2222:3333:4444:5555:6666:0:8888", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0, 0x8888 } },
1785 { "1111:2222:3333:4444:5555::", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0 } },
1786 { "1111:2222:3333:4444:5555:0:7b7b:7b7b", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0x7b7b, 0x7b7b } },
1787 { "1111:2222:3333:4444:5555:0:7777:8888", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0x7777, 0x8888 } },
1788 { "1111:2222:3333:4444:5555::8888", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8888 } },
1789 { "1111::", { 0x1111, 0, 0, 0, 0, 0, 0, 0 } },
1790 { "1111::7b7b:7b7b", { 0x1111, 0, 0, 0, 0, 0, 0x7b7b, 0x7b7b } },
1791 { "1111:0:3333:4444:5555:6666:7b7b:7b7b", { 0x1111, 0, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1792 { "1111:0:3333:4444:5555:6666:7777:8888", { 0x1111, 0, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 } },
1793 { "1111::4444:5555:6666:7b7b:7b7b", { 0x1111, 0, 0, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1794 { "1111::4444:5555:6666:7777:8888", { 0x1111, 0, 0, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 } },
1795 { "1111::5555:6666:7b7b:7b7b", { 0x1111, 0, 0, 0, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1796 { "1111::5555:6666:7777:8888", { 0x1111, 0, 0, 0, 0x5555, 0x6666, 0x7777, 0x8888 } },
1797 { "1111::6666:7b7b:7b7b", { 0x1111, 0, 0, 0, 0, 0x6666, 0x7b7b, 0x7b7b } },
1798 { "1111::6666:7777:8888", { 0x1111, 0, 0, 0, 0, 0x6666, 0x7777, 0x8888 } },
1799 { "1111::7777:8888", { 0x1111, 0, 0, 0, 0, 0, 0x7777, 0x8888 } },
1800 { "1111::8888", { 0x1111, 0, 0, 0, 0, 0, 0, 0x8888 } },
1801 { "1:2:3:4:5:6:102:304", { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x201, 0x403 } },
1802 { "1:2:3:4:5:6:7:8", { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700, 0x800 } },
1803 { "1:2:3:4:5:6::", { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0, 0 } },
1804 { "1:2:3:4:5:6:0:8", { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0, 0x800 } },
1805 { "2001:0:1234::c1c0:abcd:876", { 0x120, 0, 0x3412, 0, 0, 0xc0c1, 0xcdab, 0x7608 } },
1806 { "2001:0:4136:e378:8000:63bf:3fff:fdd2", { 0x120, 0, 0x3641, 0x78e3, 0x80, 0xbf63, 0xff3f, 0xd2fd } },
1807 { "2001:db8::1428:57ab", { 0x120, 0xb80d, 0, 0, 0, 0, 0x2814, 0xab57 } },
1808 { "2001:db8:1234:ffff:ffff:ffff:ffff:ffff", { 0x120, 0xb80d, 0x3412, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff } },
1809 { "2001:0:ce49:7601:2cad:dfff:7c94:fffe", { 0x120, 0, 0x49ce, 0x176, 0xad2c, 0xffdf, 0x947c, 0xfeff } },
1810 { "2001:db8:85a3::8a2e:370:7334", { 0x120, 0xb80d, 0xa385, 0, 0, 0x2e8a, 0x7003, 0x3473 } },
1811 { "3ffe:b00::1:0:0:a", { 0xfe3f, 0xb, 0, 0, 0x100, 0, 0, 0xa00 } },
1812 { "::a:b:c:d:e", { 0, 0, 0, 0xa00, 0xb00, 0xc00, 0xd00, 0xe00 } },
1813 { "::123.123.123.123", { 0, 0, 0, 0, 0, 0, 0x7b7b, 0x7b7b } },
1814 { "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", { 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff } },
1815 { "1111:2222:3333:4444:5555:6666:7777:1", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x100 } },
1816 { "1111:2222:3333:4444:5555:6666:7777:8888", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 } },
1817 { "1111:2222::", { 0x1111, 0x2222, 0, 0, 0, 0, 0, 0 } },
1818 { "1111::3333:4444:5555:6666:7777", { 0x1111, 0, 0, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777 } },
1819 { "1111:2222::", { 0x1111, 0x2222, 0, 0, 0, 0, 0, 0 } },
1820 { "1111::3333", { 0x1111, 0, 0, 0, 0, 0, 0, 0x3333 } },
1821 { "2001:0:1234::c1c0:abcd:876", { 0x120, 0, 0x3412, 0, 0, 0xc0c1, 0xcdab, 0x7608 } },
1822 { "2001::ffd3", { 0x120, 0, 0, 0, 0, 0, 0, 0xd3ff } },
1824 unsigned int i;
1826 if (!pRtlIpv6AddressToStringA)
1828 skip("RtlIpv6AddressToStringA not available\n");
1829 return;
1832 memset(buffer, '#', sizeof(buffer));
1833 buffer[sizeof(buffer)-1] = 0;
1834 memset(&ip, 0, sizeof(ip));
1835 result = pRtlIpv6AddressToStringA(&ip, buffer);
1837 len = strlen(buffer);
1838 ok(result == (buffer + len) && !strcmp(buffer, "::"),
1839 "got %p with '%s' (expected %p with '::')\n", result, buffer, buffer + len);
1841 result = pRtlIpv6AddressToStringA(&ip, NULL);
1842 ok(result == (LPCSTR)~0 || broken(result == (LPCSTR)len) /* WinXP / Win2k3 */,
1843 "got %p, expected %p\n", result, (LPCSTR)~0);
1845 for (i = 0; i < ARRAY_SIZE(tests); i++)
1847 init_ip6(&ip, tests[i].ip);
1848 memset(buffer, '#', sizeof(buffer));
1849 buffer[sizeof(buffer)-1] = 0;
1851 result = pRtlIpv6AddressToStringA(&ip, buffer);
1852 len = strlen(buffer);
1853 ok(result == (buffer + len) && !strcmp(buffer, tests[i].address),
1854 "got %p with '%s' (expected %p with '%s')\n", result, buffer, buffer + len, tests[i].address);
1856 ok(buffer[45] == 0 || broken(buffer[45] != 0) /* WinXP / Win2k3 */,
1857 "expected data at buffer[45] to always be NULL\n");
1858 ok(buffer[46] == '#', "expected data at buffer[46] not to change\n");
1862 static void test_RtlIpv6AddressToStringEx(void)
1864 CHAR buffer[70];
1865 NTSTATUS res;
1866 IN6_ADDR ip;
1867 ULONG len;
1868 struct
1870 PCSTR address;
1871 ULONG scopeid;
1872 USHORT port;
1873 int ip[8];
1874 } tests[] =
1876 /* ipv4 addresses & ISATAP addresses */
1877 { "::13.1.68.3", 0, 0, { 0, 0, 0, 0, 0, 0, 0x10d, 0x344 } },
1878 { "::13.1.68.3%1", 1, 0, { 0, 0, 0, 0, 0, 0, 0x10d, 0x344 } },
1879 { "::13.1.68.3%4294949819", 0xffffbbbb, 0, { 0, 0, 0, 0, 0, 0, 0x10d, 0x344 } },
1880 { "[::13.1.68.3%4294949819]:65518", 0xffffbbbb, 0xeeff, { 0, 0, 0, 0, 0, 0, 0x10d, 0x344 } },
1881 { "[::13.1.68.3%4294949819]:256", 0xffffbbbb, 1, { 0, 0, 0, 0, 0, 0, 0x10d, 0x344 } },
1882 { "[::13.1.68.3]:256", 0, 1, { 0, 0, 0, 0, 0, 0, 0x10d, 0x344 } },
1884 { "::1:d01:4403", 0, 0, { 0, 0, 0, 0, 0, 0x100, 0x10d, 0x344 } },
1885 { "::1:d01:4403%1", 1, 0, { 0, 0, 0, 0, 0, 0x100, 0x10d, 0x344 } },
1886 { "::1:d01:4403%4294949819", 0xffffbbbb, 0, { 0, 0, 0, 0, 0, 0x100, 0x10d, 0x344 } },
1887 { "[::1:d01:4403%4294949819]:65518", 0xffffbbbb, 0xeeff, { 0, 0, 0, 0, 0, 0x100, 0x10d, 0x344 } },
1888 { "[::1:d01:4403%4294949819]:256", 0xffffbbbb, 1, { 0, 0, 0, 0, 0, 0x100, 0x10d, 0x344 } },
1889 { "[::1:d01:4403]:256", 0, 1, { 0, 0, 0, 0, 0, 0x100, 0x10d, 0x344 } },
1891 { "1111:2222:3333:4444:0:5efe:129.144.52.38", 0, 0, { 0x1111, 0x2222, 0x3333, 0x4444, 0, 0xfe5e, 0x9081, 0x2634 } },
1892 { "1111:2222:3333:4444:0:5efe:129.144.52.38%1", 1, 0, { 0x1111, 0x2222, 0x3333, 0x4444, 0, 0xfe5e, 0x9081, 0x2634 } },
1893 { "1111:2222:3333:4444:0:5efe:129.144.52.38%4294949819", 0xffffbbbb, 0, { 0x1111, 0x2222, 0x3333, 0x4444, 0, 0xfe5e, 0x9081, 0x2634 } },
1894 { "[1111:2222:3333:4444:0:5efe:129.144.52.38%4294949819]:65518",0xffffbbbb, 0xeeff, { 0x1111, 0x2222, 0x3333, 0x4444, 0, 0xfe5e, 0x9081, 0x2634 } },
1895 { "[1111:2222:3333:4444:0:5efe:129.144.52.38%4294949819]:256", 0xffffbbbb, 1, { 0x1111, 0x2222, 0x3333, 0x4444, 0, 0xfe5e, 0x9081, 0x2634 } },
1896 { "[1111:2222:3333:4444:0:5efe:129.144.52.38]:256", 0, 1, { 0x1111, 0x2222, 0x3333, 0x4444, 0, 0xfe5e, 0x9081, 0x2634 } },
1898 { "::1", 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1899 { "::1%1", 1, 0, { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1900 { "::1%4294949819", 0xffffbbbb, 0, { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1901 { "[::1%4294949819]:65518", 0xffffbbbb, 0xeeff, { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1902 { "[::1%4294949819]:256", 0xffffbbbb, 1, { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1903 { "[::1]:256", 0, 1, { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1905 { "1111:2222:3333:4444:5555:6666:7b7b:7b7b", 0, 0, { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1906 { "1111:2222:3333:4444:5555:6666:7b7b:7b7b%1", 1, 0, { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1907 { "1111:2222:3333:4444:5555:6666:7b7b:7b7b%4294949819", 0xffffbbbb, 0, { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1908 { "[1111:2222:3333:4444:5555:6666:7b7b:7b7b%4294949819]:65518", 0xffffbbbb, 0xeeff, { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1909 { "[1111:2222:3333:4444:5555:6666:7b7b:7b7b%4294949819]:256", 0xffffbbbb, 1, { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1910 { "[1111:2222:3333:4444:5555:6666:7b7b:7b7b]:256", 0, 1, { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1912 { "1111::", 0, 0, { 0x1111, 0, 0, 0, 0, 0, 0, 0 } },
1913 { "1111::%1", 1, 0, { 0x1111, 0, 0, 0, 0, 0, 0, 0 } },
1914 { "1111::%4294949819", 0xffffbbbb, 0, { 0x1111, 0, 0, 0, 0, 0, 0, 0 } },
1915 { "[1111::%4294949819]:65518", 0xffffbbbb, 0xeeff, { 0x1111, 0, 0, 0, 0, 0, 0, 0 } },
1916 { "[1111::%4294949819]:256", 0xffffbbbb, 1, { 0x1111, 0, 0, 0, 0, 0, 0, 0 } },
1917 { "[1111::]:256", 0, 1, { 0x1111, 0, 0, 0, 0, 0, 0, 0 } },
1919 { "2001::ffd3", 0, 0, { 0x120, 0, 0, 0, 0, 0, 0, 0xd3ff } },
1920 { "2001::ffd3%1", 1, 0, { 0x120, 0, 0, 0, 0, 0, 0, 0xd3ff } },
1921 { "2001::ffd3%4294949819", 0xffffbbbb, 0, { 0x120, 0, 0, 0, 0, 0, 0, 0xd3ff } },
1922 { "[2001::ffd3%4294949819]:65518", 0xffffbbbb, 0xeeff, { 0x120, 0, 0, 0, 0, 0, 0, 0xd3ff } },
1923 { "[2001::ffd3%4294949819]:256", 0xffffbbbb, 1, { 0x120, 0, 0, 0, 0, 0, 0, 0xd3ff } },
1924 { "[2001::ffd3]:256", 0, 1, { 0x120, 0, 0, 0, 0, 0, 0, 0xd3ff } },
1926 unsigned int i;
1928 if (!pRtlIpv6AddressToStringExA)
1930 skip("RtlIpv6AddressToStringExA not available\n");
1931 return;
1934 memset(buffer, '#', sizeof(buffer));
1935 buffer[sizeof(buffer)-1] = 0;
1936 memset(&ip, 0, sizeof(ip));
1937 len = sizeof(buffer);
1938 res = pRtlIpv6AddressToStringExA(&ip, 0, 0, buffer, &len);
1940 ok(res == STATUS_SUCCESS, "[validate] res = 0x%08x, expected STATUS_SUCCESS\n", res);
1941 ok(len == 3 && !strcmp(buffer, "::"),
1942 "got len %d with '%s' (expected 3 with '::')\n", len, buffer);
1944 memset(buffer, '#', sizeof(buffer));
1945 buffer[sizeof(buffer)-1] = 0;
1947 len = sizeof(buffer);
1948 res = pRtlIpv6AddressToStringExA(NULL, 0, 0, buffer, &len);
1949 ok(res == STATUS_INVALID_PARAMETER, "[null ip] res = 0x%08x, expected STATUS_INVALID_PARAMETER\n", res);
1951 len = sizeof(buffer);
1952 res = pRtlIpv6AddressToStringExA(&ip, 0, 0, NULL, &len);
1953 ok(res == STATUS_INVALID_PARAMETER, "[null buffer] res = 0x%08x, expected STATUS_INVALID_PARAMETER\n", res);
1955 res = pRtlIpv6AddressToStringExA(&ip, 0, 0, buffer, NULL);
1956 ok(res == STATUS_INVALID_PARAMETER, "[null length] res = 0x%08x, expected STATUS_INVALID_PARAMETER\n", res);
1958 len = 2;
1959 memset(buffer, '#', sizeof(buffer));
1960 buffer[sizeof(buffer)-1] = 0;
1961 res = pRtlIpv6AddressToStringExA(&ip, 0, 0, buffer, &len);
1962 ok(res == STATUS_INVALID_PARAMETER, "[null length] res = 0x%08x, expected STATUS_INVALID_PARAMETER\n", res);
1963 ok(buffer[0] == '#', "got first char %c (expected '#')\n", buffer[0]);
1964 ok(len == 3, "got len %d (expected len 3)\n", len);
1966 for (i = 0; i < ARRAY_SIZE(tests); i++)
1968 init_ip6(&ip, tests[i].ip);
1969 len = sizeof(buffer);
1970 memset(buffer, '#', sizeof(buffer));
1971 buffer[sizeof(buffer)-1] = 0;
1973 res = pRtlIpv6AddressToStringExA(&ip, tests[i].scopeid, tests[i].port, buffer, &len);
1975 ok(res == STATUS_SUCCESS, "[validate] res = 0x%08x, expected STATUS_SUCCESS\n", res);
1976 ok(len == (strlen(tests[i].address) + 1) && !strcmp(buffer, tests[i].address),
1977 "got len %d with '%s' (expected %d with '%s')\n", len, buffer, (int)strlen(tests[i].address), tests[i].address);
1981 static void compare_RtlIpv6StringToAddressW(PCSTR name_a, int terminator_offset_a,
1982 const struct in6_addr *addr_a, NTSTATUS res_a)
1984 WCHAR name[512];
1985 NTSTATUS res;
1986 IN6_ADDR ip;
1987 PCWSTR terminator;
1989 if (!pRtlIpv6StringToAddressW)
1990 return;
1992 pRtlMultiByteToUnicodeN(name, sizeof(name), NULL, name_a, strlen(name_a) + 1);
1994 init_ip6(&ip, NULL);
1995 terminator = (void *)0xdeadbeef;
1996 res = pRtlIpv6StringToAddressW(name, &terminator, &ip);
1997 ok(res == res_a, "[W:%s] res = 0x%08x, expected 0x%08x\n", name_a, res, res_a);
1999 if (terminator_offset_a < 0)
2001 ok(terminator == (void *)0xdeadbeef,
2002 "[W:%s] terminator = %p, expected it not to change\n",
2003 name_a, terminator);
2005 else
2007 ok(terminator == name + terminator_offset_a,
2008 "[W:%s] terminator = %p, expected %p\n",
2009 name_a, terminator, name + terminator_offset_a);
2012 ok(!memcmp(&ip, addr_a, sizeof(ip)),
2013 "[W:%s] ip = %x:%x:%x:%x:%x:%x:%x:%x, expected %x:%x:%x:%x:%x:%x:%x:%x\n",
2014 name_a,
2015 ip.s6_words[0], ip.s6_words[1], ip.s6_words[2], ip.s6_words[3],
2016 ip.s6_words[4], ip.s6_words[5], ip.s6_words[6], ip.s6_words[7],
2017 addr_a->s6_words[0], addr_a->s6_words[1], addr_a->s6_words[2], addr_a->s6_words[3],
2018 addr_a->s6_words[4], addr_a->s6_words[5], addr_a->s6_words[6], addr_a->s6_words[7]);
2021 static void test_RtlIpv6StringToAddress(void)
2023 NTSTATUS res;
2024 IN6_ADDR ip, expected_ip;
2025 PCSTR terminator;
2026 unsigned int i;
2028 if (!pRtlIpv6StringToAddressW)
2030 skip("RtlIpv6StringToAddressW not available\n");
2031 /* we can continue, just not test W */
2034 if (!pRtlIpv6StringToAddressA)
2036 skip("RtlIpv6StringToAddressA not available\n");
2037 return; /* all tests are centered around A, we cannot continue */
2040 res = pRtlIpv6StringToAddressA("::", &terminator, &ip);
2041 ok(res == STATUS_SUCCESS, "[validate] res = 0x%08x, expected STATUS_SUCCESS\n", res);
2042 if (0)
2044 /* any of these crash */
2045 res = pRtlIpv6StringToAddressA(NULL, &terminator, &ip);
2046 ok(res == STATUS_INVALID_PARAMETER, "[null string] res = 0x%08x, expected STATUS_INVALID_PARAMETER\n", res);
2047 res = pRtlIpv6StringToAddressA("::", NULL, &ip);
2048 ok(res == STATUS_INVALID_PARAMETER, "[null terminator] res = 0x%08x, expected STATUS_INVALID_PARAMETER\n", res);
2049 res = pRtlIpv6StringToAddressA("::", &terminator, NULL);
2050 ok(res == STATUS_INVALID_PARAMETER, "[null result] res = 0x%08x, expected STATUS_INVALID_PARAMETER\n", res);
2053 /* sanity check */
2054 ok(sizeof(ip) == sizeof(USHORT)* 8, "sizeof(ip)\n");
2056 for (i = 0; i < ARRAY_SIZE(ipv6_tests); i++)
2058 init_ip6(&ip, NULL);
2059 terminator = (void *)0xdeadbeef;
2060 res = pRtlIpv6StringToAddressA(ipv6_tests[i].address, &terminator, &ip);
2061 compare_RtlIpv6StringToAddressW(ipv6_tests[i].address, (terminator != (void *)0xdeadbeef) ?
2062 (terminator - ipv6_tests[i].address) : -1, &ip, res);
2064 if (ipv6_tests[i].flags & win_broken_6)
2066 ok(res == ipv6_tests[i].res || broken(res == STATUS_INVALID_PARAMETER),
2067 "[%s] res = 0x%08x, expected 0x%08x\n",
2068 ipv6_tests[i].address, res, ipv6_tests[i].res);
2070 if (res == STATUS_INVALID_PARAMETER)
2071 continue;
2073 else
2075 ok(res == ipv6_tests[i].res,
2076 "[%s] res = 0x%08x, expected 0x%08x\n",
2077 ipv6_tests[i].address, res, ipv6_tests[i].res);
2080 if (ipv6_tests[i].terminator_offset < 0)
2082 ok(terminator == (void *)0xdeadbeef,
2083 "[%s] terminator = %p, expected it not to change\n",
2084 ipv6_tests[i].address, terminator);
2086 else if (ipv6_tests[i].flags & win_broken_6)
2088 PCSTR expected = ipv6_tests[i].address + ipv6_tests[i].terminator_offset;
2089 ok(terminator == expected || broken(terminator == expected + 2),
2090 "[%s] terminator = %p, expected %p\n",
2091 ipv6_tests[i].address, terminator, expected);
2093 else
2095 ok(terminator == ipv6_tests[i].address + ipv6_tests[i].terminator_offset,
2096 "[%s] terminator = %p, expected %p\n",
2097 ipv6_tests[i].address, terminator, ipv6_tests[i].address + ipv6_tests[i].terminator_offset);
2100 init_ip6(&expected_ip, ipv6_tests[i].ip);
2101 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
2102 "[%s] ip = %x:%x:%x:%x:%x:%x:%x:%x, expected %x:%x:%x:%x:%x:%x:%x:%x\n",
2103 ipv6_tests[i].address,
2104 ip.s6_words[0], ip.s6_words[1], ip.s6_words[2], ip.s6_words[3],
2105 ip.s6_words[4], ip.s6_words[5], ip.s6_words[6], ip.s6_words[7],
2106 expected_ip.s6_words[0], expected_ip.s6_words[1], expected_ip.s6_words[2], expected_ip.s6_words[3],
2107 expected_ip.s6_words[4], expected_ip.s6_words[5], expected_ip.s6_words[6], expected_ip.s6_words[7]);
2111 static void compare_RtlIpv6StringToAddressExW(PCSTR name_a, const struct in6_addr *addr_a, HRESULT res_a, ULONG scope_a, USHORT port_a)
2113 WCHAR name[512];
2114 NTSTATUS res;
2115 IN6_ADDR ip;
2116 ULONG scope = 0xbadf00d;
2117 USHORT port = 0xbeef;
2119 if (!pRtlIpv6StringToAddressExW)
2120 return;
2122 pRtlMultiByteToUnicodeN(name, sizeof(name), NULL, name_a, strlen(name_a) + 1);
2124 init_ip6(&ip, NULL);
2125 res = pRtlIpv6StringToAddressExW(name, &ip, &scope, &port);
2127 ok(res == res_a, "[W:%s] res = 0x%08x, expected 0x%08x\n", name_a, res, res_a);
2128 ok(scope == scope_a, "[W:%s] scope = 0x%08x, expected 0x%08x\n", name_a, scope, scope_a);
2129 ok(port == port_a, "[W:%s] port = 0x%08x, expected 0x%08x\n", name_a, port, port_a);
2131 ok(!memcmp(&ip, addr_a, sizeof(ip)),
2132 "[W:%s] ip = %x:%x:%x:%x:%x:%x:%x:%x, expected %x:%x:%x:%x:%x:%x:%x:%x\n",
2133 name_a,
2134 ip.s6_words[0], ip.s6_words[1], ip.s6_words[2], ip.s6_words[3],
2135 ip.s6_words[4], ip.s6_words[5], ip.s6_words[6], ip.s6_words[7],
2136 addr_a->s6_words[0], addr_a->s6_words[1], addr_a->s6_words[2], addr_a->s6_words[3],
2137 addr_a->s6_words[4], addr_a->s6_words[5], addr_a->s6_words[6], addr_a->s6_words[7]);
2140 static void test_RtlIpv6StringToAddressEx(void)
2142 NTSTATUS res;
2143 IN6_ADDR ip, expected_ip;
2144 ULONG scope;
2145 USHORT port;
2146 static const struct
2148 PCSTR address;
2149 NTSTATUS res;
2150 ULONG scope;
2151 USHORT port;
2152 int ip[8];
2153 } ipv6_ex_tests[] =
2155 { "[::]", STATUS_SUCCESS, 0, 0,
2156 { 0, 0, 0, 0, 0, 0, 0, 0 } },
2157 { "[::1]:8080", STATUS_SUCCESS, 0, 0x901f,
2158 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
2159 { "[::1]:0x80", STATUS_SUCCESS, 0, 0x8000,
2160 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
2161 { "[::1]:0X80", STATUS_SUCCESS, 0, 0x8000,
2162 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
2163 { "[::1]:080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2164 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
2165 { "[::1]:800000000080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2166 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
2167 { "[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80", STATUS_SUCCESS, 0, 0x5000,
2168 { 0xdcfe, 0x98ba, 0x5476, 0x1032, 0xdcfe, 0x98ba, 0x5476, 0x1032 } },
2169 { "[1080:0:0:0:8:800:200C:417A]:1234", STATUS_SUCCESS, 0, 0xd204,
2170 { 0x8010, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2171 { "[3ffe:2a00:100:7031::1]:8080", STATUS_SUCCESS, 0, 0x901f,
2172 { 0xfe3f, 0x2a, 1, 0x3170, 0, 0, 0, 0x100 } },
2173 { "[ 3ffe:2a00:100:7031::1]:8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2174 { -1 } },
2175 { "[3ffe:2a00:100:7031::1 ]:8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2176 { 0xfe3f, 0x2a, 1, 0x3170, 0, 0, 0, 0x100 } },
2177 { "[3ffe:2a00:100:7031::1].8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2178 { 0xfe3f, 0x2a, 1, 0x3170, 0, 0, 0, 0x100 } },
2179 { "[1080::8:800:200C:417A]:8080", STATUS_SUCCESS, 0, 0x901f,
2180 { 0x8010, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2181 { "[1080::8:800:200C:417A]!8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2182 { 0x8010, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2183 { "[::FFFF:129.144.52.38]:80", STATUS_SUCCESS, 0, 0x5000,
2184 { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
2185 { "[::FFFF:129.144.52.38]:-80", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2186 { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
2187 { "[::FFFF:129.144.52.38]:999999999999", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2188 { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
2189 { "[::FFFF:129.144.52.38%-8]:80", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2190 { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
2191 { "[::FFFF:129.144.52.38]:80", STATUS_SUCCESS, 0, 0x5000,
2192 { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
2193 { "[12345::6:7:8]:80", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2194 { -1 } },
2195 { "[ff01::8:800:200C:417A%16]:8080", STATUS_SUCCESS, 16, 0x901f,
2196 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2197 { "[ff01::8:800:200C:417A%100]:8080", STATUS_SUCCESS, 100, 0x901f,
2198 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2199 { "[ff01::8:800:200C:417A%1000]:8080", STATUS_SUCCESS, 1000, 0x901f,
2200 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2201 { "[ff01::8:800:200C:417A%10000]:8080", STATUS_SUCCESS, 10000, 0x901f,
2202 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2203 { "[ff01::8:800:200C:417A%1000000]:8080", STATUS_SUCCESS, 1000000, 0x901f,
2204 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2205 { "[ff01::8:800:200C:417A%4294967295]:8080", STATUS_SUCCESS, 0xffffffff, 0x901f,
2206 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2207 { "[ff01::8:800:200C:417A%4294967296]:8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2208 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2209 { "[ff01::8:800:200C:417A%-1]:8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2210 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2211 { "[ff01::8:800:200C:417A%0]:8080", STATUS_SUCCESS, 0, 0x901f,
2212 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2213 { "[ff01::8:800:200C:417A%1", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2214 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2215 { "[ff01::8:800:200C:417A%0x1000]:8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2216 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2217 { "[ff01::8:800:200C:417A/16]:8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2218 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2220 const char *simple_ip = "::";
2221 unsigned int i;
2223 if (!pRtlIpv6StringToAddressExW)
2225 skip("RtlIpv6StringToAddressExW not available\n");
2226 /* we can continue, just not test W */
2229 if (!pRtlIpv6StringToAddressExA)
2231 skip("RtlIpv6StringToAddressExA not available\n");
2232 return;
2235 res = pRtlIpv6StringToAddressExA(simple_ip, &ip, &scope, &port);
2236 ok(res == STATUS_SUCCESS, "[validate] res = 0x%08x, expected STATUS_SUCCESS\n", res);
2238 init_ip6(&ip, NULL);
2239 init_ip6(&expected_ip, NULL);
2240 scope = 0xbadf00d;
2241 port = 0xbeef;
2242 res = pRtlIpv6StringToAddressExA(NULL, &ip, &scope, &port);
2243 ok(res == STATUS_INVALID_PARAMETER,
2244 "[null string] res = 0x%08x, expected STATUS_INVALID_PARAMETER\n", res);
2245 ok(scope == 0xbadf00d, "[null string] scope = 0x%08x, expected 0xbadf00d\n", scope);
2246 ok(port == 0xbeef, "[null string] port = 0x%08x, expected 0xbeef\n", port);
2247 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
2248 "[null string] ip is changed, expected it not to change\n");
2251 init_ip6(&ip, NULL);
2252 scope = 0xbadf00d;
2253 port = 0xbeef;
2254 res = pRtlIpv6StringToAddressExA(simple_ip, NULL, &scope, &port);
2255 ok(res == STATUS_INVALID_PARAMETER,
2256 "[null result] res = 0x%08x, expected STATUS_INVALID_PARAMETER\n", res);
2257 ok(scope == 0xbadf00d, "[null result] scope = 0x%08x, expected 0xbadf00d\n", scope);
2258 ok(port == 0xbeef, "[null result] port = 0x%08x, expected 0xbeef\n", port);
2259 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
2260 "[null result] ip is changed, expected it not to change\n");
2262 init_ip6(&ip, NULL);
2263 scope = 0xbadf00d;
2264 port = 0xbeef;
2265 res = pRtlIpv6StringToAddressExA(simple_ip, &ip, NULL, &port);
2266 ok(res == STATUS_INVALID_PARAMETER,
2267 "[null scope] res = 0x%08x, expected STATUS_INVALID_PARAMETER\n", res);
2268 ok(scope == 0xbadf00d, "[null scope] scope = 0x%08x, expected 0xbadf00d\n", scope);
2269 ok(port == 0xbeef, "[null scope] port = 0x%08x, expected 0xbeef\n", port);
2270 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
2271 "[null scope] ip is changed, expected it not to change\n");
2273 init_ip6(&ip, NULL);
2274 scope = 0xbadf00d;
2275 port = 0xbeef;
2276 res = pRtlIpv6StringToAddressExA(simple_ip, &ip, &scope, NULL);
2277 ok(res == STATUS_INVALID_PARAMETER,
2278 "[null port] res = 0x%08x, expected STATUS_INVALID_PARAMETER\n", res);
2279 ok(scope == 0xbadf00d, "[null port] scope = 0x%08x, expected 0xbadf00d\n", scope);
2280 ok(port == 0xbeef, "[null port] port = 0x%08x, expected 0xbeef\n", port);
2281 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
2282 "[null port] ip is changed, expected it not to change\n");
2284 /* sanity check */
2285 ok(sizeof(ip) == sizeof(USHORT)* 8, "sizeof(ip)\n");
2287 /* first we run all ip related tests, to make sure someone didn't accidentally reimplement instead of re-use. */
2288 for (i = 0; i < ARRAY_SIZE(ipv6_tests); i++)
2290 ULONG scope = 0xbadf00d;
2291 USHORT port = 0xbeef;
2292 NTSTATUS expect_ret = (ipv6_tests[i].flags & ex_fail_6) ? STATUS_INVALID_PARAMETER : ipv6_tests[i].res;
2294 if (ipv6_tests[i].flags & ex_skip_6)
2295 continue;
2297 init_ip6(&ip, NULL);
2298 res = pRtlIpv6StringToAddressExA(ipv6_tests[i].address, &ip, &scope, &port);
2299 compare_RtlIpv6StringToAddressExW(ipv6_tests[i].address, &ip, res, scope, port);
2301 /* make sure nothing was changed if this function fails. */
2302 if (res == STATUS_INVALID_PARAMETER)
2304 ok(scope == 0xbadf00d, "[%s] scope = 0x%08x, expected 0xbadf00d\n",
2305 ipv6_tests[i].address, scope);
2306 ok(port == 0xbeef, "[%s] port = 0x%08x, expected 0xbeef\n",
2307 ipv6_tests[i].address, port);
2309 else
2311 ok(scope != 0xbadf00d, "[%s] scope = 0x%08x, not expected 0xbadf00d\n",
2312 ipv6_tests[i].address, scope);
2313 ok(port != 0xbeef, "[%s] port = 0x%08x, not expected 0xbeef\n",
2314 ipv6_tests[i].address, port);
2317 if (ipv6_tests[i].flags & win_broken_6)
2319 ok(res == expect_ret || broken(res == STATUS_INVALID_PARAMETER),
2320 "[%s] res = 0x%08x, expected 0x%08x\n", ipv6_tests[i].address, res, expect_ret);
2322 if (res == STATUS_INVALID_PARAMETER)
2323 continue;
2325 else
2327 ok(res == expect_ret, "[%s] res = 0x%08x, expected 0x%08x\n",
2328 ipv6_tests[i].address, res, expect_ret);
2331 /* If ex fails but non-ex does not we cannot check if the part that is converted
2332 before it failed was correct, since there is no data for it in the table. */
2333 if (res == expect_ret)
2335 init_ip6(&expected_ip, ipv6_tests[i].ip);
2336 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
2337 "[%s] ip = %x:%x:%x:%x:%x:%x:%x:%x, expected %x:%x:%x:%x:%x:%x:%x:%x\n",
2338 ipv6_tests[i].address,
2339 ip.s6_words[0], ip.s6_words[1], ip.s6_words[2], ip.s6_words[3],
2340 ip.s6_words[4], ip.s6_words[5], ip.s6_words[6], ip.s6_words[7],
2341 expected_ip.s6_words[0], expected_ip.s6_words[1], expected_ip.s6_words[2], expected_ip.s6_words[3],
2342 expected_ip.s6_words[4], expected_ip.s6_words[5], expected_ip.s6_words[6], expected_ip.s6_words[7]);
2346 /* now we run scope / port related tests */
2347 for (i = 0; i < ARRAY_SIZE(ipv6_ex_tests); i++)
2349 scope = 0xbadf00d;
2350 port = 0xbeef;
2351 init_ip6(&ip, NULL);
2352 res = pRtlIpv6StringToAddressExA(ipv6_ex_tests[i].address, &ip, &scope, &port);
2353 compare_RtlIpv6StringToAddressExW(ipv6_ex_tests[i].address, &ip, res, scope, port);
2355 ok(res == ipv6_ex_tests[i].res, "[%s] res = 0x%08x, expected 0x%08x\n",
2356 ipv6_ex_tests[i].address, res, ipv6_ex_tests[i].res);
2357 ok(scope == ipv6_ex_tests[i].scope, "[%s] scope = 0x%08x, expected 0x%08x\n",
2358 ipv6_ex_tests[i].address, scope, ipv6_ex_tests[i].scope);
2359 ok(port == ipv6_ex_tests[i].port, "[%s] port = 0x%08x, expected 0x%08x\n",
2360 ipv6_ex_tests[i].address, port, ipv6_ex_tests[i].port);
2362 init_ip6(&expected_ip, ipv6_ex_tests[i].ip);
2363 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
2364 "[%s] ip = %x:%x:%x:%x:%x:%x:%x:%x, expected %x:%x:%x:%x:%x:%x:%x:%x\n",
2365 ipv6_ex_tests[i].address,
2366 ip.s6_words[0], ip.s6_words[1], ip.s6_words[2], ip.s6_words[3],
2367 ip.s6_words[4], ip.s6_words[5], ip.s6_words[6], ip.s6_words[7],
2368 expected_ip.s6_words[0], expected_ip.s6_words[1], expected_ip.s6_words[2], expected_ip.s6_words[3],
2369 expected_ip.s6_words[4], expected_ip.s6_words[5], expected_ip.s6_words[6], expected_ip.s6_words[7]);
2373 static void test_LdrAddRefDll(void)
2375 HMODULE mod, mod2;
2376 NTSTATUS status;
2377 BOOL ret;
2379 if (!pLdrAddRefDll)
2381 win_skip( "LdrAddRefDll not supported\n" );
2382 return;
2385 mod = LoadLibraryA("comctl32.dll");
2386 ok(mod != NULL, "got %p\n", mod);
2387 ret = FreeLibrary(mod);
2388 ok(ret, "got %d\n", ret);
2390 mod2 = GetModuleHandleA("comctl32.dll");
2391 ok(mod2 == NULL, "got %p\n", mod2);
2393 /* load, addref and release 2 times */
2394 mod = LoadLibraryA("comctl32.dll");
2395 ok(mod != NULL, "got %p\n", mod);
2396 status = pLdrAddRefDll(0, mod);
2397 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
2398 ret = FreeLibrary(mod);
2399 ok(ret, "got %d\n", ret);
2401 mod2 = GetModuleHandleA("comctl32.dll");
2402 ok(mod2 != NULL, "got %p\n", mod2);
2403 ret = FreeLibrary(mod);
2404 ok(ret, "got %d\n", ret);
2406 mod2 = GetModuleHandleA("comctl32.dll");
2407 ok(mod2 == NULL, "got %p\n", mod2);
2409 /* pin refcount */
2410 mod = LoadLibraryA("comctl32.dll");
2411 ok(mod != NULL, "got %p\n", mod);
2412 status = pLdrAddRefDll(LDR_ADDREF_DLL_PIN, mod);
2413 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
2415 ret = FreeLibrary(mod);
2416 ok(ret, "got %d\n", ret);
2417 ret = FreeLibrary(mod);
2418 ok(ret, "got %d\n", ret);
2419 ret = FreeLibrary(mod);
2420 ok(ret, "got %d\n", ret);
2421 ret = FreeLibrary(mod);
2422 ok(ret, "got %d\n", ret);
2424 mod2 = GetModuleHandleA("comctl32.dll");
2425 ok(mod2 != NULL, "got %p\n", mod2);
2428 static void test_LdrLockLoaderLock(void)
2430 ULONG_PTR magic;
2431 ULONG result;
2432 NTSTATUS status;
2434 if (!pLdrLockLoaderLock)
2436 win_skip("LdrLockLoaderLock() is not available\n");
2437 return;
2440 /* invalid flags */
2441 result = 10;
2442 magic = 0xdeadbeef;
2443 status = pLdrLockLoaderLock(0x10, &result, &magic);
2444 ok(status == STATUS_INVALID_PARAMETER_1, "got 0x%08x\n", status);
2445 ok(result == 0, "got %d\n", result);
2446 ok(magic == 0, "got %lx\n", magic);
2448 magic = 0xdeadbeef;
2449 status = pLdrLockLoaderLock(0x10, NULL, &magic);
2450 ok(status == STATUS_INVALID_PARAMETER_1, "got 0x%08x\n", status);
2451 ok(magic == 0, "got %lx\n", magic);
2453 result = 10;
2454 status = pLdrLockLoaderLock(0x10, &result, NULL);
2455 ok(status == STATUS_INVALID_PARAMETER_1, "got 0x%08x\n", status);
2456 ok(result == 0, "got %d\n", result);
2458 /* non-blocking mode, result is null */
2459 magic = 0xdeadbeef;
2460 status = pLdrLockLoaderLock(0x2, NULL, &magic);
2461 ok(status == STATUS_INVALID_PARAMETER_2, "got 0x%08x\n", status);
2462 ok(magic == 0, "got %lx\n", magic);
2464 /* magic pointer is null */
2465 result = 10;
2466 status = pLdrLockLoaderLock(0, &result, NULL);
2467 ok(status == STATUS_INVALID_PARAMETER_3, "got 0x%08x\n", status);
2468 ok(result == 0, "got %d\n", result);
2470 /* lock in non-blocking mode */
2471 result = 0;
2472 magic = 0;
2473 status = pLdrLockLoaderLock(0x2, &result, &magic);
2474 ok(status == STATUS_SUCCESS, "got 0x%08x\n", status);
2475 ok(result == 1, "got %d\n", result);
2476 ok(magic != 0, "got %lx\n", magic);
2477 pLdrUnlockLoaderLock(0, magic);
2480 static void test_RtlCompressBuffer(void)
2482 ULONG compress_workspace, decompress_workspace;
2483 static const UCHAR test_buffer[] = "WineWineWine";
2484 static UCHAR buf1[0x1000], buf2[0x1000];
2485 ULONG final_size, buf_size;
2486 UCHAR *workspace = NULL;
2487 NTSTATUS status;
2489 if (!pRtlCompressBuffer || !pRtlDecompressBuffer || !pRtlGetCompressionWorkSpaceSize)
2491 win_skip("skipping RtlCompressBuffer tests, required functions not available\n");
2492 return;
2495 compress_workspace = decompress_workspace = 0xdeadbeef;
2496 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1, &compress_workspace,
2497 &decompress_workspace);
2498 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
2499 ok(compress_workspace != 0, "got wrong compress_workspace %u\n", compress_workspace);
2500 workspace = HeapAlloc(GetProcessHeap(), 0, compress_workspace);
2501 ok(workspace != NULL, "HeapAlloc failed %d\n", GetLastError());
2503 /* test compression format / engine */
2504 final_size = 0xdeadbeef;
2505 status = pRtlCompressBuffer(COMPRESSION_FORMAT_NONE, test_buffer, sizeof(test_buffer),
2506 buf1, sizeof(buf1) - 1, 4096, &final_size, workspace);
2507 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
2508 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
2510 final_size = 0xdeadbeef;
2511 status = pRtlCompressBuffer(COMPRESSION_FORMAT_DEFAULT, test_buffer, sizeof(test_buffer),
2512 buf1, sizeof(buf1) - 1, 4096, &final_size, workspace);
2513 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
2514 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
2516 final_size = 0xdeadbeef;
2517 status = pRtlCompressBuffer(0xFF, test_buffer, sizeof(test_buffer),
2518 buf1, sizeof(buf1) - 1, 4096, &final_size, workspace);
2519 ok(status == STATUS_UNSUPPORTED_COMPRESSION, "got wrong status 0x%08x\n", status);
2520 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
2522 /* test compression */
2523 final_size = 0xdeadbeef;
2524 memset(buf1, 0x11, sizeof(buf1));
2525 status = pRtlCompressBuffer(COMPRESSION_FORMAT_LZNT1, test_buffer, sizeof(test_buffer),
2526 buf1, sizeof(buf1), 4096, &final_size, workspace);
2527 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
2528 ok((*(WORD *)buf1 & 0x7000) == 0x3000, "no chunk signature found %04x\n", *(WORD *)buf1);
2529 todo_wine
2530 ok(final_size < sizeof(test_buffer), "got wrong final_size %u\n", final_size);
2532 /* test decompression */
2533 buf_size = final_size;
2534 final_size = 0xdeadbeef;
2535 memset(buf2, 0x11, sizeof(buf2));
2536 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf2, sizeof(buf2),
2537 buf1, buf_size, &final_size);
2538 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
2539 ok(final_size == sizeof(test_buffer), "got wrong final_size %u\n", final_size);
2540 ok(!memcmp(buf2, test_buffer, sizeof(test_buffer)), "got wrong decoded data\n");
2541 ok(buf2[sizeof(test_buffer)] == 0x11, "too many bytes written\n");
2543 /* buffer too small */
2544 final_size = 0xdeadbeef;
2545 memset(buf1, 0x11, sizeof(buf1));
2546 status = pRtlCompressBuffer(COMPRESSION_FORMAT_LZNT1, test_buffer, sizeof(test_buffer),
2547 buf1, 4, 4096, &final_size, workspace);
2548 ok(status == STATUS_BUFFER_TOO_SMALL, "got wrong status 0x%08x\n", status);
2550 HeapFree(GetProcessHeap(), 0, workspace);
2553 static void test_RtlGetCompressionWorkSpaceSize(void)
2555 ULONG compress_workspace, decompress_workspace;
2556 NTSTATUS status;
2558 if (!pRtlGetCompressionWorkSpaceSize)
2560 win_skip("RtlGetCompressionWorkSpaceSize is not available\n");
2561 return;
2564 /* test invalid format / engine */
2565 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_NONE, &compress_workspace,
2566 &decompress_workspace);
2567 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
2569 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_DEFAULT, &compress_workspace,
2570 &decompress_workspace);
2571 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
2573 status = pRtlGetCompressionWorkSpaceSize(0xFF, &compress_workspace, &decompress_workspace);
2574 ok(status == STATUS_UNSUPPORTED_COMPRESSION, "got wrong status 0x%08x\n", status);
2576 /* test LZNT1 with normal and maximum compression */
2577 compress_workspace = decompress_workspace = 0xdeadbeef;
2578 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1, &compress_workspace,
2579 &decompress_workspace);
2580 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
2581 ok(compress_workspace != 0, "got wrong compress_workspace %u\n", compress_workspace);
2582 ok(decompress_workspace == 0x1000, "got wrong decompress_workspace %u\n", decompress_workspace);
2584 compress_workspace = decompress_workspace = 0xdeadbeef;
2585 status = pRtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_MAXIMUM,
2586 &compress_workspace, &decompress_workspace);
2587 ok(status == STATUS_SUCCESS, "got wrong status 0x%08x\n", status);
2588 ok(compress_workspace != 0, "got wrong compress_workspace %u\n", compress_workspace);
2589 ok(decompress_workspace == 0x1000, "got wrong decompress_workspace %u\n", decompress_workspace);
2592 /* helper for test_RtlDecompressBuffer, checks if a chunk is incomplete */
2593 static BOOL is_incomplete_chunk(const UCHAR *compressed, ULONG compressed_size, BOOL check_all)
2595 ULONG chunk_size;
2597 if (compressed_size <= sizeof(WORD))
2598 return TRUE;
2600 while (compressed_size >= sizeof(WORD))
2602 chunk_size = (*(WORD *)compressed & 0xFFF) + 1;
2603 if (compressed_size < sizeof(WORD) + chunk_size)
2604 return TRUE;
2605 if (!check_all)
2606 break;
2607 compressed += sizeof(WORD) + chunk_size;
2608 compressed_size -= sizeof(WORD) + chunk_size;
2611 return FALSE;
2614 #define DECOMPRESS_BROKEN_FRAGMENT 1 /* < Win 7 */
2615 #define DECOMPRESS_BROKEN_TRUNCATED 2 /* broken on all machines */
2617 static void test_RtlDecompressBuffer(void)
2619 static const struct
2621 UCHAR compressed[32];
2622 ULONG compressed_size;
2623 NTSTATUS status;
2624 UCHAR uncompressed[32];
2625 ULONG uncompressed_size;
2626 DWORD broken_flags;
2628 test_lznt[] =
2630 /* 4 byte uncompressed chunk */
2632 {0x03, 0x30, 'W', 'i', 'n', 'e'},
2634 STATUS_SUCCESS,
2635 "Wine",
2637 DECOMPRESS_BROKEN_FRAGMENT
2639 /* 8 byte uncompressed chunk */
2641 {0x07, 0x30, 'W', 'i', 'n', 'e', 'W', 'i', 'n', 'e'},
2643 STATUS_SUCCESS,
2644 "WineWine",
2646 DECOMPRESS_BROKEN_FRAGMENT
2648 /* 4 byte compressed chunk */
2650 {0x04, 0xB0, 0x00, 'W', 'i', 'n', 'e'},
2652 STATUS_SUCCESS,
2653 "Wine",
2656 /* 8 byte compressed chunk */
2658 {0x08, 0xB0, 0x00, 'W', 'i', 'n', 'e', 'W', 'i', 'n', 'e'},
2660 STATUS_SUCCESS,
2661 "WineWine",
2664 /* compressed chunk using backwards reference */
2666 {0x06, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x01, 0x30},
2668 STATUS_SUCCESS,
2669 "WineWine",
2671 DECOMPRESS_BROKEN_TRUNCATED
2673 /* compressed chunk using backwards reference with length > bytes_read */
2675 {0x06, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x05, 0x30},
2677 STATUS_SUCCESS,
2678 "WineWineWine",
2680 DECOMPRESS_BROKEN_TRUNCATED
2682 /* same as above, but unused bits != 0 */
2684 {0x06, 0xB0, 0x30, 'W', 'i', 'n', 'e', 0x01, 0x30},
2686 STATUS_SUCCESS,
2687 "WineWine",
2689 DECOMPRESS_BROKEN_TRUNCATED
2691 /* compressed chunk without backwards reference and unused bits != 0 */
2693 {0x01, 0xB0, 0x02, 'W'},
2695 STATUS_SUCCESS,
2696 "W",
2699 /* termination sequence after first chunk */
2701 {0x03, 0x30, 'W', 'i', 'n', 'e', 0x00, 0x00, 0x03, 0x30, 'W', 'i', 'n', 'e'},
2703 STATUS_SUCCESS,
2704 "Wine",
2706 DECOMPRESS_BROKEN_FRAGMENT
2708 /* compressed chunk using backwards reference with 4 bit offset, 12 bit length */
2710 {0x14, 0xB0, 0x00, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
2711 0x00, 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
2712 0x01, 0x01, 0xF0},
2714 STATUS_SUCCESS,
2715 "ABCDEFGHIJKLMNOPABCD",
2717 DECOMPRESS_BROKEN_TRUNCATED
2719 /* compressed chunk using backwards reference with 5 bit offset, 11 bit length */
2721 {0x15, 0xB0, 0x00, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
2722 0x00, 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
2723 0x02, 'A', 0x00, 0x78},
2725 STATUS_SUCCESS,
2726 "ABCDEFGHIJKLMNOPABCD",
2728 DECOMPRESS_BROKEN_TRUNCATED
2730 /* uncompressed chunk with invalid magic */
2732 {0x03, 0x20, 'W', 'i', 'n', 'e'},
2734 STATUS_SUCCESS,
2735 "Wine",
2737 DECOMPRESS_BROKEN_FRAGMENT
2739 /* compressed chunk with invalid magic */
2741 {0x04, 0xA0, 0x00, 'W', 'i', 'n', 'e'},
2743 STATUS_SUCCESS,
2744 "Wine",
2747 /* garbage byte after end of buffer */
2749 {0x00, 0xB0, 0x02, 0x01},
2751 STATUS_SUCCESS,
2755 /* empty compressed chunk */
2757 {0x00, 0xB0, 0x00},
2759 STATUS_SUCCESS,
2763 /* empty compressed chunk with unused bits != 0 */
2765 {0x00, 0xB0, 0x01},
2767 STATUS_SUCCESS,
2771 /* empty input buffer */
2775 STATUS_BAD_COMPRESSION_BUFFER,
2777 /* incomplete chunk header */
2779 {0x01},
2781 STATUS_BAD_COMPRESSION_BUFFER
2783 /* incomplete chunk header */
2785 {0x00, 0x30},
2787 STATUS_BAD_COMPRESSION_BUFFER
2789 /* compressed chunk with invalid backwards reference */
2791 {0x06, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x05, 0x40},
2793 STATUS_BAD_COMPRESSION_BUFFER
2795 /* compressed chunk with incomplete backwards reference */
2797 {0x05, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x05},
2799 STATUS_BAD_COMPRESSION_BUFFER
2801 /* incomplete uncompressed chunk */
2803 {0x07, 0x30, 'W', 'i', 'n', 'e'},
2805 STATUS_BAD_COMPRESSION_BUFFER
2807 /* incomplete compressed chunk */
2809 {0x08, 0xB0, 0x00, 'W', 'i', 'n', 'e'},
2811 STATUS_BAD_COMPRESSION_BUFFER
2813 /* two compressed chunks, the second one incomplete */
2815 {0x00, 0xB0, 0x02, 0x00, 0xB0},
2817 STATUS_BAD_COMPRESSION_BUFFER,
2821 static UCHAR buf[0x2000], workspace[0x1000];
2822 NTSTATUS status, expected_status;
2823 ULONG final_size;
2824 int i;
2826 if (!pRtlDecompressBuffer || !pRtlDecompressFragment)
2828 win_skip("RtlDecompressBuffer or RtlDecompressFragment is not available\n");
2829 return;
2832 /* test compression format / engine */
2833 final_size = 0xdeadbeef;
2834 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_NONE, buf, sizeof(buf), test_lznt[0].compressed,
2835 test_lznt[0].compressed_size, &final_size);
2836 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
2837 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
2839 final_size = 0xdeadbeef;
2840 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_DEFAULT, buf, sizeof(buf), test_lznt[0].compressed,
2841 test_lznt[0].compressed_size, &final_size);
2842 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08x\n", status);
2843 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
2845 final_size = 0xdeadbeef;
2846 status = pRtlDecompressBuffer(0xFF, buf, sizeof(buf), test_lznt[0].compressed,
2847 test_lznt[0].compressed_size, &final_size);
2848 ok(status == STATUS_UNSUPPORTED_COMPRESSION, "got wrong status 0x%08x\n", status);
2849 ok(final_size == 0xdeadbeef, "got wrong final_size %u\n", final_size);
2851 /* regular tests for RtlDecompressBuffer */
2852 for (i = 0; i < ARRAY_SIZE(test_lznt); i++)
2854 trace("Running test %d (compressed_size=%u, uncompressed_size=%u, status=0x%08x)\n",
2855 i, test_lznt[i].compressed_size, test_lznt[i].uncompressed_size, test_lznt[i].status);
2857 /* test with very big buffer */
2858 final_size = 0xdeadbeef;
2859 memset(buf, 0x11, sizeof(buf));
2860 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
2861 test_lznt[i].compressed_size, &final_size);
2862 ok(status == test_lznt[i].status || broken(status == STATUS_BAD_COMPRESSION_BUFFER &&
2863 (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)), "%d: got wrong status 0x%08x\n", i, status);
2864 if (!status)
2866 ok(final_size == test_lznt[i].uncompressed_size,
2867 "%d: got wrong final_size %u\n", i, final_size);
2868 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
2869 "%d: got wrong decoded data\n", i);
2870 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
2871 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size);
2874 /* test that modifier for compression engine is ignored */
2875 final_size = 0xdeadbeef;
2876 memset(buf, 0x11, sizeof(buf));
2877 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_MAXIMUM, buf, sizeof(buf),
2878 test_lznt[i].compressed, test_lznt[i].compressed_size, &final_size);
2879 ok(status == test_lznt[i].status || broken(status == STATUS_BAD_COMPRESSION_BUFFER &&
2880 (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)), "%d: got wrong status 0x%08x\n", i, status);
2881 if (!status)
2883 ok(final_size == test_lznt[i].uncompressed_size,
2884 "%d: got wrong final_size %u\n", i, final_size);
2885 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
2886 "%d: got wrong decoded data\n", i);
2887 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
2888 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size);
2891 /* test with expected output size */
2892 if (test_lznt[i].uncompressed_size > 0)
2894 final_size = 0xdeadbeef;
2895 memset(buf, 0x11, sizeof(buf));
2896 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, test_lznt[i].uncompressed_size,
2897 test_lznt[i].compressed, test_lznt[i].compressed_size, &final_size);
2898 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
2899 if (!status)
2901 ok(final_size == test_lznt[i].uncompressed_size,
2902 "%d: got wrong final_size %u\n", i, final_size);
2903 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
2904 "%d: got wrong decoded data\n", i);
2905 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
2906 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size);
2910 /* test with smaller output size */
2911 if (test_lznt[i].uncompressed_size > 1)
2913 final_size = 0xdeadbeef;
2914 memset(buf, 0x11, sizeof(buf));
2915 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, test_lznt[i].uncompressed_size - 1,
2916 test_lznt[i].compressed, test_lznt[i].compressed_size, &final_size);
2917 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_TRUNCATED)
2918 todo_wine
2919 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
2920 else
2921 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
2922 if (!status)
2924 ok(final_size == test_lznt[i].uncompressed_size - 1,
2925 "%d: got wrong final_size %u\n", i, final_size);
2926 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size - 1),
2927 "%d: got wrong decoded data\n", i);
2928 ok(buf[test_lznt[i].uncompressed_size - 1] == 0x11,
2929 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size - 1);
2933 /* test with zero output size */
2934 final_size = 0xdeadbeef;
2935 memset(buf, 0x11, sizeof(buf));
2936 status = pRtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, 0, test_lznt[i].compressed,
2937 test_lznt[i].compressed_size, &final_size);
2938 if (is_incomplete_chunk(test_lznt[i].compressed, test_lznt[i].compressed_size, FALSE))
2939 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
2940 else
2942 ok(status == STATUS_SUCCESS, "%d: got wrong status 0x%08x\n", i, status);
2943 ok(final_size == 0, "%d: got wrong final_size %u\n", i, final_size);
2944 ok(buf[0] == 0x11, "%d: buf[0] was modified\n", i);
2947 /* test RtlDecompressFragment with offset = 0 */
2948 final_size = 0xdeadbeef;
2949 memset(buf, 0x11, sizeof(buf));
2950 status = pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
2951 test_lznt[i].compressed_size, 0, &final_size, workspace);
2952 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)
2953 todo_wine
2954 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
2955 else
2956 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
2957 if (!status)
2959 ok(final_size == test_lznt[i].uncompressed_size,
2960 "%d: got wrong final_size %u\n", i, final_size);
2961 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
2962 "%d: got wrong decoded data\n", i);
2963 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
2964 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size);
2967 /* test RtlDecompressFragment with offset = 1 */
2968 final_size = 0xdeadbeef;
2969 memset(buf, 0x11, sizeof(buf));
2970 status = pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
2971 test_lznt[i].compressed_size, 1, &final_size, workspace);
2972 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)
2973 todo_wine
2974 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
2975 else
2976 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
2977 if (!status)
2979 if (test_lznt[i].uncompressed_size == 0)
2981 todo_wine
2982 ok(final_size == 4095, "%d: got wrong final_size %u\n", i, final_size);
2983 /* Buffer doesn't contain any useful value on Windows */
2984 ok(buf[4095] == 0x11, "%d: buf[4095] was modified\n", i);
2986 else
2988 ok(final_size == test_lznt[i].uncompressed_size - 1,
2989 "%d: got wrong final_size %u\n", i, final_size);
2990 ok(!memcmp(buf, test_lznt[i].uncompressed + 1, test_lznt[i].uncompressed_size - 1),
2991 "%d: got wrong decoded data\n", i);
2992 ok(buf[test_lznt[i].uncompressed_size - 1] == 0x11,
2993 "%d: buf[%u] was modified\n", i, test_lznt[i].uncompressed_size - 1);
2997 /* test RtlDecompressFragment with offset = 4095 */
2998 final_size = 0xdeadbeef;
2999 memset(buf, 0x11, sizeof(buf));
3000 status = pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
3001 test_lznt[i].compressed_size, 4095, &final_size, workspace);
3002 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)
3003 todo_wine
3004 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08x\n", i, status);
3005 else
3006 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08x\n", i, status);
3007 if (!status)
3009 todo_wine
3010 ok(final_size == 1, "%d: got wrong final_size %u\n", i, final_size);
3011 todo_wine
3012 ok(buf[0] == 0, "%d: padding is not zero\n", i);
3013 ok(buf[1] == 0x11, "%d: buf[1] was modified\n", i);
3016 /* test RtlDecompressFragment with offset = 4096 */
3017 final_size = 0xdeadbeef;
3018 memset(buf, 0x11, sizeof(buf));
3019 status = pRtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
3020 test_lznt[i].compressed_size, 4096, &final_size, workspace);
3021 expected_status = is_incomplete_chunk(test_lznt[i].compressed, test_lznt[i].compressed_size, TRUE) ?
3022 test_lznt[i].status : STATUS_SUCCESS;
3023 ok(status == expected_status, "%d: got wrong status 0x%08x, expected 0x%08x\n", i, status, expected_status);
3024 if (!status)
3026 ok(final_size == 0, "%d: got wrong final_size %u\n", i, final_size);
3027 ok(buf[0] == 0x11, "%d: buf[4096] was modified\n", i);
3032 #undef DECOMPRESS_BROKEN_FRAGMENT
3033 #undef DECOMPRESS_BROKEN_TRUNCATED
3035 struct critsect_locked_info
3037 CRITICAL_SECTION crit;
3038 HANDLE semaphores[2];
3041 static DWORD WINAPI critsect_locked_thread(void *param)
3043 struct critsect_locked_info *info = param;
3044 DWORD ret;
3046 ret = pRtlIsCriticalSectionLocked(&info->crit);
3047 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
3048 ret = pRtlIsCriticalSectionLockedByThread(&info->crit);
3049 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
3051 ReleaseSemaphore(info->semaphores[0], 1, NULL);
3052 ret = WaitForSingleObject(info->semaphores[1], 1000);
3053 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
3055 ret = pRtlIsCriticalSectionLocked(&info->crit);
3056 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
3057 ret = pRtlIsCriticalSectionLockedByThread(&info->crit);
3058 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
3060 EnterCriticalSection(&info->crit);
3062 ret = pRtlIsCriticalSectionLocked(&info->crit);
3063 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
3064 ret = pRtlIsCriticalSectionLockedByThread(&info->crit);
3065 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
3067 ReleaseSemaphore(info->semaphores[0], 1, NULL);
3068 ret = WaitForSingleObject(info->semaphores[1], 1000);
3069 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
3071 LeaveCriticalSection(&info->crit);
3072 return 0;
3075 static void test_RtlIsCriticalSectionLocked(void)
3077 struct critsect_locked_info info;
3078 HANDLE thread;
3079 BOOL ret;
3081 if (!pRtlIsCriticalSectionLocked || !pRtlIsCriticalSectionLockedByThread)
3083 win_skip("skipping RtlIsCriticalSectionLocked tests, required functions not available\n");
3084 return;
3087 InitializeCriticalSection(&info.crit);
3088 info.semaphores[0] = CreateSemaphoreW(NULL, 0, 1, NULL);
3089 ok(info.semaphores[0] != NULL, "CreateSemaphore failed with %u\n", GetLastError());
3090 info.semaphores[1] = CreateSemaphoreW(NULL, 0, 1, NULL);
3091 ok(info.semaphores[1] != NULL, "CreateSemaphore failed with %u\n", GetLastError());
3093 ret = pRtlIsCriticalSectionLocked(&info.crit);
3094 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
3095 ret = pRtlIsCriticalSectionLockedByThread(&info.crit);
3096 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
3098 EnterCriticalSection(&info.crit);
3100 ret = pRtlIsCriticalSectionLocked(&info.crit);
3101 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
3102 ret = pRtlIsCriticalSectionLockedByThread(&info.crit);
3103 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
3105 thread = CreateThread(NULL, 0, critsect_locked_thread, &info, 0, NULL);
3106 ok(thread != NULL, "CreateThread failed with %u\n", GetLastError());
3107 ret = WaitForSingleObject(info.semaphores[0], 1000);
3108 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
3110 LeaveCriticalSection(&info.crit);
3112 ReleaseSemaphore(info.semaphores[1], 1, NULL);
3113 ret = WaitForSingleObject(info.semaphores[0], 1000);
3114 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
3116 ret = pRtlIsCriticalSectionLocked(&info.crit);
3117 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
3118 ret = pRtlIsCriticalSectionLockedByThread(&info.crit);
3119 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
3121 ReleaseSemaphore(info.semaphores[1], 1, NULL);
3122 ret = WaitForSingleObject(thread, 1000);
3123 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
3125 CloseHandle(thread);
3126 CloseHandle(info.semaphores[0]);
3127 CloseHandle(info.semaphores[1]);
3128 DeleteCriticalSection(&info.crit);
3131 static void test_RtlInitializeCriticalSectionEx(void)
3133 static const CRITICAL_SECTION_DEBUG *no_debug = (void *)~(ULONG_PTR)0;
3134 CRITICAL_SECTION cs;
3136 if (!pRtlInitializeCriticalSectionEx)
3138 win_skip("RtlInitializeCriticalSectionEx is not available\n");
3139 return;
3142 memset(&cs, 0x11, sizeof(cs));
3143 pRtlInitializeCriticalSectionEx(&cs, 0, 0);
3144 ok((cs.DebugInfo != NULL && cs.DebugInfo != no_debug) || broken(cs.DebugInfo == no_debug) /* >= Win 8 */,
3145 "expected DebugInfo != NULL and DebugInfo != ~0, got %p\n", cs.DebugInfo);
3146 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
3147 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %d\n", cs.RecursionCount);
3148 ok(cs.LockSemaphore == NULL, "expected LockSemaphore == NULL, got %p\n", cs.LockSemaphore);
3149 ok(cs.SpinCount == 0 || broken(cs.SpinCount != 0) /* >= Win 8 */,
3150 "expected SpinCount == 0, got %ld\n", cs.SpinCount);
3151 RtlDeleteCriticalSection(&cs);
3153 memset(&cs, 0x11, sizeof(cs));
3154 pRtlInitializeCriticalSectionEx(&cs, 0, RTL_CRITICAL_SECTION_FLAG_NO_DEBUG_INFO);
3155 ok(cs.DebugInfo == no_debug, "expected DebugInfo == ~0, got %p\n", cs.DebugInfo);
3156 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
3157 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %d\n", cs.RecursionCount);
3158 ok(cs.LockSemaphore == NULL, "expected LockSemaphore == NULL, got %p\n", cs.LockSemaphore);
3159 ok(cs.SpinCount == 0 || broken(cs.SpinCount != 0) /* >= Win 8 */,
3160 "expected SpinCount == 0, got %ld\n", cs.SpinCount);
3161 RtlDeleteCriticalSection(&cs);
3164 static void test_RtlLeaveCriticalSection(void)
3166 RTL_CRITICAL_SECTION cs;
3167 NTSTATUS status;
3169 if (!pRtlInitializeCriticalSectionEx)
3170 return; /* Skip winxp */
3172 status = RtlInitializeCriticalSection(&cs);
3173 ok(!status, "RtlInitializeCriticalSection failed: %x\n", status);
3175 status = RtlEnterCriticalSection(&cs);
3176 ok(!status, "RtlEnterCriticalSection failed: %x\n", status);
3177 todo_wine
3178 ok(cs.LockCount == -2, "expected LockCount == -2, got %d\n", cs.LockCount);
3179 ok(cs.RecursionCount == 1, "expected RecursionCount == 1, got %d\n", cs.RecursionCount);
3180 ok(cs.OwningThread == ULongToHandle(GetCurrentThreadId()), "unexpected OwningThread\n");
3182 status = RtlLeaveCriticalSection(&cs);
3183 ok(!status, "RtlLeaveCriticalSection failed: %x\n", status);
3184 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
3185 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %d\n", cs.RecursionCount);
3186 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
3189 * Trying to leave a section that wasn't acquired modifies RecursionCount to an invalid value,
3190 * but doesn't modify LockCount so that an attempt to enter the section later will work.
3192 status = RtlLeaveCriticalSection(&cs);
3193 ok(!status, "RtlLeaveCriticalSection failed: %x\n", status);
3194 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
3195 ok(cs.RecursionCount == -1, "expected RecursionCount == -1, got %d\n", cs.RecursionCount);
3196 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
3198 /* and again */
3199 status = RtlLeaveCriticalSection(&cs);
3200 ok(!status, "RtlLeaveCriticalSection failed: %x\n", status);
3201 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
3202 ok(cs.RecursionCount == -2, "expected RecursionCount == -2, got %d\n", cs.RecursionCount);
3203 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
3205 /* entering section fixes RecursionCount */
3206 status = RtlEnterCriticalSection(&cs);
3207 ok(!status, "RtlEnterCriticalSection failed: %x\n", status);
3208 todo_wine
3209 ok(cs.LockCount == -2, "expected LockCount == -2, got %d\n", cs.LockCount);
3210 ok(cs.RecursionCount == 1, "expected RecursionCount == 1, got %d\n", cs.RecursionCount);
3211 ok(cs.OwningThread == ULongToHandle(GetCurrentThreadId()), "unexpected OwningThread\n");
3213 status = RtlLeaveCriticalSection(&cs);
3214 ok(!status, "RtlLeaveCriticalSection failed: %x\n", status);
3215 ok(cs.LockCount == -1, "expected LockCount == -1, got %d\n", cs.LockCount);
3216 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %d\n", cs.RecursionCount);
3217 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
3219 status = RtlDeleteCriticalSection(&cs);
3220 ok(!status, "RtlDeleteCriticalSection failed: %x\n", status);
3223 struct ldr_enum_context
3225 BOOL abort;
3226 BOOL found;
3227 int count;
3230 static void WINAPI ldr_enum_callback(LDR_MODULE *module, void *context, BOOLEAN *stop)
3232 static const WCHAR ntdllW[] = {'n','t','d','l','l','.','d','l','l',0};
3233 struct ldr_enum_context *ctx = context;
3235 if (!lstrcmpiW(module->BaseDllName.Buffer, ntdllW))
3236 ctx->found = TRUE;
3238 ctx->count++;
3239 *stop = ctx->abort;
3242 static void test_LdrEnumerateLoadedModules(void)
3244 struct ldr_enum_context ctx;
3245 NTSTATUS status;
3247 if (!pLdrEnumerateLoadedModules)
3249 win_skip("LdrEnumerateLoadedModules not available\n");
3250 return;
3253 ctx.abort = FALSE;
3254 ctx.found = FALSE;
3255 ctx.count = 0;
3256 status = pLdrEnumerateLoadedModules(NULL, ldr_enum_callback, &ctx);
3257 ok(status == STATUS_SUCCESS, "LdrEnumerateLoadedModules failed with %08x\n", status);
3258 ok(ctx.count > 1, "Expected more than one module, got %d\n", ctx.count);
3259 ok(ctx.found, "Could not find ntdll in list of modules\n");
3261 ctx.abort = TRUE;
3262 ctx.count = 0;
3263 status = pLdrEnumerateLoadedModules(NULL, ldr_enum_callback, &ctx);
3264 ok(status == STATUS_SUCCESS, "LdrEnumerateLoadedModules failed with %08x\n", status);
3265 ok(ctx.count == 1, "Expected exactly one module, got %d\n", ctx.count);
3267 status = pLdrEnumerateLoadedModules((void *)0x1, ldr_enum_callback, (void *)0xdeadbeef);
3268 ok(status == STATUS_INVALID_PARAMETER, "expected STATUS_INVALID_PARAMETER, got 0x%08x\n", status);
3270 status = pLdrEnumerateLoadedModules((void *)0xdeadbeef, ldr_enum_callback, (void *)0xdeadbeef);
3271 ok(status == STATUS_INVALID_PARAMETER, "expected STATUS_INVALID_PARAMETER, got 0x%08x\n", status);
3273 status = pLdrEnumerateLoadedModules(NULL, NULL, (void *)0xdeadbeef);
3274 ok(status == STATUS_INVALID_PARAMETER, "expected STATUS_INVALID_PARAMETER, got 0x%08x\n", status);
3277 static void test_RtlMakeSelfRelativeSD(void)
3279 char buf[sizeof(SECURITY_DESCRIPTOR_RELATIVE) + 4];
3280 SECURITY_DESCRIPTOR_RELATIVE *sd_rel = (SECURITY_DESCRIPTOR_RELATIVE *)buf;
3281 SECURITY_DESCRIPTOR sd;
3282 NTSTATUS status;
3283 DWORD len;
3285 if (!pRtlMakeSelfRelativeSD || !pRtlAbsoluteToSelfRelativeSD)
3287 win_skip( "RtlMakeSelfRelativeSD/RtlAbsoluteToSelfRelativeSD not available\n" );
3288 return;
3291 memset( &sd, 0, sizeof(sd) );
3292 sd.Revision = SECURITY_DESCRIPTOR_REVISION;
3294 len = 0;
3295 status = pRtlMakeSelfRelativeSD( &sd, NULL, &len );
3296 ok( status == STATUS_BUFFER_TOO_SMALL, "got %08x\n", status );
3297 ok( len == sizeof(*sd_rel), "got %u\n", len );
3299 len += 4;
3300 status = pRtlMakeSelfRelativeSD( &sd, sd_rel, &len );
3301 ok( status == STATUS_SUCCESS, "got %08x\n", status );
3302 ok( len == sizeof(*sd_rel) + 4, "got %u\n", len );
3304 len = 0;
3305 status = pRtlAbsoluteToSelfRelativeSD( &sd, NULL, &len );
3306 ok( status == STATUS_BUFFER_TOO_SMALL, "got %08x\n", status );
3307 ok( len == sizeof(*sd_rel), "got %u\n", len );
3309 len += 4;
3310 status = pRtlAbsoluteToSelfRelativeSD( &sd, sd_rel, &len );
3311 ok( status == STATUS_SUCCESS, "got %08x\n", status );
3312 ok( len == sizeof(*sd_rel) + 4, "got %u\n", len );
3314 sd.Control = SE_SELF_RELATIVE;
3315 status = pRtlMakeSelfRelativeSD( &sd, sd_rel, &len );
3316 ok( status == STATUS_SUCCESS, "got %08x\n", status );
3317 ok( len == sizeof(*sd_rel) + 4, "got %u\n", len );
3319 status = pRtlAbsoluteToSelfRelativeSD( &sd, sd_rel, &len );
3320 ok( status == STATUS_BAD_DESCRIPTOR_FORMAT, "got %08x\n", status );
3323 static DWORD (CALLBACK *orig_entry)(HMODULE,DWORD,LPVOID);
3324 static DWORD *dll_main_data;
3326 static inline void *get_rva( HMODULE module, DWORD va )
3328 return (void *)((char *)module + va);
3331 static void CALLBACK ldr_notify_callback1(ULONG reason, LDR_DLL_NOTIFICATION_DATA *data, void *context)
3333 const IMAGE_IMPORT_DESCRIPTOR *imports;
3334 const IMAGE_THUNK_DATA *import_list;
3335 IMAGE_THUNK_DATA *thunk_list;
3336 DWORD *calls = context;
3337 LIST_ENTRY *mark;
3338 LDR_MODULE *mod;
3339 ULONG size;
3340 int i, j;
3342 *calls <<= 4;
3343 *calls |= reason;
3345 if (!lstrcmpiW(data->Loaded.BaseDllName->Buffer, expected_dll))
3346 return;
3348 ok(data->Loaded.Flags == 0, "Expected flags 0, got %x\n", data->Loaded.Flags);
3349 ok(!lstrcmpiW(data->Loaded.BaseDllName->Buffer, expected_dll), "Expected %s, got %s\n",
3350 wine_dbgstr_w(expected_dll), wine_dbgstr_w(data->Loaded.BaseDllName->Buffer));
3351 ok(!!data->Loaded.DllBase, "Expected non zero base address\n");
3352 ok(data->Loaded.SizeOfImage, "Expected non zero image size\n");
3354 /* expect module to be last module listed in LdrData load order list */
3355 mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
3356 mod = CONTAINING_RECORD(mark->Blink, LDR_MODULE, InMemoryOrderModuleList);
3357 ok(mod->BaseAddress == data->Loaded.DllBase, "Expected base address %p, got %p\n",
3358 data->Loaded.DllBase, mod->BaseAddress);
3359 ok(!lstrcmpiW(mod->BaseDllName.Buffer, expected_dll), "Expected %s, got %s\n",
3360 wine_dbgstr_w(expected_dll), wine_dbgstr_w(mod->BaseDllName.Buffer));
3362 /* show that imports have already been resolved */
3363 imports = RtlImageDirectoryEntryToData(data->Loaded.DllBase, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &size);
3364 ok(!!imports, "Expected dll to have imports\n");
3366 for (i = 0; imports[i].Name; i++)
3368 thunk_list = get_rva(data->Loaded.DllBase, (DWORD)imports[i].FirstThunk);
3369 if (imports[i].OriginalFirstThunk)
3370 import_list = get_rva(data->Loaded.DllBase, (DWORD)imports[i].OriginalFirstThunk);
3371 else
3372 import_list = thunk_list;
3374 for (j = 0; import_list[j].u1.Ordinal; j++)
3376 ok(thunk_list[j].u1.AddressOfData > data->Loaded.SizeOfImage,
3377 "Import has not been resolved: %p\n", (void*)thunk_list[j].u1.Function);
3382 static void CALLBACK ldr_notify_callback2(ULONG reason, LDR_DLL_NOTIFICATION_DATA *data, void *context)
3384 DWORD *calls = context;
3385 *calls <<= 4;
3386 *calls |= reason + 2;
3389 static BOOL WINAPI fake_dll_main(HINSTANCE instance, DWORD reason, void* reserved)
3391 if (reason == DLL_PROCESS_ATTACH)
3393 *dll_main_data <<= 4;
3394 *dll_main_data |= 3;
3396 else if (reason == DLL_PROCESS_DETACH)
3398 *dll_main_data <<= 4;
3399 *dll_main_data |= 4;
3401 return orig_entry(instance, reason, reserved);
3404 static void CALLBACK ldr_notify_callback_dll_main(ULONG reason, LDR_DLL_NOTIFICATION_DATA *data, void *context)
3406 DWORD *calls = context;
3407 LIST_ENTRY *mark;
3408 LDR_MODULE *mod;
3410 *calls <<= 4;
3411 *calls |= reason;
3413 if (reason != LDR_DLL_NOTIFICATION_REASON_LOADED)
3414 return;
3416 mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
3417 mod = CONTAINING_RECORD(mark->Blink, LDR_MODULE, InMemoryOrderModuleList);
3418 ok(mod->BaseAddress == data->Loaded.DllBase, "Expected base address %p, got %p\n",
3419 data->Loaded.DllBase, mod->BaseAddress);
3420 if (mod->BaseAddress != data->Loaded.DllBase)
3421 return;
3423 orig_entry = mod->EntryPoint;
3424 mod->EntryPoint = fake_dll_main;
3425 dll_main_data = calls;
3428 static BOOL WINAPI fake_dll_main_fail(HINSTANCE instance, DWORD reason, void* reserved)
3430 if (reason == DLL_PROCESS_ATTACH)
3432 *dll_main_data <<= 4;
3433 *dll_main_data |= 3;
3435 else if (reason == DLL_PROCESS_DETACH)
3437 *dll_main_data <<= 4;
3438 *dll_main_data |= 4;
3440 return FALSE;
3443 static void CALLBACK ldr_notify_callback_fail(ULONG reason, LDR_DLL_NOTIFICATION_DATA *data, void *context)
3445 DWORD *calls = context;
3446 LIST_ENTRY *mark;
3447 LDR_MODULE *mod;
3449 *calls <<= 4;
3450 *calls |= reason;
3452 if (reason != LDR_DLL_NOTIFICATION_REASON_LOADED)
3453 return;
3455 mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
3456 mod = CONTAINING_RECORD(mark->Blink, LDR_MODULE, InMemoryOrderModuleList);
3457 ok(mod->BaseAddress == data->Loaded.DllBase, "Expected base address %p, got %p\n",
3458 data->Loaded.DllBase, mod->BaseAddress);
3459 if (mod->BaseAddress != data->Loaded.DllBase)
3460 return;
3462 orig_entry = mod->EntryPoint;
3463 mod->EntryPoint = fake_dll_main_fail;
3464 dll_main_data = calls;
3467 static void CALLBACK ldr_notify_callback_imports(ULONG reason, LDR_DLL_NOTIFICATION_DATA *data, void *context)
3469 DWORD *calls = context;
3471 if (reason != LDR_DLL_NOTIFICATION_REASON_LOADED)
3472 return;
3474 if (!lstrcmpiW(data->Loaded.BaseDllName->Buffer, crypt32dllW))
3476 *calls <<= 4;
3477 *calls |= 1;
3480 if (!lstrcmpiW(data->Loaded.BaseDllName->Buffer, wintrustdllW))
3482 *calls <<= 4;
3483 *calls |= 2;
3487 static void test_LdrRegisterDllNotification(void)
3489 void *cookie, *cookie2;
3490 NTSTATUS status;
3491 HMODULE mod;
3492 DWORD calls;
3494 if (!pLdrRegisterDllNotification || !pLdrUnregisterDllNotification)
3496 win_skip("Ldr(Un)RegisterDllNotification not available\n");
3497 return;
3500 mod = LoadLibraryW(expected_dll);
3501 if(mod)
3502 FreeLibrary(mod);
3503 else
3504 expected_dll = ws2_32dllW; /* XP Default */
3506 /* generic test */
3507 status = pLdrRegisterDllNotification(0, ldr_notify_callback1, &calls, &cookie);
3508 ok(!status, "Expected STATUS_SUCCESS, got %08x\n", status);
3510 calls = 0;
3511 mod = LoadLibraryW(expected_dll);
3512 ok(!!mod, "Failed to load library: %d\n", GetLastError());
3513 ok(calls == LDR_DLL_NOTIFICATION_REASON_LOADED, "Expected LDR_DLL_NOTIFICATION_REASON_LOADED, got %x\n", calls);
3515 calls = 0;
3516 FreeLibrary(mod);
3517 ok(calls == LDR_DLL_NOTIFICATION_REASON_UNLOADED, "Expected LDR_DLL_NOTIFICATION_REASON_UNLOADED, got %x\n", calls);
3519 /* test order of callbacks */
3520 status = pLdrRegisterDllNotification(0, ldr_notify_callback2, &calls, &cookie2);
3521 ok(!status, "Expected STATUS_SUCCESS, got %08x\n", status);
3523 calls = 0;
3524 mod = LoadLibraryW(expected_dll);
3525 ok(!!mod, "Failed to load library: %d\n", GetLastError());
3526 ok(calls == 0x13, "Expected order 0x13, got %x\n", calls);
3528 calls = 0;
3529 FreeLibrary(mod);
3530 ok(calls == 0x24, "Expected order 0x24, got %x\n", calls);
3532 pLdrUnregisterDllNotification(cookie2);
3533 pLdrUnregisterDllNotification(cookie);
3535 /* test dll main order */
3536 status = pLdrRegisterDllNotification(0, ldr_notify_callback_dll_main, &calls, &cookie);
3537 ok(!status, "Expected STATUS_SUCCESS, got %08x\n", status);
3539 calls = 0;
3540 mod = LoadLibraryW(expected_dll);
3541 ok(!!mod, "Failed to load library: %d\n", GetLastError());
3542 ok(calls == 0x13, "Expected order 0x13, got %x\n", calls);
3544 calls = 0;
3545 FreeLibrary(mod);
3546 ok(calls == 0x42, "Expected order 0x42, got %x\n", calls);
3548 pLdrUnregisterDllNotification(cookie);
3550 /* test dll main order */
3551 status = pLdrRegisterDllNotification(0, ldr_notify_callback_fail, &calls, &cookie);
3552 ok(!status, "Expected STATUS_SUCCESS, got %08x\n", status);
3554 calls = 0;
3555 mod = LoadLibraryW(expected_dll);
3556 ok(!mod, "Expected library to fail loading\n");
3557 ok(calls == 0x1342, "Expected order 0x1342, got %x\n", calls);
3559 pLdrUnregisterDllNotification(cookie);
3561 /* test dll with dependencies */
3562 status = pLdrRegisterDllNotification(0, ldr_notify_callback_imports, &calls, &cookie);
3563 ok(!status, "Expected STATUS_SUCCESS, got %08x\n", status);
3565 calls = 0;
3566 mod = LoadLibraryW(wintrustdllW);
3567 ok(!!mod, "Failed to load library: %d\n", GetLastError());
3568 ok(calls == 0x12 || calls == 0x21, "got %x\n", calls);
3570 FreeLibrary(mod);
3571 pLdrUnregisterDllNotification(cookie);
3574 START_TEST(rtl)
3576 InitFunctionPtrs();
3578 test_RtlCompareMemory();
3579 test_RtlCompareMemoryUlong();
3580 test_RtlMoveMemory();
3581 test_RtlFillMemory();
3582 test_RtlFillMemoryUlong();
3583 test_RtlZeroMemory();
3584 test_RtlUlonglongByteSwap();
3585 test_RtlUniform();
3586 test_RtlRandom();
3587 test_RtlAreAllAccessesGranted();
3588 test_RtlAreAnyAccessesGranted();
3589 test_RtlComputeCrc32();
3590 test_HandleTables();
3591 test_RtlAllocateAndInitializeSid();
3592 test_RtlDeleteTimer();
3593 test_RtlThreadErrorMode();
3594 test_LdrProcessRelocationBlock();
3595 test_RtlIpv4AddressToString();
3596 test_RtlIpv4AddressToStringEx();
3597 test_RtlIpv4StringToAddress();
3598 test_RtlIpv4StringToAddressEx();
3599 test_RtlIpv6AddressToString();
3600 test_RtlIpv6AddressToStringEx();
3601 test_RtlIpv6StringToAddress();
3602 test_RtlIpv6StringToAddressEx();
3603 test_LdrAddRefDll();
3604 test_LdrLockLoaderLock();
3605 test_RtlCompressBuffer();
3606 test_RtlGetCompressionWorkSpaceSize();
3607 test_RtlDecompressBuffer();
3608 test_RtlIsCriticalSectionLocked();
3609 test_RtlInitializeCriticalSectionEx();
3610 test_RtlLeaveCriticalSection();
3611 test_LdrEnumerateLoadedModules();
3612 test_RtlMakeSelfRelativeSD();
3613 test_LdrRegisterDllNotification();