p2p_transport_channel_unittest : put frequently used checks into functions.

this patch is puts frequently used check into a set of Check-functions.

the behavior of p2p_transport_channel_unittest is almost unchanged,
the minor change is that when waiting for connection between specific
addresses it waits and does not assume that a particular set of
local/remote addresses will be selected first.

the patch also changes a few EXPECT_ to ASSERT_ since the
tests are not useful where the first EXPECT fails.

BUG=webrtc:10647

Change-Id: Iddcc3c88114db80576e9ebc500572a00dbafdd84
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/159882
Commit-Queue: Jonas Oreland <jonaso@webrtc.org>
Reviewed-by: Honghai Zhang <honghaiz@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29812}
diff --git a/p2p/base/p2p_transport_channel_unittest.cc b/p2p/base/p2p_transport_channel_unittest.cc
index 7603018..f906000 100644
--- a/p2p/base/p2p_transport_channel_unittest.cc
+++ b/p2p/base/p2p_transport_channel_unittest.cc
@@ -564,6 +564,37 @@
     EXPECT_EQ(expected.controlling_protocol, remote_protocol);
   }
 
+  static bool CheckCandidate(P2PTransportChannel* channel,
+                             SocketAddress from,
+                             SocketAddress to) {
+    auto local_candidate = LocalCandidate(channel);
+    auto remote_candidate = RemoteCandidate(channel);
+    return local_candidate != nullptr &&
+           local_candidate->address().EqualIPs(from) &&
+           remote_candidate != nullptr &&
+           remote_candidate->address().EqualIPs(to);
+  }
+
+  static bool CheckCandidatePair(P2PTransportChannel* ch1,
+                                 P2PTransportChannel* ch2,
+                                 SocketAddress from,
+                                 SocketAddress to) {
+    return CheckCandidate(ch1, from, to) && CheckCandidate(ch2, to, from);
+  }
+
+  static bool CheckConnected(P2PTransportChannel* ch1,
+                             P2PTransportChannel* ch2) {
+    return ch1 != nullptr && ch1->receiving() && ch1->writable() &&
+           ch2 != nullptr && ch2->receiving() && ch2->writable();
+  }
+
+  static bool CheckCandidatePairAndConnected(P2PTransportChannel* ch1,
+                                             P2PTransportChannel* ch2,
+                                             SocketAddress from,
+                                             SocketAddress to) {
+    return CheckConnected(ch1, ch2) && CheckCandidatePair(ch1, ch2, from, to);
+  }
+
   void Test(const Result& expected) {
     rtc::ScopedFakeClock clock;
     int64_t connect_start = rtc::TimeMillis();
@@ -571,11 +602,8 @@
 
     // Create the channels and wait for them to connect.
     CreateChannels();
-    EXPECT_TRUE_SIMULATED_WAIT(
-        ep1_ch1() != NULL && ep2_ch1() != NULL && ep1_ch1()->receiving() &&
-            ep1_ch1()->writable() && ep2_ch1()->receiving() &&
-            ep2_ch1()->writable(),
-        expected.connect_wait + kShortTimeout, clock);
+    EXPECT_TRUE_SIMULATED_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()),
+                               expected.connect_wait + kShortTimeout, clock);
     connect_time = rtc::TimeMillis() - connect_start;
     if (connect_time < expected.connect_wait) {
       RTC_LOG(LS_INFO) << "Connect time: " << connect_time << " ms";
@@ -595,7 +623,7 @@
       // EP1 result matrix.
       EXPECT_TRUE_SIMULATED_WAIT(
           CheckCandidate1(expected) && CheckCandidate2(expected),
-          kMediumTimeout, clock);
+          kDefaultTimeout, clock);
       // Also do EXPECT_EQ on each part so that failures are more verbose.
       ExpectCandidate1(expected);
       ExpectCandidate2(expected);
@@ -605,7 +633,7 @@
       if (converge_time < converge_wait) {
         RTC_LOG(LS_INFO) << "Converge time: " << converge_time << " ms";
       } else {
-        RTC_LOG(LS_INFO) << "Converge time: TIMEOUT (" << converge_wait
+        RTC_LOG(LS_INFO) << "Converge time: TIMEOUT (" << converge_time
                          << " ms)";
       }
     }
@@ -641,10 +669,8 @@
     rtc::ScopedFakeClock clock;
     ep1_ch1()->SetRemoteIceParameters(kIceParams[1]);
     ep2_ch1()->SetRemoteIceParameters(kIceParams[0]);
-    EXPECT_TRUE_SIMULATED_WAIT(
-        ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-            ep2_ch1()->receiving() && ep2_ch1()->writable(),
-        kMediumTimeout, clock);
+    EXPECT_TRUE_SIMULATED_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()),
+                               kMediumTimeout, clock);
 
     const Candidate* old_local_candidate1 = LocalCandidate(ep1_ch1());
     const Candidate* old_local_candidate2 = LocalCandidate(ep2_ch1());
