[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / unittests / Support / MD5Test.cpp
blob83c556cbc5bd994fe4ffa9ec635ff779819f6780
1 //===- llvm/unittest/Support/MD5Test.cpp - MD5 tests ----------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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"
18 using namespace llvm;
20 namespace {
21 /// Tests an arbitrary set of bytes passed as \p Input.
22 void TestMD5Sum(ArrayRef<uint8_t> Input, StringRef Final) {
23 MD5 Hash;
24 Hash.update(Input);
25 MD5::MD5Result MD5Res;
26 Hash.final(MD5Res);
27 SmallString<32> Res;
28 MD5::stringifyResult(MD5Res, Res);
29 EXPECT_EQ(Res, Final);
32 void TestMD5Sum(StringRef Input, StringRef Final) {
33 MD5 Hash;
34 Hash.update(Input);
35 MD5::MD5Result MD5Res;
36 Hash.final(MD5Res);
37 SmallString<32> Res;
38 MD5::stringifyResult(MD5Res, Res);
39 EXPECT_EQ(Res, Final);
42 TEST(MD5Test, MD5) {
43 TestMD5Sum(makeArrayRef((const uint8_t *)"", (size_t) 0),
44 "d41d8cd98f00b204e9800998ecf8427e");
45 TestMD5Sum(makeArrayRef((const uint8_t *)"a", (size_t) 1),
46 "0cc175b9c0f1b6a831c399e269772661");
47 TestMD5Sum(makeArrayRef((const uint8_t *)"abcdefghijklmnopqrstuvwxyz",
48 (size_t) 26),
49 "c3fcd3d76192e4007dfb496cca67e13b");
50 TestMD5Sum(makeArrayRef((const uint8_t *)"\0", (size_t) 1),
51 "93b885adfe0da089cdf634904fd59f71");
52 TestMD5Sum(makeArrayRef((const uint8_t *)"a\0", (size_t) 2),
53 "4144e195f46de78a3623da7364d04f11");
54 TestMD5Sum(makeArrayRef((const uint8_t *)"abcdefghijklmnopqrstuvwxyz\0",
55 (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;
64 SmallString<32> Res;
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) {
73 MD5 Hash;
75 Hash.update("abcd");
78 MD5 ReferenceHash;
79 ReferenceHash.update("abcd");
80 MD5::MD5Result ReferenceResult;
81 ReferenceHash.final(ReferenceResult);
82 EXPECT_EQ(Hash.result(), ReferenceResult);
85 Hash.update("xyz");
88 MD5 ReferenceHash;
89 ReferenceHash.update("abcd");
90 ReferenceHash.update("xyz");
91 MD5::MD5Result ReferenceResult;
92 ReferenceHash.final(ReferenceResult);
93 EXPECT_EQ(Hash.final(), ReferenceResult);
96 } // namespace