@@ -97,8 +97,8 @@ mod for_all_config_modes {
9797 use crate :: common:: fixtures:: invalid_info_hashes;
9898 use crate :: servers:: http:: asserts:: {
9999 assert_announce_response, assert_bad_announce_request_error_response, assert_cannot_parse_query_param_error_response,
100- assert_cannot_parse_query_params_error_response, assert_compact_announce_response, assert_empty_announce_response ,
101- assert_is_announce_response , assert_missing_query_params_for_announce_request_error_response,
100+ assert_cannot_parse_query_params_error_response, assert_compact_announce_response, assert_is_announce_response ,
101+ assert_missing_query_params_for_announce_request_error_response,
102102 } ;
103103 use crate :: servers:: http:: client:: Client ;
104104 use crate :: servers:: http:: requests:: announce:: { Compact , QueryBuilder } ;
@@ -497,26 +497,71 @@ mod for_all_config_modes {
497497 env. stop ( ) . await ;
498498 }
499499
500+ /// code-review (da2ce7): is this really intended behavior?
500501 #[ tokio:: test]
501502 async fn should_consider_two_peers_to_be_the_same_when_they_have_the_same_peer_id_even_if_the_ip_is_different ( ) {
502503 let env = Started :: new ( & configuration:: ephemeral_mode_public ( ) . into ( ) ) . await ;
503504
504- let info_hash = InfoHash :: from_str ( "9c38422213e30bff212b30c360d26f9a02136422" ) . unwrap ( ) ;
505- let peer = PeerBuilder :: default ( ) . build ( ) ;
505+ let announce_policy = env. tracker . get_announce_policy ( ) ;
506506
507- // Add a peer
508- env. add_torrent_peer ( & info_hash, & peer) . await ;
507+ let info_hash = InfoHash :: from ( [ 0 ; 20 ] ) ;
508+ let id_a = peer:: Id ( * b"-qB00000000000000000" ) ;
509+ let id_b = peer:: Id ( * b"-qB00000000000000001" ) ;
509510
510- let announce_query = QueryBuilder :: default ( )
511- . with_info_hash ( & info_hash)
512- . with_peer_id ( & peer. peer_id )
513- . query ( ) ;
511+ let addr_a = SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 126 , 0 , 0 , 1 ) ) , 8080 ) ;
512+ let addr_b = SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 126 , 0 , 0 , 2 ) ) , 8080 ) ;
513+ let addr_c = SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 126 , 0 , 0 , 3 ) ) , 8080 ) ;
514+
515+ let peer_a = PeerBuilder :: default ( ) . with_peer_id ( & id_a) . with_peer_addr ( & addr_a) . build ( ) ;
516+ let peer_b = PeerBuilder :: default ( ) . with_peer_id ( & id_b) . with_peer_addr ( & addr_b) . build ( ) ;
517+
518+ // Add both Peers into DB.
519+ env. add_torrent_peer ( & info_hash, & peer_a) . await ;
520+ env. add_torrent_peer ( & info_hash, & peer_b) . await ;
521+
522+ // The first query will overwrite the IP of the peer, no matter what ip we use.
523+ {
524+ let announce = QueryBuilder :: default ( )
525+ . with_info_hash ( & info_hash)
526+ . with_peer_id ( & id_a)
527+ . with_peer_addr ( & addr_c. ip ( ) ) // different ip, but this is erased.
528+ . query ( ) ;
529+
530+ let response = Client :: new ( * env. bind_address ( ) ) . announce ( & announce) . await ;
531+
532+ let response_assert = Announce {
533+ complete : 2 ,
534+ incomplete : 0 ,
535+ interval : announce_policy. interval ,
536+ min_interval : announce_policy. interval_min ,
537+ peers : vec ! [ peer_b. into( ) ] , // peer_b still has it's original ip.
538+ } ;
539+
540+ println ! ( "Query 1" ) ;
541+ assert_announce_response ( response, & response_assert) . await ;
542+ }
543+
544+ // The Seconds Query will result with no listed peers, as both peer id's are now
545+ // associated with the same client ip. i.e (0.0.0.0).
546+ {
547+ let announce = QueryBuilder :: default ( )
548+ . with_info_hash ( & info_hash)
549+ . with_peer_id ( & id_b) // now we use peer b
550+ . query ( ) ;
514551
515- assert_ne ! ( peer . peer_addr . ip ( ) , announce_query . peer_addr ) ;
552+ let response = Client :: new ( * env . bind_address ( ) ) . announce ( & announce ) . await ;
516553
517- let response = Client :: new ( * env. bind_address ( ) ) . announce ( & announce_query) . await ;
554+ let response_assert = Announce {
555+ complete : 2 ,
556+ incomplete : 0 ,
557+ interval : announce_policy. interval ,
558+ min_interval : announce_policy. interval_min ,
559+ peers : vec ! [ ] , // peer_a now has host ip.
560+ } ;
518561
519- assert_empty_announce_response ( response) . await ;
562+ println ! ( "Query 2" ) ;
563+ assert_announce_response ( response, & response_assert) . await ;
564+ }
520565
521566 env. stop ( ) . await ;
522567 }
0 commit comments