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

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

unix.superglobalmegacorp.com

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