Skip to content

Commit 1b71e90

Browse files
committed
Add electrum server_features tests.
1 parent e421ed5 commit 1b71e90

2 files changed

Lines changed: 85 additions & 5 deletions

File tree

src/protocols/electrum/protocol_electrum_server.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void protocol_electrum::handle_server_features(const code& ec,
108108
{
109109
{ "genesis_hash", encode_hash(hash) },
110110
{ "hosts", advertised_hosts() },
111-
{ "hash_function", "sha256" },
111+
{ "hash_function", string_t{ "sha256" } },
112112
{ "server_version", options().server_name },
113113
{ "protocol_min", string_t{ version_to_string(minimum) } },
114114
{ "protocol_max", string_t{ version_to_string(maximum) } },
@@ -165,13 +165,18 @@ object_t protocol_electrum::advertised_hosts() const NOEXCEPT
165165
map[safe.host()]["ssl_port"] = safe.port();
166166

167167
object_t hosts{};
168-
for (const auto& [host, object] : map)
168+
for (const auto& [host, object]: map)
169169
hosts[host] = object;
170170

171171
if (hosts.empty()) return
172172
{
173-
{ "tcp_port", null_t{} },
174-
{ "ssl_port", null_t{} }
173+
{
174+
"", object_t
175+
{
176+
{ "tcp_port", null_t{} },
177+
{ "ssl_port", null_t{} }
178+
}
179+
}
175180
};
176181

177182
return hosts;

test/protocols/electrum/electrum_server.cpp

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,86 @@ BOOST_AUTO_TEST_CASE(electrum__server_donation_address__jsonrpc_2__expected)
9696
}
9797

9898
// server.features
99+
100+
BOOST_AUTO_TEST_CASE(electrum__server_features__no_params__expected)
101+
{
102+
BOOST_REQUIRE(handshake(electrum::version::v1_2));
103+
104+
const auto response = get(R"({"id":301,"method":"server.features"})" "\n");
105+
REQUIRE_NO_THROW_TRUE(response.at("dropped").as_bool());
106+
}
107+
108+
BOOST_AUTO_TEST_CASE(electrum__server_features__extra_param__dropped)
109+
{
110+
BOOST_REQUIRE(handshake(electrum::version::v1_2));
111+
112+
const auto response = get(R"({"id":302,"method":"server.features","params":["extra"]})" "\n");
113+
REQUIRE_NO_THROW_TRUE(response.at("dropped").as_bool());
114+
}
115+
116+
BOOST_AUTO_TEST_CASE(electrum__server_features__default_hosts__expected)
117+
{
118+
BOOST_REQUIRE(handshake(electrum::version::v1_2));
119+
120+
const auto response = get(R"({"id":300,"method":"server.features","params":[]})" "\n");
121+
REQUIRE_NO_THROW_TRUE(response.at("result").is_object());
122+
123+
const auto& result = response.at("result").as_object();
124+
REQUIRE_NO_THROW_TRUE(result.at("genesis_hash").is_string());
125+
REQUIRE_NO_THROW_TRUE(result.at("hash_function").is_string());
126+
REQUIRE_NO_THROW_TRUE(result.at("server_version").is_string());
127+
REQUIRE_NO_THROW_TRUE(result.at("protocol_min").is_string());
128+
REQUIRE_NO_THROW_TRUE(result.at("protocol_max").is_string());
129+
REQUIRE_NO_THROW_TRUE(result.at("pruning").is_null());
130+
REQUIRE_NO_THROW_TRUE(result.at("hosts").is_object());
131+
132+
using namespace electrum;
133+
const auto min = version_to_string(protocol_electrum_version::minimum);
134+
const auto max = version_to_string(protocol_electrum_version::maximum);
135+
const auto server_name = config_.server.electrum.server_name;
136+
const auto genesis_hash = encode_hash(genesis.hash());
137+
BOOST_REQUIRE_EQUAL(result.at("genesis_hash").as_string(), genesis_hash);
138+
BOOST_REQUIRE_EQUAL(result.at("hash_function").as_string(), "sha256");
139+
BOOST_REQUIRE_EQUAL(result.at("server_version").as_string(), server_name);
140+
BOOST_REQUIRE_EQUAL(result.at("protocol_min").as_string(), min);
141+
BOOST_REQUIRE_EQUAL(result.at("protocol_max").as_string(), max);
142+
143+
const auto& hosts = result.at("hosts").as_object();
144+
BOOST_REQUIRE_EQUAL(hosts.size(), 1u);
145+
REQUIRE_NO_THROW_TRUE(hosts.at("").is_object());
146+
147+
const auto& host = hosts.at("").as_object();
148+
REQUIRE_NO_THROW_TRUE(host.at("tcp_port").is_null());
149+
REQUIRE_NO_THROW_TRUE(host.at("ssl_port").is_null());
150+
}
151+
152+
BOOST_AUTO_TEST_CASE(electrum__server_features__hosts__expected)
153+
{
154+
config_.server.electrum.advertise_binds.emplace_back("tcp.com:80");
155+
config_.server.electrum.advertise_safes.emplace_back("ssl.com:443");
156+
BOOST_REQUIRE(handshake(electrum::version::v1_0));
157+
158+
const auto response = get(R"({"id":300,"method":"server.features","params":[]})" "\n");
159+
REQUIRE_NO_THROW_TRUE(response.at("result").is_object());
160+
161+
const auto& result = response.at("result").as_object();
162+
REQUIRE_NO_THROW_TRUE(result.at("hosts").is_object());
163+
164+
const auto& hosts = result.at("hosts").as_object();
165+
REQUIRE_NO_THROW_TRUE(hosts.at("tcp.com").is_object());
166+
REQUIRE_NO_THROW_TRUE(hosts.at("ssl.com").is_object());
167+
168+
const auto& tcp = hosts.at("tcp.com").as_object();
169+
const auto& ssl = hosts.at("ssl.com").as_object();
170+
BOOST_REQUIRE_EQUAL(tcp.at("tcp_port").as_int64(), 80);
171+
BOOST_REQUIRE_EQUAL(ssl.at("ssl_port").as_int64(), 443);
172+
}
173+
99174
// server.peers.subscribe
100175

101176
// server.ping
102177

103-
BOOST_AUTO_TEST_CASE(electrum__server_ping__null)
178+
BOOST_AUTO_TEST_CASE(electrum__server_ping__always__null)
104179
{
105180
BOOST_REQUIRE(handshake(electrum::version::v1_2));
106181

0 commit comments

Comments
 (0)