aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2016-07-21 11:28:36 -0400
committerJack Lloyd <[email protected]>2016-07-21 11:28:36 -0400
commit1e9dc69d56fab948546f8b99b7ab0fc38cbeb6e4 (patch)
treec50eabf3f3353df98414655099aa499825338c72 /src/tests
parentcf00df79de807d2ff236cc4c21c1f7549cbfad2f (diff)
parentbc02dc731d1a02780e792b63c6c0a75b9e9b5ff4 (diff)
Merge GH #545 OctetString tests
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/test_octetstring.cpp168
-rw-r--r--src/tests/tests.cpp17
-rw-r--r--src/tests/tests.h5
3 files changed, 190 insertions, 0 deletions
diff --git a/src/tests/test_octetstring.cpp b/src/tests/test_octetstring.cpp
new file mode 100644
index 000000000..ff689518e
--- /dev/null
+++ b/src/tests/test_octetstring.cpp
@@ -0,0 +1,168 @@
+/*
+* (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity
+*
+* Botan is released under the Simplified BSD License (see license.txt)
+*/
+
+#include "tests.h"
+
+#include <botan/symkey.h>
+
+namespace Botan_Tests {
+
+namespace {
+
+using Botan::OctetString;
+
+Test::Result test_from_rng()
+ {
+ Test::Result result("OctetString");
+
+ OctetString os(Test::rng(), 32);
+ result.test_eq("length is 32 bytes", os.size(), 32);
+
+ return result;
+ }
+
+Test::Result test_from_hex()
+ {
+ Test::Result result("OctetString");
+
+ OctetString os("0123456789ABCDEF");
+ result.test_eq("length is 8 bytes", os.size(), 8);
+
+ return result;
+ }
+
+Test::Result test_from_byte()
+ {
+ Test::Result result("OctetString");
+
+ auto rand_bytes = Test::rng().random_vec(8);
+ OctetString os(rand_bytes.data(), rand_bytes.size());
+ result.test_eq("length is 8 bytes", os.size(), 8);
+
+ return result;
+ }
+
+Test::Result test_odd_parity()
+ {
+ Test::Result result("OctetString");
+
+ OctetString os("FFFFFFFFFFFFFFFF");
+ os.set_odd_parity();
+ OctetString expected("FEFEFEFEFEFEFEFE");
+ result.test_eq("odd parity set correctly", os, expected);
+
+ OctetString os2("EFCBDA4FAA997F63");
+ os2.set_odd_parity();
+ OctetString expected2("EFCBDA4FAB987F62");
+ result.test_eq("odd parity set correctly", os2, expected2);
+
+ return result;
+ }
+
+Test::Result test_as_string()
+ {
+ Test::Result result("OctetString");
+
+ OctetString os("0123456789ABCDEF");
+ result.test_eq("OctetString::as_string() returns correct string", os.as_string(), "0123456789ABCDEF");
+
+ return result;
+ }
+
+Test::Result test_xor()
+ {
+ Test::Result result("OctetString");
+
+ OctetString os1("0000000000000000");
+ OctetString os2("FFFFFFFFFFFFFFFF");
+
+ OctetString xor_result = os1 ^ os2;
+ result.test_eq("OctetString XOR operations works as expected", xor_result, os2);
+
+ xor_result = os1;
+ xor_result ^= os2;
+ result.test_eq("OctetString XOR operations works as expected", xor_result, os2);
+
+ xor_result = os2 ^ os2;
+ result.test_eq("OctetString XOR operations works as expected", xor_result, os1);
+
+ OctetString os3("0123456789ABCDEF");
+ xor_result = os3 ^ os2;
+ OctetString expected("FEDCBA9876543210");
+ result.test_eq("OctetString XOR operations works as expected", xor_result, expected);
+
+ return result;
+ }
+
+Test::Result test_equality()
+ {
+ Test::Result result("OctetString");
+
+ OctetString os1("0000000000000000");
+ OctetString os2("FFFFFFFFFFFFFFFF");
+
+ result.confirm("OctetString equality operations works as expected", os1 == os1);
+ result.confirm("OctetString equality operations works as expected", os2 == os2);
+ result.confirm("OctetString equality operations works as expected", os1 != os2);
+
+ return result;
+ }
+
+Test::Result test_append()
+ {
+ Test::Result result("OctetString");
+
+ OctetString os1("0000");
+ OctetString os2("FFFF");
+ OctetString expected("0000FFFF");
+
+ OctetString append_result = os1 + os2;
+
+ result.test_eq("OctetString append operations works as expected", append_result, expected);
+
+ return result;
+ }
+
+class OctetString_Tests : public Test
+ {
+ public:
+ std::vector<Test::Result> run() override
+ {
+ std::vector<Test::Result> results;
+
+ std::vector<std::function<Test::Result()>> fns =
+ {
+ test_from_rng,
+ test_from_hex,
+ test_from_byte,
+ test_odd_parity,
+ test_as_string,
+ test_xor,
+ test_equality,
+ test_append
+ };
+
+ for(size_t i = 0; i != fns.size(); ++i)
+ {
+ try
+ {
+ results.push_back(fns[ i ]());
+ }
+ catch(std::exception& e)
+ {
+ results.push_back(Test::Result::Failure("OctetString tests " + std::to_string(i), e.what()));
+ }
+ }
+
+ return results;
+ }
+ };
+
+BOTAN_REGISTER_TEST("octetstring", OctetString_Tests);
+
+}
+
+}
diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp
index aae4174b9..578418ffb 100644
--- a/src/tests/tests.cpp
+++ b/src/tests/tests.cpp
@@ -190,6 +190,23 @@ bool Test::Result::test_eq(const std::string& what, size_t produced, size_t expe
return test_is_eq(what, produced, expected);
}
+bool Test::Result::test_eq(const std::string& what, OctetString produced, OctetString expected)
+ {
+ std::ostringstream out;
+ out << m_who << " " << what;
+
+ if(produced == expected)
+ {
+ out << " produced expected result " << produced.as_string();
+ return test_success(out.str());
+ }
+ else
+ {
+ out << " produced unexpected result " << produced.as_string() << " expected " << expected.as_string();
+ return test_failure(out.str());
+ }
+ }
+
bool Test::Result::test_lt(const std::string& what, size_t produced, size_t expected)
{
if(produced >= expected)
diff --git a/src/tests/tests.h b/src/tests/tests.h
index 0016a4d90..b33b2837f 100644
--- a/src/tests/tests.h
+++ b/src/tests/tests.h
@@ -11,6 +11,7 @@
#include <botan/build.h>
#include <botan/rng.h>
#include <botan/hex.h>
+#include <botan/symkey.h>
#if defined(BOTAN_HAS_BIGINT)
#include <botan/bigint.h>
@@ -38,6 +39,8 @@ using Botan::byte;
using Botan::BigInt;
#endif
+using Botan::OctetString;
+
class Test_Error : public Botan::Exception
{
public:
@@ -175,6 +178,8 @@ class Test
bool test_eq(const std::string& what, size_t produced, size_t expected);
+ bool test_eq(const std::string& what, OctetString produced, OctetString expected);
+
template<typename I1, typename I2>
bool test_int_eq(I1 x, I2 y, const char* what)
{