Skip to content

Commit 4ff2543

Browse files
committed
Add edge case tests
1 parent a93583c commit 4ff2543

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

test/test_hmac.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,84 @@ void basic_tests()
8181
}
8282
}
8383

84+
template <typename HasherType>
85+
void test_edges()
86+
{
87+
boost::crypt::hmac<HasherType> hmac_tester;
88+
const char* msg {"The quick brown fox jumps over the lazy dog"};
89+
90+
// Usage before init
91+
const auto state1 {hmac_tester.process_bytes(msg, std::strlen(msg))};
92+
BOOST_TEST(state1 == boost::crypt::hasher_state::state_error);
93+
94+
// Init with nullptr
95+
const auto state2 {hmac_tester.init("nullptr", 0)};
96+
BOOST_TEST(state2 == boost::crypt::hasher_state::null);
97+
98+
// Good init
99+
const auto state3 {hmac_tester.init("key", 3)};
100+
BOOST_TEST(state3 == boost::crypt::hasher_state::success);
101+
102+
// Pass in nullptr
103+
const auto state4 {hmac_tester.process_bytes("msg", 0)};
104+
BOOST_TEST(state4 == boost::crypt::hasher_state::null);
105+
106+
// Good pass
107+
const auto state5 {hmac_tester.process_bytes(msg, std::strlen(msg))};
108+
BOOST_TEST(state5 == boost::crypt::hasher_state::success);
109+
110+
// Get digest twice
111+
hmac_tester.get_digest();
112+
const auto res {hmac_tester.get_digest()};
113+
114+
for (const auto byte : res)
115+
{
116+
BOOST_TEST_EQ(byte, static_cast<std::uint8_t>(0));
117+
}
118+
119+
const char* big_key {"This is a really really really really really really really really really really"
120+
" really really really really really really really really really really"
121+
" really really really really really really really really really really"
122+
" really really really really really really really really really really"
123+
" really really really really really really really really really really"
124+
" really really really really really really really really really really"
125+
" really really really really really really really really really really"
126+
" really really really really really really really really really really"
127+
" really really really really really really really really really really"
128+
" really really really really really really really really really really"
129+
" long key"};
130+
131+
const auto state6 {hmac_tester.init(big_key, std::strlen(big_key))};
132+
BOOST_TEST(state6 == boost::crypt::hasher_state::success);
133+
134+
// Init from keys
135+
const auto outer_key {hmac_tester.get_outer_key()};
136+
const auto inner_key {hmac_tester.get_inner_key()};
137+
138+
hmac_tester.process_bytes(msg, std::strlen(msg));
139+
const auto res2 {hmac_tester.get_digest()};
140+
141+
hmac_tester.init_from_keys(inner_key, outer_key);
142+
hmac_tester.process_bytes(msg, std::strlen(msg));
143+
const auto res3 {hmac_tester.get_digest()};
144+
145+
for (std::size_t i {}; i < res2.size(); ++i)
146+
{
147+
BOOST_TEST_EQ(res2[i], res3[i]);
148+
}
149+
}
150+
84151
int main()
85152
{
86153
basic_tests<boost::crypt::md5_hasher>();
87154
basic_tests<boost::crypt::sha1_hasher>();
88155
basic_tests<boost::crypt::sha256_hasher>();
89156
basic_tests<boost::crypt::sha512_hasher>();
90157

158+
test_edges<boost::crypt::md5_hasher>();
159+
test_edges<boost::crypt::sha1_hasher>();
160+
test_edges<boost::crypt::sha256_hasher>();
161+
test_edges<boost::crypt::sha512_hasher>();
162+
91163
return boost::report_errors();
92164
}

0 commit comments

Comments
 (0)