| 1 |
//===- llvm/unittest/Support/raw_ostream_test.cpp - raw_ostream 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 |
#include "llvm/Support/Format.h" |
| 10 |
#include "llvm/Support/raw_sha1_ostream.h" |
| 11 |
#include "gtest/gtest.h" |
| 12 |
|
| 13 |
#include <string> |
| 14 |
|
| 15 |
using namespace llvm; |
| 16 |
|
| 17 |
static std::string toHex(ArrayRef<uint8_t> Input) { |
| 18 |
static const char *const LUT = "0123456789ABCDEF"; |
| 19 |
size_t Length = Input.size(); |
| 20 |
|
| 21 |
std::string Output; |
| 22 |
Output.reserve(2 * Length); |
| 23 |
for (size_t i = 0; i < Length; ++i) { |
| 24 |
const unsigned char c = Input[i]; |
| 25 |
Output.push_back(LUT[c >> 4]); |
| 26 |
Output.push_back(LUT[c & 15]); |
| 27 |
} |
| 28 |
return Output; |
| 29 |
} |
| 30 |
|
| 31 |
TEST(raw_sha1_ostreamTest, Basic) { |
| 32 |
llvm::raw_sha1_ostream Sha1Stream; |
| 33 |
Sha1Stream << "Hello World!"; |
| 34 |
auto Hash = toHex(Sha1Stream.sha1()); |
| 35 |
|
| 36 |
ASSERT_EQ("2EF7BDE608CE5404E97D5F042F95F89F1C232871", Hash); |
| 37 |
} |
| 38 |
|
| 39 |
TEST(sha1_hash_test, Basic) { |
| 40 |
ArrayRef<uint8_t> Input((const uint8_t *)"Hello World!", 12); |
| 41 |
std::array<uint8_t, 20> Vec = SHA1::hash(Input); |
| 42 |
std::string Hash = toHex(Vec); |
| 43 |
ASSERT_EQ("2EF7BDE608CE5404E97D5F042F95F89F1C232871", Hash); |
| 44 |
} |
| 45 |
|
| 46 |
TEST(sha1_hash_test, Update) { |
| 47 |
SHA1 sha1; |
| 48 |
std::string Input = "123456789012345678901234567890"; |
| 49 |
ASSERT_EQ(Input.size(), 30UL); |
| 50 |
// 3 short updates. |
| 51 |
sha1.update(Input); |
| 52 |
sha1.update(Input); |
| 53 |
sha1.update(Input); |
| 54 |
// Long update that gets into the optimized loop with prefix/suffix. |
| 55 |
sha1.update(Input + Input + Input + Input); |
| 56 |
// 18 bytes buffered now. |
| 57 |
|
| 58 |
std::string Hash = toHex(sha1.final()); |
| 59 |
ASSERT_EQ("3E4A614101AD84985AB0FE54DC12A6D71551E5AE", Hash); |
| 60 |
} |
| 61 |
|
| 62 |
// Check that getting the intermediate hash in the middle of the stream does |
| 63 |
// not invalidate the final result. |
| 64 |
TEST(raw_sha1_ostreamTest, Intermediate) { |
| 65 |
llvm::raw_sha1_ostream Sha1Stream; |
| 66 |
Sha1Stream << "Hello"; |
| 67 |
auto Hash = toHex(Sha1Stream.sha1()); |
| 68 |
|
| 69 |
ASSERT_EQ("F7FF9E8B7BB2E09B70935A5D785E0CC5D9D0ABF0", Hash); |
| 70 |
Sha1Stream << " World!"; |
| 71 |
Hash = toHex(Sha1Stream.sha1()); |
| 72 |
|
| 73 |
// Compute the non-split hash separately as a reference. |
| 74 |
llvm::raw_sha1_ostream NonSplitSha1Stream; |
| 75 |
NonSplitSha1Stream << "Hello World!"; |
| 76 |
auto NonSplitHash = toHex(NonSplitSha1Stream.sha1()); |
| 77 |
|
| 78 |
ASSERT_EQ(NonSplitHash, Hash); |
| 79 |
} |
| 80 |
|
| 81 |
TEST(raw_sha1_ostreamTest, Reset) { |
| 82 |
llvm::raw_sha1_ostream Sha1Stream; |
| 83 |
Sha1Stream << "Hello"; |
| 84 |
auto Hash = toHex(Sha1Stream.sha1()); |
| 85 |
|
| 86 |
ASSERT_EQ("F7FF9E8B7BB2E09B70935A5D785E0CC5D9D0ABF0", Hash); |
| 87 |
|
| 88 |
Sha1Stream.resetHash(); |
| 89 |
Sha1Stream << " World!"; |
| 90 |
Hash = toHex(Sha1Stream.sha1()); |
| 91 |
|
| 92 |
ASSERT_EQ("7447F2A5A42185C8CF91E632789C431830B59067", Hash); |
| 93 |
} |