[IPHLPAPI_APITEST]
[reactos.git] / rostests / apitests / kernel32 / GetCurrentDirectory.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Test for GetCurrentDirectory
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 BOOLEAN
16 CheckBuffer(
17 const VOID *Buffer,
18 SIZE_T Size,
19 UCHAR Value)
20 {
21 const UCHAR *Array = Buffer;
22 SIZE_T i;
23
24 for (i = 0; i < Size; i++)
25 if (Array[i] != Value)
26 {
27 trace("Expected %x, found %x at offset %lu\n", Value, Array[i], (ULONG)i);
28 return FALSE;
29 }
30 return TRUE;
31 }
32
33 static
34 BOOLEAN
35 CheckStringBufferA(
36 const VOID *Buffer,
37 SIZE_T Size,
38 PCSTR Expected,
39 UCHAR Fill)
40 {
41 const CHAR *Chars = Buffer;
42 const UCHAR *UChars = Buffer;
43 SIZE_T Length = strlen(Expected);
44 SIZE_T i;
45
46 if (Size < Length)
47 {
48 ok(0, "Size = %lu, Length = %lu\n", (ULONG)Size, (ULONG)Length);
49 return FALSE;
50 }
51
52 for (i = 0; i < Length; i++)
53 if (Chars[i] != Expected[i])
54 {
55 trace("Expected %x, found %x at offset %lu\n", Expected[i], Chars[i], (ULONG)i);
56 return FALSE;
57 }
58
59 ok(Chars[i] == 0, "Expected null terminator, found %x at offset %lu\n", Chars[i], (ULONG)i);
60 i++;
61
62 for (; i < Size; i++)
63 if (UChars[i] != Fill)
64 {
65 trace("Expected %x, found %x at offset %lu\n", Fill, UChars[i], (ULONG)i);
66 return FALSE;
67 }
68 return TRUE;
69 }
70
71 static
72 BOOLEAN
73 CheckStringBufferW(
74 const VOID *Buffer,
75 SIZE_T Size,
76 PCWSTR Expected,
77 UCHAR Fill)
78 {
79 const WCHAR *Chars = Buffer;
80 const UCHAR *UChars = Buffer;
81 SIZE_T Length = wcslen(Expected);
82 SIZE_T i;
83
84 if (Size < Length)
85 {
86 ok(0, "Size = %lu, Length = %lu\n", (ULONG)Size, (ULONG)Length);
87 return FALSE;
88 }
89
90 for (i = 0; i < Length; i++)
91 if (Chars[i] != Expected[i])
92 {
93 trace("Expected %x, found %x at offset %lu\n", Expected[i], Chars[i], (ULONG)i);
94 return FALSE;
95 }
96
97 ok(Chars[i] == 0, "Expected null terminator, found %x at offset %lu\n", Chars[i], (ULONG)i);
98 i++;
99
100 i *= sizeof(WCHAR);
101
102 for (; i < Size; i++)
103 if (UChars[i] != Fill)
104 {
105 trace("Expected %x, found %x at offset %lu\n", Fill, UChars[i], (ULONG)i);
106 return FALSE;
107 }
108 return TRUE;
109 }
110
111 static
112 VOID
113 TestGetCurrentDirectoryA(VOID)
114 {
115 CHAR Buffer[MAX_PATH];
116 DWORD Length;
117 BOOL Ret;
118 BOOLEAN Okay;
119
120 Ret = SetCurrentDirectoryA("C:\\");
121 ok(Ret == TRUE, "SetCurrentDirectory failed with %lu\n", GetLastError());
122
123 Length = GetCurrentDirectoryA(0, NULL);
124 ok(Length == sizeof("C:\\"), "Length = %lu\n", Length);
125
126 RtlFillMemory(Buffer, sizeof(Buffer), 0x55);
127 Length = GetCurrentDirectoryA(sizeof(Buffer), Buffer);
128 ok(Length == sizeof("C:\\") - 1, "Length = %lu\n", Length);
129 Okay = CheckStringBufferA(Buffer, sizeof(Buffer), "C:\\", 0x55);
130 ok(Okay, "CheckStringBufferA failed\n");
131
132 RtlFillMemory(Buffer, sizeof(Buffer), 0x55);
133 Length = GetCurrentDirectoryA(0, Buffer);
134 ok(Length == sizeof("C:\\"), "Length = %lu\n", Length);
135 Okay = CheckBuffer(Buffer, sizeof(Buffer), 0x55);
136 ok(Okay, "CheckBuffer failed\n");
137
138 RtlFillMemory(Buffer, sizeof(Buffer), 0x55);
139 Length = GetCurrentDirectoryA(1, Buffer);
140 ok(Length == sizeof("C:\\"), "Length = %lu\n", Length);
141 Okay = CheckBuffer(Buffer, sizeof(Buffer), 0x55);
142 ok(Okay, "CheckBuffer failed\n");
143
144 RtlFillMemory(Buffer, sizeof(Buffer), 0x55);
145 Length = GetCurrentDirectoryA(2, Buffer);
146 ok(Length == sizeof("C:\\"), "Length = %lu\n", Length);
147 Okay = CheckBuffer(Buffer, sizeof(Buffer), 0x55);
148 ok(Okay, "CheckBuffer failed\n");
149
150 RtlFillMemory(Buffer, sizeof(Buffer), 0x55);
151 Length = GetCurrentDirectoryA(3, Buffer);
152 ok(Length == sizeof("C:\\"), "Length = %lu\n", Length);
153 Okay = CheckBuffer(Buffer, sizeof(Buffer), 0x55);
154 ok(Okay, "CheckBuffer failed\n");
155
156 RtlFillMemory(Buffer, sizeof(Buffer), 0x55);
157 Length = GetCurrentDirectoryA(4, Buffer);
158 ok(Length == sizeof("C:\\") - 1, "Length = %lu\n", Length);
159 Okay = CheckStringBufferA(Buffer, sizeof(Buffer), "C:\\", 0x55);
160 ok(Okay, "CheckStringBufferA failed\n");
161 }
162
163 static
164 VOID
165 TestGetCurrentDirectoryW(VOID)
166 {
167 WCHAR Buffer[MAX_PATH];
168 DWORD Length;
169 BOOL Ret;
170 BOOLEAN Okay;
171
172 Ret = SetCurrentDirectoryW(L"C:\\");
173 ok(Ret == TRUE, "SetCurrentDirectory failed with %lu\n", GetLastError());
174
175 Length = GetCurrentDirectoryW(0, NULL);
176 ok(Length == sizeof("C:\\"), "Length = %lu\n", Length);
177
178 RtlFillMemory(Buffer, sizeof(Buffer), 0x55);
179 Length = GetCurrentDirectoryW(sizeof(Buffer) / sizeof(WCHAR), Buffer);
180 ok(Length == sizeof("C:\\") - 1, "Length = %lu\n", Length);
181 Okay = CheckStringBufferW(Buffer, sizeof(Buffer), L"C:\\", 0x55);
182 ok(Okay, "CheckStringBufferW failed\n");
183
184 RtlFillMemory(Buffer, sizeof(Buffer), 0x55);
185 Length = GetCurrentDirectoryW(0, Buffer);
186 ok(Length == sizeof("C:\\"), "Length = %lu\n", Length);
187 Okay = CheckBuffer(Buffer, sizeof(Buffer), 0x55);
188 ok(Okay, "CheckBuffer failed\n");
189
190 RtlFillMemory(Buffer, sizeof(Buffer), 0x55);
191 Length = GetCurrentDirectoryW(1, Buffer);
192 ok(Length == sizeof("C:\\"), "Length = %lu\n", Length);
193 Okay = CheckBuffer(Buffer, sizeof(Buffer), 0x55);
194 ok(Okay, "CheckBuffer failed\n");
195
196 RtlFillMemory(Buffer, sizeof(Buffer), 0x55);
197 Length = GetCurrentDirectoryW(2, Buffer);
198 ok(Length == sizeof("C:\\"), "Length = %lu\n", Length);
199 Okay = CheckBuffer(Buffer, sizeof(Buffer), 0x55);
200 ok(Okay, "CheckBuffer failed\n");
201
202 RtlFillMemory(Buffer, sizeof(Buffer), 0x55);
203 Length = GetCurrentDirectoryW(3, Buffer);
204 ok(Length == sizeof("C:\\"), "Length = %lu\n", Length);
205 Okay = CheckBuffer(Buffer, sizeof(Buffer), 0x55);
206 ok(Okay, "CheckBuffer failed\n");
207
208 RtlFillMemory(Buffer, sizeof(Buffer), 0x55);
209 Length = GetCurrentDirectoryW(4, Buffer);
210 ok(Length == sizeof("C:\\") - 1, "Length = %lu\n", Length);
211 Okay = CheckStringBufferW(Buffer, sizeof(Buffer), L"C:\\", 0x55);
212 ok(Okay, "CheckStringBufferW failed\n");
213 }
214
215 START_TEST(GetCurrentDirectory)
216 {
217 TestGetCurrentDirectoryA();
218 TestGetCurrentDirectoryW();
219 }