Skip to content

Commit b02f62b

Browse files
nahuhhj-berman
andauthored
jberman - tests/unit_tests/subaddress.cpp
Co-authored-by: Justin Berman <[email protected]>
1 parent 615c7fe commit b02f62b

File tree

1 file changed

+61
-99
lines changed

1 file changed

+61
-99
lines changed

tests/unit_tests/subaddress.cpp

Lines changed: 61 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,36 @@ TEST_F(WalletSubaddress, OutOfBoundsIndexes)
103103
}
104104
}
105105

106-
TEST_F(WalletSubaddress, ExpandPubkeyTable)
106+
// Helper function to check max subaddrs allocated
107+
static void check_expected_max(const tools::wallet2 &w1, const cryptonote::subaddress_index exp_max)
107108
{
108-
// these test assume we are starting with the default setup state
109+
for (uint32_t i = 0; i <= exp_max.minor; ++i)
110+
{
111+
auto subaddr = w1.get_subaddress({exp_max.major, i});
112+
EXPECT_NE(boost::none, w1.get_subaddress_index(subaddr));
113+
}
114+
auto subaddr = w1.get_subaddress({exp_max.major, exp_max.minor + 1});
115+
EXPECT_EQ(boost::none, w1.get_subaddress_index(subaddr));
116+
};
117+
118+
static void expect_default_wallet_state(const tools::wallet2 &w1)
119+
{
120+
// these tests assume we are starting with the default setup state
109121
EXPECT_EQ(2, w1.get_num_subaddress_accounts());
110122
EXPECT_EQ(50, w1.get_subaddress_lookahead().first);
111123
EXPECT_EQ(200, w1.get_subaddress_lookahead().second);
124+
125+
// We assume we start with subaddrs for minor indexes 0 to 199
126+
check_expected_max(w1, {0,199});
127+
check_expected_max(w1, {1,199});
128+
check_expected_max(w1, {49,199});
129+
check_expected_max(w1, {50,199}); // 50 because the test starts with accounts 0 and 1 already allocated
130+
EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({51,0})));
131+
}
132+
133+
TEST_F(WalletSubaddress, SetLookahead)
134+
{
135+
expect_default_wallet_state(w1);
112136
// get_subaddress_index looks up keys in the private m_subaddresses dictionary so we will use it to test if a key is properly being scanned for
113137
cryptonote::subaddress_index test_idx = {50, 199};
114138
auto subaddr = w1.get_subaddress(test_idx);
@@ -117,138 +141,76 @@ TEST_F(WalletSubaddress, ExpandPubkeyTable)
117141
w1.set_subaddress_lookahead(100, 200);
118142
EXPECT_EQ(100, w1.get_subaddress_lookahead().first);
119143
EXPECT_EQ(200, w1.get_subaddress_lookahead().second);
120-
test_idx = {100, 199};
121-
subaddr = w1.get_subaddress(test_idx);
122-
EXPECT_NE(boost::none, w1.get_subaddress_index(subaddr));
144+
check_expected_max(w1, {100, 199});
123145
// next test expanding the minor lookahead
124146
w1.set_subaddress_lookahead(100, 300);
125147
EXPECT_EQ(100, w1.get_subaddress_lookahead().first);
126148
EXPECT_EQ(300, w1.get_subaddress_lookahead().second);
127-
test_idx = {100, 299};
128-
subaddr = w1.get_subaddress(test_idx);
129-
EXPECT_NE(boost::none, w1.get_subaddress_index(subaddr));
149+
check_expected_max(w1, {100, 299});
130150
}
131151

132152
TEST_F(WalletSubaddress, ExpandThenSetMinorIncreaseOnly)
133153
{
134-
// We assume we are starting with the default setup state
135-
EXPECT_EQ(50, w1.get_subaddress_lookahead().first);
136-
EXPECT_EQ(200, w1.get_subaddress_lookahead().second);
137-
138-
// Helper function to check max subaddrs allocated
139-
const auto check_expected_max = [&](const cryptonote::subaddress_index exp_max)
140-
{
141-
for (uint32_t i = 0; i <= exp_max.minor; ++i)
142-
{
143-
auto subaddr = w1.get_subaddress({exp_max.major, i});
144-
EXPECT_NE(boost::none, w1.get_subaddress_index(subaddr));
145-
}
146-
auto subaddr = w1.get_subaddress({exp_max.major, exp_max.minor + 1});
147-
EXPECT_EQ(boost::none, w1.get_subaddress_index(subaddr));
148-
};
149-
150-
// We assume we start with subaddrs for minor indexes 0 to 199
151-
check_expected_max({0,199});
152-
check_expected_max({1,199});
153-
check_expected_max({49,199});
154-
EXPECT_EQ(w1.get_num_subaddresses(50), 0);
154+
expect_default_wallet_state(w1);
155155

156156
// Mock receive to {0,150}, so expand from there
157157
w1.expand_subaddresses({0,150});
158158
// We should now have subaddresses for minor indexes 0 to 349
159-
check_expected_max({0,349});
160-
check_expected_max({1,199});
161-
check_expected_max({49,199});
162-
EXPECT_EQ(w1.get_num_subaddresses(50), 0);
159+
check_expected_max(w1, {0,349});
160+
check_expected_max(w1, {1,199});
161+
check_expected_max(w1, {49,199});
162+
check_expected_max(w1, {50,199});
163+
EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({51,0})));
163164

