"Fossies" - the Fresh Open Source Software Archive

Member "src/Platform/PlatformTest.cpp" (10 Oct 2018, 8920 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "PlatformTest.cpp" see the Fossies "Dox" file reference documentation.

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