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 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 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 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 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 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 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 145 TEST(MD5, MD5StringTestSuite1) { 146 std::string actual = MD5String(""); 147 std::string expected = "d41d8cd98f00b204e9800998ecf8427e"; 148 EXPECT_EQ(expected, actual); 149 } 150 151 TEST(MD5, MD5StringTestSuite2) { 152 std::string actual = MD5String("a"); 153 std::string expected = "0cc175b9c0f1b6a831c399e269772661"; 154 EXPECT_EQ(expected, actual); 155 } 156 157 TEST(MD5, MD5StringTestSuite3) { 158 std::string actual = MD5String("abc"); 159 std::string expected = "900150983cd24fb0d6963f7d28e17f72"; 160 EXPECT_EQ(expected, actual); 161 } 162 163 TEST(MD5, MD5StringTestSuite4) { 164 std::string actual = MD5String("message digest"); 165 std::string expected = "f96b697d7cb7938d525a2f31aaf161d0"; 166 EXPECT_EQ(expected, actual); 167 } 168 169 TEST(MD5, MD5StringTestSuite5) { 170 std::string actual = MD5String("abcdefghijklmnopqrstuvwxyz"); 171 std::string expected = "c3fcd3d76192e4007dfb496cca67e13b"; 172 EXPECT_EQ(expected, actual); 173 } 174 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 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 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 } // namespace base 208