aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests
diff options
context:
space:
mode:
authorlloyd <[email protected]>2015-05-13 03:00:10 +0000
committerlloyd <[email protected]>2015-05-13 03:00:10 +0000
commitcb2b4498d2a0c8a15e2a66aa3c5f0d7ffa4cf058 (patch)
treec002be7e34bd0c6973b9d49f92ee7973c5152e59 /src/tests
parenta1524df3c1b7bee3bfd7cab3309f763b3e9599a7 (diff)
Add tests for compression and SRP.
Fix zlib decompression which was not ignoring Z_BUF_ERROR which is harmless in this context as process is already checking avail_in and avail_out after run returns. Bump version to 1.11.17
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/test_compression.cpp125
-rw-r--r--src/tests/test_srp6.cpp39
-rw-r--r--src/tests/tests.cpp2
-rw-r--r--src/tests/tests.h3
4 files changed, 169 insertions, 0 deletions
diff --git a/src/tests/test_compression.cpp b/src/tests/test_compression.cpp
new file mode 100644
index 000000000..2dc8f65e2
--- /dev/null
+++ b/src/tests/test_compression.cpp
@@ -0,0 +1,125 @@
+#include "tests.h"
+#include <botan/compression.h>
+#include <botan/hex.h>
+#include <iostream>
+
+namespace {
+
+using namespace Botan;
+
+// Returns # of bytes of compressed message
+size_t run_compression(Compressor_Transform& c, Transform& d,
+ const secure_vector<byte>& msg)
+ {
+ secure_vector<byte> compressed = msg;
+
+ c.start();
+ c.finish(compressed);
+
+ const size_t c_size = compressed.size();
+
+ secure_vector<byte> decompressed = compressed;
+ d.start();
+ d.finish(decompressed);
+
+ if(msg != decompressed)
+ {
+ std::cout << hex_encode(msg) << " compressed to " << hex_encode(compressed)
+ << " but did not roundtrip - " << hex_encode(decompressed) << "\n";
+ }
+
+ return c_size;
+ }
+
+}
+
+size_t test_compression()
+ {
+ using namespace Botan;
+
+ size_t fails = 0, tests = 0;
+
+ for(auto&& algo : { "zlib", "deflate", "gzip", "bz2", "lzma" })
+ {
+ try
+ {
+ std::unique_ptr<Compressor_Transform> c1(make_compressor(algo, 1));
+ std::unique_ptr<Compressor_Transform> c9(make_compressor(algo, 9));
+ std::unique_ptr<Compressor_Transform> d(make_decompressor(algo));
+
+ if(!c1 || !c9 || !d)
+ continue;
+
+ ++tests;
+
+ const char* text_str =
+ "'Twas brillig, and the slithy toves"
+ "Did gyre and gimble in the wabe:"
+ "All mimsy were the borogoves,"
+ "And the mome raths outgrabe."
+
+ "'Beware the Jabberwock, my son!"
+ "The jaws that bite, the claws that catch!"
+ "Beware the Jubjub bird, and shun"
+ "The frumious Bandersnatch!'"
+
+ "He took his vorpal sword in hand;"
+ "Long time the manxome foe he sought—"
+ "So rested he by the Tumtum tree"
+ "And stood awhile in thought."
+
+ "And, as in uffish thought he stood,"
+ "The Jabberwock, with eyes of flame,"
+ "Came whiffling through the tulgey wood,"
+ "And burbled as it came!"
+
+ "One, two! One, two! And through and through"
+ "The vorpal blade went snicker-snack!"
+ "He left it dead, and with its head"
+ "He went galumphing back."
+
+ "'And hast thou slain the Jabberwock?"
+ "Come to my arms, my beamish boy!"
+ "O frabjous day! Callooh! Callay!'"
+ "He chortled in his joy."
+
+ "’Twas brillig, and the slithy toves"
+ "Did gyre and gimble in the wabe:"
+ "All mimsy were the borogoves,"
+ "And the mome raths outgrabe.";
+
+ const size_t text_len = strlen(text_str);
+
+ const secure_vector<byte> all_zeros(text_len, 0);
+ const secure_vector<byte> random_binary = test_rng().random_vec(text_len);
+
+ const byte* textb = reinterpret_cast<const byte*>(text_str);
+ const secure_vector<byte> text(textb, textb + text_len);
+
+ const size_t c1_z = run_compression(*c1, *d, all_zeros);
+ const size_t c9_z = run_compression(*c9, *d, all_zeros);
+ const size_t c1_r = run_compression(*c1, *d, random_binary);
+ const size_t c9_r = run_compression(*c9, *d, random_binary);
+ const size_t c1_t = run_compression(*c1, *d, text);
+ const size_t c9_t = run_compression(*c9, *d, text);
+
+#define BOTAN_TEST_GTE(x, y, msg) if(x < y) { ++fails; std::cout << "FAIL: " << x << " " << y << " " << msg << '\n'; }
+
+ BOTAN_TEST_GTE(c1_z, c9_z, "Level 9 compresses at least as well as level 1");
+ BOTAN_TEST_GTE(c1_t, c9_t, "Level 9 compresses at least as well as level 1");
+ BOTAN_TEST_GTE(c1_r, c9_r, "Level 9 compresses at least as well as level 1");
+
+ BOTAN_TEST_GTE(c1_t, c1_z/8, "Zeros compress much better than text");
+ BOTAN_TEST_GTE(c1_r, c1_t/2, "Text compress better than random");
+ }
+ catch(std::exception& e)
+ {
+ std::cout << "Failure testing " << algo << " - " << e.what() << "\n";
+ ++fails;
+ }
+ }
+
+ test_report("Compression", tests, fails);
+
+ return fails;
+ }
diff --git a/src/tests/test_srp6.cpp b/src/tests/test_srp6.cpp
new file mode 100644
index 000000000..010576110
--- /dev/null
+++ b/src/tests/test_srp6.cpp
@@ -0,0 +1,39 @@
+#include "tests.h"
+#include <botan/srp6.h>
+#include <iostream>
+
+size_t test_srp6()
+ {
+ using namespace Botan;
+
+ size_t fails = 0;
+
+ const std::string username = "user";
+ const std::string password = "Awellchosen1_to_be_sure_";
+ const std::string group_id = "modp/srp/1024";
+ const std::string hash_id = "SHA-256";
+ auto& rng = test_rng();
+
+ const auto salt = unlock(rng.random_vec(16));
+
+ const BigInt verifier = generate_srp6_verifier(username, password, salt, group_id, hash_id);
+
+ SRP6_Server_Session server;
+
+ const BigInt B = server.step1(verifier, group_id, hash_id, rng);
+
+ auto client = srp6_client_agree(username, password, group_id, hash_id, salt, B, rng);
+
+ const SymmetricKey server_K = server.step2(client.first);
+
+ if(client.second != server_K)
+ {
+ std::cout << "SRP6 computed different keys\n";
+ ++fails;
+ }
+
+ test_report("SRP6", 1, fails);
+
+ return fails;
+
+ }
diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp
index 299d07403..3213429b2 100644
--- a/src/tests/tests.cpp
+++ b/src/tests/tests.cpp
@@ -248,6 +248,7 @@ int main(int argc, char* argv[])
DEF_TEST(cryptobox);
DEF_TEST(tss);
DEF_TEST(rfc6979);
+ DEF_TEST(srp6);
DEF_TEST(bigint);
@@ -272,6 +273,7 @@ int main(int argc, char* argv[])
DEF_TEST(x509);
DEF_TEST(nist_x509);
DEF_TEST(tls);
+ DEF_TEST(compression);
if(tests.empty())
{
diff --git a/src/tests/tests.h b/src/tests/tests.h
index a51f6742f..a086281e7 100644
--- a/src/tests/tests.h
+++ b/src/tests/tests.h
@@ -99,4 +99,7 @@ size_t test_tls();
size_t test_nist_x509();
+size_t test_srp6();
+size_t test_compression();
+
#endif