1 //===- llvm/unittest/Support/MD5Test.cpp - MD5 tests ----------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements unit tests for the MD5 functions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/MD5.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "gtest/gtest.h"
18
19 using namespace llvm;
20
21 namespace {
22 /// Tests an arbitrary set of bytes passed as \p Input.
TestMD5Sum(ArrayRef<uint8_t> Input,StringRef Final)23 void TestMD5Sum(ArrayRef<uint8_t> Input, StringRef Final) {
24 MD5 Hash;
25 Hash.update(Input);
26 MD5::MD5Result MD5Res;
27 Hash.final(MD5Res);
28 SmallString<32> Res;
29 MD5::stringifyResult(MD5Res, Res);
30 EXPECT_EQ(Res, Final);
31 }
32
TestMD5Sum(StringRef Input,StringRef Final)33 void TestMD5Sum(StringRef Input, StringRef Final) {
34 MD5 Hash;
35 Hash.update(Input);
36 MD5::MD5Result MD5Res;
37 Hash.final(MD5Res);
38 SmallString<32> Res;
39 MD5::stringifyResult(MD5Res, Res);
40 EXPECT_EQ(Res, Final);
41 }
42
TEST(MD5Test,MD5)43 TEST(MD5Test, MD5) {
44 TestMD5Sum(makeArrayRef((const uint8_t *)"", (size_t) 0),
45 "d41d8cd98f00b204e9800998ecf8427e");
46 TestMD5Sum(makeArrayRef((const uint8_t *)"a", (size_t) 1),
47 "0cc175b9c0f1b6a831c399e269772661");
48 TestMD5Sum(makeArrayRef((const uint8_t *)"abcdefghijklmnopqrstuvwxyz",
49 (size_t) 26),
50 "c3fcd3d76192e4007dfb496cca67e13b");
51 TestMD5Sum(makeArrayRef((const uint8_t *)"\0", (size_t) 1),
52 "93b885adfe0da089cdf634904fd59f71");
53 TestMD5Sum(makeArrayRef((const uint8_t *)"a\0", (size_t) 2),
54 "4144e195f46de78a3623da7364d04f11");
55 TestMD5Sum(makeArrayRef((const uint8_t *)"abcdefghijklmnopqrstuvwxyz\0",
56 (size_t) 27),
57 "81948d1f1554f58cd1a56ebb01f808cb");
58 TestMD5Sum("abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b");
59 }
60
TEST(MD5HashTest,MD5)61 TEST(MD5HashTest, MD5) {
62 ArrayRef<uint8_t> Input((const uint8_t *)"abcdefghijklmnopqrstuvwxyz", 26);
63 std::array<uint8_t, 16> Vec = MD5::hash(Input);
64 MD5::MD5Result MD5Res;
65 SmallString<32> Res;
66 memcpy(MD5Res.Bytes.data(), Vec.data(), Vec.size());
67 MD5::stringifyResult(MD5Res, Res);
68 EXPECT_EQ(Res, "c3fcd3d76192e4007dfb496cca67e13b");
69 EXPECT_EQ(0x3be167ca6c49fb7dULL, MD5Res.high());
70 EXPECT_EQ(0x00e49261d7d3fcc3ULL, MD5Res.low());
71 }
72 }
73