aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2018-08-06 14:06:59 -0400
committerJack Lloyd <[email protected]>2018-08-06 14:06:59 -0400
commit83f67daeb488c91108dd502ead78e869dec12125 (patch)
tree3b721da74c0fa59067583852808289bd6e30b4a8
parentb9aafd67002be96bf00eea4e7791804693ba4b1f (diff)
Test intentionally misaligned inputs to hash functions
-rw-r--r--src/tests/test_hash.cpp18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/tests/test_hash.cpp b/src/tests/test_hash.cpp
index b3d2d2599..a36bf7214 100644
--- a/src/tests/test_hash.cpp
+++ b/src/tests/test_hash.cpp
@@ -125,6 +125,24 @@ class Hash_Function_Tests final : public Text_Based_Test
result.test_eq(provider, "hashing after clear", hash->final(), expected);
+ // Test that misaligned inputs work
+
+ if(input.size() > 0)
+ {
+ std::vector<uint8_t> misaligned = input;
+ const size_t current_alignment = reinterpret_cast<uintptr_t>(misaligned.data()) % 16;
+
+ const size_t bytes_to_misalign = 15 - current_alignment;
+
+ for(size_t i = 0; i != bytes_to_misalign; ++i)
+ misaligned.insert(misaligned.begin(), 0x23);
+
+ const size_t misalignment = reinterpret_cast<uintptr_t>(&misaligned[bytes_to_misalign]) % 16;
+ result.test_eq("Misaligned input to 15 % 16", misalignment, 15);
+ hash->update(&misaligned[bytes_to_misalign], input.size());
+ result.test_eq(provider, "hashing misaligned data", hash->final(), expected);
+ }
+
if(input.size() > 5)
{
hash->update(input[0]);