From 4ff254311ac65d8ec1ebae76a8d259c24d741150 Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Mon, 4 Nov 2024 17:21:50 -0500 Subject: [PATCH] Add edge case tests --- test/test_hmac.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/test/test_hmac.cpp b/test/test_hmac.cpp index 5c5dcd9..93da74f 100644 --- a/test/test_hmac.cpp +++ b/test/test_hmac.cpp @@ -81,6 +81,73 @@ void basic_tests() } } +template +void test_edges() +{ + boost::crypt::hmac hmac_tester; + const char* msg {"The quick brown fox jumps over the lazy dog"}; + + // Usage before init + const auto state1 {hmac_tester.process_bytes(msg, std::strlen(msg))}; + BOOST_TEST(state1 == boost::crypt::hasher_state::state_error); + + // Init with nullptr + const auto state2 {hmac_tester.init("nullptr", 0)}; + BOOST_TEST(state2 == boost::crypt::hasher_state::null); + + // Good init + const auto state3 {hmac_tester.init("key", 3)}; + BOOST_TEST(state3 == boost::crypt::hasher_state::success); + + // Pass in nullptr + const auto state4 {hmac_tester.process_bytes("msg", 0)}; + BOOST_TEST(state4 == boost::crypt::hasher_state::null); + + // Good pass + const auto state5 {hmac_tester.process_bytes(msg, std::strlen(msg))}; + BOOST_TEST(state5 == boost::crypt::hasher_state::success); + + // Get digest twice + hmac_tester.get_digest(); + const auto res {hmac_tester.get_digest()}; + + for (const auto byte : res) + { + BOOST_TEST_EQ(byte, static_cast(0)); + } + + const char* big_key {"This is a really really really really really really really really really really" + " really really really really really really really really really really" + " really really really really really really really really really really" + " really really really really really really really really really really" + " really really really really really really really really really really" + " really really really really really really really really really really" + " really really really really really really really really really really" + " really really really really really really really really really really" + " really really really really really really really really really really" + " really really really really really really really really really really" + " long key"}; + + const auto state6 {hmac_tester.init(big_key, std::strlen(big_key))}; + BOOST_TEST(state6 == boost::crypt::hasher_state::success); + + // Init from keys + const auto outer_key {hmac_tester.get_outer_key()}; + const auto inner_key {hmac_tester.get_inner_key()}; + + hmac_tester.process_bytes(msg, std::strlen(msg)); + const auto res2 {hmac_tester.get_digest()}; + + hmac_tester.init_from_keys(inner_key, outer_key); + hmac_tester.process_bytes(msg, std::strlen(msg)); + const auto res3 {hmac_tester.get_digest()}; + + for (std::size_t i {}; i < res2.size(); ++i) + { + BOOST_TEST_EQ(res2[i], res3[i]); + } +} + int main() { basic_tests(); @@ -88,5 +155,10 @@ int main() basic_tests(); basic_tests(); + test_edges(); + test_edges(); + test_edges(); + test_edges(); + return boost::report_errors(); }