fdb0ab797dbf01529ba07ca872bf00ea8922ac0f
[reactos.git] / rostests / apitests / kernel32 / GetComputerNameEx.c
1 /*
2 * PROJECT: ReactOS API tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Test for GetComputerNameEx
5 * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6 */
7
8 #include <apitest.h>
9
10 #define WIN32_NO_STATUS
11 #include <stdio.h>
12 #include <ndk/rtltypes.h>
13
14 static
15 VOID
16 TestGetComputerNameEx(
17 _In_ COMPUTER_NAME_FORMAT NameType)
18 {
19 WCHAR Reference[128];
20 DWORD ReferenceLen;
21 WCHAR BufferW[128];
22 CHAR BufferA[128];
23 BOOL Ret;
24 DWORD Size;
25 DWORD Error;
26 ULONG i;
27
28 Size = RTL_NUMBER_OF(Reference);
29 Ret = GetComputerNameExW(NameType, Reference, &Size);
30 ok(Ret == TRUE, "[%d] GetComputerNameExW returned %d\n", NameType, Ret);
31 if (!Ret)
32 {
33 skip("[%d] Failed to get reference string\n", NameType);
34 return;
35 }
36 trace("[%d] Reference is %ls\n", NameType, Reference);
37 ReferenceLen = wcslen(Reference);
38 ok(ReferenceLen < RTL_NUMBER_OF(Reference),
39 "[%d] Unexpected ReferenceLen %lu\n", NameType, ReferenceLen);
40 if (NameType != ComputerNameDnsDomain && NameType != ComputerNamePhysicalDnsDomain)
41 {
42 ok(ReferenceLen != 0, "[%d] Unexpected ReferenceLen %lu\n", NameType, ReferenceLen);
43 }
44 ok(Size == ReferenceLen, "[%d] Size is %lu, expected %lu\n", NameType, Size, ReferenceLen);
45
46 /* NULL buffer, NULL size */
47 StartSeh()
48 Ret = GetComputerNameExW(NameType, NULL, NULL);
49 Error = GetLastError();
50 ok(Ret == FALSE, "[%d] GetComputerNameExW returned %d\n", NameType, Ret);
51 ok(Error == ERROR_INVALID_PARAMETER, "[%d] GetComputerNameExW returned error %lu\n", NameType, Error);
52 EndSeh(STATUS_SUCCESS);
53 StartSeh()
54 Ret = GetComputerNameExA(NameType, NULL, NULL);
55 Error = GetLastError();
56 ok(Ret == FALSE, "[%d] GetComputerNameExW returned %d\n", NameType, Ret);
57 ok(Error == ERROR_INVALID_PARAMETER, "[%d] GetComputerNameExW returned error %lu\n", NameType, Error);
58 EndSeh(STATUS_SUCCESS);
59
60 /* NULL buffer, nonzero size */
61 Size = 0x55555555;
62 Ret = GetComputerNameExW(NameType, NULL, &Size);
63 Error = GetLastError();
64 ok(Ret == FALSE, "[%d] GetComputerNameExW returned %d\n", NameType, Ret);
65 ok(Error == ERROR_INVALID_PARAMETER, "[%d] GetComputerNameExW returned error %lu\n", NameType, Error);
66 ok(Size == 0x55555555, "[%d] Got Size %lu\n", NameType, Size);
67
68 Size = 0x55555555;
69 Ret = GetComputerNameExA(NameType, NULL, &Size);
70 Error = GetLastError();
71 ok(Ret == FALSE, "[%d] GetComputerNameExA returned %d\n", NameType, Ret);
72 ok(Error == ERROR_INVALID_PARAMETER, "[%d] GetComputerNameExA returned error %lu\n", NameType, Error);
73 ok(Size == 0x55555555, "[%d] Got Size %lu\n", NameType, Size);
74
75 /* non-NULL buffer, NULL size */
76 RtlFillMemory(BufferW, sizeof(BufferW), 0x55);
77 Ret = GetComputerNameExW(NameType, BufferW, NULL);
78 Error = GetLastError();
79 ok(Ret == FALSE, "[%d] GetComputerNameExW returned %d\n", NameType, Ret);
80 ok(Error == ERROR_INVALID_PARAMETER, "[%d] GetComputerNameExW returned error %lu\n", NameType, Error);
81 ok(BufferW[0] == 0x5555, "[%d] BufferW[0] = 0x%x\n", NameType, BufferW[0]);
82
83 RtlFillMemory(BufferA, sizeof(BufferA), 0x55);
84 Ret = GetComputerNameExA(NameType, BufferA, NULL);
85 Error = GetLastError();
86 ok(Ret == FALSE, "[%d] GetComputerNameExA returned %d\n", NameType, Ret);
87 ok(Error == ERROR_INVALID_PARAMETER, "[%d] GetComputerNameExA returned error %lu\n", NameType, Error);
88 ok(BufferA[0] == 0x55, "[%d] BufferA[0] = 0x%x\n", NameType, BufferA[0]);
89
90 /* NULL buffer, zero size */
91 Size = 0;
92 Ret = GetComputerNameExW(NameType, NULL, &Size);
93 Error = GetLastError();
94 ok(Ret == FALSE, "[%d] GetComputerNameExW returned %d\n", NameType, Ret);
95 ok(Error == ERROR_MORE_DATA, "[%d] GetComputerNameExW returned error %lu\n", NameType, Error);
96 ok(Size == ReferenceLen + 1, "[%d] Got Size %lu, expected %lu\n", NameType, Size, ReferenceLen + 1);
97
98 Size = 0;
99 Ret = GetComputerNameExA(NameType, NULL, &Size);
100 Error = GetLastError();
101 ok(Ret == FALSE, "[%d] GetComputerNameExA returned %d\n", NameType, Ret);
102 ok(Error == ERROR_MORE_DATA, "[%d] GetComputerNameExA returned error %lu\n", NameType, Error);
103 ok(Size == ReferenceLen + 1, "[%d] Got Size %lu, expected %lu\n", NameType, Size, ReferenceLen + 1);
104
105 /* non-NULL buffer, zero size */
106 RtlFillMemory(BufferW, sizeof(BufferW), 0x55);
107 Size = 0;
108 Ret = GetComputerNameExW(NameType, BufferW, &Size);
109 Error = GetLastError();
110 ok(Ret == FALSE, "[%d] GetComputerNameExW returned %d\n", NameType, Ret);
111 ok(Error == ERROR_MORE_DATA, "[%d] GetComputerNameExW returned error %lu\n", NameType, Error);
112 ok(Size == ReferenceLen + 1, "[%d] Got Size %lu, expected %lu\n", NameType, Size, ReferenceLen + 1);
113 ok(BufferW[0] == 0x5555, "[%d] BufferW[0] = 0x%x\n", NameType, BufferW[0]);
114
115 RtlFillMemory(BufferA, sizeof(BufferA), 0x55);
116 Size = 0;
117 Ret = GetComputerNameExA(NameType, BufferA, &Size);
118 Error = GetLastError();
119 ok(Ret == FALSE, "[%d] GetComputerNameExA returned %d\n", NameType, Ret);
120 ok(Error == ERROR_MORE_DATA, "[%d] GetComputerNameExA returned error %lu\n", NameType, Error);
121 ok(Size == ReferenceLen + 1, "[%d] Got Size %lu, expected %lu\n", NameType, Size, ReferenceLen + 1);
122 ok(BufferA[0] == 0x55, "[%d] BufferA[0] = 0x%x\n", NameType, BufferA[0]);
123
124 /* non-NULL buffer, too small size */
125 RtlFillMemory(BufferW, sizeof(BufferW), 0x55);
126 Size = ReferenceLen;
127 Ret = GetComputerNameExW(NameType, BufferW, &Size);
128 Error = GetLastError();
129 ok(Ret == FALSE, "[%d] GetComputerNameExW returned %d\n", NameType, Ret);
130 ok(Error == ERROR_MORE_DATA, "[%d] GetComputerNameExW returned error %lu\n", NameType, Error);
131 ok(Size == ReferenceLen + 1, "[%d] Got Size %lu, expected %lu\n", NameType, Size, ReferenceLen + 1);
132 if (NameType != ComputerNameNetBIOS && NameType != ComputerNamePhysicalNetBIOS)
133 {
134 if (ReferenceLen == 0)
135 {
136 ok(BufferW[0] == 0x5555, "[%d] BufferW[0] = 0x%x\n",
137 NameType, BufferW[0]);
138 }
139 else
140 {
141 ok(BufferW[0] == 0, "[%d] BufferW[0] = 0x%x\n",
142 NameType, BufferW[0]);
143 }
144 }
145 ok(BufferW[1] == 0x5555, "[%d] BufferW[1] = 0x%x\n", NameType, BufferW[1]);
146
147 RtlFillMemory(BufferA, sizeof(BufferA), 0x55);
148 Size = ReferenceLen;
149 Ret = GetComputerNameExA(NameType, BufferA, &Size);
150 Error = GetLastError();
151 ok(Ret == FALSE, "[%d] GetComputerNameExA returned %d\n", NameType, Ret);
152 ok(Error == ERROR_MORE_DATA, "[%d] GetComputerNameExA returned error %lu\n", NameType, Error);
153 ok(Size == ReferenceLen + 1, "[%d] Got Size %lu, expected %lu\n", NameType, Size, ReferenceLen + 1);
154 ok(BufferA[0] == 0x55, "[%d] BufferA[0] = 0x%x\n", NameType, BufferA[0]);
155
156 /* non-NULL buffer, exact size */
157 RtlFillMemory(BufferW, sizeof(BufferW), 0x55);
158 Size = ReferenceLen + 1;
159 Ret = GetComputerNameExW(NameType, BufferW, &Size);
160 ok(Ret == TRUE, "[%d] GetComputerNameExW returned %d\n", NameType, Ret);
161 ok(Size == ReferenceLen, "[%d] Got Size %lu, expected %lu\n", NameType, Size, ReferenceLen + 1);
162 ok(BufferW[ReferenceLen] == 0, "[%d] BufferW[ReferenceLen] = 0x%x\n", NameType, BufferW[ReferenceLen]);
163 ok(BufferW[ReferenceLen + 1] == 0x5555, "[%d] BufferW[ReferenceLen + 1] = 0x%x\n", NameType, BufferW[ReferenceLen + 1]);
164 ok(!wcscmp(BufferW, Reference), "[%d] '%ls' != '%ls'\n", NameType, BufferW, Reference);
165
166 RtlFillMemory(BufferA, sizeof(BufferA), 0x55);
167 Size = ReferenceLen + 1;
168 Ret = GetComputerNameExA(NameType, BufferA, &Size);
169 ok(Ret == TRUE, "[%d] GetComputerNameExA returned %d\n", NameType, Ret);
170 ok(Size == ReferenceLen, "[%d] Got Size %lu, expected %lu\n", NameType, Size, ReferenceLen + 1);
171 ok(BufferA[ReferenceLen] == 0, "[%d] BufferA[ReferenceLen] = 0x%x\n", NameType, BufferA[ReferenceLen]);
172 ok(BufferA[ReferenceLen + 1] == 0x55, "[%d] BufferA[ReferenceLen + 1] = 0x%x\n", NameType, BufferA[ReferenceLen + 1]);
173 for (i = 0; i < ReferenceLen; i++)
174 {
175 if (BufferA[i] != Reference[i])
176 {
177 ok(0, "[%d] BufferA[%lu] = 0x%x, expected 0x%x\n", NameType, i, BufferA[i], Reference[i]);
178 }
179 }
180 }
181
182 START_TEST(GetComputerNameEx)
183 {
184 TestGetComputerNameEx(ComputerNameNetBIOS);
185 TestGetComputerNameEx(ComputerNameDnsHostname);
186 TestGetComputerNameEx(ComputerNameDnsDomain);
187 //TestGetComputerNameEx(ComputerNameDnsFullyQualified);
188 TestGetComputerNameEx(ComputerNamePhysicalNetBIOS);
189 TestGetComputerNameEx(ComputerNamePhysicalDnsHostname);
190 TestGetComputerNameEx(ComputerNamePhysicalDnsDomain);
191 //TestGetComputerNameEx(ComputerNamePhysicalDnsFullyQualified);
192 }