@@ -457,6 +457,116 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
457457 /* protected_peer_ids=*/ {0 , 1 , 2 , 3 , 4 , 5 , 7 , 8 , 11 , 12 , 16 , 17 },
458458 /* unprotected_peer_ids=*/ {6 , 9 , 10 , 13 , 14 , 15 , 18 , 19 , 20 , 21 , 22 , 23 },
459459 random_context));
460+
461+ // Tests with 4 networks...
462+
463+ // Combined test: expect having 1 CJDNS, 1 I2P, 1 localhost and 1 onion peer
464+ // out of 5 to protect 1 CJDNS, 0 I2P, 0 localhost, 0 onion and 1 other peer
465+ // (2 total), sorted by longest uptime; stable sort breaks tie with array
466+ // order of CJDNS first.
467+ BOOST_CHECK (IsProtected (
468+ 5 , [](NodeEvictionCandidate& c) {
469+ c.m_connected = std::chrono::seconds{c.id };
470+ c.m_is_local = (c.id == 3 );
471+ if (c.id == 4 ) {
472+ c.m_network = NET_CJDNS;
473+ } else if (c.id == 1 ) {
474+ c.m_network = NET_I2P;
475+ } else if (c.id == 2 ) {
476+ c.m_network = NET_ONION;
477+ } else {
478+ c.m_network = NET_IPV6;
479+ }
480+ },
481+ /* protected_peer_ids */ {0 , 4 },
482+ /* unprotected_peer_ids */ {1 , 2 , 3 },
483+ random_context));
484+
485+ // Combined test: expect having 1 CJDNS, 1 I2P, 1 localhost and 1 onion peer
486+ // out of 7 to protect 1 CJDNS, 0, I2P, 0 localhost, 0 onion and 2 other
487+ // peers (3 total) sorted by longest uptime; stable sort breaks tie with
488+ // array order of CJDNS first.
489+ BOOST_CHECK (IsProtected (
490+ 7 , [](NodeEvictionCandidate& c) {
491+ c.m_connected = std::chrono::seconds{c.id };
492+ c.m_is_local = (c.id == 4 );
493+ if (c.id == 6 ) {
494+ c.m_network = NET_CJDNS;
495+ } else if (c.id == 5 ) {
496+ c.m_network = NET_I2P;
497+ } else if (c.id == 3 ) {
498+ c.m_network = NET_ONION;
499+ } else {
500+ c.m_network = NET_IPV4;
501+ }
502+ },
503+ /* protected_peer_ids=*/ {0 , 1 , 6 },
504+ /* unprotected_peer_ids=*/ {2 , 3 , 4 , 5 },
505+ random_context));
506+
507+ // Combined test: expect having 1 CJDNS, 1 I2P, 1 localhost and 1 onion peer
508+ // out of 8 to protect 1 CJDNS, 1 I2P, 0 localhost, 0 onion and 2 other
509+ // peers (4 total) sorted by longest uptime; stable sort breaks tie with
510+ // array order of CJDNS first.
511+ BOOST_CHECK (IsProtected (
512+ 8 , [](NodeEvictionCandidate& c) {
513+ c.m_connected = std::chrono::seconds{c.id };
514+ c.m_is_local = (c.id == 3 );
515+ if (c.id == 5 ) {
516+ c.m_network = NET_CJDNS;
517+ } else if (c.id == 6 ) {
518+ c.m_network = NET_I2P;
519+ } else if (c.id == 3 ) {
520+ c.m_network = NET_ONION;
521+ } else {
522+ c.m_network = NET_IPV6;
523+ }
524+ },
525+ /* protected_peer_ids=*/ {0 , 1 , 5 , 6 },
526+ /* unprotected_peer_ids=*/ {2 , 3 , 4 , 7 },
527+ random_context));
528+
529+ // Combined test: expect having 2 CJDNS, 2 I2P, 4 localhost, and 2 onion
530+ // peers out of 16 to protect 1 CJDNS, 1 I2P, 1 localhost, 1 onion (4/16
531+ // total), plus 4 others for 8 total, sorted by longest uptime.
532+ BOOST_CHECK (IsProtected (
533+ 16 , [](NodeEvictionCandidate& c) {
534+ c.m_connected = std::chrono::seconds{c.id };
535+ c.m_is_local = (c.id > 5 );
536+ if (c.id == 11 || c.id == 15 ) {
537+ c.m_network = NET_CJDNS;
538+ } else if (c.id == 10 || c.id == 14 ) {
539+ c.m_network = NET_I2P;
540+ } else if (c.id == 8 || c.id == 9 ) {
541+ c.m_network = NET_ONION;
542+ } else {
543+ c.m_network = NET_IPV4;
544+ }
545+ },
546+ /* protected_peer_ids=*/ {0 , 1 , 2 , 3 , 6 , 8 , 10 , 11 },
547+ /* unprotected_peer_ids=*/ {4 , 5 , 7 , 9 , 12 , 13 , 14 , 15 },
548+ random_context));
549+
550+ // Combined test: expect having 6 CJDNS, 1 I2P, 1 localhost, and 4 onion
551+ // peers out of 24 to protect 2 CJDNS, 1 I2P, 1 localhost, and 2 onions (6
552+ // total), plus 6 others for 12/24 total, sorted by longest uptime.
553+ BOOST_CHECK (IsProtected (
554+ 24 , [](NodeEvictionCandidate& c) {
555+ c.m_connected = std::chrono::seconds{c.id };
556+ c.m_is_local = (c.id == 13 );
557+ if (c.id > 17 ) {
558+ c.m_network = NET_CJDNS;
559+ } else if (c.id == 17 ) {
560+ c.m_network = NET_I2P;
561+ } else if (c.id == 12 || c.id == 14 || c.id == 15 || c.id == 16 ) {
562+ c.m_network = NET_ONION;
563+ } else {
564+ c.m_network = NET_IPV6;
565+ }
566+ },
567+ /* protected_peer_ids=*/ {0 , 1 , 2 , 3 , 4 , 5 , 12 , 13 , 14 , 17 , 18 , 19 },
568+ /* unprotected_peer_ids=*/ {6 , 7 , 8 , 9 , 10 , 11 , 15 , 16 , 20 , 21 , 22 , 23 },
569+ random_context));
460570}
461571
462572// Returns true if any of the node ids in node_ids are selected for eviction.
0 commit comments