@@ -690,10 +716,8 @@
     EXPECT_TRUE_SIMULATED_WAIT(GetRoleConflict(0), kShortTimeout, clock);
     EXPECT_FALSE(GetRoleConflict(1));
 
-    EXPECT_TRUE_SIMULATED_WAIT(
-        ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-            ep2_ch1()->receiving() && ep2_ch1()->writable(),
-        kShortTimeout, clock);
+    EXPECT_TRUE_SIMULATED_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()),
+                               kShortTimeout, clock);
 
     EXPECT_TRUE(ep1_ch1()->selected_connection() &&
                 ep2_ch1()->selected_connection());
@@ -1260,9 +1284,7 @@
   ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts);
 
   CreateChannels();
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable(),
+  EXPECT_TRUE_SIMULATED_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()),
                              kDefaultTimeout, clock);
 
   // Drop all packets so that both channels become not writable.
@@ -1295,9 +1317,7 @@
   ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts);
 
   CreateChannels();
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable(),
+  EXPECT_TRUE_SIMULATED_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()),
                              kDefaultTimeout, clock);
 
   ep1_ch1()->SetIceParameters(kIceParams[2]);
@@ -1356,9 +1376,7 @@
   IceConfig default_config;
   CreateChannels(continual_gathering_config, default_config);
 
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable(),
+  EXPECT_TRUE_SIMULATED_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()),
                              kDefaultTimeout, clock);
 
   // Adding address in ep1 will trigger continual gathering.
@@ -1395,9 +1413,7 @@
   config2.regather_on_failed_networks_interval = 2000;
   CreateChannels(config1, config2);
 
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable(),
+  EXPECT_TRUE_SIMULATED_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()),
                              kDefaultTimeout, clock);
 
   fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]);
@@ -1429,9 +1445,7 @@
   IceConfig config2;
   CreateChannels(config1, config2);
 
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable(),
+  EXPECT_TRUE_SIMULATED_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()),
                              kDefaultTimeout, clock);
 
   fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]);
@@ -1479,10 +1493,8 @@
     CreateChannels(config1, config2);
 
     // Wait for initial connection to be made.
-    EXPECT_TRUE_SIMULATED_WAIT(
-        ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-            ep2_ch1()->receiving() && ep2_ch1()->writable(),
-        kMediumTimeout, clock);
+    EXPECT_TRUE_SIMULATED_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()),
+                               kMediumTimeout, clock);
 
     const Connection* initial_selected = ep1_ch1()->selected_connection();
 
@@ -1686,9 +1698,7 @@
   // Wait for the initial connection to be made.
   ep1_ch1()->SetRemoteIceParameters(kIceParams[1]);
   ep2_ch1()->SetRemoteIceParameters(kIceParams[0]);
-  EXPECT_TRUE_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                       ep2_ch1()->receiving() && ep2_ch1()->writable(),
-                   kDefaultTimeout);
+  EXPECT_TRUE_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()), kDefaultTimeout);
 
   // Simulate an ICE restart on ep2, but don't signal the candidate or new
   // ICE parameters until after a prflx connection has been made.
@@ -1775,11 +1785,8 @@
   CreateChannels();
   ep1_ch1()->set_incoming_only(true);
 
-  EXPECT_TRUE_SIMULATED_WAIT(
-      ep1_ch1() != NULL && ep2_ch1() != NULL && ep1_ch1()->receiving() &&
-          ep1_ch1()->writable() && ep2_ch1()->receiving() &&
-          ep2_ch1()->writable(),
-      kMediumTimeout, clock);
+  EXPECT_TRUE_SIMULATED_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()),
+                             kMediumTimeout, clock);
 
   DestroyChannels();
 }
@@ -1846,14 +1853,10 @@
   ResumeCandidates(0);
   ResumeCandidates(1);
 
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable(),
-                             kShortTimeout, clock);
-  EXPECT_TRUE(ep1_ch1()->selected_connection() &&
-              ep2_ch1()->selected_connection() &&
-              LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) &&
-              RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1]));
+  EXPECT_TRUE_SIMULATED_WAIT(
+      CheckCandidatePairAndConnected(ep1_ch1(), ep2_ch1(), kPublicAddrs[0],
+                                     kPublicAddrs[1]),
+      kShortTimeout, clock);
 
   TestSendRecv(&clock);
   DestroyChannels();
