forked from arvidn/libtorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tracker.cpp
1839 lines (1619 loc) · 55.3 KB
/
test_tracker.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
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
/*
Copyright (c) 2010, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "test.hpp"
#include "settings.hpp"
#include "setup_swarm.hpp"
#include "setup_transfer.hpp" // for addr()
#include "utils.hpp" // for print_alerts
#include "create_torrent.hpp"
#include "simulator/simulator.hpp"
#include "simulator/http_server.hpp"
#include "simulator/utils.hpp"
#include "libtorrent/alert_types.hpp"
#include "libtorrent/announce_entry.hpp"
#include "libtorrent/session.hpp"
#include "libtorrent/create_torrent.hpp"
#include "libtorrent/file_storage.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/aux_/ip_helpers.hpp" // for is_v4
#include <iostream>
using namespace lt;
using namespace sim;
using chrono::duration_cast;
// seconds
const int duration = 10000;
template <typename Tp1, typename Tp2>
bool eq(Tp1 const lhs, Tp2 const rhs)
{
return std::abs(lt::duration_cast<seconds>(lhs - rhs).count()) <= 1;
}
void test_interval(int interval)
{
using sim::asio::ip::address_v4;
sim::default_config network_cfg;
sim::simulation sim{network_cfg};
bool ran_to_completion = false;
sim::asio::io_context web_server(sim, make_address_v4("2.2.2.2"));
// listen on port 8080
sim::http_server http(web_server, 8080);
// the timestamps of all announces
std::vector<lt::time_point> announces;
http.register_handler("/announce"
, [&announces,interval,&ran_to_completion](std::string /* method */
, std::string /* req */
, std::map<std::string, std::string>&)
{
// don't collect events once we're done. We're not interested in the
// tracker stopped announce for instance
if (!ran_to_completion)
announces.push_back(lt::clock_type::now());
char response[500];
int const size = std::snprintf(response, sizeof(response), "d8:intervali%de5:peers0:e", interval);
return sim::send_response(200, "OK", size) + response;
});
std::vector<lt::time_point> announce_alerts;
lt::settings_pack default_settings = settings();
// since the test tracker is only listening on IPv4 we need to configure the
// client to do the same so that the number of tracker_announce_alerts matches
// the number of announces seen by the tracker
default_settings.set_str(settings_pack::listen_interfaces, "0.0.0.0:6881");
lt::add_torrent_params default_add_torrent;
setup_swarm(1, swarm_test::upload, sim, default_settings, default_add_torrent
// add session
, [](lt::settings_pack&) {}
// add torrent
, [](lt::add_torrent_params& params) {
params.trackers.push_back("http://2.2.2.2:8080/announce");
}
// on alert
, [&](lt::alert const* a, lt::session&) {
if (ran_to_completion) return;
if (lt::alert_cast<lt::tracker_announce_alert>(a))
{
announce_alerts.push_back(a->timestamp());
}
}
// terminate
, [&](int const ticks, lt::session&) -> bool {
if (ticks > duration + 1)
{
ran_to_completion = true;
return true;
}
return false;
});
TEST_CHECK(ran_to_completion);
TEST_EQUAL(announce_alerts.size(), announces.size());
TEST_CHECK(announces.size() % 2 == 0);
lt::time_point last_announce = announces[0];
lt::time_point last_alert = announce_alerts[0];
for (int i = 2; i < int(announces.size()); i += 2)
{
// make sure the interval is within 1 second of what it's supposed to be
// (this accounts for network latencies, and the second-granularity
// timestamps)
TEST_CHECK(eq(duration_cast<lt::seconds>(announces[i] - last_announce), lt::seconds(interval)));
last_announce = announces[i];
TEST_CHECK(eq(duration_cast<lt::milliseconds>(announce_alerts[i] - last_alert), lt::seconds(interval)));
last_alert = announce_alerts[i];
}
}
template <typename AddTorrent, typename OnAlert>
std::vector<std::string> test_event(swarm_test_t const type
, AddTorrent add_torrent
, OnAlert on_alert)
{
using sim::asio::ip::address_v4;
sim::default_config network_cfg;
sim::simulation sim{network_cfg};
sim::asio::io_context web_server(sim, make_address_v4("2.2.2.2"));
// listen on port 8080
sim::http_server http(web_server, 8080);
// the request strings of all announces
std::vector<std::string> announces;
const int interval = 500;
http.register_handler("/announce"
, [&](std::string method, std::string req
, std::map<std::string, std::string>&)
{
TEST_EQUAL(method, "GET");
announces.push_back(req);
char response[500];
int const size = std::snprintf(response, sizeof(response), "d8:intervali%de5:peers0:e", interval);
return sim::send_response(200, "OK", size) + response;
});
lt::settings_pack default_settings = settings();
lt::add_torrent_params default_add_torrent;
setup_swarm(2, type, sim, default_settings, default_add_torrent
// add session
, [](lt::settings_pack&) { }
// add torrent
, add_torrent
// on alert
, on_alert
// terminate
, [&](int const ticks, lt::session& ses) -> bool
{
return ticks > duration;
});
// this is some basic sanity checking of the announces that should always be
// true.
// the first announce should be event=started then no other announce should
// have event=started.
// only the last announce should have event=stopped.
TEST_CHECK(announces.size() > 2);
// to keep things simple, just consider one of the v1 or v2 announces, since
// we use a hybrid torrent, we get double announces.
std::map<std::string, std::vector<std::string>> announces_ih;
for (auto&& a : announces)
{
auto const ih = a.find("info_hash=");
TEST_CHECK(ih != std::string::npos);
auto const key = a.substr(ih, 20);
announces_ih[key].push_back(std::move(a));
}
for (auto const& entry : announces_ih)
{
auto const& ann = entry.second;
TEST_CHECK(ann.size() > 2);
TEST_CHECK(ann.front().find("&event=started") != std::string::npos);
for (auto const& a : span<std::string const>(ann).subspan(1))
TEST_CHECK(a.find("&event=started") == std::string::npos);
TEST_CHECK(ann.back().find("&event=stopped") != std::string::npos);
for (auto const& a : span<std::string const>(ann).first(ann.size() - 1))
TEST_CHECK(a.find("&event=stopped") == std::string::npos);
}
return announces_ih.begin()->second;
}
TORRENT_TEST(event_completed_downloading)
{
auto const announces = test_event(swarm_test::download
, [](lt::add_torrent_params& params) {
params.trackers.push_back("http://2.2.2.2:8080/announce");
}
, [&](lt::alert const*, lt::session&) {}
);
// make sure there's exactly one event=completed
TEST_CHECK(std::count_if(announces.begin(), announces.end(), [](std::string const& s)
{ return s.find("&event=completed") != std::string::npos; }) == 1);
}
TORRENT_TEST(event_completed_downloading_replace_trackers)
{
auto const announces = test_event(swarm_test::download
, [](lt::add_torrent_params& params) {}
, [&](lt::alert const* a, lt::session&) {
if (auto const* at = alert_cast<add_torrent_alert>(a))
at->handle.replace_trackers({announce_entry{"http://2.2.2.2:8080/announce"}});
}
);
// make sure there's exactly one event=completed
TEST_CHECK(std::count_if(announces.begin(), announces.end(), [](std::string const& s)
{ return s.find("&event=completed") != std::string::npos; }) == 1);
}
TORRENT_TEST(event_completed_seeding)
{
auto const announces = test_event(swarm_test::upload | swarm_test::no_auto_stop
, [](lt::add_torrent_params& params) {
params.trackers.push_back("http://2.2.2.2:8080/announce");
}
, [&](lt::alert const*, lt::session&) {}
);
// make sure there are no event=completed, since we added the torrent as a
// seed
TEST_CHECK(std::count_if(announces.begin(), announces.end(), [](std::string const& s)
{ return s.find("&event=completed") != std::string::npos; }) == 0);
}
TORRENT_TEST(event_completed_seeding_replace_trackers)
{
auto const announces = test_event(swarm_test::upload | swarm_test::no_auto_stop
, [](lt::add_torrent_params& params) {}
, [&](lt::alert const* a, lt::session&) {
if (auto const* at = alert_cast<add_torrent_alert>(a))
at->handle.replace_trackers({announce_entry{"http://2.2.2.2:8080/announce"}});
}
);
// make sure there are no event=completed, since we added the torrent as a
// seed
TEST_CHECK(std::count_if(announces.begin(), announces.end(), [](std::string const& s)
{ return s.find("&event=completed") != std::string::npos; }) == 0);
}
TORRENT_TEST(announce_interval_440)
{
test_interval(440);
}
TORRENT_TEST(announce_interval_1800)
{
test_interval(1800);
}
TORRENT_TEST(announce_interval_1200)
{
test_interval(3600);
}
struct sim_config : sim::default_config
{
explicit sim_config(bool ipv6 = true) : ipv6(ipv6) {}
chrono::high_resolution_clock::duration hostname_lookup(
asio::ip::address const& requestor
, std::string hostname
, std::vector<asio::ip::address>& result
, boost::system::error_code& ec) override
{
if (hostname == "tracker.com")
{
result.push_back(make_address_v4("123.0.0.2"));
if (ipv6)
result.push_back(make_address_v6("ff::dead:beef"));
return duration_cast<chrono::high_resolution_clock::duration>(chrono::milliseconds(100));
}
if (hostname == "localhost")
{
result.push_back(make_address_v4("127.0.0.1"));
if (ipv6)
result.push_back(make_address_v6("::1"));
return duration_cast<chrono::high_resolution_clock::duration>(chrono::milliseconds(1));
}
if (hostname == "xn--tracker-.com")
{
result.push_back(make_address_v4("123.0.0.2"));
return duration_cast<chrono::high_resolution_clock::duration>(chrono::milliseconds(100));
}
if (hostname == "redirector.com")
{
result.push_back(make_address_v4("123.0.0.4"));
return duration_cast<chrono::high_resolution_clock::duration>(chrono::milliseconds(100));
}
return default_config::hostname_lookup(requestor, hostname, result, ec);
}
bool ipv6;
};
void on_alert_notify(lt::session* ses)
{
post(ses->get_context(), [ses] {
std::vector<lt::alert*> alerts;
ses->pop_alerts(&alerts);
for (lt::alert* a : alerts)
{
lt::time_duration d = a->timestamp().time_since_epoch();
std::uint32_t const millis = std::uint32_t(
lt::duration_cast<lt::milliseconds>(d).count());
std::printf("%4d.%03d: %s\n", millis / 1000, millis % 1000,
a->message().c_str());
}
});
}
static const int num_interfaces = 3;
void test_ipv6_support(char const* listen_interfaces
, int const expect_v4, int const expect_v6)
{
using sim::asio::ip::address_v4;
sim_config network_cfg;
sim::simulation sim{network_cfg};
sim::asio::io_context web_server_v4(sim, make_address_v4("123.0.0.2"));
sim::asio::io_context web_server_v6(sim, make_address_v6("ff::dead:beef"));
// listen on port 8080
sim::http_server http_v4(web_server_v4, 8080);
sim::http_server http_v6(web_server_v6, 8080);
int v4_announces = 0;
int v6_announces = 0;
// if we're not listening we'll just report port 0
std::string const expect_port = (listen_interfaces && listen_interfaces == ""_sv)
? "&port=1" : "&port=6881";
http_v4.register_handler("/announce"
, [&v4_announces,expect_port](std::string method, std::string req
, std::map<std::string, std::string>&)
{
++v4_announces;
TEST_EQUAL(method, "GET");
TEST_CHECK(req.find(expect_port) != std::string::npos);
char response[500];
int const size = std::snprintf(response, sizeof(response), "d8:intervali1800e5:peers0:e");
return sim::send_response(200, "OK", size) + response;
});
http_v6.register_handler("/announce"
, [&v6_announces,expect_port](std::string method, std::string req
, std::map<std::string, std::string>&)
{
++v6_announces;
TEST_EQUAL(method, "GET");
TEST_CHECK(req.find(expect_port) != std::string::npos);
char response[500];
int const size = std::snprintf(response, sizeof(response), "d8:intervali1800e5:peers0:e");
return sim::send_response(200, "OK", size) + response;
});
{
lt::session_proxy zombie;
std::vector<asio::ip::address> ips;
for (int i = 0; i < num_interfaces; i++)
{
char ep[30];
std::snprintf(ep, sizeof(ep), "123.0.0.%d", i + 1);
ips.push_back(make_address(ep));
std::snprintf(ep, sizeof(ep), "ffff::1337:%d", i + 1);
ips.push_back(make_address(ep));
}
asio::io_context ios(sim, ips);
lt::settings_pack sett = settings();
if (listen_interfaces)
{
sett.set_str(settings_pack::listen_interfaces, listen_interfaces);
}
auto ses = std::make_unique<lt::session>(sett, ios);
ses->set_alert_notify(std::bind(&on_alert_notify, ses.get()));
lt::add_torrent_params p;
p.name = "test-torrent";
p.save_path = ".";
p.info_hashes.v1.assign("abababababababababab");
//TODO: parameterize http vs. udp here
p.trackers.push_back("http://tracker.com:8080/announce");
ses->async_add_torrent(p);
// stop the torrent 5 seconds in
sim::timer t1(sim, lt::seconds(5)
, [&ses](boost::system::error_code const&)
{
std::vector<lt::torrent_handle> torrents = ses->get_torrents();
for (auto const& t : torrents)
{
t.pause();
}
});
// then shut down 10 seconds in
sim::timer t2(sim, lt::seconds(10)
, [&ses,&zombie](boost::system::error_code const&)
{
zombie = ses->abort();
ses.reset();
});
sim.run();
}
TEST_EQUAL(v4_announces, expect_v4);
TEST_EQUAL(v6_announces, expect_v6);
}
void test_udpv6_support(char const* listen_interfaces
, int const expect_v4, int const expect_v6)
{
using sim::asio::ip::address_v4;
sim_config network_cfg;
sim::simulation sim{network_cfg};
sim::asio::io_context web_server_v4(sim, make_address_v4("123.0.0.2"));
sim::asio::io_context web_server_v6(sim, make_address_v6("ff::dead:beef"));
int v4_announces = 0;
int v6_announces = 0;
{
lt::session_proxy zombie;
std::vector<asio::ip::address> ips;
for (int i = 0; i < num_interfaces; i++)
{
char ep[30];
std::snprintf(ep, sizeof(ep), "123.0.0.%d", i + 1);
ips.push_back(make_address(ep));
std::snprintf(ep, sizeof(ep), "ffff::1337:%d", i + 1);
ips.push_back(make_address(ep));
}
asio::io_context ios(sim, ips);
lt::settings_pack sett = settings();
if (listen_interfaces)
{
sett.set_str(settings_pack::listen_interfaces, listen_interfaces);
}
auto ses = std::make_unique<lt::session>(sett, ios);
// since we don't have a udp tracker to run in the sim, looking for the
// alerts is the closest proxy
ses->set_alert_notify([&]{
post(ses->get_context(), [&] {
std::vector<lt::alert*> alerts;
ses->pop_alerts(&alerts);
for (lt::alert* a : alerts)
{
lt::time_duration d = a->timestamp().time_since_epoch();
std::uint32_t const millis = std::uint32_t(
lt::duration_cast<lt::milliseconds>(d).count());
std::printf("%4d.%03d: %s\n", millis / 1000, millis % 1000,
a->message().c_str());
if (auto tr = alert_cast<tracker_announce_alert>(a))
{
if (lt::aux::is_v4(tr->local_endpoint))
++v4_announces;
else
++v6_announces;
}
else if (alert_cast<tracker_error_alert>(a))
{
TEST_ERROR("unexpected tracker error");
}
}
});
});
lt::add_torrent_params p;
p.name = "test-torrent";
p.save_path = ".";
p.info_hashes.v1.assign("abababababababababab");
p.trackers.push_back("udp://tracker.com:8080/announce");
ses->async_add_torrent(p);
// stop the torrent 5 seconds in
sim::timer t1(sim, lt::seconds(5)
, [&ses](boost::system::error_code const&)
{
std::vector<lt::torrent_handle> torrents = ses->get_torrents();
for (auto const& t : torrents)
{
t.pause();
}
});
// then shut down 10 seconds in
sim::timer t2(sim, lt::seconds(10)
, [&ses,&zombie](boost::system::error_code const&)
{
zombie = ses->abort();
ses.reset();
});
sim.run();
}
TEST_EQUAL(v4_announces, expect_v4);
TEST_EQUAL(v6_announces, expect_v6);
}
// this test makes sure that a tracker whose host name resolves to both IPv6 and
// IPv4 addresses will be announced to twice, once for each address family
TORRENT_TEST(ipv6_support)
{
// null means default
test_ipv6_support(nullptr, num_interfaces * 2, num_interfaces * 2);
}
TORRENT_TEST(announce_no_listen)
{
// if we don't listen on any sockets at all we should not announce to trackers
test_ipv6_support("", 0, 0);
}
TORRENT_TEST(announce_udp_no_listen)
{
// if we don't listen on any sockets at all we should not announce to trackers
test_udpv6_support("", 0, 0);
}
TORRENT_TEST(ipv6_support_bind_v4_v6_any)
{
// 2 because there's one announce on startup and one when shutting down
// IPv6 will send announces for each interface
test_ipv6_support("0.0.0.0:6881,[::0]:6881", num_interfaces * 2, num_interfaces * 2);
}
TORRENT_TEST(ipv6_support_bind_v6_any)
{
test_ipv6_support("[::0]:6881", 0, num_interfaces * 2);
}
TORRENT_TEST(ipv6_support_bind_v4)
{
test_ipv6_support("123.0.0.3:6881", 2, 0);
}
TORRENT_TEST(ipv6_support_bind_v6)
{
test_ipv6_support("[ffff::1337:1]:6881", 0, 2);
}
TORRENT_TEST(ipv6_support_bind_v6_3interfaces)
{
test_ipv6_support("[ffff::1337:1]:6881,[ffff::1337:2]:6881,[ffff::1337:3]:6881", 0, 3 * 2);
}
TORRENT_TEST(ipv6_support_bind_v4_v6)
{
test_ipv6_support("123.0.0.3:6881,[ffff::1337:1]:6881", 2, 2);
}
TORRENT_TEST(ipv6_support_bind_v6_v4)
{
test_ipv6_support("[ffff::1337:1]:6881,123.0.0.3:6881", 2, 2);
}
// this runs a simulation of a torrent with tracker(s), making sure the request
// received by the tracker matches the expectation.
// The Setup function is run first, giving the test an opportunity to add
// trackers to the torrent. It's expected to return the number of seconds to
// wait until test2 is called.
// The Announce function is called on http requests. Test1 is run on the session
// 5 seconds after startup. The tracker is running at 123.0.0.2 (or tracker.com)
// port 8080.
template <typename Setup, typename Announce, typename Test1, typename Test2>
void tracker_test(Setup setup, Announce a, Test1 test1, Test2 test2
, char const* url_path = "/announce"
, char const* redirect = "http://123.0.0.2/announce")
{
using sim::asio::ip::address_v4;
sim_config network_cfg;
sim::simulation sim{network_cfg};
sim::asio::io_context tracker_ios(sim, make_address_v4("123.0.0.2"));
sim::asio::io_context tracker_ios6(sim, make_address_v6("ff::dead:beef"));
sim::asio::io_context redirector_ios(sim, make_address_v4("123.0.0.4"));
sim::asio::io_context tracker_lo_ios(sim, make_address_v4("127.0.0.1"));
sim::asio::io_context tracker_lo_ios6(sim, make_address_v6("::1"));
// listen on port 8080
sim::http_server http(tracker_ios, 8080);
sim::http_server http6(tracker_ios6, 8080);
sim::http_server http_lo(tracker_lo_ios, 8080);
sim::http_server http6_lo(tracker_lo_ios6, 8080);
sim::http_server http_redirect(redirector_ios, 8080);
http.register_handler(url_path, a);
http6.register_handler(url_path, a);
http_lo.register_handler(url_path, a);
http6_lo.register_handler(url_path, a);
http_redirect.register_redirect(url_path, redirect);
lt::session_proxy zombie;
asio::io_context ios(sim, { make_address_v4("123.0.0.3")
, make_address_v6("ffff::1337") });
lt::settings_pack sett = settings();
auto ses = std::make_unique<lt::session>(sett, ios);
ses->set_alert_notify(std::bind(&on_alert_notify, ses.get()));
lt::add_torrent_params p;
p.name = "test-torrent";
p.save_path = ".";
p.info_hashes.v1.assign("abababababababababab");
int const delay = setup(p, *ses);
ses->async_add_torrent(p);
// run the test 5 seconds in
sim::timer t1(sim, lt::seconds(5)
, [&ses,&test1](boost::system::error_code const&)
{
std::vector<lt::torrent_handle> torrents = ses->get_torrents();
TEST_EQUAL(torrents.size(), 1);
torrent_handle h = torrents.front();
test1(h);
});
sim::timer t2(sim, lt::seconds(5 + delay)
, [&ses,&test2](boost::system::error_code const&)
{
std::vector<lt::torrent_handle> torrents = ses->get_torrents();
TEST_EQUAL(torrents.size(), 1);
torrent_handle h = torrents.front();
test2(h);
});
// then shut down 10 seconds in
sim::timer t3(sim, lt::seconds(10 + delay)
, [&ses,&zombie](boost::system::error_code const&)
{
zombie = ses->abort();
ses.reset();
});
sim.run();
}
template <typename Announce, typename Test1, typename Test2>
void tracker_test(Announce a, Test1 test1, Test2 test2, char const* url_path = "/announce")
{
tracker_test([](lt::add_torrent_params& p, lt::session&) {
p.trackers.push_back("http://tracker.com:8080/announce");
return 5;
},
a, test1, test2, url_path);
}
template <typename Announce, typename Test>
void announce_entry_test(Announce a, Test t, char const* url_path = "/announce")
{
tracker_test(a
, [&t] (torrent_handle h) {
std::vector<announce_entry> tr = h.trackers();
TEST_EQUAL(tr.size(), 1);
announce_entry const& ae = tr[0];
t(ae);
}
, [](torrent_handle){}
, url_path);
}
// test that we correctly omit announcing an event=stopped to a tracker we never
// managed to send an event=start to
TORRENT_TEST(omit_stop_event)
{
using sim::asio::ip::address_v4;
sim_config network_cfg;
sim::simulation sim{network_cfg};
lt::session_proxy zombie;
asio::io_context ios(sim, { make_address_v4("123.0.0.3"), make_address_v6("ff::dead:beef")});
lt::settings_pack sett = settings();
std::unique_ptr<lt::session> ses(new lt::session(sett, ios));
print_alerts(*ses);
lt::add_torrent_params p;
p.name = "test-torrent";
p.save_path = ".";
p.info_hashes.v1.assign("abababababababababab");
p.trackers.push_back("udp://tracker.com:8080/announce");
ses->async_add_torrent(p);
// run the test 5 seconds in
sim::timer t1(sim, lt::seconds(5)
, [&ses](boost::system::error_code const&)
{
std::vector<lt::torrent_handle> torrents = ses->get_torrents();
TEST_EQUAL(torrents.size(), 1);
torrent_handle h = torrents.front();
});
int stop_announces = 0;
sim::timer t2(sim, lt::seconds(1800)
, [&](boost::system::error_code const&)
{
// make sure we don't announce a stopped event when stopping
print_alerts(*ses, [&](lt::session&, lt::alert const* a) {
if (alert_cast<lt::tracker_announce_alert>(a))
++stop_announces;
});
std::vector<lt::torrent_handle> torrents = ses->get_torrents();
TEST_EQUAL(torrents.size(), 1);
torrent_handle h = torrents.front();
h.set_flags(torrent_flags::paused, torrent_flags::paused | torrent_flags::auto_managed);
});
// then shut down 10 seconds in
sim::timer t3(sim, lt::seconds(1810)
, [&](boost::system::error_code const&)
{
zombie = ses->abort();
ses.reset();
});
sim.run();
TEST_EQUAL(stop_announces, 0);
}
TORRENT_TEST(test_error)
{
announce_entry_test(
[](std::string method, std::string req
, std::map<std::string, std::string>&)
{
TEST_EQUAL(method, "GET");
char response[500];
int const size = std::snprintf(response, sizeof(response), "d14:failure reason4:teste");
return sim::send_response(200, "OK", size) + response;
}
, [](announce_entry const& ae)
{
TEST_EQUAL(ae.url, "http://tracker.com:8080/announce");
TEST_EQUAL(ae.endpoints.size(), 2);
for (auto const& aep : ae.endpoints)
{
TEST_EQUAL(aep.info_hashes[protocol_version::V1].message, "test");
TEST_EQUAL(aep.info_hashes[protocol_version::V1].last_error, error_code(errors::tracker_failure));
TEST_EQUAL(aep.info_hashes[protocol_version::V1].fails, 1);
}
});
}
TORRENT_TEST(test_no_announce_path)
{
tracker_test(
[](lt::add_torrent_params& p, lt::session&) {
p.trackers.push_back("http://tracker.com:8080");
return 5;
},
[](std::string method, std::string req, std::map<std::string, std::string>&)
{
TEST_EQUAL(method, "GET");
char response[500];
int const size = std::snprintf(response, sizeof(response), "d5:peers6:aaaaaae");
return sim::send_response(200, "OK", size) + response;
}
, [](torrent_handle h)
{
std::vector<announce_entry> tr = h.trackers();
TEST_EQUAL(tr.size(), 1);
announce_entry const& ae = tr[0];
TEST_EQUAL(ae.url, "http://tracker.com:8080");
TEST_EQUAL(ae.endpoints.size(), 2);
for (auto const& aep : ae.endpoints)
{
TEST_EQUAL(aep.info_hashes[protocol_version::V1].message, "");
TEST_EQUAL(aep.info_hashes[protocol_version::V1].last_error, error_code());
TEST_EQUAL(aep.info_hashes[protocol_version::V1].fails, 0);
}
}
, [](torrent_handle){}
, "/");
}
TORRENT_TEST(test_warning)
{
announce_entry_test(
[](std::string method, std::string req
, std::map<std::string, std::string>&)
{
TEST_EQUAL(method, "GET");
char response[500];
int const size = std::snprintf(response, sizeof(response), "d5:peers6:aaaaaa15:warning message5:test2e");
return sim::send_response(200, "OK", size) + response;
}
, [](announce_entry const& ae)
{
TEST_EQUAL(ae.url, "http://tracker.com:8080/announce");
TEST_EQUAL(ae.endpoints.size(), 2);
for (auto const& aep : ae.endpoints)
{
TEST_EQUAL(aep.info_hashes[protocol_version::V1].message, "test2");
TEST_EQUAL(aep.info_hashes[protocol_version::V1].last_error, error_code());
TEST_EQUAL(aep.info_hashes[protocol_version::V1].fails, 0);
}
});
}
TORRENT_TEST(test_scrape_data_in_announce)
{
announce_entry_test(
[](std::string method, std::string req
, std::map<std::string, std::string>&)
{
TEST_EQUAL(method, "GET");
char response[500];
int const size = std::snprintf(response, sizeof(response),
"d5:peers6:aaaaaa8:completei1e10:incompletei2e10:downloadedi3e11:downloadersi4ee");
return sim::send_response(200, "OK", size) + response;
}
, [](announce_entry const& ae)
{
TEST_EQUAL(ae.url, "http://tracker.com:8080/announce");
TEST_EQUAL(ae.endpoints.size(), 2);
for (auto const& aep : ae.endpoints)
{
TEST_EQUAL(aep.info_hashes[protocol_version::V1].message, "");
TEST_EQUAL(aep.info_hashes[protocol_version::V1].last_error, error_code());
TEST_EQUAL(aep.info_hashes[protocol_version::V1].fails, 0);
TEST_EQUAL(aep.info_hashes[protocol_version::V1].scrape_complete, 1);
TEST_EQUAL(aep.info_hashes[protocol_version::V1].scrape_incomplete, 2);
TEST_EQUAL(aep.info_hashes[protocol_version::V1].scrape_downloaded, 3);
}
});
}
TORRENT_TEST(test_scrape)
{
tracker_test(
[](std::string method, std::string req
, std::map<std::string, std::string>&)
{
TEST_EQUAL(method, "GET");
char response[500];
int const size = std::snprintf(response, sizeof(response),
"d5:filesd20:ababababababababababd8:completei1e10:downloadedi3e10:incompletei2eeee");
return sim::send_response(200, "OK", size) + response;
}
, [](torrent_handle h)
{
h.scrape_tracker();
}
, [](torrent_handle h)
{
std::vector<announce_entry> tr = h.trackers();
TEST_EQUAL(tr.size(), 1);
announce_entry const& ae = tr[0];
TEST_EQUAL(ae.endpoints.size(), 2);
for (auto const& aep : ae.endpoints)
{
TEST_EQUAL(aep.info_hashes[protocol_version::V1].scrape_incomplete, 2);
TEST_EQUAL(aep.info_hashes[protocol_version::V1].scrape_complete, 1);
TEST_EQUAL(aep.info_hashes[protocol_version::V1].scrape_downloaded, 3);
}
}
, "/scrape");
}
TORRENT_TEST(test_http_status)
{
announce_entry_test(
[](std::string method, std::string req
, std::map<std::string, std::string>&)
{
TEST_EQUAL(method, "GET");
return sim::send_response(410, "Not A Tracker", 0);
}
, [](announce_entry const& ae)
{
TEST_EQUAL(ae.url, "http://tracker.com:8080/announce");
TEST_EQUAL(ae.endpoints.size(), 2);
for (auto const& aep : ae.endpoints)
{
TEST_EQUAL(aep.info_hashes[protocol_version::V1].message, "Not A Tracker");
TEST_EQUAL(aep.info_hashes[protocol_version::V1].last_error, error_code(410, http_category()));
TEST_EQUAL(aep.info_hashes[protocol_version::V1].fails, 1);
}
});
}
TORRENT_TEST(test_interval)
{
announce_entry_test(
[](std::string method, std::string req
, std::map<std::string, std::string>&)
{
TEST_EQUAL(method, "GET");
char response[500];
int const size = std::snprintf(response, sizeof(response)
, "d10:tracker id8:testteste");
return sim::send_response(200, "OK", size) + response;
}
, [](announce_entry const& ae)
{
TEST_EQUAL(ae.url, "http://tracker.com:8080/announce");
TEST_EQUAL(ae.endpoints.size(), 2);
for (auto const& aep : ae.endpoints)
{
TEST_EQUAL(aep.info_hashes[protocol_version::V1].message, "");
TEST_EQUAL(aep.info_hashes[protocol_version::V1].last_error, error_code());
TEST_EQUAL(aep.info_hashes[protocol_version::V1].fails, 0);
}
TEST_EQUAL(ae.trackerid, "testtest");
});
}
TORRENT_TEST(test_invalid_bencoding)
{
announce_entry_test(
[](std::string method, std::string req
, std::map<std::string, std::string>&)
{
TEST_EQUAL(method, "GET");
char response[500];
int const size = std::snprintf(response, sizeof(response)
, "d10:tracer idteste");
return sim::send_response(200, "OK", size) + response;