openMSX
unittest/sha1.cc
Go to the documentation of this file.
1 #include "catch.hpp"
2 #include "sha1.hh"
3 #include <cstring>
4 #include <sstream>
5 
6 using namespace openmsx;
7 
8 TEST_CASE("Sha1Sum: constructors")
9 {
10  SECTION("default") {
11  Sha1Sum sum;
12  CHECK(sum.empty());
13  CHECK(sum.toString() == "0000000000000000000000000000000000000000");
14  }
15 
16  SECTION("from string, ok") {
17  Sha1Sum sum("1234567890123456789012345678901234567890");
18  CHECK(!sum.empty());
19  CHECK(sum.toString() == "1234567890123456789012345678901234567890");
20  }
21  SECTION("from string, too short") {
22  CHECK_THROWS(Sha1Sum("123456789012345678901234567890123456789"));
23  }
24  SECTION("from string, too long") {
25  CHECK_THROWS(Sha1Sum("12345678901234567890123456789012345678901"));
26  }
27  SECTION("from string, invalid char") {
28  CHECK_THROWS(Sha1Sum("g234567890123456789012345678901234567890"));
29  }
30 }
31 
32 TEST_CASE("Sha1Sum: parse")
33 {
34  Sha1Sum sum;
35 
36  // precondition: string must be 40 chars long
37  SECTION("ok") {
38  sum.parse40("abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd");
39  CHECK(sum.toString() == "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd");
40  }
41  SECTION("invalid char") {
42  CHECK_THROWS(sum.parse40("abcdabcdabcdabcdabcdabcdabcdabcd-bcdabcd"));
43  }
44 }
45 
46 TEST_CASE("Sha1Sum: clear")
47 {
48  Sha1Sum sum("1111111111111111111111111111111111111111");
49  REQUIRE(!sum.empty());
50  REQUIRE(sum.toString() != "0000000000000000000000000000000000000000");
51 
52  sum.clear();
53  CHECK(sum.empty());
54  CHECK(sum.toString() == "0000000000000000000000000000000000000000");
55 }
56 
57 static void testCompare(const Sha1Sum& x, const Sha1Sum& y, bool expectEqual, bool expectLess)
58 {
59  if (expectEqual) {
60  REQUIRE(!expectLess);
61  CHECK( x == y ); CHECK( y == x );
62  CHECK(!(x != y)); CHECK(!(y != x));
63 
64  CHECK(!(x < y)); CHECK(!(y < x));
65  CHECK( x <= y ); CHECK( y <= x );
66  CHECK(!(x > y)); CHECK(!(y > x));
67  CHECK( x >= y ); CHECK( y >= x );
68  } else {
69  CHECK(!(x == y)); CHECK(!(y == x));
70  CHECK( x != y ); CHECK( y != x );
71  if (expectLess) {
72  CHECK( x < y ); CHECK(!(y < x));
73  CHECK( x <= y ); CHECK(!(y <= x));
74  CHECK(!(x > y)); CHECK( y > x );
75  CHECK(!(x >= y)); CHECK( y >= x );
76  } else {
77  CHECK(!(x < y)); CHECK( y < x );
78  CHECK(!(x <= y)); CHECK( y <= x );
79  CHECK( x > y ); CHECK(!(y > x));
80  CHECK( x >= y ); CHECK(!(y >= x));
81  }
82  }
83 }
84 
85 TEST_CASE("Sha1Sum: comparisons")
86 {
87  Sha1Sum sumA ("0000000000000000000000000000000000000000");
88  Sha1Sum sumB ("0000000000000000000000000000000000000001");
89  Sha1Sum sumB2("0000000000000000000000000000000000000001");
90  Sha1Sum sumC ("0000000000100000000000000000000000000001");
91 
92  testCompare(sumB, sumB2, true, false);
93  testCompare(sumA, sumB, false, true);
94  testCompare(sumC, sumB, false, false);
95 }
96 
97 TEST_CASE("Sha1Sum: stream")
98 {
99  Sha1Sum sum("abcdef0123ABCDEF0123abcdef0123ABCDEF0123");
100  std::stringstream ss;
101  ss << sum;
102  CHECK(ss.str() == "abcdef0123abcdef0123abcdef0123abcdef0123");
103 }
104 
105 
106 TEST_CASE("sha1: calc")
107 {
108  const char* in = "abc";
109  Sha1Sum output = SHA1::calc(reinterpret_cast<const uint8_t*>(in), strlen(in));
110  CHECK(output.toString() == "a9993e364706816aba3e25717850c26c9cd0d89d");
111 }
112 
113 TEST_CASE("sha1: update,digest")
114 {
115  SHA1 sha1;
116  SECTION("single block") {
117  const char* in = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
118  sha1.update(reinterpret_cast<const uint8_t*>(in), strlen(in));
119  Sha1Sum sum1 = sha1.digest();
120  Sha1Sum sum2 = sha1.digest(); // call 2nd time is ok
121  CHECK(sum1 == sum2);
122  CHECK(sum1.toString() == "84983e441c3bd26ebaae4aa1f95129e5e54670f1");
123  }
124  SECTION("multiple blocks") {
125  const char* in = "aaaaaaaaaaaaaaaaaaaaaaaaa";
126  REQUIRE(strlen(in) == 25);
127  for (int i = 0; i < 40000; ++i) {
128  sha1.update(reinterpret_cast<const uint8_t*>(in), strlen(in));
129  }
130  // 25 * 40'000 = 1'000'000 repetitions of "a"
131  Sha1Sum sum = sha1.digest();
132  CHECK(sum.toString() == "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
133  }
134 }
void update(const uint8_t *data, size_t len)
Incrementally calculate the hash value.
Definition: utils/sha1.cc:318
auto sum(InputRange &&range)
Definition: stl.hh:288
static Sha1Sum calc(const uint8_t *data, size_t len)
Easier to use interface, if you can pass all data in one go.
Definition: utils/sha1.cc:361
bool empty() const
Definition: utils/sha1.cc:244
This class represents the result of a sha1 calculation (a 160-bit value).
Definition: sha1.hh:19
Sha1Sum digest()
Get the final hash.
Definition: utils/sha1.cc:355
CHECK(m3==m3)
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
Helper class to perform a sha1 calculation.
Definition: sha1.hh:78
std::string toString() const
Definition: utils/sha1.cc:232
void parse40(const char *str)
Parse from a 40-character long buffer.
Definition: utils/sha1.cc:141
constexpr KeyMatrixPosition x
Keyboard bindings.
Definition: Keyboard.cc:1377
TEST_CASE("Sha1Sum: constructors")
Definition: unittest/sha1.cc:8