fix silly recent breakage in signature.cpp; clean up signature unit tests and minor tweaks to the test framework

This commit is contained in:
Brian Cox 2019-10-16 22:59:21 -07:00
parent 4e0c1249e4
commit c617ed56bb
3 changed files with 65 additions and 247 deletions

View File

@ -483,6 +483,7 @@ cMD5Signature::cMD5Signature()
#else #else
memset(mMD5Info.digest, 0, sizeof(mMD5Info.digest)); memset(mMD5Info.digest, 0, sizeof(mMD5Info.digest));
#endif #endif
memset(md5_digest, 0, MD5_DIGEST_LENGTH); memset(md5_digest, 0, MD5_DIGEST_LENGTH);
} }
@ -522,9 +523,9 @@ void cMD5Signature::Finit()
MD5Final(&mMD5Info); MD5Final(&mMD5Info);
#if HAVE_MEMCPY_S #if HAVE_MEMCPY_S
memcpy_s(mMD5Info.digest, sizeof(mMD5Info.digest), md5_digest, MD5_DIGEST_LENGTH); memcpy_s(md5_digest, MD5_DIGEST_LENGTH, mMD5Info.digest, sizeof(mMD5Info.digest));
#else #else
memcpy(mMD5Info.digest, md5_digest, MD5_DIGEST_LENGTH); memcpy(md5_digest, mMD5Info.digest, MD5_DIGEST_LENGTH);
#endif #endif
#endif #endif

View File

