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

Side by Side 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, 8 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/md5.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <string.h> 5 #include <string.h>
6 #include <string> 6 #include <string>
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/md5.h" 10 #include "base/md5.h"
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 197
198 MD5Digest digest; 198 MD5Digest digest;
199 MD5Final(&digest, &ctx); 199 MD5Final(&digest, &ctx);
200 200
201 std::string actual = MD5DigestToBase16(digest); 201 std::string actual = MD5DigestToBase16(digest);
202 std::string expected = "900150983cd24fb0d6963f7d28e17f72"; 202 std::string expected = "900150983cd24fb0d6963f7d28e17f72";
203 203
204 EXPECT_EQ(expected, actual); 204 EXPECT_EQ(expected, actual);
205 } 205 }
206 206
207 // Test that a digest generated by MD5IntermediateFinal() gives the same results
208 // as an independently-calculated digest, and also does not modify the context.
209 TEST(MD5, IntermediateFinal) {
210 // Independent context over the header.
211 MD5Context check_header_context;
212 MD5Init(&check_header_context);
213
214 // Independent context over entire input.
215 MD5Context check_full_context;
216 MD5Init(&check_full_context);
217
218 // Context intermediate digest will be calculated from.
219 MD5Context context;
220 MD5Init(&context);
221
222 static const char kHeader[] = "header data";
223 static const char kBody[] = "payload data";
224
225 MD5Update(&context, kHeader);
226 MD5Update(&check_header_context, kHeader);
227 MD5Update(&check_full_context, kHeader);
228
229 MD5Digest check_header_digest;
230 MD5Final(&check_header_digest, &check_header_context);
231
232 MD5Digest header_digest;
233 MD5IntermediateFinal(&header_digest, &context);
234
235 MD5Update(&context, kBody);
236 MD5Update(&check_full_context, kBody);
237
238 MD5Digest check_full_digest;
239 MD5Final(&check_full_digest, &check_full_context);
240
241 MD5Digest digest;
242 MD5Final(&digest, &context);
243
244 // The header and full digest pairs are the same, and they aren't the same as
245 // each other.
246 EXPECT_TRUE(!memcmp(&header_digest, &check_header_digest,
247 sizeof(header_digest)));
248 EXPECT_TRUE(!memcmp(&digest, &check_full_digest, sizeof(digest)));
249 EXPECT_FALSE(!memcmp(&digest, &header_digest, sizeof(digest)));
250 }
251
207 } // namespace base 252 } // namespace base
OLDNEW
« 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