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 <cstring>
5 
6 using namespace openmsx;
7 
8 struct TTTestData final : public TTData
9 {
10  uint8_t* getData(size_t offset, size_t /*size*/) override
11  {
12  return buffer + offset;
13  }
14 
15  bool isCacheStillValid(time_t&) override
16  {
17  return false;
18  }
19 
20  uint8_t* buffer;
21 };
22 
23 
24 // TODO check that hash (re)calculation is indeed incremental
25 
26 TEST_CASE("TigerTree")
27 {
28  static constexpr auto BLOCK_SIZE = TigerTree::BLOCK_SIZE;
29  uint8_t buffer_[8 * BLOCK_SIZE + 1];
30  uint8_t* buffer = buffer_ + 1;
31  TTTestData data;
32  data.buffer = buffer;
33 
34  std::string dummyName;
35  time_t dummyTime = 0;
36  auto dummyCallback = [](size_t, size_t) {};
37 
38  SECTION("zero sized buffer") {
39  TigerTree tt(data, 0, dummyName);
40 
41  CHECK(tt.calcHash(dummyCallback).toString() ==
42  "LWPNACQDBZRYXW3VHJVCJ64QBZNGHOHHHZWCLNQ");
43  }
44  SECTION("size less than 1 block") {
45  TigerTree tt(data, 100, dummyName);
46  memset(buffer, 0, 100);
47  CHECK(tt.calcHash(dummyCallback).toString() ==
48  "EOIEKIQO6BSNCNRX2UB2MB466INV6LICZ6MPUWQ");
49  memset(buffer + 20, 1, 10);
50  tt.notifyChange(20, 10, dummyTime);
51  CHECK(tt.calcHash(dummyCallback).toString() ==
52  "GOTZVYW3WIE37XFCDOY66PLLXWGP6DPN3CQRHWA");
53  }
54  SECTION("3 full and 1 partial block") {
55  TigerTree tt(data, 3 * BLOCK_SIZE + 500, dummyName);
56  memset(buffer, 0, 3 * BLOCK_SIZE + 500);
57  CHECK(tt.calcHash(dummyCallback).toString() ==
58  "K6NHCUINLFZ7OUMUZ44JSRABL5C62WTCY2BONUI");
59 
60  memset(buffer + BLOCK_SIZE + 500, 1, 10);
61  tt.notifyChange(BLOCK_SIZE + 500, 10, dummyTime); // change a single block
62  CHECK(tt.calcHash(dummyCallback).toString() ==
63  "WGRG4PY3CZDLYLTC6BZ2X3G22H6DEB77JH4XPBA");
64 
65  memset(buffer + 2 * BLOCK_SIZE - 50, 1, 100);
66  tt.notifyChange(2 * BLOCK_SIZE - 50, 100, dummyTime); // change 2 blocks
67  CHECK(tt.calcHash(dummyCallback).toString() ==
68  "Y55VG6WSVEPWGPRZGCB4L2OYLI7CLVGQ6X6J4MY");
69 
70  }
71  SECTION("7 full blocks (unbalanced internal binary tree)") {
72  TigerTree tt(data, 7 * BLOCK_SIZE, dummyName);
73  memset(buffer, 0, 7 * BLOCK_SIZE);
74  CHECK(tt.calcHash(dummyCallback).toString() ==
75  "FPSZ35773WS4WGBVXM255KWNETQZXMTEJGFMLTA");
76 
77  memset(buffer + 500, 1, 500);
78  tt.notifyChange(500, 500, dummyTime); // part of block-0
79  CHECK(tt.calcHash(dummyCallback).toString() ==
80  "NBCRBTHDNUDTAKZRMYO6TIQGQJIWX74BYNTYXBA");
81 
82 
83  memset(buffer + 3 * BLOCK_SIZE, 1, 4 * BLOCK_SIZE);
84  tt.notifyChange(3 * BLOCK_SIZE, 4 * BLOCK_SIZE, dummyTime); // blocks 3-6
85  CHECK(tt.calcHash(dummyCallback).toString() ==
86  "PLHCYOTPV4TTXTUPHYGGVPMARGMFE4U5JYRV4VA");
87  }
88 }
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:52
void notifyChange(size_t offset, size_t len, time_t time)
Inform this calculator about changes in the input data.
Definition: TigerTree.cc:57
static constexpr size_t BLOCK_SIZE
Definition: TigerTree.hh:75
CHECK(m3==m3)
This file implemented 3 utility functions:
Definition: Autofire.cc:9
bool isCacheStillValid(time_t &) override
Because TTH calculation of a large file takes some time (a few 1/10s for a harddisk 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:10