1 /* 2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "webrtc/base/md5digest.h" 12 #include "webrtc/base/gunit.h" 13 #include "webrtc/base/stringencode.h" 14 15 namespace rtc { 16 17 std::string Md5(const std::string& input) { 18 Md5Digest md5; 19 return ComputeDigest(&md5, input); 20 } 21 22 TEST(Md5DigestTest, TestSize) { 23 Md5Digest md5; 24 EXPECT_EQ(16, static_cast<int>(Md5Digest::kSize)); 25 EXPECT_EQ(16U, md5.Size()); 26 } 27 28 TEST(Md5DigestTest, TestBasic) { 29 // These are the standard MD5 test vectors from RFC 1321. 30 EXPECT_EQ("d41d8cd98f00b204e9800998ecf8427e", Md5("")); 31 EXPECT_EQ("0cc175b9c0f1b6a831c399e269772661", Md5("a")); 32 EXPECT_EQ("900150983cd24fb0d6963f7d28e17f72", Md5("abc")); 33 EXPECT_EQ("f96b697d7cb7938d525a2f31aaf161d0", Md5("message digest")); 34 EXPECT_EQ("c3fcd3d76192e4007dfb496cca67e13b", 35 Md5("abcdefghijklmnopqrstuvwxyz")); 36 } 37 38 TEST(Md5DigestTest, TestMultipleUpdates) { 39 Md5Digest md5; 40 std::string input = "abcdefghijklmnopqrstuvwxyz"; 41 char output[Md5Digest::kSize]; 42 for (size_t i = 0; i < input.size(); ++i) { 43 md5.Update(&input[i], 1); 44 } 45 EXPECT_EQ(md5.Size(), md5.Finish(output, sizeof(output))); 46 EXPECT_EQ("c3fcd3d76192e4007dfb496cca67e13b", 47 hex_encode(output, sizeof(output))); 48 } 49 50 TEST(Md5DigestTest, TestReuse) { 51 Md5Digest md5; 52 std::string input = "message digest"; 53 EXPECT_EQ("f96b697d7cb7938d525a2f31aaf161d0", ComputeDigest(&md5, input)); 54 input = "abcdefghijklmnopqrstuvwxyz"; 55 EXPECT_EQ("c3fcd3d76192e4007dfb496cca67e13b", ComputeDigest(&md5, input)); 56 } 57 58 TEST(Md5DigestTest, TestBufferTooSmall) { 59 Md5Digest md5; 60 std::string input = "abcdefghijklmnopqrstuvwxyz"; 61 char output[Md5Digest::kSize - 1]; 62 md5.Update(input.c_str(), input.size()); 63 EXPECT_EQ(0U, md5.Finish(output, sizeof(output))); 64 } 65 66 TEST(Md5DigestTest, TestBufferConst) { 67 Md5Digest md5; 68 const int kLongSize = 1000000; 69 std::string input(kLongSize, '\0'); 70 for (int i = 0; i < kLongSize; ++i) { 71 input[i] = static_cast<char>(i); 72 } 73 md5.Update(input.c_str(), input.size()); 74 for (int i = 0; i < kLongSize; ++i) { 75 EXPECT_EQ(static_cast<char>(i), input[i]); 76 } 77 } 78 79 } // namespace rtc 80