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  uint8_t buffer_[8192 + 1];
29  uint8_t* buffer = buffer_ + 1;
30  TTTestData data;
31  data.buffer = buffer;
32 
33  std::string dummyName;
34  time_t dummyTime = 0;
35  auto dummyCallback = [](size_t, size_t) {};
36 
37  SECTION("zero sized buffer") {
38  TigerTree tt(data, 0, dummyName);
39 
40  CHECK(tt.calcHash(dummyCallback).toString() ==
41  "LWPNACQDBZRYXW3VHJVCJ64QBZNGHOHHHZWCLNQ");
42  }
43  SECTION("size less than 1 block") {
44  TigerTree tt(data, 100, dummyName);
45  memset(buffer, 0, 100);
46  CHECK(tt.calcHash(dummyCallback).toString() ==
47  "EOIEKIQO6BSNCNRX2UB2MB466INV6LICZ6MPUWQ");
48  memset(buffer + 20, 1, 10);
49  tt.notifyChange(20, 10, dummyTime);
50  CHECK(tt.calcHash(dummyCallback).toString() ==
51  "GOTZVYW3WIE37XFCDOY66PLLXWGP6DPN3CQRHWA");
52  }
53  SECTION("3 full and 1 partial block") {
54  TigerTree tt(data, 4000, dummyName);
55  memset(buffer, 0, 4000);
56  CHECK(tt.calcHash(dummyCallback).toString() ==
57  "YC44NFWFCN3QWFRSS6ICGUJDLH7F654RCKVT7VY");
58  memset(buffer + 1500, 1, 10);
59  tt.notifyChange(1500, 10, dummyTime); // change a single block
60  CHECK(tt.calcHash(dummyCallback).toString() ==
61  "JU5RYR446PVZSPMOJML4IQ2FXLDDKE522CEYIBA");
62  memset(buffer + 2000, 1, 100);
63  tt.notifyChange(2000, 100, dummyTime); // change 2 blocks
64  CHECK(tt.calcHash(dummyCallback).toString() ==
65  "IPV53CDVB2I63HXIXVK2OUPNS26YB7V7G2Y7XIA");
66  }
67  SECTION("7 full blocks (unbalanced internal binary tree)") {
68  TigerTree tt(data, 7 * 1024, dummyName);
69  memset(buffer, 0, 7 * 1024);
70  CHECK(tt.calcHash(dummyCallback).toString() ==
71  "FPSZ35773WS4WGBVXM255KWNETQZXMTEJGFMLTA");
72  memset(buffer + 512, 1, 512);
73  tt.notifyChange(512, 512, dummyTime); // part of block-0
74  CHECK(tt.calcHash(dummyCallback).toString() ==
75  "Z32BC2WSHPW5DYUSNSZGLDIFTEIP3DBFJ7MG2MQ");
76  memset(buffer + 3 * 1024, 1, 4 * 1024);
77  tt.notifyChange(3 * 1024, 4 * 1024, dummyTime); // blocks 3-6
78  CHECK(tt.calcHash(dummyCallback).toString() ==
79  "SJUYB3QVIJXNKZMSQZGIMHA7GA2MYU2UECDA26A");
80  }
81 }
openmsx::TigerHash::toString
std::string toString() const
Definition: tiger.cc:9
TTTestData::isCacheStillValid
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 ...
Definition: TigerTree_test.cc:15
openmsx::TTData
The TigerTree class will query the to-be-hashed data via this abstract interface.
Definition: TigerTree.hh:44
openmsx::TigerTree
Calculate a tiger-tree-hash.
Definition: TigerTree.hh:73
openmsx::TigerTree::notifyChange
void notifyChange(size_t offset, size_t len, time_t time)
Inform this calculator about changes in the input data.
Definition: TigerTree.cc:59
TTTestData::getData
uint8_t * getData(size_t offset, size_t) override
Return the requested portion of the to-be-hashed data block.
Definition: TigerTree_test.cc:10
TTTestData::buffer
uint8_t * buffer
Definition: TigerTree_test.cc:20
CHECK
CHECK(m3==m3)
TEST_CASE
TEST_CASE("TigerTree")
Definition: TigerTree_test.cc:26
tiger.hh
openmsx::TigerTree::calcHash
const TigerHash & calcHash(const std::function< void(size_t, size_t)> &progressCallback)
Calculate the hash value.
Definition: TigerTree.cc:54
TTTestData
Definition: TigerTree_test.cc:9
TigerTree.hh
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5