2 Copyright (c) 2008-2009 TrueCrypt Developers Association. All rights reserved.
4 Governed by the TrueCrypt License 3.0 the full text of which is contained in
5 the file License.txt included in TrueCrypt binary and source code distribution
9 #include "PlatformTest.h"
10 #include "Exception.h"
11 #include "FileStream.h"
14 #include "MemoryStream.h"
16 #include "Serializable.h"
17 #include "SharedPtr.h"
18 #include "StringConverter.h"
19 #include "SyncEvent.h"
21 #include "Common/Tcdefs.h"
25 // make_shared_auto, File, Stream, MemoryStream, Endian, Serializer, Serializable
26 void PlatformTest::SerializerTest ()
28 shared_ptr <Stream> stream (new MemoryStream);
31 make_shared_auto (File, file);
32 finally_do_arg (File&, *file, { if (finally_arg.IsOpen()) finally_arg.Delete(); });
36 file->Open ("truecrypt-serializer-test.tmp", File::CreateReadWrite);
37 stream = shared_ptr <Stream> (new FileStream (file));
42 Serializer ser (stream);
44 uint32 i32 = 0x12345678;
45 uint64 i64 = 0x0123456789abcdefULL;
46 string str = "string test";
47 wstring wstr = L"wstring test";
49 string convStr = "test";
50 StringConverter::ToSingle (wstr, convStr);
51 if (convStr != "wstring test")
52 throw TestFailed (SRC_POS);
54 StringConverter::Erase (convStr);
56 throw TestFailed (SRC_POS);
58 wstring wEraseTest = L"erase test";
59 StringConverter::Erase (wEraseTest);
60 if (wEraseTest != L" ")
61 throw TestFailed (SRC_POS);
63 list <string> stringList;
64 stringList.push_back (str + "1");
65 stringList.push_back (str + "2");
66 stringList.push_back (str + "3");
68 list <wstring> wstringList;
69 wstringList.push_back (wstr + L"1");
70 wstringList.push_back (wstr + L"2");
71 wstringList.push_back (wstr + L"3");
74 for (size_t i = 0; i < buffer.Size(); i++)
77 ser.Serialize ("int32", i32);
78 ser.Serialize ("int64", i64);
79 ser.Serialize ("string", str);
80 ser.Serialize ("wstring", wstr);
81 ser.Serialize ("stringList", stringList);
82 ser.Serialize ("wstringList", wstringList);
83 ser.Serialize ("buffer", ConstBufferPtr (buffer));
85 ExecutedProcessFailed ex (SRC_POS, "cmd", -123, "error output");
86 ex.Serialize (stream);
88 list < shared_ptr <ExecutedProcessFailed> > exList;
89 exList.push_back (make_shared <ExecutedProcessFailed> (ExecutedProcessFailed (SRC_POS, "cmd", -123, "error output1")));
90 exList.push_back (make_shared <ExecutedProcessFailed> (ExecutedProcessFailed (SRC_POS, "cmd", -234, "error output2")));
91 exList.push_back (make_shared <ExecutedProcessFailed> (ExecutedProcessFailed (SRC_POS, "cmd", -567, "error output3")));
92 Serializable::SerializeList (stream, exList);
100 ser.Deserialize ("int32", di32);
102 throw TestFailed (SRC_POS);
105 ser.Deserialize ("int64", di64);
107 throw TestFailed (SRC_POS);
110 ser.Deserialize ("string", dstr);
112 throw TestFailed (SRC_POS);
115 ser.Deserialize ("wstring", dwstr);
117 throw TestFailed (SRC_POS);
120 foreach (string item, ser.DeserializeStringList ("stringList"))
125 throw TestFailed (SRC_POS);
129 foreach (wstring item, ser.DeserializeWStringList ("wstringList"))
134 throw TestFailed (SRC_POS);
138 ser.Deserialize ("buffer", buffer);
139 for (size_t i = 0; i < buffer.Size(); i++)
140 if (buffer[i] != (byte) i)
141 throw TestFailed (SRC_POS);
143 shared_ptr <ExecutedProcessFailed> dex = Serializable::DeserializeNew <ExecutedProcessFailed> (stream);
145 || dex->GetCommand() != "cmd"
146 || dex->GetExitCode() != -123
147 || dex->GetErrorOutput() != "error output")
148 throw TestFailed (SRC_POS);
150 list < shared_ptr <ExecutedProcessFailed> > dexList;
151 Serializable::DeserializeList (stream, dexList);
153 foreach_ref (const ExecutedProcessFailed &ex, dexList)
156 s << "error output" << i++;
157 if (ex.GetErrorOutput() != s.str())
158 throw TestFailed (SRC_POS);
162 // shared_ptr, Mutex, ScopeLock, SyncEvent, Thread
165 shared_ptr <int> SharedIntPtr;
167 SyncEvent ExitAllowedEvent;
170 void PlatformTest::ThreadTest ()
176 const int maxThreads = 3;
177 ThreadTestData.SharedIntPtr.reset (new int (0));
179 for (int i = 0; i < maxThreads; i++)
182 t.Start (&ThreadTestProc, (void *) &ThreadTestData);
185 for (int i = 0; i < 50; i++)
188 ScopeLock sl (ThreadTestData.IntMutex);
189 if (*ThreadTestData.SharedIntPtr == maxThreads)
196 if (*ThreadTestData.SharedIntPtr != maxThreads)
197 throw TestFailed (SRC_POS);
199 for (int i = 0; i < 60000; i++)
201 ThreadTestData.ExitAllowedEvent.Signal();
204 ScopeLock sl (ThreadTestData.IntMutex);
205 if (*ThreadTestData.SharedIntPtr == 0)
209 if (*ThreadTestData.SharedIntPtr != 0)
210 throw TestFailed (SRC_POS);
213 TC_THREAD_PROC PlatformTest::ThreadTestProc (void *arg)
216 if (arg != (void *) &ThreadTestData)
220 ScopeLock sl (ThreadTestData.IntMutex);
221 ++(*ThreadTestData.SharedIntPtr);
224 ThreadTestData.ExitAllowedEvent.Wait();
227 ScopeLock sl (ThreadTestData.IntMutex);
228 --(*ThreadTestData.SharedIntPtr);
234 bool PlatformTest::TestAll ()
237 if (sizeof (byte) != 1 || sizeof (int8) != 1 || sizeof (__int8) != 1) throw TestFailed (SRC_POS);
238 if (sizeof (uint16) != 2 || sizeof (int16) != 2 || sizeof (__int16) != 2) throw TestFailed (SRC_POS);
239 if (sizeof (uint32) != 4 || sizeof (int32) != 4 || sizeof (__int32) != 4) throw TestFailed (SRC_POS);
240 if (sizeof (uint64) != 8 || sizeof (int64) != 8) throw TestFailed (SRC_POS);
242 // Exception handling
248 throw TestFailed (SRC_POS);
265 RttiTestBase &rttiBaseRef = rtti;
266 RttiTestBase *rttiBasePtr = &rtti;
268 if (typeid (rttiBaseRef) != typeid (rtti))
269 throw TestFailed (SRC_POS);
271 if (typeid (*rttiBasePtr) != typeid (rtti))
272 throw TestFailed (SRC_POS);
274 if (dynamic_cast <RttiTest *> (rttiBasePtr) == nullptr)
275 throw TestFailed (SRC_POS);
279 dynamic_cast <RttiTest &> (rttiBaseRef);
283 throw TestFailed (SRC_POS);
289 finally_do ({ TestFlag = true; });
291 throw TestFailed (SRC_POS);
294 throw TestFailed (SRC_POS);
298 finally_do_arg (bool*, &TestFlag, { *finally_arg = true; });
300 throw TestFailed (SRC_POS);
303 throw TestFailed (SRC_POS);
308 finally_do_arg2 (bool*, &TestFlag, int*, &tesFlag2, { *finally_arg = true; *finally_arg2 = 2; });
309 if (TestFlag || tesFlag2 != 0)
310 throw TestFailed (SRC_POS);
312 if (!TestFlag || tesFlag2 != 2)
313 throw TestFailed (SRC_POS);
315 // uint64, vector, list, string, wstring, stringstream, wstringstream
316 // shared_ptr, make_shared, StringConverter, foreach
317 list <shared_ptr <uint64> > numList;
319 numList.push_front (make_shared <uint64> (StringConverter::ToUInt64 (StringConverter::FromNumber ((uint64) 0xFFFFffffFFFFfffeULL))));
320 numList.push_front (make_shared <uint64> (StringConverter::ToUInt32 (StringConverter::GetTrailingNumber ("str2"))));
321 numList.push_front (make_shared <uint64> (3));
323 list <wstring> testList;
324 wstringstream wstream (L"test");
325 foreach_reverse_ref (uint64 n, numList)
328 wstream << L"str" << n;
329 testList.push_back (wstream.str());
332 stringstream sstream;
335 sstream << "str18446744073709551614,str2" << " str" << StringConverter::Trim (StringConverter::ToSingle (L"\t 3 \r\n"));
336 foreach (const string &s, StringConverter::Split (sstream.str(), ", "))
338 if (testList.front() != StringConverter::ToWide (s))
339 throw TestFailed (SRC_POS);
340 testList.pop_front();
349 bool PlatformTest::TestFlag;