summaryrefslogtreecommitdiff
path: root/validat0.cpp
diff options
context:
space:
mode:
authorJeffrey Walton <noloader@gmail.com>2017-05-08 06:08:27 -0400
committerJeffrey Walton <noloader@gmail.com>2017-05-08 06:08:27 -0400
commit7ba33868e06e0d5f15ce82327a60ca25f8f5bdd1 (patch)
treeb725e46ee1797c27c75c58dd618464540bba9bb2 /validat0.cpp
parent798f1c9ed8b1e9d2da6ddc0b15a0b8d797b1088b (diff)
downloadcryptopp-git-7ba33868e06e0d5f15ce82327a60ca25f8f5bdd1.tar.gz
Add zLib compressor and decompressor tests
Diffstat (limited to 'validat0.cpp')
-rw-r--r--validat0.cpp58
1 files changed, 56 insertions, 2 deletions
diff --git a/validat0.cpp b/validat0.cpp
index 651c4f82..629fa11b 100644
--- a/validat0.cpp
+++ b/validat0.cpp
@@ -11,6 +11,7 @@
#include "default.h"
#include "zinflate.h"
#include "gzip.h"
+#include "zlib.h"
#include "hex.h"
#include "asn.h"
@@ -31,7 +32,7 @@ NAMESPACE_BEGIN(Test)
bool TestCompressors()
{
std::cout << "\nTesting Compressors and Decompressors...\n\n";
- bool fail1 = false, fail2 = false;
+ bool fail1 = false, fail2 = false, fail3 = false;
try
{
@@ -168,8 +169,61 @@ bool TestCompressors()
std::cout << "FAILED:";
std::cout << " 128 deflates and inflates\n";
+ // **************************************************************
+
+ 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 ZlibCompressor(new StringSink(dest)));
+ StringSource(dest, true, new ZlibDecompressor (new StringSink(rec)));
+ if (src != rec)
+ throw Exception(Exception::OTHER_ERROR, "ZlibCompressor failed to decompress stream");
+
+ // Tamper
+ try {
+ StringSource(dest.substr(0, len-2), true, new ZlibDecompressor (new StringSink(rec)));
+ throw Exception(Exception::OTHER_ERROR, "ZlibCompressor failed to detect a truncated stream");
+ } catch(const Exception&) {}
+ }
+ }
+ catch(const Exception&)
+ {
+ fail3 = true;
+ }
+
+ // **************************************************************
+
+ // Unzip random data. See if we can induce a crash
+ for (unsigned int i=0; i<128; i++)
+ {
+ std::string src, dest;
+ unsigned int len = GlobalRNG().GenerateWord32() & 0xffff;
+
+ src.resize(len);
+ GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
+
+ try {
+ StringSource(src, true, new ZlibDecompressor(new StringSink(dest)));
+ } catch(const Exception&) { }
+ }
+
+ if (!fail3)
+ std::cout << "passed:";
+ else
+ std::cout << "FAILED:";
+ std::cout << " 128 zlib decompress and compress" << std::endl;
+
+ // **************************************************************
+
std::cout.flush();
- return !fail1 && !fail2;
+ return !fail1 && !fail2 && !fail3;
}
bool TestEncryptors()