forked from XRPLF/rippled
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidatorSite_test.cpp
419 lines (352 loc) · 13.1 KB
/
ValidatorSite_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright 2016 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <beast/core/detail/base64.hpp>
#include <beast/http.hpp>
#include <ripple/app/misc/ValidatorSite.h>
#include <ripple/basics/Slice.h>
#include <ripple/basics/strHex.h>
#include <ripple/protocol/digest.h>
#include <ripple/protocol/HashPrefix.h>
#include <ripple/protocol/PublicKey.h>
#include <ripple/protocol/SecretKey.h>
#include <ripple/protocol/Sign.h>
#include <test/jtx.h>
#include <boost/utility/in_place_factory.hpp>
#include <boost/asio.hpp>
namespace ripple {
namespace test {
struct Validator
{
PublicKey masterPublic;
PublicKey signingPublic;
std::string manifest;
};
class http_sync_server
{
using endpoint_type = boost::asio::ip::tcp::endpoint;
using address_type = boost::asio::ip::address;
using socket_type = boost::asio::ip::tcp::socket;
using req_type = beast::http::request<beast::http::string_body>;
using resp_type = beast::http::response<beast::http::string_body>;
using error_code = boost::system::error_code;
socket_type sock_;
boost::asio::ip::tcp::acceptor acceptor_;
std::string list_;
public:
http_sync_server(endpoint_type const& ep,
boost::asio::io_service& ios,
std::pair<PublicKey, SecretKey> keys,
std::string const& manifest,
int sequence,
std::size_t expiration,
int version,
std::vector <Validator> const& validators)
: sock_(ios)
, acceptor_(ios)
{
std::string data =
"{\"sequence\":" + std::to_string(sequence) +
",\"expiration\":" + std::to_string(expiration) +
",\"validators\":[";
for (auto const& val : validators)
{
data += "{\"validation_public_key\":\"" + strHex(val.masterPublic) +
"\",\"manifest\":\"" + val.manifest + "\"},";
}
data.pop_back();
data += "]}";
std::string blob = beast::detail::base64_encode(data);
list_ = "{\"blob\":\"" + blob + "\"";
auto const sig = sign(keys.first, keys.second, makeSlice(data));
list_ += ",\"signature\":\"" + strHex(sig) + "\"";
list_ += ",\"manifest\":\"" + manifest + "\"";
list_ += ",\"version\":" + std::to_string(version) + '}';
acceptor_.open(ep.protocol());
error_code ec;
acceptor_.set_option(
boost::asio::ip::tcp::acceptor::reuse_address(true), ec);
acceptor_.bind(ep);
acceptor_.listen(boost::asio::socket_base::max_connections);
acceptor_.async_accept(sock_,
std::bind(&http_sync_server::on_accept, this,
std::placeholders::_1));
}
~http_sync_server()
{
error_code ec;
acceptor_.close(ec);
}
private:
struct lambda
{
int id;
http_sync_server& self;
socket_type sock;
boost::asio::io_service::work work;
lambda(int id_, http_sync_server& self_,
socket_type&& sock_)
: id(id_)
, self(self_)
, sock(std::move(sock_))
, work(sock.get_io_service())
{
}
void operator()()
{
self.do_peer(id, std::move(sock));
}
};
void
on_accept(error_code ec)
{
// ec must be checked before `acceptor_` or the member variable may be
// accessed after the destructor has completed
if(ec || !acceptor_.is_open())
return;
static int id_ = 0;
std::thread{lambda{++id_, *this, std::move(sock_)}}.detach();
acceptor_.async_accept(sock_,
std::bind(&http_sync_server::on_accept, this,
std::placeholders::_1));
}
void
do_peer(int id, socket_type&& sock0)
{
socket_type sock(std::move(sock0));
beast::multi_buffer sb;
error_code ec;
for(;;)
{
req_type req;
beast::http::read(sock, sb, req, ec);
if(ec)
break;
auto path = req.target().to_string();
if(path != "/validators")
{
resp_type res;
res.result(beast::http::status::not_found);
res.version = req.version;
res.insert("Server", "http_sync_server");
res.insert("Content-Type", "text/html");
res.body = "The file '" + path + "' was not found";
res.prepare_payload();
write(sock, res, ec);
if(ec)
break;
}
resp_type res;
res.result(beast::http::status::ok);
res.version = req.version;
res.insert("Server", "http_sync_server");
res.insert("Content-Type", "application/json");
res.body = list_;
try
{
res.prepare_payload();
}
catch(std::exception const& e)
{
res = {};
res.result(beast::http::status::internal_server_error);
res.version = req.version;
res.insert("Server", "http_sync_server");
res.insert("Content-Type", "text/html");
res.body =
std::string{"An internal error occurred"} + e.what();
res.prepare_payload();
}
write(sock, res, ec);
if(ec)
break;
}
}
};
class ValidatorSite_test : public beast::unit_test::suite
{
private:
static
PublicKey
randomNode ()
{
return derivePublicKey (KeyType::secp256k1, randomSecretKey());
}
static
std::string
makeManifestString (
PublicKey const& pk,
SecretKey const& sk,
PublicKey const& spk,
SecretKey const& ssk,
int seq)
{
STObject st(sfGeneric);
st[sfSequence] = seq;
st[sfPublicKey] = pk;
st[sfSigningPubKey] = spk;
sign(st, HashPrefix::manifest, *publicKeyType(spk), ssk);
sign(st, HashPrefix::manifest, *publicKeyType(pk), sk,
sfMasterSignature);
Serializer s;
st.add(s);
return beast::detail::base64_encode (std::string(
static_cast<char const*> (s.data()), s.size()));
}
static
Validator
randomValidator ()
{
auto const secret = randomSecretKey();
auto const masterPublic =
derivePublicKey(KeyType::ed25519, secret);
auto const signingKeys = randomKeyPair(KeyType::secp256k1);
return { masterPublic, signingKeys.first, makeManifestString (
masterPublic, secret, signingKeys.first, signingKeys.second, 1) };
}
void
testConfigLoad ()
{
testcase ("Config Load");
using namespace jtx;
Env env (*this);
auto trustedSites = std::make_unique<ValidatorSite> (
env.app().getIOService(), env.app().validators(), beast::Journal());
// load should accept empty sites list
std::vector<std::string> emptyCfgSites;
BEAST_EXPECT(trustedSites->load (emptyCfgSites));
// load should accept valid validator site uris
std::vector<std::string> cfgSites({
"http://ripple.com/",
"http://ripple.com/validators",
"http://ripple.com:8080/validators",
"http://207.261.33.37/validators",
"http://207.261.33.37:8080/validators",
"https://ripple.com/validators",
"https://ripple.com:443/validators"});
BEAST_EXPECT(trustedSites->load (cfgSites));
// load should reject validator site uris with invalid schemes
std::vector<std::string> badSites(
{"ftp://ripple.com/validators"});
BEAST_EXPECT(!trustedSites->load (badSites));
badSites[0] = "wss://ripple.com/validators";
BEAST_EXPECT(!trustedSites->load (badSites));
badSites[0] = "ripple.com/validators";
BEAST_EXPECT(!trustedSites->load (badSites));
}
void
testFetchList ()
{
testcase ("Fetch list");
using namespace jtx;
Env env (*this);
auto& ioService = env.app ().getIOService ();
auto& trustedKeys = env.app ().validators ();
beast::Journal journal;
PublicKey emptyLocalKey;
std::vector<std::string> emptyCfgKeys;
auto const publisherSecret1 = randomSecretKey();
auto const publisherPublic1 =
derivePublicKey(KeyType::ed25519, publisherSecret1);
auto const pubSigningKeys1 = randomKeyPair(KeyType::secp256k1);
auto const manifest1 = makeManifestString (
publisherPublic1, publisherSecret1,
pubSigningKeys1.first, pubSigningKeys1.second, 1);
auto const publisherSecret2 = randomSecretKey();
auto const publisherPublic2 =
derivePublicKey(KeyType::ed25519, publisherSecret2);
auto const pubSigningKeys2 = randomKeyPair(KeyType::secp256k1);
auto const manifest2 = makeManifestString (
publisherPublic2, publisherSecret2,
pubSigningKeys2.first, pubSigningKeys2.second, 1);
std::vector<std::string> cfgPublishers({
strHex(publisherPublic1),
strHex(publisherPublic2)});
BEAST_EXPECT(trustedKeys.load (
emptyLocalKey, emptyCfgKeys, cfgPublishers));
auto constexpr listSize = 20;
std::vector<Validator> list1;
list1.reserve (listSize);
while (list1.size () < listSize)
list1.push_back (randomValidator());
std::vector<Validator> list2;
list2.reserve (listSize);
while (list2.size () < listSize)
list2.push_back (randomValidator());
std::uint16_t constexpr port1 = 7475;
std::uint16_t constexpr port2 = 7476;
using endpoint_type = boost::asio::ip::tcp::endpoint;
using address_type = boost::asio::ip::address;
endpoint_type ep1{address_type::from_string("127.0.0.1"), port1};
endpoint_type ep2{address_type::from_string("127.0.0.1"), port2};
auto const sequence = 1;
auto const version = 1;
NetClock::time_point const expiration =
env.timeKeeper().now() + 3600s;
http_sync_server server1(
ep1, ioService, pubSigningKeys1, manifest1, sequence,
expiration.time_since_epoch().count(), version, list1);
http_sync_server server2(
ep2, ioService, pubSigningKeys2, manifest2, sequence,
expiration.time_since_epoch().count(), version, list2);
{
// fetch single site
std::vector<std::string> cfgSites(
{"http://127.0.0.1:" + std::to_string(port1) + "/validators"});
auto sites = std::make_unique<ValidatorSite> (
env.app().getIOService(), env.app().validators(), journal);
sites->load (cfgSites);
sites->start();
sites->join();
for (auto const& val : list1)
{
BEAST_EXPECT(trustedKeys.listed (val.masterPublic));
BEAST_EXPECT(trustedKeys.listed (val.signingPublic));
}
}
{
// fetch multiple sites
std::vector<std::string> cfgSites({
"http://127.0.0.1:" + std::to_string(port1) + "/validators",
"http://127.0.0.1:" + std::to_string(port2) + "/validators"});
auto sites = std::make_unique<ValidatorSite> (
env.app().getIOService(), env.app().validators(), journal);
sites->load (cfgSites);
sites->start();
sites->join();
for (auto const& val : list1)
{
BEAST_EXPECT(trustedKeys.listed (val.masterPublic));
BEAST_EXPECT(trustedKeys.listed (val.signingPublic));
}
for (auto const& val : list2)
{
BEAST_EXPECT(trustedKeys.listed (val.masterPublic));
BEAST_EXPECT(trustedKeys.listed (val.signingPublic));
}
}
}
public:
void
run() override
{
testConfigLoad ();
testFetchList ();
}
};
BEAST_DEFINE_TESTSUITE(ValidatorSite, app, ripple);
} // test
} // ripple