hunspell: Cleanup to fix the header include guards under google/ directory.
[chromium-blink-merge.git] / base / md5_unittest.cc
blob08c99f19049bac8d9523cba228435aaedccdb69b
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
3 // found in the LICENSE file.
5 #include <string.h>
6 #include <string>
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/md5.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace base {
15 TEST(MD5, DigestToBase16) {
16 MD5Digest digest;
18 int data[] = {
19 0xd4, 0x1d, 0x8c, 0xd9,
20 0x8f, 0x00, 0xb2, 0x04,
21 0xe9, 0x80, 0x09, 0x98,
22 0xec, 0xf8, 0x42, 0x7e
25 for (int i = 0; i < 16; ++i)
26 digest.a[i] = data[i] & 0xff;
28 std::string actual = MD5DigestToBase16(digest);
29 std::string expected = "d41d8cd98f00b204e9800998ecf8427e";
31 EXPECT_EQ(expected, actual);
34 TEST(MD5, MD5SumEmtpyData) {
35 MD5Digest digest;
36 const char data[] = "";
38 MD5Sum(data, strlen(data), &digest);
40 int expected[] = {
41 0xd4, 0x1d, 0x8c, 0xd9,
42 0x8f, 0x00, 0xb2, 0x04,
43 0xe9, 0x80, 0x09, 0x98,
44 0xec, 0xf8, 0x42, 0x7e
47 for (int i = 0; i < 16; ++i)
48 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
51 TEST(MD5, MD5SumOneByteData) {
52 MD5Digest digest;
53 const char data[] = "a";
55 MD5Sum(data, strlen(data), &digest);
57 int expected[] = {
58 0x0c, 0xc1, 0x75, 0xb9,
59 0xc0, 0xf1, 0xb6, 0xa8,
60 0x31, 0xc3, 0x99, 0xe2,
61 0x69, 0x77, 0x26, 0x61
64 for (int i = 0; i < 16; ++i)
65 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
68 TEST(MD5, MD5SumLongData) {
69 const int length = 10 * 1024 * 1024 + 1;
70 scoped_ptr<char[]> data(new char[length]);
72 for (int i = 0; i < length; ++i)
73 data[i] = i & 0xFF;
75 MD5Digest digest;
76 MD5Sum(data.get(), length, &digest);
78 int expected[] = {
79 0x90, 0xbd, 0x6a, 0xd9,
80 0x0a, 0xce, 0xf5, 0xad,
81 0xaa, 0x92, 0x20, 0x3e,
82 0x21, 0xc7, 0xa1, 0x3e
85 for (int i = 0; i < 16; ++i)
86 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
89 TEST(MD5, ContextWithEmptyData) {
90 MD5Context ctx;
91 MD5Init(&ctx);
93 MD5Digest digest;
94 MD5Final(&digest, &ctx);
96 int expected[] = {
97 0xd4, 0x1d, 0x8c, 0xd9,
98 0x8f, 0x00, 0xb2, 0x04,
99 0xe9, 0x80, 0x09, 0x98,
100 0xec, 0xf8, 0x42, 0x7e
103 for (int i = 0; i < 16; ++i)
104 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
107 TEST(MD5, ContextWithLongData) {
108 MD5Context ctx;
109 MD5Init(&ctx);
111 const int length = 10 * 1024 * 1024 + 1;
112 scoped_ptr<char[]> data(new char[length]);
114 for (int i = 0; i < length; ++i)
115 data[i] = i & 0xFF;
117 int total = 0;
118 while (total < length) {
119 int len = 4097; // intentionally not 2^k.
120 if (len > length - total)
121 len = length - total;
123 MD5Update(&ctx,
124 StringPiece(reinterpret_cast<char*>(data.get() + total), len));
125 total += len;
128 EXPECT_EQ(length, total);
130 MD5Digest digest;
131 MD5Final(&digest, &ctx);
133 int expected[] = {
134 0x90, 0xbd, 0x6a, 0xd9,
135 0x0a, 0xce, 0xf5, 0xad,
136 0xaa, 0x92, 0x20, 0x3e,
137 0x21, 0xc7, 0xa1, 0x3e
140 for (int i = 0; i < 16; ++i)
141 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
144 // Example data from http://www.ietf.org/rfc/rfc1321.txt A.5 Test Suite
145 TEST(MD5, MD5StringTestSuite1) {
146 std::string actual = MD5String("");
147 std::string expected = "d41d8cd98f00b204e9800998ecf8427e";
148 EXPECT_EQ(expected, actual);
151 TEST(MD5, MD5StringTestSuite2) {
152 std::string actual = MD5String("a");
153 std::string expected = "0cc175b9c0f1b6a831c399e269772661";
154 EXPECT_EQ(expected, actual);
157 TEST(MD5, MD5StringTestSuite3) {
158 std::string actual = MD5String("abc");
159 std::string expected = "900150983cd24fb0d6963f7d28e17f72";
160 EXPECT_EQ(expected, actual);
163 TEST(MD5, MD5StringTestSuite4) {
164 std::string actual = MD5String("message digest");
165 std::string expected = "f96b697d7cb7938d525a2f31aaf161d0";
166 EXPECT_EQ(expected, actual);
169 TEST(MD5, MD5StringTestSuite5) {
170 std::string actual = MD5String("abcdefghijklmnopqrstuvwxyz");
171 std::string expected = "c3fcd3d76192e4007dfb496cca67e13b";
172 EXPECT_EQ(expected, actual);
175 TEST(MD5, MD5StringTestSuite6) {
176 std::string actual = MD5String("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
177 "abcdefghijklmnopqrstuvwxyz"
178 "0123456789");
179 std::string expected = "d174ab98d277d9f5a5611c2c9f419d9f";
180 EXPECT_EQ(expected, actual);
183 TEST(MD5, MD5StringTestSuite7) {
184 std::string actual = MD5String("12345678901234567890"
185 "12345678901234567890"
186 "12345678901234567890"
187 "12345678901234567890");
188 std::string expected = "57edf4a22be3c955ac49da2e2107b67a";
189 EXPECT_EQ(expected, actual);
192 TEST(MD5, ContextWithStringData) {
193 MD5Context ctx;
194 MD5Init(&ctx);
196 MD5Update(&ctx, "abc");
198 MD5Digest digest;
199 MD5Final(&digest, &ctx);
201 std::string actual = MD5DigestToBase16(digest);
202 std::string expected = "900150983cd24fb0d6963f7d28e17f72";
204 EXPECT_EQ(expected, actual);
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);
214 // Independent context over entire input.
215 MD5Context check_full_context;
216 MD5Init(&check_full_context);
218 // Context intermediate digest will be calculated from.
219 MD5Context context;
220 MD5Init(&context);
222 static const char kHeader[] = "header data";
223 static const char kBody[] = "payload data";
225 MD5Update(&context, kHeader);
226 MD5Update(&check_header_context, kHeader);
227 MD5Update(&check_full_context, kHeader);
229 MD5Digest check_header_digest;
230 MD5Final(&check_header_digest, &check_header_context);
232 MD5Digest header_digest;
233 MD5IntermediateFinal(&header_digest, &context);
235 MD5Update(&context, kBody);
236 MD5Update(&check_full_context, kBody);
238 MD5Digest check_full_digest;
239 MD5Final(&check_full_digest, &check_full_context);
241 MD5Digest digest;
242 MD5Final(&digest, &context);
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_TRUE(memcmp(&digest, &header_digest, sizeof(digest)));
252 } // namespace base