1 //===- llvm/unittest/Support/MD5Test.cpp - MD5 tests ----------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements unit tests for the MD5 functions.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/MD5.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "gtest/gtest.h"
21 /// Tests an arbitrary set of bytes passed as \p Input.
22 void TestMD5Sum(ArrayRef
<uint8_t> Input
, StringRef Final
) {
25 MD5::MD5Result MD5Res
;
28 MD5::stringifyResult(MD5Res
, Res
);
29 EXPECT_EQ(Res
, Final
);
32 void TestMD5Sum(StringRef Input
, StringRef Final
) {
35 MD5::MD5Result MD5Res
;
38 MD5::stringifyResult(MD5Res
, Res
);
39 EXPECT_EQ(Res
, Final
);
43 TestMD5Sum(ArrayRef((const uint8_t *)"", (size_t)0),
44 "d41d8cd98f00b204e9800998ecf8427e");
45 TestMD5Sum(ArrayRef((const uint8_t *)"a", (size_t)1),
46 "0cc175b9c0f1b6a831c399e269772661");
48 ArrayRef((const uint8_t *)"abcdefghijklmnopqrstuvwxyz", (size_t)26),
49 "c3fcd3d76192e4007dfb496cca67e13b");
50 TestMD5Sum(ArrayRef((const uint8_t *)"\0", (size_t)1),
51 "93b885adfe0da089cdf634904fd59f71");
52 TestMD5Sum(ArrayRef((const uint8_t *)"a\0", (size_t)2),
53 "4144e195f46de78a3623da7364d04f11");
55 ArrayRef((const uint8_t *)"abcdefghijklmnopqrstuvwxyz\0", (size_t)27),
56 "81948d1f1554f58cd1a56ebb01f808cb");
57 TestMD5Sum("abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b");
60 TEST(MD5HashTest
, MD5
) {
61 ArrayRef
<uint8_t> Input((const uint8_t *)"abcdefghijklmnopqrstuvwxyz", 26);
62 std::array
<uint8_t, 16> Vec
= MD5::hash(Input
);
63 MD5::MD5Result MD5Res
;
65 memcpy(MD5Res
.data(), Vec
.data(), Vec
.size());
66 MD5::stringifyResult(MD5Res
, Res
);
67 EXPECT_EQ(Res
, "c3fcd3d76192e4007dfb496cca67e13b");
68 EXPECT_EQ(0x3be167ca6c49fb7dULL
, MD5Res
.high());
69 EXPECT_EQ(0x00e49261d7d3fcc3ULL
, MD5Res
.low());
72 TEST(MD5Test
, FinalAndResultHelpers
) {
79 ReferenceHash
.update("abcd");
80 MD5::MD5Result ReferenceResult
;
81 ReferenceHash
.final(ReferenceResult
);
82 EXPECT_EQ(Hash
.result(), ReferenceResult
);
89 ReferenceHash
.update("abcd");
90 ReferenceHash
.update("xyz");
91 MD5::MD5Result ReferenceResult
;
92 ReferenceHash
.final(ReferenceResult
);
93 EXPECT_EQ(Hash
.final(), ReferenceResult
);