1 /*
2  * PROJECT:         ReactOS API tests
3  * LICENSE:         LGPLv2.1+ - See COPYING.LIB in the top level directory
4  * PURPOSE:         Test for loading/unloading user32.dll
5  * PROGRAMMERS:     Thomas Faber <thomas.faber@reactos.org>
6  */
7 
8 #include <apitest.h>
9 #include <ndk/pstypes.h>
10 #include <ndk/rtlfuncs.h>
11 
12 START_TEST(load)
13 {
14     HMODULE hUser32;
15     HMODULE hUser32_2;
16     BOOL Ret;
17     DWORD Error;
18     PPEB Peb = NtCurrentPeb();
19 
20     /* Before init */
21     hUser32 = GetModuleHandleW(L"user32");
22     ok(hUser32 == NULL, "hUser32 = %p\n", hUser32);
23     ok(Peb->KernelCallbackTable == NULL, "KernelCallbackTable = %p\n", Peb->KernelCallbackTable);
24     ok(Peb->PostProcessInitRoutine == NULL, "PostProcessInitRoutine = %p\n", Peb->PostProcessInitRoutine);
25 
26     SetLastError(12345);
27     hUser32 = LoadLibraryW(L"user32");
28     Error = GetLastError();
29     ok(hUser32 != NULL, "LoadLibrary failed\n");
30     ok(Error != 12345, "Error = %lu\n", Error);
31 
32     /* Initialized */
33     hUser32_2 = GetModuleHandleW(L"user32");
34     ok(hUser32_2 == hUser32, "hUser32 = %p\n", hUser32);
35     ok(Peb->KernelCallbackTable != NULL, "KernelCallbackTable = %p\n", Peb->KernelCallbackTable);
36     ok(Peb->PostProcessInitRoutine == NULL, "PostProcessInitRoutine = %p\n", Peb->PostProcessInitRoutine);
37 
38     SetLastError(12345);
39     hUser32_2 = LoadLibraryW(L"user32");
40     Error = GetLastError();
41     ok(hUser32_2 == hUser32, "LoadLibrary failed\n");
42     ok(Error == 12345, "Error = %lu\n", Error);
43 
44     /* Still initialized */
45     hUser32_2 = GetModuleHandleW(L"user32");
46     ok(hUser32_2 == hUser32, "hUser32 = %p\n", hUser32);
47     ok(Peb->KernelCallbackTable != NULL, "KernelCallbackTable = %p\n", Peb->KernelCallbackTable);
48     ok(Peb->PostProcessInitRoutine == NULL, "PostProcessInitRoutine = %p\n", Peb->PostProcessInitRoutine);
49 
50     SetLastError(12345);
51     Ret = FreeLibrary(hUser32);
52     Error = GetLastError();
53     ok(Ret == TRUE, "FreeLibrary returned %d\n", Ret);
54     ok(Error == 12345, "Error = %lu\n", Error);
55 
56     /* Still initialized */
57     hUser32_2 = GetModuleHandleW(L"user32");
58     ok(hUser32_2 == hUser32, "hUser32 = %p\n", hUser32);
59     ok(Peb->KernelCallbackTable != NULL, "KernelCallbackTable = %p\n", Peb->KernelCallbackTable);
60     ok(Peb->PostProcessInitRoutine == NULL, "PostProcessInitRoutine = %p\n", Peb->PostProcessInitRoutine);
61 
62     SetLastError(12345);
63     Ret = FreeLibrary(hUser32);
64     Error = GetLastError();
65     ok(Ret == TRUE, "FreeLibrary returned %d\n", Ret);
66     ok(Error == 12345, "Error = %lu\n", Error);
67 
68     /* Somebody kept an extra reference! */
69     hUser32_2 = GetModuleHandleW(L"user32");
70     ok(hUser32_2 == hUser32, "hUser32 = %p\n", hUser32);
71     ok(Peb->KernelCallbackTable != NULL, "KernelCallbackTable = %p\n", Peb->KernelCallbackTable);
72     ok(Peb->PostProcessInitRoutine == NULL, "PostProcessInitRoutine = %p\n", Peb->PostProcessInitRoutine);
73 
74     SetLastError(12345);
75     Ret = FreeLibrary(hUser32);
76     Error = GetLastError();
77     ok(Ret == TRUE, "FreeLibrary returned %d\n", Ret);
78     ok(Error == 12345, "Error = %lu\n", Error);
79 
80     /* Uninitialized */
81     hUser32 = GetModuleHandleW(L"user32");
82     ok(hUser32 == NULL, "hUser32 = %p\n", hUser32);
83     ok(Peb->KernelCallbackTable != NULL, "KernelCallbackTable = %p\n", Peb->KernelCallbackTable);
84     ok(Peb->PostProcessInitRoutine == NULL, "PostProcessInitRoutine = %p\n", Peb->PostProcessInitRoutine);
85 
86     SetLastError(12345);
87     hUser32 = LoadLibraryW(L"user32");
88     Error = GetLastError();
89     ok(hUser32 != NULL, "LoadLibrary failed\n");
90     ok(Error != 12345, "Error = %lu\n", Error);
91 
92     /* Initialized again */
93     hUser32_2 = GetModuleHandleW(L"user32");
94     ok(hUser32_2 == hUser32, "hUser32 = %p\n", hUser32);
95     ok(Peb->KernelCallbackTable != NULL, "KernelCallbackTable = %p\n", Peb->KernelCallbackTable);
96     ok(Peb->PostProcessInitRoutine == NULL, "PostProcessInitRoutine = %p\n", Peb->PostProcessInitRoutine);
97 
98     SetLastError(12345);
99     Ret = FreeLibrary(hUser32);
100     Error = GetLastError();
101     ok(Ret == TRUE, "FreeLibrary returned %d\n", Ret);
102     ok(Error == 12345, "Error = %lu\n", Error);
103 
104     /* Extra reference again */
105     hUser32_2 = GetModuleHandleW(L"user32");
106     ok(hUser32_2 == hUser32, "hUser32 = %p\n", hUser32);
107     ok(Peb->KernelCallbackTable != NULL, "KernelCallbackTable = %p\n", Peb->KernelCallbackTable);
108     ok(Peb->PostProcessInitRoutine == NULL, "PostProcessInitRoutine = %p\n", Peb->PostProcessInitRoutine);
109 
110     SetLastError(12345);
111     Ret = FreeLibrary(hUser32);
112     Error = GetLastError();
113     ok(Ret == TRUE, "FreeLibrary returned %d\n", Ret);
114     ok(Error == 12345, "Error = %lu\n", Error);
115 
116     /* Uninitialized again */
117     hUser32 = GetModuleHandleW(L"user32");
118     ok(hUser32 == NULL, "hUser32 = %p\n", hUser32);
119     ok(Peb->KernelCallbackTable != NULL, "KernelCallbackTable = %p\n", Peb->KernelCallbackTable);
120     ok(Peb->PostProcessInitRoutine == NULL, "PostProcessInitRoutine = %p\n", Peb->PostProcessInitRoutine);
121 }
122