164165
// Now set the minor lookahead 100 higher
165166
w1.set_subaddress_lookahead(50, 200+100);
166167
// We should have subaddresses for minor indexes 0 to 449
167-
check_expected_max({0,449});
168-
check_expected_max({1,299});
169-
check_expected_max({49,299});
170-
EXPECT_EQ(w1.get_num_subaddresses(50), 0);
168+
check_expected_max(w1, {0,449});
169+
check_expected_max(w1, {1,299});
170+
check_expected_max(w1, {49,299});
171+
check_expected_max(w1, {50,299});
172+
EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({51,0})));
171173
}
172174

173175
TEST_F(WalletSubaddress, ExpandThenSetMajorIncreaseOnly)
174176
{
175-
// We assume we are starting with the default setup state
176-
EXPECT_EQ(50, w1.get_subaddress_lookahead().first);
177-
EXPECT_EQ(200, w1.get_subaddress_lookahead().second);
178-
179-
// Helper function to check max subaddrs allocated
180-
const auto check_expected_max = [&](const cryptonote::subaddress_index exp_max)
181-
{
182-
for (uint32_t i = 0; i <= exp_max.minor; ++i)
183-
{
184-
auto subaddr = w1.get_subaddress({exp_max.major, i});
185-
EXPECT_NE(boost::none, w1.get_subaddress_index(subaddr));
186-
}
187-
auto subaddr = w1.get_subaddress({exp_max.major, exp_max.minor + 1});
188-
EXPECT_EQ(boost::none, w1.get_subaddress_index(subaddr));
189-
};
190-
191-
// We assume we start with subaddrs for minor indexes 0 to 199
192-
check_expected_max({0,199});
193-
check_expected_max({1,199});
194-
check_expected_max({49,199});
195-
EXPECT_EQ(w1.get_num_subaddresses(50), 0);
177+
expect_default_wallet_state(w1);
196178

197179
// Mock receive to {40,0}, so expand from there
198180
w1.expand_subaddresses({40,0});
199-
check_expected_max({0,199});
200-
check_expected_max({1,199});
201-
check_expected_max({40,199});
202-
check_expected_max({89,199});
203-
EXPECT_EQ(w1.get_num_subaddresses(90), 0);
181+
check_expected_max(w1, {0,199});
182+
check_expected_max(w1, {1,199});
183+
check_expected_max(w1, {40,199});
184+
check_expected_max(w1, {89,199});
185+
EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({90,0})));
204186

205187
// Now set the major lookahead 10 higher
206188
w1.set_subaddress_lookahead(50+10, 200);
207-
check_expected_max({0,199});
208-
check_expected_max({1,199});
209-
check_expected_max({40,199});
210-
check_expected_max({99,199});
211-
EXPECT_EQ(w1.get_num_subaddresses(100), 0);
189+
check_expected_max(w1, {0,199});
190+
check_expected_max(w1, {1,199});
191+
check_expected_max(w1, {40,199});
192+
check_expected_max(w1, {99,199});
193+
EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({100,0})));
212194
}
213195

214196
TEST_F(WalletSubaddress, ExpandThenSetIncreaseBoth)
215197
{
216-
// We assume we are starting with the default setup state
217-
EXPECT_EQ(50, w1.get_subaddress_lookahead().first);
218-
EXPECT_EQ(200, w1.get_subaddress_lookahead().second);
219-
220-
// Helper function to check max subaddrs allocated
221-
const auto check_expected_max = [&](const cryptonote::subaddress_index exp_max)
222-
{
223-
for (uint32_t i = 0; i <= exp_max.minor; ++i)
224-
{
225-
auto subaddr = w1.get_subaddress({exp_max.major, i});
226-
EXPECT_NE(boost::none, w1.get_subaddress_index(subaddr));
227-
}
228-
auto subaddr = w1.get_subaddress({exp_max.major, exp_max.minor + 1});
229-
EXPECT_EQ(boost::none, w1.get_subaddress_index(subaddr));
230-
};
231-
232-
// We assume we start with subaddrs for minor indexes 0 to 199
233-
check_expected_max({0,199});
234-
check_expected_max({1,199});
235-
check_expected_max({49,199});
236-
EXPECT_EQ(w1.get_num_subaddresses(50), 0);
198+
expect_default_wallet_state(w1);
237199

238200
// Mock receive to {40,150}, so expand from there
239201
w1.expand_subaddresses({40,150});
240-
check_expected_max({0,199});
241-
check_expected_max({1,199});
242-
check_expected_max({40,349});
243-
check_expected_max({89,199});
244-
EXPECT_EQ(w1.get_num_subaddresses(90), 0);
202+
check_expected_max(w1, {0,199});
203+
check_expected_max(w1, {1,199});
204+
check_expected_max(w1, {40,349});
205+
check_expected_max(w1, {89,199});
206+
EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({90,0})));
245207

246208
// Now set the major lookahead 10 higher and minor 100 higher
247209
w1.set_subaddress_lookahead(50+10, 200+100);
248-
check_expected_max({0,299});
249-
check_expected_max({1,299});
250-
check_expected_max({40,449});
251-
check_expected_max({99,299});
252-
EXPECT_EQ(w1.get_num_subaddresses(100), 0);
210+
check_expected_max(w1, {0,299});
211+
check_expected_max(w1, {1,299});
212+
check_expected_max(w1, {40,449});
213+
check_expected_max(w1, {99,299});
214+
EXPECT_EQ(boost::none, w1.get_subaddress_index(w1.get_subaddress({100,0})));
253215
}
254216

0 commit comments

Comments
 (0)