ntdll/tests: Add some RtlValidSecurityDescriptor() tests.
[wine.git] / dlls / ntdll / tests / rtl.c
blobb1ef492627aabbd14c03b285f93de2725d2eaf7b
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"
29 #include "ip2string.h"
30 #include "wine/asm.h"
32 #ifndef __WINE_WINTERNL_H
34 typedef struct _RTL_HANDLE
36 struct _RTL_HANDLE * Next;
37 } RTL_HANDLE;
39 typedef struct _RTL_HANDLE_TABLE
41 ULONG MaxHandleCount;
42 ULONG HandleSize;
43 ULONG Unused[2];
44 PVOID NextFree;
45 PVOID FirstHandle;
46 PVOID ReservedMemory;
47 PVOID MaxHandle;
48 } RTL_HANDLE_TABLE;
50 #endif
52 /* avoid #include <winsock2.h> */
53 #undef htons
54 #ifdef WORDS_BIGENDIAN
55 #define htons(s) ((USHORT)(s))
56 #else /* WORDS_BIGENDIAN */
57 static inline USHORT __my_ushort_swap(USHORT s)
59 return (s >> 8) | (s << 8);
61 #define htons(s) __my_ushort_swap(s)
62 #endif /* WORDS_BIGENDIAN */
65 #ifdef __ASM_USE_FASTCALL_WRAPPER
66 extern ULONG WINAPI wrap_fastcall_func1( void *func, ULONG a );
67 __ASM_STDCALL_FUNC( wrap_fastcall_func1, 8,
68 "popl %ecx\n\t"
69 "popl %eax\n\t"
70 "xchgl (%esp),%ecx\n\t"
71 "jmp *%eax" )
72 #define call_fastcall_func1(func,a) wrap_fastcall_func1(func,a)
73 #else
74 #define call_fastcall_func1(func,a) func(a)
75 #endif
78 /* Function ptrs for ntdll calls */
79 static HMODULE hntdll = 0;
80 static VOID (WINAPI *pRtlMoveMemory)(LPVOID,LPCVOID,SIZE_T);
81 static VOID (WINAPI *pRtlFillMemory)(LPVOID,SIZE_T,BYTE);
82 static VOID (WINAPI *pRtlFillMemoryUlong)(LPVOID,SIZE_T,ULONG);
83 static VOID (WINAPI *pRtlZeroMemory)(LPVOID,SIZE_T);
84 static USHORT (FASTCALL *pRtlUshortByteSwap)(USHORT source);
85 static ULONG (FASTCALL *pRtlUlongByteSwap)(ULONG source);
86 static ULONGLONG (FASTCALL *pRtlUlonglongByteSwap)(ULONGLONG source);
87 static DWORD (WINAPI *pRtlGetThreadErrorMode)(void);
88 static NTSTATUS (WINAPI *pRtlSetThreadErrorMode)(DWORD, LPDWORD);
89 static NTSTATUS (WINAPI *pRtlIpv4AddressToStringExA)(const IN_ADDR *, USHORT, LPSTR, PULONG);
90 static NTSTATUS (WINAPI *pRtlIpv4StringToAddressExA)(PCSTR, BOOLEAN, IN_ADDR *, PUSHORT);
91 static NTSTATUS (WINAPI *pRtlIpv6AddressToStringExA)(struct in6_addr *, ULONG, USHORT, PCHAR, PULONG);
92 static NTSTATUS (WINAPI *pRtlIpv6StringToAddressExA)(PCSTR, struct in6_addr *, PULONG, PUSHORT);
93 static NTSTATUS (WINAPI *pRtlIpv6StringToAddressExW)(PCWSTR, struct in6_addr *, PULONG, PUSHORT);
94 static BOOL (WINAPI *pRtlIsCriticalSectionLocked)(CRITICAL_SECTION *);
95 static BOOL (WINAPI *pRtlIsCriticalSectionLockedByThread)(CRITICAL_SECTION *);
96 static NTSTATUS (WINAPI *pRtlInitializeCriticalSectionEx)(CRITICAL_SECTION *, ULONG, ULONG);
97 static NTSTATUS (WINAPI *pLdrEnumerateLoadedModules)(void *, void *, void *);
98 static NTSTATUS (WINAPI *pLdrRegisterDllNotification)(ULONG, PLDR_DLL_NOTIFICATION_FUNCTION, void *, void **);
99 static NTSTATUS (WINAPI *pLdrUnregisterDllNotification)(void *);
101 static HMODULE hkernel32 = 0;
102 static BOOL (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
105 #define LEN 16
106 static const char* src_src = "This is a test!"; /* 16 bytes long, incl NUL */
107 static WCHAR ws2_32dllW[] = {'w','s','2','_','3','2','.','d','l','l',0};
108 static WCHAR nsidllW[] = {'n','s','i','.','d','l','l',0};
109 static WCHAR wintrustdllW[] = {'w','i','n','t','r','u','s','t','.','d','l','l',0};
110 static WCHAR crypt32dllW[] = {'c','r','y','p','t','3','2','.','d','l','l',0};
111 static ULONG src_aligned_block[4];
112 static ULONG dest_aligned_block[32];
113 static const char *src = (const char*)src_aligned_block;
114 static char* dest = (char*)dest_aligned_block;
115 const WCHAR *expected_dll = nsidllW;
117 static void InitFunctionPtrs(void)
119 hntdll = LoadLibraryA("ntdll.dll");
120 ok(hntdll != 0, "LoadLibrary failed\n");
121 if (hntdll) {
122 pRtlMoveMemory = (void *)GetProcAddress(hntdll, "RtlMoveMemory");
123 pRtlFillMemory = (void *)GetProcAddress(hntdll, "RtlFillMemory");
124 pRtlFillMemoryUlong = (void *)GetProcAddress(hntdll, "RtlFillMemoryUlong");
125 pRtlZeroMemory = (void *)GetProcAddress(hntdll, "RtlZeroMemory");
126 pRtlUshortByteSwap = (void *)GetProcAddress(hntdll, "RtlUshortByteSwap");
127 pRtlUlongByteSwap = (void *)GetProcAddress(hntdll, "RtlUlongByteSwap");
128 pRtlUlonglongByteSwap = (void *)GetProcAddress(hntdll, "RtlUlonglongByteSwap");
129 pRtlGetThreadErrorMode = (void *)GetProcAddress(hntdll, "RtlGetThreadErrorMode");
130 pRtlSetThreadErrorMode = (void *)GetProcAddress(hntdll, "RtlSetThreadErrorMode");
131 pRtlIpv4AddressToStringExA = (void *)GetProcAddress(hntdll, "RtlIpv4AddressToStringExA");
132 pRtlIpv4StringToAddressExA = (void *)GetProcAddress(hntdll, "RtlIpv4StringToAddressExA");
133 pRtlIpv6AddressToStringExA = (void *)GetProcAddress(hntdll, "RtlIpv6AddressToStringExA");
134 pRtlIpv6StringToAddressExA = (void *)GetProcAddress(hntdll, "RtlIpv6StringToAddressExA");
135 pRtlIpv6StringToAddressExW = (void *)GetProcAddress(hntdll, "RtlIpv6StringToAddressExW");
136 pRtlIsCriticalSectionLocked = (void *)GetProcAddress(hntdll, "RtlIsCriticalSectionLocked");
137 pRtlIsCriticalSectionLockedByThread = (void *)GetProcAddress(hntdll, "RtlIsCriticalSectionLockedByThread");
138 pRtlInitializeCriticalSectionEx = (void *)GetProcAddress(hntdll, "RtlInitializeCriticalSectionEx");
139 pLdrEnumerateLoadedModules = (void *)GetProcAddress(hntdll, "LdrEnumerateLoadedModules");
140 pLdrRegisterDllNotification = (void *)GetProcAddress(hntdll, "LdrRegisterDllNotification");
141 pLdrUnregisterDllNotification = (void *)GetProcAddress(hntdll, "LdrUnregisterDllNotification");
143 hkernel32 = LoadLibraryA("kernel32.dll");
144 ok(hkernel32 != 0, "LoadLibrary failed\n");
145 if (hkernel32) {
146 pIsWow64Process = (void *)GetProcAddress(hkernel32, "IsWow64Process");
148 strcpy((char*)src_aligned_block, src_src);
149 ok(strlen(src) == 15, "Source must be 16 bytes long!\n");
152 static void test_RtlQueryProcessDebugInformation(void)
154 DEBUG_BUFFER *buffer;
155 NTSTATUS status;
157 buffer = RtlCreateQueryDebugBuffer( 0, 0 );
158 ok( buffer != NULL, "RtlCreateQueryDebugBuffer returned NULL" );
160 status = RtlQueryProcessDebugInformation( GetCurrentThreadId(), PDI_HEAPS | PDI_HEAP_BLOCKS, buffer );
161 ok( status == STATUS_INVALID_CID, "RtlQueryProcessDebugInformation returned %lx\n", status );
163 status = RtlQueryProcessDebugInformation( GetCurrentProcessId(), PDI_HEAPS | PDI_HEAP_BLOCKS, buffer );
164 ok( !status, "RtlQueryProcessDebugInformation returned %lx\n", status );
166 status = RtlDestroyQueryDebugBuffer( buffer );
167 ok( !status, "RtlDestroyQueryDebugBuffer returned %lx\n", status );
170 #define COMP(str1,str2,cmplen,len) size = RtlCompareMemory(str1, str2, cmplen); \
171 ok(size == len, "Expected %Id, got %Id\n", size, (SIZE_T)len)
173 static void test_RtlCompareMemory(void)
175 SIZE_T size;
177 strcpy(dest, src);
179 COMP(src,src,0,0);
180 COMP(src,src,LEN,LEN);
181 dest[0] = 'x';
182 COMP(src,dest,LEN,0);
185 static void test_RtlCompareMemoryUlong(void)
187 ULONG a[10];
188 ULONG result;
190 a[0]= 0x0123;
191 a[1]= 0x4567;
192 a[2]= 0x89ab;
193 a[3]= 0xcdef;
194 result = RtlCompareMemoryUlong(a, 0, 0x0123);
195 ok(result == 0, "RtlCompareMemoryUlong(%p, 0, 0x0123) returns %lu, expected 0\n", a, result);
196 result = RtlCompareMemoryUlong(a, 3, 0x0123);
197 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %lu, expected 0\n", a, result);
198 result = RtlCompareMemoryUlong(a, 4, 0x0123);
199 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %lu, expected 4\n", a, result);
200 result = RtlCompareMemoryUlong(a, 5, 0x0123);
201 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %lu, expected 4\n", a, result);
202 result = RtlCompareMemoryUlong(a, 7, 0x0123);
203 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %lu, expected 4\n", a, result);
204 result = RtlCompareMemoryUlong(a, 8, 0x0123);
205 ok(result == 4, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %lu, expected 4\n", a, result);
206 result = RtlCompareMemoryUlong(a, 9, 0x0123);
207 ok(result == 4, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %lu, expected 4\n", a, result);
208 result = RtlCompareMemoryUlong(a, 4, 0x0127);
209 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x0127) returns %lu, expected 0\n", a, result);
210 result = RtlCompareMemoryUlong(a, 4, 0x7123);
211 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x7123) returns %lu, expected 0\n", a, result);
212 result = RtlCompareMemoryUlong(a, 16, 0x4567);
213 ok(result == 0, "RtlCompareMemoryUlong(%p, 16, 0x4567) returns %lu, expected 0\n", a, result);
215 a[1]= 0x0123;
216 result = RtlCompareMemoryUlong(a, 3, 0x0123);
217 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %lu, expected 0\n", a, result);
218 result = RtlCompareMemoryUlong(a, 4, 0x0123);
219 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %lu, expected 4\n", a, result);
220 result = RtlCompareMemoryUlong(a, 5, 0x0123);
221 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %lu, expected 4\n", a, result);
222 result = RtlCompareMemoryUlong(a, 7, 0x0123);
223 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %lu, expected 4\n", a, result);
224 result = RtlCompareMemoryUlong(a, 8, 0x0123);
225 ok(result == 8, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %lu, expected 8\n", a, result);
226 result = RtlCompareMemoryUlong(a, 9, 0x0123);
227 ok(result == 8, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %lu, expected 8\n", a, result);
230 #define COPY(len) memset(dest,0,sizeof(dest_aligned_block)); pRtlMoveMemory(dest, src, len)
231 #define CMP(str) ok(strcmp(dest,str) == 0, "Expected '%s', got '%s'\n", str, dest)
233 static void test_RtlMoveMemory(void)
235 if (!pRtlMoveMemory)
237 win_skip("RtlMoveMemory is not available\n");
238 return;
241 /* Length should be in bytes and not rounded. Use strcmp to ensure we
242 * didn't write past the end (it checks for the final NUL left by memset)
244 COPY(0); CMP("");
245 COPY(1); CMP("T");
246 COPY(2); CMP("Th");
247 COPY(3); CMP("Thi");
248 COPY(4); CMP("This");
249 COPY(5); CMP("This ");
250 COPY(6); CMP("This i");
251 COPY(7); CMP("This is");
252 COPY(8); CMP("This is ");
253 COPY(9); CMP("This is a");
255 /* Overlapping */
256 strcpy(dest, src); pRtlMoveMemory(dest, dest + 1, strlen(src) - 1);
257 CMP("his is a test!!");
258 strcpy(dest, src); pRtlMoveMemory(dest + 1, dest, strlen(src));
259 CMP("TThis is a test!");
262 #define FILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemory(dest,len,'x')
264 static void test_RtlFillMemory(void)
266 if (!pRtlFillMemory)
268 win_skip("RtlFillMemory is not available\n");
269 return;
272 /* Length should be in bytes and not rounded. Use strcmp to ensure we
273 * didn't write past the end (the remainder of the string should match)
275 FILL(0); CMP("This is a test!");
276 FILL(1); CMP("xhis is a test!");
277 FILL(2); CMP("xxis is a test!");
278 FILL(3); CMP("xxxs is a test!");
279 FILL(4); CMP("xxxx is a test!");
280 FILL(5); CMP("xxxxxis a test!");
281 FILL(6); CMP("xxxxxxs a test!");
282 FILL(7); CMP("xxxxxxx a test!");
283 FILL(8); CMP("xxxxxxxxa test!");
284 FILL(9); CMP("xxxxxxxxx test!");
287 #define LFILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemoryUlong(dest,len,val)
289 static void test_RtlFillMemoryUlong(void)
291 ULONG val = ('x' << 24) | ('x' << 16) | ('x' << 8) | 'x';
292 if (!pRtlFillMemoryUlong)
294 win_skip("RtlFillMemoryUlong is not available\n");
295 return;
298 /* Length should be in bytes and not rounded. Use strcmp to ensure we
299 * didn't write past the end (the remainder of the string should match)
301 LFILL(0); CMP("This is a test!");
302 LFILL(1); CMP("This is a test!");
303 LFILL(2); CMP("This is a test!");
304 LFILL(3); CMP("This is a test!");
305 LFILL(4); CMP("xxxx is a test!");
306 LFILL(5); CMP("xxxx is a test!");
307 LFILL(6); CMP("xxxx is a test!");
308 LFILL(7); CMP("xxxx is a test!");
309 LFILL(8); CMP("xxxxxxxxa test!");
310 LFILL(9); CMP("xxxxxxxxa test!");
313 #define ZERO(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlZeroMemory(dest,len)
314 #define MCMP(str) ok(memcmp(dest,str,LEN) == 0, "Memcmp failed\n")
316 static void test_RtlZeroMemory(void)
318 if (!pRtlZeroMemory)
320 win_skip("RtlZeroMemory is not available\n");
321 return;
324 /* Length should be in bytes and not rounded. */
325 ZERO(0); MCMP("This is a test!");
326 ZERO(1); MCMP("\0his is a test!");
327 ZERO(2); MCMP("\0\0is is a test!");
328 ZERO(3); MCMP("\0\0\0s is a test!");
329 ZERO(4); MCMP("\0\0\0\0 is a test!");
330 ZERO(5); MCMP("\0\0\0\0\0is a test!");
331 ZERO(6); MCMP("\0\0\0\0\0\0s a test!");
332 ZERO(7); MCMP("\0\0\0\0\0\0\0 a test!");
333 ZERO(8); MCMP("\0\0\0\0\0\0\0\0a test!");
334 ZERO(9); MCMP("\0\0\0\0\0\0\0\0\0 test!");
337 static void test_RtlByteSwap(void)
339 ULONGLONG llresult;
340 ULONG lresult;
341 USHORT sresult;
343 #ifdef _WIN64
344 /* the Rtl*ByteSwap() are always inlined and not exported from ntdll on 64bit */
345 sresult = RtlUshortByteSwap( 0x1234 );
346 ok( 0x3412 == sresult,
347 "inlined RtlUshortByteSwap() returns 0x%x\n", sresult );
348 lresult = RtlUlongByteSwap( 0x87654321 );
349 ok( 0x21436587 == lresult,
350 "inlined RtlUlongByteSwap() returns 0x%lx\n", lresult );
351 llresult = RtlUlonglongByteSwap( 0x7654321087654321ull );
352 ok( 0x2143658710325476 == llresult,
353 "inlined RtlUlonglongByteSwap() returns %#I64x\n", llresult );
354 #else
355 ok( pRtlUshortByteSwap != NULL, "RtlUshortByteSwap is not available\n" );
356 if ( pRtlUshortByteSwap )
358 sresult = call_fastcall_func1( pRtlUshortByteSwap, 0x1234u );
359 ok( 0x3412u == sresult,
360 "ntdll.RtlUshortByteSwap() returns %#x\n", sresult );
363 ok( pRtlUlongByteSwap != NULL, "RtlUlongByteSwap is not available\n" );
364 if ( pRtlUlongByteSwap )
366 lresult = call_fastcall_func1( pRtlUlongByteSwap, 0x87654321ul );
367 ok( 0x21436587ul == lresult,
368 "ntdll.RtlUlongByteSwap() returns %#lx\n", lresult );
371 ok( pRtlUlonglongByteSwap != NULL, "RtlUlonglongByteSwap is not available\n");
372 if ( pRtlUlonglongByteSwap )
374 llresult = pRtlUlonglongByteSwap( 0x7654321087654321ull );
375 ok( 0x2143658710325476ull == llresult,
376 "ntdll.RtlUlonglongByteSwap() returns %#I64x\n", llresult );
378 #endif
382 static void test_RtlUniform(void)
384 const ULONG step = 0x7fff;
385 ULONG num;
386 ULONG seed;
387 ULONG seed_bak;
388 ULONG expected;
389 ULONG result;
392 * According to the documentation RtlUniform is using D.H. Lehmer's 1948
393 * algorithm. We assume a more generic version of this algorithm,
394 * which is the linear congruential generator (LCG). Its formula is:
396 * X_(n+1) = (a * X_n + c) % m
398 * where a is the multiplier, c is the increment, and m is the modulus.
400 * According to the documentation, the random numbers are distributed over
401 * [0..MAXLONG]. Therefore, the modulus is MAXLONG + 1:
403 * X_(n+1) = (a * X_n + c) % (MAXLONG + 1)
405 * To find out the increment, we just call RtlUniform with seed set to 0.
406 * This reveals c = 0x7fffffc3.
408 seed = 0;
409 expected = 0x7fffffc3;
410 result = RtlUniform(&seed);
411 ok(result == expected,
412 "RtlUniform(&seed (seed == 0)) returns %lx, expected %lx\n",
413 result, expected);
416 * The formula is now:
418 * X_(n+1) = (a * X_n + 0x7fffffc3) % (MAXLONG + 1)
420 * If the modulus is correct, RtlUniform(0) shall equal RtlUniform(MAXLONG + 1).
421 * However, testing reveals that this is not the case.
422 * That is, the modulus in the documentation is incorrect.
424 seed = 0x80000000U;
425 expected = 0x7fffffb1;
426 result = RtlUniform(&seed);
428 ok(result == expected,
429 "RtlUniform(&seed (seed == 0x80000000)) returns %lx, expected %lx\n",
430 result, expected);
433 * We try another value for modulus, say MAXLONG.
434 * We discover that RtlUniform(0) equals RtlUniform(MAXLONG), which means
435 * the correct value for the modulus is actually MAXLONG.
437 seed = 0x7fffffff;
438 expected = 0x7fffffc3;
439 result = RtlUniform(&seed);
440 ok(result == expected,
441 "RtlUniform(&seed (seed == 0x7fffffff)) returns %lx, expected %lx\n",
442 result, expected);
445 * The formula is now:
447 * X_(n+1) = (a * X_n + 0x7fffffc3) % MAXLONG
449 * To find out the multiplier we can use:
451 * a = RtlUniform(1) - 0x7fffffc3 (mod MAXLONG)
453 * This way, we find out that a = -18 (mod MAXLONG),
454 * which is congruent to 0x7fffffed (MAXLONG - 18).
456 seed = 1;
457 expected = ((ULONGLONG)seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
458 result = RtlUniform(&seed);
459 ok(result == expected,
460 "RtlUniform(&seed (seed == 1)) returns %lx, expected %lx\n",
461 result, expected);
463 num = 2;
466 seed = num;
467 expected = ((ULONGLONG)seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
468 result = RtlUniform(&seed);
469 ok(result == expected,
470 "test: RtlUniform(&seed (seed == %lx)) returns %lx, expected %lx\n",
471 num, result, expected);
472 ok(seed == expected,
473 "test: RtlUniform(&seed (seed == %lx)) sets seed to %lx, expected %lx\n",
474 num, result, expected);
476 num += step;
477 } while (num >= 2 + step);
479 seed = 0;
480 for (num = 0; num <= 100000; num++) {
481 expected = ((ULONGLONG)seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
482 seed_bak = seed;
483 result = RtlUniform(&seed);
484 ok(result == expected,
485 "test: %ld RtlUniform(&seed (seed == %lx)) returns %lx, expected %lx\n",
486 num, seed_bak, result, expected);
487 ok(seed == expected,
488 "test: %ld RtlUniform(&seed (seed == %lx)) sets seed to %lx, expected %lx\n",
489 num, seed_bak, result, expected);
490 } /* for */
494 static void test_RtlRandom(void)
496 int i, j;
497 ULONG seed;
498 ULONG res[512];
500 seed = 0;
501 for (i = 0; i < ARRAY_SIZE(res); i++)
503 res[i] = RtlRandom(&seed);
504 ok(seed != res[i], "%i: seed is same as res %lx\n", i, seed);
505 for (j = 0; j < i; j++)
506 ok(res[i] != res[j], "res[%i] (%lx) is same as res[%i] (%lx)\n", j, res[j], i, res[i]);
511 typedef struct {
512 ACCESS_MASK GrantedAccess;
513 ACCESS_MASK DesiredAccess;
514 BOOLEAN result;
515 } all_accesses_t;
517 static const all_accesses_t all_accesses[] = {
518 {0xFEDCBA76, 0xFEDCBA76, 1},
519 {0x00000000, 0xFEDCBA76, 0},
520 {0xFEDCBA76, 0x00000000, 1},
521 {0x00000000, 0x00000000, 1},
522 {0xFEDCBA76, 0xFEDCBA70, 1},
523 {0xFEDCBA70, 0xFEDCBA76, 0},
524 {0xFEDCBA76, 0xFEDC8A76, 1},
525 {0xFEDC8A76, 0xFEDCBA76, 0},
526 {0xFEDCBA76, 0xC8C4B242, 1},
527 {0xC8C4B242, 0xFEDCBA76, 0},
531 static void test_RtlAreAllAccessesGranted(void)
533 unsigned int test_num;
534 BOOLEAN result;
536 for (test_num = 0; test_num < ARRAY_SIZE(all_accesses); test_num++) {
537 result = RtlAreAllAccessesGranted(all_accesses[test_num].GrantedAccess,
538 all_accesses[test_num].DesiredAccess);
539 ok(all_accesses[test_num].result == result,
540 "(test %d): RtlAreAllAccessesGranted(%08lx, %08lx) returns %d, expected %d\n",
541 test_num, all_accesses[test_num].GrantedAccess,
542 all_accesses[test_num].DesiredAccess,
543 result, all_accesses[test_num].result);
544 } /* for */
548 typedef struct {
549 ACCESS_MASK GrantedAccess;
550 ACCESS_MASK DesiredAccess;
551 BOOLEAN result;
552 } any_accesses_t;
554 static const any_accesses_t any_accesses[] = {
555 {0xFEDCBA76, 0xFEDCBA76, 1},
556 {0x00000000, 0xFEDCBA76, 0},
557 {0xFEDCBA76, 0x00000000, 0},
558 {0x00000000, 0x00000000, 0},
559 {0xFEDCBA76, 0x01234589, 0},
560 {0x00040000, 0xFEDCBA76, 1},
561 {0x00040000, 0xFED8BA76, 0},
562 {0xFEDCBA76, 0x00040000, 1},
563 {0xFED8BA76, 0x00040000, 0},
567 static void test_RtlAreAnyAccessesGranted(void)
569 unsigned int test_num;
570 BOOLEAN result;
572 for (test_num = 0; test_num < ARRAY_SIZE(any_accesses); test_num++) {
573 result = RtlAreAnyAccessesGranted(any_accesses[test_num].GrantedAccess,
574 any_accesses[test_num].DesiredAccess);
575 ok(any_accesses[test_num].result == result,
576 "(test %d): RtlAreAnyAccessesGranted(%08lx, %08lx) returns %d, expected %d\n",
577 test_num, any_accesses[test_num].GrantedAccess,
578 any_accesses[test_num].DesiredAccess,
579 result, any_accesses[test_num].result);
580 } /* for */
583 static void test_RtlComputeCrc32(void)
585 DWORD crc = 0;
587 crc = RtlComputeCrc32(crc, (const BYTE *)src, LEN);
588 ok(crc == 0x40861dc2,"Expected 0x40861dc2, got %8lx\n", crc);
592 typedef struct MY_HANDLE
594 RTL_HANDLE RtlHandle;
595 void * MyValue;
596 } MY_HANDLE;
598 static inline void RtlpMakeHandleAllocated(RTL_HANDLE * Handle)
600 ULONG_PTR *AllocatedBit = (ULONG_PTR *)(&Handle->Next);
601 *AllocatedBit = *AllocatedBit | 1;
604 static void test_HandleTables(void)
606 BOOLEAN result;
607 NTSTATUS status;
608 ULONG Index;
609 MY_HANDLE * MyHandle;
610 RTL_HANDLE_TABLE HandleTable;
612 RtlInitializeHandleTable(0x3FFF, sizeof(MY_HANDLE), &HandleTable);
613 MyHandle = (MY_HANDLE *)RtlAllocateHandle(&HandleTable, &Index);
614 ok(MyHandle != NULL, "RtlAllocateHandle failed\n");
615 RtlpMakeHandleAllocated(&MyHandle->RtlHandle);
616 MyHandle = NULL;
617 result = RtlIsValidIndexHandle(&HandleTable, Index, (RTL_HANDLE **)&MyHandle);
618 ok(result, "Handle %p wasn't valid\n", MyHandle);
619 result = RtlFreeHandle(&HandleTable, &MyHandle->RtlHandle);
620 ok(result, "Couldn't free handle %p\n", MyHandle);
621 status = RtlDestroyHandleTable(&HandleTable);
622 ok(status == STATUS_SUCCESS, "RtlDestroyHandleTable failed with error 0x%08lx\n", status);
625 static void test_RtlAllocateAndInitializeSid(void)
627 NTSTATUS ret;
628 SID_IDENTIFIER_AUTHORITY sia = {{ 1, 2, 3, 4, 5, 6 }};
629 PSID psid;
631 ret = RtlAllocateAndInitializeSid(&sia, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
632 ok(!ret, "RtlAllocateAndInitializeSid error %08lx\n", ret);
633 ret = RtlFreeSid(psid);
634 ok(!ret, "RtlFreeSid error %08lx\n", ret);
636 /* these tests crash on XP */
637 if (0)
639 RtlAllocateAndInitializeSid(NULL, 0, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
640 RtlAllocateAndInitializeSid(&sia, 0, 1, 2, 3, 4, 5, 6, 7, 8, NULL);
643 ret = RtlAllocateAndInitializeSid(&sia, 9, 1, 2, 3, 4, 5, 6, 7, 8, &psid);
644 ok(ret == STATUS_INVALID_SID, "wrong error %08lx\n", ret);
647 static void test_RtlDeleteTimer(void)
649 NTSTATUS ret;
651 ret = RtlDeleteTimer(NULL, NULL, NULL);
652 ok(ret == STATUS_INVALID_PARAMETER_1 ||
653 ret == STATUS_INVALID_PARAMETER, /* W2K */
654 "expected STATUS_INVALID_PARAMETER_1 or STATUS_INVALID_PARAMETER, got %lx\n", ret);
657 static void test_RtlThreadErrorMode(void)
659 DWORD oldmode;
660 BOOL is_wow64;
661 DWORD mode;
662 NTSTATUS status;
664 if (!pRtlGetThreadErrorMode || !pRtlSetThreadErrorMode)
666 win_skip("RtlGetThreadErrorMode and/or RtlSetThreadErrorMode not available\n");
667 return;
670 if (!pIsWow64Process || !pIsWow64Process(GetCurrentProcess(), &is_wow64))
671 is_wow64 = FALSE;
673 oldmode = pRtlGetThreadErrorMode();
675 status = pRtlSetThreadErrorMode(0x70, &mode);
676 ok(status == STATUS_SUCCESS ||
677 status == STATUS_WAIT_1, /* Vista */
678 "RtlSetThreadErrorMode failed with error 0x%08lx\n", status);
679 ok(mode == oldmode,
680 "RtlSetThreadErrorMode returned mode 0x%lx, expected 0x%lx\n",
681 mode, oldmode);
682 ok(pRtlGetThreadErrorMode() == 0x70,
683 "RtlGetThreadErrorMode returned 0x%lx, expected 0x%x\n", mode, 0x70);
684 if (!is_wow64)
686 ok(NtCurrentTeb()->HardErrorMode == 0x70,
687 "The TEB contains 0x%lx, expected 0x%x\n",
688 NtCurrentTeb()->HardErrorMode, 0x70);
691 status = pRtlSetThreadErrorMode(0, &mode);
692 ok(status == STATUS_SUCCESS ||
693 status == STATUS_WAIT_1, /* Vista */
694 "RtlSetThreadErrorMode failed with error 0x%08lx\n", status);
695 ok(mode == 0x70,
696 "RtlSetThreadErrorMode returned mode 0x%lx, expected 0x%x\n",
697 mode, 0x70);
698 ok(pRtlGetThreadErrorMode() == 0,
699 "RtlGetThreadErrorMode returned 0x%lx, expected 0x%x\n", mode, 0);
700 if (!is_wow64)
702 ok(NtCurrentTeb()->HardErrorMode == 0,
703 "The TEB contains 0x%lx, expected 0x%x\n",
704 NtCurrentTeb()->HardErrorMode, 0);
707 for (mode = 1; mode; mode <<= 1)
709 status = pRtlSetThreadErrorMode(mode, NULL);
710 if (mode & 0x70)
711 ok(status == STATUS_SUCCESS ||
712 status == STATUS_WAIT_1, /* Vista */
713 "RtlSetThreadErrorMode(%lx,NULL) failed with error 0x%08lx\n",
714 mode, status);
715 else
716 ok(status == STATUS_INVALID_PARAMETER_1,
717 "RtlSetThreadErrorMode(%lx,NULL) returns 0x%08lx, "
718 "expected STATUS_INVALID_PARAMETER_1\n",
719 mode, status);
722 pRtlSetThreadErrorMode(oldmode, NULL);
725 static void test_LdrProcessRelocationBlock(void)
727 IMAGE_BASE_RELOCATION *ret;
728 USHORT reloc;
729 DWORD addr32;
730 SHORT addr16;
732 addr32 = 0x50005;
733 reloc = IMAGE_REL_BASED_HIGHLOW<<12;
734 ret = LdrProcessRelocationBlock(&addr32, 1, &reloc, 0x500050);
735 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
736 ok(addr32 == 0x550055, "addr32 = %lx, expected 0x550055\n", addr32);
738 addr16 = 0x505;
739 reloc = IMAGE_REL_BASED_HIGH<<12;
740 ret = LdrProcessRelocationBlock(&addr16, 1, &reloc, 0x500060);
741 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
742 ok(addr16 == 0x555, "addr16 = %x, expected 0x555\n", addr16);
744 addr16 = 0x505;
745 reloc = IMAGE_REL_BASED_LOW<<12;
746 ret = LdrProcessRelocationBlock(&addr16, 1, &reloc, 0x500060);
747 ok((USHORT*)ret == &reloc+1, "ret = %p, expected %p\n", ret, &reloc+1);
748 ok(addr16 == 0x565, "addr16 = %x, expected 0x565\n", addr16);
751 static void test_RtlIpv4AddressToString(void)
753 CHAR buffer[20];
754 CHAR *res;
755 IN_ADDR ip;
756 DWORD_PTR len;
758 ip.S_un.S_un_b.s_b1 = 1;
759 ip.S_un.S_un_b.s_b2 = 2;
760 ip.S_un.S_un_b.s_b3 = 3;
761 ip.S_un.S_un_b.s_b4 = 4;
763 memset(buffer, '#', sizeof(buffer) - 1);
764 buffer[sizeof(buffer) -1] = 0;
765 res = RtlIpv4AddressToStringA(&ip, buffer);
766 len = strlen(buffer);
767 ok(res == (buffer + len), "got %p with '%s' (expected %p)\n", res, buffer, buffer + len);
769 res = RtlIpv4AddressToStringA(&ip, NULL);
770 ok( (res == (char *)~0) ||
771 broken(res == (char *)len), /* XP and w2003 */
772 "got %p (expected ~0)\n", res);
774 if (0) {
775 /* this crashes in windows */
776 memset(buffer, '#', sizeof(buffer) - 1);
777 buffer[sizeof(buffer) -1] = 0;
778 res = RtlIpv4AddressToStringA(NULL, buffer);
779 trace("got %p with '%s'\n", res, buffer);
782 if (0) {
783 /* this crashes in windows */
784 res = RtlIpv4AddressToStringA(NULL, NULL);
785 trace("got %p\n", res);
789 static void test_RtlIpv4AddressToStringEx(void)
791 CHAR ip_1234[] = "1.2.3.4";
792 CHAR ip_1234_80[] = "1.2.3.4:80";
793 LPSTR expect;
794 CHAR buffer[30];
795 NTSTATUS res;
796 IN_ADDR ip;
797 ULONG size;
798 DWORD used;
799 USHORT port;
801 if (!pRtlIpv4AddressToStringExA)
803 win_skip("RtlIpv4AddressToStringExA not available\n");
804 return;
807 ip.S_un.S_un_b.s_b1 = 1;
808 ip.S_un.S_un_b.s_b2 = 2;
809 ip.S_un.S_un_b.s_b3 = 3;
810 ip.S_un.S_un_b.s_b4 = 4;
812 port = htons(80);
813 expect = ip_1234_80;
815 size = sizeof(buffer);
816 memset(buffer, '#', sizeof(buffer) - 1);
817 buffer[sizeof(buffer) -1] = 0;
818 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
819 used = strlen(buffer);
820 ok( (res == STATUS_SUCCESS) &&
821 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
822 "got 0x%lx and size %ld with '%s'\n", res, size, buffer);
824 size = used + 1;
825 memset(buffer, '#', sizeof(buffer) - 1);
826 buffer[sizeof(buffer) -1] = 0;
827 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
828 ok( (res == STATUS_SUCCESS) &&
829 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
830 "got 0x%lx and size %ld with '%s'\n", res, size, buffer);
832 size = used;
833 memset(buffer, '#', sizeof(buffer) - 1);
834 buffer[sizeof(buffer) -1] = 0;
835 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
836 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
837 "got 0x%lx and %ld with '%s' (expected STATUS_INVALID_PARAMETER and %ld)\n",
838 res, size, buffer, used + 1);
840 size = used - 1;
841 memset(buffer, '#', sizeof(buffer) - 1);
842 buffer[sizeof(buffer) -1] = 0;
843 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
844 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
845 "got 0x%lx and %ld with '%s' (expected STATUS_INVALID_PARAMETER and %ld)\n",
846 res, size, buffer, used + 1);
849 /* to get only the ip, use 0 as port */
850 port = 0;
851 expect = ip_1234;
853 size = sizeof(buffer);
854 memset(buffer, '#', sizeof(buffer) - 1);
855 buffer[sizeof(buffer) -1] = 0;
856 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
857 used = strlen(buffer);
858 ok( (res == STATUS_SUCCESS) &&
859 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
860 "got 0x%lx and size %ld with '%s'\n", res, size, buffer);
862 size = used + 1;
863 memset(buffer, '#', sizeof(buffer) - 1);
864 buffer[sizeof(buffer) -1] = 0;
865 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
866 ok( (res == STATUS_SUCCESS) &&
867 (size == strlen(expect) + 1) && !strcmp(buffer, expect),
868 "got 0x%lx and size %ld with '%s'\n", res, size, buffer);
870 size = used;
871 memset(buffer, '#', sizeof(buffer) - 1);
872 buffer[sizeof(buffer) -1] = 0;
873 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
874 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
875 "got 0x%lx and %ld with '%s' (expected STATUS_INVALID_PARAMETER and %ld)\n",
876 res, size, buffer, used + 1);
878 size = used - 1;
879 memset(buffer, '#', sizeof(buffer) - 1);
880 buffer[sizeof(buffer) -1] = 0;
881 res = pRtlIpv4AddressToStringExA(&ip, port, buffer, &size);
882 ok( (res == STATUS_INVALID_PARAMETER) && (size == used + 1),
883 "got 0x%lx and %ld with '%s' (expected STATUS_INVALID_PARAMETER and %ld)\n",
884 res, size, buffer, used + 1);
887 /* parameters are checked */
888 memset(buffer, '#', sizeof(buffer) - 1);
889 buffer[sizeof(buffer) -1] = 0;
890 res = pRtlIpv4AddressToStringExA(&ip, 0, buffer, NULL);
891 ok(res == STATUS_INVALID_PARAMETER,
892 "got 0x%lx with '%s' (expected STATUS_INVALID_PARAMETER)\n", res, buffer);
894 size = sizeof(buffer);
895 res = pRtlIpv4AddressToStringExA(&ip, 0, NULL, &size);
896 ok( res == STATUS_INVALID_PARAMETER,
897 "got 0x%lx and size %ld (expected STATUS_INVALID_PARAMETER)\n", res, size);
899 size = sizeof(buffer);
900 memset(buffer, '#', sizeof(buffer) - 1);
901 buffer[sizeof(buffer) -1] = 0;
902 res = pRtlIpv4AddressToStringExA(NULL, 0, buffer, &size);
903 ok( res == STATUS_INVALID_PARAMETER,
904 "got 0x%lx and size %ld with '%s' (expected STATUS_INVALID_PARAMETER)\n",
905 res, size, buffer);
908 static struct
910 PCSTR address;
911 NTSTATUS res;
912 int terminator_offset;
913 int ip[4];
914 enum { normal_4, strict_diff_4 = 1, ex_fail_4 = 2 } flags;
915 NTSTATUS res_strict;
916 int terminator_offset_strict;
917 int ip_strict[4];
918 } ipv4_tests[] =
920 { "", STATUS_INVALID_PARAMETER, 0, { -1 } },
921 { " ", STATUS_INVALID_PARAMETER, 0, { -1 } },
922 { "1.1.1.1", STATUS_SUCCESS, 7, { 1, 1, 1, 1 } },
923 { "0.0.0.0", STATUS_SUCCESS, 7, { 0, 0, 0, 0 } },
924 { "255.255.255.255", STATUS_SUCCESS, 15, { 255, 255, 255, 255 } },
925 { "255.255.255.255:123", STATUS_SUCCESS, 15, { 255, 255, 255, 255 } },
926 { "255.255.255.256", STATUS_INVALID_PARAMETER, 15, { -1 } },
927 { "255.255.255.4294967295", STATUS_INVALID_PARAMETER, 22, { -1 } },
928 { "255.255.255.4294967296", STATUS_INVALID_PARAMETER, 21, { -1 } },
929 { "255.255.255.4294967297", STATUS_INVALID_PARAMETER, 21, { -1 } },
930 { "a", STATUS_INVALID_PARAMETER, 0, { -1 } },
931 { "1.1.1.0xaA", STATUS_SUCCESS, 10, { 1, 1, 1, 170 }, strict_diff_4,
932 STATUS_INVALID_PARAMETER, 8, { -1 } },
933 { "1.1.1.0XaA", STATUS_SUCCESS, 10, { 1, 1, 1, 170 }, strict_diff_4,
934 STATUS_INVALID_PARAMETER, 8, { -1 } },
935 { "1.1.1.0x", STATUS_INVALID_PARAMETER, 8, { -1 } },
936 { "1.1.1.0xff", STATUS_SUCCESS, 10, { 1, 1, 1, 255 }, strict_diff_4,
937 STATUS_INVALID_PARAMETER, 8, { -1 } },
938 { "1.1.1.0x100", STATUS_INVALID_PARAMETER, 11, { -1 }, strict_diff_4,
939 STATUS_INVALID_PARAMETER, 8, { -1 } },
940 { "1.1.1.0xffffffff", STATUS_INVALID_PARAMETER, 16, { -1 }, strict_diff_4,
941 STATUS_INVALID_PARAMETER, 8, { -1 } },
942 { "1.1.1.0x100000000", STATUS_INVALID_PARAMETER, 16, { -1, 0, 0, 0 }, strict_diff_4,
943 STATUS_INVALID_PARAMETER, 8, { -1 } },
944 { "1.1.1.010", STATUS_SUCCESS, 9, { 1, 1, 1, 8 }, strict_diff_4,
945 STATUS_INVALID_PARAMETER, 7, { -1 } },
946 { "1.1.1.00", STATUS_SUCCESS, 8, { 1, 1, 1, 0 }, strict_diff_4,
947 STATUS_INVALID_PARAMETER, 7, { -1 } },
948 { "1.1.1.007", STATUS_SUCCESS, 9, { 1, 1, 1, 7 }, strict_diff_4,
949 STATUS_INVALID_PARAMETER, 7, { -1 } },
950 { "1.1.1.08", STATUS_INVALID_PARAMETER, 7, { -1 } },
951 { "1.1.1.008", STATUS_SUCCESS, 8, { 1, 1, 1, 0 }, strict_diff_4 | ex_fail_4,
952 STATUS_INVALID_PARAMETER, 7, { -1 } },
953 { "1.1.1.0a", STATUS_SUCCESS, 7, { 1, 1, 1, 0 }, ex_fail_4 },
954 { "1.1.1.0o10", STATUS_SUCCESS, 7, { 1, 1, 1, 0 }, ex_fail_4 },
955 { "1.1.1.0b10", STATUS_SUCCESS, 7, { 1, 1, 1, 0 }, ex_fail_4 },
956 { "1.1.1.-2", STATUS_INVALID_PARAMETER, 6, { -1 } },
957 { "1", STATUS_SUCCESS, 1, { 0, 0, 0, 1 }, strict_diff_4,
958 STATUS_INVALID_PARAMETER, 1, { -1 } },
959 { "-1", STATUS_INVALID_PARAMETER, 0, { -1 } },
960 { "1.2", STATUS_SUCCESS, 3, { 1, 0, 0, 2 }, strict_diff_4,
961 STATUS_INVALID_PARAMETER, 3, { -1 } },
962 { "1000.2000", STATUS_INVALID_PARAMETER, 9, { -1 } },
963 { "1.2.", STATUS_INVALID_PARAMETER, 4, { -1 } },
964 { "1..2", STATUS_INVALID_PARAMETER, 3, { -1 } },
965 { "1...2", STATUS_INVALID_PARAMETER, 3, { -1 } },
966 { "1.2.3", STATUS_SUCCESS, 5, { 1, 2, 0, 3 }, strict_diff_4,
967 STATUS_INVALID_PARAMETER, 5, { -1 } },
968 { "1.2.3.", STATUS_INVALID_PARAMETER, 6, { -1 } },
969 { "203569230", STATUS_SUCCESS, 9, { 12, 34, 56, 78 }, strict_diff_4,
970 STATUS_INVALID_PARAMETER, 9, { -1 } },
971 { "1.223756", STATUS_SUCCESS, 8, { 1, 3, 106, 12 }, strict_diff_4,
972 STATUS_INVALID_PARAMETER, 8, { -1 } },
973 { "3.4.756", STATUS_SUCCESS, 7, { 3, 4, 2, 244 }, strict_diff_4,
974 STATUS_INVALID_PARAMETER, 7, { -1 } },
975 { "756.3.4", STATUS_INVALID_PARAMETER, 7, { -1 } },
976 { "3.756.4", STATUS_INVALID_PARAMETER, 7, { -1 } },
977 { "3.4.756.1", STATUS_INVALID_PARAMETER, 9, { -1 } },
978 { "3.4.65536", STATUS_INVALID_PARAMETER, 9, { -1 } },
979 { "3.4.5.6.7", STATUS_INVALID_PARAMETER, 7, { -1 } },
980 { "3.4.5.+6", STATUS_INVALID_PARAMETER, 6, { -1 } },
981 { " 3.4.5.6", STATUS_INVALID_PARAMETER, 0, { -1 } },
982 { "\t3.4.5.6", STATUS_INVALID_PARAMETER, 0, { -1 } },
983 { "3.4.5.6 ", STATUS_SUCCESS, 7, { 3, 4, 5, 6 }, ex_fail_4 },
984 { "3. 4.5.6", STATUS_INVALID_PARAMETER, 2, { -1 } },
985 { ".", STATUS_INVALID_PARAMETER, 1, { -1 } },
986 { "..", STATUS_INVALID_PARAMETER, 1, { -1 } },
987 { "1.", STATUS_INVALID_PARAMETER, 2, { -1 } },
988 { "1..", STATUS_INVALID_PARAMETER, 3, { -1 } },
989 { ".1", STATUS_INVALID_PARAMETER, 1, { -1 } },
990 { ".1.", STATUS_INVALID_PARAMETER, 1, { -1 } },
991 { ".1.2.3", STATUS_INVALID_PARAMETER, 1, { -1 } },
992 { ".1.2.3.4", STATUS_INVALID_PARAMETER, 1, { -1 } },
993 { "0.1.2.3", STATUS_SUCCESS, 7, { 0, 1, 2, 3 } },
994 { "0.1.2.3.", STATUS_INVALID_PARAMETER, 7, { -1 } },
995 { "[0.1.2.3]", STATUS_INVALID_PARAMETER, 0, { -1 } },
996 { "0x00010203", STATUS_SUCCESS, 10, { 0, 1, 2, 3 }, strict_diff_4,
997 STATUS_INVALID_PARAMETER, 2, { -1 } },
998 { "0X00010203", STATUS_SUCCESS, 10, { 0, 1, 2, 3 }, strict_diff_4,
999 STATUS_INVALID_PARAMETER, 2, { -1 } },
1000 { "0x1234", STATUS_SUCCESS, 6, { 0, 0, 18, 52 }, strict_diff_4,
1001 STATUS_INVALID_PARAMETER, 2, { -1 } },
1002 { "0x123456789", STATUS_SUCCESS, 11, { 35, 69, 103, 137 }, strict_diff_4,
1003 STATUS_INVALID_PARAMETER, 2, { -1 } },
1004 { "0x00010Q03", STATUS_SUCCESS, 7, { 0, 0, 0, 16 }, strict_diff_4 | ex_fail_4,
1005 STATUS_INVALID_PARAMETER, 2, { -1 } },
1006 { "x00010203", STATUS_INVALID_PARAMETER, 0, { -1 } },
1007 { "1234BEEF", STATUS_SUCCESS, 4, { 0, 0, 4, 210 }, strict_diff_4 | ex_fail_4,
1008 STATUS_INVALID_PARAMETER, 4, { -1 } },
1009 { "017700000001", STATUS_SUCCESS, 12, { 127, 0, 0, 1 }, strict_diff_4,
1010 STATUS_INVALID_PARAMETER, 1, { -1 } },
1011 { "0777", STATUS_SUCCESS, 4, { 0, 0, 1, 255 }, strict_diff_4,
1012 STATUS_INVALID_PARAMETER, 1, { -1 } },
1013 { "::1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1014 { ":1", STATUS_INVALID_PARAMETER, 0, { -1 } },
1017 static void init_ip4(IN_ADDR* addr, const int src[4])
1019 if (!src || src[0] == -1)
1021 addr->S_un.S_addr = 0xabababab;
1023 else
1025 addr->S_un.S_un_b.s_b1 = src[0];
1026 addr->S_un.S_un_b.s_b2 = src[1];
1027 addr->S_un.S_un_b.s_b3 = src[2];
1028 addr->S_un.S_un_b.s_b4 = src[3];
1032 static void test_RtlIpv4StringToAddress(void)
1034 NTSTATUS res;
1035 IN_ADDR ip, expected_ip;
1036 PCSTR terminator;
1037 CHAR dummy;
1038 int i;
1040 if (0)
1042 /* leaving either parameter NULL crashes on Windows */
1043 res = RtlIpv4StringToAddressA(NULL, FALSE, &terminator, &ip);
1044 res = RtlIpv4StringToAddressA("1.1.1.1", FALSE, NULL, &ip);
1045 res = RtlIpv4StringToAddressA("1.1.1.1", FALSE, &terminator, NULL);
1046 /* same for the wide char version */
1048 res = RtlIpv4StringToAddressW(NULL, FALSE, &terminatorW, &ip);
1049 res = RtlIpv4StringToAddressW(L"1.1.1.1", FALSE, NULL, &ip);
1050 res = RtlIpv4StringToAddressW(L"1.1.1.1", FALSE, &terminatorW, NULL);
1054 for (i = 0; i < ARRAY_SIZE(ipv4_tests); i++)
1056 /* non-strict */
1057 terminator = &dummy;
1058 ip.S_un.S_addr = 0xabababab;
1059 res = RtlIpv4StringToAddressA(ipv4_tests[i].address, FALSE, &terminator, &ip);
1060 ok(res == ipv4_tests[i].res,
1061 "[%s] res = 0x%08lx, expected 0x%08lx\n",
1062 ipv4_tests[i].address, res, ipv4_tests[i].res);
1063 ok(terminator == ipv4_tests[i].address + ipv4_tests[i].terminator_offset,
1064 "[%s] terminator = %p, expected %p\n",
1065 ipv4_tests[i].address, terminator, ipv4_tests[i].address + ipv4_tests[i].terminator_offset);
1067 init_ip4(&expected_ip, ipv4_tests[i].ip);
1068 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr,
1069 "[%s] ip = %08lx, expected %08lx\n",
1070 ipv4_tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1072 if (!(ipv4_tests[i].flags & strict_diff_4))
1074 ipv4_tests[i].res_strict = ipv4_tests[i].res;
1075 ipv4_tests[i].terminator_offset_strict = ipv4_tests[i].terminator_offset;
1076 ipv4_tests[i].ip_strict[0] = ipv4_tests[i].ip[0];
1077 ipv4_tests[i].ip_strict[1] = ipv4_tests[i].ip[1];
1078 ipv4_tests[i].ip_strict[2] = ipv4_tests[i].ip[2];
1079 ipv4_tests[i].ip_strict[3] = ipv4_tests[i].ip[3];
1081 /* strict */
1082 terminator = &dummy;
1083 ip.S_un.S_addr = 0xabababab;
1084 res = RtlIpv4StringToAddressA(ipv4_tests[i].address, TRUE, &terminator, &ip);
1085 ok(res == ipv4_tests[i].res_strict,
1086 "[%s] res = 0x%08lx, expected 0x%08lx\n",
1087 ipv4_tests[i].address, res, ipv4_tests[i].res_strict);
1088 ok(terminator == ipv4_tests[i].address + ipv4_tests[i].terminator_offset_strict,
1089 "[%s] terminator = %p, expected %p\n",
1090 ipv4_tests[i].address, terminator, ipv4_tests[i].address + ipv4_tests[i].terminator_offset_strict);
1092 init_ip4(&expected_ip, ipv4_tests[i].ip_strict);
1093 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr,
1094 "[%s] ip = %08lx, expected %08lx\n",
1095 ipv4_tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1099 static void test_RtlIpv4StringToAddressEx(void)
1101 NTSTATUS res;
1102 IN_ADDR ip, expected_ip;
1103 USHORT port;
1104 static const struct
1106 PCSTR address;
1107 NTSTATUS res;
1108 int ip[4];
1109 USHORT port;
1110 } ipv4_ex_tests[] =
1112 { "", STATUS_INVALID_PARAMETER, { -1 }, 0xdead },
1113 { " ", STATUS_INVALID_PARAMETER, { -1 }, 0xdead },
1114 { "1.1.1.1:", STATUS_INVALID_PARAMETER, { 1, 1, 1, 1 }, 0xdead },
1115 { "1.1.1.1+", STATUS_INVALID_PARAMETER, { 1, 1, 1, 1 }, 0xdead },
1116 { "1.1.1.1:1", STATUS_SUCCESS, { 1, 1, 1, 1 }, 0x100 },
1117 { "256.1.1.1:1", STATUS_INVALID_PARAMETER, { -1 }, 0xdead },
1118 { "-1.1.1.1:1", STATUS_INVALID_PARAMETER, { -1 }, 0xdead },
1119 { "0.0.0.0:0", STATUS_INVALID_PARAMETER, { 0, 0, 0, 0 }, 0xdead },
1120 { "0.0.0.0:1", STATUS_SUCCESS, { 0, 0, 0, 0 }, 0x100 },
1121 { "1.2.3.4:65535", STATUS_SUCCESS, { 1, 2, 3, 4 }, 65535 },
1122 { "1.2.3.4:65536", STATUS_INVALID_PARAMETER, { 1, 2, 3, 4 }, 0xdead },
1123 { "1.2.3.4:0xffff", STATUS_SUCCESS, { 1, 2, 3, 4 }, 65535 },
1124 { "1.2.3.4:0XfFfF", STATUS_SUCCESS, { 1, 2, 3, 4 }, 65535 },
1125 { "1.2.3.4:011064", STATUS_SUCCESS, { 1, 2, 3, 4 }, 0x3412 },
1126 { "1.2.3.4:1234a", STATUS_INVALID_PARAMETER, { 1, 2, 3, 4 }, 0xdead },
1127 { "1.2.3.4:1234+", STATUS_INVALID_PARAMETER, { 1, 2, 3, 4 }, 0xdead },
1128 { "1.2.3.4: 1234", STATUS_INVALID_PARAMETER, { 1, 2, 3, 4 }, 0xdead },
1129 { "1.2.3.4:\t1234", STATUS_INVALID_PARAMETER, { 1, 2, 3, 4 }, 0xdead },
1131 unsigned int i;
1132 BOOLEAN strict;
1134 if (!pRtlIpv4StringToAddressExA)
1136 skip("RtlIpv4StringToAddressEx not available\n");
1137 return;
1140 /* do not crash, and do not touch the ip / port. */
1141 ip.S_un.S_addr = 0xabababab;
1142 port = 0xdead;
1143 res = pRtlIpv4StringToAddressExA(NULL, FALSE, &ip, &port);
1144 ok(res == STATUS_INVALID_PARAMETER, "[null address] res = 0x%08lx, expected 0x%08lx\n",
1145 res, STATUS_INVALID_PARAMETER);
1146 ok(ip.S_un.S_addr == 0xabababab, "RtlIpv4StringToAddressExA should not touch the ip!, ip == %lx\n", ip.S_un.S_addr);
1147 ok(port == 0xdead, "RtlIpv4StringToAddressExA should not touch the port!, port == %x\n", port);
1149 port = 0xdead;
1150 res = pRtlIpv4StringToAddressExA("1.1.1.1", FALSE, NULL, &port);
1151 ok(res == STATUS_INVALID_PARAMETER, "[null ip] res = 0x%08lx, expected 0x%08lx\n",
1152 res, STATUS_INVALID_PARAMETER);
1153 ok(port == 0xdead, "RtlIpv4StringToAddressExA should not touch the port!, port == %x\n", port);
1155 ip.S_un.S_addr = 0xabababab;
1156 port = 0xdead;
1157 res = pRtlIpv4StringToAddressExA("1.1.1.1", FALSE, &ip, NULL);
1158 ok(res == STATUS_INVALID_PARAMETER, "[null port] res = 0x%08lx, expected 0x%08lx\n",
1159 res, STATUS_INVALID_PARAMETER);
1160 ok(ip.S_un.S_addr == 0xabababab, "RtlIpv4StringToAddressExA should not touch the ip!, ip == %lx\n", ip.S_un.S_addr);
1161 ok(port == 0xdead, "RtlIpv4StringToAddressExA should not touch the port!, port == %x\n", port);
1163 /* first we run the non-ex testcases on the ex function */
1164 for (i = 0; i < ARRAY_SIZE(ipv4_tests); i++)
1166 NTSTATUS expect_res = (ipv4_tests[i].flags & ex_fail_4) ? STATUS_INVALID_PARAMETER : ipv4_tests[i].res;
1168 /* non-strict */
1169 port = 0xdead;
1170 ip.S_un.S_addr = 0xabababab;
1171 res = pRtlIpv4StringToAddressExA(ipv4_tests[i].address, FALSE, &ip, &port);
1172 ok(res == expect_res, "[%s] res = 0x%08lx, expected 0x%08lx\n",
1173 ipv4_tests[i].address, res, expect_res);
1175 init_ip4(&expected_ip, ipv4_tests[i].ip);
1176 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr, "[%s] ip = %08lx, expected %08lx\n",
1177 ipv4_tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1179 if (!(ipv4_tests[i].flags & strict_diff_4))
1181 ipv4_tests[i].res_strict = ipv4_tests[i].res;
1182 ipv4_tests[i].terminator_offset_strict = ipv4_tests[i].terminator_offset;
1183 ipv4_tests[i].ip_strict[0] = ipv4_tests[i].ip[0];
1184 ipv4_tests[i].ip_strict[1] = ipv4_tests[i].ip[1];
1185 ipv4_tests[i].ip_strict[2] = ipv4_tests[i].ip[2];
1186 ipv4_tests[i].ip_strict[3] = ipv4_tests[i].ip[3];
1188 /* strict */
1189 expect_res = (ipv4_tests[i].flags & ex_fail_4) ? STATUS_INVALID_PARAMETER : ipv4_tests[i].res_strict;
1190 port = 0xdead;
1191 ip.S_un.S_addr = 0xabababab;
1192 res = pRtlIpv4StringToAddressExA(ipv4_tests[i].address, TRUE, &ip, &port);
1193 ok(res == expect_res, "[%s] res = 0x%08lx, expected 0x%08lx\n",
1194 ipv4_tests[i].address, res, expect_res);
1196 init_ip4(&expected_ip, ipv4_tests[i].ip_strict);
1197 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr, "[%s] ip = %08lx, expected %08lx\n",
1198 ipv4_tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1202 for (i = 0; i < ARRAY_SIZE(ipv4_ex_tests); i++)
1204 /* Strict is only relevant for the ip address, so make sure that it does not influence the port */
1205 for (strict = 0; strict < 2; strict++)
1207 ip.S_un.S_addr = 0xabababab;
1208 port = 0xdead;
1209 res = pRtlIpv4StringToAddressExA(ipv4_ex_tests[i].address, strict, &ip, &port);
1210 ok(res == ipv4_ex_tests[i].res, "[%s] res = 0x%08lx, expected 0x%08lx\n",
1211 ipv4_ex_tests[i].address, res, ipv4_ex_tests[i].res);
1213 init_ip4(&expected_ip, ipv4_ex_tests[i].ip);
1214 ok(ip.S_un.S_addr == expected_ip.S_un.S_addr, "[%s] ip = %08lx, expected %08lx\n",
1215 ipv4_ex_tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr);
1216 ok(port == ipv4_ex_tests[i].port, "[%s] port = %u, expected %u\n",
1217 ipv4_ex_tests[i].address, port, ipv4_ex_tests[i].port);
1222 /* ipv6 addresses based on the set from https://github.com/beaugunderson/javascript-ipv6/tree/master/test/data */
1223 static const struct
1225 PCSTR address;
1226 NTSTATUS res;
1227 int terminator_offset;
1228 int ip[8];
1229 /* win_broken: XP and Vista do not handle this correctly
1230 ex_fail: Ex function does need the string to be terminated, non-Ex does not.
1231 ex_skip: test doesn't make sense for Ex (f.e. it's invalid for non-Ex but valid for Ex) */
1232 enum { normal_6, win_broken_6 = 1, ex_fail_6 = 2, ex_skip_6 = 4, win_extra_zero = 8 } flags;
1233 } ipv6_tests[] =
1235 { "0000:0000:0000:0000:0000:0000:0000:0000", STATUS_SUCCESS, 39,
1236 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1237 { "0000:0000:0000:0000:0000:0000:0000:0001", STATUS_SUCCESS, 39,
1238 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1239 { "0:0:0:0:0:0:0:0", STATUS_SUCCESS, 15,
1240 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1241 { "0:0:0:0:0:0:0:1", STATUS_SUCCESS, 15,
1242 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1243 { "0:0:0:0:0:0:0::", STATUS_SUCCESS, 15,
1244 { 0, 0, 0, 0, 0, 0, 0, 0 }, win_broken_6 },
1245 { "0:0:0:0:0:0:13.1.68.3", STATUS_SUCCESS, 21,
1246 { 0, 0, 0, 0, 0, 0, 0x10d, 0x344 } },
1247 { "0:0:0:0:0:0::", STATUS_SUCCESS, 13,
1248 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1249 { "0:0:0:0:0::", STATUS_SUCCESS, 11,
1250 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1251 { "0:0:0:0:0:FFFF:129.144.52.38", STATUS_SUCCESS, 28,
1252 { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
1253 { "0::", STATUS_SUCCESS, 3,
1254 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1255 { "0:1:2:3:4:5:6:7", STATUS_SUCCESS, 15,
1256 { 0, 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700 } },
1257 { "1080:0:0:0:8:800:200c:417a", STATUS_SUCCESS, 26,
1258 { 0x8010, 0, 0, 0, 0x800, 0x8, 0x0c20, 0x7a41 } },
1259 { "0:a:b:c:d:e:f::", STATUS_SUCCESS, 15,
1260 { 0, 0xa00, 0xb00, 0xc00, 0xd00, 0xe00, 0xf00, 0 }, win_broken_6 },
1261 { "1111:2222:3333:4444:5555:6666:123.123.123.123", STATUS_SUCCESS, 45,
1262 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1263 { "1111:2222:3333:4444:5555:6666:7777:8888", STATUS_SUCCESS, 39,
1264 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 } },
1265 { "1111:2222:3333:4444:0x5555:6666:7777:8888", STATUS_INVALID_PARAMETER, 21,
1266 { 0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab } },
1267 { "1111:2222:3333:4444:x555:6666:7777:8888", STATUS_INVALID_PARAMETER, 20,
1268 { 0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab } },
1269 { "1111:2222:3333:4444:0r5555:6666:7777:8888", STATUS_INVALID_PARAMETER, 21,
1270 { 0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab } },
1271 { "1111:2222:3333:4444:r5555:6666:7777:8888", STATUS_INVALID_PARAMETER, 20,
1272 { 0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab } },
1273 { "1111:2222:3333:4444:5555:6666:7777::", STATUS_SUCCESS, 36,
1274 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0 }, win_broken_6 },
1275 { "1111:2222:3333:4444:5555:6666::", STATUS_SUCCESS, 31,
1276 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0, 0 } },
1277 { "1111:2222:3333:4444:5555:6666::8888", STATUS_SUCCESS, 35,
1278 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0, 0x8888 } },
1279 { "1111:2222:3333:4444:5555:6666::7777:8888", STATUS_SUCCESS, 35,
1280 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0, 0x7777 }, ex_fail_6 },
1281 { "1111:2222:3333:4444:5555:6666:7777::8888", STATUS_SUCCESS, 36,
1282 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0 }, ex_fail_6|win_broken_6 },
1283 { "1111:2222:3333:4444:5555::", STATUS_SUCCESS, 26,
1284 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0 } },
1285 { "1111:2222:3333:4444:5555::123.123.123.123", STATUS_SUCCESS, 41,
1286 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0x7b7b, 0x7b7b } },
1287 { "1111:2222:3333:4444:5555::0x1.123.123.123", STATUS_SUCCESS, 27,
1288 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x100 }, ex_fail_6 },
1289 { "1111:2222:3333:4444:5555::0x88", STATUS_SUCCESS, 27,
1290 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8800 }, ex_fail_6 },
1291 { "1111:2222:3333:4444:5555::0X88", STATUS_SUCCESS, 27,
1292 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8800 }, ex_fail_6 },
1293 { "1111:2222:3333:4444:5555::0X", STATUS_SUCCESS, 27,
1294 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0 }, ex_fail_6 },
1295 { "1111:2222:3333:4444:5555::0X88:7777", STATUS_SUCCESS, 27,
1296 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8800 }, ex_fail_6 },
1297 { "1111:2222:3333:4444:5555::0x8888", STATUS_SUCCESS, 27,
1298 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8888 }, ex_fail_6 },
1299 { "1111:2222:3333:4444:5555::0x80000000", STATUS_SUCCESS, 27,
1300 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0xffff }, ex_fail_6 },
1301 { "1111:2222:3333:4444::5555:0x012345678", STATUS_SUCCESS, 27,
1302 { 0x1111, 0x2222, 0x3333, 0x4444, 0, 0, 0x5555, 0x7856 }, ex_fail_6 },
1303 { "1111:2222:3333:4444::5555:0x123456789", STATUS_SUCCESS, 27,
1304 { 0x1111, 0x2222, 0x3333, 0x4444, 0, 0, 0x5555, 0xffff }, ex_fail_6 },
1305 { "1111:2222:3333:4444:5555:6666:0x12345678", STATUS_INVALID_PARAMETER, 31,
1306 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0xabab, 0xabab }, ex_fail_6 },
1307 { "1111:2222:3333:4444:5555:6666:7777:0x80000000", STATUS_SUCCESS, 36,
1308 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0xffff }, ex_fail_6 },
1309 { "1111:2222:3333:4444:5555:6666:7777:0x012345678", STATUS_SUCCESS, 36,
1310 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x7856 }, ex_fail_6 },
1311 { "1111:2222:3333:4444:5555:6666:7777:0x123456789", STATUS_SUCCESS, 36,
1312 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0xffff }, ex_fail_6 },
1313 { "111:222:333:444:555:666:777:0x123456789abcdef0", STATUS_SUCCESS, 29,
1314 { 0x1101, 0x2202, 0x3303, 0x4404, 0x5505, 0x6606, 0x7707, 0xffff }, ex_fail_6 },
1315 { "1111:2222:3333:4444:5555::08888", STATUS_INVALID_PARAMETER, 31,
1316 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0xabab, 0xabab, 0xabab } },
1317 { "1111:2222:3333:4444:5555::08888::", STATUS_INVALID_PARAMETER, 31,
1318 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0xabab, 0xabab, 0xabab } },
1319 { "1111:2222:3333:4444:5555:6666:7777:fffff:", STATUS_INVALID_PARAMETER, 40,
1320 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0xabab } },
1321 { "1111:2222:3333:4444:5555:6666::fffff:", STATUS_INVALID_PARAMETER, 36,
1322 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0xabab, 0xabab } },
1323 { "1111:2222:3333:4444:5555::fffff", STATUS_INVALID_PARAMETER, 31,
1324 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0xabab, 0xabab, 0xabab } },
1325 { "1111:2222:3333:4444::fffff", STATUS_INVALID_PARAMETER, 26,
1326 { 0x1111, 0x2222, 0x3333, 0x4444, 0xabab, 0xabab, 0xabab, 0xabab } },
1327 { "1111:2222:3333::fffff", STATUS_INVALID_PARAMETER, 21,
1328 { 0x1111, 0x2222, 0x3333, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1329 { "1111:2222:3333:4444:5555::7777:8888", STATUS_SUCCESS, 35,
1330 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0x7777, 0x8888 } },
1331 { "1111:2222:3333:4444:5555::8888", STATUS_SUCCESS, 30,
1332 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8888 } },
1333 { "1111::", STATUS_SUCCESS, 6,
1334 { 0x1111, 0, 0, 0, 0, 0, 0, 0 } },
1335 { "1111::123.123.123.123", STATUS_SUCCESS, 21,
1336 { 0x1111, 0, 0, 0, 0, 0, 0x7b7b, 0x7b7b } },
1337 { "1111::3333:4444:5555:6666:123.123.123.123", STATUS_SUCCESS, 41,
1338 { 0x1111, 0, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1339 { "1111::3333:4444:5555:6666:7777:8888", STATUS_SUCCESS, 35,
1340 { 0x1111, 0, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 } },
1341 { "1111::4444:5555:6666:123.123.123.123", STATUS_SUCCESS, 36,
1342 { 0x1111, 0, 0, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1343 { "1111::4444:5555:6666:7777:8888", STATUS_SUCCESS, 30,
1344 { 0x1111, 0, 0, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 } },
1345 { "1111::5555:6666:123.123.123.123", STATUS_SUCCESS, 31,
1346 { 0x1111, 0, 0, 0, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1347 { "1111::5555:6666:7777:8888", STATUS_SUCCESS, 25,
1348 { 0x1111, 0, 0, 0, 0x5555, 0x6666, 0x7777, 0x8888 } },
1349 { "1111::6666:123.123.123.123", STATUS_SUCCESS, 26,
1350 { 0x1111, 0, 0, 0, 0, 0x6666, 0x7b7b, 0x7b7b } },
1351 { "1111::6666:7777:8888", STATUS_SUCCESS, 20,
1352 { 0x1111, 0, 0, 0, 0, 0x6666, 0x7777, 0x8888 } },
1353 { "1111::7777:8888", STATUS_SUCCESS, 15,
1354 { 0x1111, 0, 0, 0, 0, 0, 0x7777, 0x8888 } },
1355 { "1111::8888", STATUS_SUCCESS, 10,
1356 { 0x1111, 0, 0, 0, 0, 0, 0, 0x8888 } },
1357 { "1:2:3:4:5:6:1.2.3.4", STATUS_SUCCESS, 19,
1358 { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x201, 0x403 } },
1359 { "1:2:3:4:5:6:7:8", STATUS_SUCCESS, 15,
1360 { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700, 0x800 } },
1361 { "1:2:3:4:5:6::", STATUS_SUCCESS, 13,
1362 { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0, 0 } },
1363 { "1:2:3:4:5:6::8", STATUS_SUCCESS, 14,
1364 { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0, 0x800 } },
1365 { "2001:0000:1234:0000:0000:C1C0:ABCD:0876", STATUS_SUCCESS, 39,
1366 { 0x120, 0, 0x3412, 0, 0, 0xc0c1, 0xcdab, 0x7608 } },
1367 { "2001:0000:4136:e378:8000:63bf:3fff:fdd2", STATUS_SUCCESS, 39,
1368 { 0x120, 0, 0x3641, 0x78e3, 0x80, 0xbf63, 0xff3f, 0xd2fd } },
1369 { "2001:0db8:0:0:0:0:1428:57ab", STATUS_SUCCESS, 27,
1370 { 0x120, 0xb80d, 0, 0, 0, 0, 0x2814, 0xab57 } },
1371 { "2001:0db8:1234:ffff:ffff:ffff:ffff:ffff", STATUS_SUCCESS, 39,
1372 { 0x120, 0xb80d, 0x3412, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff } },
1373 { "2001::CE49:7601:2CAD:DFFF:7C94:FFFE", STATUS_SUCCESS, 35,
1374 { 0x120, 0, 0x49ce, 0x176, 0xad2c, 0xffdf, 0x947c, 0xfeff } },
1375 { "2001:db8:85a3::8a2e:370:7334", STATUS_SUCCESS, 28,
1376 { 0x120, 0xb80d, 0xa385, 0, 0, 0x2e8a, 0x7003, 0x3473 } },
1377 { "3ffe:0b00:0000:0000:0001:0000:0000:000a", STATUS_SUCCESS, 39,
1378 { 0xfe3f, 0xb, 0, 0, 0x100, 0, 0, 0xa00 } },
1379 { "::", STATUS_SUCCESS, 2,
1380 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1381 { "::%16", STATUS_SUCCESS, 2,
1382 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1383 { "::/16", STATUS_SUCCESS, 2,
1384 { 0, 0, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1385 { "::01234", STATUS_INVALID_PARAMETER, 7,
1386 { 0, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1387 { "::0", STATUS_SUCCESS, 3,
1388 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1389 { "::0:0", STATUS_SUCCESS, 5,
1390 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1391 { "::0:0:0", STATUS_SUCCESS, 7,
1392 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1393 { "::0:0:0:0", STATUS_SUCCESS, 9,
1394 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1395 { "::0:0:0:0:0", STATUS_SUCCESS, 11,
1396 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1397 { "::0:0:0:0:0:0", STATUS_SUCCESS, 13,
1398 { 0, 0, 0, 0, 0, 0, 0, 0 } },
1399 /* this one and the next one are incorrectly parsed before Windows 11,
1400 it adds one zero too many in front, cutting off the last digit. */
1401 { "::0:0:0:0:0:0:0", STATUS_SUCCESS, 15,
1402 { 0, 0, 0, 0, 0, 0, 0, 0 }, win_broken_6|win_extra_zero },
1403 { "::0:a:b:c:d:e:f", STATUS_SUCCESS, 15,
1404 { 0, 0, 0xa00, 0xb00, 0xc00, 0xd00, 0xe00, 0xf00 }, win_broken_6|win_extra_zero },
1405 { "::123.123.123.123", STATUS_SUCCESS, 17,
1406 { 0, 0, 0, 0, 0, 0, 0x7b7b, 0x7b7b } },
1407 { "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", STATUS_SUCCESS, 39,
1408 { 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff } },
1410 { "':10.0.0.1", STATUS_INVALID_PARAMETER, 0,
1411 { -1 } },
1412 { "-1", STATUS_INVALID_PARAMETER, 0,
1413 { -1 } },
1414 { "02001:0000:1234:0000:0000:C1C0:ABCD:0876", STATUS_INVALID_PARAMETER, -1,
1415 { -1 } },
1416 { "2001:00000:1234:0000:0000:C1C0:ABCD:0876", STATUS_INVALID_PARAMETER, -1,
1417 { 0x120, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1418 { "2001:0000:01234:0000:0000:C1C0:ABCD:0876", STATUS_INVALID_PARAMETER, -1,
1419 { 0x120, 0, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1420 { "2001:0000::01234.0", STATUS_INVALID_PARAMETER, -1,
1421 { 0x120, 0, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1422 { "2001:0::b.0", STATUS_SUCCESS, 9,
1423 { 0x120, 0, 0, 0, 0, 0, 0, 0xb00 }, ex_fail_6 },
1424 { "2001::0:b.0", STATUS_SUCCESS, 9,
1425 { 0x120, 0, 0, 0, 0, 0, 0, 0xb00 }, ex_fail_6 },
1426 { "1.2.3.4", STATUS_INVALID_PARAMETER, 7,
1427 { 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1428 { "1.2.3.4:1111::5555", STATUS_INVALID_PARAMETER, 7,
1429 { 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1430 { "1.2.3.4::5555", STATUS_INVALID_PARAMETER, 7,
1431 { 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1432 { "11112222:3333:4444:5555:6666:1.2.3.4", STATUS_INVALID_PARAMETER, -1,
1433 { -1 } },
1434 { "11112222:3333:4444:5555:6666:7777:8888", STATUS_INVALID_PARAMETER, -1,
1435 { -1 } },
1436 { "1111", STATUS_INVALID_PARAMETER, 4,
1437 { -1 } },
1438 { "0x1111", STATUS_INVALID_PARAMETER, 1,
1439 { -1 } },
1440 { "1111:22223333:4444:5555:6666:1.2.3.4", STATUS_INVALID_PARAMETER, -1,
1441 { 0x1111, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1442 { "1111:22223333:4444:5555:6666:7777:8888", STATUS_INVALID_PARAMETER, -1,
1443 { 0x1111, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1444 { "1111:123456789:4444:5555:6666:7777:8888", STATUS_INVALID_PARAMETER, -1,
1445 { 0x1111, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1446 { "1111:1234567890abcdef0:4444:5555:6666:7777:888", STATUS_INVALID_PARAMETER, -1,
1447 { 0x1111, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1448 { "1111:2222:", STATUS_INVALID_PARAMETER, 10,
1449 { 0x1111, 0x2222, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1450 { "1111:2222:1.2.3.4", STATUS_INVALID_PARAMETER, 17,
1451 { 0x1111, 0x2222, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab } },
1452 { "1111:2222:3333", STATUS_INVALID_PARAMETER, 14,
1453 { 0x1111, 0x2222, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1454 { "1111:2222:3333:4444:5555:6666::1.2.3.4", STATUS_SUCCESS, 32,
1455 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0, 0x100 }, ex_fail_6 },
1456 { "1111:2222:3333:4444:5555:6666:7777:1.2.3.4", STATUS_SUCCESS, 36,
1457 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x100 }, ex_fail_6 },
1458 { "1111:2222:3333:4444:5555:6666:7777:8888:", STATUS_SUCCESS, 39,
1459 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 }, ex_fail_6 },
1460 { "1111:2222:3333:4444:5555:6666:7777:8888:1.2.3.4",STATUS_SUCCESS, 39,
1461 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 }, ex_fail_6 },
1462 { "1111:2222:3333:4444:5555:6666:7777:8888:9999", STATUS_SUCCESS, 39,
1463 { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 }, ex_fail_6 },
1464 { "1111:2222:::", STATUS_SUCCESS, 11,
1465 { 0x1111, 0x2222, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1466 { "1111::5555:", STATUS_INVALID_PARAMETER, 11,
1467 { 0x1111, 0x5555, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1468 { "1111::3333:4444:5555:6666:7777::", STATUS_SUCCESS, 30,
1469 { 0x1111, 0, 0, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777 }, ex_fail_6 },
1470 { "1111:2222:::4444:5555:6666:1.2.3.4", STATUS_SUCCESS, 11,
1471 { 0x1111, 0x2222, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1472 { "1111::3333::5555:6666:1.2.3.4", STATUS_SUCCESS, 10,
1473 { 0x1111, 0, 0, 0, 0, 0, 0, 0x3333 }, ex_fail_6 },
1474 { "12345::6:7:8", STATUS_INVALID_PARAMETER, -1,
1475 { -1 } },
1476 { "1::001.2.3.4", STATUS_SUCCESS, 12,
1477 { 0x100, 0, 0, 0, 0, 0, 0x201, 0x403 } },
1478 { "1::1.002.3.4", STATUS_SUCCESS, 12,
1479 { 0x100, 0, 0, 0, 0, 0, 0x201, 0x403 } },
1480 { "1::0001.2.3.4", STATUS_INVALID_PARAMETER, -1,
1481 { 0x100, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1482 { "1::1.0002.3.4", STATUS_INVALID_PARAMETER, -1,
1483 { 0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1484 { "1::1.2.256.4", STATUS_INVALID_PARAMETER, -1,
1485 { 0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1486 { "1::1.2.4294967296.4", STATUS_INVALID_PARAMETER, -1,
1487 { 0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1488 { "1::1.2.18446744073709551616.4", STATUS_INVALID_PARAMETER, -1,
1489 { 0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1490 { "1::1.2.3.256", STATUS_INVALID_PARAMETER, 12,
1491 { 0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1492 { "1::1.2.3.4294967296", STATUS_INVALID_PARAMETER, 19,
1493 { 0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1494 { "1::1.2.3.18446744073709551616", STATUS_INVALID_PARAMETER, 29,
1495 { 0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1496 { "1::1.2.3.300", STATUS_INVALID_PARAMETER, 12,
1497 { 0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1498 { "1::1.2.3.300.", STATUS_INVALID_PARAMETER, 12,
1499 { 0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1500 { "1::1.2::1", STATUS_INVALID_PARAMETER, 6,
1501 { 0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1502 { "1::1.2.3.4::1", STATUS_SUCCESS, 10,
1503 { 0x100, 0, 0, 0, 0, 0, 0x201, 0x403 }, ex_fail_6 },
1504 { "1::1.", STATUS_INVALID_PARAMETER, 5,
1505 { 0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1506 { "1::1.2", STATUS_INVALID_PARAMETER, 6,
1507 { 0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1508 { "1::1.2.", STATUS_INVALID_PARAMETER, 7,
1509 { 0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1510 { "1::1.2.3", STATUS_INVALID_PARAMETER, 8,
1511 { 0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1512 { "1::1.2.3.", STATUS_INVALID_PARAMETER, 9,
1513 { 0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1514 { "1::1.2.3.4", STATUS_SUCCESS, 10,
1515 { 0x100, 0, 0, 0, 0, 0, 0x201, 0x403 } },
1516 { "1::1.2.3.900", STATUS_INVALID_PARAMETER, 12,
1517 { 0x100, 0x201, 0xab03, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1518 { "1::1.2.300.4", STATUS_INVALID_PARAMETER, -1,
1519 { 0x100, 0x201, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1520 { "1::1.256.3.4", STATUS_INVALID_PARAMETER, -1,
1521 { 0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1522 { "1::1.256:3.4", STATUS_INVALID_PARAMETER, 8,
1523 { 0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1524 { "1::1.2a.3.4", STATUS_INVALID_PARAMETER, 6,
1525 { 0x100, 0xab01, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1526 { "1::256.2.3.4", STATUS_INVALID_PARAMETER, -1,
1527 { 0x100, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1528 { "1::1a.2.3.4", STATUS_SUCCESS, 5,
1529 { 0x100, 0, 0, 0, 0, 0, 0, 0x1a00 }, ex_fail_6 },
1530 { "1::2::3", STATUS_SUCCESS, 4,
1531 { 0x100, 0, 0, 0, 0, 0, 0, 0x200 }, ex_fail_6 },
1532 { "2001:0000:1234: 0000:0000:C1C0:ABCD:0876", STATUS_INVALID_PARAMETER, 15,
1533 { 0x120, 0, 0x3412, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1534 { "2001:0000:1234:0000:0000:C1C0:ABCD:0876 0", STATUS_SUCCESS, 39,
1535 { 0x120, 0, 0x3412, 0, 0, 0xc0c1, 0xcdab, 0x7608 }, ex_fail_6 },
1536 { "2001:1:1:1:1:1:255Z255X255Y255", STATUS_INVALID_PARAMETER, 18,
1537 { 0x120, 0x100, 0x100, 0x100, 0x100, 0x100, 0xabab, 0xabab } },
1538 { "2001::FFD3::57ab", STATUS_SUCCESS, 10,
1539 { 0x120, 0, 0, 0, 0, 0, 0, 0xd3ff }, ex_fail_6 },
1540 { ":", STATUS_INVALID_PARAMETER, 0,
1541 { -1 } },
1542 { ":1111:2222:3333:4444:5555:6666:1.2.3.4", STATUS_INVALID_PARAMETER, 0,
1543 { -1 } },
1544 { ":1111:2222:3333:4444:5555:6666:7777:8888", STATUS_INVALID_PARAMETER, 0,
1545 { -1 } },
1546 { ":1111::", STATUS_INVALID_PARAMETER, 0,
1547 { -1 } },
1548 { "::-1", STATUS_SUCCESS, 2,
1549 { 0, 0, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1550 { "::12345678", STATUS_INVALID_PARAMETER, 10,
1551 { 0, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1552 { "::123456789", STATUS_INVALID_PARAMETER, 11,
1553 { 0, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1554 { "::1234567890abcdef0", STATUS_INVALID_PARAMETER, 19,
1555 { 0, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab, 0xabab } },
1556 { "::0x80000000", STATUS_SUCCESS, 3,
1557 { 0, 0, 0, 0, 0, 0, 0, 0xffff }, ex_fail_6 },
1558 { "::0x012345678", STATUS_SUCCESS, 3,
1559 { 0, 0, 0, 0, 0, 0, 0, 0x7856 }, ex_fail_6 },
1560 { "::0x123456789", STATUS_SUCCESS, 3,
1561 { 0, 0, 0, 0, 0, 0, 0, 0xffff }, ex_fail_6 },
1562 { "::0x1234567890abcdef0", STATUS_SUCCESS, 3,
1563 { 0, 0, 0, 0, 0, 0, 0, 0xffff }, ex_fail_6 },
1564 { "::.", STATUS_SUCCESS, 2,
1565 { 0, 0, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1566 { "::..", STATUS_SUCCESS, 2,
1567 { 0, 0, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1568 { "::...", STATUS_SUCCESS, 2,
1569 { 0, 0, 0, 0, 0, 0, 0, 0 }, ex_fail_6 },
1570 { "XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:1.2.3.4", STATUS_INVALID_PARAMETER, 0,
1571 { -1 } },
1572 { "[::]", STATUS_INVALID_PARAMETER, 0,
1573 { -1 }, ex_skip_6 },
1576 static void init_ip6(IN6_ADDR* addr, const int src[8])
1578 unsigned int j;
1579 if (!src || src[0] == -1)
1581 for (j = 0; j < 8; ++j)
1582 addr->s6_words[j] = 0xabab;
1584 else
1586 for (j = 0; j < 8; ++j)
1587 addr->s6_words[j] = src[j];
1591 static void test_RtlIpv6AddressToString(void)
1593 CHAR buffer[50];
1594 LPCSTR result;
1595 IN6_ADDR ip;
1596 DWORD_PTR len;
1597 static const struct
1599 PCSTR address;
1600 int ip[8];
1601 } tests[] =
1603 /* ipv4 addresses & ISATAP addresses */
1604 { "::13.1.68.3", { 0, 0, 0, 0, 0, 0, 0x10d, 0x344 } },
1605 { "::123.123.123.123", { 0, 0, 0, 0, 0, 0, 0x7b7b, 0x7b7b } },
1606 { "::ffff", { 0, 0, 0, 0, 0, 0, 0, 0xffff } },
1607 { "::0.1.0.0", { 0, 0, 0, 0, 0, 0, 0x100, 0 } },
1608 { "::ffff:13.1.68.3", { 0, 0, 0, 0, 0, 0xffff, 0x10d, 0x344 } },
1609 { "::feff:d01:4403", { 0, 0, 0, 0, 0, 0xfffe, 0x10d, 0x344 } },
1610 { "::fffe:d01:4403", { 0, 0, 0, 0, 0, 0xfeff, 0x10d, 0x344 } },
1611 { "::100:d01:4403", { 0, 0, 0, 0, 0, 1, 0x10d, 0x344 } },
1612 { "::1:d01:4403", { 0, 0, 0, 0, 0, 0x100, 0x10d, 0x344 } },
1613 { "::1:0:d01:4403", { 0, 0, 0, 0, 0x100, 0, 0x10d, 0x344 } },
1614 { "::fffe:d01:4403", { 0, 0, 0, 0, 0, 0xfeff, 0x10d, 0x344 } },
1615 { "::fffe:0:d01:4403", { 0, 0, 0, 0, 0xfeff, 0, 0x10d, 0x344 } },
1616 { "::ffff:0:4403", { 0, 0, 0, 0, 0, 0xffff, 0, 0x344 } },
1617 { "::ffff:0.1.0.0", { 0, 0, 0, 0, 0, 0xffff, 0x100, 0 } },
1618 { "::ffff:13.1.0.0", { 0, 0, 0, 0, 0, 0xffff, 0x10d, 0 } },
1619 { "::ffff:0:0", { 0, 0, 0, 0, 0, 0xffff, 0, 0 } },
1620 { "::ffff:0:ffff", { 0, 0, 0, 0, 0, 0xffff, 0, 0xffff } },
1621 { "::ffff:0:0.1.0.0", { 0, 0, 0, 0, 0xffff, 0, 0x100, 0 } },
1622 { "::ffff:0:13.1.68.3", { 0, 0, 0, 0, 0xffff, 0, 0x10d, 0x344 } },
1623 { "::ffff:ffff:d01:4403", { 0, 0, 0, 0, 0xffff, 0xffff, 0x10d, 0x344 } },
1624 { "::ffff:0:0:d01:4403", { 0, 0, 0, 0xffff, 0, 0, 0x10d, 0x344 } },
1625 { "::ffff:255.255.255.255", { 0, 0, 0, 0, 0, 0xffff, 0xffff, 0xffff } },
1626 { "::ffff:129.144.52.38", { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
1627 { "::5efe:0.0.0.0", { 0, 0, 0, 0, 0, 0xfe5e, 0, 0 } },
1628 { "::5efe:129.144.52.38", { 0, 0, 0, 0, 0, 0xfe5e, 0x9081, 0x2634 } },
1629 { "1111:2222:3333:4444:0:5efe:129.144.52.38", { 0x1111, 0x2222, 0x3333, 0x4444, 0, 0xfe5e, 0x9081, 0x2634 } },
1630 { "1111:2222:3333::5efe:129.144.52.38", { 0x1111, 0x2222, 0x3333, 0, 0, 0xfe5e, 0x9081, 0x2634 } },
1631 { "1111:2222::5efe:129.144.52.38", { 0x1111, 0x2222, 0, 0, 0, 0xfe5e, 0x9081, 0x2634 } },
1632 { "1111::5efe:129.144.52.38", { 0x1111, 0, 0, 0, 0, 0xfe5e, 0x9081, 0x2634 } },
1633 { "::300:5efe:8190:3426", { 0, 0, 0, 0, 3, 0xfe5e, 0x9081, 0x2634 } },
1634 { "::200:5efe:129.144.52.38", { 0, 0, 0, 0, 2, 0xfe5e, 0x9081, 0x2634 } },
1635 { "::100:5efe:8190:3426", { 0, 0, 0, 0, 1, 0xfe5e, 0x9081, 0x2634 } },
1636 /* 'normal' addresses */
1637 { "::1", { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1638 { "::2", { 0, 0, 0, 0, 0, 0, 0, 0x200 } },
1639 { "0:1:2:3:4:5:6:7", { 0, 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700 } },
1640 { "1080::8:800:200c:417a", { 0x8010, 0, 0, 0, 0x800, 0x8, 0x0c20, 0x7a41 } },
1641 { "1111:2222:3333:4444:5555:6666:7b7b:7b7b", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1642 { "1111:2222:3333:4444:5555:6666:7777:8888", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 } },
1643 { "1111:2222:3333:4444:5555:6666::", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0, 0 } },
1644 { "1111:2222:3333:4444:5555:6666:0:8888", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0, 0x8888 } },
1645 { "1111:2222:3333:4444:5555::", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0 } },
1646 { "1111:2222:3333:4444:5555:0:7b7b:7b7b", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0x7b7b, 0x7b7b } },
1647 { "1111:2222:3333:4444:5555:0:7777:8888", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0x7777, 0x8888 } },
1648 { "1111:2222:3333:4444:5555::8888", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0, 0, 0x8888 } },
1649 { "1111::", { 0x1111, 0, 0, 0, 0, 0, 0, 0 } },
1650 { "1111::7b7b:7b7b", { 0x1111, 0, 0, 0, 0, 0, 0x7b7b, 0x7b7b } },
1651 { "1111:0:3333:4444:5555:6666:7b7b:7b7b", { 0x1111, 0, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1652 { "1111:0:3333:4444:5555:6666:7777:8888", { 0x1111, 0, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 } },
1653 { "1111::4444:5555:6666:7b7b:7b7b", { 0x1111, 0, 0, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1654 { "1111::4444:5555:6666:7777:8888", { 0x1111, 0, 0, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 } },
1655 { "1111::5555:6666:7b7b:7b7b", { 0x1111, 0, 0, 0, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1656 { "1111::5555:6666:7777:8888", { 0x1111, 0, 0, 0, 0x5555, 0x6666, 0x7777, 0x8888 } },
1657 { "1111::6666:7b7b:7b7b", { 0x1111, 0, 0, 0, 0, 0x6666, 0x7b7b, 0x7b7b } },
1658 { "1111::6666:7777:8888", { 0x1111, 0, 0, 0, 0, 0x6666, 0x7777, 0x8888 } },
1659 { "1111::7777:8888", { 0x1111, 0, 0, 0, 0, 0, 0x7777, 0x8888 } },
1660 { "1111::8888", { 0x1111, 0, 0, 0, 0, 0, 0, 0x8888 } },
1661 { "1:2:3:4:5:6:102:304", { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x201, 0x403 } },
1662 { "1:2:3:4:5:6:7:8", { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700, 0x800 } },
1663 { "1:2:3:4:5:6::", { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0, 0 } },
1664 { "1:2:3:4:5:6:0:8", { 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0, 0x800 } },
1665 { "2001:0:1234::c1c0:abcd:876", { 0x120, 0, 0x3412, 0, 0, 0xc0c1, 0xcdab, 0x7608 } },
1666 { "2001:0:4136:e378:8000:63bf:3fff:fdd2", { 0x120, 0, 0x3641, 0x78e3, 0x80, 0xbf63, 0xff3f, 0xd2fd } },
1667 { "2001:db8::1428:57ab", { 0x120, 0xb80d, 0, 0, 0, 0, 0x2814, 0xab57 } },
1668 { "2001:db8:1234:ffff:ffff:ffff:ffff:ffff", { 0x120, 0xb80d, 0x3412, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff } },
1669 { "2001:0:ce49:7601:2cad:dfff:7c94:fffe", { 0x120, 0, 0x49ce, 0x176, 0xad2c, 0xffdf, 0x947c, 0xfeff } },
1670 { "2001:db8:85a3::8a2e:370:7334", { 0x120, 0xb80d, 0xa385, 0, 0, 0x2e8a, 0x7003, 0x3473 } },
1671 { "3ffe:b00::1:0:0:a", { 0xfe3f, 0xb, 0, 0, 0x100, 0, 0, 0xa00 } },
1672 { "::a:b:c:d:e", { 0, 0, 0, 0xa00, 0xb00, 0xc00, 0xd00, 0xe00 } },
1673 { "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", { 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff } },
1674 { "1111:2222:3333:4444:5555:6666:7777:1", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x100 } },
1675 { "1111:2222:3333:4444:5555:6666:7777:8888", { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888 } },
1676 { "1111:2222::", { 0x1111, 0x2222, 0, 0, 0, 0, 0, 0 } },
1677 { "1111::3333:4444:5555:6666:7777", { 0x1111, 0, 0, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777 } },
1678 { "1111:2222::", { 0x1111, 0x2222, 0, 0, 0, 0, 0, 0 } },
1679 { "1111::3333", { 0x1111, 0, 0, 0, 0, 0, 0, 0x3333 } },
1680 { "2001:0:1234::c1c0:abcd:876", { 0x120, 0, 0x3412, 0, 0, 0xc0c1, 0xcdab, 0x7608 } },
1681 { "2001::ffd3", { 0x120, 0, 0, 0, 0, 0, 0, 0xd3ff } },
1683 unsigned int i;
1685 memset(buffer, '#', sizeof(buffer));
1686 buffer[sizeof(buffer)-1] = 0;
1687 memset(&ip, 0, sizeof(ip));
1688 result = RtlIpv6AddressToStringA(&ip, buffer);
1690 len = strlen(buffer);
1691 ok(result == (buffer + len) && !strcmp(buffer, "::"),
1692 "got %p with '%s' (expected %p with '::')\n", result, buffer, buffer + len);
1694 result = RtlIpv6AddressToStringA(&ip, NULL);
1695 ok(result == (LPCSTR)~0 || broken(result == (LPCSTR)len) /* WinXP / Win2k3 */,
1696 "got %p, expected %p\n", result, (LPCSTR)~0);
1698 for (i = 0; i < ARRAY_SIZE(tests); i++)
1700 init_ip6(&ip, tests[i].ip);
1701 memset(buffer, '#', sizeof(buffer));
1702 buffer[sizeof(buffer)-1] = 0;
1704 result = RtlIpv6AddressToStringA(&ip, buffer);
1705 len = strlen(buffer);
1706 ok(result == (buffer + len) && !strcmp(buffer, tests[i].address),
1707 "got %p with '%s' (expected %p with '%s')\n", result, buffer, buffer + len, tests[i].address);
1709 ok(buffer[45] == 0 || broken(buffer[45] != 0) /* WinXP / Win2k3 */,
1710 "expected data at buffer[45] to always be NULL\n");
1711 ok(buffer[46] == '#', "expected data at buffer[46] not to change\n");
1715 static void test_RtlIpv6AddressToStringEx(void)
1717 CHAR buffer[70];
1718 NTSTATUS res;
1719 IN6_ADDR ip;
1720 ULONG len;
1721 static const struct
1723 PCSTR address;
1724 ULONG scopeid;
1725 USHORT port;
1726 int ip[8];
1727 } tests[] =
1729 /* ipv4 addresses & ISATAP addresses */
1730 { "::13.1.68.3", 0, 0, { 0, 0, 0, 0, 0, 0, 0x10d, 0x344 } },
1731 { "::13.1.68.3%1", 1, 0, { 0, 0, 0, 0, 0, 0, 0x10d, 0x344 } },
1732 { "::13.1.68.3%4294949819", 0xffffbbbb, 0, { 0, 0, 0, 0, 0, 0, 0x10d, 0x344 } },
1733 { "[::13.1.68.3%4294949819]:65518", 0xffffbbbb, 0xeeff, { 0, 0, 0, 0, 0, 0, 0x10d, 0x344 } },
1734 { "[::13.1.68.3%4294949819]:256", 0xffffbbbb, 1, { 0, 0, 0, 0, 0, 0, 0x10d, 0x344 } },
1735 { "[::13.1.68.3]:256", 0, 1, { 0, 0, 0, 0, 0, 0, 0x10d, 0x344 } },
1737 { "::1:d01:4403", 0, 0, { 0, 0, 0, 0, 0, 0x100, 0x10d, 0x344 } },
1738 { "::1:d01:4403%1", 1, 0, { 0, 0, 0, 0, 0, 0x100, 0x10d, 0x344 } },
1739 { "::1:d01:4403%4294949819", 0xffffbbbb, 0, { 0, 0, 0, 0, 0, 0x100, 0x10d, 0x344 } },
1740 { "[::1:d01:4403%4294949819]:65518", 0xffffbbbb, 0xeeff, { 0, 0, 0, 0, 0, 0x100, 0x10d, 0x344 } },
1741 { "[::1:d01:4403%4294949819]:256", 0xffffbbbb, 1, { 0, 0, 0, 0, 0, 0x100, 0x10d, 0x344 } },
1742 { "[::1:d01:4403]:256", 0, 1, { 0, 0, 0, 0, 0, 0x100, 0x10d, 0x344 } },
1744 { "1111:2222:3333:4444:0:5efe:129.144.52.38", 0, 0, { 0x1111, 0x2222, 0x3333, 0x4444, 0, 0xfe5e, 0x9081, 0x2634 } },
1745 { "1111:2222:3333:4444:0:5efe:129.144.52.38%1", 1, 0, { 0x1111, 0x2222, 0x3333, 0x4444, 0, 0xfe5e, 0x9081, 0x2634 } },
1746 { "1111:2222:3333:4444:0:5efe:129.144.52.38%4294949819", 0xffffbbbb, 0, { 0x1111, 0x2222, 0x3333, 0x4444, 0, 0xfe5e, 0x9081, 0x2634 } },
1747 { "[1111:2222:3333:4444:0:5efe:129.144.52.38%4294949819]:65518",0xffffbbbb, 0xeeff, { 0x1111, 0x2222, 0x3333, 0x4444, 0, 0xfe5e, 0x9081, 0x2634 } },
1748 { "[1111:2222:3333:4444:0:5efe:129.144.52.38%4294949819]:256", 0xffffbbbb, 1, { 0x1111, 0x2222, 0x3333, 0x4444, 0, 0xfe5e, 0x9081, 0x2634 } },
1749 { "[1111:2222:3333:4444:0:5efe:129.144.52.38]:256", 0, 1, { 0x1111, 0x2222, 0x3333, 0x4444, 0, 0xfe5e, 0x9081, 0x2634 } },
1751 { "::1", 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1752 { "::1%1", 1, 0, { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1753 { "::1%4294949819", 0xffffbbbb, 0, { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1754 { "[::1%4294949819]:65518", 0xffffbbbb, 0xeeff, { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1755 { "[::1%4294949819]:256", 0xffffbbbb, 1, { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1756 { "[::1]:256", 0, 1, { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
1758 { "1111:2222:3333:4444:5555:6666:7b7b:7b7b", 0, 0, { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1759 { "1111:2222:3333:4444:5555:6666:7b7b:7b7b%1", 1, 0, { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1760 { "1111:2222:3333:4444:5555:6666:7b7b:7b7b%4294949819", 0xffffbbbb, 0, { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1761 { "[1111:2222:3333:4444:5555:6666:7b7b:7b7b%4294949819]:65518", 0xffffbbbb, 0xeeff, { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1762 { "[1111:2222:3333:4444:5555:6666:7b7b:7b7b%4294949819]:256", 0xffffbbbb, 1, { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1763 { "[1111:2222:3333:4444:5555:6666:7b7b:7b7b]:256", 0, 1, { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7b7b, 0x7b7b } },
1765 { "1111::", 0, 0, { 0x1111, 0, 0, 0, 0, 0, 0, 0 } },
1766 { "1111::%1", 1, 0, { 0x1111, 0, 0, 0, 0, 0, 0, 0 } },
1767 { "1111::%4294949819", 0xffffbbbb, 0, { 0x1111, 0, 0, 0, 0, 0, 0, 0 } },
1768 { "[1111::%4294949819]:65518", 0xffffbbbb, 0xeeff, { 0x1111, 0, 0, 0, 0, 0, 0, 0 } },
1769 { "[1111::%4294949819]:256", 0xffffbbbb, 1, { 0x1111, 0, 0, 0, 0, 0, 0, 0 } },
1770 { "[1111::]:256", 0, 1, { 0x1111, 0, 0, 0, 0, 0, 0, 0 } },
1772 { "2001::ffd3", 0, 0, { 0x120, 0, 0, 0, 0, 0, 0, 0xd3ff } },
1773 { "2001::ffd3%1", 1, 0, { 0x120, 0, 0, 0, 0, 0, 0, 0xd3ff } },
1774 { "2001::ffd3%4294949819", 0xffffbbbb, 0, { 0x120, 0, 0, 0, 0, 0, 0, 0xd3ff } },
1775 { "[2001::ffd3%4294949819]:65518", 0xffffbbbb, 0xeeff, { 0x120, 0, 0, 0, 0, 0, 0, 0xd3ff } },
1776 { "[2001::ffd3%4294949819]:256", 0xffffbbbb, 1, { 0x120, 0, 0, 0, 0, 0, 0, 0xd3ff } },
1777 { "[2001::ffd3]:256", 0, 1, { 0x120, 0, 0, 0, 0, 0, 0, 0xd3ff } },
1779 unsigned int i;
1781 if (!pRtlIpv6AddressToStringExA)
1783 skip("RtlIpv6AddressToStringExA not available\n");
1784 return;
1787 memset(buffer, '#', sizeof(buffer));
1788 buffer[sizeof(buffer)-1] = 0;
1789 memset(&ip, 0, sizeof(ip));
1790 len = sizeof(buffer);
1791 res = pRtlIpv6AddressToStringExA(&ip, 0, 0, buffer, &len);
1793 ok(res == STATUS_SUCCESS, "[validate] res = 0x%08lx, expected STATUS_SUCCESS\n", res);
1794 ok(len == 3 && !strcmp(buffer, "::"),
1795 "got len %ld with '%s' (expected 3 with '::')\n", len, buffer);
1797 memset(buffer, '#', sizeof(buffer));
1798 buffer[sizeof(buffer)-1] = 0;
1800 len = sizeof(buffer);
1801 res = pRtlIpv6AddressToStringExA(NULL, 0, 0, buffer, &len);
1802 ok(res == STATUS_INVALID_PARAMETER, "[null ip] res = 0x%08lx, expected STATUS_INVALID_PARAMETER\n", res);
1804 len = sizeof(buffer);
1805 res = pRtlIpv6AddressToStringExA(&ip, 0, 0, NULL, &len);
1806 ok(res == STATUS_INVALID_PARAMETER, "[null buffer] res = 0x%08lx, expected STATUS_INVALID_PARAMETER\n", res);
1808 res = pRtlIpv6AddressToStringExA(&ip, 0, 0, buffer, NULL);
1809 ok(res == STATUS_INVALID_PARAMETER, "[null length] res = 0x%08lx, expected STATUS_INVALID_PARAMETER\n", res);
1811 len = 2;
1812 memset(buffer, '#', sizeof(buffer));
1813 buffer[sizeof(buffer)-1] = 0;
1814 res = pRtlIpv6AddressToStringExA(&ip, 0, 0, buffer, &len);
1815 ok(res == STATUS_INVALID_PARAMETER, "[null length] res = 0x%08lx, expected STATUS_INVALID_PARAMETER\n", res);
1816 ok(buffer[0] == '#', "got first char %c (expected '#')\n", buffer[0]);
1817 ok(len == 3, "got len %ld (expected len 3)\n", len);
1819 for (i = 0; i < ARRAY_SIZE(tests); i++)
1821 init_ip6(&ip, tests[i].ip);
1822 len = sizeof(buffer);
1823 memset(buffer, '#', sizeof(buffer));
1824 buffer[sizeof(buffer)-1] = 0;
1826 res = pRtlIpv6AddressToStringExA(&ip, tests[i].scopeid, tests[i].port, buffer, &len);
1828 ok(res == STATUS_SUCCESS, "[validate] res = 0x%08lx, expected STATUS_SUCCESS\n", res);
1829 ok(len == (strlen(tests[i].address) + 1) && !strcmp(buffer, tests[i].address),
1830 "got len %ld with '%s' (expected %d with '%s')\n", len, buffer, (int)strlen(tests[i].address), tests[i].address);
1834 static void compare_RtlIpv6StringToAddressW(PCSTR name_a, int terminator_offset_a,
1835 const struct in6_addr *addr_a, NTSTATUS res_a)
1837 WCHAR name[512];
1838 NTSTATUS res;
1839 IN6_ADDR ip;
1840 PCWSTR terminator;
1842 RtlMultiByteToUnicodeN(name, sizeof(name), NULL, name_a, strlen(name_a) + 1);
1844 init_ip6(&ip, NULL);
1845 terminator = (void *)0xdeadbeef;
1846 res = RtlIpv6StringToAddressW(name, &terminator, &ip);
1847 ok(res == res_a, "[W:%s] res = 0x%08lx, expected 0x%08lx\n", name_a, res, res_a);
1849 if (terminator_offset_a < 0)
1851 ok(terminator == (void *)0xdeadbeef,
1852 "[W:%s] terminator = %p, expected it not to change\n",
1853 name_a, terminator);
1855 else
1857 ok(terminator == name + terminator_offset_a,
1858 "[W:%s] terminator = %p, expected %p\n",
1859 name_a, terminator, name + terminator_offset_a);
1862 ok(!memcmp(&ip, addr_a, sizeof(ip)),
1863 "[W:%s] ip = %x:%x:%x:%x:%x:%x:%x:%x, expected %x:%x:%x:%x:%x:%x:%x:%x\n",
1864 name_a,
1865 ip.s6_words[0], ip.s6_words[1], ip.s6_words[2], ip.s6_words[3],
1866 ip.s6_words[4], ip.s6_words[5], ip.s6_words[6], ip.s6_words[7],
1867 addr_a->s6_words[0], addr_a->s6_words[1], addr_a->s6_words[2], addr_a->s6_words[3],
1868 addr_a->s6_words[4], addr_a->s6_words[5], addr_a->s6_words[6], addr_a->s6_words[7]);
1871 static void test_RtlIpv6StringToAddress(void)
1873 NTSTATUS res;
1874 IN6_ADDR ip, expected_ip;
1875 PCSTR terminator;
1876 unsigned int i;
1878 res = RtlIpv6StringToAddressA("::", &terminator, &ip);
1879 ok(res == STATUS_SUCCESS, "[validate] res = 0x%08lx, expected STATUS_SUCCESS\n", res);
1880 if (0)
1882 /* any of these crash */
1883 res = RtlIpv6StringToAddressA(NULL, &terminator, &ip);
1884 ok(res == STATUS_INVALID_PARAMETER, "[null string] res = 0x%08lx, expected STATUS_INVALID_PARAMETER\n", res);
1885 res = RtlIpv6StringToAddressA("::", NULL, &ip);
1886 ok(res == STATUS_INVALID_PARAMETER, "[null terminator] res = 0x%08lx, expected STATUS_INVALID_PARAMETER\n", res);
1887 res = RtlIpv6StringToAddressA("::", &terminator, NULL);
1888 ok(res == STATUS_INVALID_PARAMETER, "[null result] res = 0x%08lx, expected STATUS_INVALID_PARAMETER\n", res);
1891 /* sanity check */
1892 ok(sizeof(ip) == sizeof(USHORT)* 8, "sizeof(ip)\n");
1894 for (i = 0; i < ARRAY_SIZE(ipv6_tests); i++)
1896 init_ip6(&ip, NULL);
1897 terminator = (void *)0xdeadbeef;
1898 res = RtlIpv6StringToAddressA(ipv6_tests[i].address, &terminator, &ip);
1899 compare_RtlIpv6StringToAddressW(ipv6_tests[i].address, (terminator != (void *)0xdeadbeef) ?
1900 (terminator - ipv6_tests[i].address) : -1, &ip, res);
1902 if (ipv6_tests[i].flags & win_broken_6)
1904 ok(res == ipv6_tests[i].res || broken(res == STATUS_INVALID_PARAMETER),
1905 "[%s] res = 0x%08lx, expected 0x%08lx\n",
1906 ipv6_tests[i].address, res, ipv6_tests[i].res);
1908 if (res == STATUS_INVALID_PARAMETER)
1909 continue;
1911 else
1913 ok(res == ipv6_tests[i].res,
1914 "[%s] res = 0x%08lx, expected 0x%08lx\n",
1915 ipv6_tests[i].address, res, ipv6_tests[i].res);
1918 if (ipv6_tests[i].terminator_offset < 0)
1920 ok(terminator == (void *)0xdeadbeef,
1921 "[%s] terminator = %p, expected it not to change\n",
1922 ipv6_tests[i].address, terminator);
1924 else
1926 if (ipv6_tests[i].flags & win_extra_zero)
1927 ok(terminator == ipv6_tests[i].address + ipv6_tests[i].terminator_offset ||
1928 broken(terminator != ipv6_tests[i].address + ipv6_tests[i].terminator_offset),
1929 "[%s] terminator = %p, expected %p\n",
1930 ipv6_tests[i].address, terminator, ipv6_tests[i].address + ipv6_tests[i].terminator_offset);
1931 else
1932 ok(terminator == ipv6_tests[i].address + ipv6_tests[i].terminator_offset,
1933 "[%s] terminator = %p, expected %p\n",
1934 ipv6_tests[i].address, terminator, ipv6_tests[i].address + ipv6_tests[i].terminator_offset);
1937 init_ip6(&expected_ip, ipv6_tests[i].ip);
1938 if (ipv6_tests[i].flags & win_extra_zero)
1939 ok(!memcmp(&ip, &expected_ip, sizeof(ip)) || broken(memcmp(&ip, &expected_ip, sizeof(ip))),
1940 "[%s] ip = %x:%x:%x:%x:%x:%x:%x:%x, expected %x:%x:%x:%x:%x:%x:%x:%x\n",
1941 ipv6_tests[i].address, ip.s6_words[0], ip.s6_words[1], ip.s6_words[2], ip.s6_words[3],
1942 ip.s6_words[4], ip.s6_words[5], ip.s6_words[6], ip.s6_words[7],
1943 expected_ip.s6_words[0], expected_ip.s6_words[1], expected_ip.s6_words[2], expected_ip.s6_words[3],
1944 expected_ip.s6_words[4], expected_ip.s6_words[5], expected_ip.s6_words[6], expected_ip.s6_words[7]);
1945 else
1946 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
1947 "[%s] ip = %x:%x:%x:%x:%x:%x:%x:%x, expected %x:%x:%x:%x:%x:%x:%x:%x\n",
1948 ipv6_tests[i].address, ip.s6_words[0], ip.s6_words[1], ip.s6_words[2], ip.s6_words[3],
1949 ip.s6_words[4], ip.s6_words[5], ip.s6_words[6], ip.s6_words[7],
1950 expected_ip.s6_words[0], expected_ip.s6_words[1], expected_ip.s6_words[2], expected_ip.s6_words[3],
1951 expected_ip.s6_words[4], expected_ip.s6_words[5], expected_ip.s6_words[6], expected_ip.s6_words[7]);
1955 static void compare_RtlIpv6StringToAddressExW(PCSTR name_a, const struct in6_addr *addr_a, HRESULT res_a, ULONG scope_a, USHORT port_a)
1957 WCHAR name[512];
1958 NTSTATUS res;
1959 IN6_ADDR ip;
1960 ULONG scope = 0xbadf00d;
1961 USHORT port = 0xbeef;
1963 if (!pRtlIpv6StringToAddressExW)
1964 return;
1966 RtlMultiByteToUnicodeN(name, sizeof(name), NULL, name_a, strlen(name_a) + 1);
1968 init_ip6(&ip, NULL);
1969 res = pRtlIpv6StringToAddressExW(name, &ip, &scope, &port);
1971 ok(res == res_a, "[W:%s] res = 0x%08lx, expected 0x%08lx\n", name_a, res, res_a);
1972 ok(scope == scope_a, "[W:%s] scope = 0x%08lx, expected 0x%08lx\n", name_a, scope, scope_a);
1973 ok(port == port_a, "[W:%s] port = 0x%08x, expected 0x%08x\n", name_a, port, port_a);
1975 ok(!memcmp(&ip, addr_a, sizeof(ip)),
1976 "[W:%s] ip = %x:%x:%x:%x:%x:%x:%x:%x, expected %x:%x:%x:%x:%x:%x:%x:%x\n",
1977 name_a,
1978 ip.s6_words[0], ip.s6_words[1], ip.s6_words[2], ip.s6_words[3],
1979 ip.s6_words[4], ip.s6_words[5], ip.s6_words[6], ip.s6_words[7],
1980 addr_a->s6_words[0], addr_a->s6_words[1], addr_a->s6_words[2], addr_a->s6_words[3],
1981 addr_a->s6_words[4], addr_a->s6_words[5], addr_a->s6_words[6], addr_a->s6_words[7]);
1984 static void test_RtlIpv6StringToAddressEx(void)
1986 NTSTATUS res;
1987 IN6_ADDR ip, expected_ip;
1988 ULONG scope;
1989 USHORT port;
1990 static const struct
1992 PCSTR address;
1993 NTSTATUS res;
1994 ULONG scope;
1995 USHORT port;
1996 int ip[8];
1997 } ipv6_ex_tests[] =
1999 { "[::]", STATUS_SUCCESS, 0, 0,
2000 { 0, 0, 0, 0, 0, 0, 0, 0 } },
2001 { "[::1]:8080", STATUS_SUCCESS, 0, 0x901f,
2002 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
2003 { "[::1]:0x80", STATUS_SUCCESS, 0, 0x8000,
2004 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
2005 { "[::1]:0X80", STATUS_SUCCESS, 0, 0x8000,
2006 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
2007 { "[::1]:080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2008 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
2009 { "[::1]:800000000080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2010 { 0, 0, 0, 0, 0, 0, 0, 0x100 } },
2011 { "[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80", STATUS_SUCCESS, 0, 0x5000,
2012 { 0xdcfe, 0x98ba, 0x5476, 0x1032, 0xdcfe, 0x98ba, 0x5476, 0x1032 } },
2013 { "[1080:0:0:0:8:800:200C:417A]:1234", STATUS_SUCCESS, 0, 0xd204,
2014 { 0x8010, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2015 { "[3ffe:2a00:100:7031::1]:8080", STATUS_SUCCESS, 0, 0x901f,
2016 { 0xfe3f, 0x2a, 1, 0x3170, 0, 0, 0, 0x100 } },
2017 { "[ 3ffe:2a00:100:7031::1]:8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2018 { -1 } },
2019 { "[3ffe:2a00:100:7031::1 ]:8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2020 { 0xfe3f, 0x2a, 1, 0x3170, 0, 0, 0, 0x100 } },
2021 { "[3ffe:2a00:100:7031::1].8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2022 { 0xfe3f, 0x2a, 1, 0x3170, 0, 0, 0, 0x100 } },
2023 { "[1080::8:800:200C:417A]:8080", STATUS_SUCCESS, 0, 0x901f,
2024 { 0x8010, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2025 { "[1080::8:800:200C:417A]!8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2026 { 0x8010, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2027 { "[::FFFF:129.144.52.38]:80", STATUS_SUCCESS, 0, 0x5000,
2028 { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
2029 { "[::FFFF:129.144.52.38]:-80", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2030 { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
2031 { "[::FFFF:129.144.52.38]:999999999999", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2032 { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
2033 { "[::FFFF:129.144.52.38%-8]:80", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2034 { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
2035 { "[::FFFF:129.144.52.38]:80", STATUS_SUCCESS, 0, 0x5000,
2036 { 0, 0, 0, 0, 0, 0xffff, 0x9081, 0x2634 } },
2037 { "[12345::6:7:8]:80", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2038 { -1 } },
2039 { "[ff01::8:800:200C:417A%16]:8080", STATUS_SUCCESS, 16, 0x901f,
2040 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2041 { "[ff01::8:800:200C:417A%100]:8080", STATUS_SUCCESS, 100, 0x901f,
2042 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2043 { "[ff01::8:800:200C:417A%1000]:8080", STATUS_SUCCESS, 1000, 0x901f,
2044 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2045 { "[ff01::8:800:200C:417A%10000]:8080", STATUS_SUCCESS, 10000, 0x901f,
2046 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2047 { "[ff01::8:800:200C:417A%1000000]:8080", STATUS_SUCCESS, 1000000, 0x901f,
2048 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2049 { "[ff01::8:800:200C:417A%4294967295]:8080", STATUS_SUCCESS, 0xffffffff, 0x901f,
2050 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2051 { "[ff01::8:800:200C:417A%4294967296]:8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2052 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2053 { "[ff01::8:800:200C:417A%-1]:8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2054 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2055 { "[ff01::8:800:200C:417A%0]:8080", STATUS_SUCCESS, 0, 0x901f,
2056 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2057 { "[ff01::8:800:200C:417A%1", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2058 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2059 { "[ff01::8:800:200C:417A%0x1000]:8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2060 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2061 { "[ff01::8:800:200C:417A/16]:8080", STATUS_INVALID_PARAMETER, 0xbadf00d, 0xbeef,
2062 { 0x1ff, 0, 0, 0, 0x800, 8, 0xc20, 0x7a41 } },
2064 const char *simple_ip = "::";
2065 unsigned int i;
2067 if (!pRtlIpv6StringToAddressExW)
2069 skip("RtlIpv6StringToAddressExW not available\n");
2070 /* we can continue, just not test W */
2073 if (!pRtlIpv6StringToAddressExA)
2075 skip("RtlIpv6StringToAddressExA not available\n");
2076 return;
2079 res = pRtlIpv6StringToAddressExA(simple_ip, &ip, &scope, &port);
2080 ok(res == STATUS_SUCCESS, "[validate] res = 0x%08lx, expected STATUS_SUCCESS\n", res);
2082 init_ip6(&ip, NULL);
2083 init_ip6(&expected_ip, NULL);
2084 scope = 0xbadf00d;
2085 port = 0xbeef;
2086 res = pRtlIpv6StringToAddressExA(NULL, &ip, &scope, &port);
2087 ok(res == STATUS_INVALID_PARAMETER,
2088 "[null string] res = 0x%08lx, expected STATUS_INVALID_PARAMETER\n", res);
2089 ok(scope == 0xbadf00d, "[null string] scope = 0x%08lx, expected 0xbadf00d\n", scope);
2090 ok(port == 0xbeef, "[null string] port = 0x%08x, expected 0xbeef\n", port);
2091 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
2092 "[null string] ip is changed, expected it not to change\n");
2095 init_ip6(&ip, NULL);
2096 scope = 0xbadf00d;
2097 port = 0xbeef;
2098 res = pRtlIpv6StringToAddressExA(simple_ip, NULL, &scope, &port);
2099 ok(res == STATUS_INVALID_PARAMETER,
2100 "[null result] res = 0x%08lx, expected STATUS_INVALID_PARAMETER\n", res);
2101 ok(scope == 0xbadf00d, "[null result] scope = 0x%08lx, expected 0xbadf00d\n", scope);
2102 ok(port == 0xbeef, "[null result] port = 0x%08x, expected 0xbeef\n", port);
2103 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
2104 "[null result] ip is changed, expected it not to change\n");
2106 init_ip6(&ip, NULL);
2107 scope = 0xbadf00d;
2108 port = 0xbeef;
2109 res = pRtlIpv6StringToAddressExA(simple_ip, &ip, NULL, &port);
2110 ok(res == STATUS_INVALID_PARAMETER,
2111 "[null scope] res = 0x%08lx, expected STATUS_INVALID_PARAMETER\n", res);
2112 ok(scope == 0xbadf00d, "[null scope] scope = 0x%08lx, expected 0xbadf00d\n", scope);
2113 ok(port == 0xbeef, "[null scope] port = 0x%08x, expected 0xbeef\n", port);
2114 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
2115 "[null scope] ip is changed, expected it not to change\n");
2117 init_ip6(&ip, NULL);
2118 scope = 0xbadf00d;
2119 port = 0xbeef;
2120 res = pRtlIpv6StringToAddressExA(simple_ip, &ip, &scope, NULL);
2121 ok(res == STATUS_INVALID_PARAMETER,
2122 "[null port] res = 0x%08lx, expected STATUS_INVALID_PARAMETER\n", res);
2123 ok(scope == 0xbadf00d, "[null port] scope = 0x%08lx, expected 0xbadf00d\n", scope);
2124 ok(port == 0xbeef, "[null port] port = 0x%08x, expected 0xbeef\n", port);
2125 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
2126 "[null port] ip is changed, expected it not to change\n");
2128 /* sanity check */
2129 ok(sizeof(ip) == sizeof(USHORT)* 8, "sizeof(ip)\n");
2131 /* first we run all ip related tests, to make sure someone didn't accidentally reimplement instead of re-use. */
2132 for (i = 0; i < ARRAY_SIZE(ipv6_tests); i++)
2134 ULONG scope = 0xbadf00d;
2135 USHORT port = 0xbeef;
2136 NTSTATUS expect_ret = (ipv6_tests[i].flags & ex_fail_6) ? STATUS_INVALID_PARAMETER : ipv6_tests[i].res;
2138 if (ipv6_tests[i].flags & ex_skip_6)
2139 continue;
2141 init_ip6(&ip, NULL);
2142 res = pRtlIpv6StringToAddressExA(ipv6_tests[i].address, &ip, &scope, &port);
2143 compare_RtlIpv6StringToAddressExW(ipv6_tests[i].address, &ip, res, scope, port);
2145 /* make sure nothing was changed if this function fails. */
2146 if (res == STATUS_INVALID_PARAMETER)
2148 ok(scope == 0xbadf00d, "[%s] scope = 0x%08lx, expected 0xbadf00d\n",
2149 ipv6_tests[i].address, scope);
2150 ok(port == 0xbeef, "[%s] port = 0x%08x, expected 0xbeef\n",
2151 ipv6_tests[i].address, port);
2153 else
2155 ok(scope != 0xbadf00d, "[%s] scope = 0x%08lx, not expected 0xbadf00d\n",
2156 ipv6_tests[i].address, scope);
2157 ok(port != 0xbeef, "[%s] port = 0x%08x, not expected 0xbeef\n",
2158 ipv6_tests[i].address, port);
2161 if (ipv6_tests[i].flags & win_broken_6)
2163 ok(res == expect_ret || broken(res == STATUS_INVALID_PARAMETER),
2164 "[%s] res = 0x%08lx, expected 0x%08lx\n", ipv6_tests[i].address, res, expect_ret);
2166 if (res == STATUS_INVALID_PARAMETER)
2167 continue;
2169 else
2171 ok(res == expect_ret, "[%s] res = 0x%08lx, expected 0x%08lx\n",
2172 ipv6_tests[i].address, res, expect_ret);
2175 /* If ex fails but non-ex does not we cannot check if the part that is converted
2176 before it failed was correct, since there is no data for it in the table. */
2177 if (res == expect_ret)
2179 init_ip6(&expected_ip, ipv6_tests[i].ip);
2180 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
2181 "[%s] ip = %x:%x:%x:%x:%x:%x:%x:%x, expected %x:%x:%x:%x:%x:%x:%x:%x\n",
2182 ipv6_tests[i].address,
2183 ip.s6_words[0], ip.s6_words[1], ip.s6_words[2], ip.s6_words[3],
2184 ip.s6_words[4], ip.s6_words[5], ip.s6_words[6], ip.s6_words[7],
2185 expected_ip.s6_words[0], expected_ip.s6_words[1], expected_ip.s6_words[2], expected_ip.s6_words[3],
2186 expected_ip.s6_words[4], expected_ip.s6_words[5], expected_ip.s6_words[6], expected_ip.s6_words[7]);
2190 /* now we run scope / port related tests */
2191 for (i = 0; i < ARRAY_SIZE(ipv6_ex_tests); i++)
2193 scope = 0xbadf00d;
2194 port = 0xbeef;
2195 init_ip6(&ip, NULL);
2196 res = pRtlIpv6StringToAddressExA(ipv6_ex_tests[i].address, &ip, &scope, &port);
2197 compare_RtlIpv6StringToAddressExW(ipv6_ex_tests[i].address, &ip, res, scope, port);
2199 ok(res == ipv6_ex_tests[i].res, "[%s] res = 0x%08lx, expected 0x%08lx\n",
2200 ipv6_ex_tests[i].address, res, ipv6_ex_tests[i].res);
2201 ok(scope == ipv6_ex_tests[i].scope, "[%s] scope = 0x%08lx, expected 0x%08lx\n",
2202 ipv6_ex_tests[i].address, scope, ipv6_ex_tests[i].scope);
2203 ok(port == ipv6_ex_tests[i].port, "[%s] port = 0x%08x, expected 0x%08x\n",
2204 ipv6_ex_tests[i].address, port, ipv6_ex_tests[i].port);
2206 init_ip6(&expected_ip, ipv6_ex_tests[i].ip);
2207 ok(!memcmp(&ip, &expected_ip, sizeof(ip)),
2208 "[%s] ip = %x:%x:%x:%x:%x:%x:%x:%x, expected %x:%x:%x:%x:%x:%x:%x:%x\n",
2209 ipv6_ex_tests[i].address,
2210 ip.s6_words[0], ip.s6_words[1], ip.s6_words[2], ip.s6_words[3],
2211 ip.s6_words[4], ip.s6_words[5], ip.s6_words[6], ip.s6_words[7],
2212 expected_ip.s6_words[0], expected_ip.s6_words[1], expected_ip.s6_words[2], expected_ip.s6_words[3],
2213 expected_ip.s6_words[4], expected_ip.s6_words[5], expected_ip.s6_words[6], expected_ip.s6_words[7]);
2217 static void test_LdrAddRefDll(void)
2219 HMODULE mod, mod2;
2220 NTSTATUS status;
2221 BOOL ret;
2223 mod = LoadLibraryA("comctl32.dll");
2224 ok(mod != NULL, "got %p\n", mod);
2225 ret = FreeLibrary(mod);
2226 ok(ret, "got %d\n", ret);
2228 mod2 = GetModuleHandleA("comctl32.dll");
2229 ok(mod2 == NULL, "got %p\n", mod2);
2231 /* load, addref and release 2 times */
2232 mod = LoadLibraryA("comctl32.dll");
2233 ok(mod != NULL, "got %p\n", mod);
2234 status = LdrAddRefDll(0, mod);
2235 ok(status == STATUS_SUCCESS, "got 0x%08lx\n", status);
2236 ret = FreeLibrary(mod);
2237 ok(ret, "got %d\n", ret);
2239 mod2 = GetModuleHandleA("comctl32.dll");
2240 ok(mod2 != NULL, "got %p\n", mod2);
2241 ret = FreeLibrary(mod);
2242 ok(ret, "got %d\n", ret);
2244 mod2 = GetModuleHandleA("comctl32.dll");
2245 ok(mod2 == NULL, "got %p\n", mod2);
2247 /* pin refcount */
2248 mod = LoadLibraryA("comctl32.dll");
2249 ok(mod != NULL, "got %p\n", mod);
2250 status = LdrAddRefDll(LDR_ADDREF_DLL_PIN, mod);
2251 ok(status == STATUS_SUCCESS, "got 0x%08lx\n", status);
2253 ret = FreeLibrary(mod);
2254 ok(ret, "got %d\n", ret);
2255 ret = FreeLibrary(mod);
2256 ok(ret, "got %d\n", ret);
2257 ret = FreeLibrary(mod);
2258 ok(ret, "got %d\n", ret);
2259 ret = FreeLibrary(mod);
2260 ok(ret, "got %d\n", ret);
2262 mod2 = GetModuleHandleA("comctl32.dll");
2263 ok(mod2 != NULL, "got %p\n", mod2);
2266 static void test_LdrLockLoaderLock(void)
2268 ULONG_PTR magic;
2269 ULONG result;
2270 NTSTATUS status;
2272 /* invalid flags */
2273 result = 10;
2274 magic = 0xdeadbeef;
2275 status = LdrLockLoaderLock(0x10, &result, &magic);
2276 ok(status == STATUS_INVALID_PARAMETER_1, "got 0x%08lx\n", status);
2277 ok(result == 0, "got %ld\n", result);
2278 ok(magic == 0, "got %Ix\n", magic);
2280 magic = 0xdeadbeef;
2281 status = LdrLockLoaderLock(0x10, NULL, &magic);
2282 ok(status == STATUS_INVALID_PARAMETER_1, "got 0x%08lx\n", status);
2283 ok(magic == 0, "got %Ix\n", magic);
2285 result = 10;
2286 status = LdrLockLoaderLock(0x10, &result, NULL);
2287 ok(status == STATUS_INVALID_PARAMETER_1, "got 0x%08lx\n", status);
2288 ok(result == 0, "got %ld\n", result);
2290 /* non-blocking mode, result is null */
2291 magic = 0xdeadbeef;
2292 status = LdrLockLoaderLock(0x2, NULL, &magic);
2293 ok(status == STATUS_INVALID_PARAMETER_2, "got 0x%08lx\n", status);
2294 ok(magic == 0, "got %Ix\n", magic);
2296 /* magic pointer is null */
2297 result = 10;
2298 status = LdrLockLoaderLock(0, &result, NULL);
2299 ok(status == STATUS_INVALID_PARAMETER_3, "got 0x%08lx\n", status);
2300 ok(result == 0, "got %ld\n", result);
2302 /* lock in non-blocking mode */
2303 result = 0;
2304 magic = 0;
2305 status = LdrLockLoaderLock(0x2, &result, &magic);
2306 ok(status == STATUS_SUCCESS, "got 0x%08lx\n", status);
2307 ok(result == 1, "got %ld\n", result);
2308 ok(magic != 0, "got %Ix\n", magic);
2309 LdrUnlockLoaderLock(0, magic);
2312 static void test_RtlCompressBuffer(void)
2314 ULONG compress_workspace, decompress_workspace;
2315 static UCHAR test_buffer[] = "WineWineWine";
2316 static UCHAR buf1[0x1000], buf2[0x1000];
2317 ULONG final_size, buf_size;
2318 UCHAR *workspace = NULL;
2319 NTSTATUS status;
2321 compress_workspace = decompress_workspace = 0xdeadbeef;
2322 status = RtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1, &compress_workspace,
2323 &decompress_workspace);
2324 ok(status == STATUS_SUCCESS, "got wrong status 0x%08lx\n", status);
2325 ok(compress_workspace != 0, "got wrong compress_workspace %lu\n", compress_workspace);
2326 workspace = HeapAlloc(GetProcessHeap(), 0, compress_workspace);
2327 ok(workspace != NULL, "HeapAlloc failed %ld\n", GetLastError());
2329 /* test compression format / engine */
2330 final_size = 0xdeadbeef;
2331 status = RtlCompressBuffer(COMPRESSION_FORMAT_NONE, test_buffer, sizeof(test_buffer),
2332 buf1, sizeof(buf1) - 1, 4096, &final_size, workspace);
2333 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08lx\n", status);
2334 ok(final_size == 0xdeadbeef, "got wrong final_size %lu\n", final_size);
2336 final_size = 0xdeadbeef;
2337 status = RtlCompressBuffer(COMPRESSION_FORMAT_DEFAULT, test_buffer, sizeof(test_buffer),
2338 buf1, sizeof(buf1) - 1, 4096, &final_size, workspace);
2339 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08lx\n", status);
2340 ok(final_size == 0xdeadbeef, "got wrong final_size %lu\n", final_size);
2342 final_size = 0xdeadbeef;
2343 status = RtlCompressBuffer(0xFF, test_buffer, sizeof(test_buffer),
2344 buf1, sizeof(buf1) - 1, 4096, &final_size, workspace);
2345 ok(status == STATUS_UNSUPPORTED_COMPRESSION, "got wrong status 0x%08lx\n", status);
2346 ok(final_size == 0xdeadbeef, "got wrong final_size %lu\n", final_size);
2348 /* test compression */
2349 final_size = 0xdeadbeef;
2350 memset(buf1, 0x11, sizeof(buf1));
2351 status = RtlCompressBuffer(COMPRESSION_FORMAT_LZNT1, test_buffer, sizeof(test_buffer),
2352 buf1, sizeof(buf1), 4096, &final_size, workspace);
2353 ok(status == STATUS_SUCCESS, "got wrong status 0x%08lx\n", status);
2354 ok((*(WORD *)buf1 & 0x7000) == 0x3000, "no chunk signature found %04x\n", *(WORD *)buf1);
2355 todo_wine
2356 ok(final_size < sizeof(test_buffer), "got wrong final_size %lu\n", final_size);
2358 /* test decompression */
2359 buf_size = final_size;
2360 final_size = 0xdeadbeef;
2361 memset(buf2, 0x11, sizeof(buf2));
2362 status = RtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf2, sizeof(buf2),
2363 buf1, buf_size, &final_size);
2364 ok(status == STATUS_SUCCESS, "got wrong status 0x%08lx\n", status);
2365 ok(final_size == sizeof(test_buffer), "got wrong final_size %lu\n", final_size);
2366 ok(!memcmp(buf2, test_buffer, sizeof(test_buffer)), "got wrong decoded data\n");
2367 ok(buf2[sizeof(test_buffer)] == 0x11, "too many bytes written\n");
2369 /* buffer too small */
2370 final_size = 0xdeadbeef;
2371 memset(buf1, 0x11, sizeof(buf1));
2372 status = RtlCompressBuffer(COMPRESSION_FORMAT_LZNT1, test_buffer, sizeof(test_buffer),
2373 buf1, 4, 4096, &final_size, workspace);
2374 ok(status == STATUS_BUFFER_TOO_SMALL, "got wrong status 0x%08lx\n", status);
2376 HeapFree(GetProcessHeap(), 0, workspace);
2379 static void test_RtlGetCompressionWorkSpaceSize(void)
2381 ULONG compress_workspace, decompress_workspace;
2382 NTSTATUS status;
2384 /* test invalid format / engine */
2385 status = RtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_NONE, &compress_workspace,
2386 &decompress_workspace);
2387 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08lx\n", status);
2389 status = RtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_DEFAULT, &compress_workspace,
2390 &decompress_workspace);
2391 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08lx\n", status);
2393 status = RtlGetCompressionWorkSpaceSize(0xFF, &compress_workspace, &decompress_workspace);
2394 ok(status == STATUS_UNSUPPORTED_COMPRESSION, "got wrong status 0x%08lx\n", status);
2396 /* test LZNT1 with normal and maximum compression */
2397 compress_workspace = decompress_workspace = 0xdeadbeef;
2398 status = RtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1, &compress_workspace,
2399 &decompress_workspace);
2400 ok(status == STATUS_SUCCESS, "got wrong status 0x%08lx\n", status);
2401 ok(compress_workspace != 0, "got wrong compress_workspace %lu\n", compress_workspace);
2402 ok(decompress_workspace == 0x1000, "got wrong decompress_workspace %lu\n", decompress_workspace);
2404 compress_workspace = decompress_workspace = 0xdeadbeef;
2405 status = RtlGetCompressionWorkSpaceSize(COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_MAXIMUM,
2406 &compress_workspace, &decompress_workspace);
2407 ok(status == STATUS_SUCCESS, "got wrong status 0x%08lx\n", status);
2408 ok(compress_workspace != 0, "got wrong compress_workspace %lu\n", compress_workspace);
2409 ok(decompress_workspace == 0x1000, "got wrong decompress_workspace %lu\n", decompress_workspace);
2412 /* helper for test_RtlDecompressBuffer, checks if a chunk is incomplete */
2413 static BOOL is_incomplete_chunk(const UCHAR *compressed, ULONG compressed_size, BOOL check_all)
2415 ULONG chunk_size;
2417 if (compressed_size <= sizeof(WORD))
2418 return TRUE;
2420 while (compressed_size >= sizeof(WORD))
2422 chunk_size = (*(WORD *)compressed & 0xFFF) + 1;
2423 if (compressed_size < sizeof(WORD) + chunk_size)
2424 return TRUE;
2425 if (!check_all)
2426 break;
2427 compressed += sizeof(WORD) + chunk_size;
2428 compressed_size -= sizeof(WORD) + chunk_size;
2431 return FALSE;
2434 #define DECOMPRESS_BROKEN_FRAGMENT 1 /* < Win 7 */
2435 #define DECOMPRESS_BROKEN_TRUNCATED 2 /* broken on all machines */
2437 static void test_RtlDecompressBuffer(void)
2439 static struct
2441 UCHAR compressed[32];
2442 ULONG compressed_size;
2443 NTSTATUS status;
2444 UCHAR uncompressed[32];
2445 ULONG uncompressed_size;
2446 DWORD broken_flags;
2448 test_lznt[] =
2450 /* 4 byte uncompressed chunk */
2452 {0x03, 0x30, 'W', 'i', 'n', 'e'},
2454 STATUS_SUCCESS,
2455 "Wine",
2457 DECOMPRESS_BROKEN_FRAGMENT
2459 /* 8 byte uncompressed chunk */
2461 {0x07, 0x30, 'W', 'i', 'n', 'e', 'W', 'i', 'n', 'e'},
2463 STATUS_SUCCESS,
2464 "WineWine",
2466 DECOMPRESS_BROKEN_FRAGMENT
2468 /* 4 byte compressed chunk */
2470 {0x04, 0xB0, 0x00, 'W', 'i', 'n', 'e'},
2472 STATUS_SUCCESS,
2473 "Wine",
2476 /* 8 byte compressed chunk */
2478 {0x08, 0xB0, 0x00, 'W', 'i', 'n', 'e', 'W', 'i', 'n', 'e'},
2480 STATUS_SUCCESS,
2481 "WineWine",
2484 /* compressed chunk using backwards reference */
2486 {0x06, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x01, 0x30},
2488 STATUS_SUCCESS,
2489 "WineWine",
2491 DECOMPRESS_BROKEN_TRUNCATED
2493 /* compressed chunk using backwards reference with length > bytes_read */
2495 {0x06, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x05, 0x30},
2497 STATUS_SUCCESS,
2498 "WineWineWine",
2500 DECOMPRESS_BROKEN_TRUNCATED
2502 /* same as above, but unused bits != 0 */
2504 {0x06, 0xB0, 0x30, 'W', 'i', 'n', 'e', 0x01, 0x30},
2506 STATUS_SUCCESS,
2507 "WineWine",
2509 DECOMPRESS_BROKEN_TRUNCATED
2511 /* compressed chunk without backwards reference and unused bits != 0 */
2513 {0x01, 0xB0, 0x02, 'W'},
2515 STATUS_SUCCESS,
2516 "W",
2519 /* termination sequence after first chunk */
2521 {0x03, 0x30, 'W', 'i', 'n', 'e', 0x00, 0x00, 0x03, 0x30, 'W', 'i', 'n', 'e'},
2523 STATUS_SUCCESS,
2524 "Wine",
2526 DECOMPRESS_BROKEN_FRAGMENT
2528 /* compressed chunk using backwards reference with 4 bit offset, 12 bit length */
2530 {0x14, 0xB0, 0x00, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
2531 0x00, 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
2532 0x01, 0x01, 0xF0},
2534 STATUS_SUCCESS,
2535 "ABCDEFGHIJKLMNOPABCD",
2537 DECOMPRESS_BROKEN_TRUNCATED
2539 /* compressed chunk using backwards reference with 5 bit offset, 11 bit length */
2541 {0x15, 0xB0, 0x00, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
2542 0x00, 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
2543 0x02, 'A', 0x00, 0x78},
2545 STATUS_SUCCESS,
2546 "ABCDEFGHIJKLMNOPABCD",
2548 DECOMPRESS_BROKEN_TRUNCATED
2550 /* uncompressed chunk with invalid magic */
2552 {0x03, 0x20, 'W', 'i', 'n', 'e'},
2554 STATUS_SUCCESS,
2555 "Wine",
2557 DECOMPRESS_BROKEN_FRAGMENT
2559 /* compressed chunk with invalid magic */
2561 {0x04, 0xA0, 0x00, 'W', 'i', 'n', 'e'},
2563 STATUS_SUCCESS,
2564 "Wine",
2567 /* garbage byte after end of buffer */
2569 {0x00, 0xB0, 0x02, 0x01},
2571 STATUS_SUCCESS,
2575 /* empty compressed chunk */
2577 {0x00, 0xB0, 0x00},
2579 STATUS_SUCCESS,
2583 /* empty compressed chunk with unused bits != 0 */
2585 {0x00, 0xB0, 0x01},
2587 STATUS_SUCCESS,
2591 /* empty input buffer */
2595 STATUS_BAD_COMPRESSION_BUFFER,
2597 /* incomplete chunk header */
2599 {0x01},
2601 STATUS_BAD_COMPRESSION_BUFFER
2603 /* incomplete chunk header */
2605 {0x00, 0x30},
2607 STATUS_BAD_COMPRESSION_BUFFER
2609 /* compressed chunk with invalid backwards reference */
2611 {0x06, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x05, 0x40},
2613 STATUS_BAD_COMPRESSION_BUFFER
2615 /* compressed chunk with incomplete backwards reference */
2617 {0x05, 0xB0, 0x10, 'W', 'i', 'n', 'e', 0x05},
2619 STATUS_BAD_COMPRESSION_BUFFER
2621 /* incomplete uncompressed chunk */
2623 {0x07, 0x30, 'W', 'i', 'n', 'e'},
2625 STATUS_BAD_COMPRESSION_BUFFER
2627 /* incomplete compressed chunk */
2629 {0x08, 0xB0, 0x00, 'W', 'i', 'n', 'e'},
2631 STATUS_BAD_COMPRESSION_BUFFER
2633 /* two compressed chunks, the second one incomplete */
2635 {0x00, 0xB0, 0x02, 0x00, 0xB0},
2637 STATUS_BAD_COMPRESSION_BUFFER,
2641 static UCHAR buf[0x2000], workspace[0x1000];
2642 NTSTATUS status, expected_status;
2643 ULONG final_size;
2644 int i;
2646 /* test compression format / engine */
2647 final_size = 0xdeadbeef;
2648 status = RtlDecompressBuffer(COMPRESSION_FORMAT_NONE, buf, sizeof(buf), test_lznt[0].compressed,
2649 test_lznt[0].compressed_size, &final_size);
2650 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08lx\n", status);
2651 ok(final_size == 0xdeadbeef, "got wrong final_size %lu\n", final_size);
2653 final_size = 0xdeadbeef;
2654 status = RtlDecompressBuffer(COMPRESSION_FORMAT_DEFAULT, buf, sizeof(buf), test_lznt[0].compressed,
2655 test_lznt[0].compressed_size, &final_size);
2656 ok(status == STATUS_INVALID_PARAMETER, "got wrong status 0x%08lx\n", status);
2657 ok(final_size == 0xdeadbeef, "got wrong final_size %lu\n", final_size);
2659 final_size = 0xdeadbeef;
2660 status = RtlDecompressBuffer(0xFF, buf, sizeof(buf), test_lznt[0].compressed,
2661 test_lznt[0].compressed_size, &final_size);
2662 ok(status == STATUS_UNSUPPORTED_COMPRESSION, "got wrong status 0x%08lx\n", status);
2663 ok(final_size == 0xdeadbeef, "got wrong final_size %lu\n", final_size);
2665 /* regular tests for RtlDecompressBuffer */
2666 for (i = 0; i < ARRAY_SIZE(test_lznt); i++)
2668 trace("Running test %d (compressed_size=%lu, uncompressed_size=%lu, status=0x%08lx)\n",
2669 i, test_lznt[i].compressed_size, test_lznt[i].uncompressed_size, test_lznt[i].status);
2671 /* test with very big buffer */
2672 final_size = 0xdeadbeef;
2673 memset(buf, 0x11, sizeof(buf));
2674 status = RtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
2675 test_lznt[i].compressed_size, &final_size);
2676 ok(status == test_lznt[i].status || broken(status == STATUS_BAD_COMPRESSION_BUFFER &&
2677 (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)), "%d: got wrong status 0x%08lx\n", i, status);
2678 if (!status)
2680 ok(final_size == test_lznt[i].uncompressed_size,
2681 "%d: got wrong final_size %lu\n", i, final_size);
2682 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
2683 "%d: got wrong decoded data\n", i);
2684 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
2685 "%d: buf[%lu] was modified\n", i, test_lznt[i].uncompressed_size);
2688 /* test that modifier for compression engine is ignored */
2689 final_size = 0xdeadbeef;
2690 memset(buf, 0x11, sizeof(buf));
2691 status = RtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_MAXIMUM, buf, sizeof(buf),
2692 test_lznt[i].compressed, test_lznt[i].compressed_size, &final_size);
2693 ok(status == test_lznt[i].status || broken(status == STATUS_BAD_COMPRESSION_BUFFER &&
2694 (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)), "%d: got wrong status 0x%08lx\n", i, status);
2695 if (!status)
2697 ok(final_size == test_lznt[i].uncompressed_size,
2698 "%d: got wrong final_size %lu\n", i, final_size);
2699 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
2700 "%d: got wrong decoded data\n", i);
2701 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
2702 "%d: buf[%lu] was modified\n", i, test_lznt[i].uncompressed_size);
2705 /* test with expected output size */
2706 if (test_lznt[i].uncompressed_size > 0)
2708 final_size = 0xdeadbeef;
2709 memset(buf, 0x11, sizeof(buf));
2710 status = RtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, test_lznt[i].uncompressed_size,
2711 test_lznt[i].compressed, test_lznt[i].compressed_size, &final_size);
2712 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08lx\n", i, status);
2713 if (!status)
2715 ok(final_size == test_lznt[i].uncompressed_size,
2716 "%d: got wrong final_size %lu\n", i, final_size);
2717 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
2718 "%d: got wrong decoded data\n", i);
2719 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
2720 "%d: buf[%lu] was modified\n", i, test_lznt[i].uncompressed_size);
2724 /* test with smaller output size */
2725 if (test_lznt[i].uncompressed_size > 1)
2727 final_size = 0xdeadbeef;
2728 memset(buf, 0x11, sizeof(buf));
2729 status = RtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, test_lznt[i].uncompressed_size - 1,
2730 test_lznt[i].compressed, test_lznt[i].compressed_size, &final_size);
2731 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_TRUNCATED)
2732 todo_wine
2733 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08lx\n", i, status);
2734 else
2735 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08lx\n", i, status);
2736 if (!status)
2738 ok(final_size == test_lznt[i].uncompressed_size - 1,
2739 "%d: got wrong final_size %lu\n", i, final_size);
2740 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size - 1),
2741 "%d: got wrong decoded data\n", i);
2742 ok(buf[test_lznt[i].uncompressed_size - 1] == 0x11,
2743 "%d: buf[%lu] was modified\n", i, test_lznt[i].uncompressed_size - 1);
2747 /* test with zero output size */
2748 final_size = 0xdeadbeef;
2749 memset(buf, 0x11, sizeof(buf));
2750 status = RtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, buf, 0, test_lznt[i].compressed,
2751 test_lznt[i].compressed_size, &final_size);
2752 if (is_incomplete_chunk(test_lznt[i].compressed, test_lznt[i].compressed_size, FALSE))
2753 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08lx\n", i, status);
2754 else
2756 ok(status == STATUS_SUCCESS, "%d: got wrong status 0x%08lx\n", i, status);
2757 ok(final_size == 0, "%d: got wrong final_size %lu\n", i, final_size);
2758 ok(buf[0] == 0x11, "%d: buf[0] was modified\n", i);
2761 /* test RtlDecompressFragment with offset = 0 */
2762 final_size = 0xdeadbeef;
2763 memset(buf, 0x11, sizeof(buf));
2764 status = RtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
2765 test_lznt[i].compressed_size, 0, &final_size, workspace);
2766 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)
2767 todo_wine
2768 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08lx\n", i, status);
2769 else
2770 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08lx\n", i, status);
2771 if (!status)
2773 ok(final_size == test_lznt[i].uncompressed_size,
2774 "%d: got wrong final_size %lu\n", i, final_size);
2775 ok(!memcmp(buf, test_lznt[i].uncompressed, test_lznt[i].uncompressed_size),
2776 "%d: got wrong decoded data\n", i);
2777 ok(buf[test_lznt[i].uncompressed_size] == 0x11,
2778 "%d: buf[%lu] was modified\n", i, test_lznt[i].uncompressed_size);
2781 /* test RtlDecompressFragment with offset = 1 */
2782 final_size = 0xdeadbeef;
2783 memset(buf, 0x11, sizeof(buf));
2784 status = RtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
2785 test_lznt[i].compressed_size, 1, &final_size, workspace);
2786 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)
2787 todo_wine
2788 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08lx\n", i, status);
2789 else
2790 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08lx\n", i, status);
2791 if (!status)
2793 if (test_lznt[i].uncompressed_size == 0)
2795 todo_wine
2796 ok(final_size == 4095, "%d: got wrong final_size %lu\n", i, final_size);
2797 /* Buffer doesn't contain any useful value on Windows */
2798 ok(buf[4095] == 0x11, "%d: buf[4095] was modified\n", i);
2800 else
2802 ok(final_size == test_lznt[i].uncompressed_size - 1,
2803 "%d: got wrong final_size %lu\n", i, final_size);
2804 ok(!memcmp(buf, test_lznt[i].uncompressed + 1, test_lznt[i].uncompressed_size - 1),
2805 "%d: got wrong decoded data\n", i);
2806 ok(buf[test_lznt[i].uncompressed_size - 1] == 0x11,
2807 "%d: buf[%lu] was modified\n", i, test_lznt[i].uncompressed_size - 1);
2811 /* test RtlDecompressFragment with offset = 4095 */
2812 final_size = 0xdeadbeef;
2813 memset(buf, 0x11, sizeof(buf));
2814 status = RtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
2815 test_lznt[i].compressed_size, 4095, &final_size, workspace);
2816 if (test_lznt[i].broken_flags & DECOMPRESS_BROKEN_FRAGMENT)
2817 todo_wine
2818 ok(status == STATUS_BAD_COMPRESSION_BUFFER, "%d: got wrong status 0x%08lx\n", i, status);
2819 else
2820 ok(status == test_lznt[i].status, "%d: got wrong status 0x%08lx\n", i, status);
2821 if (!status)
2823 todo_wine
2824 ok(final_size == 1, "%d: got wrong final_size %lu\n", i, final_size);
2825 todo_wine
2826 ok(buf[0] == 0, "%d: padding is not zero\n", i);
2827 ok(buf[1] == 0x11, "%d: buf[1] was modified\n", i);
2830 /* test RtlDecompressFragment with offset = 4096 */
2831 final_size = 0xdeadbeef;
2832 memset(buf, 0x11, sizeof(buf));
2833 status = RtlDecompressFragment(COMPRESSION_FORMAT_LZNT1, buf, sizeof(buf), test_lznt[i].compressed,
2834 test_lznt[i].compressed_size, 4096, &final_size, workspace);
2835 expected_status = is_incomplete_chunk(test_lznt[i].compressed, test_lznt[i].compressed_size, TRUE) ?
2836 test_lznt[i].status : STATUS_SUCCESS;
2837 ok(status == expected_status, "%d: got wrong status 0x%08lx, expected 0x%08lx\n", i, status, expected_status);
2838 if (!status)
2840 ok(final_size == 0, "%d: got wrong final_size %lu\n", i, final_size);
2841 ok(buf[0] == 0x11, "%d: buf[4096] was modified\n", i);
2846 #undef DECOMPRESS_BROKEN_FRAGMENT
2847 #undef DECOMPRESS_BROKEN_TRUNCATED
2849 struct critsect_locked_info
2851 CRITICAL_SECTION crit;
2852 HANDLE semaphores[2];
2855 static DWORD WINAPI critsect_locked_thread(void *param)
2857 struct critsect_locked_info *info = param;
2858 DWORD ret;
2860 ret = pRtlIsCriticalSectionLocked(&info->crit);
2861 ok(ret == TRUE, "expected TRUE, got %lu\n", ret);
2862 ret = pRtlIsCriticalSectionLockedByThread(&info->crit);
2863 ok(ret == FALSE, "expected FALSE, got %lu\n", ret);
2865 ReleaseSemaphore(info->semaphores[0], 1, NULL);
2866 ret = WaitForSingleObject(info->semaphores[1], 1000);
2867 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %lu\n", ret);
2869 ret = pRtlIsCriticalSectionLocked(&info->crit);
2870 ok(ret == FALSE, "expected FALSE, got %lu\n", ret);
2871 ret = pRtlIsCriticalSectionLockedByThread(&info->crit);
2872 ok(ret == FALSE, "expected FALSE, got %lu\n", ret);
2874 EnterCriticalSection(&info->crit);
2876 ret = pRtlIsCriticalSectionLocked(&info->crit);
2877 ok(ret == TRUE, "expected TRUE, got %lu\n", ret);
2878 ret = pRtlIsCriticalSectionLockedByThread(&info->crit);
2879 ok(ret == TRUE, "expected TRUE, got %lu\n", ret);
2881 ReleaseSemaphore(info->semaphores[0], 1, NULL);
2882 ret = WaitForSingleObject(info->semaphores[1], 1000);
2883 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %lu\n", ret);
2885 LeaveCriticalSection(&info->crit);
2886 return 0;
2889 static void test_RtlIsCriticalSectionLocked(void)
2891 struct critsect_locked_info info;
2892 HANDLE thread;
2893 BOOL ret;
2895 if (!pRtlIsCriticalSectionLocked || !pRtlIsCriticalSectionLockedByThread)
2897 win_skip("skipping RtlIsCriticalSectionLocked tests, required functions not available\n");
2898 return;
2901 InitializeCriticalSection(&info.crit);
2902 info.semaphores[0] = CreateSemaphoreW(NULL, 0, 1, NULL);
2903 ok(info.semaphores[0] != NULL, "CreateSemaphore failed with %lu\n", GetLastError());
2904 info.semaphores[1] = CreateSemaphoreW(NULL, 0, 1, NULL);
2905 ok(info.semaphores[1] != NULL, "CreateSemaphore failed with %lu\n", GetLastError());
2907 ret = pRtlIsCriticalSectionLocked(&info.crit);
2908 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
2909 ret = pRtlIsCriticalSectionLockedByThread(&info.crit);
2910 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
2912 EnterCriticalSection(&info.crit);
2914 ret = pRtlIsCriticalSectionLocked(&info.crit);
2915 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
2916 ret = pRtlIsCriticalSectionLockedByThread(&info.crit);
2917 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
2919 thread = CreateThread(NULL, 0, critsect_locked_thread, &info, 0, NULL);
2920 ok(thread != NULL, "CreateThread failed with %lu\n", GetLastError());
2921 ret = WaitForSingleObject(info.semaphores[0], 1000);
2922 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
2924 LeaveCriticalSection(&info.crit);
2926 ReleaseSemaphore(info.semaphores[1], 1, NULL);
2927 ret = WaitForSingleObject(info.semaphores[0], 1000);
2928 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
2930 ret = pRtlIsCriticalSectionLocked(&info.crit);
2931 ok(ret == TRUE, "expected TRUE, got %u\n", ret);
2932 ret = pRtlIsCriticalSectionLockedByThread(&info.crit);
2933 ok(ret == FALSE, "expected FALSE, got %u\n", ret);
2935 ReleaseSemaphore(info.semaphores[1], 1, NULL);
2936 ret = WaitForSingleObject(thread, 1000);
2937 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %u\n", ret);
2939 CloseHandle(thread);
2940 CloseHandle(info.semaphores[0]);
2941 CloseHandle(info.semaphores[1]);
2942 DeleteCriticalSection(&info.crit);
2945 static void test_RtlInitializeCriticalSectionEx(void)
2947 static const CRITICAL_SECTION_DEBUG *no_debug = (void *)~(ULONG_PTR)0;
2948 CRITICAL_SECTION cs;
2950 if (!pRtlInitializeCriticalSectionEx)
2952 win_skip("RtlInitializeCriticalSectionEx is not available\n");
2953 return;
2956 memset(&cs, 0x11, sizeof(cs));
2957 pRtlInitializeCriticalSectionEx(&cs, 0, 0);
2958 ok((cs.DebugInfo != NULL && cs.DebugInfo != no_debug) || broken(cs.DebugInfo == no_debug) /* >= Win 8 */,
2959 "expected DebugInfo != NULL and DebugInfo != ~0, got %p\n", cs.DebugInfo);
2960 ok(cs.LockCount == -1, "expected LockCount == -1, got %ld\n", cs.LockCount);
2961 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %ld\n", cs.RecursionCount);
2962 ok(cs.LockSemaphore == NULL, "expected LockSemaphore == NULL, got %p\n", cs.LockSemaphore);
2963 ok(cs.SpinCount == 0 || broken(cs.SpinCount != 0) /* >= Win 8 */,
2964 "expected SpinCount == 0, got %Id\n", cs.SpinCount);
2965 RtlDeleteCriticalSection(&cs);
2967 memset(&cs, 0x11, sizeof(cs));
2968 pRtlInitializeCriticalSectionEx(&cs, 0, RTL_CRITICAL_SECTION_FLAG_NO_DEBUG_INFO);
2969 ok(cs.DebugInfo == no_debug, "expected DebugInfo == ~0, got %p\n", cs.DebugInfo);
2970 ok(cs.LockCount == -1, "expected LockCount == -1, got %ld\n", cs.LockCount);
2971 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %ld\n", cs.RecursionCount);
2972 ok(cs.LockSemaphore == NULL, "expected LockSemaphore == NULL, got %p\n", cs.LockSemaphore);
2973 ok(cs.SpinCount == 0 || broken(cs.SpinCount != 0) /* >= Win 8 */,
2974 "expected SpinCount == 0, got %Id\n", cs.SpinCount);
2975 RtlDeleteCriticalSection(&cs);
2978 static void test_RtlLeaveCriticalSection(void)
2980 RTL_CRITICAL_SECTION cs;
2981 NTSTATUS status;
2983 if (!pRtlInitializeCriticalSectionEx)
2984 return; /* Skip winxp */
2986 status = RtlInitializeCriticalSection(&cs);
2987 ok(!status, "RtlInitializeCriticalSection failed: %lx\n", status);
2989 status = RtlEnterCriticalSection(&cs);
2990 ok(!status, "RtlEnterCriticalSection failed: %lx\n", status);
2991 todo_wine
2992 ok(cs.LockCount == -2, "expected LockCount == -2, got %ld\n", cs.LockCount);
2993 ok(cs.RecursionCount == 1, "expected RecursionCount == 1, got %ld\n", cs.RecursionCount);
2994 ok(cs.OwningThread == ULongToHandle(GetCurrentThreadId()), "unexpected OwningThread\n");
2996 status = RtlLeaveCriticalSection(&cs);
2997 ok(!status, "RtlLeaveCriticalSection failed: %lx\n", status);
2998 ok(cs.LockCount == -1, "expected LockCount == -1, got %ld\n", cs.LockCount);
2999 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %ld\n", cs.RecursionCount);
3000 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
3003 * Trying to leave a section that wasn't acquired modifies RecursionCount to an invalid value,
3004 * but doesn't modify LockCount so that an attempt to enter the section later will work.
3006 status = RtlLeaveCriticalSection(&cs);
3007 ok(!status, "RtlLeaveCriticalSection failed: %lx\n", status);
3008 ok(cs.LockCount == -1, "expected LockCount == -1, got %ld\n", cs.LockCount);
3009 ok(cs.RecursionCount == -1, "expected RecursionCount == -1, got %ld\n", cs.RecursionCount);
3010 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
3012 /* and again */
3013 status = RtlLeaveCriticalSection(&cs);
3014 ok(!status, "RtlLeaveCriticalSection failed: %lx\n", status);
3015 ok(cs.LockCount == -1, "expected LockCount == -1, got %ld\n", cs.LockCount);
3016 ok(cs.RecursionCount == -2, "expected RecursionCount == -2, got %ld\n", cs.RecursionCount);
3017 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
3019 /* entering section fixes RecursionCount */
3020 status = RtlEnterCriticalSection(&cs);
3021 ok(!status, "RtlEnterCriticalSection failed: %lx\n", status);
3022 todo_wine
3023 ok(cs.LockCount == -2, "expected LockCount == -2, got %ld\n", cs.LockCount);
3024 ok(cs.RecursionCount == 1, "expected RecursionCount == 1, got %ld\n", cs.RecursionCount);
3025 ok(cs.OwningThread == ULongToHandle(GetCurrentThreadId()), "unexpected OwningThread\n");
3027 status = RtlLeaveCriticalSection(&cs);
3028 ok(!status, "RtlLeaveCriticalSection failed: %lx\n", status);
3029 ok(cs.LockCount == -1, "expected LockCount == -1, got %ld\n", cs.LockCount);
3030 ok(cs.RecursionCount == 0, "expected RecursionCount == 0, got %ld\n", cs.RecursionCount);
3031 ok(!cs.OwningThread, "unexpected OwningThread %p\n", cs.OwningThread);
3033 status = RtlDeleteCriticalSection(&cs);
3034 ok(!status, "RtlDeleteCriticalSection failed: %lx\n", status);
3037 struct ldr_enum_context
3039 BOOL abort;
3040 BOOL found;
3041 int count;
3044 static void WINAPI ldr_enum_callback(LDR_DATA_TABLE_ENTRY *module, void *context, BOOLEAN *stop)
3046 static const WCHAR ntdllW[] = {'n','t','d','l','l','.','d','l','l',0};
3047 struct ldr_enum_context *ctx = context;
3049 if (!lstrcmpiW(module->BaseDllName.Buffer, ntdllW))
3050 ctx->found = TRUE;
3052 ctx->count++;
3053 *stop = ctx->abort;
3056 static void test_LdrEnumerateLoadedModules(void)
3058 struct ldr_enum_context ctx;
3059 NTSTATUS status;
3061 if (!pLdrEnumerateLoadedModules)
3063 win_skip("LdrEnumerateLoadedModules not available\n");
3064 return;
3067 ctx.abort = FALSE;
3068 ctx.found = FALSE;
3069 ctx.count = 0;
3070 status = pLdrEnumerateLoadedModules(NULL, ldr_enum_callback, &ctx);
3071 ok(status == STATUS_SUCCESS, "LdrEnumerateLoadedModules failed with %08lx\n", status);
3072 ok(ctx.count > 1, "Expected more than one module, got %d\n", ctx.count);
3073 ok(ctx.found, "Could not find ntdll in list of modules\n");
3075 ctx.abort = TRUE;
3076 ctx.count = 0;
3077 status = pLdrEnumerateLoadedModules(NULL, ldr_enum_callback, &ctx);
3078 ok(status == STATUS_SUCCESS, "LdrEnumerateLoadedModules failed with %08lx\n", status);
3079 ok(ctx.count == 1, "Expected exactly one module, got %d\n", ctx.count);
3081 status = pLdrEnumerateLoadedModules((void *)0x1, ldr_enum_callback, (void *)0xdeadbeef);
3082 ok(status == STATUS_INVALID_PARAMETER, "expected STATUS_INVALID_PARAMETER, got 0x%08lx\n", status);
3084 status = pLdrEnumerateLoadedModules((void *)0xdeadbeef, ldr_enum_callback, (void *)0xdeadbeef);
3085 ok(status == STATUS_INVALID_PARAMETER, "expected STATUS_INVALID_PARAMETER, got 0x%08lx\n", status);
3087 status = pLdrEnumerateLoadedModules(NULL, NULL, (void *)0xdeadbeef);
3088 ok(status == STATUS_INVALID_PARAMETER, "expected STATUS_INVALID_PARAMETER, got 0x%08lx\n", status);
3091 static void test_RtlMakeSelfRelativeSD(void)
3093 char buf[sizeof(SECURITY_DESCRIPTOR_RELATIVE) + 4];
3094 SECURITY_DESCRIPTOR_RELATIVE *sd_rel = (SECURITY_DESCRIPTOR_RELATIVE *)buf;
3095 SECURITY_DESCRIPTOR sd;
3096 NTSTATUS status;
3097 DWORD len;
3099 memset( &sd, 0, sizeof(sd) );
3100 sd.Revision = SECURITY_DESCRIPTOR_REVISION;
3102 len = 0;
3103 status = RtlMakeSelfRelativeSD( &sd, NULL, &len );
3104 ok( status == STATUS_BUFFER_TOO_SMALL, "got %08lx\n", status );
3105 ok( len == sizeof(*sd_rel), "got %lu\n", len );
3107 len += 4;
3108 status = RtlMakeSelfRelativeSD( &sd, sd_rel, &len );
3109 ok( status == STATUS_SUCCESS, "got %08lx\n", status );
3110 ok( len == sizeof(*sd_rel) + 4, "got %lu\n", len );
3112 len = 0;
3113 status = RtlAbsoluteToSelfRelativeSD( &sd, NULL, &len );
3114 ok( status == STATUS_BUFFER_TOO_SMALL, "got %08lx\n", status );
3115 ok( len == sizeof(*sd_rel), "got %lu\n", len );
3117 len += 4;
3118 status = RtlAbsoluteToSelfRelativeSD( &sd, sd_rel, &len );
3119 ok( status == STATUS_SUCCESS, "got %08lx\n", status );
3120 ok( len == sizeof(*sd_rel) + 4, "got %lu\n", len );
3122 sd.Control = SE_SELF_RELATIVE;
3123 status = RtlMakeSelfRelativeSD( &sd, sd_rel, &len );
3124 ok( status == STATUS_SUCCESS, "got %08lx\n", status );
3125 ok( len == sizeof(*sd_rel) + 4, "got %lu\n", len );
3127 status = RtlAbsoluteToSelfRelativeSD( &sd, sd_rel, &len );
3128 ok( status == STATUS_BAD_DESCRIPTOR_FORMAT, "got %08lx\n", status );
3131 static DWORD (CALLBACK *orig_entry)(HMODULE,DWORD,LPVOID);
3132 static DWORD *dll_main_data;
3134 static inline void *get_rva( HMODULE module, DWORD va )
3136 return (void *)((char *)module + va);
3139 static void CALLBACK ldr_notify_callback1(ULONG reason, LDR_DLL_NOTIFICATION_DATA *data, void *context)
3141 const IMAGE_IMPORT_DESCRIPTOR *imports;
3142 const IMAGE_THUNK_DATA *import_list;
3143 IMAGE_THUNK_DATA *thunk_list;
3144 LDR_DATA_TABLE_ENTRY *mod;
3145 DWORD *calls = context;
3146 LIST_ENTRY *mark;
3147 ULONG size;
3148 int i, j;
3150 *calls <<= 4;
3151 *calls |= reason;
3153 if (!lstrcmpiW(data->Loaded.BaseDllName->Buffer, expected_dll))
3154 return;
3156 ok(data->Loaded.Flags == 0, "Expected flags 0, got %lx\n", data->Loaded.Flags);
3157 ok(!lstrcmpiW(data->Loaded.BaseDllName->Buffer, expected_dll), "Expected %s, got %s\n",
3158 wine_dbgstr_w(expected_dll), wine_dbgstr_w(data->Loaded.BaseDllName->Buffer));
3159 ok(!!data->Loaded.DllBase, "Expected non zero base address\n");
3160 ok(data->Loaded.SizeOfImage, "Expected non zero image size\n");
3162 /* expect module to be last module listed in LdrData load order list */
3163 mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
3164 mod = CONTAINING_RECORD(mark->Blink, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);
3165 ok(mod->DllBase == data->Loaded.DllBase, "Expected base address %p, got %p\n",
3166 data->Loaded.DllBase, mod->DllBase);
3167 ok(!lstrcmpiW(mod->BaseDllName.Buffer, expected_dll), "Expected %s, got %s\n",
3168 wine_dbgstr_w(expected_dll), wine_dbgstr_w(mod->BaseDllName.Buffer));
3170 /* show that imports have already been resolved */
3171 imports = RtlImageDirectoryEntryToData(data->Loaded.DllBase, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &size);
3172 ok(!!imports, "Expected dll to have imports\n");
3174 for (i = 0; imports[i].Name; i++)
3176 thunk_list = get_rva(data->Loaded.DllBase, (DWORD)imports[i].FirstThunk);
3177 if (imports[i].OriginalFirstThunk)
3178 import_list = get_rva(data->Loaded.DllBase, (DWORD)imports[i].OriginalFirstThunk);
3179 else
3180 import_list = thunk_list;
3182 for (j = 0; import_list[j].u1.Ordinal; j++)
3184 ok(thunk_list[j].u1.AddressOfData > data->Loaded.SizeOfImage,
3185 "Import has not been resolved: %p\n", (void*)thunk_list[j].u1.Function);
3190 static void CALLBACK ldr_notify_callback2(ULONG reason, LDR_DLL_NOTIFICATION_DATA *data, void *context)
3192 DWORD *calls = context;
3193 *calls <<= 4;
3194 *calls |= reason + 2;
3197 static BOOL WINAPI fake_dll_main(HINSTANCE instance, DWORD reason, void* reserved)
3199 if (reason == DLL_PROCESS_ATTACH)
3201 *dll_main_data <<= 4;
3202 *dll_main_data |= 3;
3204 else if (reason == DLL_PROCESS_DETACH)
3206 *dll_main_data <<= 4;
3207 *dll_main_data |= 4;
3209 return orig_entry(instance, reason, reserved);
3212 static void CALLBACK ldr_notify_callback_dll_main(ULONG reason, LDR_DLL_NOTIFICATION_DATA *data, void *context)
3214 DWORD *calls = context;
3215 LIST_ENTRY *mark;
3216 LDR_DATA_TABLE_ENTRY *mod;
3218 *calls <<= 4;
3219 *calls |= reason;
3221 if (reason != LDR_DLL_NOTIFICATION_REASON_LOADED)
3222 return;
3224 mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
3225 mod = CONTAINING_RECORD(mark->Blink, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);
3226 ok(mod->DllBase == data->Loaded.DllBase, "Expected base address %p, got %p\n",
3227 data->Loaded.DllBase, mod->DllBase);
3228 if (mod->DllBase != data->Loaded.DllBase)
3229 return;
3231 orig_entry = mod->EntryPoint;
3232 mod->EntryPoint = fake_dll_main;
3233 dll_main_data = calls;
3236 static BOOL WINAPI fake_dll_main_fail(HINSTANCE instance, DWORD reason, void* reserved)
3238 if (reason == DLL_PROCESS_ATTACH)
3240 *dll_main_data <<= 4;
3241 *dll_main_data |= 3;
3243 else if (reason == DLL_PROCESS_DETACH)
3245 *dll_main_data <<= 4;
3246 *dll_main_data |= 4;
3248 return FALSE;
3251 static void CALLBACK ldr_notify_callback_fail(ULONG reason, LDR_DLL_NOTIFICATION_DATA *data, void *context)
3253 DWORD *calls = context;
3254 LIST_ENTRY *mark;
3255 LDR_DATA_TABLE_ENTRY *mod;
3257 *calls <<= 4;
3258 *calls |= reason;
3260 if (reason != LDR_DLL_NOTIFICATION_REASON_LOADED)
3261 return;
3263 mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
3264 mod = CONTAINING_RECORD(mark->Blink, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);
3265 ok(mod->DllBase == data->Loaded.DllBase, "Expected base address %p, got %p\n",
3266 data->Loaded.DllBase, mod->DllBase);
3267 if (mod->DllBase != data->Loaded.DllBase)
3268 return;
3270 orig_entry = mod->EntryPoint;
3271 mod->EntryPoint = fake_dll_main_fail;
3272 dll_main_data = calls;
3275 static void CALLBACK ldr_notify_callback_imports(ULONG reason, LDR_DLL_NOTIFICATION_DATA *data, void *context)
3277 DWORD *calls = context;
3279 if (reason != LDR_DLL_NOTIFICATION_REASON_LOADED)
3280 return;
3282 if (!lstrcmpiW(data->Loaded.BaseDllName->Buffer, crypt32dllW))
3284 *calls <<= 4;
3285 *calls |= 1;
3288 if (!lstrcmpiW(data->Loaded.BaseDllName->Buffer, wintrustdllW))
3290 *calls <<= 4;
3291 *calls |= 2;
3295 static void test_LdrRegisterDllNotification(void)
3297 void *cookie, *cookie2;
3298 NTSTATUS status;
3299 HMODULE mod;
3300 DWORD calls;
3302 if (!pLdrRegisterDllNotification || !pLdrUnregisterDllNotification)
3304 win_skip("Ldr(Un)RegisterDllNotification not available\n");
3305 return;
3308 mod = LoadLibraryW(expected_dll);
3309 if(mod)
3310 FreeLibrary(mod);
3311 else
3312 expected_dll = ws2_32dllW; /* XP Default */
3314 /* generic test */
3315 status = pLdrRegisterDllNotification(0, ldr_notify_callback1, &calls, &cookie);
3316 ok(!status, "Expected STATUS_SUCCESS, got %08lx\n", status);
3318 calls = 0;
3319 mod = LoadLibraryW(expected_dll);
3320 ok(!!mod, "Failed to load library: %ld\n", GetLastError());
3321 ok(calls == LDR_DLL_NOTIFICATION_REASON_LOADED, "Expected LDR_DLL_NOTIFICATION_REASON_LOADED, got %lx\n", calls);
3323 calls = 0;
3324 FreeLibrary(mod);
3325 ok(calls == LDR_DLL_NOTIFICATION_REASON_UNLOADED, "Expected LDR_DLL_NOTIFICATION_REASON_UNLOADED, got %lx\n", calls);
3327 /* test order of callbacks */
3328 status = pLdrRegisterDllNotification(0, ldr_notify_callback2, &calls, &cookie2);
3329 ok(!status, "Expected STATUS_SUCCESS, got %08lx\n", status);
3331 calls = 0;
3332 mod = LoadLibraryW(expected_dll);
3333 ok(!!mod, "Failed to load library: %ld\n", GetLastError());
3334 ok(calls == 0x13, "Expected order 0x13, got %lx\n", calls);
3336 calls = 0;
3337 FreeLibrary(mod);
3338 ok(calls == 0x24, "Expected order 0x24, got %lx\n", calls);
3340 pLdrUnregisterDllNotification(cookie2);
3341 pLdrUnregisterDllNotification(cookie);
3343 /* test dll main order */
3344 status = pLdrRegisterDllNotification(0, ldr_notify_callback_dll_main, &calls, &cookie);
3345 ok(!status, "Expected STATUS_SUCCESS, got %08lx\n", status);
3347 calls = 0;
3348 mod = LoadLibraryW(expected_dll);
3349 ok(!!mod, "Failed to load library: %ld\n", GetLastError());
3350 ok(calls == 0x13, "Expected order 0x13, got %lx\n", calls);
3352 calls = 0;
3353 FreeLibrary(mod);
3354 ok(calls == 0x42, "Expected order 0x42, got %lx\n", calls);
3356 pLdrUnregisterDllNotification(cookie);
3358 /* test dll main order */
3359 status = pLdrRegisterDllNotification(0, ldr_notify_callback_fail, &calls, &cookie);
3360 ok(!status, "Expected STATUS_SUCCESS, got %08lx\n", status);
3362 calls = 0;
3363 mod = LoadLibraryW(expected_dll);
3364 ok(!mod, "Expected library to fail loading\n");
3365 ok(calls == 0x1342, "Expected order 0x1342, got %lx\n", calls);
3367 pLdrUnregisterDllNotification(cookie);
3369 /* test dll with dependencies */
3370 status = pLdrRegisterDllNotification(0, ldr_notify_callback_imports, &calls, &cookie);
3371 ok(!status, "Expected STATUS_SUCCESS, got %08lx\n", status);
3373 calls = 0;
3374 mod = LoadLibraryW(wintrustdllW);
3375 ok(!!mod, "Failed to load library: %ld\n", GetLastError());
3376 ok(calls == 0x12 || calls == 0x21, "got %lx\n", calls);
3378 FreeLibrary(mod);
3379 pLdrUnregisterDllNotification(cookie);
3382 static BOOL test_dbg_print_except;
3383 static LONG test_dbg_print_except_ret;
3385 static LONG CALLBACK test_dbg_print_except_handler( EXCEPTION_POINTERS *eptrs )
3387 if (eptrs->ExceptionRecord->ExceptionCode == DBG_PRINTEXCEPTION_C)
3389 ok( eptrs->ExceptionRecord->NumberParameters == 2,
3390 "Unexpected NumberParameters: %ld\n", eptrs->ExceptionRecord->NumberParameters );
3391 ok( eptrs->ExceptionRecord->ExceptionInformation[0] == strlen("test_DbgPrint: Hello World") + 1,
3392 "Unexpected ExceptionInformation[0]: %d\n", (int)eptrs->ExceptionRecord->ExceptionInformation[0] );
3393 ok( !strcmp((char *)eptrs->ExceptionRecord->ExceptionInformation[1], "test_DbgPrint: Hello World"),
3394 "Unexpected ExceptionInformation[1]: %s\n", wine_dbgstr_a((char *)eptrs->ExceptionRecord->ExceptionInformation[1]) );
3395 test_dbg_print_except = TRUE;
3396 return test_dbg_print_except_ret;
3399 return (LONG)EXCEPTION_CONTINUE_SEARCH;
3402 static NTSTATUS WINAPIV test_vDbgPrintEx( ULONG id, ULONG level, const char *fmt, ... )
3404 NTSTATUS status;
3405 va_list args;
3406 va_start( args, fmt );
3407 status = vDbgPrintEx( id, level, fmt, args );
3408 va_end( args );
3409 return status;
3412 static NTSTATUS WINAPIV test_vDbgPrintExWithPrefix( const char *prefix, ULONG id, ULONG level, const char *fmt, ... )
3414 NTSTATUS status;
3415 va_list args;
3416 va_start( args, fmt );
3417 status = vDbgPrintExWithPrefix( prefix, id, level, fmt, args );
3418 va_end( args );
3419 return status;
3422 static void test_DbgPrint(void)
3424 NTSTATUS status;
3425 void *handler = RtlAddVectoredExceptionHandler( TRUE, test_dbg_print_except_handler );
3426 PEB *Peb = NtCurrentTeb()->Peb;
3427 BOOL debugged = Peb->BeingDebugged;
3429 test_dbg_print_except = FALSE;
3430 test_dbg_print_except_ret = (LONG)EXCEPTION_EXECUTE_HANDLER;
3431 status = DbgPrint( "test_DbgPrint: %s", "Hello World" );
3432 ok( !status, "DbgPrint returned %lx\n", status );
3433 ok( !test_dbg_print_except, "DBG_PRINTEXCEPTION_C received\n" );
3435 Peb->BeingDebugged = TRUE;
3436 test_dbg_print_except = FALSE;
3437 test_dbg_print_except_ret = (LONG)EXCEPTION_EXECUTE_HANDLER;
3438 status = DbgPrint( "test_DbgPrint: %s", "Hello World" );
3439 ok( !status, "DbgPrint returned %lx\n", status );
3440 ok( test_dbg_print_except, "DBG_PRINTEXCEPTION_C not received\n" );
3442 test_dbg_print_except = FALSE;
3443 test_dbg_print_except_ret = (LONG)EXCEPTION_CONTINUE_EXECUTION;
3444 status = DbgPrint( "test_DbgPrint: %s", "Hello World" );
3445 ok( !status, "DbgPrint returned %lx\n", status );
3446 ok( test_dbg_print_except, "DBG_PRINTEXCEPTION_C not received\n" );
3448 test_dbg_print_except = FALSE;
3449 test_dbg_print_except_ret = (LONG)EXCEPTION_CONTINUE_SEARCH;
3450 status = DbgPrint( "test_DbgPrint: %s", "Hello World" );
3451 ok( !status, "DbgPrint returned %lx\n", status );
3452 ok( test_dbg_print_except, "DBG_PRINTEXCEPTION_C not received\n" );
3455 /* FIXME: NtSetDebugFilterState / DbgSetDebugFilterState are probably what's controlling these */
3457 test_dbg_print_except = FALSE;
3458 test_dbg_print_except_ret = (LONG)EXCEPTION_EXECUTE_HANDLER;
3459 status = DbgPrintEx( 0, DPFLTR_ERROR_LEVEL, "test_DbgPrint: %s", "Hello World" );
3460 ok( !status, "DbgPrintEx returned %lx\n", status );
3461 ok( test_dbg_print_except, "DBG_PRINTEXCEPTION_C not received\n" );
3463 test_dbg_print_except = FALSE;
3464 test_dbg_print_except_ret = (LONG)EXCEPTION_EXECUTE_HANDLER;
3465 status = DbgPrintEx( 0, DPFLTR_WARNING_LEVEL, "test_DbgPrint: %s", "Hello World" );
3466 ok( !status, "DbgPrintEx returned %lx\n", status );
3467 ok( !test_dbg_print_except, "DBG_PRINTEXCEPTION_C not received\n" );
3469 test_dbg_print_except = FALSE;
3470 test_dbg_print_except_ret = (LONG)EXCEPTION_EXECUTE_HANDLER;
3471 status = DbgPrintEx( 0, DPFLTR_MASK|(1 << DPFLTR_ERROR_LEVEL), "test_DbgPrint: %s", "Hello World" );
3472 ok( !status, "DbgPrintEx returned %lx\n", status );
3473 ok( test_dbg_print_except, "DBG_PRINTEXCEPTION_C not received\n" );
3475 test_dbg_print_except = FALSE;
3476 test_dbg_print_except_ret = (LONG)EXCEPTION_EXECUTE_HANDLER;
3477 status = DbgPrintEx( 0, DPFLTR_MASK|(1 << DPFLTR_WARNING_LEVEL), "test_DbgPrint: %s", "Hello World" );
3478 ok( !status, "DbgPrintEx returned %lx\n", status );
3479 ok( !test_dbg_print_except, "DBG_PRINTEXCEPTION_C not received\n" );
3482 test_dbg_print_except = FALSE;
3483 test_dbg_print_except_ret = (LONG)EXCEPTION_EXECUTE_HANDLER;
3484 status = test_vDbgPrintEx( 0, 0xFFFFFFFF, "test_DbgPrint: %s", "Hello World" );
3485 ok( !status, "vDbgPrintEx returned %lx\n", status );
3486 ok( test_dbg_print_except, "DBG_PRINTEXCEPTION_C not received\n" );
3488 test_dbg_print_except = FALSE;
3489 test_dbg_print_except_ret = (LONG)EXCEPTION_EXECUTE_HANDLER;
3490 status = test_vDbgPrintExWithPrefix( "test_", 0, 0xFFFFFFFF, "DbgPrint: %s", "Hello World" );
3491 ok( !status, "vDbgPrintExWithPrefix returned %lx\n", status );
3492 ok( test_dbg_print_except, "DBG_PRINTEXCEPTION_C not received\n" );
3494 Peb->BeingDebugged = debugged;
3495 RtlRemoveVectoredExceptionHandler( handler );
3498 static BOOL test_heap_destroy_dbgstr = FALSE;
3499 static BOOL test_heap_destroy_break = FALSE;
3501 static LONG CALLBACK test_heap_destroy_except_handler( EXCEPTION_POINTERS *eptrs )
3503 if (eptrs->ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT)
3505 #if defined( __i386__ )
3506 eptrs->ContextRecord->Eip += 1;
3507 test_heap_destroy_break = TRUE;
3508 return (LONG)EXCEPTION_CONTINUE_EXECUTION;
3509 #elif defined( __x86_64__ )
3510 eptrs->ContextRecord->Rip += 1;
3511 test_heap_destroy_break = TRUE;
3512 return (LONG)EXCEPTION_CONTINUE_EXECUTION;
3513 #endif
3516 if (eptrs->ExceptionRecord->ExceptionCode == DBG_PRINTEXCEPTION_C)
3518 test_heap_destroy_dbgstr = TRUE;
3519 return (LONG)EXCEPTION_CONTINUE_EXECUTION;
3522 return (LONG)EXCEPTION_CONTINUE_SEARCH;
3525 /* partially copied from ntdll/heap.c */
3526 #define HEAP_VALIDATE_PARAMS 0x40000000
3528 struct heap
3530 DWORD_PTR unknown1[2];
3531 DWORD unknown2[2];
3532 DWORD_PTR unknown3[4];
3533 DWORD unknown4;
3534 DWORD_PTR unknown5[2];
3535 DWORD unknown6[3];
3536 DWORD_PTR unknown7[2];
3537 DWORD flags;
3538 DWORD force_flags;
3539 DWORD_PTR unknown8[6];
3542 static void test_RtlDestroyHeap(void)
3544 const struct heap invalid = {{0, 0}, {0, HEAP_VALIDATE_PARAMS}, {0, 0, 0, 0}, 0, {0, 0}, {0, 0, 0}, {0, 0}, HEAP_VALIDATE_PARAMS, 0, {0}};
3545 HANDLE heap = (HANDLE)&invalid, ret;
3546 PEB *Peb = NtCurrentTeb()->Peb;
3547 BOOL debugged;
3548 void *handler = RtlAddVectoredExceptionHandler( TRUE, test_heap_destroy_except_handler );
3550 test_heap_destroy_dbgstr = FALSE;
3551 test_heap_destroy_break = FALSE;
3552 debugged = Peb->BeingDebugged;
3553 Peb->BeingDebugged = TRUE;
3554 ret = RtlDestroyHeap( heap );
3555 ok( ret == heap, "RtlDestroyHeap(%p) returned %p\n", heap, ret );
3556 ok( test_heap_destroy_dbgstr, "HeapDestroy didn't call OutputDebugStrA\n" );
3557 ok( test_heap_destroy_break, "HeapDestroy didn't call DbgBreakPoint\n" );
3558 Peb->BeingDebugged = debugged;
3560 RtlRemoveVectoredExceptionHandler( handler );
3563 static void test_RtlFirstFreeAce(void)
3565 PACL acl;
3566 PACE_HEADER first;
3567 BOOL ret;
3568 DWORD size;
3569 BOOLEAN found;
3571 size = sizeof(ACL) + (sizeof(ACCESS_ALLOWED_ACE));
3572 acl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
3573 ret = InitializeAcl(acl, sizeof(ACL), ACL_REVISION);
3574 ok(ret, "InitializeAcl failed with error %ld\n", GetLastError());
3576 /* AceCount = 0 */
3577 first = (ACE_HEADER *)0xdeadbeef;
3578 found = RtlFirstFreeAce(acl, &first);
3579 ok(found, "RtlFirstFreeAce failed\n");
3580 ok(first == (PACE_HEADER)(acl + 1), "Failed to find ACL\n");
3582 acl->AclSize = sizeof(ACL) - 1;
3583 first = (ACE_HEADER *)0xdeadbeef;
3584 found = RtlFirstFreeAce(acl, &first);
3585 ok(found, "RtlFirstFreeAce failed\n");
3586 ok(first == NULL, "Found FirstAce = %p\n", first);
3588 /* AceCount = 1 */
3589 acl->AceCount = 1;
3590 acl->AclSize = size;
3591 first = (ACE_HEADER *)0xdeadbeef;
3592 found = RtlFirstFreeAce(acl, &first);
3593 ok(found, "RtlFirstFreeAce failed\n");
3594 ok(first == (PACE_HEADER)(acl + 1), "Failed to find ACL %p, %p\n", first, (PACE_HEADER)(acl + 1));
3596 acl->AclSize = sizeof(ACL) - 1;
3597 first = (ACE_HEADER *)0xdeadbeef;
3598 found = RtlFirstFreeAce(acl, &first);
3599 ok(!found, "RtlFirstFreeAce failed\n");
3600 ok(first == NULL, "Found FirstAce = %p\n", first);
3602 acl->AclSize = sizeof(ACL);
3603 first = (ACE_HEADER *)0xdeadbeef;
3604 found = RtlFirstFreeAce(acl, &first);
3605 ok(!found, "RtlFirstFreeAce failed\n");
3606 ok(first == NULL, "Found FirstAce = %p\n", first);
3608 HeapFree(GetProcessHeap(), 0, acl);
3611 static void test_RtlInitializeSid(void)
3613 SID_IDENTIFIER_AUTHORITY sid_ident = { SECURITY_NT_AUTHORITY };
3614 char buffer[SECURITY_MAX_SID_SIZE];
3615 PSID sid = (PSID)&buffer;
3616 NTSTATUS status;
3618 status = RtlInitializeSid(sid, &sid_ident, 1);
3619 ok(!status, "Unexpected status %#lx.\n", status);
3621 status = RtlInitializeSid(sid, &sid_ident, SID_MAX_SUB_AUTHORITIES);
3622 ok(!status, "Unexpected status %#lx.\n", status);
3624 status = RtlInitializeSid(sid, &sid_ident, SID_MAX_SUB_AUTHORITIES + 1);
3625 ok(status == STATUS_INVALID_PARAMETER, "Unexpected status %#lx.\n", status);
3628 static void test_RtlValidSecurityDescriptor(void)
3630 SECURITY_DESCRIPTOR *sd;
3631 NTSTATUS status;
3632 BOOLEAN ret;
3634 ret = RtlValidSecurityDescriptor(NULL);
3635 ok(!ret, "Unexpected return value %d.\n", ret);
3637 sd = calloc(1, SECURITY_DESCRIPTOR_MIN_LENGTH);
3639 ret = RtlValidSecurityDescriptor(sd);
3640 ok(!ret, "Unexpected return value %d.\n", ret);
3642 status = RtlCreateSecurityDescriptor(sd, SECURITY_DESCRIPTOR_REVISION);
3643 ok(!status, "Unexpected return value %#lx.\n", status);
3645 ret = RtlValidSecurityDescriptor(sd);
3646 ok(ret, "Unexpected return value %d.\n", ret);
3648 free(sd);
3651 START_TEST(rtl)
3653 InitFunctionPtrs();
3655 test_RtlQueryProcessDebugInformation();
3656 test_RtlCompareMemory();
3657 test_RtlCompareMemoryUlong();
3658 test_RtlMoveMemory();
3659 test_RtlFillMemory();
3660 test_RtlFillMemoryUlong();
3661 test_RtlZeroMemory();
3662 test_RtlByteSwap();
3663 test_RtlUniform();
3664 test_RtlRandom();
3665 test_RtlAreAllAccessesGranted();
3666 test_RtlAreAnyAccessesGranted();
3667 test_RtlComputeCrc32();
3668 test_HandleTables();
3669 test_RtlAllocateAndInitializeSid();
3670 test_RtlDeleteTimer();
3671 test_RtlThreadErrorMode();
3672 test_LdrProcessRelocationBlock();
3673 test_RtlIpv4AddressToString();
3674 test_RtlIpv4AddressToStringEx();
3675 test_RtlIpv4StringToAddress();
3676 test_RtlIpv4StringToAddressEx();
3677 test_RtlIpv6AddressToString();
3678 test_RtlIpv6AddressToStringEx();
3679 test_RtlIpv6StringToAddress();
3680 test_RtlIpv6StringToAddressEx();
3681 test_LdrAddRefDll();
3682 test_LdrLockLoaderLock();
3683 test_RtlCompressBuffer();
3684 test_RtlGetCompressionWorkSpaceSize();
3685 test_RtlDecompressBuffer();
3686 test_RtlIsCriticalSectionLocked();
3687 test_RtlInitializeCriticalSectionEx();
3688 test_RtlLeaveCriticalSection();
3689 test_LdrEnumerateLoadedModules();
3690 test_RtlMakeSelfRelativeSD();
3691 test_LdrRegisterDllNotification();
3692 test_DbgPrint();
3693 test_RtlDestroyHeap();
3694 test_RtlFirstFreeAce();
3695 test_RtlInitializeSid();
3696 test_RtlValidSecurityDescriptor();