Add additional self tests under debug builds

pull/416/head
Jeffrey Walton 2017-05-05 17:21:08 -04:00
parent 5c1de7b5a5
commit c1377b2955
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
4 changed files with 2166 additions and 1916 deletions

View File

@ -577,7 +577,7 @@ coverage: libcryptopp.a cryptest.exe
./cryptest.exe v
./cryptest.exe tv all
lcov --base-directory . --directory . -c -o cryptest.info
lcov --remove cryptest.info "fips140.*" "*test.*" "bench*.cpp" "validat*.*" "/usr/*" -o cryptest.info
lcov --remove cryptest.info "adhoc.cpp" "wait.*" "network.*" "socketft.*" "fips140.*" "*test.*" "bench*.cpp" "validat*.*" "/usr/*" -o cryptest.info
genhtml -o ./TestCoverage/ -t "cryptest.exe test coverage" --num-spaces 4 cryptest.info
.PHONY: test check

View File

@ -8,6 +8,9 @@
#include "zdeflate.h"
#include "filters.h"
#include "stdcpp.h"
#include "default.h"
#include "zinflate.h"
#include "gzip.h"
#include "hex.h"
#include "asn.h"
@ -25,6 +28,237 @@ NAMESPACE_BEGIN(CryptoPP)
NAMESPACE_BEGIN(Test)
#if (defined(CRYPTOPP_DEBUG) || defined(CRYPTOPP_COVERAGE)) && !defined(CRYPTOPP_IMPORTS)
bool TestGzip()
{
std::cout << "\nTesting Gzip and Gunzip...\n\n";
bool fail = false;
try
{
for (unsigned int i=0; i<128; ++i)
{
std::string src, dest, rec;
unsigned int len = GlobalRNG().GenerateWord32() & 0xffff;
src.resize(len);
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
StringSource(src, true, new Gzip(new StringSink(dest)));
StringSource(dest, true, new Gunzip(new StringSink(rec)));
if (src != rec)
throw Exception(Exception::OTHER_ERROR, "Gzip failed a self test");
}
}
catch(const Exception&)
{
fail = true;
}
if (!fail)
std::cout << "passed: ";
else
std::cout << "FAILED: ";
std::cout << "128 zip and unzip" << std::endl;
return !fail;
}
bool TestZinflate()
{
std::cout << "\nTesting Deflate and Inflate...\n\n";
bool fail = false;
try
{
for (unsigned int i=0; i<128; ++i)
{
std::string src, dest, rec;
unsigned int len = GlobalRNG().GenerateWord32() & 0xffff;
src.resize(len);
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
StringSource(src, true, new Deflator(new StringSink(dest)));
StringSource(dest, true, new Inflator(new StringSink(rec)));
if (src != rec)
throw Exception(Exception::OTHER_ERROR, "Inflate failed a self test");
}
}
catch(const Exception&)
{
fail = true;
}
if (!fail)
std::cout << "passed: ";
else
std::cout << "FAILED: ";
std::cout << "128 deflate and inflate" << std::endl;
return !fail;
}
bool TestMersenne()
{
return true;
}
bool TestDefaultEncryptor()
{
std::cout << "\nTesting DefaultEncryptor...\n\n";
bool fail = false;
try
{
for (unsigned int i=0; i<128; ++i)
{
std::string pwd, src, dest, rec;
unsigned int len = (GlobalRNG().GenerateWord32() & 0xffff) + 8;
src.resize(len);
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
HexEncoder encoder(new StringSink(pwd));
encoder.Put(reinterpret_cast<byte*>(&src[0]), src.length()/4);
encoder.MessageEnd();
StringSource(src, true, new DefaultEncryptor(pwd.c_str(), new StringSink(dest)));
StringSource(dest, true, new DefaultDecryptor(pwd.c_str(), new StringSink(rec)));
if (src != rec)
throw Exception(Exception::OTHER_ERROR, "DefaultEncryptor failed a self test");
}
}
catch(const Exception&)
{
fail = true;
}
if (!fail)
std::cout << "passed: ";
else
std::cout << "FAILED: ";
std::cout << "128 default encryptions and decryptions" << std::endl;
return !fail;
}
bool TestDefaultEncryptorWithMAC()
{
std::cout << "\nTesting DefaultEncryptorWithMAC...\n\n";
bool fail = false;
try
{
for (unsigned int i=0; i<128; ++i)
{
std::string pwd, src, dest, rec;
unsigned int len = (GlobalRNG().GenerateWord32() & 0xffff) + 8;
src.resize(len);
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
HexEncoder encoder(new StringSink(pwd));
encoder.Put(reinterpret_cast<byte*>(&src[0]), src.length()/4);
encoder.MessageEnd();
StringSource(src, true, new DefaultEncryptorWithMAC(pwd.c_str(),new StringSink(dest)));
StringSource(dest, true, new DefaultDecryptorWithMAC(pwd.c_str(), new StringSink(rec)));
if (src != rec)
throw Exception(Exception::OTHER_ERROR, "DefaultEncryptorWithMAC failed a self test");
}
}
catch(const Exception&)
{
fail = true;
}
if (!fail)
std::cout << "passed: ";
else
std::cout << "FAILED: ";
std::cout << "128 default encryptions and decryptions with MAC" << std::endl;
return !fail;
}
bool TestLegacyEncryptor()
{
std::cout << "\nTesting LegacyEncryptor...\n\n";
bool fail = false;
try
{
for (unsigned int i=0; i<128; ++i)
{
std::string pwd, src, dest, rec;
unsigned int len = (GlobalRNG().GenerateWord32() & 0xffff) + 8;
src.resize(len);
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
HexEncoder encoder(new StringSink(pwd));
encoder.Put(reinterpret_cast<byte*>(&src[0]), src.length()/4);
encoder.MessageEnd();
StringSource(src, true, new LegacyEncryptor(pwd.c_str(),new StringSink(dest)));
StringSource(dest, true, new LegacyDecryptor(pwd.c_str(),new StringSink(rec)));
if (src != rec)
throw Exception(Exception::OTHER_ERROR, "LegacyEncryptor failed a self test");
}
}
catch(const Exception&)
{
fail = true;
}
if (!fail)
std::cout << "passed: ";
else
std::cout << "FAILED: ";
std::cout << "128 legacy encryptions and decryptions" << std::endl;
return !fail;
}
bool TestLegacyEncryptorWithMAC()
{
std::cout << "\nTesting LegacyEncryptorWithMAC...\n\n";
bool fail = false;
try
{
for (unsigned int i=0; i<128; ++i)
{
std::string pwd, src, dest, rec;
unsigned int len = (GlobalRNG().GenerateWord32() & 0xffff) + 8;
src.resize(len);
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
HexEncoder encoder(new StringSink(pwd));
encoder.Put(reinterpret_cast<byte*>(&src[0]), src.length()/4);
encoder.MessageEnd();
StringSource(src, true, new LegacyEncryptorWithMAC(pwd.c_str(), new StringSink(dest)));
StringSource(dest, true, new LegacyDecryptorWithMAC(pwd.c_str(), new StringSink(rec)));
if (src != rec)
throw Exception(Exception::OTHER_ERROR, "LegacyEncryptorWithMAC failed a self test");
}
}
catch(const Exception&)
{
fail = true;
}
if (!fail)
std::cout << "passed: ";
else
std::cout << "FAILED: ";
std::cout << "128 legacy encryptions and decryptions with MAC" << std::endl;
return !fail;
}
bool TestRounding()
{
std::cout << "\nTesting RoundUpToMultipleOf/RoundDownToMultipleOf...\n\n";

View File

@ -93,6 +93,14 @@ bool ValidateAll(bool thorough)
pass=TestASN1Parse() && pass;
// Enable during debug for code coverage
pass=ValidateBaseCode() && pass;
// Additional tests due to no coverage
pass=TestGzip() && pass;
pass=TestZinflate() && pass;
pass=TestMersenne() && pass;
pass=TestDefaultEncryptor() && pass;
pass=TestDefaultEncryptorWithMAC() && pass;
pass=TestLegacyEncryptor() && pass;
pass=TestLegacyEncryptorWithMAC() && pass;
#endif
pass=ValidateCRC32() && pass;

View File

@ -120,6 +120,14 @@ bool TestRounding();
bool TestHuffmanCodes();
// http://github.com/weidai11/cryptopp/issues/346
bool TestASN1Parse();
// Additional tests due to no coverage
bool TestGzip();
bool TestZinflate();
bool TestMersenne();
bool TestDefaultEncryptor();
bool TestDefaultEncryptorWithMAC();
bool TestLegacyEncryptor();
bool TestLegacyEncryptorWithMAC();
#endif
#if 1