@ -103,275 +103,95 @@ void TestSignatureBasic()
// TEST(nullSig.Compare(&checksumSig, iFCOProp::OP_EQ) == iFCOProp::CMP_WRONG_PROP_TYPE); // TEST(nullSig.Compare(&checksumSig, iFCOProp::OP_EQ) == iFCOProp::CMP_WRONG_PROP_TYPE);
void TestChecksum() template<class T>
void testSignature(const TSTRING& sigFileName, const TSTRING& expectedBase64, const TSTRING& expectedHex)
{ {
TSTRING sigFileName = getTestFile();
cFileArchive fileArc; cFileArchive fileArc;
cDebug d("TestChecksum"); cDebug d("testSignature");
// test begins here
// general signature & archive variables // general signature & archive variables
uint8_t abBuf[iSignature::SUGGESTED_BLOCK_SIZE]; uint8_t abBuf[iSignature::SUGGESTED_BLOCK_SIZE] = {0};
const int cbToRead = iSignature::SUGGESTED_BLOCK_SIZE; const int cbToRead = iSignature::SUGGESTED_BLOCK_SIZE;
int cbRead; int cbRead =0;
// test checksum // test checksum
cChecksumSignature check1, check2; T sig1, sig2;
d.TraceDetail("Testing checksum.\n"); d.TraceDetail("Testing signature...\n");
// check1 // sig1
fileArc.OpenRead(sigFileName.c_str()); fileArc.OpenRead(sigFileName.c_str());
check1.Init(); sig1.Init();
do do
{ {
cbRead = fileArc.ReadBlob(abBuf, cbToRead); cbRead = fileArc.ReadBlob(abBuf, cbToRead);
check1.Update(abBuf, cbRead); sig1.Update(abBuf, cbRead);
} while (cbRead == cbToRead); } while (cbRead == cbToRead);
check1.Finit(); sig1.Finit();
fileArc.Close(); fileArc.Close();
// check2 // sig2
fileArc.OpenRead(sigFileName.c_str()); fileArc.OpenRead(sigFileName.c_str());
check2.Init(); sig2.Init();
do do
{ {
cbRead = fileArc.ReadBlob(abBuf, cbToRead); cbRead = fileArc.ReadBlob(abBuf, cbToRead);
check2.Update(abBuf, cbRead); sig2.Update(abBuf, cbRead);
} while (cbRead == cbToRead); } while (cbRead == cbToRead);
check2.Finit(); sig2.Finit();
fileArc.Close(); fileArc.Close();
// compare to each other and the known values // compare to each other and the known values
TEST(check1.Compare(&check2, iFCOProp::OP_EQ) == iFCOProp::CMP_TRUE); TEST(sig1.Compare(&sig2, iFCOProp::OP_EQ) == iFCOProp::CMP_TRUE);
TEST(check1.AsString().compare(_T("AAAAAAAAAAt")) == 0); if (!expectedBase64.empty())
{
TEST(sig1.AsString().compare(expectedBase64) == 0);
}
if (!expectedHex.empty())
{
TEST(sig1.AsStringHex().compare(expectedHex) == 0);
}
// test write capabilities // test write capabilities
{ {
cMemoryArchive sigArchive; cMemoryArchive sigArchive;
cSerializerImpl writeSer(sigArchive, cSerializerImpl::S_WRITE); cSerializerImpl writeSer(sigArchive, cSerializerImpl::S_WRITE);
check1.Write(&writeSer); sig1.Write(&writeSer);
sigArchive.Seek(0, cBidirArchive::BEGINNING); sigArchive.Seek(0, cBidirArchive::BEGINNING);
cSerializerImpl readSer(sigArchive, cSerializerImpl::S_READ); cSerializerImpl readSer(sigArchive, cSerializerImpl::S_READ);
check2.Read(&readSer); sig2.Read(&readSer);
TEST(check1.Compare(&check2, iFCOProp::OP_EQ) == iFCOProp::CMP_TRUE); TEST(sig1.Compare(&sig2, iFCOProp::OP_EQ) == iFCOProp::CMP_TRUE);
} }
} }
void TestChecksum()
{
testSignature<cChecksumSignature>(getTestFile(), _T("AAAAAAAAAAt"), _T(""));
}
void TestCRC32() void TestCRC32()
{ {
TSTRING sigFileName = getTestFile(); testSignature<cCRC32Signature>(getTestFile(), _T("B1kP9v"), _T("7590ff6f"));
cFileArchive fileArc;
cDebug d("TestCRC32");
// general signature & archive variables
uint8_t abBuf[iSignature::SUGGESTED_BLOCK_SIZE];
const int cbToRead = iSignature::SUGGESTED_BLOCK_SIZE;
int cbRead;
// test CRC32
cCRC32Signature crc1, crc2;
d.TraceDetail("Testing CRC32.\n");
// crc1
fileArc.OpenRead(sigFileName.c_str());
crc1.Init();
do
{
cbRead = fileArc.ReadBlob(abBuf, cbToRead);
crc1.Update(abBuf, cbRead);
} while (cbRead == cbToRead);
crc1.Finit();
fileArc.Close();
// crc2
fileArc.OpenRead(sigFileName.c_str());
crc2.Init();
do
{
cbRead = fileArc.ReadBlob(abBuf, cbToRead);
crc2.Update(abBuf, cbRead);
} while (cbRead == cbToRead);
crc2.Finit();
fileArc.Close();
// compare to each other and the known values
TEST(crc1.Compare(&crc2, iFCOProp::OP_EQ) == iFCOProp::CMP_TRUE);
TEST(crc1.AsString().compare(_T("B1kP9v")) == 0);
TEST(crc1.AsStringHex().compare(_T("7590ff6f")) == 0);
// test write capabilities
{
cMemoryArchive sigArchive;
cSerializerImpl writeSer(sigArchive, cSerializerImpl::S_WRITE);
crc1.Write(&writeSer);
sigArchive.Seek(0, cBidirArchive::BEGINNING);
cSerializerImpl readSer(sigArchive, cSerializerImpl::S_READ);
crc2.Read(&readSer);
TEST(crc1.Compare(&crc2, iFCOProp::OP_EQ) == iFCOProp::CMP_TRUE);
}
} }
void TestMD5() void TestMD5()
{ {
TSTRING sigFileName = getTestFile(); testSignature<cMD5Signature>(getTestFile(),
cFileArchive fileArc; _T("B/Y8ttBnlyw/NPCUu353ao"),
cDebug d("TestMD5"); _T("7f63cb6d067972c3f34f094bb7e776a8"));
// general signature & archive variables
uint8_t abBuf[iSignature::SUGGESTED_BLOCK_SIZE];
const int cbToRead = iSignature::SUGGESTED_BLOCK_SIZE;
int cbRead;
// test MD5
cMD5Signature md51, md52;
d.TraceDetail("Testing MD5.\n");
// md51
fileArc.OpenRead(sigFileName.c_str());
md51.Init();
do
{
cbRead = fileArc.ReadBlob(abBuf, cbToRead);
md51.Update(abBuf, cbRead);
} while (cbRead == cbToRead);
md51.Finit();
fileArc.Close();
// md52
fileArc.OpenRead(sigFileName.c_str());
md52.Init();
do
{
cbRead = fileArc.ReadBlob(abBuf, cbToRead);
md52.Update(abBuf, cbRead);
} while (cbRead == cbToRead);
md52.Finit();
fileArc.Close();
// compare to each other and the known values
TEST(md51.Compare(&md52, iFCOProp::OP_EQ) == iFCOProp::CMP_TRUE);
TEST(md51.AsString().compare(_T("B/Y8ttBnlyw/NPCUu353ao")) == 0);
TEST(md51.AsStringHex().compare(_T("7f63cb6d067972c3f34f094bb7e776a8")) == 0);
// test write capabilities
{
cMemoryArchive sigArchive;
cSerializerImpl writeSer(sigArchive, cSerializerImpl::S_WRITE);
md51.Write(&writeSer);
sigArchive.Seek(0, cBidirArchive::BEGINNING);
cSerializerImpl readSer(sigArchive, cSerializerImpl::S_READ);
md52.Read(&readSer);
TEST(md51.Compare(&md52, iFCOProp::OP_EQ) == iFCOProp::CMP_TRUE);
}
} }
void TestSHA1() void TestSHA1()
{ {
TSTRING sigFileName = getTestFile(); testSignature<cSHASignature>(getTestFile(),
cFileArchive fileArc; _T("Oia1aljHD793tfj7M55tND+3OG/"),
cDebug d("TestSHA1"); _T("e89ad5a9631c3efdded7e3ecce79b4d0fedce1bf"));
// general signature & archive variables
uint8_t abBuf[iSignature::SUGGESTED_BLOCK_SIZE];
const int cbToRead = iSignature::SUGGESTED_BLOCK_SIZE;
int cbRead;
// test SHA
cSHASignature sha1, sha2;
d.TraceDetail("Testing SHA.\n");
// sha1
fileArc.OpenRead(sigFileName.c_str());
sha1.Init();
do
{
cbRead = fileArc.ReadBlob(abBuf, cbToRead);
sha1.Update(abBuf, cbRead);
} while (cbRead == cbToRead);
sha1.Finit();
fileArc.Close();
// sha2
fileArc.OpenRead(sigFileName.c_str());
sha2.Init();
do
{
cbRead = fileArc.ReadBlob(abBuf, cbToRead);
sha2.Update(abBuf, cbRead);
} while (cbRead == cbToRead);
sha2.Finit();
fileArc.Close();
// compare to each other and the known values
TEST(sha1.Compare(&sha2, iFCOProp::OP_EQ) == iFCOProp::CMP_TRUE);
TEST(sha1.AsString().compare(_T("Oia1aljHD793tfj7M55tND+3OG/")) == 0);
TEST(sha1.AsStringHex().compare(_T("e89ad5a9631c3efdded7e3ecce79b4d0fedce1bf")) == 0);
// test write capabilities
{
cMemoryArchive sigArchive;
cSerializerImpl writeSer(sigArchive, cSerializerImpl::S_WRITE);
sha1.Write(&writeSer);
sigArchive.Seek(0, cBidirArchive::BEGINNING);
cSerializerImpl readSer(sigArchive, cSerializerImpl::S_READ);
sha2.Read(&readSer);
TEST(sha1.Compare(&sha2, iFCOProp::OP_EQ) == iFCOProp::CMP_TRUE);
}
} }
void TestHAVAL() void TestHAVAL()
{ {
TSTRING sigFileName = getTestFile(); testSignature<cHAVALSignature>(getTestFile(),
cFileArchive fileArc; _T("BL6bFSo0EP5zf8lGSueeed"),
cDebug d("TestHAVAL"); _T("4be9b152a3410fe737fc9464ae79e79d"));
// general signature & archive variables
uint8_t abBuf[iSignature::SUGGESTED_BLOCK_SIZE];
const int cbToRead = iSignature::SUGGESTED_BLOCK_SIZE;
int cbRead;
// test HAVAL
cHAVALSignature haval1, haval2;
d.TraceDetail("Testing HAVAL.\n");
// haval1
fileArc.OpenRead(sigFileName.c_str());
haval1.Init();
do
{
cbRead = fileArc.ReadBlob(abBuf, cbToRead);
haval1.Update(abBuf, cbRead);
} while (cbRead == cbToRead);
haval1.Finit();
fileArc.Close();
// haval2
fileArc.OpenRead(sigFileName.c_str());
haval2.Init();
do
{
cbRead = fileArc.ReadBlob(abBuf, cbToRead);
haval2.Update(abBuf, cbRead);
} while (cbRead == cbToRead);
haval2.Finit();
fileArc.Close();
// compare to each other and the known values
TEST(haval1.Compare(&haval2, iFCOProp::OP_EQ) == iFCOProp::CMP_TRUE);
TEST(haval1.AsString().compare(_T("BL6bFSo0EP5zf8lGSueeed")) == 0);
TEST(haval1.AsStringHex().compare(_T("4be9b152a3410fe737fc9464ae79e79d")) == 0);
// test write capabilities
{
cMemoryArchive sigArchive;
cSerializerImpl writeSer(sigArchive, cSerializerImpl::S_WRITE);
haval1.Write(&writeSer);
sigArchive.Seek(0, cBidirArchive::BEGINNING);
cSerializerImpl readSer(sigArchive, cSerializerImpl::S_READ);
haval2.Read(&readSer);
TEST(haval1.Compare(&haval2, iFCOProp::OP_EQ) == iFCOProp::CMP_TRUE);
}
} }
void TestArchiveSigGen() void TestArchiveSigGen()
@ -412,28 +232,27 @@ void TestArchiveSigGen()
fileArc.Close(); fileArc.Close();
} }
void assertMD5(const std::string& source, const std::string& expectedHex) template<class T>
void assertSignature(const std::string& source, const std::string& expectedHex)
{ {
// Signature usage example (?) // Signature usage example (?)
cMD5Signature md5Sig; T signature;
md5Sig.Init(); signature.Init();
md5Sig.Update((const uint8_t*)source.c_str(), source.length()); signature.Update((const uint8_t*)source.c_str(), source.length());
md5Sig.Finit(); signature.Finit();
TEST(md5Sig.AsStringHex() == expectedHex); TEST(signature.AsStringHex() == expectedHex);
}
void assertMD5(const std::string& source, const std::string& expectedHex)
{
assertSignature<cMD5Signature>(source, expectedHex);
} }
void assertSHA1(const std::string& source, const std::string& expectedHex) void assertSHA1(const std::string& source, const std::string& expectedHex)
{ {
// Signature usage example (?) assertSignature<cSHASignature>(source, expectedHex);
cSHASignature shaSig;
shaSig.Init();
shaSig.Update((const uint8_t*)source.c_str(), source.length());
shaSig.Finit();
TEST(shaSig.AsStringHex() == expectedHex);
} }

