/* * Unit test suite for directory functions. * * Copyright 2002 Geoffrey Hausheer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Define _WIN32_WINNT to get SetThreadIdealProcessor on Windows */ #define _WIN32_WINNT 0x0500 #include #include "wine/test.h" #include #include #include #include /* Specify the number of simultaneous threads to test */ #define NUM_THREADS 4 /* Specify whether to test the extended priorities for Win2k/XP */ #define USE_EXTENDED_PRIORITIES 0 /* Specify whether to test the stack allocation in CreateThread */ #define CHECK_STACK 0 /* Set CHECK_STACK to 1 if you want to try to test the stack-limit from CreateThread. So far I have been unable to make this work, and I am in doubt as to how portable it is. Also, according to MSDN, you shouldn't mix C-run-time-libraries (i.e. alloca) with CreateThread. Anyhow, the check is currently commented out */ #if CHECK_STACK # ifdef __try # define __TRY __try # define __EXCEPT __except # define __ENDTRY # else # include "wine/exception.h" # endif #endif typedef BOOL (WINAPI *GetThreadPriorityBoost_t)(HANDLE,PBOOL); static GetThreadPriorityBoost_t pGetThreadPriorityBoost=NULL; typedef HANDLE (WINAPI *OpenThread_t)(DWORD,BOOL,DWORD); static OpenThread_t pOpenThread=NULL; typedef DWORD (WINAPI *SetThreadIdealProcessor_t)(HANDLE,DWORD); static SetThreadIdealProcessor_t pSetThreadIdealProcessor=NULL; typedef BOOL (WINAPI *SetThreadPriorityBoost_t)(HANDLE,BOOL); static SetThreadPriorityBoost_t pSetThreadPriorityBoost=NULL; /* Functions not tested yet: AttachThreadInput CreateRemoteThread SetThreadContext SwitchToThread In addition there are no checks that the inheritance works properly in CreateThread */ DWORD tlsIndex; typedef struct { int threadnum; HANDLE *event; DWORD *threadmem; } t1Struct; /* WinME supports OpenThread but doesn't know about access restrictions so we require them to be either completely ignored or always obeyed. */ INT obeying_ars = 0; /* -1 == no, 0 == dunno yet, 1 == yes */ #define obey_ar(x) \ (obeying_ars == 0 \ ? ((x) \ ? (obeying_ars = +1) \ : ((obeying_ars = -1), \ trace("not restricted, assuming consistent behaviour\n"))) \ : (obeying_ars < 0) \ ? ok(!(x), "access restrictions obeyed\n") \ : ok( (x), "access restrictions not obeyed\n")) /* Basic test that simultaneous threads can access shared memory, that the thread local storage routines work correctly, and that threads actually run concurrently */ static DWORD WINAPI threadFunc1(LPVOID p) { t1Struct *tstruct = (t1Struct *)p; int i; /* write our thread # into shared memory */ tstruct->threadmem[tstruct->threadnum]=GetCurrentThreadId(); ok(TlsSetValue(tlsIndex,(LPVOID)(tstruct->threadnum+1))!=0, "TlsSetValue failed\n"); /* The threads synchronize before terminating. This is done by Signaling an event, and waiting for all events to occur */ SetEvent(tstruct->event[tstruct->threadnum]); WaitForMultipleObjects(NUM_THREADS,tstruct->event,TRUE,INFINITE); /* Double check that all threads really did run by validating that they have all written to the shared memory. There should be no race here, since all threads were synchronized after the write.*/ for(i=0;ithreadmem[i]==0) ; } /* Check that noone changed our tls memory */ ok((int)TlsGetValue(tlsIndex)-1==tstruct->threadnum, "TlsGetValue failed\n"); return NUM_THREADS+tstruct->threadnum; } static DWORD WINAPI threadFunc2(LPVOID p) { return 99; } static DWORD WINAPI threadFunc3(LPVOID p) { HANDLE thread; thread=GetCurrentThread(); SuspendThread(thread); return 99; } static DWORD WINAPI threadFunc4(LPVOID p) { HANDLE event = (HANDLE)p; if(event != NULL) { SetEvent(event); } Sleep(99000); return 0; } #if CHECK_STACK static DWORD WINAPI threadFunc5(LPVOID p) { DWORD *exitCode = (DWORD *)p; SYSTEM_INFO sysInfo; sysInfo.dwPageSize=0; GetSystemInfo(&sysInfo); *exitCode=0; __TRY { alloca(2*sysInfo.dwPageSize); } __EXCEPT(1) { *exitCode=1; } __ENDTRY return 0; } #endif /* Check basic funcationality of CreateThread and Tls* functions */ static VOID test_CreateThread_basic(void) { HANDLE thread[NUM_THREADS],event[NUM_THREADS]; DWORD threadid[NUM_THREADS],curthreadId; DWORD threadmem[NUM_THREADS]; DWORD exitCode; t1Struct tstruct[NUM_THREADS]; int error; DWORD i,j; /* Retrieve current Thread ID for later comparisons */ curthreadId=GetCurrentThreadId(); /* Allocate some local storage */ ok((tlsIndex=TlsAlloc())!=TLS_OUT_OF_INDEXES,"TlsAlloc failed\n"); /* Create events for thread synchronization */ for(i=0;i0,"GetSystemInfo should return a valid page size\n"); thread = CreateThread(NULL,sysInfo.dwPageSize, threadFunc5,&exitCode, 0,&threadId); ok(WaitForSingleObject(thread,5000)==WAIT_OBJECT_0, "TerminateThread didn't work\n"); ok(exitCode==1,"CreateThread did not obey stack-size-limit\n"); ok(CloseHandle(thread)!=0,"CloseHandle failed\n"); #endif } /* Check whether setting/retrieving thread priorities works */ static VOID test_thread_priority(void) { HANDLE curthread,access_thread; DWORD curthreadId,exitCode; int min_priority=-2,max_priority=2; BOOL disabled,rc; int i; curthread=GetCurrentThread(); curthreadId=GetCurrentThreadId(); /* Check thread priority */ /* NOTE: on Win2k/XP priority can be from -7 to 6. All other platforms it is -2 to 2. However, even on a real Win2k system, using thread priorities beyond the -2 to 2 range does not work. If you want to try anyway, enable USE_EXTENDED_PRIORITIES */ ok(GetThreadPriority(curthread)==THREAD_PRIORITY_NORMAL, "GetThreadPriority Failed\n"); if (pOpenThread) { /* check that access control is obeyed */ access_thread=pOpenThread(THREAD_ALL_ACCESS & (~THREAD_QUERY_INFORMATION) & (~THREAD_SET_INFORMATION), 0,curthreadId); ok(access_thread!=NULL,"OpenThread returned an invalid handle\n"); if (access_thread!=NULL) { obey_ar(SetThreadPriority(access_thread,1)==0); obey_ar(GetThreadPriority(access_thread)==THREAD_PRIORITY_ERROR_RETURN); obey_ar(GetExitCodeThread(access_thread,&exitCode)==0); ok(CloseHandle(access_thread),"Error Closing thread handle\n"); } #if USE_EXTENDED_PRIORITIES min_priority=-7; max_priority=6; #endif } for(i=min_priority;i<=max_priority;i++) { ok(SetThreadPriority(curthread,i)!=0, "SetThreadPriority Failed for priority: %d\n",i); ok(GetThreadPriority(curthread)==i, "GetThreadPriority Failed for priority: %d\n",i); } ok(SetThreadPriority(curthread,THREAD_PRIORITY_TIME_CRITICAL)!=0, "SetThreadPriority Failed\n"); ok(GetThreadPriority(curthread)==THREAD_PRIORITY_TIME_CRITICAL, "GetThreadPriority Failed\n"); ok(SetThreadPriority(curthread,THREAD_PRIORITY_IDLE)!=0, "SetThreadPriority Failed\n"); ok(GetThreadPriority(curthread)==THREAD_PRIORITY_IDLE, "GetThreadPriority Failed\n"); ok(SetThreadPriority(curthread,0)!=0,"SetThreadPriority Failed\n"); /* Check thread priority boost */ if (!pGetThreadPriorityBoost || !pSetThreadPriorityBoost) return; /* Win9x */ SetLastError(0xdeadbeef); rc=pGetThreadPriorityBoost(curthread,&disabled); if (rc==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED) return; /* WinME */ /* check that access control is obeyed */ access_thread=pOpenThread(THREAD_ALL_ACCESS & (~THREAD_QUERY_INFORMATION) & (~THREAD_SET_INFORMATION), 0,curthreadId); ok(access_thread!=NULL,"OpenThread returned an invalid handle\n"); if (access_thread!=NULL) { obey_ar(pSetThreadPriorityBoost(access_thread,1)==0); obey_ar(pGetThreadPriorityBoost(access_thread,&disabled)==0); ok(CloseHandle(access_thread),"Error Closing thread handle\n"); } todo_wine { ok(rc!=0,"error=%ld\n",GetLastError()); rc = pSetThreadPriorityBoost(curthread,1); ok( rc != 0, "error=%ld\n",GetLastError()); rc=pGetThreadPriorityBoost(curthread,&disabled); ok(rc!=0 && disabled==1, "rc=%d error=%ld disabled=%d\n",rc,GetLastError(),disabled); rc = pSetThreadPriorityBoost(curthread,0); ok( rc != 0, "error=%ld\n",GetLastError()); rc=pGetThreadPriorityBoost(curthread,&disabled); ok(rc!=0 && disabled==0, "rc=%d error=%ld disabled=%d\n",rc,GetLastError(),disabled); } } /* check the GetThreadTimes function */ static VOID test_GetThreadTimes(void) { HANDLE thread,access_thread=NULL; FILETIME creationTime,exitTime,kernelTime,userTime; DWORD threadId; int error; thread = CreateThread(NULL,0,threadFunc2,NULL, CREATE_SUSPENDED,&threadId); ok(thread!=NULL,"Create Thread failed\n"); /* check that access control is obeyed */ if (pOpenThread) { access_thread=pOpenThread(THREAD_ALL_ACCESS & (~THREAD_QUERY_INFORMATION), 0,threadId); ok(access_thread!=NULL, "OpenThread returned an invalid handle\n"); } ok(ResumeThread(thread)==1,"Resume thread returned an invalid value\n"); ok(WaitForSingleObject(thread,5000)==WAIT_OBJECT_0, "ResumeThread didn't work\n"); creationTime.dwLowDateTime=99; creationTime.dwHighDateTime=99; exitTime.dwLowDateTime=99; exitTime.dwHighDateTime=99; kernelTime.dwLowDateTime=99; kernelTime.dwHighDateTime=99; userTime.dwLowDateTime=99; userTime.dwHighDateTime=99; /* GetThreadTimes should set all of the parameters passed to it */ error=GetThreadTimes(thread,&creationTime,&exitTime, &kernelTime,&userTime); if (error!=0 || GetLastError()!=ERROR_CALL_NOT_IMPLEMENTED) { ok(error!=0,"GetThreadTimes failed\n"); ok(creationTime.dwLowDateTime!=99 || creationTime.dwHighDateTime!=99, "creationTime was invalid\n"); ok(exitTime.dwLowDateTime!=99 || exitTime.dwHighDateTime!=99, "exitTime was invalid\n"); ok(kernelTime.dwLowDateTime!=99 || kernelTime.dwHighDateTime!=99, "kernelTimewas invalid\n"); ok(userTime.dwLowDateTime!=99 || userTime.dwHighDateTime!=99, "userTime was invalid\n"); ok(CloseHandle(thread)!=0,"CloseHandle failed\n"); if(access_thread!=NULL) { error=GetThreadTimes(access_thread,&creationTime,&exitTime, &kernelTime,&userTime); obey_ar(error==0); } } if(access_thread!=NULL) { ok(CloseHandle(access_thread)!=0,"CloseHandle Failed\n"); } } /* Check the processor affinity functions */ /* NOTE: These functions should also be checked that they obey access control */ static VOID test_thread_processor(void) { HANDLE curthread,curproc; DWORD processMask,systemMask; SYSTEM_INFO sysInfo; int error=0; sysInfo.dwNumberOfProcessors=0; GetSystemInfo(&sysInfo); ok(sysInfo.dwNumberOfProcessors>0, "GetSystemInfo failed to return a valid # of processors\n"); /* Use the current Thread/process for all tests */ curthread=GetCurrentThread(); ok(curthread!=NULL,"GetCurrentThread failed\n"); curproc=GetCurrentProcess(); ok(curproc!=NULL,"GetCurrentProcess failed\n"); /* Check the Affinity Mask functions */ ok(GetProcessAffinityMask(curproc,&processMask,&systemMask)!=0, "GetProcessAffinityMask failed\n"); ok(SetThreadAffinityMask(curthread,processMask)==processMask, "SetThreadAffinityMask failed\n"); ok(SetThreadAffinityMask(curthread,processMask+1)==0, "SetThreadAffinityMask passed for an illegal processor\n"); /* NOTE: This only works on WinNT/2000/XP) */ if (pSetThreadIdealProcessor) { todo_wine { SetLastError(0); error=pSetThreadIdealProcessor(curthread,0); if (GetLastError()!=ERROR_CALL_NOT_IMPLEMENTED) { ok(error!=-1, "SetThreadIdealProcessor failed\n"); } } if (GetLastError()!=ERROR_CALL_NOT_IMPLEMENTED) { error=pSetThreadIdealProcessor(curthread,MAXIMUM_PROCESSORS+1); ok(error==-1, "SetThreadIdealProcessor succeeded with an illegal processor #\n"); todo_wine { error=pSetThreadIdealProcessor(curthread,MAXIMUM_PROCESSORS); ok(error==0, "SetThreadIdealProcessor returned an incorrect value\n"); } } } } START_TEST(thread) { HINSTANCE lib; /* Neither Cygwin nor mingW export OpenThread, so do a dynamic check so that the compile passes */ lib=LoadLibraryA("kernel32"); ok(lib!=NULL,"Couldn't load kernel32.dll\n"); pGetThreadPriorityBoost=(GetThreadPriorityBoost_t)GetProcAddress(lib,"GetThreadPriorityBoost"); pOpenThread=(OpenThread_t)GetProcAddress(lib,"OpenThread"); pSetThreadIdealProcessor=(SetThreadIdealProcessor_t)GetProcAddress(lib,"SetThreadIdealProcessor"); pSetThreadPriorityBoost=(SetThreadPriorityBoost_t)GetProcAddress(lib,"SetThreadPriorityBoost"); test_CreateThread_basic(); test_CreateThread_suspended(); test_SuspendThread(); test_TerminateThread(); test_CreateThread_stack(); test_thread_priority(); test_GetThreadTimes(); test_thread_processor(); }