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

Side by Side Diff: base/md5_unittest.cc

Issue 6896006: added md5_unittest (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: fixed the pointed problems. Created 9 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/base.gyp ('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
(Empty)
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>
6 #include <string.h>
Mark Mentovai 2011/04/22 19:32:05 C system headers should precede C++ system headers
7
8 #include "base/basictypes.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/md5.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 using namespace std;
Mark Mentovai 2011/04/22 19:32:05 I had asked you to fix this. “using namespace” is
15
16 TEST(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
26 for (int i = 0; i < 16; ++i) {
27 digest.a[i] = data[i] & 0xff;
28 }
29
30 string actual = MD5DigestToBase16(digest);
31 string expected = "d41d8cd98f00b204e9800998ecf8427e";
32
33 EXPECT_EQ(expected, actual);
34 }
35
36 TEST(MD5, MD5SumEmtpyData) {
37 MD5Digest digest;
38 const char* data = "";
39
40 MD5Sum(data, strlen(data), &digest);
41
42 int expected[] = {
43 0xd4, 0x1d, 0x8c, 0xd9,
44 0x8f, 0x00, 0xb2, 0x04,
45 0xe9, 0x80, 0x09, 0x98,
46 0xec, 0xf8, 0x42, 0x7e
47 };
48
49 for (int i = 0; i < 16; ++i) {
50 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
51 }
52 }
53
54 TEST(MD5, MD5SumOneByteData) {
55 MD5Digest digest;
56 const char* data = "a";
57
58 MD5Sum(data, strlen(data), &digest);
59
60 int expected[] = {
61 0x0c, 0xc1, 0x75, 0xb9,
62 0xc0, 0xf1, 0xb6, 0xa8,
63 0x31, 0xc3, 0x99, 0xe2,
64 0x69, 0x77, 0x26, 0x61
65 };
66
67 for (int i = 0; i < 16; ++i) {
68 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
69 }
70 }
71
72 TEST(MD5, MD5SumLongData) {
73 MD5Digest digest;
74
75 const int length = 10 * 1024 * 1024 + 1;
76 scoped_array<char> data(new char[length]);
77
78 for (int i = 0; i < length; ++i) {
79 data[i] = i & 0xFF;
80 }
81
82 MD5Sum(data.get(), length, &digest);
83
84 int expected[] = {
85 0x90, 0xbd, 0x6a, 0xd9,
86 0x0a, 0xce, 0xf5, 0xad,
87 0xaa, 0x92, 0x20, 0x3e,
88 0x21, 0xc7, 0xa1, 0x3e
89 };
90
91 for (int i = 0; i < 16; ++i) {
92 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
93 }
94 }
95
96 TEST(MD5, ContextWithEmptyData) {
97 MD5Context ctx;
98 MD5Init(&ctx);
99
100 MD5Digest digest;
101 MD5Final(&digest, &ctx);
102
103 int expected[] = {
104 0xd4, 0x1d, 0x8c, 0xd9,
105 0x8f, 0x00, 0xb2, 0x04,
106 0xe9, 0x80, 0x09, 0x98,
107 0xec, 0xf8, 0x42, 0x7e
108 };
109
110 for (int i = 0; i < 16; ++i) {
111 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
112 }
113 }
114
115 TEST(MD5, ContextWithLongData) {
116 MD5Context ctx;
117 MD5Init(&ctx);
118
119 const int length = 10 * 1024 * 1024 + 1;
120 scoped_array<char> data(new char[length]);
121
122 for (int i = 0; i < length; ++i) {
123 data[i] = i & 0xFF;
124 }
125
126 int total = 0;
127 while (total < length) {
128 int len = 4097; // intentionally not 2^k.
129 if (len > length - total) {
130 len = length - total;
131 }
132
133 MD5Update(&ctx, data.get() + total, len);
134 total += len;
135 }
136
137 EXPECT_EQ(length, total);
138
139 MD5Digest digest;
140 MD5Final(&digest, &ctx);
141
142 int expected[] = {
143 0x90, 0xbd, 0x6a, 0xd9,
144 0x0a, 0xce, 0xf5, 0xad,
145 0xaa, 0x92, 0x20, 0x3e,
146 0x21, 0xc7, 0xa1, 0x3e
147 };
148
149 for (int i = 0; i < 16; ++i) {
150 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
151 }
152 }
153
154 // Example data from http://www.ietf.org/rfc/rfc1321.txt A.5 Test Suite
155 TEST(MD5, MD5StringTestSuite1) {
156 string actual = MD5String("");
157 string expected = "d41d8cd98f00b204e9800998ecf8427e";
158 EXPECT_EQ(expected, actual);
159 }
160
161 TEST(MD5, MD5StringTestSuite2) {
162 string actual = MD5String("a");
163 string expected = "0cc175b9c0f1b6a831c399e269772661";
164 EXPECT_EQ(expected, actual);
165 }
166
167 TEST(MD5, MD5StringTestSuite3) {
168 string actual = MD5String("abc");
169 string expected = "900150983cd24fb0d6963f7d28e17f72";
170 EXPECT_EQ(expected, actual);
171 }
172
173 TEST(MD5, MD5StringTestSuite4) {
174 string actual = MD5String("message digest");
175 string expected = "f96b697d7cb7938d525a2f31aaf161d0";
176 EXPECT_EQ(expected, actual);
177 }
178
179 TEST(MD5, MD5StringTestSuite5) {
180 string actual = MD5String("abcdefghijklmnopqrstuvwxyz");
181 string expected = "c3fcd3d76192e4007dfb496cca67e13b";
182 EXPECT_EQ(expected, actual);
183 }
184
185 TEST(MD5, MD5StringTestSuite6) {
186 string actual = MD5String("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
187 "abcdefghijklmnopqrstuvwxyz"
188 "0123456789");
189 string expected = "d174ab98d277d9f5a5611c2c9f419d9f";
190 EXPECT_EQ(expected, actual);
191 }
192
193 TEST(MD5, MD5StringTestSuite7) {
194 string actual = MD5String("12345678901234567890"
195 "12345678901234567890"
196 "12345678901234567890"
197 "12345678901234567890");
198 string expected = "57edf4a22be3c955ac49da2e2107b67a";
199 EXPECT_EQ(expected, actual);
200 }
OLDNEW
« no previous file with comments | « base/base.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698