diff options
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/test_filters.cpp | 110 |
1 files changed, 109 insertions, 1 deletions
diff --git a/src/tests/test_filters.cpp b/src/tests/test_filters.cpp index 392fd98ef..d6ad32c73 100644 --- a/src/tests/test_filters.cpp +++ b/src/tests/test_filters.cpp @@ -11,6 +11,8 @@ #include <botan/secqueue.h> #include <botan/pipe.h> #include <botan/filters.h> + #include <botan/comp_filter.h> + #include <botan/cipher_filter.h> #endif namespace Botan_Tests { @@ -26,7 +28,10 @@ class Filter_Tests : public Test results.push_back(test_secqueue()); results.push_back(test_pipe_hash()); + results.push_back(test_pipe_mac()); results.push_back(test_pipe_stream()); + results.push_back(test_pipe_cipher()); + results.push_back(test_pipe_compress()); results.push_back(test_pipe_codec()); results.push_back(test_fork()); @@ -74,6 +79,24 @@ class Filter_Tests : public Test return result; } + Test::Result test_pipe_mac() + { + Test::Result result("Pipe"); + const Botan::SymmetricKey key("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); + Botan::Pipe pipe(new Botan::MAC_Filter("HMAC(SHA-256)", key, 12), + new Botan::Base64_Encoder); + + pipe.process_msg("Hi"); + pipe.process_msg("Bye"); + pipe.process_msg("Hi"); + + result.test_eq("MAC 1", pipe.read_all_as_string(0), "e7NoVbtudgU0QiCZ"); + result.test_eq("MAC 2", pipe.read_all_as_string(1), "LhPnfEG+0rk+Ej6y"); + result.test_eq("MAC 3", pipe.read_all_as_string(2), "e7NoVbtudgU0QiCZ"); + + return result; + } + Test::Result test_pipe_hash() { Test::Result result("Pipe"); @@ -93,13 +116,98 @@ class Filter_Tests : public Test result.test_eq("Message count", pipe.message_count(), 1); result.test_eq("Message size", pipe.remaining(), 32); - std::vector<uint8_t> out(32); + std::vector<uint8_t> out(32), last16(16); + + result.test_eq("Bytes read", pipe.get_bytes_read(0), 0); result.test_eq("Expected read count", pipe.read(&out[0], 5), 5); + result.test_eq("Bytes read", pipe.get_bytes_read(0), 5); + result.test_eq("Peek read", pipe.peek(last16.data(), 18, 11), 16); result.test_eq("Expected read count", pipe.read(&out[5], 17), 17); + result.test_eq("Bytes read", pipe.get_bytes_read(0), 22); + result.test_eq("Remaining", pipe.remaining(), 10); + result.test_eq("Remaining", pipe.remaining(), 10); result.test_eq("Expected read count", pipe.read(&out[22], 12), 10); result.test_eq("Expected read count", pipe.read(&out[0], 1), 0); // no more output + result.test_eq("Bytes read", pipe.get_bytes_read(0), 32); result.test_eq("Expected output", out, "C34AB6ABB7B2BB595BC25C3B388C872FD1D575819A8F55CC689510285E212385"); + result.test_eq("Expected last16", last16, "D1D575819A8F55CC689510285E212385"); + + pipe.reset(); + +#if defined(BOTAN_HAS_CRC32) + pipe.prepend(new Botan::Hash_Filter("CRC32")); + pipe.append(new Botan::Hash_Filter("CRC32")); + pipe.process_msg(std::vector<byte>(1024, 0)); + result.test_eq("Expected CRC32d", pipe.read_all(1), "99841F60"); +#endif + + return result; + } + + Test::Result test_pipe_cipher() + { + Test::Result result("Pipe"); + + Botan::Cipher_Mode_Filter* cipher = + new Botan::Cipher_Mode_Filter(Botan::get_cipher_mode("AES-128/CBC/PKCS7", Botan::ENCRYPTION)); + + // takes ownership of cipher + Botan::Pipe pipe(cipher); + + cipher->set_key(Botan::SymmetricKey("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")); + cipher->set_iv(Botan::InitializationVector("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")); + + pipe.process_msg("Don't use plain CBC mode"); + + auto ciphertext = pipe.read_all(); + + result.test_eq("Ciphertext", ciphertext, "9BDD7300E0CB61CA71FFF957A71605DB6836159C36781246A1ADF50982757F4B"); + + Botan::Cipher_Mode_Filter* dec_cipher = + new Botan::Cipher_Mode_Filter(Botan::get_cipher_mode("AES-128/CBC/PKCS7", Botan::DECRYPTION)); + pipe.append(dec_cipher); + dec_cipher->set_key(Botan::SymmetricKey("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")); + dec_cipher->set_iv(Botan::InitializationVector("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB")); + cipher->set_iv(Botan::InitializationVector("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB")); + + const std::vector<byte> zeros_in(1024); + Botan::DataSource_Memory src(zeros_in); + pipe.start_msg(); + pipe.write(src); + pipe.end_msg(); + + Botan::secure_vector<byte> zeros_out = pipe.read_all(1); + + result.test_eq("Cipher roundtrip", zeros_in, zeros_out); + return result; + } + + Test::Result test_pipe_compress() + { + Test::Result result("Pipe"); + +#if defined(BOTAN_HAS_ZLIB) + Botan::Pipe pipe(new Botan::Compression_Filter("zlib", 9)); + + const std::string input_str = "Hello there HELLO there I said is this thing on?"; + + pipe.start_msg(); + pipe.write(input_str); + pipe.end_msg(); + + auto compr = pipe.read_all(0); + // Can't do equality check on compression because output may differ + result.test_lt("Compressed is shorter", compr.size(), input_str.size()); + + pipe.append(new Botan::Decompression_Filter("zlib")); + pipe.pop(); // remove compressor + + pipe.process_msg(compr); + + std::string decomp = pipe.read_all_as_string(1); + result.test_eq("Decompressed ok", decomp, input_str); +#endif return result; } |