@@ -1900,9 +1903,7 @@
     EXPECT_EQ(kHighTiebreaker, ports_before[i]->IceTiebreaker());
   }
 
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable(),
+  EXPECT_TRUE_SIMULATED_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()),
                              kShortTimeout, clock);
 
   EXPECT_TRUE(ep1_ch1()->selected_connection() &&
@@ -1949,14 +1950,10 @@
 
   CreateChannels();
 
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable(),
-                             kShortTimeout, clock);
-  EXPECT_TRUE(
-      ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection() &&
-      LocalCandidate(ep1_ch1())->address().EqualIPs(kIPv6PublicAddrs[0]) &&
-      RemoteCandidate(ep1_ch1())->address().EqualIPs(kIPv6PublicAddrs[1]));
+  EXPECT_TRUE_SIMULATED_WAIT(
+      CheckCandidatePairAndConnected(ep1_ch1(), ep2_ch1(), kIPv6PublicAddrs[0],
+                                     kIPv6PublicAddrs[1]),
+      kShortTimeout, clock);
 
   TestSendRecv(&clock);
   DestroyChannels();
@@ -1976,9 +1973,7 @@
 
   CreateChannels();
 
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable(),
+  EXPECT_TRUE_SIMULATED_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()),
                              kMediumTimeout, clock);
 
   EXPECT_TRUE(ep1_ch1()->selected_connection() &&
@@ -2007,11 +2002,8 @@
   IceConfig default_config;
   CreateChannels(continual_gathering_config, default_config);
 
-  EXPECT_TRUE_SIMULATED_WAIT(
-      ep1_ch1() != NULL && ep2_ch1() != NULL && ep1_ch1()->receiving() &&
-          ep1_ch1()->writable() && ep2_ch1()->receiving() &&
-          ep2_ch1()->writable(),
-      kMediumTimeout, clock);
+  EXPECT_TRUE_SIMULATED_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()),
+                             kMediumTimeout, clock);
   SIMULATED_WAIT(
       IceGatheringState::kIceGatheringComplete == ep1_ch1()->gathering_state(),
       kShortTimeout, clock);
@@ -2053,11 +2045,8 @@
   EXPECT_TRUE(pooled_session_2->ReadyCandidates().empty());
   // Now let the endpoints connect and try exchanging some data.
   CreateChannels();
-  EXPECT_TRUE_SIMULATED_WAIT(
-      ep1_ch1() != NULL && ep2_ch1() != NULL && ep1_ch1()->receiving() &&
-          ep1_ch1()->writable() && ep2_ch1()->receiving() &&
-          ep2_ch1()->writable(),
-      kMediumTimeout, clock);
+  EXPECT_TRUE_SIMULATED_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()),
+                             kMediumTimeout, clock);
   TestSendRecv(&clock);
   // Make sure the P2PTransportChannels are actually using ports from the
   // pooled sessions.
@@ -2098,11 +2087,8 @@
                              kDefaultTimeout, clock);
   // Now let the endpoints connect and try exchanging some data.
   CreateChannels();
-  EXPECT_TRUE_SIMULATED_WAIT(
-      ep1_ch1() != NULL && ep2_ch1() != NULL && ep1_ch1()->receiving() &&
-          ep1_ch1()->writable() && ep2_ch1()->receiving() &&
-          ep2_ch1()->writable(),
-      kMediumTimeout, clock);
+  EXPECT_TRUE_SIMULATED_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()),
+                             kMediumTimeout, clock);
   TestSendRecv(&clock);
   // Make sure the P2PTransportChannels are actually using ports from the
   // pooled sessions.
@@ -2220,11 +2206,7 @@
   ep1_ch1()->MaybeStartGathering();
   ep2_ch1()->MaybeStartGathering();
   // Wait for initial connection as usual.
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep1_ch1()->selected_connection()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable() &&
-                                 ep2_ch1()->selected_connection()->writable(),
+  EXPECT_TRUE_SIMULATED_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()),
                              kShortTimeout, fake_clock);
   const Connection* old_selected_connection = ep1_ch1()->selected_connection();
   // Destroy the second channel and wait for the current connection on the
@@ -2347,11 +2329,8 @@
   CreateChannels(ep1_config, ep2_config);
 
   // Wait until both sides become writable for the first time.
-  EXPECT_TRUE_SIMULATED_WAIT(
-      ep1_ch1() != nullptr && ep2_ch1() != nullptr && ep1_ch1()->receiving() &&
-          ep1_ch1()->writable() && ep2_ch1()->receiving() &&
-          ep2_ch1()->writable(),
-      kDefaultTimeout, clock);
+  EXPECT_TRUE_SIMULATED_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()),
+                             kDefaultTimeout, clock);
   // Block the ingress traffic to ep1 so that there is no check response from
   // ep2.
   ASSERT_NE(nullptr, LocalCandidate(ep1_ch1()));