View File

@ -250,6 +250,8 @@ static void RunTest(const std::string& suiteName, const std::string& testName, T
std::stringstream sstr; std::stringstream sstr;
sstr << "Test " << suiteName << "/" << testName << ": <unknown>"; sstr << "Test " << suiteName << "/" << testName << ": <unknown>";
error_strings.push_back(sstr.str()); error_strings.push_back(sstr.str());
failed_count++;
} }
} }
@ -472,7 +474,6 @@ int _tmain(int argc, TCHAR** argv)
RegisterSuites(); RegisterSuites();
std::string arg1 = argv[1]; std::string arg1 = argv[1];
if (arg1 == "all" || arg1 == "--all") if (arg1 == "all" || arg1 == "--all")
{ {
RunAllTests(); RunAllTests();
@ -501,20 +502,17 @@ int _tmain(int argc, TCHAR** argv)
catch (eError& error) catch (eError& error)
{ {
cTWUtil::PrintErrorMsg(error); cTWUtil::PrintErrorMsg(error);
ASSERT(false); failed_count++;
return 1;
} }
catch (std::exception& error) catch (std::exception& error)
{ {
TCERR << "Caught std::exception: " << error.what() << std::endl; TCERR << "Caught std::exception: " << error.what() << std::endl;
ASSERT(false); failed_count++;
return 1;
} }
catch (...) catch (...)
{ {
TCERR << _T("Unhandled exception caught!"); TCERR << _T("Unhandled exception caught!");
ASSERT(false); failed_count++;
return 1;
} }
// make sure all the reference counted objects have been destroyed // make sure all the reference counted objects have been destroyed