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

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: not using namespace std 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.h>
6 #include <string>
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 TEST(MD5, DigestToBase16) {
15 MD5Digest digest;
16
17 int data[] = {
18 0xd4, 0x1d, 0x8c, 0xd9,
19 0x8f, 0x00, 0xb2, 0x04,
20 0xe9, 0x80, 0x09, 0x98,
21 0xec, 0xf8, 0x42, 0x7e
22 };
23
24 for (int i = 0; i < 16; ++i) {
25 digest.a[i] = data[i] & 0xff;
26 }
27
28 std::string actual = MD5DigestToBase16(digest);
29 std::string expected = "d41d8cd98f00b204e9800998ecf8427e";
30
31 EXPECT_EQ(expected, actual);
32 }
33
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 }
51
52 TEST(MD5, MD5SumOneByteData) {
53 MD5Digest digest;
54 const char* data = "a";
55
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
65 for (int i = 0; i < 16; ++i) {
66 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
67 }
68 }
69
70 TEST(MD5, MD5SumLongData) {
71 MD5Digest digest;
72
73 const int length = 10 * 1024 * 1024 + 1;
74 scoped_array<char> data(new char[length]);
75
76 for (int i = 0; i < length; ++i) {
77 data[i] = i & 0xFF;
78 }
79
80 MD5Sum(data.get(), length, &digest);
81
82 int expected[] = {
83 0x90, 0xbd, 0x6a, 0xd9,
84 0x0a, 0xce, 0xf5, 0xad,
85 0xaa, 0x92, 0x20, 0x3e,
86 0x21, 0xc7, 0xa1, 0x3e
87 };
88
89 for (int i = 0; i < 16; ++i) {
90 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
91 }
92 }
93
94 TEST(MD5, ContextWithEmptyData) {
95 MD5Context ctx;
96 MD5Init(&ctx);
97
98 MD5Digest digest;
99 MD5Final(&digest, &ctx);
100
101 int expected[] = {
102 0xd4, 0x1d, 0x8c, 0xd9,
103 0x8f, 0x00, 0xb2, 0x04,
104 0xe9, 0x80, 0x09, 0x98,
105 0xec, 0xf8, 0x42, 0x7e
106 };
107
108 for (int i = 0; i < 16; ++i) {
109 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
110 }
111 }
112
113 TEST(MD5, ContextWithLongData) {
114 MD5Context ctx;
115 MD5Init(&ctx);
116
117 const int length = 10 * 1024 * 1024 + 1;
118 scoped_array<char> data(new char[length]);
119
120 for (int i = 0; i < length; ++i) {
121 data[i] = i & 0xFF;
122 }
123
124 int total = 0;
125 while (total < length) {
126 int len = 4097; // intentionally not 2^k.
127 if (len > length - total) {
128 len = length - total;
129 }
130
131 MD5Update(&ctx, data.get() + total, len);
132 total += len;
133 }
134
135 EXPECT_EQ(length, total);
136
137 MD5Digest digest;
138 MD5Final(&digest, &ctx);
139
140 int expected[] = {
141 0x90, 0xbd, 0x6a, 0xd9,
142 0x0a, 0xce, 0xf5, 0xad,
143 0xaa, 0x92, 0x20, 0x3e,
144 0x21, 0xc7, 0xa1, 0x3e
145 };
146
147 for (int i = 0; i < 16; ++i) {
148 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
149 }
150 }
151
152 // Example data from http://www.ietf.org/rfc/rfc1321.txt A.5 Test Suite
153 TEST(MD5, MD5StringTestSuite1) {
154 std::string actual = MD5String("");
155 std::string expected = "d41d8cd98f00b204e9800998ecf8427e";
156 EXPECT_EQ(expected, actual);
157 }
158
159 TEST(MD5, MD5StringTestSuite2) {
160 std::string actual = MD5String("a");
161 std::string expected = "0cc175b9c0f1b6a831c399e269772661";
162 EXPECT_EQ(expected, actual);
163 }
164
165 TEST(MD5, MD5StringTestSuite3) {
166 std::string actual = MD5String("abc");
167 std::string expected = "900150983cd24fb0d6963f7d28e17f72";
168 EXPECT_EQ(expected, actual);
169 }
170
171 TEST(MD5, MD5StringTestSuite4) {
172 std::string actual = MD5String("message digest");
173 std::string expected = "f96b697d7cb7938d525a2f31aaf161d0";
174 EXPECT_EQ(expected, actual);
175 }
176
177 TEST(MD5, MD5StringTestSuite5) {
178 std::string actual = MD5String("abcdefghijklmnopqrstuvwxyz");
179 std::string expected = "c3fcd3d76192e4007dfb496cca67e13b";
180 EXPECT_EQ(expected, actual);
181 }
182
183 TEST(MD5, MD5StringTestSuite6) {
184 std::string actual = MD5String("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
185 "abcdefghijklmnopqrstuvwxyz"
186 "0123456789");
187 std::string expected = "d174ab98d277d9f5a5611c2c9f419d9f";
188 EXPECT_EQ(expected, actual);
189 }
190
191 TEST(MD5, MD5StringTestSuite7) {
192 std::string actual = MD5String("12345678901234567890"
193 "12345678901234567890"
194 "12345678901234567890"
195 "12345678901234567890");
196 std::string expected = "57edf4a22be3c955ac49da2e2107b67a";
197 EXPECT_EQ(expected, actual);
198 }
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