From 25ddcc0ca61d8d75d6719e00aafead105af8cc3f Mon Sep 17 00:00:00 2001 From: Brian Cox Date: Mon, 4 Sep 2017 11:47:35 -0700 Subject: [PATCH] Break some big unit tests into smaller ones --- src/fco/fcospecattr.cpp | 6 ++ src/fco/fcospecattr.h | 2 + src/fs/fspropdisplayer.cpp | 7 +- src/fs/fspropdisplayer.h | 3 + src/twtest/archive_t.cpp | 78 ++++++++++++------ src/twtest/fcopropimpl_t.cpp | 17 ++-- src/twtest/signature_t.cpp | 127 ++++++++++++++++++++++------- src/twtest/unixfsservices_t.cpp | 138 +++++++++++++++++++++----------- 8 files changed, 269 insertions(+), 109 deletions(-) diff --git a/src/fco/fcospecattr.cpp b/src/fco/fcospecattr.cpp index 8f7cbd9..9394c80 100644 --- a/src/fco/fcospecattr.cpp +++ b/src/fco/fcospecattr.cpp @@ -92,4 +92,10 @@ void cFCOSpecAttr::TraceContents(int dl) const } } +bool cFCOSpecAttr::operator==(const cFCOSpecAttr& rhs) const +{ + return ( (mEmailAddrs == rhs.mEmailAddrs) + && (mName == rhs.mName) + && (mSeverity == rhs.mSeverity)); +} diff --git a/src/fco/fcospecattr.h b/src/fco/fcospecattr.h index 1924bf8..746e9dc 100644 --- a/src/fco/fcospecattr.h +++ b/src/fco/fcospecattr.h @@ -76,6 +76,8 @@ public: void TraceContents(int dl = -1) const; + bool operator==(const cFCOSpecAttr& rhs) const; + DECLARE_SERREFCOUNT() private: cFCOSpecAttr (const cFCOSpecAttr& rhs); // not impl diff --git a/src/fs/fspropdisplayer.cpp b/src/fs/fspropdisplayer.cpp index 375cf67..28e733f 100644 --- a/src/fs/fspropdisplayer.cpp +++ b/src/fs/fspropdisplayer.cpp @@ -509,4 +509,9 @@ bool cFSPropDisplayer::AddGroupnameMapping( const int64& i64gid, const TSTRING& return( ret.second = false ); // returns true if key didn't exist before } - +bool cFSPropDisplayer::operator==(const cFSPropDisplayer& rhs) const +{ + return (mpvPropsWeDisplay == rhs.mpvPropsWeDisplay + && uidToUsername == rhs.uidToUsername + && gidToGroupname == rhs.gidToGroupname); +} diff --git a/src/fs/fspropdisplayer.h b/src/fs/fspropdisplayer.h index 91164b6..780bfe4 100644 --- a/src/fs/fspropdisplayer.h +++ b/src/fs/fspropdisplayer.h @@ -114,6 +114,9 @@ public: virtual bool GetLazy() const; virtual void Read (iSerializer* pSerializer, int32 version = 0); // throw (eSerializer, eArchive) virtual void Write(iSerializer* pSerializer) const; // throw (eSerializer, eArchive) + + bool operator==(const cFSPropDisplayer& rhs) const; // for testing + private: void AddMapping( const iFCOProp* const pProp, const TSTRING& tstrValue, const int propTypeEnum ); // pass in a property value and its string representation. for instance: ( FS::PROP_UID --> username ) diff --git a/src/twtest/archive_t.cpp b/src/twtest/archive_t.cpp index 3db5e8a..cf6410f 100644 --- a/src/twtest/archive_t.cpp +++ b/src/twtest/archive_t.cpp @@ -42,7 +42,7 @@ TSS_EXCEPTION(eTestArchiveError, eError); -void TestArchive() +void TestMemoryArchive() { // cMemoryArchive cMemoryArchive memarch; @@ -52,7 +52,7 @@ void TestArchive() memarch.WriteInt32(3); memarch.WriteInt32(4); - TSTRING s = _T("Weenus"); + TSTRING s = _T("Iridogorgia"); memarch.WriteString(s); memarch.WriteInt64(1234567L); @@ -74,7 +74,7 @@ void TestArchive() TSTRING s2; memarch.ReadString(s2); - TEST(s2.compare(_T("Weenus")) == 0); + TEST(s2.compare(_T("Iridogorgia")) == 0); memarch.ReadInt64(l); TEST(l == 1234567L); @@ -101,37 +101,61 @@ void TestArchive() TEST(memarch.GetMappedOffset() == 4 * sizeof(int32) + sizeof(int32) + 6); TEST(memarch.GetMappedLength() == sizeof(int64)); // TEST(tw_ntohll(*(int64*)memarch.GetMap()) == 1234567L); +} +void TestLockedTemporaryArchive() +{ + TSTRING s = _T("Metallogorgia"); + bool threw = false; // cLockedTemporaryFileArchive TSTRING lockedFileName = TwTestPath("inaccessable_file.bin"); -// lockedFileName += _T("/inaccessable_file.bin"); cLockedTemporaryFileArchive lockedArch; - // try to create an archive using a temp file - lockedArch.OpenReadWrite(); - lockedArch.Close(); + try + { + // try to create an archive using a temp file + lockedArch.OpenReadWrite(); + lockedArch.Close(); + } + catch (...) + { + threw = true; + } - // this should open and lock the file -- shouldn't be able to access it - lockedArch.OpenReadWrite(lockedFileName.c_str()); - lockedArch.Seek(0, cBidirArchive::BEGINNING); + try + { + // this should open and lock the file -- shouldn't be able to access it + lockedArch.OpenReadWrite(lockedFileName.c_str()); + lockedArch.Seek(0, cBidirArchive::BEGINNING); - // shouldn't be able to see these changes - lockedArch.WriteInt32(1); - lockedArch.WriteInt32(2); - lockedArch.WriteInt32(3); - lockedArch.WriteInt32(4); - lockedArch.WriteString(s); - lockedArch.WriteInt64(1234567L); - lockedArch.WriteInt16(42); + // shouldn't be able to see these changes + lockedArch.WriteInt32(1); + lockedArch.WriteInt32(2); + lockedArch.WriteInt32(3); + lockedArch.WriteInt32(4); + lockedArch.WriteString(s); + lockedArch.WriteInt64(1234567L); + lockedArch.WriteInt16(42); - // this should delete the file - lockedArch.Close(); + // this should delete the file + lockedArch.Close(); + } + catch (...) + { + threw = true; + } - // cFileArchive + TEST(!threw); +} + +void TestFileArchive() +{ + bool threw = false; + TSTRING s = _T("Acanthogorgia"); + // cFileArchive TSTRING fileName = TwTestPath("archive_test.bin"); - //fileName += _T("/archive_test.bin"); cFileArchive filearch; filearch.OpenReadWrite(fileName.c_str()); @@ -163,7 +187,7 @@ void TestArchive() TSTRING s3; filearch.ReadString(s3); - TEST(s3.compare(_T("Weenus")) == 0); + TEST(s3.compare(_T("Acanthogorgia")) == 0); filearch.ReadInt64(k); TEST(k == 1234567L); @@ -181,12 +205,16 @@ void TestArchive() } catch (eError& e) { - TEST(false); + threw=true; (void)e; } + + TEST(!threw); } void RegisterSuite_Archive() { - RegisterTest("Archive", "Basic", TestArchive); + RegisterTest("Archive", "MemoryArchive", TestMemoryArchive); + RegisterTest("Archive", "LockedTemporaryArchive)", TestLockedTemporaryArchive); + RegisterTest("Archive", "FileArchive", TestFileArchive); } diff --git a/src/twtest/fcopropimpl_t.cpp b/src/twtest/fcopropimpl_t.cpp index 492a36f..18b472d 100644 --- a/src/twtest/fcopropimpl_t.cpp +++ b/src/twtest/fcopropimpl_t.cpp @@ -35,9 +35,9 @@ #include "core/debug.h" #include "twtest/test.h" -void TestFCOPropImpl() +void TestNumeric() { - cDebug d("TestFCOPropImpl"); + cDebug d("TestNumeric"); d.TraceDebug("Entering...\n"); // print the enum key: @@ -69,12 +69,18 @@ void TestFCOPropImpl() d.TraceDebug("333ui64 == 456ui64 = %d\n", pui64.Compare(&pui64b, iFCOProp::OP_EQ)); TEST( iFCOProp::CMP_FALSE == p2i64.Compare(&pi64, iFCOProp::OP_EQ)); - +} + +void TestStrings() +{ + cDebug d("TestStrings"); cFCOPropTSTRING pt1; cFCOPropTSTRING pt2; pt1.SetValue(TSTRING(_T("bar"))); pt2.SetValue(TSTRING(_T("foo"))); - + cFCOPropInt64 pi64; + pi64.SetValue(8675309); + d.TraceDebug(_T("property TSTRING = (should be \"bar\") %s\n"), pt1.AsString().c_str()); TEST(pt1.AsString() == "bar"); @@ -96,5 +102,6 @@ void TestFCOPropImpl() void RegisterSuite_FCOPropImpl() { - RegisterTest("FCOPropImpl", "Basic", TestFCOPropImpl); + RegisterTest("FCOPropImpl", "Numeric", TestNumeric); + RegisterTest("FCOPropImpl", "Strings", TestStrings); } diff --git a/src/twtest/signature_t.cpp b/src/twtest/signature_t.cpp index 82b4b7d..30e54f6 100644 --- a/src/twtest/signature_t.cpp +++ b/src/twtest/signature_t.cpp @@ -43,11 +43,38 @@ using namespace std; -void TestSignature() +std::string getTestFile() +{ + // Create a file for which we know the signatures + // + //% siggen ~/signature_test.bin + //crc : AAAAAAAAAAy + //md5 : B/Y8ttBnlyw/NPCUu353ao + //crc32 : B1kP9v + //sha : Oia1aljHD793tfj7M55tND+3OG/ + //haval : BL6bFSo0EP5zf8lGSueeed + + static TSTRING sigFileName; + + if (sigFileName.empty()) + { + sigFileName = TwTestPath("signature_test.bin"); + + cFileArchive fileArc; + fileArc.OpenReadWrite(sigFileName.c_str()); + fileArc.WriteBlob("\x1\x2\x3\x4\x5\x6\x7\x8\x9\x0", 10); + fileArc.Close(); + } + + return sigFileName; +} + + +void TestSignatureBasic() { // Signature usage example (?) cCRC32Signature crcSig; - cDebug d("TestSignature"); + cDebug d("TestSignature1"); byte abData[ 64 ]; int i; @@ -58,7 +85,6 @@ void TestSignature() crcSig.Update( &abData[0], 32 ); crcSig.Update( &abData[32], 32 ); crcSig.Finit(); - TCOUT << _T("new way: ") << crcSig.AsString() << endl; cMemoryArchive arch; arch.WriteBlob( &abData[0], 32 ); @@ -69,32 +95,19 @@ void TestSignature() asg.AddSig( &crc ); asg.CalculateSignatures( arch ); - TCOUT << _T("old way: ") << crc.AsString() << endl; - + TEST( crc.AsStringHex() == crcSig.AsStringHex()); +} // Note: The following causes an ASSERT() in iSignature::Compare(), as it should, but // we don't want asserts to occur in a working test suite! // TEST(nullSig.Compare(&checksumSig, iFCOProp::OP_EQ) == iFCOProp::CMP_WRONG_PROP_TYPE); - - // Create a file for which we know the signatures - // - //% siggen ~/signature_test.bin - //crc : AAAAAAAAAAy - //md5 : B/Y8ttBnlyw/NPCUu353ao - //crc32 : B1kP9v - //sha : Oia1aljHD793tfj7M55tND+3OG/ - //haval : BL6bFSo0EP5zf8lGSueeed - - TSTRING sigFileName = TwTestPath("signature_test.bin"); - +void TestChecksum() +{ + TSTRING sigFileName = getTestFile(); cFileArchive fileArc; - fileArc.OpenReadWrite(sigFileName.c_str()); - fileArc.WriteBlob("\x1\x2\x3\x4\x5\x6\x7\x8\x9\x0", 10); - fileArc.Close(); - - + cDebug d("TestChecksum"); // test begins here // general signature & archive variables @@ -145,8 +158,19 @@ void TestSignature() check2.Read(&readSer); TEST(check1.Compare(&check2, iFCOProp::OP_EQ) == iFCOProp::CMP_TRUE); } +} + +void TestCRC32() +{ + TSTRING sigFileName = getTestFile(); + cFileArchive fileArc; + cDebug d("TestCRC32"); + + // general signature & archive variables + byte abBuf[iSignature::SUGGESTED_BLOCK_SIZE]; + const int cbToRead = iSignature::SUGGESTED_BLOCK_SIZE; + int cbRead; - // test CRC32 cCRC32Signature crc1, crc2; d.TraceDetail("Testing CRC32.\n"); @@ -190,7 +214,18 @@ void TestSignature() crc2.Read(&readSer); TEST(crc1.Compare(&crc2, iFCOProp::OP_EQ) == iFCOProp::CMP_TRUE); } +} +void TestMD5() +{ + TSTRING sigFileName = getTestFile(); + cFileArchive fileArc; + cDebug d("TestMD5"); + + // general signature & archive variables + byte abBuf[iSignature::SUGGESTED_BLOCK_SIZE]; + const int cbToRead = iSignature::SUGGESTED_BLOCK_SIZE; + int cbRead; // test MD5 cMD5Signature md51, md52; @@ -235,7 +270,18 @@ void TestSignature() md52.Read(&readSer); TEST(md51.Compare(&md52, iFCOProp::OP_EQ) == iFCOProp::CMP_TRUE); } +} +void TestSHA1() +{ + TSTRING sigFileName = getTestFile(); + cFileArchive fileArc; + cDebug d("TestSHA1"); + + // general signature & archive variables + byte abBuf[iSignature::SUGGESTED_BLOCK_SIZE]; + const int cbToRead = iSignature::SUGGESTED_BLOCK_SIZE; + int cbRead; // test SHA cSHASignature sha1, sha2; @@ -280,8 +326,19 @@ void TestSignature() sha2.Read(&readSer); TEST(sha1.Compare(&sha2, iFCOProp::OP_EQ) == iFCOProp::CMP_TRUE); } +} + +void TestHAVAL() +{ + TSTRING sigFileName = getTestFile(); + cFileArchive fileArc; + cDebug d("TestHAVAL"); + + // general signature & archive variables + byte abBuf[iSignature::SUGGESTED_BLOCK_SIZE]; + const int cbToRead = iSignature::SUGGESTED_BLOCK_SIZE; + int cbRead; - // test HAVAL cHAVALSignature haval1, haval2; d.TraceDetail("Testing HAVAL.\n"); @@ -322,10 +379,16 @@ void TestSignature() haval1.Write(&writeSer); sigArchive.Seek(0, cBidirArchive::BEGINNING); cSerializerImpl readSer(sigArchive, cSerializerImpl::S_READ); - md52.Read(&readSer); + haval2.Read(&readSer); TEST(haval1.Compare(&haval2, iFCOProp::OP_EQ) == iFCOProp::CMP_TRUE); } - +} + +void TestArchiveSigGen() +{ + TSTRING sigFileName = getTestFile(); + cFileArchive fileArc; + cDebug d("TestArchiveSigGen"); // test cArchiveSigGen cArchiveSigGen asgtest; @@ -356,12 +419,16 @@ void TestSignature() TEST(haval3.AsString().compare(_T("BL6bFSo0EP5zf8lGSueeed")) == 0); TEST(haval3.AsStringHex().compare(_T("4be9b152a3410fe737fc9464ae79e79d")) == 0); - fileArc.Close(); - - return; + fileArc.Close(); } void RegisterSuite_Signature() { - RegisterTest("Signature", "Basic", TestSignature); + RegisterTest("Signature", "Basic", TestSignatureBasic); + RegisterTest("Signature", "Checksum", TestChecksum); + RegisterTest("Signature", "CRC32", TestCRC32); + RegisterTest("Signature", "MD5", TestMD5); + RegisterTest("Signature", "SHA1", TestSHA1); + RegisterTest("Signature", "HAVAL", TestHAVAL); + RegisterTest("Signature", "ArchiveSigGen", TestArchiveSigGen); } diff --git a/src/twtest/unixfsservices_t.cpp b/src/twtest/unixfsservices_t.cpp index 45acdbb..70865fe 100644 --- a/src/twtest/unixfsservices_t.cpp +++ b/src/twtest/unixfsservices_t.cpp @@ -44,12 +44,24 @@ using namespace std; -//Tests the functions that are currently implemented in win32fsservices. -void TestUnixFSServices() -{ - cDebug d("TestUnixFSServices"); - // d.RemoveOutTarget(cDebug::OUT_STDOUT); +std::string makeTestFile(const std::string &filename) +{ + TSTRING testfile = TwTestPath(filename); + cFileArchive filearch; + filearch.OpenReadWrite(testfile.c_str()); + filearch.Seek(0, cBidirArchive::BEGINNING); + filearch.WriteString(_T("This is a test")); + filearch.Close(); + + return testfile; +} + +//Tests the functions that are currently implemented in win32fsservices. +void TestReadDir() +{ + cDebug d("TestReadDir"); + // d.RemoveOutTarget(cDebug::OUT_STDOUT); iFSServices* pFSServices = iFSServices::GetInstance(); @@ -75,20 +87,20 @@ void TestUnixFSServices() TEST(n == v.size()); } +} +void TestStat() +{ //Test the Stat method cFSStatArgs stat; - //TO DO: use archive to create this file - TSTRING testfile = TwTestPath("tmp.tmp"); - cFileArchive filearch; - filearch.OpenReadWrite(testfile.c_str()); - filearch.Seek(0, cBidirArchive::BEGINNING); - filearch.WriteString(_T("This is a test")); - filearch.Close(); + std::string testfile = makeTestFile("stat.tmp"); - pFSServices->Stat(testfile, stat); + iFSServices::GetInstance()->Stat(testfile, stat); + TEST("Stat() did not throw"); +/* + cDebug d("TestStat"); //print out the information returned by Stat d.TraceDetail("Information returned by Stat: \n"); d.TraceDetail("Group ID : %-5d \n", stat.gid); @@ -102,65 +114,95 @@ void TestUnixFSServices() d.TraceDetail("Major/minor dev if special: %d \n", stat.rdev); d.TraceDetail("File size: %d \n", stat.size); d.TraceDetail("User ID: %d \n", stat.uid); +*/ +} - //Test GetCurrentDir: +void TestGetCurrentDir() +{ TSTRING currpath; - pFSServices->GetCurrentDir(currpath); - d.TraceDetail("GetCurrentDir returned %s\n", currpath.c_str()); + iFSServices::GetInstance()->GetCurrentDir(currpath); + + TEST("GetCurrentDir() did not throw"); + + //d.TraceDetail("GetCurrentDir returned %s\n", currpath.c_str()); //TEST(currpath == _T("~")); //they should both be ~!! +} - //Test MakeTempFilename +void TestMakeTempFilename() +{ TSTRING _template(_T("twtempXXXXXX")); - pFSServices->MakeTempFilename(_template); - d.TraceDetail("Testing MakeTempFilename: \n"); - d.TraceDetail("%s \n", _template.c_str() ); + iFSServices::GetInstance()->MakeTempFilename(_template); - // Test GetMachineName - d.TraceDetail("Testing GetMachineName:\n"); + TEST("MakeTempFilename() did not throw"); +} + +void TestGetMachineName() +{ TSTRING uname; - pFSServices->GetMachineName(uname); - d.TraceDetail("GetMachineName returned: %s\n", uname.c_str()); + iFSServices::GetInstance()->GetMachineName(uname); - // Test GetHostID - d.TraceDetail("Testing GetHostID:\n"); + TEST("GetMachineName() did not throw"); +} + +void TestGetHostID() +{ TSTRING hostid; - pFSServices->GetHostID(hostid); - d.TraceDetail("GetHostID returned: %s\n", hostid.c_str()); + iFSServices::GetInstance()->GetHostID(hostid); - // Test GetCurrentUserName - d.TraceDetail("Testing GetCurrentUserName:\n"); + TEST("GetHostID() did not throw:"); +} + +void TestGetCurrentUserName() +{ TSTRING username; - TEST( pFSServices->GetCurrentUserName(username) ); - d.TraceDetail("GetCurrentUserName returned: %s\n", username.c_str()); + TEST( iFSServices::GetInstance()->GetCurrentUserName(username) ); +} - // Test GetIPAddress - d.TraceDetail("Testing GetIPAddress:\n"); +void TestGetIPAddress() +{ uint32 ipaddr; - TEST( pFSServices->GetIPAddress( ipaddr ) ); - d.TraceDetail("GetIPAddress returned: %d\n", ipaddr); + TEST( iFSServices::GetInstance()->GetIPAddress( ipaddr ) ); +} - // test GetExecutableFilename - d.TraceDetail("Testing GetExecutableFilename: \n"); +void TestGetExecutableFilename() +{ TSTRING filename = _T("sh"); TSTRING fullpath = _T("/bin/"); - TEST(pFSServices->GetExecutableFilename(fullpath, filename)); + TEST( iFSServices::GetInstance()->GetExecutableFilename(fullpath, filename)); + filename = _T("/bin/sh"); - TEST(pFSServices->GetExecutableFilename(fullpath, filename)); + TEST( iFSServices::GetInstance()->GetExecutableFilename(fullpath, filename)); +} - // test Rename - d.TraceDetail("Testing Rename:\n"); - TSTRING newtestfile = TwTestPath("new.tmp"); - TEST( pFSServices->Rename( testfile, newtestfile ) ); +void TestRename() +{ + std::string testfile = makeTestFile("rename_from"); - // test FileDelete - d.TraceDetail("Testing FileDelete:\n"); - TEST( pFSServices->FileDelete( newtestfile ) ); + TSTRING newtestfile = TwTestPath("rename_to"); + TEST( iFSServices::GetInstance()->Rename( testfile, newtestfile ) ); +} + +void TestFileDelete() +{ + std::string to_rm = makeTestFile("to_rm"); + + TEST( iFSServices::GetInstance()->FileDelete( to_rm ) ); } void RegisterSuite_UnixFSServices() { - RegisterTest("UnixFSServices", "Basic", TestUnixFSServices); + RegisterTest("UnixFSServices", "ReadDir", TestReadDir); + RegisterTest("UnixFSServices", "Stat", TestStat); + RegisterTest("UnixFSServices", "GetCurrentDir", TestGetCurrentDir); + RegisterTest("UnixFSServices", "MakeTempFilename", TestMakeTempFilename); + RegisterTest("UnixFSServices", "GetMachineName", TestGetMachineName); + RegisterTest("UnixFSServices", "GetHostID", TestGetHostID); + RegisterTest("UnixFSServices", "GetCurrentUserName", TestGetCurrentUserName); + RegisterTest("UnixFSServices", "GetIPAddress", TestGetIPAddress); + RegisterTest("UnixFSServices", "GetExecutableFilename", TestGetExecutableFilename); + RegisterTest("UnixFSServices", "Rename", TestRename); + RegisterTest("UnixFSServices", "FileDelete", TestFileDelete); }