1 //===- llvm/unittest/Support/raw_ostream_test.cpp - raw_ostream 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 #include "llvm/Support/Format.h"
10 #include "llvm/Support/raw_sha1_ostream.h"
11 #include "gtest/gtest.h"
17 static std::string
toHex(StringRef Input
) {
18 static const char *const LUT
= "0123456789ABCDEF";
19 size_t Length
= Input
.size();
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]);
31 TEST(raw_sha1_ostreamTest
, Basic
) {
32 llvm::raw_sha1_ostream Sha1Stream
;
33 Sha1Stream
<< "Hello World!";
34 auto Hash
= toHex(Sha1Stream
.sha1());
36 ASSERT_EQ("2EF7BDE608CE5404E97D5F042F95F89F1C232871", Hash
);
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({(const char *)Vec
.data(), 20});
43 ASSERT_EQ("2EF7BDE608CE5404E97D5F042F95F89F1C232871", Hash
);
46 // Check that getting the intermediate hash in the middle of the stream does
47 // not invalidate the final result.
48 TEST(raw_sha1_ostreamTest
, Intermediate
) {
49 llvm::raw_sha1_ostream Sha1Stream
;
50 Sha1Stream
<< "Hello";
51 auto Hash
= toHex(Sha1Stream
.sha1());
53 ASSERT_EQ("F7FF9E8B7BB2E09B70935A5D785E0CC5D9D0ABF0", Hash
);
54 Sha1Stream
<< " World!";
55 Hash
= toHex(Sha1Stream
.sha1());
57 // Compute the non-split hash separately as a reference.
58 llvm::raw_sha1_ostream NonSplitSha1Stream
;
59 NonSplitSha1Stream
<< "Hello World!";
60 auto NonSplitHash
= toHex(NonSplitSha1Stream
.sha1());
62 ASSERT_EQ(NonSplitHash
, Hash
);
65 TEST(raw_sha1_ostreamTest
, Reset
) {
66 llvm::raw_sha1_ostream Sha1Stream
;
67 Sha1Stream
<< "Hello";
68 auto Hash
= toHex(Sha1Stream
.sha1());
70 ASSERT_EQ("F7FF9E8B7BB2E09B70935A5D785E0CC5D9D0ABF0", Hash
);
72 Sha1Stream
.resetHash();
73 Sha1Stream
<< " World!";
74 Hash
= toHex(Sha1Stream
.sha1());
76 ASSERT_EQ("7447F2A5A42185C8CF91E632789C431830B59067", Hash
);