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

Side by Side Diff: third_party/libaddressinput/chromium/cpp/test/util/md5_unittest.cc

Issue 110533002: [rac] Temporarily copy libaddressinput to Chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Avoid redefinition of basictypes.h Created 7 years 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
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 // The original source code is from:
6 // http://src.chromium.org/viewvc/chrome/trunk/src/base/md5_unittest.cc?revision =94203
7
8 #include "util/md5.h"
9
10 #include <libaddressinput/util/scoped_ptr.h>
11
12 #include <cstring>
13 #include <string>
14
15 #include <gtest/gtest.h>
16
17 namespace {
18
19 using i18n::addressinput::MD5Context;
20 using i18n::addressinput::MD5Digest;
21 using i18n::addressinput::MD5Init;
22 using i18n::addressinput::MD5String;
23 using i18n::addressinput::MD5Update;
24 using i18n::addressinput::scoped_ptr;
25
26 TEST(MD5, DigestToBase16) {
27 MD5Digest digest;
28
29 int data[] = {
30 0xd4, 0x1d, 0x8c, 0xd9,
31 0x8f, 0x00, 0xb2, 0x04,
32 0xe9, 0x80, 0x09, 0x98,
33 0xec, 0xf8, 0x42, 0x7e
34 };
35
36 for (int i = 0; i < 16; ++i)
37 digest.a[i] = data[i] & 0xff;
38
39 std::string actual = MD5DigestToBase16(digest);
40 std::string expected = "d41d8cd98f00b204e9800998ecf8427e";
41
42 EXPECT_EQ(expected, actual);
43 }
44
45 TEST(MD5, MD5SumEmtpyData) {
46 MD5Digest digest;
47 const char* data = "";
48
49 MD5Sum(data, strlen(data), &digest);
50
51 int expected[] = {
52 0xd4, 0x1d, 0x8c, 0xd9,
53 0x8f, 0x00, 0xb2, 0x04,
54 0xe9, 0x80, 0x09, 0x98,
55 0xec, 0xf8, 0x42, 0x7e
56 };
57
58 for (int i = 0; i < 16; ++i)
59 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
60 }
61
62 TEST(MD5, MD5SumOneByteData) {
63 MD5Digest digest;
64 const char* data = "a";
65
66 MD5Sum(data, strlen(data), &digest);
67
68 int expected[] = {
69 0x0c, 0xc1, 0x75, 0xb9,
70 0xc0, 0xf1, 0xb6, 0xa8,
71 0x31, 0xc3, 0x99, 0xe2,
72 0x69, 0x77, 0x26, 0x61
73 };
74
75 for (int i = 0; i < 16; ++i)
76 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
77 }
78
79 TEST(MD5, MD5SumLongData) {
80 const int length = 10 * 1024 * 1024 + 1;
81 scoped_ptr<char[]> data(new char[length]);
82
83 for (int i = 0; i < length; ++i)
84 data[i] = i & 0xFF;
85
86 MD5Digest digest;
87 MD5Sum(data.get(), length, &digest);
88
89 int expected[] = {
90 0x90, 0xbd, 0x6a, 0xd9,
91 0x0a, 0xce, 0xf5, 0xad,
92 0xaa, 0x92, 0x20, 0x3e,
93 0x21, 0xc7, 0xa1, 0x3e
94 };
95
96 for (int i = 0; i < 16; ++i)
97 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
98 }
99
100 TEST(MD5, ContextWithEmptyData) {
101 MD5Context ctx;
102 MD5Init(&ctx);
103
104 MD5Digest digest;
105 MD5Final(&digest, &ctx);
106
107 int expected[] = {
108 0xd4, 0x1d, 0x8c, 0xd9,
109 0x8f, 0x00, 0xb2, 0x04,
110 0xe9, 0x80, 0x09, 0x98,
111 0xec, 0xf8, 0x42, 0x7e
112 };
113
114 for (int i = 0; i < 16; ++i)
115 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
116 }
117
118 TEST(MD5, ContextWithLongData) {
119 MD5Context ctx;
120 MD5Init(&ctx);
121
122 const int length = 10 * 1024 * 1024 + 1;
123 scoped_ptr<char[]> data(new char[length]);
124
125 for (int i = 0; i < length; ++i)
126 data[i] = i & 0xFF;
127
128 int total = 0;
129 while (total < length) {
130 int len = 4097; // intentionally not 2^k.
131 if (len > length - total)
132 len = length - total;
133
134 MD5Update(&ctx,
135 std::string(reinterpret_cast<char*>(data.get() + total), len));
136 total += len;
137 }
138
139 EXPECT_EQ(length, total);
140
141 MD5Digest digest;
142 MD5Final(&digest, &ctx);
143
144 int expected[] = {
145 0x90, 0xbd, 0x6a, 0xd9,
146 0x0a, 0xce, 0xf5, 0xad,
147 0xaa, 0x92, 0x20, 0x3e,
148 0x21, 0xc7, 0xa1, 0x3e
149 };
150
151 for (int i = 0; i < 16; ++i)
152 EXPECT_EQ(expected[i], digest.a[i] & 0xFF);
153 }
154
155 // Example data from http://www.ietf.org/rfc/rfc1321.txt A.5 Test Suite
156 TEST(MD5, MD5StringTestSuite1) {
157 std::string actual = MD5String("");
158 std::string expected = "d41d8cd98f00b204e9800998ecf8427e";
159 EXPECT_EQ(expected, actual);
160 }
161
162 TEST(MD5, MD5StringTestSuite2) {
163 std::string actual = MD5String("a");
164 std::string expected = "0cc175b9c0f1b6a831c399e269772661";
165 EXPECT_EQ(expected, actual);
166 }
167
168 TEST(MD5, MD5StringTestSuite3) {
169 std::string actual = MD5String("abc");
170 std::string expected = "900150983cd24fb0d6963f7d28e17f72";
171 EXPECT_EQ(expected, actual);
172 }
173
174 TEST(MD5, MD5StringTestSuite4) {
175 std::string actual = MD5String("message digest");
176 std::string expected = "f96b697d7cb7938d525a2f31aaf161d0";
177 EXPECT_EQ(expected, actual);
178 }
179
180 TEST(MD5, MD5StringTestSuite5) {
181 std::string actual = MD5String("abcdefghijklmnopqrstuvwxyz");
182 std::string expected = "c3fcd3d76192e4007dfb496cca67e13b";
183 EXPECT_EQ(expected, actual);
184 }
185
186 TEST(MD5, MD5StringTestSuite6) {
187 std::string actual = MD5String("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
188 "abcdefghijklmnopqrstuvwxyz"
189 "0123456789");
190 std::string expected = "d174ab98d277d9f5a5611c2c9f419d9f";
191 EXPECT_EQ(expected, actual);
192 }
193
194 TEST(MD5, MD5StringTestSuite7) {
195 std::string actual = MD5String("12345678901234567890"
196 "12345678901234567890"
197 "12345678901234567890"
198 "12345678901234567890");
199 std::string expected = "57edf4a22be3c955ac49da2e2107b67a";
200 EXPECT_EQ(expected, actual);
201 }
202
203 TEST(MD5, ContextWithStringData) {
204 MD5Context ctx;
205 MD5Init(&ctx);
206
207 MD5Update(&ctx, "abc");
208
209 MD5Digest digest;
210 MD5Final(&digest, &ctx);
211
212 std::string actual = MD5DigestToBase16(digest);
213 std::string expected = "900150983cd24fb0d6963f7d28e17f72";
214
215 EXPECT_EQ(expected, actual);
216 }
217
218 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698