candidate_unittest.cc (3072B)
1 /* 2 * Copyright 2024 The WebRTC Project Authors. All rights reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "api/candidate.h" 12 13 #include <string> 14 15 #include "p2p/base/p2p_constants.h" 16 #include "rtc_base/socket_address.h" 17 #include "test/gtest.h" 18 19 using webrtc::IceCandidateType; 20 21 namespace webrtc { 22 23 TEST(CandidateTest, Id) { 24 Candidate c; 25 EXPECT_EQ(c.id().size(), 8u); 26 std::string current_id = c.id(); 27 // Generate a new ID. 28 c.generate_id(); 29 EXPECT_EQ(c.id().size(), 8u); 30 EXPECT_NE(current_id, c.id()); 31 } 32 33 TEST(CandidateTest, Component) { 34 Candidate c; 35 EXPECT_EQ(c.component(), ICE_CANDIDATE_COMPONENT_DEFAULT); 36 c.set_component(ICE_CANDIDATE_COMPONENT_RTCP); 37 EXPECT_EQ(c.component(), ICE_CANDIDATE_COMPONENT_RTCP); 38 } 39 40 TEST(CandidateTest, TypeName) { 41 Candidate c; 42 // The `type_name()` property defaults to "host". 43 EXPECT_EQ(c.type_name(), "host"); 44 EXPECT_EQ(c.type(), IceCandidateType::kHost); 45 46 c.set_type(IceCandidateType::kSrflx); 47 EXPECT_EQ(c.type_name(), "srflx"); 48 EXPECT_EQ(c.type(), IceCandidateType::kSrflx); 49 50 c.set_type(IceCandidateType::kPrflx); 51 EXPECT_EQ(c.type_name(), "prflx"); 52 EXPECT_EQ(c.type(), IceCandidateType::kPrflx); 53 54 c.set_type(IceCandidateType::kRelay); 55 EXPECT_EQ(c.type_name(), "relay"); 56 EXPECT_EQ(c.type(), IceCandidateType::kRelay); 57 } 58 59 TEST(CandidateTest, Foundation) { 60 Candidate c; 61 EXPECT_TRUE(c.foundation().empty()); 62 c.set_protocol("udp"); 63 c.set_relay_protocol("udp"); 64 65 SocketAddress address("99.99.98.1", 1024); 66 c.set_address(address); 67 c.ComputeFoundation(c.address(), 1); 68 std::string foundation1 = c.foundation(); 69 EXPECT_FALSE(foundation1.empty()); 70 71 // Change the tiebreaker. 72 c.ComputeFoundation(c.address(), 2); 73 std::string foundation2 = c.foundation(); 74 EXPECT_NE(foundation1, foundation2); 75 76 // Provide a different base address. 77 address.SetIP("100.100.100.1"); 78 c.ComputeFoundation(address, 1); // Same tiebreaker as for foundation1. 79 foundation2 = c.foundation(); 80 EXPECT_NE(foundation1, foundation2); 81 82 // Consistency check (just in case the algorithm ever changes to random!). 83 c.ComputeFoundation(c.address(), 1); 84 foundation2 = c.foundation(); 85 EXPECT_EQ(foundation1, foundation2); 86 87 // Changing the protocol should affect the foundation. 88 auto prev_protocol = c.protocol(); 89 c.set_protocol("tcp"); 90 ASSERT_NE(prev_protocol, c.protocol()); 91 c.ComputeFoundation(c.address(), 1); 92 EXPECT_NE(foundation1, c.foundation()); 93 c.set_protocol(prev_protocol); 94 95 // Changing the relay protocol should affect the foundation. 96 prev_protocol = c.relay_protocol(); 97 c.set_relay_protocol("tcp"); 98 ASSERT_NE(prev_protocol, c.relay_protocol()); 99 c.ComputeFoundation(c.address(), 1); 100 EXPECT_NE(foundation1, c.foundation()); 101 } 102 103 } // namespace webrtc