--- truecrypt/platform/platformtest.cpp 2018/04/24 16:51:27 1.1.1.2 +++ truecrypt/platform/platformtest.cpp 2018/04/24 17:03:09 1.1.1.6 @@ -1,7 +1,7 @@ /* - Copyright (c) 2008 TrueCrypt Foundation. All rights reserved. + Copyright (c) 2008-2009 TrueCrypt Foundation. All rights reserved. - Governed by the TrueCrypt License 2.4 the full text of which is contained + Governed by the TrueCrypt License 2.7 the full text of which is contained in the file License.txt included in TrueCrypt binary and source code distribution packages. */ @@ -16,7 +16,9 @@ #include "Serializable.h" #include "SharedPtr.h" #include "StringConverter.h" +#include "SyncEvent.h" #include "Thread.h" +#include "Common/Tcdefs.h" namespace TrueCrypt { @@ -44,6 +46,20 @@ namespace TrueCrypt string str = "string test"; wstring wstr = L"wstring test"; + string convStr = "test"; + StringConverter::ToSingle (wstr, convStr); + if (convStr != "wstring test") + throw TestFailed (SRC_POS); + + StringConverter::Erase (convStr); + if (convStr != " ") + throw TestFailed (SRC_POS); + + wstring wEraseTest = L"erase test"; + StringConverter::Erase (wEraseTest); + if (wEraseTest != L" ") + throw TestFailed (SRC_POS); + list stringList; stringList.push_back (str + "1"); stringList.push_back (str + "2"); @@ -143,7 +159,14 @@ namespace TrueCrypt } } - // shared_ptr, Mutex, ScopeLock, Thread + // shared_ptr, Mutex, ScopeLock, SyncEvent, Thread + static struct + { + shared_ptr SharedIntPtr; + Mutex IntMutex; + SyncEvent ExitAllowedEvent; + } ThreadTestData; + void PlatformTest::ThreadTest () { Mutex mutex; @@ -151,35 +174,58 @@ namespace TrueCrypt mutex.Unlock(); const int maxThreads = 3; - make_shared_auto (int, SharedIntPtr); - *SharedIntPtr = 0; + ThreadTestData.SharedIntPtr.reset (new int (0)); for (int i = 0; i < maxThreads; i++) { Thread t; - t.Start (&ThreadTestProc, (void *)&SharedIntPtr); + t.Start (&ThreadTestProc, (void *) &ThreadTestData); } for (int i = 0; i < 50; i++) { - if (SharedIntPtr.use_count() == 1 && *SharedIntPtr == maxThreads) - break; + { + ScopeLock sl (ThreadTestData.IntMutex); + if (*ThreadTestData.SharedIntPtr == maxThreads) + break; + } Thread::Sleep(100); } - if (SharedIntPtr.use_count() != 1 || *SharedIntPtr != maxThreads) + if (*ThreadTestData.SharedIntPtr != maxThreads) + throw TestFailed (SRC_POS); + + for (int i = 0; i < 60000; i++) + { + ThreadTestData.ExitAllowedEvent.Signal(); + Thread::Sleep(1); + + ScopeLock sl (ThreadTestData.IntMutex); + if (*ThreadTestData.SharedIntPtr == 0) + break; + } + + if (*ThreadTestData.SharedIntPtr != 0) throw TestFailed (SRC_POS); } - TC_THREAD_PROC PlatformTest::ThreadTestProc (void *param) + TC_THREAD_PROC PlatformTest::ThreadTestProc (void *arg) { - static Mutex mutex; - shared_ptr sharedIntPtr = *(shared_ptr *) param; + + if (arg != (void *) &ThreadTestData) + return 0; + + { + ScopeLock sl (ThreadTestData.IntMutex); + ++(*ThreadTestData.SharedIntPtr); + } + + ThreadTestData.ExitAllowedEvent.Wait(); { - ScopeLock sl (mutex); - (*sharedIntPtr)++; + ScopeLock sl (ThreadTestData.IntMutex); + --(*ThreadTestData.SharedIntPtr); } return 0; @@ -191,7 +237,7 @@ namespace TrueCrypt if (sizeof (byte) != 1 || sizeof (int8) != 1 || sizeof (__int8) != 1) throw TestFailed (SRC_POS); if (sizeof (uint16) != 2 || sizeof (int16) != 2 || sizeof (__int16) != 2) throw TestFailed (SRC_POS); if (sizeof (uint32) != 4 || sizeof (int32) != 4 || sizeof (__int32) != 4) throw TestFailed (SRC_POS); - if (sizeof (uint64) != 8 || sizeof (int64) != 8 || sizeof (__int64) != 8) throw TestFailed (SRC_POS); + if (sizeof (uint64) != 8 || sizeof (int64) != 8) throw TestFailed (SRC_POS); // Exception handling TestFlag = false;