Fold compressor and default encryptor tests

This commit is contained in:
Jeffrey Walton 2017-05-06 19:20:57 -04:00
parent bdd82d781e
commit 14236ce94b
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
3 changed files with 53 additions and 82 deletions

View File

@ -28,10 +28,10 @@ NAMESPACE_BEGIN(CryptoPP)
NAMESPACE_BEGIN(Test)
#if defined(CRYPTOPP_EXTENDED_VALIDATION)
bool TestGzip()
bool TestCompressors()
{
std::cout << "\nTesting Gzip and Gunzip...\n\n";
bool fail = false;
std::cout << "\nTesting Compressors and Decompressors...\n\n";
bool fail1 = false, fail2 = false;
try
{
@ -57,9 +57,11 @@ bool TestGzip()
}
catch(const Exception&)
{
fail = true;
fail1 = true;
}
// **************************************************************
// Unzip random data. See if we can induce a crash
for (unsigned int i=0; i<128; i++)
{
@ -84,19 +86,13 @@ bool TestGzip()
} catch(const Exception&) { }
}
if (!fail)
if (!fail1)
std::cout << "passed:";
else
std::cout << "FAILED:";
std::cout << " 256 zips and unzips" << std::endl;
std::cout << " 128 zips and unzips" << std::endl;
return !fail;
}
bool TestZinflate()
{
std::cout << "\nTesting Deflate and Inflate...\n\n";
bool fail = false;
// **************************************************************
try
{
@ -117,15 +113,17 @@ bool TestZinflate()
try {
StringSource(dest.substr(0, len-2), true, new Inflator(new StringSink(rec)));
std::cout << "Deflate failed to detect a truncated stream\n";
fail = true;
fail2 = true;
} catch(const Exception& ex) { }
}
}
catch(const Exception&)
{
fail = true;
fail2 = true;
}
// **************************************************************
for (unsigned int i=0; i<128; i++)
{
// See if we can induce a crash
@ -164,20 +162,20 @@ bool TestZinflate()
} catch(const Exception&) { }
}
if (!fail)
if (!fail2)
std::cout << "passed:";
else
std::cout << "FAILED:";
std::cout << " 256 deflates and inflates\n";
std::cout << " 128 deflates and inflates\n";
std::cout.flush();
return !fail;
return !fail1 && !fail2;
}
bool TestDefaultEncryptor()
bool TestEncryptors()
{
std::cout << "\nTesting DefaultEncryptor...\n\n";
bool fail = false;
std::cout << "\nTesting Default Encryptors and Decryptors...\n\n";
bool fail1 = false, fail2 = false, fail3 = false, fail4 = false;
try
{
@ -201,22 +199,16 @@ bool TestDefaultEncryptor()
}
catch(const Exception&)
{
fail = true;
fail1 = true;
}
if (!fail)
if (!fail1)
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
{
@ -241,14 +233,14 @@ bool TestDefaultEncryptorWithMAC()
try {
StringSource(dest.substr(0, len-2), true, new DefaultDecryptorWithMAC(pwd.c_str(), new StringSink(rec)));
std::cout << "FAILED: DefaultDecryptorWithMAC failed to detect a truncated stream\n";
fail = true;
fail2 = true;
} catch(const Exception& ex) { }
try {
// tamper salt
dest[DefaultDecryptorWithMAC::SALTLENGTH/2] ^= 0x01;
StringSource(dest, true, new DefaultDecryptorWithMAC(pwd.c_str(), new StringSink(rec)));
std::cout << "FAILED: DefaultDecryptorWithMAC failed to detect a tampered salt\n";
fail = true;
fail2 = true;
} catch(const Exception& ex) { }
try {
// undo previous tamper
@ -257,7 +249,7 @@ bool TestDefaultEncryptorWithMAC()
dest[DefaultDecryptorWithMAC::SALTLENGTH+DefaultDecryptorWithMAC::KEYLENGTH/2] ^= 0x01;
StringSource(dest, true, new DefaultDecryptorWithMAC(pwd.c_str(), new StringSink(rec)));
std::cout << "FAILED: DefaultDecryptorWithMAC failed to detect a tampered keycheck\n";
fail = true;
fail2 = true;
} catch(const Exception& ex) { }
try {
// undo previous tamper
@ -266,28 +258,22 @@ bool TestDefaultEncryptorWithMAC()
dest[dest.length()-2] ^= 0x01;
StringSource(dest, true, new DefaultDecryptorWithMAC(pwd.c_str(), new StringSink(rec)));
std::cout << "FAILED: DefaultDecryptorWithMAC failed to detect a tampered data\n";
fail = true;
fail2 = true;
} catch(const Exception& ex) { }
}
}
catch(const Exception&)
{
fail = true;
fail2 = true;
}
if (!fail)
if (!fail2)
std::cout << "passed:";
else
std::cout << "FAILED:";
std::cout << " 256 default encryptions and decryptions with MAC" << std::endl;
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
{
@ -311,22 +297,16 @@ bool TestLegacyEncryptor()
}
catch(const Exception&)
{
fail = true;
fail3 = true;
}
if (!fail)
if (!fail3)
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
{
@ -351,14 +331,14 @@ bool TestLegacyEncryptorWithMAC()
try {
StringSource(dest.substr(0, len-2), true, new LegacyDecryptorWithMAC(pwd.c_str(), new StringSink(rec)));
std::cout << "FAILED: LegacyEncryptorWithMAC failed to detect a truncated stream\n";
fail = true;
fail4 = true;
} catch(const Exception& ex) { }
try {
// tamper salt
dest[LegacyEncryptorWithMAC::SALTLENGTH/2] ^= 0x01;
StringSource(dest, true, new LegacyDecryptorWithMAC(pwd.c_str(), new StringSink(rec)));
std::cout << "FAILED: LegacyEncryptorWithMAC failed to detect a tampered salt\n";
fail = true;
fail4 = true;
} catch(const Exception& ex) { }
try {
// undo previous tamper
@ -367,7 +347,7 @@ bool TestLegacyEncryptorWithMAC()
dest[LegacyEncryptorWithMAC::SALTLENGTH+LegacyEncryptorWithMAC::KEYLENGTH/2] ^= 0x01;
StringSource(dest, true, new LegacyDecryptorWithMAC(pwd.c_str(), new StringSink(rec)));
std::cout << "FAILED: LegacyEncryptorWithMAC failed to detect a tampered keycheck\n";
fail = true;
fail4 = true;
} catch(const Exception& ex) { }
try {
// undo previous tamper
@ -376,22 +356,22 @@ bool TestLegacyEncryptorWithMAC()
dest[dest.length()-2] ^= 0x01;
StringSource(dest, true, new LegacyDecryptorWithMAC(pwd.c_str(), new StringSink(rec)));
std::cout << "FAILED: LegacyEncryptorWithMAC failed to detect a tampered data\n";
fail = true;
fail4 = true;
} catch(const Exception& ex) { }
}
}
catch(const Exception&)
{
fail = true;
fail4 = true;
}
if (!fail)
if (!fail4)
std::cout << "passed:";
else
std::cout << "FAILED:";
std::cout << " 128 legacy encryptions and decryptions with MAC" << std::endl;
return !fail;
return !fail1 && !fail2 && !fail3 && !fail4;
}
bool TestRounding()

