Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2520)

Unified Diff: base/md5_unittest.cc

Issue 211273009: base::MD5IntermediateFinal() generates digest non-destructively. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Change to suggested comment. Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/md5.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/md5_unittest.cc
diff --git a/base/md5_unittest.cc b/base/md5_unittest.cc
index 1112c4b4258d21eb13bc4a55a0fc5a6d06ef8526..3e7f2ad587d1e590811afad7c7454caa8cddda5a 100644
--- a/base/md5_unittest.cc
+++ b/base/md5_unittest.cc
@@ -204,4 +204,49 @@ TEST(MD5, ContextWithStringData) {
EXPECT_EQ(expected, actual);
}
+// Test that a digest generated by MD5IntermediateFinal() gives the same results
+// as an independently-calculated digest, and also does not modify the context.
+TEST(MD5, IntermediateFinal) {
+ // Independent context over the header.
+ MD5Context check_header_context;
+ MD5Init(&check_header_context);
+
+ // Independent context over entire input.
+ MD5Context check_full_context;
+ MD5Init(&check_full_context);
+
+ // Context intermediate digest will be calculated from.
+ MD5Context context;
+ MD5Init(&context);
+
+ static const char kHeader[] = "header data";
+ static const char kBody[] = "payload data";
+
+ MD5Update(&context, kHeader);
+ MD5Update(&check_header_context, kHeader);
+ MD5Update(&check_full_context, kHeader);
+
+ MD5Digest check_header_digest;
+ MD5Final(&check_header_digest, &check_header_context);
+
+ MD5Digest header_digest;
+ MD5IntermediateFinal(&header_digest, &context);
+
+ MD5Update(&context, kBody);
+ MD5Update(&check_full_context, kBody);
+
+ MD5Digest check_full_digest;
+ MD5Final(&check_full_digest, &check_full_context);
+
+ MD5Digest digest;
+ MD5Final(&digest, &context);
+
+ // The header and full digest pairs are the same, and they aren't the same as
+ // each other.
+ EXPECT_TRUE(!memcmp(&header_digest, &check_header_digest,
+ sizeof(header_digest)));
+ EXPECT_TRUE(!memcmp(&digest, &check_full_digest, sizeof(digest)));
+ EXPECT_FALSE(!memcmp(&digest, &header_digest, sizeof(digest)));
+}
+
} // namespace base
« no previous file with comments | « base/md5.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698