Debian Codegraph @

tests/unit_tests/hashchain.cpp

raw
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 // FIXME: move this into a full wallet2 unit test suite, if possible
30
31 #include "gtest/gtest.h"
32
33 #include "wallet/wallet2.h"
34
35 static crypto::hash make_hash(uint64_t n)
36 {
37 union
38 {
39 crypto::hash hash;
40 uint64_t n;
41 } hash;
42 hash.hash = crypto::null_hash;
43 hash.n = n;
44 return hash.hash;
45 }
46
47 TEST(hashchain, empty)
48 {
49 tools::hashchain hashchain;
50 ASSERT_EQ(hashchain.size(), 0);
51 ASSERT_EQ(hashchain.offset(), 0);
52 }
53
54 TEST(hashchain, genesis)
55 {
56 tools::hashchain hashchain;
57 hashchain.push_back(make_hash(1));
58 ASSERT_EQ(hashchain.size(), 1);
59 ASSERT_EQ(hashchain.genesis(), make_hash(1));
60 hashchain.push_back(make_hash(2));
61 ASSERT_EQ(hashchain.size(), 2);
62 ASSERT_EQ(hashchain.genesis(), make_hash(1));
63 }
64
65 TEST(hashchain, push_back)
66 {
67 tools::hashchain hashchain;
68 hashchain.push_back(make_hash(1));
69 hashchain.push_back(make_hash(2));
70 hashchain.push_back(make_hash(3));
71 ASSERT_EQ(hashchain[0], make_hash(1));
72 ASSERT_EQ(hashchain[1], make_hash(2));
73 ASSERT_EQ(hashchain[2], make_hash(3));
74 }
75
76 TEST(hashchain, clear_empty)
77 {
78 tools::hashchain hashchain;
79 ASSERT_TRUE(hashchain.empty());
80 hashchain.push_back(make_hash(1));
81 ASSERT_FALSE(hashchain.empty());
82 hashchain.push_back(make_hash(2));
83 ASSERT_FALSE(hashchain.empty());
84 hashchain.clear();
85 ASSERT_TRUE(hashchain.empty());
86 }
87
88 TEST(hashchain, crop)
89 {
90 tools::hashchain hashchain;
91 hashchain.push_back(make_hash(1));
92 hashchain.push_back(make_hash(2));
93 hashchain.push_back(make_hash(3));
94 ASSERT_EQ(hashchain.size(), 3);
95 ASSERT_EQ(hashchain[0], make_hash(1));
96 ASSERT_EQ(hashchain[1], make_hash(2));
97 ASSERT_EQ(hashchain[2], make_hash(3));
98 hashchain.crop(3);
99 ASSERT_EQ(hashchain.size(), 3);
100 hashchain.crop(2);
101 ASSERT_EQ(hashchain.size(), 2);
102 ASSERT_EQ(hashchain[0], make_hash(1));
103 ASSERT_EQ(hashchain[1], make_hash(2));
104 ASSERT_EQ(hashchain.genesis(), make_hash(1));
105 hashchain.crop(0);
106 ASSERT_TRUE(hashchain.empty());
107 ASSERT_EQ(hashchain.size(), 0);
108 hashchain.push_back(make_hash(5));
109 ASSERT_EQ(hashchain.genesis(), make_hash(5));
110 ASSERT_EQ(hashchain.size(), 1);
111 }
112
113 TEST(hashchain, trim)
114 {
115 tools::hashchain hashchain;
116 hashchain.push_back(make_hash(1));
117 hashchain.push_back(make_hash(2));
118 hashchain.push_back(make_hash(3));
119 ASSERT_EQ(hashchain.offset(), 0);
120 hashchain.trim(2);
121 ASSERT_EQ(hashchain.offset(), 2);
122 ASSERT_EQ(hashchain.size(), 3);
123 ASSERT_EQ(hashchain[2], make_hash(3));
124 hashchain.trim(3);
125 ASSERT_EQ(hashchain.offset(), 2); // never gets it empty
126 ASSERT_EQ(hashchain.size(), 3);
127 ASSERT_FALSE(hashchain.empty());
128 ASSERT_EQ(hashchain.genesis(), make_hash(1));
129 }