@@ -2487,25 +2466,19 @@
   // Create channels and let them go writable, as usual.
   CreateChannels(config, config);
 
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable(),
-                             kMediumTimeout, clock);
-  EXPECT_TRUE(ep1_ch1()->selected_connection() &&
-              ep2_ch1()->selected_connection() &&
-              LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) &&
-              RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1]));
+  EXPECT_TRUE_SIMULATED_WAIT(
+      CheckCandidatePairAndConnected(ep1_ch1(), ep2_ch1(), kPublicAddrs[0],
+                                     kPublicAddrs[1]),
+      kMediumTimeout, clock);
 
   // Blackhole any traffic to or from the public addrs.
   RTC_LOG(LS_INFO) << "Failing over...";
   fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[1]);
   // The selected connections may switch, so keep references to them.
   const Connection* selected_connection1 = ep1_ch1()->selected_connection();
-  const Connection* selected_connection2 = ep2_ch1()->selected_connection();
   // We should detect loss of receiving within 1 second or so.
-  EXPECT_TRUE_SIMULATED_WAIT(
-      !selected_connection1->receiving() && !selected_connection2->receiving(),
-      kMediumTimeout, clock);
+  EXPECT_TRUE_SIMULATED_WAIT(!selected_connection1->receiving(), kMediumTimeout,
+                             clock);
 
   // We should switch over to use the alternate addr on both sides
   // when we are not receiving.
@@ -2538,36 +2511,22 @@
   IceConfig config = CreateIceConfig(1000, GATHER_ONCE);
   // Create channels and let them go writable, as usual.
   CreateChannels(config, config);
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable(),
-                             kMediumTimeout, clock);
-  EXPECT_TRUE(ep1_ch1()->selected_connection() &&
-              ep2_ch1()->selected_connection() &&
-              LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) &&
-              RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1]));
+  EXPECT_TRUE_SIMULATED_WAIT(
+      CheckCandidatePairAndConnected(ep1_ch1(), ep2_ch1(), kPublicAddrs[0],
+                                     kPublicAddrs[1]),
+      kMediumTimeout, clock);
 
   // Blackhole any traffic to or from the public addrs.
   RTC_LOG(LS_INFO) << "Failing over...";
   fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]);
-  // The selected connections will switch, so keep references to them.
-  const Connection* selected_connection1 = ep1_ch1()->selected_connection();
-  const Connection* selected_connection2 = ep2_ch1()->selected_connection();
-  // We should detect loss of receiving within 1 second or so.
-  EXPECT_TRUE_SIMULATED_WAIT(
-      !selected_connection1->receiving() && !selected_connection2->receiving(),
-      kMediumTimeout, clock);
 
+  // We should detect loss of receiving within 1 second or so.
   // We should switch over to use the alternate addr on both sides
   // when we are not receiving.
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection()->receiving() &&
-                                 ep2_ch1()->selected_connection()->receiving(),
-                             kMediumTimeout, clock);
-  EXPECT_TRUE(
-      LocalCandidate(ep1_ch1())->address().EqualIPs(kAlternateAddrs[0]));
-  EXPECT_TRUE(RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1]));
-  EXPECT_TRUE(
-      RemoteCandidate(ep2_ch1())->address().EqualIPs(kAlternateAddrs[0]));
+  EXPECT_TRUE_SIMULATED_WAIT(
+      CheckCandidatePairAndConnected(ep1_ch1(), ep2_ch1(), kAlternateAddrs[0],
+                                     kPublicAddrs[1]),
+      kMediumTimeout, clock);
 
   DestroyChannels();
 }
@@ -2614,14 +2573,9 @@
   IceConfig config = CreateIceConfig(1000, GATHER_CONTINUALLY);
   // Create channels and let them go writable, as usual.
   CreateChannels(config, config, true /* ice_renomination */);
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable(),
-                             kMediumTimeout, clock);
   EXPECT_TRUE_SIMULATED_WAIT(
-      ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection() &&
-          LocalCandidate(ep1_ch1())->address().EqualIPs(wifiIpv6[0]) &&
-          RemoteCandidate(ep1_ch1())->address().EqualIPs(wifiIpv6[1]),
+      CheckCandidatePairAndConnected(ep1_ch1(), ep2_ch1(), wifiIpv6[0],
+                                     wifiIpv6[1]),
       kMediumTimeout, clock);
 
   // Blackhole any traffic to or from the wifi on endpoint 1.
@@ -2646,6 +2600,8 @@
   EXPECT_TRUE_SIMULATED_WAIT(
       num_pings_sent1 < per_network_best_connection1->num_pings_sent(),
       kMediumTimeout, clock);
+  ASSERT_GT(per_network_best_connection1->num_pings_sent() - num_pings_sent1,
+            0);
   int64_t ping_interval1 =
       (per_network_best_connection1->last_ping_sent() - last_ping_sent1) /
       (per_network_best_connection1->num_pings_sent() - num_pings_sent1);
