File: test_swarm.cpp

package info (click to toggle)
libtorrent-rasterbar 2.0.11-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,304 kB
  • sloc: cpp: 190,670; python: 7,142; makefile: 1,374; ansic: 574; sh: 317; xml: 104
file content (1032 lines) | stat: -rw-r--r-- 27,781 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
/*

Copyright (c) 2008, 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 "setup_swarm.hpp"
#include "test.hpp"
#include "utils.hpp"
#include "libtorrent/alert.hpp"
#include "libtorrent/alert_types.hpp"
#include "libtorrent/session.hpp"
#include "libtorrent/session_stats.hpp"
#include "libtorrent/aux_/path.hpp"
#include "libtorrent/random.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/time.hpp"
#include "settings.hpp"
#include "setup_transfer.hpp" // for ep()
#include "fake_peer.hpp"

#include "simulator/nat.hpp"
#include "simulator/queue.hpp"
#include "utils.hpp"

#include <fstream>

using namespace lt;

TORRENT_TEST(seed_mode)
{
	// with seed mode
	setup_swarm(3, swarm_test::upload
		// add session
		, [](lt::settings_pack&) {}
		// add torrent
		, [](lt::add_torrent_params& params) {
			params.flags |= torrent_flags::seed_mode;
		}
		// on alert
		, [](lt::alert const*, lt::session&) {}
		// terminate
		, [](int, lt::session&) -> bool
		{ return false; });
}

TORRENT_TEST(seed_mode_disable_hash_checks)
{
	// all nodes need to disable hash checking, otherwise the downloader would
	// just fail
	settings_pack swarm_settings = settings();
	swarm_settings.set_bool(settings_pack::disable_hash_checks, true);

	dsl_config network_cfg;
	sim::simulation sim{network_cfg};

	// with seed mode
	setup_swarm(2, swarm_test::upload, sim, swarm_settings, add_torrent_params()
		// add session
		, [](lt::settings_pack& pack) {
			pack.set_int(settings_pack::suggest_mode, settings_pack::suggest_read_cache);
		}
		// add torrent
		, [](lt::add_torrent_params& params) {
			params.flags |= torrent_flags::seed_mode;
			// just to make sure the disable_hash_checks really work, we
			// shouldn't be verifying anything from the storage
//			params.storage = disabled_storage_constructor;
		}
		// on alert
		, [](lt::alert const*, lt::session&) {}
		// terminate
		, [](int, lt::session&) -> bool
		{ return false; });
}

TORRENT_TEST(seed_mode_suggest)
{
	setup_swarm(2, swarm_test::upload
		// add session
		, [](lt::settings_pack& pack) {
			pack.set_int(settings_pack::suggest_mode, settings_pack::suggest_read_cache);
#if TORRENT_ABI_VERSION == 1
			pack.set_int(settings_pack::cache_size, 2);
#endif
		}
		// add torrent
		, [](lt::add_torrent_params& params) {
			params.flags |= torrent_flags::seed_mode;
		}
		// on alert
		, [](lt::alert const*, lt::session&) {}
		// terminate
		, [](int, lt::session&) -> bool
		{ return true; });
}

TORRENT_TEST(plain)
{
	setup_swarm(2, swarm_test::download
		// add session
		, [](lt::settings_pack&) {}
		// add torrent
		, [](lt::add_torrent_params&) {}
		// on alert
		, [](lt::alert const*, lt::session&) {}
		// terminate
		, [](int const ticks, lt::session& ses) -> bool
		{
			if (ticks > 80)
			{
				TEST_ERROR("timeout");
				return true;
			}
			if (!is_seed(ses)) return false;
			std::printf("completed in %d ticks\n", ticks);
			return true;
		});
}

TORRENT_TEST(session_stats)
{
	std::vector<stats_metric> stats = session_stats_metrics();
	int const downloading_idx = find_metric_idx("ses.num_downloading_torrents");
	TEST_CHECK(downloading_idx >= 0);
	int const incoming_extended_idx = find_metric_idx("ses.num_incoming_extended");
	TEST_CHECK(incoming_extended_idx >= 0);

	setup_swarm(2, swarm_test::download
		// add session
		, [](lt::settings_pack&) {}
		// add torrent
		, [](lt::add_torrent_params&) {}
		// on alert
		, [=](lt::alert const* a, lt::session&)
		{
			auto const* ss = lt::alert_cast<session_stats_alert>(a);
			if (!ss) return;

			// there's one downloading torrent
			TEST_EQUAL(ss->counters()[downloading_idx], 1);
			TEST_EQUAL(ss->counters()[incoming_extended_idx], 1);
		}
		// terminate
		, [](int const ticks, lt::session& ses) -> bool
		{
			ses.post_session_stats();
			if (ticks > 80)
			{
				TEST_ERROR("timeout");
				return true;
			}
			if (!is_seed(ses)) return false;
			std::printf("completed in %d ticks\n", ticks);
			return true;
		});
}

// this test relies on picking up log alerts
#ifndef TORRENT_DISABLE_LOGGING
TORRENT_TEST(suggest)
{
	int num_suggests = 0;
	setup_swarm(10, swarm_test::upload
		// add session
		, [](lt::settings_pack& pack) {
			pack.set_int(settings_pack::suggest_mode, settings_pack::suggest_read_cache);
			pack.set_int(settings_pack::max_suggest_pieces, 10);
#if TORRENT_ABI_VERSION == 1
			pack.set_int(settings_pack::cache_size, 2);
#endif
		}
		// add torrent
		, [](lt::add_torrent_params&) {}
		// on alert
		, [&num_suggests](lt::alert const* a, lt::session&) {
			if (auto pl = alert_cast<peer_log_alert>(a))
			{
				if (pl->direction == peer_log_alert::outgoing_message
					&& pl->event_type == std::string("SUGGEST"))
				{
					++num_suggests;
				}
			}
		}
		// terminate
		, [](int const ticks, lt::session&) -> bool
		{
			if (ticks > 500)
			{
				return true;
			}
			return false;
		});

	// for now, just make sure we send any suggests at all. This feature is
	// experimental and it's not entirely clear it's correct or how to verify
	// that it does what it's supposed to do.
	// perhaps a better way would be to look at piece upload distribution over
	// time
	TEST_CHECK(num_suggests > 0);
}
#endif

TORRENT_TEST(utp_only)
{
	setup_swarm(2, swarm_test::download
		// add session
		, [](lt::settings_pack& pack) {
			pack.set_bool(settings_pack::enable_incoming_utp, true);
			pack.set_bool(settings_pack::enable_outgoing_utp, true);
			pack.set_bool(settings_pack::enable_incoming_tcp, false);
			pack.set_bool(settings_pack::enable_outgoing_tcp, false);
		}
		// add torrent
		, [](lt::add_torrent_params&) {}
		// on alert
		, [](lt::alert const*, lt::session&) {}
		// terminate
		, [](int const ticks, lt::session& ses) -> bool
		{
			if (ticks > 80)
			{
				TEST_ERROR("timeout");
				return true;
			}
			if (!is_seed(ses)) return false;
			return true;
		});
}

void test_stop_start_download(swarm_test_t type, bool graceful)
{
	bool paused_once = false;
	bool resumed = false;

	setup_swarm(3, type
		// add session
		, [](lt::settings_pack& pack) {
			// this test will pause and resume the torrent immediately, we expect
			// to reconnect immediately too, so disable the min reconnect time
			// limit.
			pack.set_int(settings_pack::min_reconnect_time, 0);
		}
		// add torrent
		, [](lt::add_torrent_params&) {

		}
		// on alert
		, [&](lt::alert const* a, lt::session& ses) {

			if (lt::alert_cast<lt::add_torrent_alert>(a))
				add_extra_peers(ses);

			if (auto tp = lt::alert_cast<lt::torrent_paused_alert>(a))
			{
				TEST_EQUAL(resumed, false);
				std::printf("\nSTART\n\n");
				tp->handle.resume();
				resumed = true;
			}
		}
		// terminate
		, [&](int const ticks, lt::session& ses) -> bool
		{
			if (paused_once == false)
			{
				auto st = get_status(ses);
				const bool limit_reached = (type & swarm_test::download)
					? st.total_wanted_done > st.total_wanted / 2
					: st.total_payload_upload >= 3 * 16 * 1024;

				if (limit_reached)
				{
					std::printf("\nSTOP\n\n");
					auto h = ses.get_torrents()[0];
					h.pause(graceful ? torrent_handle::graceful_pause : pause_flags_t{});
					paused_once = true;
				}
			}

			std::printf("tick: %d\n", ticks);

			const int timeout = (type & swarm_test::download) ? 22 : 100;
			if (ticks > timeout)
			{
				TEST_ERROR("timeout");
				return true;
			}
			if (type & swarm_test::upload) return false;
			if (!is_seed(ses)) return false;
			std::printf("completed in %d ticks\n", ticks);
			return true;
		});

	TEST_EQUAL(paused_once, true);
	TEST_EQUAL(resumed, true);
}

TORRENT_TEST(stop_start_download)
{
	test_stop_start_download(swarm_test::download, false);
}

TORRENT_TEST(stop_start_download_graceful)
{
	test_stop_start_download(swarm_test::download, true);
}

TORRENT_TEST(stop_start_download_graceful_no_peers)
{
	bool paused_once = false;
	bool resumed = false;

	setup_swarm(1, swarm_test::download
		// add session
		, [](lt::settings_pack&) {}
		// add torrent
		, [](lt::add_torrent_params&) {}
		// on alert
		, [&](lt::alert const* a, lt::session&) {
			if (auto tp = lt::alert_cast<lt::torrent_paused_alert>(a))
			{
				TEST_EQUAL(resumed, false);
				std::printf("\nSTART\n\n");
				tp->handle.resume();
				resumed = true;
			}
		}
		// terminate
		, [&](int const ticks, lt::session& ses) -> bool
		{
			if (paused_once == false
				&& ticks == 6)
			{
				std::printf("\nSTOP\n\n");
				auto h = ses.get_torrents()[0];
				h.pause(torrent_handle::graceful_pause);
				paused_once = true;
			}

			std::printf("tick: %d\n", ticks);

			// when there's only one node (i.e. no peers) we won't ever download
			// the torrent. It's just a test to make sure we still get the
			// torrent_paused_alert
			return ticks > 60;
		});

	TEST_EQUAL(paused_once, true);
	TEST_EQUAL(resumed, true);
}


TORRENT_TEST(stop_start_seed)
{
	test_stop_start_download(swarm_test::upload, false);
}

TORRENT_TEST(stop_start_seed_graceful)
{
	test_stop_start_download(swarm_test::upload, true);
}

TORRENT_TEST(shutdown)
{
	setup_swarm(4, swarm_test::download
		// add session
		, [](lt::settings_pack&) {}
		// add torrent
		, [](lt::add_torrent_params&) {}
		// on alert
		, [](lt::alert const*, lt::session&) {}
		// terminate
		, [](int, lt::session& ses) -> bool
		{
			if (completed_pieces(ses) == 0) return false;
			TEST_EQUAL(is_seed(ses), false);
			return true;
		});
}

// make the delays on the connections unreasonable long, so libtorrent times-out
// the connection attempts
struct timeout_config : sim::default_config
{
	virtual sim::route incoming_route(lt::address ip) override
	{
		auto it = m_incoming.find(ip);
		if (it != m_incoming.end()) return sim::route().append(it->second);
		it = m_incoming.insert(it, std::make_pair(ip, std::make_shared<queue>(
			m_sim->get_io_context()
			, 1000
			, lt::duration_cast<lt::time_duration>(seconds(10))
			, 1000, "packet-loss modem in")));
		return sim::route().append(it->second);
	}

	virtual sim::route outgoing_route(lt::address ip) override
	{
		auto it = m_outgoing.find(ip);
		if (it != m_outgoing.end()) return sim::route().append(it->second);
		it = m_outgoing.insert(it, std::make_pair(ip, std::make_shared<queue>(
			m_sim->get_io_context(), 1000
			, lt::duration_cast<lt::time_duration>(seconds(5)), 200 * 1000, "packet-loss out")));
		return sim::route().append(it->second);
	}
};

// make sure peers that are no longer alive are handled correctly.
TORRENT_TEST(dead_peers)
{
	int num_connect_timeout = 0;

	timeout_config network_cfg;
	sim::simulation sim{network_cfg};
	setup_swarm(1, swarm_test::download, sim
		// add session
		, [](lt::settings_pack& p) {
			p.set_int(settings_pack::peer_connect_timeout, 1);
		}
		// add torrent
		, [](lt::add_torrent_params& params) {
			params.peers.assign({
				ep("66.66.66.60", 9999)
				, ep("66.66.66.61", 9999)
				, ep("66.66.66.62", 9999)
			});
		}
		// on alert
		, [&](lt::alert const* a, lt::session&) {
			auto* e = alert_cast<peer_disconnected_alert>(a);
			if (e
				&& e->op == operation_t::connect
				&& e->error == error_code(errors::timed_out))
			{
				++num_connect_timeout;
			}
		}
		// terminate
		, [](int t, lt::session&) -> bool
		{ return t > 100; });

	TEST_EQUAL(num_connect_timeout, 3);
}

// the address 50.0.0.1 sits behind a NAT. All of its outgoing connections have
// their source address rewritten to 51.51.51.51
struct nat_config : sim::default_config
{
	nat_config() : m_nat_hop(std::make_shared<nat>(addr("51.51.51.51"))) {}

	sim::route outgoing_route(lt::address ip) override
	{
		// This is extremely simplistic. It will simply alter the perceived source
		// IP of the connecting client.
		sim::route r;
		if (ip == addr("50.0.0.1")) r.append(m_nat_hop);
		return r;
	}
	std::shared_ptr<nat> m_nat_hop;
};

TORRENT_TEST(self_connect)
{
	int num_self_connection_disconnects = 0;

	nat_config network_cfg;
	sim::simulation sim{network_cfg};

	setup_swarm(1, swarm_test::download, sim
		// add session
		, [](lt::settings_pack& p) {
			p.set_bool(settings_pack::enable_incoming_utp, false);
			p.set_bool(settings_pack::enable_outgoing_utp, false);
		}
		// add torrent
		, [](lt::add_torrent_params& params) {
			// this is our own address and listen port, just to make sure we get
			// ourself as a peer (which normally happens one way or another in the
			// wild)
			params.peers.assign({ep("50.0.0.1", 6881)});
		}
		// on alert
		, [&](lt::alert const* a, lt::session&) {
			auto* e = alert_cast<peer_disconnected_alert>(a);
			if (e
				&& e->op == operation_t::bittorrent
				&& e->error == error_code(errors::self_connection))
			{
				++num_self_connection_disconnects;
			}
		}
		// terminate
		, [](int t, lt::session&) -> bool
		{ return t > 100; });

	TEST_EQUAL(num_self_connection_disconnects, 1);
}

TORRENT_TEST(delete_files)
{
	std::string save_path;

	setup_swarm(2, swarm_test::download
		// add session
		, [](lt::settings_pack&) {}
		// add torrent
		, [](lt::add_torrent_params&) {}
		// on alert
		, [](lt::alert const*, lt::session&) {}
		// terminate
		, [&save_path](int, lt::session& ses) -> bool
		{
			if (completed_pieces(ses) == 0) return false;

			auto h = ses.get_torrents()[0];
			save_path = h.status().save_path;
			ses.remove_torrent(h, session::delete_files);
			return true;
		});

	// assert the file is no longer there
	file_status st;
	error_code ec;
	stat_file(combine_path(save_path, "temporary"), &st, ec);
	std::printf("expecting \"%s/temporary\" to NOT exist [%s | %s]\n"
		, save_path.c_str()
		, ec.category().name()
		, ec.message().c_str());
	TEST_EQUAL(ec, error_code(boost::system::errc::no_such_file_or_directory, system_category()));
}

TORRENT_TEST(delete_partfile)
{
	std::string save_path;
	setup_swarm(2, swarm_test::download | swarm_test::real_disk
		// add session
		, [](lt::settings_pack&) {}
		// add torrent
		, [](lt::add_torrent_params&) {}
		// on alert
		, [](lt::alert const*, lt::session&) {}
		// terminate
		, [&save_path](int, lt::session& ses) -> bool
		{
			if (completed_pieces(ses) == 0) return false;

			auto h = ses.get_torrents()[0];
			save_path = h.status().save_path;
			ses.remove_torrent(h, session::delete_partfile);
			return true;
		});
	// assert the file *is* still there
	file_status st;
	error_code ec;
	stat_file(combine_path(save_path, "temporary"), &st, ec);
	std::printf("expecting \"%s/temporary\" to exist [%s]\n", save_path.c_str()
		, ec.message().c_str());
	TEST_CHECK(!ec);
}

TORRENT_TEST(torrent_completed_alert)
{
	int num_file_completed = false;

	setup_swarm(2, swarm_test::download
		// add session
		, [](lt::settings_pack& pack)
		{
			pack.set_int(lt::settings_pack::alert_mask, alert_category::file_progress);
		}
		// add torrent
		, [](lt::add_torrent_params&) {}
		// on alert
		, [&](lt::alert const* a, lt::session&)
		{
			auto tc = alert_cast<lt::file_completed_alert>(a);
			if (tc == nullptr) return;
			++num_file_completed;
		}
		// terminate
		, [](int ticks, lt::session& ses) -> bool
		{
			if (ticks > 80)
			{
				TEST_ERROR("timeout");
				return true;
			}
			if (!is_seed(ses)) return false;
			printf("completed in %d ticks\n", ticks);
			return true;
		});

	TEST_EQUAL(num_file_completed, 1);
}

TORRENT_TEST(block_uploaded_alert)
{
	// blocks[piece count][number of blocks per piece] (each block's element will
	// be set to true when a block_uploaded_alert alert is received for that block)
	std::vector<std::vector<bool>> blocks;

	setup_swarm(2, swarm_test::upload
		// add session
		, [](lt::settings_pack& pack)
		{
			pack.set_int(lt::settings_pack::alert_mask,
				alert_category::upload | alert_category::status);
		}
		// add torrent
		, [](lt::add_torrent_params&) {}
		// on alert
		, [&](lt::alert const* a, lt::session&) {
			if (auto at = lt::alert_cast<lt::add_torrent_alert>(a))
			{
				// init blocks vector, MUST happen before any block_uploaded_alert alerts
				int blocks_per_piece = at->handle.torrent_file()->piece_length() / 0x4000;
				blocks.resize(at->handle.torrent_file()->num_pieces(), std::vector<bool>(blocks_per_piece, false));
			}
			else if (auto ua = lt::alert_cast<lt::block_uploaded_alert>(a))
			{
				TEST_EQUAL(blocks[static_cast<int>(ua->piece_index)][ua->block_index], false);
				blocks[static_cast<int>(ua->piece_index)][ua->block_index] = true;
			}
		}
		// terminate
		, [](int, lt::session&) -> bool
		{ return false; });

		// ensure a block_uploaded_alert was received for each block in the torrent
		TEST_CHECK(std::all_of(blocks.begin(), blocks.end(),
			[](std::vector<bool> const& piece_row) {
				return std::all_of(piece_row.begin(), piece_row.end(),
					[](bool upload_alert_received) {
						return upload_alert_received;
					}
				);
			}
		));
}

// template for testing running swarms with edge case settings
template <typename SettingsFun>
void test_settings(SettingsFun fun)
{
	setup_swarm(2, swarm_test::download
		// add session
		, fun
		// add torrent
		, [](lt::add_torrent_params&) {}
		// on alert
		, [](lt::alert const*, lt::session&) {}
		// terminate
		, [](int ticks, lt::session& ses) -> bool
		{
			if (ticks > 89)
			{
				TEST_ERROR("timeout");
				return true;
			}
			if (!is_seed(ses)) return false;
			return true;
		});
}

TORRENT_TEST(unlimited_connections)
{
	test_settings([](lt::settings_pack& pack) {
		pack.set_int(settings_pack::connections_limit, std::numeric_limits<int>::max()); }
	);
}

TORRENT_TEST(default_connections_limit)
{
	test_settings([](lt::settings_pack& pack) {
		pack.set_int(settings_pack::connections_limit, 0); }
	);
}

TORRENT_TEST(default_connections_limit_negative)
{
	test_settings([](lt::settings_pack& pack) {
		pack.set_int(settings_pack::connections_limit, -1); }
	);
}


TORRENT_TEST(redundant_have)
{
	test_settings([](lt::settings_pack& pack) {
		pack.set_bool(settings_pack::send_redundant_have, false); }
	);
}

#if TORRENT_ABI_VERSION == 1
TORRENT_TEST(lazy_bitfields)
{
	test_settings([](lt::settings_pack& pack) {
		pack.set_bool(settings_pack::lazy_bitfields, true); }
	);
}
#endif

TORRENT_TEST(prioritize_partial_pieces)
{
	test_settings([](lt::settings_pack& pack) {
		pack.set_bool(settings_pack::prioritize_partial_pieces, true); }
	);
}

TORRENT_TEST(active_downloads)
{
	test_settings([](lt::settings_pack& pack) {
		pack.set_int(settings_pack::active_downloads, std::numeric_limits<int>::max()); }
	);
}

TORRENT_TEST(active_seeds)
{
	test_settings([](lt::settings_pack& pack) {
		pack.set_int(settings_pack::active_seeds, std::numeric_limits<int>::max()); }
	);
}

TORRENT_TEST(active_seeds_negative)
{
	test_settings([](lt::settings_pack& pack) {
		pack.set_int(settings_pack::active_seeds, -1); }
	);
}

TORRENT_TEST(active_limit)
{
	test_settings([](lt::settings_pack& pack) {
		pack.set_int(settings_pack::active_limit, std::numeric_limits<int>::max()); }
	);
}

TORRENT_TEST(active_limit_negative)
{
	test_settings([](lt::settings_pack& pack) {
		pack.set_int(settings_pack::active_limit, -1); }
	);
}

TORRENT_TEST(upload_rate_limit)
{
	test_settings([](lt::settings_pack& pack) {
		pack.set_int(settings_pack::upload_rate_limit, std::numeric_limits<int>::max()); }
	);
}

TORRENT_TEST(upload_rate_limit_negative)
{
	test_settings([](lt::settings_pack& pack) {
		pack.set_int(settings_pack::upload_rate_limit, -1); }
	);
}

TORRENT_TEST(download_rate_limit)
{
	test_settings([](lt::settings_pack& pack) {
		pack.set_int(settings_pack::download_rate_limit, std::numeric_limits<int>::max()); }
	);
}

TORRENT_TEST(download_rate_limit_negative)
{
	test_settings([](lt::settings_pack& pack) {
		pack.set_int(settings_pack::download_rate_limit, -1); }
	);
}

TORRENT_TEST(unchoke_slots_limit)
{
	test_settings([](lt::settings_pack& pack) {
		pack.set_int(settings_pack::unchoke_slots_limit, std::numeric_limits<int>::max()); }
	);
}

TORRENT_TEST(unchoke_slots_limit_negative)
{
	test_settings([](lt::settings_pack& pack) {
		pack.set_int(settings_pack::unchoke_slots_limit, -1);
		pack.set_int(settings_pack::choking_algorithm, settings_pack::fixed_slots_choker);
	});
}

TORRENT_TEST(settings_stress_test)
{
	std::array<int, 11> const settings{{
		settings_pack::unchoke_slots_limit,
		settings_pack::connections_limit,
		settings_pack::predictive_piece_announce,
		settings_pack::allow_multiple_connections_per_ip,
		settings_pack::send_redundant_have,
		settings_pack::rate_limit_ip_overhead,
		settings_pack::rate_limit_ip_overhead,
		settings_pack::anonymous_mode,
//		settings_pack::enable_upnp,
//		settings_pack::enable_natpmp,
		settings_pack::enable_lsd,
		settings_pack::enable_ip_notifier,
		settings_pack::piece_extent_affinity,
	}};
	std::array<int, 4> const values{{-1, 0, 1, std::numeric_limits<int>::max()}};

	for (auto t : { swarm_test::download, swarm_test::upload})
	{
		for (auto s1 : settings)
		{
			for (auto s2 : settings)
			{
				if (s1 == s2) continue;

				setup_swarm(2, t
					// add session
					, [](lt::settings_pack& p) {
					p.set_int(settings_pack::choking_algorithm, settings_pack::fixed_slots_choker);
					}
					// add torrent
					, [](lt::add_torrent_params& params) {}
					// on alert
					, [](lt::alert const*, lt::session&) {}
					// terminate
					, [&](int tick, lt::session& session) -> bool
					{
						int const s = (tick & 1) ? s2 : s1;
						settings_pack p;
						if ((s & settings_pack::type_mask) == settings_pack::bool_type_base)
							p.set_bool(s, bool(tick & 2));
						else
							p.set_int(s, values[(tick >> 1) % values.size()]);
						session.apply_settings(std::move(p));
						return tick > int(settings.size() * values.size() * 2);
					});
			}
		}
	}
}

TORRENT_TEST(pex)
{
	// we create 3 nodes. Node 0 seeds and node 1 and 2 are downloaders.
	// node 0 is initially only connected to node 1. The test ensures that node
	// 0 eventually is connected to node 2, as it should have been introduced
	// via PEX

	dsl_config network_cfg;
	sim::simulation sim{network_cfg};

	asio::io_context ios(sim);
	lt::time_point start_time(lt::clock_type::now());

	std::vector<std::shared_ptr<lt::session>> nodes;
	std::vector<std::shared_ptr<sim::asio::io_context>> io_service;
	std::vector<lt::session_proxy> zombies;
	lt::deadline_timer timer(ios);

	lt::error_code ec;
	int const swarm_id = unit_test::test_counter();
	std::string path = save_path(swarm_id, 0);

	lt::create_directory(path, ec);
	if (ec) std::printf("failed to create directory: \"%s\": %s\n"
		, path.c_str(), ec.message().c_str());
	std::ofstream file(lt::combine_path(path, "temporary").c_str());
	auto ti = ::create_torrent(&file, "temporary", 0x4000, 50, false);
	file.close();

	int const num_nodes = 3;

	bool done = false;

	// session 0 is the seeding one.
	// the IPs are 50.0.0.1, 50.0.0.2 and 50.0.0.3
	for (int i = 0; i < num_nodes; ++i)
	{
		// create a new io_service
		char ep[30];
		std::snprintf(ep, sizeof(ep), "50.0.%d.%d", (i + 1) >> 8, (i + 1) & 0xff);
		io_service.push_back(std::make_shared<sim::asio::io_context>(sim, addr(ep)));

		lt::settings_pack pack = settings();

		// make sure the sessions have different peer ids
		lt::peer_id pid;
		lt::aux::random_bytes(pid);
		pack.set_str(lt::settings_pack::peer_fingerprint, pid.to_string());
		std::shared_ptr<lt::session> ses =
			std::make_shared<lt::session>(pack, *io_service.back());
		nodes.push_back(ses);

		lt::add_torrent_params p;
		p.flags &= ~lt::torrent_flags::paused;
		p.flags &= ~lt::torrent_flags::auto_managed;

		// node 0 and 1 are downloaders and node 2 is a seed
		// save path 0 is where the files are, so that's for seeds
		// It's important that node 1 and 2 want to stay connected, otherwise
		// node 1 won't be able to gossip about 2 to 0.
		p.save_path = save_path(swarm_id, i > 1 ? 0 : 1);
		p.ti = ti;
		ses->async_add_torrent(p);

		ses->set_alert_notify([&, i]() {
			// this function is called inside libtorrent and we cannot perform work
			// immediately in it. We have to notify the outside to pull all the alerts
			post(*io_service[i], [&,i]()
			{
				lt::session* ses = nodes[i].get();

				// when shutting down, we may have destructed the session
				if (ses == nullptr) return;

				std::vector<lt::alert*> alerts;
				ses->pop_alerts(&alerts);

				for (lt::alert* a : alerts)
				{
					// only print alerts from the session under test
					lt::time_duration d = a->timestamp() - start_time;
					std::uint32_t const millis = std::uint32_t(
						lt::duration_cast<lt::milliseconds>(d).count());

					if (i == 0) {
					std::printf("%4u.%03u: %-25s %s\n"
						, millis / 1000, millis % 1000
						, a->what()
						, a->message().c_str());
					}

					// if a torrent was added save the torrent handle
					if (lt::add_torrent_alert* at = lt::alert_cast<lt::add_torrent_alert>(a))
					{
						lt::torrent_handle h = at->handle;

						if (i == 0)
						{
							// node only connects to node 1
							h.connect_peer(lt::tcp::endpoint(addr("50.0.0.2"), 6881));
						}
						else
						{
							// other nodes connect to each other
							for (int k = 1; k < num_nodes; ++k)
							{
								char ep[30];
								std::snprintf(ep, sizeof(ep), "50.0.%d.%d"
									, (k + 1) >> 8, (k + 1) & 0xff);
								h.connect_peer(lt::tcp::endpoint(addr(ep), 6881));
							}
						}
					}

					if (i == 0)
					{
						// if node 0 was connected to 50.0.0.3, we're done
						if (lt::peer_connect_alert* ca = lt::alert_cast<lt::peer_connect_alert>(a))
						{
							if (ca->endpoint.address() == addr("50.0.0.3"))
								done = true;
						}
						if (lt::incoming_connection_alert* ca = lt::alert_cast<lt::incoming_connection_alert>(a))
						{
							if (ca->endpoint.address() == addr("50.0.0.3"))
								done = true;
						}
					}
				}
			});
		});
	}

	std::function<void(lt::error_code const&)> on_done
		= [&](lt::error_code const& ec)
	{
		if (ec) return;

		std::printf("TERMINATING\n");

		// terminate simulation
		for (int i = 0; i < int(nodes.size()); ++i)
		{
			zombies.push_back(nodes[i]->abort());
			nodes[i].reset();
		}
	};

	timer.expires_after(lt::seconds(65));
	timer.async_wait(on_done);

	sim.run();

	TEST_EQUAL(done, true);
}

// TODO: add test that makes sure a torrent in graceful pause mode won't make
// outgoing connections
// TODO: add test that makes sure a torrent in graceful pause mode won't accept
// incoming connections
// TODO: test the different storage allocation modes
// TODO: test contiguous buffer