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

Side by Side Diff: core/fdrm/crypto/fx_crypt_unittest.cpp

Issue 2517153003: Add unit test for fdrm's MD5 (Closed)
Patch Set: year Created 4 years, 1 month 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
« no previous file with comments | « core/fdrm/crypto/fx_crypt.cpp ('k') | core/fpdfapi/parser/cpdf_security_handler.cpp » ('j') | 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) 2016 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 // Originally from chromium's /src/base/md5_unittest.cc.
6
7 #include "core/fdrm/crypto/fx_crypt.h"
8
9 #include <memory>
10 #include <string>
11
12 #include "core/fxcrt/fx_basic.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace {
16
17 std::string CRYPT_ToBase16(const uint8_t* digest) {
18 static char const zEncode[] = "0123456789abcdef";
19 std::string ret;
20 ret.resize(32);
21 for (int i = 0, j = 0; i < 16; i++, j += 2) {
22 uint8_t a = digest[i];
23 ret[j] = zEncode[(a >> 4) & 0xf];
24 ret[j + 1] = zEncode[a & 0xf];
25 }
26 return ret;
27 }
28
29 std::string CRYPT_MD5String(const char* str) {
30 uint8_t digest[16];
31 CRYPT_MD5Generate(reinterpret_cast<const uint8_t*>(str), strlen(str), digest);
32 return CRYPT_ToBase16(digest);
33 }
34
35 } // namespace
36
37 TEST(FXCRYPT, CRYPT_ToBase16) {
38 uint8_t data[] = {0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
39 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e};
40
41 std::string actual = CRYPT_ToBase16(data);
42 std::string expected = "d41d8cd98f00b204e9800998ecf8427e";
43
44 EXPECT_EQ(expected, actual);
45 }
46
47 TEST(FXCRYPT, MD5GenerateEmtpyData) {
48 uint8_t digest[16];
49 const char data[] = "";
50 uint32_t length = static_cast<uint32_t>(strlen(data));
51
52 CRYPT_MD5Generate(reinterpret_cast<const uint8_t*>(data), length, digest);
53
54 uint8_t expected[] = {0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
55 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e};
56
57 for (int i = 0; i < 16; ++i)
58 EXPECT_EQ(expected[i], digest[i]);
59 }
60
61 TEST(FXCRYPT, MD5GenerateOneByteData) {
62 uint8_t digest[16];
63 const char data[] = "a";
64 uint32_t length = static_cast<uint32_t>(strlen(data));
65
66 CRYPT_MD5Generate(reinterpret_cast<const uint8_t*>(data), length, digest);
67
68 uint8_t expected[] = {0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8,
69 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61};
70
71 for (int i = 0; i < 16; ++i)
72 EXPECT_EQ(expected[i], digest[i]);
73 }
74
75 TEST(FXCRYPT, MD5GenerateLongData) {
76 const uint32_t length = 10 * 1024 * 1024 + 1;
77 std::unique_ptr<char[]> data(new char[length]);
78
79 for (uint32_t i = 0; i < length; ++i)
80 data[i] = i & 0xFF;
81
82 uint8_t digest[16];
83 CRYPT_MD5Generate(reinterpret_cast<const uint8_t*>(data.get()), length,
84 digest);
85
86 uint8_t expected[] = {0x90, 0xbd, 0x6a, 0xd9, 0x0a, 0xce, 0xf5, 0xad,
87 0xaa, 0x92, 0x20, 0x3e, 0x21, 0xc7, 0xa1, 0x3e};
88
89 for (int i = 0; i < 16; ++i)
90 EXPECT_EQ(expected[i], digest[i]);
91 }
92
93 TEST(FXCRYPT, ContextWithEmptyData) {
94 CRYPT_md5_context ctx;
95 CRYPT_MD5Start(&ctx);
96
97 uint8_t digest[16];
98 CRYPT_MD5Finish(&ctx, digest);
99
100 uint8_t expected[] = {0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
101 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e};
102
103 for (int i = 0; i < 16; ++i)
104 EXPECT_EQ(expected[i], digest[i]);
105 }
106
107 TEST(FXCRYPT, ContextWithLongData) {
108 CRYPT_md5_context ctx;
109 CRYPT_MD5Start(&ctx);
110
111 const uint32_t length = 10 * 1024 * 1024 + 1;
112 std::unique_ptr<uint8_t[]> data(new uint8_t[length]);
113
114 for (uint32_t i = 0; i < length; ++i)
115 data[i] = i & 0xFF;
116
117 uint32_t total = 0;
118 while (total < length) {
119 uint32_t len = 4097; // intentionally not 2^k.
120 if (len > length - total)
121 len = length - total;
122
123 CRYPT_MD5Update(&ctx, data.get() + total, len);
124 total += len;
125 }
126
127 EXPECT_EQ(length, total);
128
129 uint8_t digest[16];
130 CRYPT_MD5Finish(&ctx, digest);
131
132 uint8_t expected[] = {0x90, 0xbd, 0x6a, 0xd9, 0x0a, 0xce, 0xf5, 0xad,
133 0xaa, 0x92, 0x20, 0x3e, 0x21, 0xc7, 0xa1, 0x3e};
134
135 for (int i = 0; i < 16; ++i)
136 EXPECT_EQ(expected[i], digest[i]);
137 }
138
139 // Example data from http://www.ietf.org/rfc/rfc1321.txt A.5 Test Suite
140 TEST(FXCRYPT, MD5StringTestSuite1) {
141 std::string actual = CRYPT_MD5String("");
142 std::string expected = "d41d8cd98f00b204e9800998ecf8427e";
143 EXPECT_EQ(expected, actual);
144 }
145
146 TEST(FXCRYPT, MD5StringTestSuite2) {
147 std::string actual = CRYPT_MD5String("a");
148 std::string expected = "0cc175b9c0f1b6a831c399e269772661";
149 EXPECT_EQ(expected, actual);
150 }
151
152 TEST(FXCRYPT, MD5StringTestSuite3) {
153 std::string actual = CRYPT_MD5String("abc");
154 std::string expected = "900150983cd24fb0d6963f7d28e17f72";
155 EXPECT_EQ(expected, actual);
156 }
157
158 TEST(FXCRYPT, MD5StringTestSuite4) {
159 std::string actual = CRYPT_MD5String("message digest");
160 std::string expected = "f96b697d7cb7938d525a2f31aaf161d0";
161 EXPECT_EQ(expected, actual);
162 }
163
164 TEST(FXCRYPT, MD5StringTestSuite5) {
165 std::string actual = CRYPT_MD5String("abcdefghijklmnopqrstuvwxyz");
166 std::string expected = "c3fcd3d76192e4007dfb496cca67e13b";
167 EXPECT_EQ(expected, actual);
168 }
169
170 TEST(FXCRYPT, MD5StringTestSuite6) {
171 std::string actual = CRYPT_MD5String(
172 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
173 "abcdefghijklmnopqrstuvwxyz"
174 "0123456789");
175 std::string expected = "d174ab98d277d9f5a5611c2c9f419d9f";
176 EXPECT_EQ(expected, actual);
177 }
178
179 TEST(FXCRYPT, MD5StringTestSuite7) {
180 std::string actual = CRYPT_MD5String(
181 "12345678901234567890"
182 "12345678901234567890"
183 "12345678901234567890"
184 "12345678901234567890");
185 std::string expected = "57edf4a22be3c955ac49da2e2107b67a";
186 EXPECT_EQ(expected, actual);
187 }
188
189 TEST(FXCRYPT, ContextWithStringData) {
190 CRYPT_md5_context ctx;
191 CRYPT_MD5Start(&ctx);
192 CRYPT_MD5Update(&ctx, reinterpret_cast<const uint8_t*>("abc"), 3);
193
194 uint8_t digest[16];
195 CRYPT_MD5Finish(&ctx, digest);
196
197 std::string actual = CRYPT_ToBase16(digest);
198 std::string expected = "900150983cd24fb0d6963f7d28e17f72";
199 EXPECT_EQ(expected, actual);
200 }
OLDNEW
« no previous file with comments | « core/fdrm/crypto/fx_crypt.cpp ('k') | core/fpdfapi/parser/cpdf_security_handler.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698