@@ -2657,10 +2613,8 @@
   // It should switch over to use the cellular IPv6 addr on endpoint 1 before
   // it timed out on writing.
   EXPECT_TRUE_SIMULATED_WAIT(
-      ep1_ch1()->selected_connection()->receiving() &&
-          ep2_ch1()->selected_connection()->receiving() &&
-          RemoteCandidate(ep2_ch1())->address().EqualIPs(cellularIpv6[0]) &&
-          LocalCandidate(ep1_ch1())->address().EqualIPs(cellularIpv6[0]),
+      CheckCandidatePairAndConnected(ep1_ch1(), ep2_ch1(), cellularIpv6[0],
+                                     wifiIpv6[1]),
       kMediumTimeout, clock);
 
   DestroyChannels();
@@ -2686,9 +2640,7 @@
   IceConfig config = CreateIceConfig(1000, GATHER_ONCE);
   // Create channels with ICE renomination and let them go writable as usual.
   CreateChannels(config, config, true);
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable(),
+  ASSERT_TRUE_SIMULATED_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()),
                              kMediumTimeout, clock);
   EXPECT_TRUE_SIMULATED_WAIT(
       ep2_ch1()->selected_connection()->remote_nomination() > 0 &&
@@ -2742,14 +2694,10 @@
   // Create channels and let them go writable, as usual.
   CreateChannels();
 
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable(),
-                             kMediumTimeout, clock);
-  EXPECT_TRUE(ep1_ch1()->selected_connection() &&
-              ep2_ch1()->selected_connection() &&
-              LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) &&
-              RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1]));
+  EXPECT_TRUE_SIMULATED_WAIT(
+      CheckCandidatePairAndConnected(ep1_ch1(), ep2_ch1(), kPublicAddrs[0],
+                                     kPublicAddrs[1]),
+      kMediumTimeout, clock);
 
   // Make the receiving timeout shorter for testing.
   IceConfig config = CreateIceConfig(1000, GATHER_ONCE);
@@ -2763,11 +2711,9 @@
 
   // The selected connections may switch, so keep references to them.
   const Connection* selected_connection1 = ep1_ch1()->selected_connection();
-  const Connection* selected_connection2 = ep2_ch1()->selected_connection();
   // We should detect loss of receiving within 1 second or so.
-  EXPECT_TRUE_SIMULATED_WAIT(
-      !selected_connection1->receiving() && !selected_connection2->receiving(),
-      kMediumTimeout, clock);
+  EXPECT_TRUE_SIMULATED_WAIT(!selected_connection1->receiving(), kMediumTimeout,
+                             clock);
   // After a short while, the link recovers itself.
   SIMULATED_WAIT(false, 10, clock);
   fw()->ClearRules();
@@ -2800,14 +2746,10 @@
 
   // Create channels and let them go writable, as usual.
   CreateChannels();
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable(),
-                             kMediumTimeout, clock);
-  EXPECT_TRUE(ep1_ch1()->selected_connection() &&
-              ep2_ch1()->selected_connection() &&
-              LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) &&
-              RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1]));
+  EXPECT_TRUE_SIMULATED_WAIT(
+      CheckCandidatePairAndConnected(ep1_ch1(), ep2_ch1(), kPublicAddrs[0],
+                                     kPublicAddrs[1]),
+      kMediumTimeout, clock);
 
   // Make the receiving timeout shorter for testing.
   IceConfig config = CreateIceConfig(1000, GATHER_ONCE);
@@ -2820,22 +2762,19 @@
   fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]);
   // The selected connections may switch, so keep references to them.
   const Connection* selected_connection1 = ep1_ch1()->selected_connection();
-  const Connection* selected_connection2 = ep2_ch1()->selected_connection();
   // We should detect loss of receiving within 1 second or so.
-  EXPECT_TRUE_SIMULATED_WAIT(
-      !selected_connection1->receiving() && !selected_connection2->receiving(),
-      kMediumTimeout, clock);
+  EXPECT_TRUE_SIMULATED_WAIT(!selected_connection1->receiving(), kMediumTimeout,
+                             clock);
   // The link recovers after a short while.
   SIMULATED_WAIT(false, 10, clock);
   fw()->ClearRules();
 
   // We should not switch to the alternate addr on both sides because of the
   // dampening.
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection()->receiving() &&
-                                 ep2_ch1()->selected_connection()->receiving(),
-                             kMediumTimeout, clock);
-  EXPECT_TRUE(LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]));
-  EXPECT_TRUE(RemoteCandidate(ep2_ch1())->address().EqualIPs(kPublicAddrs[0]));
+  EXPECT_TRUE_SIMULATED_WAIT(
+      CheckCandidatePairAndConnected(ep1_ch1(), ep2_ch1(), kPublicAddrs[0],
+                                     kPublicAddrs[1]),
+      kMediumTimeout, clock);
   EXPECT_EQ(0, reset_selected_candidate_pair_switches());
   DestroyChannels();
 }
