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

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

unix.superglobalmegacorp.com

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