Process Hacker
t_basesup.c
Go to the documentation of this file.
1 #include "tests.h"
2 
3 static VOID Test_time(
4  VOID
5  )
6 {
7  LARGE_INTEGER time;
8  FILETIME fileTime;
9  LARGE_INTEGER localTime;
10  FILETIME localFileTime;
11  LARGE_INTEGER systemTime;
12  FILETIME systemFileTime;
13 
14  PhQuerySystemTime(&time);
15  fileTime.dwLowDateTime = time.LowPart;
16  fileTime.dwHighDateTime = time.HighPart;
17 
18  PhSystemTimeToLocalTime(&time, &localTime);
19  FileTimeToLocalFileTime(&fileTime, &localFileTime);
20 
21  assert(localTime.LowPart == localFileTime.dwLowDateTime);
22  assert(localTime.HighPart == localFileTime.dwHighDateTime);
23 
24  PhLocalTimeToSystemTime(&localTime, &systemTime);
25  LocalFileTimeToFileTime(&localFileTime, &systemFileTime);
26 
27  assert(systemTime.LowPart == systemFileTime.dwLowDateTime);
28  assert(systemTime.HighPart == systemFileTime.dwHighDateTime);
29 }
30 
31 static VOID Test_stringz(
32  VOID
33  )
34 {
35  BOOLEAN result;
36  CHAR inputA[16] = "test";
37  CHAR outputA[16];
38  WCHAR inputW[16] = L"test";
39  WCHAR outputW[16];
40  ULONG returnCount;
41  PWSTR zero = L"\0\0\0\0\0\0\0\0";
42  PWSTR asdf = L"asdfasdfasdfasdf";
43  ULONG i;
44 
45  for (i = 0; i < 8; i++)
46  assert(PhCountStringZ(zero + i) == 0);
47  for (i = 0; i < 16; i++)
48  assert(PhCountStringZ(asdf + i) == 16 - i);
49 
50  result = PhCopyBytesZ(inputA, 4, outputA, 4, &returnCount);
51  assert(!result && returnCount == 5);
52  result = PhCopyBytesZ(inputA, 100, outputA, 4, &returnCount);
53  assert(!result && returnCount == 5);
54  result = PhCopyBytesZ(inputA, 3, outputA, 4, &returnCount);
55  assert(result && returnCount == 4);
56  result = PhCopyBytesZ(inputA, 4, outputA, 5, &returnCount);
57  assert(result && returnCount == 5);
58  result = PhCopyBytesZ(inputA, 100, outputA, 5, &returnCount);
59  assert(result && returnCount == 5);
60 
61  result = PhCopyStringZ(inputW, 100, outputW, 4, &returnCount);
62  assert(!result && returnCount == 5);
63  result = PhCopyStringZ(inputW, 4, outputW, 5, &returnCount);
64  assert(result && returnCount == 5);
65  result = PhCopyStringZ(inputW, 100, outputW, 5, &returnCount);
66  assert(result && returnCount == 5);
67 
68  result = PhCopyStringZFromMultiByte(inputA, 4, outputW, 4, &returnCount);
69  assert(!result && returnCount == 5);
70  result = PhCopyStringZFromMultiByte(inputA, 100, outputW, 4, &returnCount);
71  assert(!result && returnCount == 5);
72  result = PhCopyStringZFromMultiByte(inputA, 3, outputW, 4, &returnCount);
73  assert(result && returnCount == 4);
74  result = PhCopyStringZFromMultiByte(inputA, 4, outputW, 5, &returnCount);
75  assert(result && returnCount == 5);
76  result = PhCopyStringZFromMultiByte(inputA, 100, outputW, 5, &returnCount);
77  assert(result && returnCount == 5);
78 
79  assert(PhCompareStringZNatural(L"abc", L"abc", FALSE) == 0);
80  assert(PhCompareStringZNatural(L"abc", L"abc", TRUE) == 0);
81  assert(PhCompareStringZNatural(L"abc", L"ABC", FALSE) != 0);
82  assert(PhCompareStringZNatural(L"abc", L"ABC", TRUE) == 0);
83  assert(PhCompareStringZNatural(L"abc", L"abd", FALSE) < 0);
84  assert(PhCompareStringZNatural(L"abe", L"abd", FALSE) > 0);
85  assert(PhCompareStringZNatural(L"1", L"2", FALSE) < 0);
86  assert(PhCompareStringZNatural(L"12", L"9", FALSE) > 0);
87  assert(PhCompareStringZNatural(L"file-1", L"file-9", FALSE) < 0);
88  assert(PhCompareStringZNatural(L"file-12", L"file-9", FALSE) > 0);
89  assert(PhCompareStringZNatural(L"file-12", L"file-90", FALSE) < 0);
90 }
91 
93  VOID
94  )
95 {
96  ULONG i;
97  ULONG j;
98  PH_STRINGREF s1;
99  PH_STRINGREF s2;
100  PH_STRINGREF s3;
101  WCHAR buffer[26 * 2];
102 
103  // PhEqualStringRef, PhFindCharInStringRef, PhFindLastCharInStringRef
104 
105  // Alignment tests
106 
107  s1.Buffer = buffer;
108  s1.Length = sizeof(buffer);
109 
110  for (i = 0; i < 26; i++)
111  s1.Buffer[i] = (WCHAR)i;
112 
113  for (i = 0; i < 26; i++)
114  assert(PhFindCharInStringRef(&s1, (WCHAR)i, FALSE) == i);
115 
116  memset(buffer, 0, sizeof(buffer));
117  s1.Length = 0;
118 
119  for (i = 0; i < 26; i++)
120  assert(PhFindCharInStringRef(&s1, 0, FALSE) == -1);
121 
122  buffer[26] = 1;
123 
124  for (i = 0; i < 26; i++)
125  {
126  s1.Buffer = buffer + 26 - i;
127  s1.Length = i * sizeof(WCHAR);
128  assert(PhFindCharInStringRef(&s1, 1, FALSE) == -1);
129  }
130 
131  for (i = 1; i < 26; i++)
132  {
133  s1.Buffer = buffer;
134  s1.Length = i * 2 * sizeof(WCHAR);
135 
136  for (j = 0; j < i; j++)
137  {
138  buffer[j] = (WCHAR)('a' + j);
139  buffer[i + j] = (WCHAR)('A' + j);
140  }
141 
142  s2.Buffer = buffer;
143  s2.Length = i * sizeof(WCHAR);
144  s3.Buffer = buffer + i;
145  s3.Length = i * sizeof(WCHAR);
146  assert(!PhEqualStringRef(&s2, &s3, FALSE));
147  assert(PhEqualStringRef(&s2, &s3, TRUE));
148 
149  for (j = 0; j < i; j++)
150  {
151  buffer[j] = 'z';
152  assert(!PhEqualStringRef(&s2, &s3, FALSE));
153  assert(!PhEqualStringRef(&s2, &s3, TRUE));
154  buffer[j] = (WCHAR)('a' + j);
155  }
156 
157  s3 = s2;
158  assert(PhEqualStringRef(&s2, &s3, FALSE));
159  assert(PhEqualStringRef(&s2, &s3, TRUE));
160 
161  for (j = 0; j < i; j++)
162  {
163  assert(PhFindCharInStringRef(&s1, s1.Buffer[j], FALSE) == j);
164  assert(PhFindLastCharInStringRef(&s1, s1.Buffer[j], FALSE) == j);
165  assert(PhFindCharInStringRef(&s1, s1.Buffer[j], TRUE) == j % i);
166  assert(PhFindLastCharInStringRef(&s1, s1.Buffer[j], TRUE) == i + j % i);
167  }
168 
169  s1.Length = i * sizeof(WCHAR);
170 
171  for (j = 0; j < i; j++)
172  {
173  assert(PhFindCharInStringRef(&s1, s1.Buffer[j] - ('a' - 'A'), FALSE) == -1);
174  assert(PhFindLastCharInStringRef(&s1, s1.Buffer[j] - ('a' - 'A'), FALSE) == -1);
175  assert(PhFindCharInStringRef(&s1, s1.Buffer[j] - ('a' - 'A'), TRUE) == j);
176  assert(PhFindLastCharInStringRef(&s1, s1.Buffer[j] - ('a' - 'A'), TRUE) == j);
177  }
178 
179  s1.Buffer += i;
180 
181  for (j = 0; j < i; j++)
182  {
183  assert(PhFindCharInStringRef(&s1, s1.Buffer[j] + ('a' - 'A'), FALSE) == -1);
184  assert(PhFindLastCharInStringRef(&s1, s1.Buffer[j] + ('a' - 'A'), FALSE) == -1);
185  assert(PhFindCharInStringRef(&s1, s1.Buffer[j] + ('a' - 'A'), TRUE) == j);
186  assert(PhFindLastCharInStringRef(&s1, s1.Buffer[j] + ('a' - 'A'), TRUE) == j);
187  }
188  }
189 
190  // PhFindStringInStringRef
191 
192 #define DO_STRSTR_TEST(func, s1, s2, expected, ...) \
193  do { \
194  PH_STRINGREF ___t1; \
195  PH_STRINGREF ___t2; \
196  PhInitializeStringRef(&___t1, s1); \
197  PhInitializeStringRef(&___t2, s2); \
198  assert(func(&___t1, &___t2, __VA_ARGS__) == expected); \
199  } while (0)
200 
201  DO_STRSTR_TEST(PhFindStringInStringRef, L"asdfasdf", L"f", 3, FALSE);
202  DO_STRSTR_TEST(PhFindStringInStringRef, L"asdfasdf", L"g", -1, FALSE);
203  DO_STRSTR_TEST(PhFindStringInStringRef, L"asdfasdg", L"g", 7, FALSE);
204  DO_STRSTR_TEST(PhFindStringInStringRef, L"asdfasdg", L"asdg", 4, FALSE);
205  DO_STRSTR_TEST(PhFindStringInStringRef, L"asdfasdg", L"asdgh", -1, FALSE);
206  DO_STRSTR_TEST(PhFindStringInStringRef, L"asdfasdg", L"asdfasdg", 0, FALSE);
207  DO_STRSTR_TEST(PhFindStringInStringRef, L"asdfasdg", L"sdfasdg", 1, FALSE);
208  DO_STRSTR_TEST(PhFindStringInStringRef, L"asdfasdg", L"asdfasdgg", -1, FALSE);
209  DO_STRSTR_TEST(PhFindStringInStringRef, L"asdfasdg", L"asdfasdgggggg", -1, FALSE);
210  DO_STRSTR_TEST(PhFindStringInStringRef, L"", L"asdfasdgggggg", -1, FALSE);
211  DO_STRSTR_TEST(PhFindStringInStringRef, L"asdfasdg", L"", 0, FALSE);
213  // Test roll-over
214  DO_STRSTR_TEST(PhFindStringInStringRef, L"0sdfasdf1sdfasdf2sdfasdf3sdfasdg4sdfg", L"0sdfasdf1sdfasdf2sdfasdf3sdfasdg4sdfg", 0, FALSE);
215  DO_STRSTR_TEST(PhFindStringInStringRef, L"0sdfasdf1sdfasdf2sdfasdf3sdfasdg4sdfg", L"asdg4sdfg", 28, FALSE);
216  DO_STRSTR_TEST(PhFindStringInStringRef, L"0sdfasdf1sdfasdf2sdfasdf3sdfasdg4sdfg", L"asdg4Gdfg", -1, FALSE);
217 }
218 
220  VOID
221  )
222 {
223  BOOLEAN result;
224  PH_STRINGREF sr;
225  PPH_STRING string;
226  UCHAR buffer[16];
227 
228  PhInitializeStringRef(&sr, L"0011223344");
229  result = PhHexStringToBuffer(&sr, buffer);
230  assert(result && buffer[0] == 0 && buffer[1] == 0x11 && buffer[2] == 0x22 && buffer[3] == 0x33 && buffer[4] == 0x44);
231 
232  PhInitializeStringRef(&sr, L"00111");
233  result = PhHexStringToBuffer(&sr, buffer);
234  assert(!result);
235 
236  buffer[0] = 0;
237  buffer[1] = 0x99;
238  buffer[2] = 0xff;
239 
240  string = PhBufferToHexString(buffer, 3);
241  assert(wcscmp(string->Buffer, L"0099ff") == 0);
242 }
243 
245  VOID
246  )
247 {
248  PH_STRINGREF sr;
249  LONG64 integer;
250  PPH_STRING string;
251 
252  PhInitializeStringRef(&sr, L"123");
253  PhStringToInteger64(&sr, 0, &integer);
254  assert(integer == 123);
255  PhStringToInteger64(&sr, 10, &integer);
256  assert(integer == 123);
257  PhStringToInteger64(&sr, 8, &integer);
258  assert(integer == 0123);
259  PhStringToInteger64(&sr, 16, &integer);
260  assert(integer == 0x123);
261 
262  PhInitializeStringRef(&sr, L"0o123");
263  PhStringToInteger64(&sr, 0, &integer);
264  assert(integer == 0123);
265  PhInitializeStringRef(&sr, L"0x123");
266  PhStringToInteger64(&sr, 0, &integer);
267  assert(integer == 0x123);
268 
269  string = PhIntegerToString64(123, 0, FALSE);
270  assert(wcscmp(string->Buffer, L"123") == 0);
271  string = PhIntegerToString64(123, 10, FALSE);
272  assert(wcscmp(string->Buffer, L"123") == 0);
273  string = PhIntegerToString64(123, 8, FALSE);
274  assert(wcscmp(string->Buffer, L"173") == 0);
275  string = PhIntegerToString64(123, 16, FALSE);
276  assert(wcscmp(string->Buffer, L"7b") == 0);
277 
278  string = PhIntegerToString64(-123, 0, TRUE);
279  assert(wcscmp(string->Buffer, L"-123") == 0);
280  string = PhIntegerToString64(-123, 10, TRUE);
281  assert(wcscmp(string->Buffer, L"-123") == 0);
282  string = PhIntegerToString64(-123, 8, TRUE);
283  assert(wcscmp(string->Buffer, L"-173") == 0);
284  string = PhIntegerToString64(-123, 16, TRUE);
285  assert(wcscmp(string->Buffer, L"-7b") == 0);
286 
287  string = PhIntegerToString64(-123, 0, FALSE);
288  assert(wcscmp(string->Buffer, L"18446744073709551493") == 0);
289 }
290 
292  VOID
293  )
294 {
295  BOOLEAN result;
296  ULONG codePoints[6];
297  SIZE_T i;
298  WCHAR utf16[sizeof(codePoints) / sizeof(WCHAR)];
299  CHAR utf8[sizeof(codePoints) / sizeof(CHAR)];
300  ULONG numberOfCodePoints;
301  SIZE_T utf16Position = 0;
302  SIZE_T utf8Position = 0;
303  PPH_STRING utf16_1, utf16_2, utf16_3;
304  PPH_BYTES utf8_1, utf8_2, utf8_3;
305 
306  codePoints[0] = 0;
307  codePoints[1] = 0x50;
308  codePoints[2] = 0x312;
309  codePoints[3] = 0x3121;
310  codePoints[4] = 0x31212;
311  codePoints[5] = PH_UNICODE_MAX_CODE_POINT;
312 
313  for (i = 0; i < sizeof(codePoints) / sizeof(ULONG); i++)
314  {
315  result = PhEncodeUnicode(PH_UNICODE_UTF16, codePoints[i], utf16 + utf16Position, &numberOfCodePoints);
316  assert(result);
317  utf16Position += numberOfCodePoints;
318 
319  result = PhEncodeUnicode(PH_UNICODE_UTF8, codePoints[i], utf8 + utf8Position, &numberOfCodePoints);
320  assert(result);
321  utf8Position += numberOfCodePoints;
322  }
323 
324  utf16_1 = PhCreateStringEx(utf16, utf16Position * sizeof(WCHAR));
325  utf8_1 = PhCreateBytesEx(utf8, utf8Position);
326  utf16_2 = PhConvertUtf8ToUtf16Ex(utf8_1->Buffer, utf8_1->Length);
327  utf8_2 = PhConvertUtf16ToUtf8Ex(utf16_1->Buffer, utf16_1->Length);
328  utf16_3 = PhConvertUtf8ToUtf16Ex(utf8_2->Buffer, utf8_2->Length);
329  utf8_3 = PhConvertUtf16ToUtf8Ex(utf16_2->Buffer, utf16_2->Length);
330 
331  assert(utf16_1->Length == utf16_2->Length);
332  assert(memcmp(utf16_1->Buffer, utf16_2->Buffer, utf16_1->Length) == 0);
333  assert(utf16_2->Length == utf16_3->Length);
334  assert(memcmp(utf16_2->Buffer, utf16_3->Buffer, utf16_2->Length) == 0);
335 
336  assert(utf8_1->Length == utf8_2->Length);
337  assert(memcmp(utf8_1->Buffer, utf8_2->Buffer, utf8_1->Length) == 0);
338  assert(utf8_2->Length == utf8_3->Length);
339  assert(memcmp(utf8_2->Buffer, utf8_3->Buffer, utf8_2->Length) == 0);
340 }
341 
343  VOID
344  )
345 {
346  Test_time();
347  Test_stringz();
348  Test_stringref();
349  Test_hexstring();
350  Test_strint();
351  Test_unicode();
352 }