@@ -2865,14 +2804,8 @@
   ep1_ch1()->SetIceConfig(config);
   ep2_ch1()->SetIceConfig(config);
   // Need to wait to make sure the connections on both networks are writable.
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable(),
-                             kMediumTimeout, clock);
   EXPECT_TRUE_SIMULATED_WAIT(
-      ep1_ch1()->selected_connection() &&
-          LocalCandidate(ep1_ch1())->address().EqualIPs(wifi[0]) &&
-          RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1]),
+      CheckCandidatePairAndConnected(ep1_ch1(), ep2_ch1(), wifi[0], wifi[1]),
       kDefaultTimeout, clock);
   Connection* backup_conn =
       GetConnectionWithLocalAddress(ep1_ch1(), cellular[0]);
@@ -2912,18 +2845,11 @@
   // Create channels and let them go writable, as usual.
   CreateChannels();
 
-  EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                              ep2_ch1()->receiving() && ep2_ch1()->writable(),
-                          1000, 1000);
+  EXPECT_TRUE_WAIT_MARGIN(CheckConnected(ep1_ch1(), ep2_ch1()), 1000, 1000);
   // Need to wait to make sure the connections on both networks are writable.
-  EXPECT_TRUE_WAIT(ep1_ch1()->selected_connection() &&
-                       LocalCandidate(ep1_ch1())->address().EqualIPs(wifi[0]) &&
-                       RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1]),
-                   1000);
-  EXPECT_TRUE_WAIT(ep2_ch1()->selected_connection() &&
-                       LocalCandidate(ep2_ch1())->address().EqualIPs(wifi[1]) &&
-                       RemoteCandidate(ep2_ch1())->address().EqualIPs(wifi[0]),
-                   1000);
+  EXPECT_TRUE_WAIT(
+      CheckCandidatePairAndConnected(ep1_ch1(), ep2_ch1(), wifi[0], wifi[1]),
+      1000);
   DestroyChannels();
 }
 
@@ -2945,16 +2871,9 @@
   // Create channels and let them go writable, as usual.
   CreateChannels();
 
-  EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                              ep2_ch1()->receiving() && ep2_ch1()->writable(),
+  EXPECT_TRUE_WAIT_MARGIN(CheckCandidatePairAndConnected(ep1_ch1(), ep2_ch1(),
+                                                         cellular[0], wifi[1]),
                           1000, 1000);
-  // Need to wait to make sure the connections on both networks are writable.
-  EXPECT_TRUE_WAIT(ep1_ch1()->selected_connection() &&
-                       RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1]),
-                   1000);
-  EXPECT_TRUE_WAIT(ep2_ch1()->selected_connection() &&
-                       LocalCandidate(ep2_ch1())->address().EqualIPs(wifi[1]),
-                   1000);
   DestroyChannels();
 }
 
@@ -2973,9 +2892,7 @@
 
   // Create channels and let them go writable, as usual.
   CreateChannels();
-  EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                              ep2_ch1()->receiving() && ep2_ch1()->writable(),
-                          1000, 1000);
+  EXPECT_TRUE_WAIT_MARGIN(CheckConnected(ep1_ch1(), ep2_ch1()), 1000, 1000);
   int backup_ping_interval = 2000;
   ep2_ch1()->SetIceConfig(
       CreateIceConfig(2000, GATHER_ONCE, backup_ping_interval));
@@ -2983,7 +2900,7 @@
   // once every |backup_ping_interval| milliseconds.
   ASSERT_TRUE_WAIT(ep2_ch1()->GetState() == IceTransportState::STATE_COMPLETED,
                    1000);
-  const std::vector<Connection*>& connections = ep2_ch1()->connections();
+  auto connections = ep2_ch1()->connections();
   ASSERT_EQ(2U, connections.size());
   Connection* backup_conn = connections[1];
   EXPECT_TRUE_WAIT(backup_conn->writable(), kMediumTimeout);
@@ -3029,9 +2946,7 @@
 
   SetAllocatorFlags(0, kOnlyLocalPorts);
   SetAllocatorFlags(1, kOnlyLocalPorts);
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable(),
+  ASSERT_TRUE_SIMULATED_WAIT(CheckConnected(ep1_ch1(), ep2_ch1()),
                              kDefaultTimeout, clock);
   // More than one port has been created.
   EXPECT_LE(1U, ep1_ch1()->ports().size());
@@ -3073,9 +2988,8 @@
   CreateChannels(continual_gathering_config, continual_gathering_config);
   SetAllocatorFlags(0, kOnlyLocalPorts);
   SetAllocatorFlags(1, kOnlyLocalPorts);
