@@ -361,6 +361,90 @@ void on_alert_notify(lt::session* ses)
361
361
});
362
362
}
363
363
364
+ void test_announce ()
365
+ {
366
+ using sim::asio::ip::address_v4;
367
+ sim::default_config network_cfg;
368
+ sim::simulation sim{network_cfg};
369
+
370
+ sim::asio::io_context web_server (sim, make_address_v4 (" 2.2.2.2" ));
371
+
372
+ // listen on port 8080
373
+ sim::http_server http (web_server, 8080 );
374
+
375
+ int announces = 0 ;
376
+
377
+ // expect announced IP & port
378
+ std::string const expect_port = " &port=1234" ;
379
+ std::string const expect_ip = " &ip=1.2.3.4" ;
380
+
381
+ http.register_handler (" /announce"
382
+ , [&announces, expect_port, expect_ip](std::string method, std::string req
383
+ , std::map<std::string, std::string>&)
384
+ {
385
+ ++announces;
386
+ TEST_EQUAL (method, " GET" );
387
+ TEST_CHECK (req.find (expect_port) != std::string::npos);
388
+ TEST_CHECK (req.find (expect_ip) != std::string::npos);
389
+ char response[500 ];
390
+ int const size = std::snprintf (response, sizeof (response), " d8:intervali1800e5:peers0:e" );
391
+ return sim::send_response (200 , " OK" , size) + response;
392
+ });
393
+
394
+ {
395
+ lt::session_proxy zombie;
396
+
397
+ std::vector<asio::ip::address> ips;
398
+ ips.push_back (make_address (" 123.0.0.3" ));
399
+
400
+ asio::io_context ios (sim, ips);
401
+ lt::settings_pack sett = settings ();
402
+ sett.set_str (settings_pack::listen_interfaces, " 0.0.0.0:6881" );
403
+ sett.set_str (settings_pack::announce_ip, " 1.2.3.4" );
404
+ sett.set_int (settings_pack::announce_port, 1234 );
405
+
406
+ auto ses = std::make_unique<lt::session>(sett, ios);
407
+
408
+ ses->set_alert_notify (std::bind (&on_alert_notify, ses.get ()));
409
+
410
+ lt::add_torrent_params p;
411
+ p.name = " test-torrent" ;
412
+ p.save_path = " ." ;
413
+ p.info_hashes .v1 .assign (" abababababababababab" );
414
+
415
+ p.trackers .push_back (" http://2.2.2.2:8080/announce" );
416
+ ses->async_add_torrent (p);
417
+
418
+ // stop the torrent 5 seconds in
419
+ sim::timer t1 (sim, lt::seconds (5 )
420
+ , [&ses](boost::system ::error_code const &)
421
+ {
422
+ std::vector<lt::torrent_handle> torrents = ses->get_torrents ();
423
+ for (auto const & t : torrents)
424
+ {
425
+ t.pause ();
426
+ }
427
+ });
428
+
429
+ // then shut down 10 seconds in
430
+ sim::timer t2 (sim, lt::seconds (10 )
431
+ , [&ses,&zombie](boost::system ::error_code const &)
432
+ {
433
+ zombie = ses->abort ();
434
+ ses.reset ();
435
+ });
436
+
437
+ sim.run ();
438
+ }
439
+
440
+ TEST_EQUAL (announces, 2 );
441
+ }
442
+
443
+ // this test makes sure that a seed can overwrite its announced IP & port
444
+ TORRENT_TEST (announce_ip_port) {
445
+ test_announce ();
446
+ }
447
+
364
448
static const int num_interfaces = 3 ;
365
449
366
450
void test_ipv6_support (char const * listen_interfaces
0 commit comments