diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/StormTest.cpp | 178 | ||||
-rw-r--r-- | test/TLogHelper.cpp | 151 | ||||
-rw-r--r-- | test/stormlib-test-001.txt (renamed from test/stormlib-test.txt) | 0 |
3 files changed, 130 insertions, 199 deletions
diff --git a/test/StormTest.cpp b/test/StormTest.cpp index 0123353..3a3c52d 100644 --- a/test/StormTest.cpp +++ b/test/StormTest.cpp @@ -25,6 +25,7 @@ #ifdef _MSC_VER
#pragma warning(disable: 4505) // 'XXX' : unreferenced local function has been removed
+#include <crtdbg.h>
#pragma comment(lib, "winmm.lib")
#endif
@@ -386,30 +387,6 @@ xchar * StringFromBinary(LPBYTE pbBinary, size_t cbBinary, xchar * szBuffer) return szSaveBuffer;
}
-static void AddStringBeforeExtension(char * szBuffer, LPCSTR szFileName, LPCSTR szExtraString)
-{
- LPCSTR szExtension;
- size_t nLength;
-
- // Get the extension
- szExtension = strrchr(szFileName, '.');
- if(szExtension == NULL)
- szExtension = szFileName + strlen(szFileName);
- nLength = (size_t)(szExtension - szFileName);
-
- // Copy the part before extension
- memcpy(szBuffer, szFileName, nLength);
- szFileName += nLength;
- szBuffer += nLength;
-
- // Append the extra data
- if(szExtraString != NULL)
- strcpy(szBuffer, szExtraString);
-
- // Append the rest of the file name
- strcat(szBuffer, szFileName);
-}
-
static bool CompareBlocks(LPBYTE pbBlock1, LPBYTE pbBlock2, DWORD dwLength, DWORD * pdwDifference)
{
for(DWORD i = 0; i < dwLength; i++)
@@ -424,20 +401,6 @@ static bool CompareBlocks(LPBYTE pbBlock1, LPBYTE pbBlock2, DWORD dwLength, DWOR return true;
}
-static int GetPathSeparatorCount(LPCSTR szPath)
-{
- int nSeparatorCount = 0;
-
- while(szPath[0] != 0)
- {
- if(szPath[0] == '\\' || szPath[0] == '/')
- nSeparatorCount++;
- szPath++;
- }
-
- return nSeparatorCount;
-}
-
template <typename XCHAR>
static const XCHAR * FindNextPathPart(const XCHAR * szPath, size_t nPartCount)
{
@@ -482,18 +445,6 @@ static const XCHAR * GetShortPlainName(const XCHAR * szFileName) return szPlainName;
}
-static void CopyPathPart(char * szBuffer, LPCSTR szPath)
-{
- while(szPath[0] != 0)
- {
- szBuffer[0] = (szPath[0] == '\\' || szPath[0] == '/') ? '/' : szPath[0];
- szBuffer++;
- szPath++;
- }
-
- *szBuffer = 0;
-}
-
static bool CopyStringAndVerifyConversion(
LPCTSTR szFoundFile,
TCHAR * szBufferT,
@@ -2870,62 +2821,6 @@ static DWORD TestOpenArchive_GetFileInfo(LPCTSTR szPlainName1, LPCTSTR szPlainNa return ERROR_SUCCESS;
}
-static DWORD TestOpenArchive_MasterMirror(LPCTSTR szMirrorName, LPCTSTR szMasterName, LPCSTR szFileToExtract, bool bCopyMirrorFile)
-{
- TFileData * pFileData;
- TLogHelper Logger("OpenServerMirror", szMirrorName);
- HANDLE hFile = NULL;
- HANDLE hMpq = NULL;
- DWORD dwVerifyResult;
- TCHAR szMirrorPath[MAX_PATH + MAX_PATH]; // Combined name
- TCHAR szMasterPath[MAX_PATH]; // Original (server) name
- DWORD dwErrCode;
-
- // Create both paths
- dwErrCode = CreateMasterAndMirrorPaths(&Logger, szMirrorPath, szMasterPath, szMirrorName, szMasterName, bCopyMirrorFile);
-
- // Now open both archives as local-server pair
- if(dwErrCode == ERROR_SUCCESS)
- {
- dwErrCode = OpenExistingArchive(&Logger, szMirrorPath, 0, &hMpq);
- }
-
- // The MPQ must be read-only. Writing to mirrored MPQ is not allowed
- if(dwErrCode == ERROR_SUCCESS)
- {
- if(SFileCreateFile(hMpq, "AddedFile.bin", 0, 0x10, 0, MPQ_FILE_COMPRESS, &hFile))
- {
- SFileCloseFile(hFile);
- Logger.PrintMessage("The archive is writable, although it should not be");
- dwErrCode = ERROR_FILE_CORRUPT;
- }
- }
-
- // Verify the file
- if(dwErrCode == ERROR_SUCCESS && szFileToExtract != NULL)
- {
- dwVerifyResult = SFileVerifyFile(hMpq, szFileToExtract, SFILE_VERIFY_ALL);
- if(dwVerifyResult & VERIFY_FILE_ERROR_MASK)
- {
- Logger.PrintMessage("File verification failed");
- dwErrCode = ERROR_FILE_CORRUPT;
- }
- }
-
- // Load the file to memory
- if(dwErrCode == ERROR_SUCCESS && szFileToExtract)
- {
- pFileData = LoadMpqFile(&Logger, hMpq, szFileToExtract);
- if(pFileData != NULL)
- STORM_FREE(pFileData);
- }
-
- if(hMpq != NULL)
- SFileCloseArchive(hMpq);
- return dwErrCode;
-}
-
-
static DWORD TestOpenArchive_VerifySignature(LPCTSTR szPlainName, LPCTSTR szOriginalName)
{
TLogHelper Logger("VerifySignatureTest", szPlainName);
@@ -3332,77 +3227,6 @@ static DWORD TestAddFile_ListFileTest(LPCTSTR szSourceMpq, bool bShouldHaveListF SFileCloseArchive(hMpq);
return dwErrCode;
}
-/*
-static DWORD TestCreateArchive_Deprotect(LPCSTR szPlainName)
-{
- TLogHelper Logger("DeprotectTest", szPlainName);
- HANDLE hMpq1 = NULL;
- HANDLE hMpq2 = NULL;
- char szMpqName1[MAX_PATH];
- char szMpqName2[MAX_PATH];
- BYTE FileHash1[MD5_DIGEST_SIZE];
- BYTE FileHash2[MD5_DIGEST_SIZE];
- DWORD dwFileCount1 = 0;
- DWORD dwFileCount2 = 0;
- DWORD dwTestFlags = SEARCH_FLAG_LOAD_FILES | SEARCH_FLAG_HASH_FILES;
- DWORD dwErrCode = ERROR_SUCCESS;
-
- // First copy: The original (untouched) file
- if(dwErrCode == ERROR_SUCCESS)
- {
- AddStringBeforeExtension(szMpqName1, szPlainName, "_original");
- dwErrCode = OpenExistingArchiveWithCopy(&Logger, szPlainName, szMpqName1, &hMpq1);
- if(dwErrCode != ERROR_SUCCESS)
- Logger.PrintMessage("Open failed: %s", szMpqName1);
- }
-
- // Second copy: Will be deprotected
- if(dwErrCode == ERROR_SUCCESS)
- {
- AddStringBeforeExtension(szMpqName2, szPlainName, "_deprotected");
- dwErrCode = OpenExistingArchiveWithCopy(&Logger, szPlainName, szMpqName2, &hMpq2);
- if(dwErrCode != ERROR_SUCCESS)
- Logger.PrintMessage("Open failed: %s", szMpqName2);
- }
-
- // Deprotect the second map
- if(dwErrCode == ERROR_SUCCESS)
- {
- SFileSetCompactCallback(hMpq2, CompactCallback, &Logger);
- if(!SFileCompactArchive(hMpq2, NULL, false))
- dwErrCode = Logger.PrintError("Failed to deprotect archive %s", szMpqName2);
- }
-
- // Calculate number of files and compare their hash (archive 1)
- if(dwErrCode == ERROR_SUCCESS)
- {
- memset(FileHash1, 0, sizeof(FileHash1));
- dwErrCode = SearchArchive(&Logger, hMpq1, dwTestFlags, &dwFileCount1, FileHash1);
- }
-
- // Calculate number of files and compare their hash (archive 2)
- if(dwErrCode == ERROR_SUCCESS)
- {
- memset(FileHash1, 0, sizeof(FileHash2));
- dwErrCode = SearchArchive(&Logger, hMpq2, dwTestFlags, &dwFileCount2, FileHash2);
- }
-
- if(dwErrCode == ERROR_SUCCESS)
- {
- if(dwFileCount1 != dwFileCount2)
- Logger.PrintMessage("Different file count (%u in %s; %u in %s)", dwFileCount1, szMpqName1, dwFileCount2, szMpqName2);
- if(memcmp(FileHash1, FileHash2, MD5_DIGEST_SIZE))
- Logger.PrintMessage("Different file hash (%s vs %s)", szMpqName1, szMpqName2);
- }
-
- // Close both MPQs
- if(hMpq2 != NULL)
- SFileCloseArchive(hMpq2);
- if(hMpq1 != NULL)
- SFileCloseArchive(hMpq1);
- return dwErrCode;
-}
-*/
static DWORD TestCreateArchive_EmptyMpq(LPCTSTR szPlainName, DWORD dwCreateFlags)
{
diff --git a/test/TLogHelper.cpp b/test/TLogHelper.cpp index 4dcc514..0570c65 100644 --- a/test/TLogHelper.cpp +++ b/test/TLogHelper.cpp @@ -28,10 +28,12 @@ #define fmt_I64X_a "%llX"
#endif
-#ifdef __STORMLIB_SELF__
-#define TEST_MIN STORMLIB_MIN
-#else
+#ifdef __CASCLIB_SELF__
#define TEST_MIN CASCLIB_MIN
+#define TEST_PRINT_PREFIX false
+#else
+#define TEST_MIN STORMLIB_MIN
+#define TEST_PRINT_PREFIX true
#endif
//-----------------------------------------------------------------------------
@@ -44,7 +46,6 @@ inline DWORD TestInterlockedIncrement(DWORD * PtrValue) #elif defined(__GNUC__)
return __sync_add_and_fetch(PtrValue, 1);
#else
-#define INTERLOCKED_NOT_SUPPORTED
return ++(*PtrValue);
#endif
}
@@ -91,6 +92,22 @@ void TestStrCopy(char * szTarget, size_t cchTarget, const wchar_t * szSource, si }
}
+void TestStrCopy(wchar_t * szTarget, size_t cchTarget, const char * szSource, size_t cchSource = -1)
+{
+ size_t cchToCopy;
+
+ if(cchTarget > 0)
+ {
+ // Make sure we know the length
+ if(cchSource == -1)
+ cchSource = strlen(szSource);
+ cchToCopy = TEST_MIN((cchTarget - 1), cchSource);
+
+ mbstowcs(szTarget, szSource, cchToCopy);
+ szTarget[cchToCopy] = 0;
+ }
+}
+
wchar_t * CopyFormatCharacter(wchar_t * szBuffer, const wchar_t *& szFormat)
{
static const wchar_t * szStringFormat = _T("%s");
@@ -202,42 +219,73 @@ class TLogHelper TLogHelper(const char * szNewMainTitle = NULL, const TCHAR * szNewSubTitle1 = NULL, const TCHAR * szNewSubTitle2 = NULL)
{
- TCHAR szMainTitleT[0x80];
-
// Fill the variables
memset(this, 0, sizeof(TLogHelper));
-
UserString = "";
+ TotalFiles = 1;
UserCount = 1;
UserTotal = 1;
- // Fill the test line structure
+#ifdef TEST_PLATFORM_WINDOWS
+ InitializeCriticalSection(&Locker);
+ TickCount = GetTickCount();
+#endif
+
+ // Remember the startup time
+ SetStartTime();
+
+ // Save all three titles
szMainTitle = szNewMainTitle;
szSubTitle1 = szNewSubTitle1;
szSubTitle2 = szNewSubTitle2;
- // Copy the UNICODE main title
- StringCopy(szMainTitleT, _countof(szMainTitleT), szMainTitle);
-
// Print the initial information
if(szNewMainTitle != NULL)
{
+#ifdef __CASCLIB_SELF__
+ char szMainTitleT[0x100] = {0};
+ size_t nLength;
+
+ nLength = TestStrPrintf(szMainTitleT, _countof(szMainTitleT), "-- \"%s\" --", szNewMainTitle);
+ while(nLength < 90)
+ szMainTitleT[nLength++] = '-';
+ if(nLength < sizeof(szMainTitleT))
+ szMainTitleT[nLength++] = 0;
+
+ printf("%s\n", szMainTitleT);
+#endif
+
+#ifdef __STORMLIB_SELF__
+ TCHAR szMainTitleT[0x100] = {0};
+
+ // Copy the UNICODE main title
+ TestStrCopy(szMainTitleT, _countof(szMainTitleT), szMainTitle);
+
if(szSubTitle1 != NULL && szSubTitle2 != NULL)
nPrevPrinted = _tprintf(_T("\rRunning %s (%s+%s) ..."), szMainTitleT, szSubTitle1, szSubTitle2);
else if(szSubTitle1 != NULL)
nPrevPrinted = _tprintf(_T("\rRunning %s (%s) ..."), szMainTitleT, szSubTitle1);
else
nPrevPrinted = _tprintf(_T("\rRunning %s ..."), szMainTitleT);
+#endif
}
}
- TLogHelper::~TLogHelper()
+ ~TLogHelper()
{
// Print a verdict, if no verdict was printed yet
if(bMessagePrinted == false)
{
PrintVerdict(ERROR_SUCCESS);
}
+
+#ifdef TEST_PLATFORM_WINDOWS
+ DeleteCriticalSection(&Locker);
+#endif
+
+#ifdef __CASCLIB_SELF__
+ printf("\n");
+#endif
}
//
@@ -267,7 +315,7 @@ class TLogHelper //
template <typename XCHAR>
- DWORD PrintWithClreol(const XCHAR * szFormat, va_list argList, bool bPrintPrefix, bool bPrintLastError, bool bPrintEndOfLine)
+ DWORD PrintWithClreol(const XCHAR * szFormat, va_list argList, bool bPrintLastError, bool bPrintEndOfLine)
{
char * szBufferPtr;
char * szBufferEnd;
@@ -276,6 +324,7 @@ class TLogHelper DWORD dwErrCode = Test_GetLastError();
XCHAR szMessage[0x200];
char szBuffer[0x200];
+ bool bPrintPrefix = TEST_PRINT_PREFIX;
// Always start the buffer with '\r'
szBufferEnd = szBuffer + _countof(szBuffer);
@@ -353,7 +402,7 @@ class TLogHelper if(ProgressReady())
{
va_start(argList, szFormat);
- PrintWithClreol(szFormat, argList, true, false, false);
+ PrintWithClreol(szFormat, argList, false, false);
va_end(argList);
}
}
@@ -364,7 +413,7 @@ class TLogHelper va_list argList;
va_start(argList, szFormat);
- PrintWithClreol(szFormat, argList, true, false, true);
+ PrintWithClreol(szFormat, argList, false, true);
va_end(argList);
}
@@ -384,7 +433,7 @@ class TLogHelper int nResult;
va_start(argList, szFormat);
- nResult = PrintWithClreol(szFormat, argList, true, true, true);
+ nResult = PrintWithClreol(szFormat, argList, true, true);
va_end(argList);
return nResult;
@@ -404,17 +453,17 @@ class TLogHelper TCHAR szSaveMainTitle[0x80];
// Set both to NULL so they won't be printed
- StringCopy(szSaveMainTitle, _countof(szSaveMainTitle), szMainTitle);
+ TestStrCopy(szSaveMainTitle, _countof(szSaveMainTitle), szMainTitle);
+ szMainTitle = NULL;
szSubTitle1 = NULL;
szSubTitle2 = NULL;
- szMainTitle = NULL;
// Print the final information
if(szSaveMainTitle[0] != 0)
{
if(DontPrintResult == false)
{
- LPCTSTR szVerdict = (dwErrCode == ERROR_SUCCESS) ? _T("succeeded") : _T("failed");
+ const TCHAR * szVerdict = (dwErrCode == ERROR_SUCCESS) ? _T("succeeded") : _T("failed");
if(szSaveSubTitle1 != NULL && szSaveSubTitle2 != NULL)
PrintMessage(_T("%s (%s+%s) %s."), szSaveMainTitle, szSaveSubTitle1, szSaveSubTitle2, szVerdict);
@@ -435,6 +484,24 @@ class TLogHelper }
//
+ // Locking functions (Windows only)
+ //
+
+ void Lock()
+ {
+#ifdef TEST_PLATFORM_WINDOWS
+ EnterCriticalSection(&Locker);
+#endif
+ }
+
+ void Unlock()
+ {
+#ifdef TEST_PLATFORM_WINDOWS
+ LeaveCriticalSection(&Locker);
+#endif
+ }
+
+ //
// Time functions
//
@@ -483,16 +550,56 @@ class TLogHelper return (DWORD)(EndTime - StartTime);
}
+ void IncrementTotalBytes(ULONGLONG IncrementValue)
+ {
+ // For some weird reason, this is measurably faster then InterlockedAdd64
+ Lock();
+ TotalBytes = TotalBytes + IncrementValue;
+ Unlock();
+ }
+
+ void FormatTotalBytes(char * szBuffer, size_t ccBuffer)
+ {
+ ULONGLONG Bytes = TotalBytes;
+ ULONGLONG Divider = 1000000000;
+ char * szBufferEnd = szBuffer + ccBuffer;
+ bool bDividingOn = false;
+
+ while((szBuffer + 4) < szBufferEnd && Divider > 0)
+ {
+ // Are we already dividing?
+ if(bDividingOn)
+ {
+ szBuffer += TestStrPrintf(szBuffer, ccBuffer, " %03u", (DWORD)(Bytes / Divider));
+ Bytes = Bytes % Divider;
+ }
+ else if(Bytes > Divider)
+ {
+ szBuffer += TestStrPrintf(szBuffer, ccBuffer, "%u", (DWORD)(Bytes / Divider));
+ Bytes = Bytes % Divider;
+ bDividingOn = true;
+ }
+ Divider /= 1000;
+ }
+ }
+
+#ifdef TEST_PLATFORM_WINDOWS
+ CRITICAL_SECTION Locker;
+#endif
+
+ ULONGLONG TotalBytes; // For user's convenience: Total number of bytes
+ ULONGLONG ByteCount; // For user's convenience: Current number of bytes
ULONGLONG StartTime; // Start time of an operation, in milliseconds
ULONGLONG EndTime; // End time of an operation, in milliseconds
const char * UserString;
- unsigned int UserCount;
- unsigned int UserTotal;
+ DWORD UserCount;
+ DWORD UserTotal;
DWORD TickCount;
DWORD TimeTrigger; // For triggering elapsed timers
+ DWORD TotalFiles; // For user's convenience: Total number of files
+ DWORD FileCount; // For user's convenience: Curernt number of files
DWORD DontPrintResult:1; // If true, supress printing result from the destructor
-
protected:
const char * szMainTitle; // Title of the text (usually name)
diff --git a/test/stormlib-test.txt b/test/stormlib-test-001.txt index c910278..c910278 100644 --- a/test/stormlib-test.txt +++ b/test/stormlib-test-001.txt |