Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
1250 lines (1100 loc) · 44.1 KB

File metadata and controls

1250 lines (1100 loc) · 44.1 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 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 <ripple/app/ledger/LedgerMaster.h>
#include <ripple/app/misc/LoadFeeTrack.h>
#include <ripple/app/misc/NetworkOPs.h>
#include <ripple/basics/base64.h>
#include <ripple/json/json_reader.h>
#include <ripple/rpc/ServerHandler.h>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/beast/core/multi_buffer.hpp>
#include <boost/beast/http.hpp>
#include <algorithm>
#include <array>
#include <beast/test/yield_to.hpp>
#include <random>
#include <regex>
#include <test/jtx.h>
#include <test/jtx/JSONRPCClient.h>
#include <test/jtx/WSClient.h>
#include <test/jtx/envconfig.h>
namespace ripple {
namespace test {
class ServerStatus_test : public beast::unit_test::suite,
public beast::test::enable_yield_to
{
class myFields : public boost::beast::http::fields
{
};
auto
makeConfig(
std::string const& proto,
bool admin = true,
bool credentials = false)
{
auto const section_name =
boost::starts_with(proto, "h") ? "port_rpc" : "port_ws";
auto p = jtx::envconfig();
p->overwrite(section_name, "protocol", proto);
if (!admin)
p->overwrite(section_name, "admin", "");
if (credentials)
{
(*p)[section_name].set("admin_password", "p");
(*p)[section_name].set("admin_user", "u");
}
p->overwrite(
boost::starts_with(proto, "h") ? "port_ws" : "port_rpc",
"protocol",
boost::starts_with(proto, "h") ? "ws" : "http");
if (proto == "https")
{
// this port is here to allow the env to create its internal client,
// which requires an http endpoint to talk to. In the connection
// failure test, this endpoint should never be used
(*p)["server"].append("port_alt");
(*p)["port_alt"].set("ip", getEnvLocalhostAddr());
(*p)["port_alt"].set("port", "8099");
(*p)["port_alt"].set("protocol", "http");
(*p)["port_alt"].set("admin", getEnvLocalhostAddr());
}
return p;
}
auto
makeWSUpgrade(std::string const& host, uint16_t port)
{
using namespace boost::asio;
using namespace boost::beast::http;
request<string_body> req;
req.target("/");
req.version(11);
req.insert("Host", host + ":" + std::to_string(port));
req.insert("User-Agent", "test");
req.method(boost::beast::http::verb::get);
req.insert("Upgrade", "websocket");
{
// not secure, but OK for a testing
std::random_device rd;
std::mt19937 e{rd()};
std::uniform_int_distribution<> d(0, 255);
std::array<std::uint8_t, 16> key;
for (auto& v : key)
v = d(e);
req.insert(
"Sec-WebSocket-Key", base64_encode(key.data(), key.size()));
};
req.insert("Sec-WebSocket-Version", "13");
req.insert(boost::beast::http::field::connection, "upgrade");
return req;
}
auto
makeHTTPRequest(
std::string const& host,
uint16_t port,
std::string const& body,
myFields const& fields)
{
using namespace boost::asio;
using namespace boost::beast::http;
request<string_body> req;
req.target("/");
req.version(11);
for (auto const& f : fields)
req.insert(f.name(), f.value());
req.insert("Host", host + ":" + std::to_string(port));
req.insert("User-Agent", "test");
if (body.empty())
{
req.method(boost::beast::http::verb::get);
}
else
{
req.method(boost::beast::http::verb::post);
req.insert("Content-Type", "application/json; charset=UTF-8");
req.body() = body;
}
req.prepare_payload();
return req;
}
void
doRequest(
boost::asio::yield_context& yield,
boost::beast::http::request<boost::beast::http::string_body>&& req,
std::string const& host,
uint16_t port,
bool secure,
boost::beast::http::response<boost::beast::http::string_body>& resp,
boost::system::error_code& ec)
{
using namespace boost::asio;
using namespace boost::beast::http;
io_service& ios = get_io_service();
ip::tcp::resolver r{ios};
boost::beast::multi_buffer sb;
auto it = r.async_resolve(
ip::tcp::resolver::query{host, std::to_string(port)}, yield[ec]);
if (ec)
return;
resp.body().clear();
if (secure)
{
ssl::context ctx{ssl::context::sslv23};
ctx.set_verify_mode(ssl::verify_none);
ssl::stream<ip::tcp::socket> ss{ios, ctx};
async_connect(ss.next_layer(), it, yield[ec]);
if (ec)
return;
ss.async_handshake(ssl::stream_base::client, yield[ec]);
if (ec)
return;
boost::beast::http::async_write(ss, req, yield[ec]);
if (ec)
return;
async_read(ss, sb, resp, yield[ec]);
if (ec)
return;
}
else
{
ip::tcp::socket sock{ios};
async_connect(sock, it, yield[ec]);
if (ec)
return;
boost::beast::http::async_write(sock, req, yield[ec]);
if (ec)
return;
async_read(sock, sb, resp, yield[ec]);
if (ec)
return;
}
return;
}
void
doWSRequest(
test::jtx::Env& env,
boost::asio::yield_context& yield,
bool secure,
boost::beast::http::response<boost::beast::http::string_body>& resp,
boost::system::error_code& ec)
{
auto const port =
env.app().config()["port_ws"].get<std::uint16_t>("port");
auto ip = env.app().config()["port_ws"].get<std::string>("ip");
doRequest(
yield, makeWSUpgrade(*ip, *port), *ip, *port, secure, resp, ec);
return;
}
void
doHTTPRequest(
test::jtx::Env& env,
boost::asio::yield_context& yield,
bool secure,
boost::beast::http::response<boost::beast::http::string_body>& resp,
boost::system::error_code& ec,
std::string const& body = "",
myFields const& fields = {})
{
auto const port =
env.app().config()["port_rpc"].get<std::uint16_t>("port");
auto const ip = env.app().config()["port_rpc"].get<std::string>("ip");
doRequest(
yield,
makeHTTPRequest(*ip, *port, body, fields),
*ip,
*port,
secure,
resp,
ec);
return;
}
auto
makeAdminRequest(
jtx::Env& env,
std::string const& proto,
std::string const& user,
std::string const& password,
bool subobject = false)
{
Json::Value jrr;
Json::Value jp = Json::objectValue;
if (!user.empty())
{
jp["admin_user"] = user;
if (subobject)
{
// special case of bad password..passed as object
Json::Value jpi = Json::objectValue;
jpi["admin_password"] = password;
jp["admin_password"] = jpi;
}
else
{
jp["admin_password"] = password;
}
}
if (boost::starts_with(proto, "h"))
{
auto jrc = makeJSONRPCClient(env.app().config());
jrr = jrc->invoke("ledger_accept", jp);
}
else
{
auto wsc = makeWSClient(env.app().config(), proto == "ws2");
jrr = wsc->invoke("ledger_accept", jp);
}
return jrr;
}
// ------------
// Test Cases
// ------------
void
testAdminRequest(std::string const& proto, bool admin, bool credentials)
{
testcase << "Admin request over " << proto << ", config "
<< (admin ? "enabled" : "disabled") << ", credentials "
<< (credentials ? "" : "not ") << "set";
using namespace jtx;
Env env{*this, makeConfig(proto, admin, credentials)};
Json::Value jrr;
auto const proto_ws = boost::starts_with(proto, "w");
// the set of checks we do are different depending
// on how the admin config options are set
if (admin && credentials)
{
auto const user = env.app()
.config()[proto_ws ? "port_ws" : "port_rpc"]
.get<std::string>("admin_user");
auto const password =
env.app()
.config()[proto_ws ? "port_ws" : "port_rpc"]
.get<std::string>("admin_password");
// 1 - FAILS with wrong pass
jrr = makeAdminRequest(
env, proto, *user, *password + "_")[jss::result];
BEAST_EXPECT(
jrr["error"] == proto_ws ? "forbidden" : "noPermission");
BEAST_EXPECT(
jrr["error_message"] == proto_ws
? "Bad credentials."
: "You don't have permission for this command.");
// 2 - FAILS with password in an object
jrr = makeAdminRequest(
env, proto, *user, *password, true)[jss::result];
BEAST_EXPECT(
jrr["error"] == proto_ws ? "forbidden" : "noPermission");
BEAST_EXPECT(
jrr["error_message"] == proto_ws
? "Bad credentials."
: "You don't have permission for this command.");
// 3 - FAILS with wrong user
jrr = makeAdminRequest(
env, proto, *user + "_", *password)[jss::result];
BEAST_EXPECT(
jrr["error"] == proto_ws ? "forbidden" : "noPermission");
BEAST_EXPECT(
jrr["error_message"] == proto_ws
? "Bad credentials."
: "You don't have permission for this command.");
// 4 - FAILS no credentials
jrr = makeAdminRequest(env, proto, "", "")[jss::result];
BEAST_EXPECT(
jrr["error"] == proto_ws ? "forbidden" : "noPermission");
BEAST_EXPECT(
jrr["error_message"] == proto_ws
? "Bad credentials."
: "You don't have permission for this command.");
// 5 - SUCCEEDS with proper credentials
jrr = makeAdminRequest(env, proto, *user, *password)[jss::result];
BEAST_EXPECT(jrr["status"] == "success");
}
else if (admin)
{
// 1 - SUCCEEDS with proper credentials
jrr = makeAdminRequest(env, proto, "u", "p")[jss::result];
BEAST_EXPECT(jrr["status"] == "success");
// 2 - SUCCEEDS without proper credentials
jrr = makeAdminRequest(env, proto, "", "")[jss::result];
BEAST_EXPECT(jrr["status"] == "success");
}
else
{
// 1 - FAILS - admin disabled
jrr = makeAdminRequest(env, proto, "", "")[jss::result];
BEAST_EXPECT(
jrr["error"] == proto_ws ? "forbidden" : "noPermission");
BEAST_EXPECT(
jrr["error_message"] == proto_ws
? "Bad credentials."
: "You don't have permission for this command.");
}
}
void
testWSClientToHttpServer(boost::asio::yield_context& yield)
{
testcase("WS client to http server fails");
using namespace jtx;
Env env{*this, envconfig([](std::unique_ptr<Config> cfg) {
cfg->section("port_ws").set("protocol", "http,https");
return cfg;
})};
// non-secure request
{
boost::system::error_code ec;
boost::beast::http::response<boost::beast::http::string_body> resp;
doWSRequest(env, yield, false, resp, ec);
if (!BEAST_EXPECTS(!ec, ec.message()))
return;
BEAST_EXPECT(
resp.result() == boost::beast::http::status::unauthorized);
}
// secure request
{
boost::system::error_code ec;
boost::beast::http::response<boost::beast::http::string_body> resp;
doWSRequest(env, yield, true, resp, ec);
if (!BEAST_EXPECTS(!ec, ec.message()))
return;
BEAST_EXPECT(
resp.result() == boost::beast::http::status::unauthorized);
}
}
void
testStatusRequest(boost::asio::yield_context& yield)
{
testcase("Status request");
using namespace jtx;
Env env{*this, envconfig([](std::unique_ptr<Config> cfg) {
cfg->section("port_rpc").set("protocol", "ws2,wss2");
cfg->section("port_ws").set("protocol", "http");
return cfg;
})};
// non-secure request
{
boost::system::error_code ec;
boost::beast::http::response<boost::beast::http::string_body> resp;
doHTTPRequest(env, yield, false, resp, ec);
if (!BEAST_EXPECTS(!ec, ec.message()))
return;
BEAST_EXPECT(resp.result() == boost::beast::http::status::ok);
}
// secure request
{
boost::system::error_code ec;
boost::beast::http::response<boost::beast::http::string_body> resp;
doHTTPRequest(env, yield, true, resp, ec);
if (!BEAST_EXPECTS(!ec, ec.message()))
return;
BEAST_EXPECT(resp.result() == boost::beast::http::status::ok);
}
}
void
testTruncatedWSUpgrade(boost::asio::yield_context& yield)
{
testcase("Partial WS upgrade request");
using namespace jtx;
using namespace boost::asio;
using namespace boost::beast::http;
Env env{*this, envconfig([](std::unique_ptr<Config> cfg) {
cfg->section("port_ws").set("protocol", "ws2");
return cfg;
})};
auto const port =
env.app().config()["port_ws"].get<std::uint16_t>("port");
auto const ip = env.app().config()["port_ws"].get<std::string>("ip");
boost::system::error_code ec;
response<string_body> resp;
auto req = makeWSUpgrade(*ip, *port);
// truncate the request message to near the value of the version header
auto req_string = boost::lexical_cast<std::string>(req);
req_string.erase(req_string.find_last_of("13"), std::string::npos);
io_service& ios = get_io_service();
ip::tcp::resolver r{ios};
boost::beast::multi_buffer sb;
auto it = r.async_resolve(
ip::tcp::resolver::query{*ip, std::to_string(*port)}, yield[ec]);
if (!BEAST_EXPECTS(!ec, ec.message()))
return;
ip::tcp::socket sock{ios};
async_connect(sock, it, yield[ec]);
if (!BEAST_EXPECTS(!ec, ec.message()))
return;
async_write(sock, boost::asio::buffer(req_string), yield[ec]);
if (!BEAST_EXPECTS(!ec, ec.message()))
return;
// since we've sent an incomplete request, the server will
// keep trying to read until it gives up (by timeout)
async_read(sock, sb, resp, yield[ec]);
BEAST_EXPECT(ec);
}
void
testCantConnect(
std::string const& client_protocol,
std::string const& server_protocol,
boost::asio::yield_context& yield)
{
// The essence of this test is to have a client and server configured
// out-of-phase with respect to ssl (secure client and insecure server
// or vice-versa)
testcase << "Connect fails: " << client_protocol << " client to "
<< server_protocol << " server";
using namespace jtx;
Env env{*this, makeConfig(server_protocol)};
boost::beast::http::response<boost::beast::http::string_body> resp;
boost::system::error_code ec;
if (boost::starts_with(client_protocol, "h"))
{
doHTTPRequest(env, yield, client_protocol == "https", resp, ec);
BEAST_EXPECT(ec);
}
else
{
doWSRequest(
env,
yield,
client_protocol == "wss" || client_protocol == "wss2",
resp,
ec);
BEAST_EXPECT(ec);
}
}
void
testAuth(bool secure, boost::asio::yield_context& yield)
{
testcase << "Server with authorization, "
<< (secure ? "secure" : "non-secure");
using namespace test::jtx;
Env env{*this, envconfig([secure](std::unique_ptr<Config> cfg) {
(*cfg)["port_rpc"].set("user", "me");
(*cfg)["port_rpc"].set("password", "secret");
(*cfg)["port_rpc"].set(
"protocol", secure ? "https" : "http");
if (secure)
(*cfg)["port_ws"].set("protocol", "http,ws");
return cfg;
})};
Json::Value jr;
jr[jss::method] = "server_info";
boost::beast::http::response<boost::beast::http::string_body> resp;
boost::system::error_code ec;
doHTTPRequest(env, yield, secure, resp, ec, to_string(jr));
BEAST_EXPECT(resp.result() == boost::beast::http::status::forbidden);
myFields auth;
auth.insert("Authorization", "");
doHTTPRequest(env, yield, secure, resp, ec, to_string(jr), auth);
BEAST_EXPECT(resp.result() == boost::beast::http::status::forbidden);
auth.set("Authorization", "Basic NOT-VALID");
doHTTPRequest(env, yield, secure, resp, ec, to_string(jr), auth);
BEAST_EXPECT(resp.result() == boost::beast::http::status::forbidden);
auth.set("Authorization", "Basic " + base64_encode("me:badpass"));
doHTTPRequest(env, yield, secure, resp, ec, to_string(jr), auth);
BEAST_EXPECT(resp.result() == boost::beast::http::status::forbidden);
auto const user = env.app()
.config()
.section("port_rpc")
.get<std::string>("user")
.value();
auto const pass = env.app()
.config()
.section("port_rpc")
.get<std::string>("password")
.value();
// try with the correct user/pass, but not encoded
auth.set("Authorization", "Basic " + user + ":" + pass);
doHTTPRequest(env, yield, secure, resp, ec, to_string(jr), auth);
BEAST_EXPECT(resp.result() == boost::beast::http::status::forbidden);
// finally if we use the correct user/pass encoded, we should get a 200
auth.set("Authorization", "Basic " + base64_encode(user + ":" + pass));
doHTTPRequest(env, yield, secure, resp, ec, to_string(jr), auth);
BEAST_EXPECT(resp.result() == boost::beast::http::status::ok);
BEAST_EXPECT(!resp.body().empty());
}
void
testLimit(boost::asio::yield_context& yield, int limit)
{
testcase << "Server with connection limit of " << limit;
using namespace test::jtx;
using namespace boost::asio;
using namespace boost::beast::http;
Env env{*this, envconfig([&](std::unique_ptr<Config> cfg) {
(*cfg)["port_rpc"].set("limit", std::to_string(limit));
return cfg;
})};
auto const port =
env.app().config()["port_rpc"].get<std::uint16_t>("port").value();
auto const ip =
env.app().config()["port_rpc"].get<std::string>("ip").value();
boost::system::error_code ec;
io_service& ios = get_io_service();
ip::tcp::resolver r{ios};
Json::Value jr;
jr[jss::method] = "server_info";
auto it = r.async_resolve(
ip::tcp::resolver::query{ip, std::to_string(port)}, yield[ec]);
BEAST_EXPECT(!ec);
std::vector<std::pair<ip::tcp::socket, boost::beast::multi_buffer>>
clients;
int connectionCount{1}; // starts at 1 because the Env already has one
// for JSONRPCCLient
// for nonzero limits, go one past the limit, although failures happen
// at the limit, so this really leads to the last two clients failing.
// for zero limit, pick an arbitrary nonzero number of clients - all
// should connect fine.
int testTo = (limit == 0) ? 50 : limit + 1;
while (connectionCount < testTo)
{
clients.emplace_back(std::make_pair(
ip::tcp::socket{ios}, boost::beast::multi_buffer{}));
async_connect(clients.back().first, it, yield[ec]);
BEAST_EXPECT(!ec);
auto req = makeHTTPRequest(ip, port, to_string(jr), {});
async_write(clients.back().first, req, yield[ec]);
BEAST_EXPECT(!ec);
++connectionCount;
}
int readCount = 0;
for (auto& [soc, buf] : clients)
{
boost::beast::http::response<boost::beast::http::string_body> resp;
async_read(soc, buf, resp, yield[ec]);
++readCount;
// expect the reads to fail for the clients that connected at or
// above the limit. If limit is 0, all reads should succeed
BEAST_EXPECT(
(limit == 0 || readCount < limit - 1) ? (!ec) : bool(ec));
}
}
void
testWSHandoff(boost::asio::yield_context& yield)
{
testcase("Connection with WS handoff");
using namespace test::jtx;
Env env{*this, envconfig([](std::unique_ptr<Config> cfg) {
(*cfg)["port_ws"].set("protocol", "wss");
return cfg;
})};
auto const port =
env.app().config()["port_ws"].get<std::uint16_t>("port").value();
auto const ip =
env.app().config()["port_ws"].get<std::string>("ip").value();
boost::beast::http::response<boost::beast::http::string_body> resp;
boost::system::error_code ec;
doRequest(yield, makeWSUpgrade(ip, port), ip, port, true, resp, ec);
BEAST_EXPECT(
resp.result() == boost::beast::http::status::switching_protocols);
BEAST_EXPECT(
resp.find("Upgrade") != resp.end() &&
resp["Upgrade"] == "websocket");
BEAST_EXPECT(
resp.find("Connection") != resp.end() &&
resp["Connection"] == "upgrade");
}
void
testNoRPC(boost::asio::yield_context& yield)
{
testcase("Connection to port with no RPC enabled");
using namespace test::jtx;
Env env{*this};
auto const port =
env.app().config()["port_ws"].get<std::uint16_t>("port").value();
auto const ip =
env.app().config()["port_ws"].get<std::string>("ip").value();
boost::beast::http::response<boost::beast::http::string_body> resp;
boost::system::error_code ec;
// body content is required here to avoid being
// detected as a status request
doRequest(
yield,
makeHTTPRequest(ip, port, "foo", {}),
ip,
port,
false,
resp,
ec);
BEAST_EXPECT(resp.result() == boost::beast::http::status::forbidden);
BEAST_EXPECT(resp.body() == "Forbidden\r\n");
}
void
testWSRequests(boost::asio::yield_context& yield)
{
testcase("WS client sends assorted input");
using namespace test::jtx;
using namespace boost::asio;
using namespace boost::beast::http;
Env env{*this};
auto const port =
env.app().config()["port_ws"].get<std::uint16_t>("port").value();
auto const ip =
env.app().config()["port_ws"].get<std::string>("ip").value();
boost::system::error_code ec;
io_service& ios = get_io_service();
ip::tcp::resolver r{ios};
auto it = r.async_resolve(
ip::tcp::resolver::query{ip, std::to_string(port)}, yield[ec]);
if (!BEAST_EXPECT(!ec))
return;
ip::tcp::socket sock{ios};
async_connect(sock, it, yield[ec]);
if (!BEAST_EXPECT(!ec))
return;
boost::beast::websocket::stream<boost::asio::ip::tcp::socket&> ws{sock};
ws.handshake(ip + ":" + std::to_string(port), "/");
// helper lambda, used below
auto sendAndParse = [&](std::string const& req) -> Json::Value {
ws.async_write_some(true, buffer(req), yield[ec]);
if (!BEAST_EXPECT(!ec))
return Json::objectValue;
boost::beast::multi_buffer sb;
ws.async_read(sb, yield[ec]);
if (!BEAST_EXPECT(!ec))
return Json::objectValue;
Json::Value resp;
Json::Reader jr;
if (!BEAST_EXPECT(jr.parse(
boost::lexical_cast<std::string>(
boost::beast::make_printable(sb.data())),
resp)))
return Json::objectValue;
sb.consume(sb.size());
return resp;
};
{ // send invalid json
auto resp = sendAndParse("NOT JSON");
BEAST_EXPECT(
resp.isMember(jss::error) && resp[jss::error] == "jsonInvalid");
BEAST_EXPECT(!resp.isMember(jss::status));
}
{ // send incorrect json (method and command fields differ)
Json::Value jv;
jv[jss::command] = "foo";
jv[jss::method] = "bar";
auto resp = sendAndParse(to_string(jv));
BEAST_EXPECT(
resp.isMember(jss::error) &&
resp[jss::error] == "missingCommand");
BEAST_EXPECT(
resp.isMember(jss::status) && resp[jss::status] == "error");
}
{ // send a ping (not an error)
Json::Value jv;
jv[jss::command] = "ping";
auto resp = sendAndParse(to_string(jv));
BEAST_EXPECT(
resp.isMember(jss::status) && resp[jss::status] == "success");
BEAST_EXPECT(
resp.isMember(jss::result) &&
resp[jss::result].isMember(jss::role) &&
resp[jss::result][jss::role] == "admin");
}
}
void
testAmendmentWarning(boost::asio::yield_context& yield)
{
testcase(
"Status request over WS and RPC with/without Amendment Warning");
using namespace jtx;
using namespace boost::asio;
using namespace boost::beast::http;
Env env{
*this,
validator(
envconfig([](std::unique_ptr<Config> cfg) {
cfg->section("port_rpc").set("protocol", "http");
return cfg;
}),
"")};
env.close();
// advance the ledger so that server status
// sees a published ledger -- without this, we get a status
// failure message about no published ledgers
env.app().getLedgerMaster().tryAdvance();
// make an RPC server info request and look for
// amendment warning status
auto si = env.rpc("server_info")[jss::result];
BEAST_EXPECT(si.isMember(jss::info));
BEAST_EXPECT(!si[jss::info].isMember(jss::amendment_blocked));
BEAST_EXPECT(
env.app().getOPs().getConsensusInfo()["validating"] == true);
BEAST_EXPECT(!si.isMember(jss::warnings));
// make an RPC server state request and look for
// amendment warning status
si = env.rpc("server_state")[jss::result];
BEAST_EXPECT(si.isMember(jss::state));
BEAST_EXPECT(!si[jss::state].isMember(jss::amendment_blocked));
BEAST_EXPECT(
env.app().getOPs().getConsensusInfo()["validating"] == true);
BEAST_EXPECT(!si[jss::state].isMember(jss::warnings));
auto const port_ws =
env.app().config()["port_ws"].get<std::uint16_t>("port");
auto const ip_ws = env.app().config()["port_ws"].get<std::string>("ip");
boost::system::error_code ec;
response<string_body> resp;
doRequest(
yield,
makeHTTPRequest(*ip_ws, *port_ws, "", {}),
*ip_ws,
*port_ws,
false,
resp,
ec);
if (!BEAST_EXPECTS(!ec, ec.message()))
return;
BEAST_EXPECT(resp.result() == boost::beast::http::status::ok);
BEAST_EXPECT(
resp.body().find("connectivity is working.") != std::string::npos);
// mark the Network as having an Amendment Warning, but won't fail
env.app().getOPs().setAmendmentWarned();
env.app().getOPs().beginConsensus(env.closed()->info().hash);
// consensus doesn't change
BEAST_EXPECT(
env.app().getOPs().getConsensusInfo()["validating"] == true);
// RPC request server_info again, now unsupported majority should be
// returned
si = env.rpc("server_info")[jss::result];
BEAST_EXPECT(si.isMember(jss::info));
BEAST_EXPECT(!si[jss::info].isMember(jss::amendment_blocked));
BEAST_EXPECT(
si[jss::info].isMember(jss::warnings) &&
si[jss::info][jss::warnings].isArray() &&
si[jss::info][jss::warnings].size() == 1 &&
si[jss::info][jss::warnings][0u][jss::id].asInt() ==
warnRPC_UNSUPPORTED_MAJORITY);
// RPC request server_state again, now unsupported majority should be
// returned
si = env.rpc("server_state")[jss::result];
BEAST_EXPECT(si.isMember(jss::state));
BEAST_EXPECT(!si[jss::state].isMember(jss::amendment_blocked));
BEAST_EXPECT(
si[jss::state].isMember(jss::warnings) &&
si[jss::state][jss::warnings].isArray() &&
si[jss::state][jss::warnings].size() == 1 &&
si[jss::state][jss::warnings][0u][jss::id].asInt() ==
warnRPC_UNSUPPORTED_MAJORITY);
// but status does not indicate a problem
doRequest(
yield,
makeHTTPRequest(*ip_ws, *port_ws, "", {}),
*ip_ws,
*port_ws,
false,
resp,
ec);
if (!BEAST_EXPECTS(!ec, ec.message()))
return;
BEAST_EXPECT(resp.result() == boost::beast::http::status::ok);
BEAST_EXPECT(
resp.body().find("connectivity is working.") != std::string::npos);
// with ELB_SUPPORT, status still does not indicate a problem
env.app().config().ELB_SUPPORT = true;
doRequest(
yield,
makeHTTPRequest(*ip_ws, *port_ws, "", {}),
*ip_ws,
*port_ws,
false,
resp,
ec);
if (!BEAST_EXPECTS(!ec, ec.message()))
return;
BEAST_EXPECT(resp.result() == boost::beast::http::status::ok);
BEAST_EXPECT(
resp.body().find("connectivity is working.") != std::string::npos);
}
void
testAmendmentBlock(boost::asio::yield_context& yield)
{
testcase("Status request over WS and RPC with/without Amendment Block");
using namespace jtx;
using namespace boost::asio;
using namespace boost::beast::http;
Env env{
*this,
validator(
envconfig([](std::unique_ptr<Config> cfg) {
cfg->section("port_rpc").set("protocol", "http");
return cfg;
}),
"")};
env.close();
// advance the ledger so that server status
// sees a published ledger -- without this, we get a status
// failure message about no published ledgers
env.app().getLedgerMaster().tryAdvance();
// make an RPC server info request and look for
// amendment_blocked status
auto si = env.rpc("server_info")[jss::result];
BEAST_EXPECT(si.isMember(jss::info));
BEAST_EXPECT(!si[jss::info].isMember(jss::amendment_blocked));
BEAST_EXPECT(
env.app().getOPs().getConsensusInfo()["validating"] == true);
BEAST_EXPECT(!si.isMember(jss::warnings));
// make an RPC server state request and look for
// amendment_blocked status
si = env.rpc("server_state")[jss::result];
BEAST_EXPECT(si.isMember(jss::state));
BEAST_EXPECT(!si[jss::state].isMember(jss::amendment_blocked));
BEAST_EXPECT(
env.app().getOPs().getConsensusInfo()["validating"] == true);
BEAST_EXPECT(!si[jss::state].isMember(jss::warnings));
auto const port_ws =
env.app().config()["port_ws"].get<std::uint16_t>("port");
auto const ip_ws = env.app().config()["port_ws"].get<std::string>("ip");
boost::system::error_code ec;
response<string_body> resp;
doRequest(
yield,
makeHTTPRequest(*ip_ws, *port_ws, "", {}),
*ip_ws,
*port_ws,
false,
resp,
ec);
if (!BEAST_EXPECTS(!ec, ec.message()))
return;
BEAST_EXPECT(resp.result() == boost::beast::http::status::ok);
BEAST_EXPECT(
resp.body().find("connectivity is working.") != std::string::npos);
// mark the Network as Amendment Blocked, but still won't fail until
// ELB is enabled (next step)
env.app().getOPs().setAmendmentBlocked();
env.app().getOPs().beginConsensus(env.closed()->info().hash);
// consensus now sees validation disabled
BEAST_EXPECT(
env.app().getOPs().getConsensusInfo()["validating"] == false);
// RPC request server_info again, now AB should be returned
Morty Proxy This is a proxified and sanitized view of the page, visit original site.