ntdll: Define correct address space limits for x86_64.
[wine] / dlls / ntdll / tests / atom.c
1 /* Unit test suite for Ntdll atom API functions
2  *
3  * Copyright 2003 Gyorgy 'Nog' Jeney
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  *
19  * NOTES
20  * We use function pointers here as there is no import library for NTDLL on
21  * windows.
22  */
23
24 #include <stdio.h>
25 #include <stdarg.h>
26
27 #include "ntstatus.h"
28 /* Define WIN32_NO_STATUS so MSVC does not give us duplicate macro 
29  * definition errors when we get to winnt.h
30  */
31 #define WIN32_NO_STATUS
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winreg.h"
36 #include "winnls.h"
37 #include "wine/test.h"
38 #include "winternl.h"
39
40 #ifndef __WINE_WINTERNL_H
41 typedef unsigned short RTL_ATOM, *PRTL_ATOM;
42 typedef struct atom_table *RTL_ATOM_TABLE, **PRTL_ATOM_TABLE;
43 #endif
44
45 /* Function pointers for ntdll calls */
46 static HMODULE hntdll = 0;
47 static NTSTATUS (WINAPI *pRtlCreateAtomTable)(ULONG,PRTL_ATOM_TABLE);
48 static NTSTATUS (WINAPI *pRtlDestroyAtomTable)(RTL_ATOM_TABLE);
49 static NTSTATUS (WINAPI *pRtlEmptyAtomTable)(RTL_ATOM_TABLE,BOOLEAN);
50 static NTSTATUS (WINAPI *pRtlAddAtomToAtomTable)(RTL_ATOM_TABLE,PCWSTR,PRTL_ATOM);
51 static NTSTATUS (WINAPI *pRtlDeleteAtomFromAtomTable)(RTL_ATOM_TABLE,RTL_ATOM);
52 static NTSTATUS (WINAPI *pRtlLookupAtomInAtomTable)(RTL_ATOM_TABLE,PCWSTR,PRTL_ATOM);
53 static NTSTATUS (WINAPI *pRtlPinAtomInAtomTable)(RTL_ATOM_TABLE,RTL_ATOM);
54 static NTSTATUS (WINAPI *pRtlQueryAtomInAtomTable)(RTL_ATOM_TABLE,RTL_ATOM,PULONG,PULONG,PWSTR,PULONG);
55
56 static NTSTATUS (WINAPI* pNtAddAtom)(LPCWSTR,ULONG,RTL_ATOM*);
57 static NTSTATUS (WINAPI* pNtAddAtomNT4)(LPCWSTR,RTL_ATOM*);
58 static NTSTATUS (WINAPI* pNtQueryInformationAtom)(RTL_ATOM,DWORD,void*,ULONG,PULONG);
59
60 static const WCHAR EmptyAtom[] = {0};
61 static const WCHAR testAtom1[] = {'H','e','l','l','o',' ','W','o','r','l','d',0};
62 static const WCHAR testAtom2[] = {'H','e','l','l','o',' ','W','o','r','l','d','2',0};
63 static const WCHAR testAtom3[] = {'H','e','l','l','o',' ','W','o','r','l','d','3',0};
64
65 static const WCHAR testAtom1Cap[] = {'H','E','L','L','O',' ','W','O','R','L','D',0};
66 static const WCHAR testAtom1Low[] = {'h','e','l','l','o',' ','w','o','r','l','d',0};
67
68 static const WCHAR testAtomInt[] = {'#','1','3','2',0};
69 static const WCHAR testAtomIntInv[] = {'#','2','3','4','z',0};
70 static const WCHAR testAtomOTT[] = {'#','1','2','3',0};
71
72 static void InitFunctionPtr(void)
73 {
74     hntdll = LoadLibraryA("ntdll.dll");
75     ok(hntdll != 0, "Unable to load ntdll.dll\n");
76
77     if (hntdll)
78     {
79         pRtlCreateAtomTable = (void *)GetProcAddress(hntdll, "RtlCreateAtomTable");
80         pRtlDestroyAtomTable = (void *)GetProcAddress(hntdll, "RtlDestroyAtomTable");
81         pRtlEmptyAtomTable = (void *)GetProcAddress(hntdll, "RtlEmptyAtomTable");
82         pRtlAddAtomToAtomTable = (void *)GetProcAddress(hntdll, "RtlAddAtomToAtomTable");
83         pRtlDeleteAtomFromAtomTable = (void *)GetProcAddress(hntdll, "RtlDeleteAtomFromAtomTable");
84         pRtlLookupAtomInAtomTable = (void *)GetProcAddress(hntdll, "RtlLookupAtomInAtomTable");
85         pRtlPinAtomInAtomTable = (void *)GetProcAddress(hntdll, "RtlPinAtomInAtomTable");
86         pRtlQueryAtomInAtomTable = (void *)GetProcAddress(hntdll, "RtlQueryAtomInAtomTable");
87
88         pNtAddAtom = (void *)GetProcAddress(hntdll, "NtAddAtom");
89         pNtQueryInformationAtom = (void *)GetProcAddress(hntdll, "NtQueryInformationAtom");
90     }
91 }
92
93 static DWORD WINAPI RtlAtomTestThread(LPVOID Table)
94 {
95     RTL_ATOM_TABLE AtomTable = *(PRTL_ATOM_TABLE)Table;
96     RTL_ATOM Atom;
97     NTSTATUS res;
98     ULONG RefCount = 0, PinCount = 0, Len = 0;
99     WCHAR Name[64];
100
101     res = pRtlLookupAtomInAtomTable(AtomTable, testAtom1, &Atom);
102     ok(!res, "Unable to find atom from another thread, retval: %x\n", res);
103
104     res = pRtlLookupAtomInAtomTable(AtomTable, testAtom2, &Atom);
105     ok(!res, "Unable to lookup pinned atom in table, retval: %x\n", res);
106
107     res = pRtlQueryAtomInAtomTable(AtomTable, Atom, &RefCount, &PinCount, Name, &Len);
108     ok(res == STATUS_BUFFER_TOO_SMALL, "We got wrong retval: %x\n", res);
109
110     Len = 64;
111     res = pRtlQueryAtomInAtomTable(AtomTable, Atom, &RefCount, &PinCount, Name, &Len);
112     ok(!res, "Failed with longenough buffer, retval: %x\n", res);
113     ok(RefCount == 1, "Refcount was not 1 but %x\n", RefCount);
114     ok(PinCount == 1, "Pincount was not 1 but %x\n", PinCount);
115     ok(!lstrcmpW(Name, testAtom2), "We found wrong atom!!\n");
116     ok((lstrlenW(testAtom2) * sizeof(WCHAR)) == Len, "Returned wrong length %d\n", Len);
117
118     Len = 64;
119     res = pRtlQueryAtomInAtomTable(AtomTable, Atom, NULL, NULL, Name, &Len);
120     ok(!res, "RtlQueryAtomInAtomTable with optional args invalid failed, retval: %x\n", res);
121     ok(!lstrcmpW(Name, testAtom2), "Found Wrong atom!\n");
122     ok((lstrlenW(testAtom2) * sizeof(WCHAR)) == Len, "Returned wrong length %d\n", Len);
123
124     res = pRtlPinAtomInAtomTable(AtomTable, Atom);
125     ok(!res, "Unable to pin atom in atom table, retval: %x\n", res);
126
127     return 0;
128 }
129
130 static void test_NtAtom(void)
131 {
132     RTL_ATOM_TABLE AtomTable = NULL;
133     NTSTATUS res;
134     RTL_ATOM Atom1, Atom2, Atom3, testEAtom, testAtom;
135     HANDLE testThread;
136     ULONG RefCount = 0, PinCount = 0, Len = 0;
137     WCHAR Name[64];
138
139     /* If we pass a non-null string to create atom table, then it thinks that we
140      * have passed it an already allocated atom table */
141     res = pRtlCreateAtomTable(0, &AtomTable);
142     ok(!res, "RtlCreateAtomTable should succeed with an atom table size of 0\n");
143
144     if (!res)
145     {
146         res = pRtlDestroyAtomTable(AtomTable);
147         ok(!res, "We could create the atom table, but we couldn't destroy it! retval: %x\n", res);
148     }
149
150     AtomTable = NULL;
151     res = pRtlCreateAtomTable(37, &AtomTable);
152     ok(!res, "We're unable to create an atom table with a valid table size retval: %x\n", res);
153     if (!res)
154     {
155         res = pRtlAddAtomToAtomTable(AtomTable, testAtom1, &Atom1);
156         ok(!res, "We were unable to add a simple atom to the atom table, retval: %x\n", res);
157
158         res = pRtlLookupAtomInAtomTable(AtomTable, testAtom1Cap, &testAtom);
159         ok(!res, "We were unable to find capital version of the atom, retval: %x\n", res);
160         ok(Atom1 == testAtom, "Found wrong atom in table when querying capital atom\n");
161
162         res = pRtlLookupAtomInAtomTable(AtomTable, testAtom1Low, &testAtom);
163         ok(!res, "Unable to find lowercase version of the atom, retval: %x\n", res);
164         ok(testAtom == Atom1, "Found wrong atom when querying lowercase atom\n");
165
166         res = pRtlAddAtomToAtomTable(AtomTable, EmptyAtom, &testEAtom);
167         ok(res == STATUS_OBJECT_NAME_INVALID, "Got wrong retval, retval: %x\n", res);
168
169         res = pRtlLookupAtomInAtomTable(AtomTable, testAtom1, &testAtom);
170         ok(!res, "Failed to find totally legitimate atom, retval: %x\n", res);
171         ok(testAtom == Atom1, "Found wrong atom!\n");
172
173         res = pRtlAddAtomToAtomTable(AtomTable, testAtom2, &Atom2);
174         ok(!res, "Unable to add other legitimate atom to table, retval: %x\n", res);
175
176         res = pRtlPinAtomInAtomTable(AtomTable, Atom2);
177         ok(!res, "Unable to pin atom in atom table, retval: %x\n", res);
178
179         testThread = CreateThread(NULL, 0, RtlAtomTestThread, &AtomTable, 0, NULL);
180         WaitForSingleObject(testThread, INFINITE);
181
182         Len = 64;
183         res = pRtlQueryAtomInAtomTable(AtomTable, Atom2, &RefCount, &PinCount, Name, &Len);
184         ok(!res, "Unable to query atom in atom table, retval: %x\n", res);
185         ok(RefCount == 1, "RefCount is not 1 but %x\n", RefCount);
186         ok(PinCount == 1, "PinCount is not 1 but %x\n", PinCount);
187         ok(!lstrcmpW(Name, testAtom2), "We found wrong atom\n");
188         ok((lstrlenW(testAtom2) * sizeof(WCHAR)) == Len, "Returned wrong length %d\n", Len);
189
190         res = pRtlEmptyAtomTable(AtomTable, FALSE);
191         ok(!res, "Unable to empty atom table, retval %x\n", res);
192
193         Len = 64;
194         res = pRtlQueryAtomInAtomTable(AtomTable, Atom2, &RefCount, &PinCount, Name, &Len);
195         ok(!res, "It seems RtlEmptyAtomTable deleted our pinned atom eaven though we asked it not to, retval: %x\n", res);
196         ok(RefCount == 1, "RefCount is not 1 but %x\n", RefCount);
197         ok(PinCount == 1, "PinCount is not 1 but %x\n", PinCount);
198         ok(!lstrcmpW(Name, testAtom2), "We found wrong atom\n");
199         ok((lstrlenW(testAtom2) * sizeof(WCHAR)) == Len, "Returned wrong length %d\n", Len);
200
201         Len = 8;
202         Name[0] = Name[1] = Name[2] = Name[3] = Name[4] = 0x1337;
203         res = pRtlQueryAtomInAtomTable(AtomTable, Atom2, NULL, NULL, Name, &Len);
204         ok(!res, "query atom %x\n", res);
205         ok(Len == 6, "wrong length %u\n", Len);
206         ok(!memcmp(Name, testAtom2, Len), "wrong atom string\n");
207         ok(!Name[3], "wrong string termination\n");
208         ok(Name[4] == 0x1337, "buffer overwrite\n");
209
210         Len = lstrlenW(testAtom2) * sizeof(WCHAR);
211         memset(Name, '.', sizeof(Name));
212         res = pRtlQueryAtomInAtomTable( AtomTable, Atom2, NULL, NULL, Name, &Len );
213         ok(!res, "query atom %x\n", res);
214         ok(Len == (lstrlenW(testAtom2) - 1) * sizeof(WCHAR), "wrong length %u\n", Len);
215         ok(!memcmp(testAtom2, Name, (lstrlenW(testAtom2) - 1) * sizeof(WCHAR)), "wrong atom name\n");
216         ok(Name[lstrlenW(testAtom2) - 1] == '\0', "wrong char\n");
217         ok(Name[lstrlenW(testAtom2)] == ('.' << 8) + '.', "wrong char\n");
218
219         res = pRtlLookupAtomInAtomTable(AtomTable, testAtom2, &testAtom);
220         ok(!res, "We can't find our pinned atom!! retval: %x\n", res);
221         ok(testAtom == Atom2, "We found wrong atom!!!\n");
222
223         res = pRtlLookupAtomInAtomTable(AtomTable, testAtom1, &testAtom);
224         ok(res == STATUS_OBJECT_NAME_NOT_FOUND, "We found the atom in our table eaven though we asked RtlEmptyAtomTable to remove it, retval: %x\n", res);
225
226         res = pRtlAddAtomToAtomTable(AtomTable, testAtom3, &Atom3);
227         ok(!res, "Unable to add atom to table, retval: %x\n", res);
228
229         res = pRtlEmptyAtomTable(AtomTable, TRUE);
230         ok(!res, "Unable to empty atom table, retval: %x\n", res);
231
232         res = pRtlLookupAtomInAtomTable(AtomTable, testAtom2, &testAtom);
233         ok(res == STATUS_OBJECT_NAME_NOT_FOUND, "The pinned atom should be removed, retval: %x\n", res);
234
235         res = pRtlLookupAtomInAtomTable(AtomTable, testAtom3, &testAtom);
236         ok(res == STATUS_OBJECT_NAME_NOT_FOUND, "Non pinned atom should also be removed, retval: %x\n", res);
237
238         res = pRtlDestroyAtomTable(AtomTable);
239         ok(!res, "Can't destroy atom table, retval: %x\n", res);
240     }
241
242     AtomTable = NULL;
243     res = pRtlCreateAtomTable(37, &AtomTable);
244     ok(!res, "Unable to create atom table, retval: %x\n", res);
245
246     if (!res)
247     {
248         res = pRtlLookupAtomInAtomTable(AtomTable, testAtom1, &testAtom);
249         ok(res == STATUS_OBJECT_NAME_NOT_FOUND, "Didn't get expected retval with querying an empty atom table, retval: %x\n", res);
250
251         res = pRtlAddAtomToAtomTable(AtomTable, testAtom1, &Atom1);
252         ok(!res, "Unable to add atom to atom table, retval %x\n", res);
253
254         res = pRtlLookupAtomInAtomTable(AtomTable, testAtom1, &testAtom);
255         ok(!res, "Can't find previously added atom in table, retval: %x\n", res);
256         ok(testAtom == Atom1, "Found wrong atom! retval: %x\n", res);
257
258         res = pRtlDeleteAtomFromAtomTable(AtomTable, Atom1);
259         ok(!res, "Unable to delete atom from table, retval: %x\n", res);
260
261         res = pRtlLookupAtomInAtomTable(AtomTable, testAtom1, &testAtom);
262         ok(res == STATUS_OBJECT_NAME_NOT_FOUND, "Able to find previously deleted atom in table, retval: %x\n", res);
263
264         res = pRtlAddAtomToAtomTable(AtomTable, testAtom1, &Atom1);
265         ok(!res, "Unable to add atom to atom table, retval: %x\n", res);
266
267         Len = 0;
268         res = pRtlQueryAtomInAtomTable(AtomTable, Atom1, NULL, NULL, Name, &Len);
269         ok(res == STATUS_BUFFER_TOO_SMALL, "Got wrong retval, retval: %x\n", res);
270         ok((lstrlenW(testAtom1) * sizeof(WCHAR)) == Len || broken(!Len) /* nt4 */, "Got wrong length %x\n", Len);
271         if (!Len) pNtAddAtomNT4 = (void *)pNtAddAtom;
272
273         res = pRtlPinAtomInAtomTable(AtomTable, Atom1);
274         ok(!res, "Unable to pin atom in atom table, retval: %x\n", res);
275
276         res = pRtlLookupAtomInAtomTable(AtomTable, testAtom1, &testAtom);
277         ok(!res, "Unable to find atom in atom table, retval: %x\n", res);
278         ok(testAtom == Atom1, "Wrong atom found\n");
279
280         res = pRtlDeleteAtomFromAtomTable(AtomTable, Atom1);
281         ok(res == STATUS_WAS_LOCKED, "Unable to delete atom from table, retval: %x\n", res);
282
283         res = pRtlLookupAtomInAtomTable(AtomTable, testAtom1, &testAtom);
284         ok(!res, "Able to find deleted atom in table\n");
285
286         res = pRtlDestroyAtomTable(AtomTable);
287         ok(!res, "Unable to destroy atom table\n");
288     }
289 }
290
291 /* Test Adding integer atoms to atom table */
292 static void test_NtIntAtom(void)
293 {
294     NTSTATUS res;
295     RTL_ATOM_TABLE AtomTable;
296     RTL_ATOM testAtom;
297     ULONG RefCount = 0, PinCount = 0;
298     INT_PTR i;
299     WCHAR Name[64];
300     ULONG Len;
301
302     AtomTable = NULL;
303     res = pRtlCreateAtomTable(37, &AtomTable);
304     ok(!res, "Unable to create atom table, %x\n", res);
305
306     if (!res)
307     {
308         /* According to the kernel32 functions, integer atoms are only allowed from
309          * 0x0001 to 0xbfff and not 0xc000 to 0xffff, which is correct */
310         res = pRtlAddAtomToAtomTable(AtomTable, NULL, &testAtom);
311         ok(res == STATUS_INVALID_PARAMETER, "Didn't get expected result from adding 0 int atom, retval: %x\n", res);
312         for (i = 1; i <= 0xbfff; i++)
313         {
314             res = pRtlAddAtomToAtomTable(AtomTable, (LPWSTR)i, &testAtom);
315             ok(!res, "Unable to add valid integer atom %li, retval: %x\n", i, res);
316         }
317
318         for (i = 1; i <= 0xbfff; i++)
319         {
320             res = pRtlLookupAtomInAtomTable(AtomTable, (LPWSTR)i, &testAtom);
321             ok(!res, "Unable to find int atom %li, retval: %x\n", i, res);
322             if (!res)
323             {
324                 res = pRtlPinAtomInAtomTable(AtomTable, testAtom);
325                 ok(!res, "Unable to pin int atom %li, retval: %x\n", i, res);
326             }
327         }
328
329         for (i = 0xc000; i <= 0xffff; i++)
330         {
331             res = pRtlAddAtomToAtomTable(AtomTable, (LPWSTR)i, &testAtom);
332             ok(res, "Able to illeageal integer atom %li, retval: %x\n", i, res);
333         }
334
335         res = pRtlDestroyAtomTable(AtomTable);
336         ok(!res, "Unable to destroy atom table, retval: %x\n", res);
337     }
338
339     AtomTable = NULL;
340     res = pRtlCreateAtomTable(37, &AtomTable);
341     ok(!res, "Unable to create atom table, %x\n", res);
342     if (!res)
343     {
344         res = pRtlLookupAtomInAtomTable(AtomTable, (PWSTR)123, &testAtom);
345         ok(!res, "Unable to query atom in atom table, retval: %x\n", res);
346
347         res = pRtlAddAtomToAtomTable(AtomTable, testAtomInt, &testAtom);
348         ok(!res, "Unable to add int atom to table, retval: %x\n", res);
349
350         res = pRtlAddAtomToAtomTable(AtomTable, testAtomIntInv, &testAtom);
351         ok(!res, "Unable to add int atom to table, retval: %x\n", res);
352
353         res = pRtlAddAtomToAtomTable(AtomTable, (PWSTR)123, &testAtom);
354         ok(!res, "Unable to add int atom to table, retval: %x\n", res);
355
356         res = pRtlAddAtomToAtomTable(AtomTable, (PWSTR)123, &testAtom);
357         ok(!res, "Unable to re-add int atom to table, retval: %x\n", res);
358
359         Len = 64;
360         res = pRtlQueryAtomInAtomTable(AtomTable, testAtom, &RefCount, &PinCount, Name, &Len);
361         ok(!res, "Unable to query atom in atom table, retval: %x\n", res);
362         ok(PinCount == 1, "Expected pincount 1 but got %x\n", PinCount);
363         ok(RefCount == 1, "Expected refcount 1 but got %x\n", RefCount);
364         ok(!lstrcmpW(testAtomOTT, Name), "Got wrong atom name\n");
365         ok((lstrlenW(testAtomOTT) * sizeof(WCHAR)) == Len, "Got wrong len %d\n", Len);
366
367         res = pRtlPinAtomInAtomTable(AtomTable, testAtom);
368         ok(!res, "Unable to pin int atom, retval: %x\n", res);
369
370         res = pRtlPinAtomInAtomTable(AtomTable, testAtom);
371         ok(!res, "Unable to pin int atom, retval: %x\n", res);
372
373         res = pRtlQueryAtomInAtomTable(AtomTable, testAtom, &RefCount, &PinCount, NULL, NULL);
374         ok(!res, "Unable to query atom in atom table, retval: %x\n", res);
375         ok(PinCount == 1, "Expected pincount 1 but got %x\n", PinCount);
376         ok(RefCount == 1, "Expected refcount 1 but got %x\n", RefCount);
377
378         res = pRtlDestroyAtomTable(AtomTable);
379         ok(!res, "Unable to destroy atom table, retval: %x\n", res);
380     }
381 }
382
383 /* Tests to see how the pincount and refcount actually works */
384 static void test_NtRefPinAtom(void)
385 {
386     RTL_ATOM_TABLE AtomTable;
387     RTL_ATOM Atom;
388     ULONG PinCount = 0, RefCount = 0;
389     NTSTATUS res;
390
391     AtomTable = NULL;
392     res = pRtlCreateAtomTable(37, &AtomTable);
393     ok(!res, "Unable to create atom table, %x\n", res);
394
395     if (!res)
396     {
397         res = pRtlAddAtomToAtomTable(AtomTable, testAtom1, &Atom);
398         ok(!res, "Unable to add our atom to the atom table, retval: %x\n", res);
399
400         res = pRtlAddAtomToAtomTable(AtomTable, testAtom1, &Atom);
401         ok(!res, "Unable to add our atom to the atom table, retval: %x\n", res);
402
403         res = pRtlAddAtomToAtomTable(AtomTable, testAtom1, &Atom);
404         ok(!res, "Unable to add our atom to the atom table, retval: %x\n", res);
405
406         res = pRtlQueryAtomInAtomTable(AtomTable, Atom, &RefCount, &PinCount, NULL, NULL);
407         ok(!res, "Unable to query atom in atom table, retval: %x\n", res);
408         ok(PinCount == 0, "Expected pincount 0 but got %x\n", PinCount);
409         ok(RefCount == 3, "Expected refcount 3 but got %x\n", RefCount);
410
411         res = pRtlPinAtomInAtomTable(AtomTable, Atom);
412         ok(!res, "Unable to pin atom in atom table, retval: %x\n", res);
413
414         res = pRtlPinAtomInAtomTable(AtomTable, Atom);
415         ok(!res, "Unable to pin atom in atom table, retval: %x\n", res);
416
417         res = pRtlPinAtomInAtomTable(AtomTable, Atom);
418         ok(!res, "Unable to pin atom in atom table, retval: %x\n", res);
419
420         res = pRtlQueryAtomInAtomTable(AtomTable, Atom, &RefCount, &PinCount, NULL, NULL);
421         ok(!res, "Unable to query atom in atom table, retval: %x\n", res);
422         ok(PinCount == 1, "Expected pincount 1 but got %x\n", PinCount);
423         ok(RefCount == 3, "Expected refcount 3 but got %x\n", RefCount);
424
425         res = pRtlDestroyAtomTable(AtomTable);
426         ok(!res, "Unable to destroy atom table, retval: %x\n", res);
427     }
428 }
429
430 static void test_Global(void)
431 {
432     NTSTATUS    res;
433     RTL_ATOM    atom;
434     char        ptr[sizeof(ATOM_BASIC_INFORMATION) + 255 * sizeof(WCHAR)];
435     ATOM_BASIC_INFORMATION*     abi = (ATOM_BASIC_INFORMATION*)ptr;
436     ULONG       ptr_size = sizeof(ATOM_BASIC_INFORMATION) + 255 * sizeof(WCHAR);
437
438     if (pNtAddAtomNT4)
439         res = pNtAddAtomNT4(testAtom1, &atom);
440     else
441         res = pNtAddAtom(testAtom1, lstrlenW(testAtom1) * sizeof(WCHAR), &atom);
442
443     ok(!res, "Added atom (%x)\n", res);
444
445     memset(abi->Name, 0xcc, 255 * sizeof(WCHAR));
446     res = pNtQueryInformationAtom( atom, AtomBasicInformation, (void*)ptr, ptr_size, NULL );
447     ok(!res, "atom lookup\n");
448     ok(!lstrcmpW(abi->Name, testAtom1), "ok strings\n");
449     ok(abi->NameLength == lstrlenW(testAtom1) * sizeof(WCHAR), "wrong string length\n");
450     ok(abi->Name[lstrlenW(testAtom1)] == 0, "wrong string termination %x\n", abi->Name[lstrlenW(testAtom1)]);
451     ok(abi->Name[lstrlenW(testAtom1) + 1] == 0xcccc, "buffer overwrite %x\n", abi->Name[lstrlenW(testAtom1) + 1]);
452
453     ptr_size = sizeof(ATOM_BASIC_INFORMATION);
454     res = pNtQueryInformationAtom( atom, AtomBasicInformation, (void*)ptr, ptr_size, NULL );
455     ok(res == STATUS_BUFFER_TOO_SMALL, "wrong return status (%x)\n", res);
456     ok(abi->NameLength == lstrlenW(testAtom1) * sizeof(WCHAR) || broken(abi->NameLength == sizeof(WCHAR)), /* nt4 */
457        "string length %u\n",abi->NameLength);
458
459     memset(abi->Name, 0xcc, lstrlenW(testAtom1) * sizeof(WCHAR));
460     ptr_size = sizeof(ATOM_BASIC_INFORMATION) + lstrlenW(testAtom1) * sizeof(WCHAR);
461     res = pNtQueryInformationAtom( atom, AtomBasicInformation, (void*)ptr, ptr_size, NULL );
462     ok(!res, "atom lookup %x\n", res);
463     ok(!lstrcmpW(abi->Name, testAtom1), "strings don't match\n");
464     ok(abi->NameLength == lstrlenW(testAtom1) * sizeof(WCHAR), "wrong string length\n");
465     ok(abi->Name[lstrlenW(testAtom1)] == 0, "buffer overwrite %x\n", abi->Name[lstrlenW(testAtom1)]);
466     ok(abi->Name[lstrlenW(testAtom1) + 1] == 0xcccc, "buffer overwrite %x\n", abi->Name[lstrlenW(testAtom1) + 1]);
467
468     ptr_size = sizeof(ATOM_BASIC_INFORMATION) + 4 * sizeof(WCHAR);
469     abi->Name[0] = abi->Name[1] = abi->Name[2] = abi->Name[3] = '\0';
470     res = pNtQueryInformationAtom( atom, AtomBasicInformation, (void*)ptr, ptr_size, NULL );
471     ok(!res, "couldn't find atom\n");
472     ok(abi->NameLength == 8, "wrong string length %u\n", abi->NameLength);
473     ok(!memcmp(abi->Name, testAtom1, 8), "strings don't match\n");
474 }
475
476 START_TEST(atom)
477 {
478     InitFunctionPtr();
479     if (pRtlCreateAtomTable)
480     {
481         test_NtAtom();
482         test_NtIntAtom();
483         test_NtRefPinAtom();
484         test_Global();
485     }
486     else
487         win_skip("Needed atom functions are not available\n");
488
489     FreeLibrary(hntdll);
490 }