21 #include <winpr/crt.h>
22 #include <winpr/crypto.h>
23 #include <winpr/wlog.h>
24 #include <winpr/synch.h>
25 #include <winpr/thread.h>
26 #include <winpr/interlocked.h>
28 #include <winpr/error.h>
30 static int status = 0;
32 static LONG* pLoopCount = NULL;
33 static BOOL bStopTest = FALSE;
35 static UINT32 prand(UINT32 max)
40 winpr_RAND(&tmp,
sizeof(tmp));
41 return tmp % (max - 1) + 1;
44 static DWORD WINAPI test_error_thread(LPVOID arg)
50 id = (int)(
size_t)arg;
54 dwErrorSet = prand(UINT32_MAX - 1) + 1;
55 SetLastError(dwErrorSet);
56 if ((dwErrorGet = GetLastError()) != dwErrorSet)
58 printf(
"GetLastError() failure (thread %d): Expected: 0x%08" PRIX32
59 ", Actual: 0x%08" PRIX32
"\n",
60 id, dwErrorSet, dwErrorGet);
65 InterlockedIncrement(pLoopCount);
66 }
while (!status && !bStopTest);
71 int TestErrorSetLastError(
int argc,
char* argv[])
84 SetLastError(ERROR_ACCESS_DENIED);
86 error = GetLastError();
88 if (error != ERROR_ACCESS_DENIED)
90 printf(
"GetLastError() failure: Expected: 0x%08X, Actual: 0x%08" PRIX32
"\n",
91 ERROR_ACCESS_DENIED, error);
95 pLoopCount = winpr_aligned_malloc(
sizeof(LONG),
sizeof(LONG));
98 printf(
"Unable to allocate memory\n");
103 for (
int i = 0; i < 4; i++)
105 if (!(threads[i] = CreateThread(NULL, 0, test_error_thread, (
void*)(
size_t)0, 0, NULL)))
107 printf(
"Failed to create thread #%d\n", i);
116 (void)WaitForSingleObject(threads[0], INFINITE);
117 (void)WaitForSingleObject(threads[1], INFINITE);
118 (void)WaitForSingleObject(threads[2], INFINITE);
119 (void)WaitForSingleObject(threads[3], INFINITE);
121 (void)CloseHandle(threads[0]);
122 (void)CloseHandle(threads[1]);
123 (void)CloseHandle(threads[2]);
124 (void)CloseHandle(threads[3]);
126 error = GetLastError();
128 if (error != ERROR_ACCESS_DENIED)
130 printf(
"GetLastError() failure: Expected: 0x%08X, Actual: 0x%08" PRIX32
"\n",
131 ERROR_ACCESS_DENIED, error);
137 printf(
"Error: unexpected loop count\n");
141 printf(
"Completed %" PRId32
" iterations.\n", *pLoopCount);
142 winpr_aligned_free(pLoopCount);