blob: b27efe9f96f00836cee30d45d6e532738f3d6e36 [file] [log] [blame]
shinyak@google.com5c521022011-04-26 08:54:07 +09001// 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.
4
dchengcc8e4d82016-04-05 06:25:51 +09005#include "base/md5.h"
6
shinyak@google.com5c521022011-04-26 08:54:07 +09007#include <string.h>
dchengcc8e4d82016-04-05 06:25:51 +09008
9#include <memory>
shinyak@google.com5c521022011-04-26 08:54:07 +090010#include <string>
11
shinyak@google.com5c521022011-04-26 08:54:07 +090012#include "testing/gtest/include/gtest/gtest.h"
13
tfarina@chromium.orgb64a8a72011-07-15 11:53:37 +090014namespace base {
15
shinyak@google.com5c521022011-04-26 08:54:07 +090016TEST(MD5, DigestToBase16) {
17 MD5Digest digest;
18
19 int data[] = {
20 0xd4, 0x1d, 0x8c, 0xd9,
21 0x8f, 0x00, 0xb2, 0x04,
22 0xe9, 0x80, 0x09, 0x98,
23 0xec, 0xf8, 0x42, 0x7e
24 };
25
tfarina@chromium.orgb64a8a72011-07-15 11:53:37 +090026 for (int i = 0; i < 16; ++i)
shinyak@google.com5c521022011-04-26 08:54:07 +090027 digest.a[i] = data[i] & 0xff;
shinyak@google.com5c521022011-04-26 08:54:07 +090028
29 std::string actual = MD5DigestToBase16(digest);
30 std::string expected = "d41d8cd98f00b204e9800998ecf8427e";
31
32 EXPECT_EQ(expected, actual);
33}
34
35TEST(MD5, MD5SumEmtpyData) {
36 MD5Digest digest;
thestigdd7aaa02014-10-21 12:11:21 +090037 const char data[] = "";
shinyak@google.com5c521022011-04-26 08:54:07 +090038
39 MD5Sum(data, strlen(data), &digest);
40
41 int expected[] = {
42 0xd4, 0x1d, 0x8c, 0xd9,
43 0x8f, 0x00, 0xb2, 0x04,
44 0xe9, 0x80, 0x09, 0x98,
45 0xec, 0xf8, 0x42, 0x7e
46 };
47
tfarina@chromium.orgb64a8a72011-07-15 11:53:37 +090048 for (int i = 0; i < 16; ++i)
shinyak@google.com5c521022011-04-26 08:54:07 +090049 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
shinyak@google.com5c521022011-04-26 08:54:07 +090050}
51
52TEST(MD5, MD5SumOneByteData) {
53 MD5Digest digest;
thestigdd7aaa02014-10-21 12:11:21 +090054 const char data[] = "a";
shinyak@google.com5c521022011-04-26 08:54:07 +090055
56 MD5Sum(data, strlen(data), &digest);
57
58 int expected[] = {
59 0x0c, 0xc1, 0x75, 0xb9,
60 0xc0, 0xf1, 0xb6, 0xa8,
61 0x31, 0xc3, 0x99, 0xe2,
62 0x69, 0x77, 0x26, 0x61
63 };
64
tfarina@chromium.orgb64a8a72011-07-15 11:53:37 +090065 for (int i = 0; i < 16; ++i)
shinyak@google.com5c521022011-04-26 08:54:07 +090066 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
shinyak@google.com5c521022011-04-26 08:54:07 +090067}
68
69TEST(MD5, MD5SumLongData) {
shinyak@google.com5c521022011-04-26 08:54:07 +090070 const int length = 10 * 1024 * 1024 + 1;
dchengcc8e4d82016-04-05 06:25:51 +090071 std::unique_ptr<char[]> data(new char[length]);
shinyak@google.com5c521022011-04-26 08:54:07 +090072
tfarina@chromium.orgb64a8a72011-07-15 11:53:37 +090073 for (int i = 0; i < length; ++i)
shinyak@google.com5c521022011-04-26 08:54:07 +090074 data[i] = i & 0xFF;
shinyak@google.com5c521022011-04-26 08:54:07 +090075
tfarina@chromium.orgb64a8a72011-07-15 11:53:37 +090076 MD5Digest digest;
shinyak@google.com5c521022011-04-26 08:54:07 +090077 MD5Sum(data.get(), length, &digest);
78
79 int expected[] = {
80 0x90, 0xbd, 0x6a, 0xd9,
81 0x0a, 0xce, 0xf5, 0xad,
82 0xaa, 0x92, 0x20, 0x3e,
83 0x21, 0xc7, 0xa1, 0x3e
84 };
85
tfarina@chromium.orgb64a8a72011-07-15 11:53:37 +090086 for (int i = 0; i < 16; ++i)
shinyak@google.com5c521022011-04-26 08:54:07 +090087 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
shinyak@google.com5c521022011-04-26 08:54:07 +090088}
89
90TEST(MD5, ContextWithEmptyData) {
91 MD5Context ctx;
92 MD5Init(&ctx);
93
94 MD5Digest digest;
95 MD5Final(&digest, &ctx);
96
97 int expected[] = {
98 0xd4, 0x1d, 0x8c, 0xd9,
99 0x8f, 0x00, 0xb2, 0x04,
100 0xe9, 0x80, 0x09, 0x98,
101 0xec, 0xf8, 0x42, 0x7e
102 };
103
tfarina@chromium.orgb64a8a72011-07-15 11:53:37 +0900104 for (int i = 0; i < 16; ++i)
shinyak@google.com5c521022011-04-26 08:54:07 +0900105 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
shinyak@google.com5c521022011-04-26 08:54:07 +0900106}
107
108TEST(MD5, ContextWithLongData) {
109 MD5Context ctx;
110 MD5Init(&ctx);
111
112 const int length = 10 * 1024 * 1024 + 1;
dchengcc8e4d82016-04-05 06:25:51 +0900113 std::unique_ptr<char[]> data(new char[length]);
shinyak@google.com5c521022011-04-26 08:54:07 +0900114
tfarina@chromium.orgb64a8a72011-07-15 11:53:37 +0900115 for (int i = 0; i < length; ++i)
shinyak@google.com5c521022011-04-26 08:54:07 +0900116 data[i] = i & 0xFF;
shinyak@google.com5c521022011-04-26 08:54:07 +0900117
118 int total = 0;
119 while (total < length) {
120 int len = 4097; // intentionally not 2^k.
tfarina@chromium.orgb64a8a72011-07-15 11:53:37 +0900121 if (len > length - total)
shinyak@google.com5c521022011-04-26 08:54:07 +0900122 len = length - total;
shinyak@google.com5c521022011-04-26 08:54:07 +0900123
dominich@chromium.orgf03d6942011-07-27 08:39:50 +0900124 MD5Update(&ctx,
125 StringPiece(reinterpret_cast<char*>(data.get() + total), len));
shinyak@google.com5c521022011-04-26 08:54:07 +0900126 total += len;
127 }
128
129 EXPECT_EQ(length, total);
130
131 MD5Digest digest;
132 MD5Final(&digest, &ctx);
133
134 int expected[] = {
135 0x90, 0xbd, 0x6a, 0xd9,
136 0x0a, 0xce, 0xf5, 0xad,
137 0xaa, 0x92, 0x20, 0x3e,
138 0x21, 0xc7, 0xa1, 0x3e
139 };
140
tfarina@chromium.orgb64a8a72011-07-15 11:53:37 +0900141 for (int i = 0; i < 16; ++i)
shinyak@google.com5c521022011-04-26 08:54:07 +0900142 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
shinyak@google.com5c521022011-04-26 08:54:07 +0900143}
144
145// Example data from http://www.ietf.org/rfc/rfc1321.txt A.5 Test Suite
146TEST(MD5, MD5StringTestSuite1) {
147 std::string actual = MD5String("");
148 std::string expected = "d41d8cd98f00b204e9800998ecf8427e";
149 EXPECT_EQ(expected, actual);
150}
151
152TEST(MD5, MD5StringTestSuite2) {
153 std::string actual = MD5String("a");
154 std::string expected = "0cc175b9c0f1b6a831c399e269772661";
155 EXPECT_EQ(expected, actual);
156}
157
158TEST(MD5, MD5StringTestSuite3) {
159 std::string actual = MD5String("abc");
160 std::string expected = "900150983cd24fb0d6963f7d28e17f72";
161 EXPECT_EQ(expected, actual);
162}
163
164TEST(MD5, MD5StringTestSuite4) {
165 std::string actual = MD5String("message digest");
166 std::string expected = "f96b697d7cb7938d525a2f31aaf161d0";
167 EXPECT_EQ(expected, actual);
168}
169
170TEST(MD5, MD5StringTestSuite5) {
171 std::string actual = MD5String("abcdefghijklmnopqrstuvwxyz");
172 std::string expected = "c3fcd3d76192e4007dfb496cca67e13b";
173 EXPECT_EQ(expected, actual);
174}
175
176TEST(MD5, MD5StringTestSuite6) {
177 std::string actual = MD5String("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
tfarina@chromium.orgb64a8a72011-07-15 11:53:37 +0900178 "abcdefghijklmnopqrstuvwxyz"
179 "0123456789");
shinyak@google.com5c521022011-04-26 08:54:07 +0900180 std::string expected = "d174ab98d277d9f5a5611c2c9f419d9f";
181 EXPECT_EQ(expected, actual);
182}
183
184TEST(MD5, MD5StringTestSuite7) {
185 std::string actual = MD5String("12345678901234567890"
tfarina@chromium.orgb64a8a72011-07-15 11:53:37 +0900186 "12345678901234567890"
187 "12345678901234567890"
188 "12345678901234567890");
shinyak@google.com5c521022011-04-26 08:54:07 +0900189 std::string expected = "57edf4a22be3c955ac49da2e2107b67a";
190 EXPECT_EQ(expected, actual);
191}
tfarina@chromium.orgb64a8a72011-07-15 11:53:37 +0900192
dominich@chromium.orgf03d6942011-07-27 08:39:50 +0900193TEST(MD5, ContextWithStringData) {
194 MD5Context ctx;
195 MD5Init(&ctx);
196
197 MD5Update(&ctx, "abc");
198
199 MD5Digest digest;
200 MD5Final(&digest, &ctx);
201
202 std::string actual = MD5DigestToBase16(digest);
203 std::string expected = "900150983cd24fb0d6963f7d28e17f72";
204
205 EXPECT_EQ(expected, actual);
206}
207
shess@chromium.org7f164d92014-04-01 10:31:37 +0900208// Test that a digest generated by MD5IntermediateFinal() gives the same results
209// as an independently-calculated digest, and also does not modify the context.
210TEST(MD5, IntermediateFinal) {
211 // Independent context over the header.
212 MD5Context check_header_context;
213 MD5Init(&check_header_context);
214
215 // Independent context over entire input.
216 MD5Context check_full_context;
217 MD5Init(&check_full_context);
218
219 // Context intermediate digest will be calculated from.
220 MD5Context context;
221 MD5Init(&context);
222
223 static const char kHeader[] = "header data";
224 static const char kBody[] = "payload data";
225
226 MD5Update(&context, kHeader);
227 MD5Update(&check_header_context, kHeader);
228 MD5Update(&check_full_context, kHeader);
229
230 MD5Digest check_header_digest;
231 MD5Final(&check_header_digest, &check_header_context);
232
233 MD5Digest header_digest;
234 MD5IntermediateFinal(&header_digest, &context);
235
236 MD5Update(&context, kBody);
237 MD5Update(&check_full_context, kBody);
238
239 MD5Digest check_full_digest;
240 MD5Final(&check_full_digest, &check_full_context);
241
242 MD5Digest digest;
243 MD5Final(&digest, &context);
244
245 // The header and full digest pairs are the same, and they aren't the same as
246 // each other.
247 EXPECT_TRUE(!memcmp(&header_digest, &check_header_digest,
248 sizeof(header_digest)));
249 EXPECT_TRUE(!memcmp(&digest, &check_full_digest, sizeof(digest)));
thestigf57d3cb2015-05-07 06:22:59 +0900250 EXPECT_TRUE(memcmp(&digest, &header_digest, sizeof(digest)));
shess@chromium.org7f164d92014-04-01 10:31:37 +0900251}
252
tfarina@chromium.orgb64a8a72011-07-15 11:53:37 +0900253} // namespace base