2013-05-25 07:08:17 +08:00
|
|
|
//===- llvm/unittest/Support/MD5Test.cpp - MD5 tests ----------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-05-25 07:08:17 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements unit tests for the MD5 functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 19:06:56 +08:00
|
|
|
#include "llvm/Support/MD5.h"
|
2013-05-25 07:08:17 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Tests an arbitrary set of bytes passed as \p Input.
|
2013-06-01 06:34:52 +08:00
|
|
|
void TestMD5Sum(ArrayRef<uint8_t> Input, StringRef Final) {
|
2013-05-25 07:08:17 +08:00
|
|
|
MD5 Hash;
|
|
|
|
Hash.update(Input);
|
|
|
|
MD5::MD5Result MD5Res;
|
|
|
|
Hash.final(MD5Res);
|
|
|
|
SmallString<32> Res;
|
|
|
|
MD5::stringifyResult(MD5Res, Res);
|
|
|
|
EXPECT_EQ(Res, Final);
|
|
|
|
}
|
|
|
|
|
2013-06-01 06:34:56 +08:00
|
|
|
void TestMD5Sum(StringRef Input, StringRef Final) {
|
|
|
|
MD5 Hash;
|
|
|
|
Hash.update(Input);
|
|
|
|
MD5::MD5Result MD5Res;
|
|
|
|
Hash.final(MD5Res);
|
|
|
|
SmallString<32> Res;
|
|
|
|
MD5::stringifyResult(MD5Res, Res);
|
|
|
|
EXPECT_EQ(Res, Final);
|
|
|
|
}
|
|
|
|
|
2013-05-25 07:08:17 +08:00
|
|
|
TEST(MD5Test, MD5) {
|
2014-08-27 13:25:25 +08:00
|
|
|
TestMD5Sum(makeArrayRef((const uint8_t *)"", (size_t) 0),
|
2013-05-25 07:08:17 +08:00
|
|
|
"d41d8cd98f00b204e9800998ecf8427e");
|
2014-08-27 13:25:25 +08:00
|
|
|
TestMD5Sum(makeArrayRef((const uint8_t *)"a", (size_t) 1),
|
2013-05-25 07:08:17 +08:00
|
|
|
"0cc175b9c0f1b6a831c399e269772661");
|
2014-08-27 13:25:25 +08:00
|
|
|
TestMD5Sum(makeArrayRef((const uint8_t *)"abcdefghijklmnopqrstuvwxyz",
|
|
|
|
(size_t) 26),
|
2013-05-25 07:08:17 +08:00
|
|
|
"c3fcd3d76192e4007dfb496cca67e13b");
|
2014-08-27 13:25:25 +08:00
|
|
|
TestMD5Sum(makeArrayRef((const uint8_t *)"\0", (size_t) 1),
|
2013-05-30 08:43:26 +08:00
|
|
|
"93b885adfe0da089cdf634904fd59f71");
|
2014-08-27 13:25:25 +08:00
|
|
|
TestMD5Sum(makeArrayRef((const uint8_t *)"a\0", (size_t) 2),
|
2013-05-30 08:43:26 +08:00
|
|
|
"4144e195f46de78a3623da7364d04f11");
|
2014-08-27 13:25:25 +08:00
|
|
|
TestMD5Sum(makeArrayRef((const uint8_t *)"abcdefghijklmnopqrstuvwxyz\0",
|
|
|
|
(size_t) 27),
|
2013-05-30 08:43:26 +08:00
|
|
|
"81948d1f1554f58cd1a56ebb01f808cb");
|
2013-06-01 06:34:56 +08:00
|
|
|
TestMD5Sum("abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b");
|
2013-05-25 07:08:17 +08:00
|
|
|
}
|
2016-11-23 08:46:09 +08:00
|
|
|
|
|
|
|
TEST(MD5HashTest, MD5) {
|
|
|
|
ArrayRef<uint8_t> Input((const uint8_t *)"abcdefghijklmnopqrstuvwxyz", 26);
|
|
|
|
std::array<uint8_t, 16> Vec = MD5::hash(Input);
|
|
|
|
MD5::MD5Result MD5Res;
|
|
|
|
SmallString<32> Res;
|
2017-03-21 07:33:18 +08:00
|
|
|
memcpy(MD5Res.Bytes.data(), Vec.data(), Vec.size());
|
2016-11-23 08:46:09 +08:00
|
|
|
MD5::stringifyResult(MD5Res, Res);
|
|
|
|
EXPECT_EQ(Res, "c3fcd3d76192e4007dfb496cca67e13b");
|
2017-03-21 07:33:18 +08:00
|
|
|
EXPECT_EQ(0x3be167ca6c49fb7dULL, MD5Res.high());
|
|
|
|
EXPECT_EQ(0x00e49261d7d3fcc3ULL, MD5Res.low());
|
2016-11-23 08:46:09 +08:00
|
|
|
}
|
2013-05-25 07:08:17 +08:00
|
|
|
}
|