Annotation of truecrypt/platform/platformtest.cpp, revision 1.1.1.1

1.1       root        1: /*
                      2:  Copyright (c) 2008 TrueCrypt Foundation. All rights reserved.
                      3: 
                      4:  Governed by the TrueCrypt License 2.4 the full text of which is contained
                      5:  in the file License.txt included in TrueCrypt binary and source code
                      6:  distribution packages.
                      7: */
                      8: 
                      9: #include "PlatformTest.h"
                     10: #include "Exception.h"
                     11: #include "FileStream.h"
                     12: #include "Finally.h"
                     13: #include "ForEach.h"
                     14: #include "MemoryStream.h"
                     15: #include "Mutex.h"
                     16: #include "Serializable.h"
                     17: #include "SharedPtr.h"
                     18: #include "StringConverter.h"
                     19: #include "Thread.h"
                     20: 
                     21: namespace TrueCrypt
                     22: {
                     23:        // make_shared_auto, File, Stream, MemoryStream, Endian, Serializer, Serializable
                     24:        void PlatformTest::SerializerTest ()
                     25:        {
                     26:                shared_ptr <Stream> stream (new MemoryStream);
                     27: 
                     28: #if 0
                     29:                make_shared_auto (File, file);
                     30:                finally_do_arg (File&, *file, { if (finally_arg.IsOpen()) finally_arg.Delete(); });
                     31: 
                     32:                try
                     33:                {
                     34:                        file->Open ("truecrypt-serializer-test.tmp", File::CreateReadWrite);
                     35:                        stream = shared_ptr <Stream> (new FileStream (file));
                     36:                }
                     37:                catch (...) { }
                     38: #endif
                     39: 
                     40:                Serializer ser (stream);
                     41: 
                     42:                uint32 i32 = 0x12345678;
                     43:                uint64 i64 = 0x0123456789abcdefULL;
                     44:                string str = "string test";
                     45:                wstring wstr = L"wstring test";
                     46: 
                     47:                list <string> stringList;
                     48:                stringList.push_back (str + "1");
                     49:                stringList.push_back (str + "2");
                     50:                stringList.push_back (str + "3");
                     51: 
                     52:                list <wstring> wstringList;
                     53:                wstringList.push_back (wstr + L"1");
                     54:                wstringList.push_back (wstr + L"2");
                     55:                wstringList.push_back (wstr + L"3");
                     56: 
                     57:                Buffer buffer (10);
                     58:                for (size_t i = 0; i < buffer.Size(); i++)
                     59:                        buffer[i] = (byte) i;
                     60: 
                     61:                ser.Serialize ("int32", i32);
                     62:                ser.Serialize ("int64", i64);
                     63:                ser.Serialize ("string", str);
                     64:                ser.Serialize ("wstring", wstr);
                     65:                ser.Serialize ("stringList", stringList);
                     66:                ser.Serialize ("wstringList", wstringList);
                     67:                ser.Serialize ("buffer", ConstBufferPtr (buffer));
                     68: 
                     69:                ExecutedProcessFailed ex (SRC_POS, "cmd", -123, "error output");
                     70:                ex.Serialize (stream);
                     71: 
                     72:                list < shared_ptr <ExecutedProcessFailed> > exList;
                     73:                exList.push_back (make_shared <ExecutedProcessFailed> (ExecutedProcessFailed (SRC_POS, "cmd", -123, "error output1")));
                     74:                exList.push_back (make_shared <ExecutedProcessFailed> (ExecutedProcessFailed (SRC_POS, "cmd", -234, "error output2")));
                     75:                exList.push_back (make_shared <ExecutedProcessFailed> (ExecutedProcessFailed (SRC_POS, "cmd", -567, "error output3")));
                     76:                Serializable::SerializeList (stream, exList);
                     77: 
                     78: #if 0
                     79:                if (file->IsOpen())
                     80:                        file->SeekAt (0);
                     81: #endif
                     82: 
                     83:                uint32 di32;
                     84:                ser.Deserialize ("int32", di32);
                     85:                if (i32 != di32)
                     86:                        throw TestFailed (SRC_POS);
                     87: 
                     88:                uint64 di64;
                     89:                ser.Deserialize ("int64", di64);
                     90:                if (i64 != di64)
                     91:                        throw TestFailed (SRC_POS);
                     92: 
                     93:                string dstr;
                     94:                ser.Deserialize ("string", dstr);
                     95:                if (str != dstr)
                     96:                        throw TestFailed (SRC_POS);
                     97: 
                     98:                wstring dwstr;
                     99:                ser.Deserialize ("wstring", dwstr);
                    100:                if (str != dstr)
                    101:                        throw TestFailed (SRC_POS);
                    102: 
                    103:                int i = 1;
                    104:                foreach (string item, ser.DeserializeStringList ("stringList"))
                    105:                {
                    106:                        stringstream s;
                    107:                        s << str << i++;
                    108:                        if (item != s.str())
                    109:                                throw TestFailed (SRC_POS);
                    110:                }
                    111: 
                    112:                i = 1;
                    113:                foreach (wstring item, ser.DeserializeWStringList ("wstringList"))
                    114:                {
                    115:                        wstringstream s;
                    116:                        s << wstr << i++;
                    117:                        if (item != s.str())
                    118:                                throw TestFailed (SRC_POS);
                    119:                }
                    120: 
                    121:                Buffer dbuffer (10);
                    122:                ser.Deserialize ("buffer", buffer);
                    123:                for (size_t i = 0; i < buffer.Size(); i++)
                    124:                        if (buffer[i] != (byte) i)
                    125:                                throw TestFailed (SRC_POS);
                    126: 
                    127:                shared_ptr <ExecutedProcessFailed> dex = Serializable::DeserializeNew <ExecutedProcessFailed> (stream);
                    128:                if (!dex
                    129:                        || dex->GetCommand() != "cmd"
                    130:                        || dex->GetExitCode() != -123
                    131:                        || dex->GetErrorOutput() != "error output")
                    132:                        throw TestFailed (SRC_POS);
                    133: 
                    134:                list < shared_ptr <ExecutedProcessFailed> > dexList;
                    135:                Serializable::DeserializeList (stream, dexList);
                    136:                i = 1;
                    137:                foreach_ref (const ExecutedProcessFailed &ex, dexList)
                    138:                {
                    139:                        stringstream s;
                    140:                        s << "error output" << i++;
                    141:                        if (ex.GetErrorOutput() != s.str())
                    142:                                throw TestFailed (SRC_POS);
                    143:                }
                    144:        }
                    145:        
                    146:        // shared_ptr, Mutex, ScopeLock, Thread
                    147:        void PlatformTest::ThreadTest ()
                    148:        {
                    149:                Mutex mutex;
                    150:                mutex.Lock();
                    151:                mutex.Unlock();
                    152: 
                    153:                const int maxThreads = 3;
                    154:                make_shared_auto (int, SharedIntPtr);
                    155:                *SharedIntPtr = 0;
                    156: 
                    157:                for (int i = 0; i < maxThreads; i++)
                    158:                {
                    159:                        Thread t;
                    160:                        t.Start (&ThreadTestProc, (void *)&SharedIntPtr);
                    161:                }
                    162: 
                    163:                for (int i = 0; i < 50; i++)
                    164:                {
                    165:                        if (SharedIntPtr.use_count() == 1 && *SharedIntPtr == maxThreads)
                    166:                                break;
                    167: 
                    168:                        Thread::Sleep(100);
                    169:                }
                    170: 
                    171:                if (SharedIntPtr.use_count() != 1 || *SharedIntPtr != maxThreads)
                    172:                        throw TestFailed (SRC_POS);
                    173:        }
                    174: 
                    175:        TC_THREAD_PROC PlatformTest::ThreadTestProc (void *param)
                    176:        {
                    177:                static Mutex mutex;
                    178:                shared_ptr <int> sharedIntPtr = *(shared_ptr <int> *) param;
                    179: 
                    180:                {
                    181:                        ScopeLock sl (mutex);
                    182:                        (*sharedIntPtr)++;
                    183:                }
                    184: 
                    185:                return 0;
                    186:        }
                    187: 
                    188:        bool PlatformTest::TestAll ()
                    189:        {
                    190:                // Integer types
                    191:                if (sizeof (byte)   != 1 || sizeof (int8)  != 1 || sizeof (__int8)  != 1) throw TestFailed (SRC_POS);
                    192:                if (sizeof (uint16) != 2 || sizeof (int16) != 2 || sizeof (__int16) != 2) throw TestFailed (SRC_POS);
                    193:                if (sizeof (uint32) != 4 || sizeof (int32) != 4 || sizeof (__int32) != 4) throw TestFailed (SRC_POS);
                    194:                if (sizeof (uint64) != 8 || sizeof (int64) != 8 || sizeof (__int64) != 8) throw TestFailed (SRC_POS);
                    195: 
                    196:                // Exception handling
                    197:                TestFlag = false;
                    198:                try
                    199:                {
                    200:                        try
                    201:                        {
                    202:                                throw TestFailed (SRC_POS);
                    203:                        }
                    204:                        catch (...)
                    205:                        {
                    206:                                throw;
                    207:                        }
                    208:                        return false;
                    209:                }
                    210:                catch (Exception &)
                    211:                {
                    212:                        TestFlag = true;
                    213:                }
                    214:                if (!TestFlag)
                    215:                        return false;
                    216: 
                    217:                // RTTI
                    218:                RttiTest rtti;
                    219:                RttiTestBase &rttiBaseRef = rtti;
                    220:                RttiTestBase *rttiBasePtr = &rtti;
                    221: 
                    222:                if (typeid (rttiBaseRef) != typeid (rtti))
                    223:                        throw TestFailed (SRC_POS);
                    224: 
                    225:                if (typeid (*rttiBasePtr) != typeid (rtti))
                    226:                        throw TestFailed (SRC_POS);
                    227: 
                    228:                if (dynamic_cast <RttiTest *> (rttiBasePtr) == nullptr)
                    229:                        throw TestFailed (SRC_POS);
                    230: 
                    231:                try
                    232:                {
                    233:                        dynamic_cast <RttiTest &> (rttiBaseRef);
                    234:                }
                    235:                catch (...)
                    236:                {
                    237:                        throw TestFailed (SRC_POS);
                    238:                }
                    239: 
                    240:                // finally
                    241:                TestFlag = false;
                    242:                {
                    243:                        finally_do ({ TestFlag = true; });
                    244:                        if (TestFlag)
                    245:                                throw TestFailed (SRC_POS);
                    246:                }
                    247:                if (!TestFlag)
                    248:                        throw TestFailed (SRC_POS);
                    249: 
                    250:                TestFlag = false;
                    251:                {
                    252:                        finally_do_arg (bool&, TestFlag, { finally_arg = true; });
                    253:                        if (TestFlag)
                    254:                                throw TestFailed (SRC_POS);
                    255:                }
                    256:                if (!TestFlag)
                    257:                        throw TestFailed (SRC_POS);
                    258: 
                    259:                TestFlag = false;
                    260:                int tesFlag2 = 0;
                    261:                {
                    262:                        finally_do_arg2 (bool&, TestFlag, int&, tesFlag2, { finally_arg = true; finally_arg2 = 2; });
                    263:                        if (TestFlag || tesFlag2 != 0)
                    264:                                throw TestFailed (SRC_POS);
                    265:                }
                    266:                if (!TestFlag || tesFlag2 != 2)
                    267:                        throw TestFailed (SRC_POS);
                    268: 
                    269:                // uint64, vector, list, string, wstring, stringstream, wstringstream
                    270:                // shared_ptr, make_shared, StringConverter, foreach
                    271:                list <shared_ptr <uint64> > numList;
                    272:                
                    273:                numList.push_front (make_shared <uint64> (StringConverter::ToUInt64 (StringConverter::FromNumber (0xFFFFffffFFFFfffeULL))));
                    274:                numList.push_front (make_shared <uint64> (StringConverter::ToUInt32 (StringConverter::GetTrailingNumber ("str2"))));
                    275:                numList.push_front (make_shared <uint64> (3));
                    276: 
                    277:                list <wstring> testList;
                    278:                wstringstream wstream (L"test");
                    279:                foreach_reverse_ref (uint64 n, numList)
                    280:                {
                    281:                        wstream.str (L"");
                    282:                        wstream << L"str" << n;
                    283:                        testList.push_back (wstream.str());
                    284:                }
                    285: 
                    286:                stringstream sstream;
                    287:                sstream << "dummy";
                    288:                sstream.str ("");
                    289:                sstream << "str18446744073709551614,str2" << " str" << StringConverter::Trim (StringConverter::ToSingle (L"\t 3 \r\n"));
                    290:                foreach (const string &s, StringConverter::Split (sstream.str(), ", "))
                    291:                {
                    292:                        if (testList.front() != StringConverter::ToWide (s))
                    293:                                throw TestFailed (SRC_POS);
                    294:                        testList.pop_front();
                    295:                }
                    296: 
                    297:                SerializerTest();
                    298:                ThreadTest();
                    299: 
                    300:                return true;
                    301:        }
                    302: 
                    303:        bool PlatformTest::TestFlag;
                    304: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.