Debian Codegraph @

tests/unit_tests/bootstrap_node_selector.cpp

raw symbols SCIP
1 // Copyright (c) 2020-2022, The Monero Project
2
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include <gtest/gtest.h>
31
32 #include "rpc/bootstrap_node_selector.h"
33
34 class bootstrap_node_selector : public ::testing::Test
35 {
36 protected:
37 void SetUp() override
38 {
39 nodes.insert(white_nodes.begin(), white_nodes.end());
40 nodes.insert(gray_nodes.begin(), gray_nodes.end());
41 }
42
43 const std::map<std::string, bool> white_nodes = {
44 {
45 "white_node_1:18089", true
46 },
47 {
48 "white_node_2:18081", true
49 }
50 };
51 const std::map<std::string, bool> gray_nodes = {
52 {
53 "gray_node_1:18081", false
54 },
55 {
56 "gray_node_2:18089", false
57 }
58 };
59
60 std::map<std::string, bool> nodes;
61 };
62
63 TEST_F(bootstrap_node_selector, selector_auto_empty)
64 {
65 cryptonote::bootstrap_node::selector_auto selector([]() {
66 return std::map<std::string, bool>();
67 });
68
69 EXPECT_FALSE(selector.next_node());
70 }
71
72 TEST_F(bootstrap_node_selector, selector_auto_no_credentials)
73 {
74 cryptonote::bootstrap_node::selector_auto selector([this]() {
75 return nodes;
76 });
77
78 for (size_t fails = 0; fails < nodes.size(); ++fails)
79 {
80 const auto current = selector.next_node();
81 EXPECT_FALSE(current->credentials);
82
83 selector.handle_result(current->address, false);
84 }
85 }
86
87 TEST_F(bootstrap_node_selector, selector_auto_success)
88 {
89 cryptonote::bootstrap_node::selector_auto selector([this]() {
90 return nodes;
91 });
92
93 auto current = selector.next_node();
94 for (size_t fails = 0; fails < nodes.size(); ++fails)
95 {
96 selector.handle_result(current->address, true);
97
98 current = selector.next_node();
99 EXPECT_TRUE(white_nodes.count(current->address) > 0);
100 }
101 }
102
103 TEST_F(bootstrap_node_selector, selector_auto_failure)
104 {
105 cryptonote::bootstrap_node::selector_auto selector([this]() {
106 return nodes;
107 });
108
109 auto current = selector.next_node();
110 for (size_t fails = 0; fails < nodes.size(); ++fails)
111 {
112 const auto previous = current;
113
114 selector.handle_result(current->address, false);
115
116 current = selector.next_node();
117 EXPECT_NE(current->address, previous->address);
118 }
119 }
120
121 TEST_F(bootstrap_node_selector, selector_auto_white_nodes_first)
122 {
123 cryptonote::bootstrap_node::selector_auto selector([this]() {
124 return nodes;
125 });
126
127 for (size_t iterations = 0; iterations < 2; ++iterations)
128 {
129 for (size_t fails = 0; fails < white_nodes.size(); ++fails)
130 {
131 const auto current = selector.next_node();
132 EXPECT_TRUE(white_nodes.count(current->address) > 0);
133
134 selector.handle_result(current->address, false);
135 }
136
137 for (size_t fails = 0; fails < gray_nodes.size(); ++fails)
138 {
139 const auto current = selector.next_node();
140 EXPECT_TRUE(gray_nodes.count(current->address) > 0);
141
142 selector.handle_result(current->address, false);
143 }
144 }
145 }
146
147 TEST_F(bootstrap_node_selector, selector_auto_max_nodes)
148 {
149 const size_t max_nodes = nodes.size() / 2;
150
151 bool populated_once = false;
152 cryptonote::bootstrap_node::selector_auto selector([this, &populated_once]() {
153 if (!populated_once)
154 {
155 populated_once = true;
156 return nodes;
157 }
158
159 return std::map<std::string, bool>();
160 }, max_nodes);
161
162 std::set<std::string> unique_nodes;
163
164 for (size_t fails = 0; fails < nodes.size(); ++fails)
165 {
166 const auto current = selector.next_node();
167 unique_nodes.insert(current->address);
168
169 selector.handle_result(current->address, false);
170 }
171
172 EXPECT_EQ(unique_nodes.size(), max_nodes);
173 }