View File

@ -94,15 +94,10 @@ bool ValidateAll(bool thorough)
pass=TestHuffmanCodes() && pass;
// http://github.com/weidai11/cryptopp/issues/346
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=TestDefaultEncryptor() && pass;
pass=TestDefaultEncryptorWithMAC() && pass;
pass=TestLegacyEncryptor() && pass;
pass=TestLegacyEncryptorWithMAC() && pass;
pass=ValidateBaseCode() && pass;
pass=TestCompressors() && pass;
pass=TestEncryptors() && pass;
#endif
pass=ValidateCRC32() && pass;
@ -2731,56 +2726,56 @@ bool ValidateBaseCode()
try {HexEncoder().IsolatedInitialize(g_nullNameValuePairs);}
catch(const Exception&) {fail=true;}
std::cout << (fail ? "FAILED:" : "passed:");
std::cout << " Hex Encoding\n";
std::cout << " Hex Encoding\n";
pass = pass && !fail;
fail = !TestFilter(HexDecoder().Ref(), (const byte *)hexEncoded, strlen(hexEncoded), data, 255);
try {HexDecoder().IsolatedInitialize(g_nullNameValuePairs);}
catch(const Exception&) {fail=true;}
std::cout << (fail ? "FAILED:" : "passed:");
std::cout << " Hex Decoding\n";
std::cout << " Hex Decoding\n";
pass = pass && !fail;
fail = !TestFilter(Base32Encoder().Ref(), data, 255, (const byte *)base32Encoded, strlen(base32Encoded));
try {Base32Encoder().IsolatedInitialize(g_nullNameValuePairs);}
catch(const Exception&) {fail=true;}
std::cout << (fail ? "FAILED:" : "passed:");
std::cout << " Base32 Encoding\n";
std::cout << " Base32 Encoding\n";
pass = pass && !fail;
fail = !TestFilter(Base32Decoder().Ref(), (const byte *)base32Encoded, strlen(base32Encoded), data, 255);
try {Base32Decoder().IsolatedInitialize(g_nullNameValuePairs);}
catch(const Exception&) {fail=true;}
std::cout << (fail ? "FAILED:" : "passed:");
std::cout << " Base32 Decoding\n";
std::cout << " Base32 Decoding\n";
pass = pass && !fail;
fail = !TestFilter(Base64Encoder(new HexEncoder).Ref(), data, 255, (const byte *)base64AndHexEncoded, strlen(base64AndHexEncoded));
try {Base64Encoder().IsolatedInitialize(g_nullNameValuePairs);}
catch(const Exception&) {fail=true;}
std::cout << (fail ? "FAILED:" : "passed:");
std::cout << " Base64 Encoding\n";
std::cout << " Base64 Encoding\n";
pass = pass && !fail;
fail = !TestFilter(HexDecoder(new Base64Decoder).Ref(), (const byte *)base64AndHexEncoded, strlen(base64AndHexEncoded), data, 255);
try {Base64Decoder().IsolatedInitialize(g_nullNameValuePairs);}
catch(const Exception&) {fail=true;}
std::cout << (fail ? "FAILED:" : "passed:");
std::cout << " Base64 Decoding\n";
std::cout << " Base64 Decoding\n";
pass = pass && !fail;
fail = !TestFilter(Base64URLEncoder(new HexEncoder).Ref(), data, 255, (const byte *)base64URLAndHexEncoded, strlen(base64URLAndHexEncoded));
try {Base64URLEncoder().IsolatedInitialize(g_nullNameValuePairs);}
catch(const Exception&) {fail=true;}
std::cout << (fail ? "FAILED:" : "passed:");
std::cout << " Base64 URL Encoding\n";
std::cout << " Base64 URL Encoding\n";
pass = pass && !fail;
fail = !TestFilter(HexDecoder(new Base64URLDecoder).Ref(), (const byte *)base64URLAndHexEncoded, strlen(base64URLAndHexEncoded), data, 255);
try {Base64URLDecoder().IsolatedInitialize(g_nullNameValuePairs);}
catch(const Exception&) {fail=true;}
std::cout << (fail ? "FAILED:" : "passed:");
std::cout << " Base64 URL Decoding\n";
std::cout << " Base64 URL Decoding\n";
pass = pass && !fail;
return pass;

View File

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