openMSX
TigerTree_test.cc
Go to the documentation of this file.
1#include "catch.hpp"
2#include "TigerTree.hh"
3#include "tiger.hh"
4#include "ranges.hh"
5#include <span>
6
7using namespace openmsx;
8
9struct TTTestData final : public TTData
10{
11 uint8_t* getData(size_t offset, size_t /*size*/) override
12 {
13 return buffer + offset;
14 }
15
16 bool isCacheStillValid(time_t&) override
17 {
18 return false;
19 }
20
21 uint8_t* buffer;
22};
23
24
25// TODO check that hash (re)calculation is indeed incremental
26
27TEST_CASE("TigerTree")
28{
29 static constexpr auto BLOCK_SIZE = TigerTree::BLOCK_SIZE;
30 std::array<uint8_t, 8 * BLOCK_SIZE + 1> buffer_;
31 auto buffer = subspan(buffer_, 1);
32 TTTestData data;
33 data.buffer = buffer.data();
34
35 std::string dummyName;
36 time_t dummyTime = 0;
37 auto dummyCallback = [](size_t, size_t) {};
38
39 // Note: hash-values (manually) verified via the tool:
40 // rhash --tth <filename>
41 SECTION("zero sized buffer") {
42 TigerTree tt(data, 0, dummyName);
43
44 CHECK(tt.calcHash(dummyCallback).toString() ==
45 "LWPNACQDBZRYXW3VHJVCJ64QBZNGHOHHHZWCLNQ");
46 }
47 SECTION("size less than 1 block") {
48 TigerTree tt(data, 100, dummyName);
49 ranges::fill(subspan<100>(buffer), 0);
50 CHECK(tt.calcHash(dummyCallback).toString() ==
51 "EOIEKIQO6BSNCNRX2UB2MB466INV6LICZ6MPUWQ");
52 ranges::fill(subspan<10>(buffer, 20), 1);
53 tt.notifyChange(20, 10, dummyTime);
54 CHECK(tt.calcHash(dummyCallback).toString() ==
55 "GOTZVYW3WIE37XFCDOY66PLLXWGP6DPN3CQRHWA");
56 }
57 SECTION("3 full and 1 partial block") {
58 TigerTree tt(data, 3 * BLOCK_SIZE + 500, dummyName);
59 ranges::fill(subspan<3 * BLOCK_SIZE + 500>(buffer), 0);
60 CHECK(tt.calcHash(dummyCallback).toString() ==
61 "K6NHCUINLFZ7OUMUZ44JSRABL5C62WTCY2BONUI");
62
63 ranges::fill(subspan<10>(buffer, BLOCK_SIZE + 500), 1);
64 tt.notifyChange(BLOCK_SIZE + 500, 10, dummyTime); // change a single block
65 CHECK(tt.calcHash(dummyCallback).toString() ==
66 "WGRG4PY3CZDLYLTC6BZ2X3G22H6DEB77JH4XPBA");
67
68 ranges::fill(subspan<100>(buffer, 2 * BLOCK_SIZE - 50), 1);
69 tt.notifyChange(2 * BLOCK_SIZE - 50, 100, dummyTime); // change 2 blocks
70 CHECK(tt.calcHash(dummyCallback).toString() ==
71 "YV4NHQASK3QCYJH7FCOO4K3ABNC3WFVDDZ6FYAY");
72
73 }
74 SECTION("7 full blocks (unbalanced internal binary tree)") {
75 TigerTree tt(data, 7 * BLOCK_SIZE, dummyName);
76 ranges::fill(subspan<7 * BLOCK_SIZE>(buffer), 0);
77 CHECK(tt.calcHash(dummyCallback).toString() ==
78 "FPSZ35773WS4WGBVXM255KWNETQZXMTEJGFMLTA");
79
80 ranges::fill(subspan<500>(buffer, 500), 1);
81 tt.notifyChange(500, 500, dummyTime); // part of block-0
82 CHECK(tt.calcHash(dummyCallback).toString() ==
83 "NBCRBTHDNUDTAKZRMYO6TIQGQJIWX74BYNTYXBA");
84
85 ranges::fill(subspan<4 * BLOCK_SIZE>(buffer, 3 * BLOCK_SIZE), 1);
86 tt.notifyChange(3 * BLOCK_SIZE, 4 * BLOCK_SIZE, dummyTime); // blocks 3-6
87 CHECK(tt.calcHash(dummyCallback).toString() ==
88 "PLHCYOTPV4TTXTUPHYGGVPMARGMFE4U5JYRV4VA");
89 }
90}
TEST_CASE("TigerTree")
The TigerTree class will query the to-be-hashed data via this abstract interface.
Definition TigerTree.hh:44
Calculate a tiger-tree-hash.
Definition TigerTree.hh:73
const TigerHash & calcHash(const std::function< void(size_t, size_t)> &progressCallback)
Calculate the hash value.
Definition TigerTree.cc:55
void notifyChange(size_t offset, size_t len, time_t time)
Inform this calculator about changes in the input data.
Definition TigerTree.cc:60
static constexpr size_t BLOCK_SIZE
Definition TigerTree.hh:75
CHECK(m3==m3)
This file implemented 3 utility functions:
Definition Autofire.cc:11
constexpr void fill(ForwardRange &&range, const T &value)
Definition ranges.hh:315
constexpr auto subspan(Range &&range, size_t offset, size_t count=std::dynamic_extent)
Definition ranges.hh:481
bool isCacheStillValid(time_t &) override
Because TTH calculation of a large file takes some time (a few 1/10s for a hard disk image) we try to...
uint8_t * getData(size_t offset, size_t) override
Return the requested portion of the to-be-hashed data block.
uint8_t * buffer
std::string toString() const
Definition tiger.cc:12