Fold compressor and default encryptor tests

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

View File

@ -94,15 +94,10 @@ bool ValidateAll(bool thorough)
pass=TestHuffmanCodes() && pass; pass=TestHuffmanCodes() && pass;
// http://github.com/weidai11/cryptopp/issues/346 // http://github.com/weidai11/cryptopp/issues/346
pass=TestASN1Parse() && pass; pass=TestASN1Parse() && pass;
// Enable during debug for code coverage
pass=ValidateBaseCode() && pass;
// Additional tests due to no coverage // Additional tests due to no coverage
pass=TestGzip() && pass; pass=ValidateBaseCode() && pass;
pass=TestZinflate() && pass; pass=TestCompressors() && pass;
pass=TestDefaultEncryptor() && pass; pass=TestEncryptors() && pass;
pass=TestDefaultEncryptorWithMAC() && pass;
pass=TestLegacyEncryptor() && pass;
pass=TestLegacyEncryptorWithMAC() && pass;
#endif #endif
pass=ValidateCRC32() && pass; pass=ValidateCRC32() && pass;

View File

@ -126,13 +126,9 @@ 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 // Additional tests due to no coverage
bool TestGzip(); bool TestCompressors();
bool TestZinflate(); bool TestEncryptors();
bool TestMersenne(); bool TestMersenne();
bool TestDefaultEncryptor();
bool TestDefaultEncryptorWithMAC();
bool TestLegacyEncryptor();
bool TestLegacyEncryptorWithMAC();
#endif #endif
#if 1 #if 1