mirror of
https://github.com/XRPLF/clio.git
synced 2026-04-29 15:37:53 +00:00
92 lines
2.2 KiB
C++
92 lines
2.2 KiB
C++
#include "util/Shasum.hpp"
|
|
|
|
#include <gtest/gtest.h>
|
|
#include <xrpl/basics/base_uint.h>
|
|
|
|
#include <cstdint>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
using namespace util;
|
|
|
|
struct ShasumTest : testing::Test {
|
|
static constexpr auto kEMPTY_HASH =
|
|
"E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855";
|
|
static constexpr auto kHELLO_WORLD_HASH =
|
|
"B94D27B9934D3E08A52E52D7DA7DABFAC484EFE37A5380EE9088F7ACE2EFCDE9";
|
|
};
|
|
|
|
TEST_F(ShasumTest, sha256sum)
|
|
{
|
|
ripple::uint256 expected;
|
|
|
|
ASSERT_TRUE(expected.parseHex(kEMPTY_HASH));
|
|
EXPECT_EQ(sha256sum(""), expected);
|
|
|
|
ASSERT_TRUE(expected.parseHex(kHELLO_WORLD_HASH));
|
|
EXPECT_EQ(sha256sum("hello world"), expected);
|
|
}
|
|
|
|
TEST_F(ShasumTest, sha256sumString)
|
|
{
|
|
EXPECT_EQ(sha256sumString(""), kEMPTY_HASH);
|
|
EXPECT_EQ(sha256sumString("hello world"), kHELLO_WORLD_HASH);
|
|
}
|
|
|
|
TEST_F(ShasumTest, Sha256sumStreamingEmpty)
|
|
{
|
|
Sha256sum hasher;
|
|
auto result = std::move(hasher).finalize();
|
|
|
|
ripple::uint256 expected;
|
|
ASSERT_TRUE(expected.parseHex(kEMPTY_HASH));
|
|
EXPECT_EQ(result, expected);
|
|
}
|
|
|
|
TEST_F(ShasumTest, Sha256sumStreamingSingleUpdate)
|
|
{
|
|
Sha256sum hasher;
|
|
std::string data = "hello world";
|
|
hasher.update(data.data(), data.size());
|
|
auto result = std::move(hasher).finalize();
|
|
|
|
ripple::uint256 expected;
|
|
ASSERT_TRUE(expected.parseHex(kHELLO_WORLD_HASH));
|
|
EXPECT_EQ(result, expected);
|
|
}
|
|
|
|
TEST_F(ShasumTest, Sha256sumStreamingMultipleUpdates)
|
|
{
|
|
Sha256sum hasher;
|
|
hasher.update("hello", 5);
|
|
hasher.update(" ", 1);
|
|
hasher.update("world", 5);
|
|
auto result = std::move(hasher).finalize();
|
|
|
|
ripple::uint256 expected;
|
|
ASSERT_TRUE(expected.parseHex(kHELLO_WORLD_HASH));
|
|
EXPECT_EQ(result, expected);
|
|
}
|
|
|
|
TEST_F(ShasumTest, Sha256sumUpdateTemplate)
|
|
{
|
|
Sha256sum hasher;
|
|
|
|
uint32_t value32 = 0x12345678;
|
|
uint64_t value64 = 0x123456789ABCDEF0;
|
|
|
|
hasher.update(value32);
|
|
hasher.update(value64);
|
|
|
|
auto result1 = std::move(hasher).finalize();
|
|
|
|
// Verify same result with raw data
|
|
Sha256sum hasher2;
|
|
hasher2.update(&value32, sizeof(value32));
|
|
hasher2.update(&value64, sizeof(value64));
|
|
|
|
auto result2 = std::move(hasher2).finalize();
|
|
|
|
EXPECT_EQ(result1, result2);
|
|
}
|