2013-10-11 21:34:23 +04:00
|
|
|
/**
|
|
|
|
* CTest for winpr's SetLastError/GetLastError
|
|
|
|
*
|
|
|
|
* Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2013-12-04 14:37:57 +04:00
|
|
|
* Copyright 2013 Thincast Technologies GmbH
|
|
|
|
* Copyright 2013 Norbert Federa <norbert.federa@thincast.com>
|
2013-10-11 21:34:23 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2013-08-22 18:18:38 +04:00
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
2015-02-18 21:35:33 +03:00
|
|
|
#include <winpr/wlog.h>
|
2013-08-22 18:18:38 +04:00
|
|
|
#include <winpr/synch.h>
|
|
|
|
#include <winpr/thread.h>
|
2013-10-11 21:34:23 +04:00
|
|
|
#include <winpr/interlocked.h>
|
2013-08-22 18:18:38 +04:00
|
|
|
|
|
|
|
#include <winpr/error.h>
|
|
|
|
|
|
|
|
static int status = 0;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
LONG* pLoopCount = NULL;
|
2013-10-11 21:34:23 +04:00
|
|
|
BOOL bStopTest = FALSE;
|
2013-08-22 18:18:38 +04:00
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
static DWORD WINAPI test_error_thread(LPVOID arg)
|
2013-08-22 18:18:38 +04:00
|
|
|
{
|
|
|
|
int id;
|
2013-10-11 21:34:23 +04:00
|
|
|
DWORD dwErrorSet;
|
|
|
|
DWORD dwErrorGet;
|
2013-08-22 18:18:38 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
id = (int)(size_t)arg;
|
2013-08-22 18:18:38 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
do
|
|
|
|
{
|
2021-06-02 16:46:27 +03:00
|
|
|
dwErrorSet = (DWORD)abs(rand()) + 1;
|
2013-10-11 21:34:23 +04:00
|
|
|
SetLastError(dwErrorSet);
|
|
|
|
if ((dwErrorGet = GetLastError()) != dwErrorSet)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("GetLastError() failure (thread %d): Expected: 0x%08" PRIX32
|
|
|
|
", Actual: 0x%08" PRIX32 "\n",
|
|
|
|
id, dwErrorSet, dwErrorGet);
|
2013-10-11 21:34:23 +04:00
|
|
|
if (!status)
|
|
|
|
status = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
InterlockedIncrement(pLoopCount);
|
|
|
|
} while (!status && !bStopTest);
|
2013-08-22 18:18:38 +04:00
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
return 0;
|
2013-08-22 18:18:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int TestErrorSetLastError(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
DWORD error;
|
|
|
|
HANDLE threads[4];
|
2015-05-05 14:55:48 +03:00
|
|
|
int i;
|
2013-08-22 18:18:38 +04:00
|
|
|
|
2021-06-02 16:46:27 +03:00
|
|
|
WINPR_UNUSED(argc);
|
|
|
|
WINPR_UNUSED(argv);
|
|
|
|
|
2015-01-16 12:11:50 +03:00
|
|
|
/* We must initialize WLog here. It will check for settings
|
|
|
|
* in the environment and if the variables are not set, the last
|
|
|
|
* error state is changed... */
|
|
|
|
WLog_GetRoot();
|
|
|
|
|
2013-08-22 18:18:38 +04:00
|
|
|
SetLastError(ERROR_ACCESS_DENIED);
|
|
|
|
|
|
|
|
error = GetLastError();
|
|
|
|
|
|
|
|
if (error != ERROR_ACCESS_DENIED)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("GetLastError() failure: Expected: 0x%08X, Actual: 0x%08" PRIX32 "\n",
|
|
|
|
ERROR_ACCESS_DENIED, error);
|
2013-08-22 18:18:38 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-10-11 21:34:23 +04:00
|
|
|
pLoopCount = _aligned_malloc(sizeof(LONG), sizeof(LONG));
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!pLoopCount)
|
|
|
|
{
|
|
|
|
printf("Unable to allocate memory\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2013-10-11 21:34:23 +04:00
|
|
|
*pLoopCount = 0;
|
|
|
|
|
2015-05-05 14:55:48 +03:00
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!(threads[i] = CreateThread(NULL, 0, test_error_thread, (void*)(size_t)0, 0, NULL)))
|
2015-05-05 14:55:48 +03:00
|
|
|
{
|
|
|
|
printf("Failed to create thread #%d\n", i);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2013-08-22 18:18:38 +04:00
|
|
|
|
2021-06-02 16:46:27 +03:00
|
|
|
// let the threads run for at least 0.2 seconds
|
|
|
|
Sleep(200);
|
2013-10-11 21:34:23 +04:00
|
|
|
bStopTest = TRUE;
|
|
|
|
|
2013-08-22 18:18:38 +04:00
|
|
|
WaitForSingleObject(threads[0], INFINITE);
|
|
|
|
WaitForSingleObject(threads[1], INFINITE);
|
|
|
|
WaitForSingleObject(threads[2], INFINITE);
|
|
|
|
WaitForSingleObject(threads[3], INFINITE);
|
|
|
|
|
|
|
|
CloseHandle(threads[0]);
|
|
|
|
CloseHandle(threads[1]);
|
|
|
|
CloseHandle(threads[2]);
|
|
|
|
CloseHandle(threads[3]);
|
|
|
|
|
|
|
|
error = GetLastError();
|
|
|
|
|
|
|
|
if (error != ERROR_ACCESS_DENIED)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("GetLastError() failure: Expected: 0x%08X, Actual: 0x%08" PRIX32 "\n",
|
|
|
|
ERROR_ACCESS_DENIED, error);
|
2013-08-22 18:18:38 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-10-11 21:34:23 +04:00
|
|
|
if (*pLoopCount < 4)
|
|
|
|
{
|
|
|
|
printf("Error: unexpected loop count\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("Completed %" PRId32 " iterations.\n", *pLoopCount);
|
2013-10-11 21:34:23 +04:00
|
|
|
|
2013-08-22 18:18:38 +04:00
|
|
|
return status;
|
|
|
|
}
|