Add additional self tests under debug builds
parent
5c1de7b5a5
commit
c1377b2955
|
|
@ -577,7 +577,7 @@ coverage: libcryptopp.a cryptest.exe
|
||||||
./cryptest.exe v
|
./cryptest.exe v
|
||||||
./cryptest.exe tv all
|
./cryptest.exe tv all
|
||||||
lcov --base-directory . --directory . -c -o cryptest.info
|
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
|
genhtml -o ./TestCoverage/ -t "cryptest.exe test coverage" --num-spaces 4 cryptest.info
|
||||||
|
|
||||||
.PHONY: test check
|
.PHONY: test check
|
||||||
|
|
|
||||||
234
validat0.cpp
234
validat0.cpp
|
|
@ -8,6 +8,9 @@
|
||||||
#include "zdeflate.h"
|
#include "zdeflate.h"
|
||||||
#include "filters.h"
|
#include "filters.h"
|
||||||
#include "stdcpp.h"
|
#include "stdcpp.h"
|
||||||
|
#include "default.h"
|
||||||
|
#include "zinflate.h"
|
||||||
|
#include "gzip.h"
|
||||||
#include "hex.h"
|
#include "hex.h"
|
||||||
#include "asn.h"
|
#include "asn.h"
|
||||||
|
|
||||||
|
|
@ -25,6 +28,237 @@ NAMESPACE_BEGIN(CryptoPP)
|
||||||
NAMESPACE_BEGIN(Test)
|
NAMESPACE_BEGIN(Test)
|
||||||
|
|
||||||
#if (defined(CRYPTOPP_DEBUG) || defined(CRYPTOPP_COVERAGE)) && !defined(CRYPTOPP_IMPORTS)
|
#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()
|
bool TestRounding()
|
||||||
{
|
{
|
||||||
std::cout << "\nTesting RoundUpToMultipleOf/RoundDownToMultipleOf...\n\n";
|
std::cout << "\nTesting RoundUpToMultipleOf/RoundDownToMultipleOf...\n\n";
|
||||||
|
|
|
||||||
|
|
@ -93,6 +93,14 @@ bool ValidateAll(bool thorough)
|
||||||
pass=TestASN1Parse() && pass;
|
pass=TestASN1Parse() && pass;
|
||||||
// Enable during debug for code coverage
|
// Enable during debug for code coverage
|
||||||
pass=ValidateBaseCode() && pass;
|
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
|
#endif
|
||||||
|
|
||||||
pass=ValidateCRC32() && pass;
|
pass=ValidateCRC32() && pass;
|
||||||
|
|
|
||||||
|
|
@ -120,6 +120,14 @@ bool TestRounding();
|
||||||
bool TestHuffmanCodes();
|
bool TestHuffmanCodes();
|
||||||
// http://github.com/weidai11/cryptopp/issues/346
|
// http://github.com/weidai11/cryptopp/issues/346
|
||||||
bool TestASN1Parse();
|
bool TestASN1Parse();
|
||||||
|
// Additional tests due to no coverage
|
||||||
|
bool TestGzip();
|
||||||
|
bool TestZinflate();
|
||||||
|
bool TestMersenne();
|
||||||
|
bool TestDefaultEncryptor();
|
||||||
|
bool TestDefaultEncryptorWithMAC();
|
||||||
|
bool TestLegacyEncryptor();
|
||||||
|
bool TestLegacyEncryptorWithMAC();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if 1
|
#if 1
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue