OLD | NEW |
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 "base/md5.h" |
| 6 |
5 #include <string.h> | 7 #include <string.h> |
| 8 |
| 9 #include <memory> |
6 #include <string> | 10 #include <string> |
7 | 11 |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/md5.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
11 | 13 |
12 namespace base { | 14 namespace base { |
13 | 15 |
14 TEST(MD5, DigestToBase16) { | 16 TEST(MD5, DigestToBase16) { |
15 MD5Digest digest; | 17 MD5Digest digest; |
16 | 18 |
17 int data[] = { | 19 int data[] = { |
18 0xd4, 0x1d, 0x8c, 0xd9, | 20 0xd4, 0x1d, 0x8c, 0xd9, |
19 0x8f, 0x00, 0xb2, 0x04, | 21 0x8f, 0x00, 0xb2, 0x04, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 0x31, 0xc3, 0x99, 0xe2, | 61 0x31, 0xc3, 0x99, 0xe2, |
60 0x69, 0x77, 0x26, 0x61 | 62 0x69, 0x77, 0x26, 0x61 |
61 }; | 63 }; |
62 | 64 |
63 for (int i = 0; i < 16; ++i) | 65 for (int i = 0; i < 16; ++i) |
64 EXPECT_EQ(expected[i], digest.a[i] & 0xFF); | 66 EXPECT_EQ(expected[i], digest.a[i] & 0xFF); |
65 } | 67 } |
66 | 68 |
67 TEST(MD5, MD5SumLongData) { | 69 TEST(MD5, MD5SumLongData) { |
68 const int length = 10 * 1024 * 1024 + 1; | 70 const int length = 10 * 1024 * 1024 + 1; |
69 scoped_ptr<char[]> data(new char[length]); | 71 std::unique_ptr<char[]> data(new char[length]); |
70 | 72 |
71 for (int i = 0; i < length; ++i) | 73 for (int i = 0; i < length; ++i) |
72 data[i] = i & 0xFF; | 74 data[i] = i & 0xFF; |
73 | 75 |
74 MD5Digest digest; | 76 MD5Digest digest; |
75 MD5Sum(data.get(), length, &digest); | 77 MD5Sum(data.get(), length, &digest); |
76 | 78 |
77 int expected[] = { | 79 int expected[] = { |
78 0x90, 0xbd, 0x6a, 0xd9, | 80 0x90, 0xbd, 0x6a, 0xd9, |
79 0x0a, 0xce, 0xf5, 0xad, | 81 0x0a, 0xce, 0xf5, 0xad, |
(...skipping 21 matching lines...) Expand all Loading... |
101 | 103 |
102 for (int i = 0; i < 16; ++i) | 104 for (int i = 0; i < 16; ++i) |
103 EXPECT_EQ(expected[i], digest.a[i] & 0xFF); | 105 EXPECT_EQ(expected[i], digest.a[i] & 0xFF); |
104 } | 106 } |
105 | 107 |
106 TEST(MD5, ContextWithLongData) { | 108 TEST(MD5, ContextWithLongData) { |
107 MD5Context ctx; | 109 MD5Context ctx; |
108 MD5Init(&ctx); | 110 MD5Init(&ctx); |
109 | 111 |
110 const int length = 10 * 1024 * 1024 + 1; | 112 const int length = 10 * 1024 * 1024 + 1; |
111 scoped_ptr<char[]> data(new char[length]); | 113 std::unique_ptr<char[]> data(new char[length]); |
112 | 114 |
113 for (int i = 0; i < length; ++i) | 115 for (int i = 0; i < length; ++i) |
114 data[i] = i & 0xFF; | 116 data[i] = i & 0xFF; |
115 | 117 |
116 int total = 0; | 118 int total = 0; |
117 while (total < length) { | 119 while (total < length) { |
118 int len = 4097; // intentionally not 2^k. | 120 int len = 4097; // intentionally not 2^k. |
119 if (len > length - total) | 121 if (len > length - total) |
120 len = length - total; | 122 len = length - total; |
121 | 123 |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 | 244 |
243 // The header and full digest pairs are the same, and they aren't the same as | 245 // The header and full digest pairs are the same, and they aren't the same as |
244 // each other. | 246 // each other. |
245 EXPECT_TRUE(!memcmp(&header_digest, &check_header_digest, | 247 EXPECT_TRUE(!memcmp(&header_digest, &check_header_digest, |
246 sizeof(header_digest))); | 248 sizeof(header_digest))); |
247 EXPECT_TRUE(!memcmp(&digest, &check_full_digest, sizeof(digest))); | 249 EXPECT_TRUE(!memcmp(&digest, &check_full_digest, sizeof(digest))); |
248 EXPECT_TRUE(memcmp(&digest, &header_digest, sizeof(digest))); | 250 EXPECT_TRUE(memcmp(&digest, &header_digest, sizeof(digest))); |
249 } | 251 } |
250 | 252 |
251 } // namespace base | 253 } // namespace base |
OLD | NEW |