• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include <string.h>
6 #include <string>
7 
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"
12 
13 namespace base {
14 
TEST(MD5,DigestToBase16)15 TEST(MD5, DigestToBase16) {
16   MD5Digest digest;
17 
18   int data[] = {
19     0xd4, 0x1d, 0x8c, 0xd9,
20     0x8f, 0x00, 0xb2, 0x04,
21     0xe9, 0x80, 0x09, 0x98,
22     0xec, 0xf8, 0x42, 0x7e
23   };
24 
25   for (int i = 0; i < 16; ++i)
26     digest.a[i] = data[i] & 0xff;
27 
28   std::string actual = MD5DigestToBase16(digest);
29   std::string expected = "d41d8cd98f00b204e9800998ecf8427e";
30 
31   EXPECT_EQ(expected, actual);
32 }
33 
TEST(MD5,MD5SumEmtpyData)34 TEST(MD5, MD5SumEmtpyData) {
35   MD5Digest digest;
36   const char* data = "";
37 
38   MD5Sum(data, strlen(data), &digest);
39 
40   int expected[] = {
41     0xd4, 0x1d, 0x8c, 0xd9,
42     0x8f, 0x00, 0xb2, 0x04,
43     0xe9, 0x80, 0x09, 0x98,
44     0xec, 0xf8, 0x42, 0x7e
45   };
46 
47   for (int i = 0; i < 16; ++i)
48     EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
49 }
50 
TEST(MD5,MD5SumOneByteData)51 TEST(MD5, MD5SumOneByteData) {
52   MD5Digest digest;
53   const char* data = "a";
54 
55   MD5Sum(data, strlen(data), &digest);
56 
57   int expected[] = {
58     0x0c, 0xc1, 0x75, 0xb9,
59     0xc0, 0xf1, 0xb6, 0xa8,
60     0x31, 0xc3, 0x99, 0xe2,
61     0x69, 0x77, 0x26, 0x61
62   };
63 
64   for (int i = 0; i < 16; ++i)
65     EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
66 }
67 
TEST(MD5,MD5SumLongData)68 TEST(MD5, MD5SumLongData) {
69   const int length = 10 * 1024 * 1024 + 1;
70   scoped_ptr<char[]> data(new char[length]);
71 
72   for (int i = 0; i < length; ++i)
73     data[i] = i & 0xFF;
74 
75   MD5Digest digest;
76   MD5Sum(data.get(), length, &digest);
77 
78   int expected[] = {
79     0x90, 0xbd, 0x6a, 0xd9,
80     0x0a, 0xce, 0xf5, 0xad,
81     0xaa, 0x92, 0x20, 0x3e,
82     0x21, 0xc7, 0xa1, 0x3e
83   };
84 
85   for (int i = 0; i < 16; ++i)
86     EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
87 }
88 
TEST(MD5,ContextWithEmptyData)89 TEST(MD5, ContextWithEmptyData) {
90   MD5Context ctx;
91   MD5Init(&ctx);
92 
93   MD5Digest digest;
94   MD5Final(&digest, &ctx);
95 
96   int expected[] = {
97     0xd4, 0x1d, 0x8c, 0xd9,
98     0x8f, 0x00, 0xb2, 0x04,
99     0xe9, 0x80, 0x09, 0x98,
100     0xec, 0xf8, 0x42, 0x7e
101   };
102 
103   for (int i = 0; i < 16; ++i)
104     EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
105 }
106 
TEST(MD5,ContextWithLongData)107 TEST(MD5, ContextWithLongData) {
108   MD5Context ctx;
109   MD5Init(&ctx);
110 
111   const int length = 10 * 1024 * 1024 + 1;
112   scoped_ptr<char[]> data(new char[length]);
113 
114   for (int i = 0; i < length; ++i)
115     data[i] = i & 0xFF;
116 
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;
122 
123     MD5Update(&ctx,
124               StringPiece(reinterpret_cast<char*>(data.get() + total), len));
125     total += len;
126   }
127 
128   EXPECT_EQ(length, total);
129 
130   MD5Digest digest;
131   MD5Final(&digest, &ctx);
132 
133   int expected[] = {
134     0x90, 0xbd, 0x6a, 0xd9,
135     0x0a, 0xce, 0xf5, 0xad,
136     0xaa, 0x92, 0x20, 0x3e,
137     0x21, 0xc7, 0xa1, 0x3e
138   };
139 
140   for (int i = 0; i < 16; ++i)
141     EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
142 }
143 
144 // Example data from http://www.ietf.org/rfc/rfc1321.txt A.5 Test Suite
TEST(MD5,MD5StringTestSuite1)145 TEST(MD5, MD5StringTestSuite1) {
146   std::string actual = MD5String("");
147   std::string expected = "d41d8cd98f00b204e9800998ecf8427e";
148   EXPECT_EQ(expected, actual);
149 }
150 
TEST(MD5,MD5StringTestSuite2)151 TEST(MD5, MD5StringTestSuite2) {
152   std::string actual = MD5String("a");
153   std::string expected = "0cc175b9c0f1b6a831c399e269772661";
154   EXPECT_EQ(expected, actual);
155 }
156 
TEST(MD5,MD5StringTestSuite3)157 TEST(MD5, MD5StringTestSuite3) {
158   std::string actual = MD5String("abc");
159   std::string expected = "900150983cd24fb0d6963f7d28e17f72";
160   EXPECT_EQ(expected, actual);
161 }
162 
TEST(MD5,MD5StringTestSuite4)163 TEST(MD5, MD5StringTestSuite4) {
164   std::string actual = MD5String("message digest");
165   std::string expected = "f96b697d7cb7938d525a2f31aaf161d0";
166   EXPECT_EQ(expected, actual);
167 }
168 
TEST(MD5,MD5StringTestSuite5)169 TEST(MD5, MD5StringTestSuite5) {
170   std::string actual = MD5String("abcdefghijklmnopqrstuvwxyz");
171   std::string expected = "c3fcd3d76192e4007dfb496cca67e13b";
172   EXPECT_EQ(expected, actual);
173 }
174 
TEST(MD5,MD5StringTestSuite6)175 TEST(MD5, MD5StringTestSuite6) {
176   std::string actual = MD5String("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
177                                  "abcdefghijklmnopqrstuvwxyz"
178                                  "0123456789");
179   std::string expected = "d174ab98d277d9f5a5611c2c9f419d9f";
180   EXPECT_EQ(expected, actual);
181 }
182 
TEST(MD5,MD5StringTestSuite7)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);
190 }
191 
TEST(MD5,ContextWithStringData)192 TEST(MD5, ContextWithStringData) {
193   MD5Context ctx;
194   MD5Init(&ctx);
195 
196   MD5Update(&ctx, "abc");
197 
198   MD5Digest digest;
199   MD5Final(&digest, &ctx);
200 
201   std::string actual = MD5DigestToBase16(digest);
202   std::string expected = "900150983cd24fb0d6963f7d28e17f72";
203 
204   EXPECT_EQ(expected, actual);
205 }
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.
TEST(MD5,IntermediateFinal)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 
252 }  // namespace base
253