-  EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                              ep2_ch1()->receiving() && ep2_ch1()->writable(),
-                          kDefaultTimeout, kDefaultTimeout);
+  EXPECT_TRUE_WAIT_MARGIN(CheckConnected(ep1_ch1(), ep2_ch1()), kDefaultTimeout,
+                          kDefaultTimeout);
 
   // Add a new wifi interface on end point 2. We should expect a new connection
   // to be created and the new one will be the best connection.
@@ -3122,14 +3036,10 @@
       CreateIceConfig(1000, GATHER_CONTINUALLY);
   // Create channels and let them go writable, as usual.
   CreateChannels(continual_gathering_config, continual_gathering_config);
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable(),
-                             kMediumTimeout, clock);
-  EXPECT_TRUE(ep1_ch1()->selected_connection() &&
-              ep2_ch1()->selected_connection() &&
-              LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) &&
-              RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1]));
+  EXPECT_TRUE_SIMULATED_WAIT(
+      CheckCandidatePairAndConnected(ep1_ch1(), ep2_ch1(), kPublicAddrs[0],
+                                     kPublicAddrs[1]),
+      kMediumTimeout, clock);
 
   // Add the new address first and then remove the other one.
   RTC_LOG(LS_INFO) << "Draining...";
@@ -3137,9 +3047,8 @@
   RemoveAddress(1, kPublicAddrs[1]);
   // We should switch to use the alternate address after an exchange of pings.
   EXPECT_TRUE_SIMULATED_WAIT(
-      ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection() &&
-          LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) &&
-          RemoteCandidate(ep1_ch1())->address().EqualIPs(kAlternateAddrs[1]),
+      CheckCandidatePairAndConnected(ep1_ch1(), ep2_ch1(), kPublicAddrs[0],
+                                     kAlternateAddrs[1]),
       kMediumTimeout, clock);
 
   // Remove one address first and then add another address.
@@ -3147,9 +3056,8 @@
   RemoveAddress(1, kAlternateAddrs[1]);
   AddAddress(1, kAlternateAddrs[0]);
   EXPECT_TRUE_SIMULATED_WAIT(
-      ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection() &&
-          LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) &&
-          RemoteCandidate(ep1_ch1())->address().EqualIPs(kAlternateAddrs[0]),
+      CheckCandidatePairAndConnected(ep1_ch1(), ep2_ch1(), kPublicAddrs[0],
+                                     kAlternateAddrs[0]),
       kMediumTimeout, clock);
 
   DestroyChannels();
@@ -3172,14 +3080,9 @@
   IceConfig config = CreateIceConfig(1000, GATHER_CONTINUALLY);
   config.regather_on_failed_networks_interval = 2000;
   CreateChannels(config, config);
-  EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
-                                 ep2_ch1()->receiving() &&
-                                 ep2_ch1()->writable(),
-                             kMediumTimeout, clock);
-  EXPECT_TRUE(ep1_ch1()->selected_connection() &&
-              ep2_ch1()->selected_connection() &&
-              LocalCandidate(ep1_ch1())->address().EqualIPs(wifi[0]) &&
-              RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1]));
+  EXPECT_TRUE_SIMULATED_WAIT(
+      CheckCandidatePairAndConnected(ep1_ch1(), ep2_ch1(), wifi[0], wifi[1]),
+      kMediumTimeout, clock);
 
   // Destroy all backup connections.
   DestroyAllButBestConnection(ep1_ch1());
@@ -3719,8 +3622,8 @@
   conn2->ReceivedPing();
   conn2->ReceivedPingResponse(LOW_RTT, "id");
 
-  // Wait for conn1 to be pruned.
-  EXPECT_TRUE_WAIT(conn1->pruned(), kMediumTimeout);
+  // Wait for conn2 to be selected.
+  EXPECT_EQ_WAIT(conn2, ch.selected_connection(), kMediumTimeout);
   // Destroy the connection to test SignalUnknownAddress.
   conn1->Destroy();
   EXPECT_TRUE_WAIT(GetConnectionTo(&ch, "1.1.1.1", 1) == nullptr,
@@ -3833,7 +3736,8 @@
   // But if it is nominated via use_candidate, it is chosen as the selected
   // connection.
   NominateConnection(conn3);
-  EXPECT_EQ(conn3, ch.selected_connection());
+  ASSERT_EQ(conn3, ch.selected_connection());
+
   EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn3));
   EXPECT_TRUE(
       ConnectionMatchesChangeEvent(conn3, "nomination on the controlled side"));
@@ -3990,7 +3894,7 @@
                              &request, kIceUfrag[1], false);
   Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3);
   ASSERT_TRUE(conn3 != nullptr);
-  EXPECT_EQ(conn2, ch.selected_connection());  // Not writable yet.
+  EXPECT_NE(conn3, ch.selected_connection());  // Not writable yet.
   conn3->ReceivedPingResponse(LOW_RTT, "id");  // Become writable.
   EXPECT_EQ_WAIT(conn3, ch.selected_connection(), kDefaultTimeout);
 
@@ -4595,6 +4499,7 @@
       const std::string& remote_candidate_type,
       const std::string& relay_protocol_type = UDP_PROTOCOL_NAME) {
     Connection* conn = FindNextPingableConnectionAndPingIt(channel_.get());
+    ASSERT_TRUE(conn != nullptr);
     EXPECT_EQ(conn->local_candidate().type(), local_candidate_type);
     if (conn->local_candidate().type() == RELAY_PORT_TYPE) {
       EXPECT_EQ(conn->local_candidate().relay_protocol(), relay_protocol_type);
@@ -4627,11 +4532,13 @@
 
   // Relay/Relay should be the first pingable connection.
   Connection* conn = FindNextPingableConnectionAndPingIt(&ch);
+  ASSERT_TRUE(conn != nullptr);
   EXPECT_EQ(conn->local_candidate().type(), RELAY_PORT_TYPE);
   EXPECT_EQ(conn->remote_candidate().type(), RELAY_PORT_TYPE);
 
   // Unless that we have a trigger check waiting to be pinged.
   Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
+  ASSERT_TRUE(conn2 != nullptr);
   EXPECT_EQ(conn2->local_candidate().type(), LOCAL_PORT_TYPE);
   EXPECT_EQ(conn2->remote_candidate().type(), LOCAL_PORT_TYPE);
   conn2->ReceivedPing();
@@ -4639,6 +4546,7 @@
 
   // Make conn3 the selected connection.
   Connection* conn3 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
+  ASSERT_TRUE(conn3 != nullptr);
   EXPECT_EQ(conn3->local_candidate().type(), LOCAL_PORT_TYPE);
   EXPECT_EQ(conn3->remote_candidate().type(), RELAY_PORT_TYPE);
   conn3->ReceivedPingResponse(LOW_RTT, "id");
@@ -4914,7 +4822,7 @@
   const auto& local_candidate =
       GetEndpoint(0)->saved_candidates_[0]->candidates[0];
   // The IP address of ep1's host candidate should be obfuscated.
-  EXPECT_TRUE(local_candidate.address().IsUnresolvedIP());
+  ASSERT_TRUE(local_candidate.address().IsUnresolvedIP());
   // This is the underlying private IP address of the same candidate at ep1.
   const auto local_address = rtc::SocketAddress(
       kPublicAddrs[0].ipaddr(), local_candidate.address().port());
@@ -4927,7 +4835,7 @@
       }));
   // Let ep1 signal its hostname candidate to ep2.
   ResumeCandidates(0);
-  EXPECT_TRUE_WAIT(mock_async_resolver_started, kMediumTimeout);
+  ASSERT_TRUE_WAIT(mock_async_resolver_started, kMediumTimeout);
   // Now that ep2 is in the process of resolving the hostname candidate signaled
   // by ep1. Let ep2 signal its host candidate with an IP address to ep1, so
   // that ep1 can form a candidate pair, select it and start to ping ep2.
@@ -5400,6 +5308,7 @@
   fw()->AddRule(false, rtc::FP_ANY, kPublicAddrs[1], kPublicAddrs[0]);
   fw()->AddRule(false, rtc::FP_ANY, kPublicAddrs[0], kTurnUdpExtAddr);
   fw()->AddRule(false, rtc::FP_ANY, kPublicAddrs[1], kTurnUdpExtAddr);
+
   // We should be able to reuse the previously gathered relay candidates.
   EXPECT_EQ_SIMULATED_WAIT(
       RELAY_PORT_TYPE,
@@ -5544,8 +5453,14 @@
                              kDefaultTimeout, clock);
   ResumeCandidates(0);
   ResumeCandidates(1);
-  ASSERT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection() != nullptr,
-                             kDefaultTimeout, clock);
+  ASSERT_TRUE_SIMULATED_WAIT(
+      ep1_ch1()->selected_connection() != nullptr &&
+          LOCAL_PORT_TYPE ==
+              ep1_ch1()->selected_connection()->local_candidate().type() &&
+          ep2_ch1()->selected_connection() != nullptr &&
+          LOCAL_PORT_TYPE ==
+              ep1_ch1()->selected_connection()->remote_candidate().type(),
+      kDefaultTimeout, clock);
   ASSERT_TRUE_SIMULATED_WAIT(ep2_ch1()->selected_connection() != nullptr,
                              kDefaultTimeout, clock);
   // Test that we have a host-host candidate pair selected and the number of