blob: 4743610729ce445e8f6dd31bad21c2da24b819e4 [file] [log] [blame]
Tommif7b22c62024-02-15 08:18:341/*
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/gunit.h"
17
Tommic7a4b2a2024-02-25 22:34:3918using webrtc::IceCandidateType;
19
Tommif7b22c62024-02-15 08:18:3420namespace cricket {
21
22TEST(CandidateTest, Id) {
23 Candidate c;
24 EXPECT_EQ(c.id().size(), 8u);
Tommibde80e32024-02-17 14:52:5725 std::string current_id = c.id();
26 // Generate a new ID.
27 c.generate_id();
28 EXPECT_EQ(c.id().size(), 8u);
29 EXPECT_NE(current_id, c.id());
Tommif7b22c62024-02-15 08:18:3430}
31
32TEST(CandidateTest, Component) {
33 Candidate c;
Tommif7b22c62024-02-15 08:18:3434 EXPECT_EQ(c.component(), ICE_CANDIDATE_COMPONENT_DEFAULT);
Tommif2431a92024-02-20 14:27:4435 c.set_component(ICE_CANDIDATE_COMPONENT_RTCP);
36 EXPECT_EQ(c.component(), ICE_CANDIDATE_COMPONENT_RTCP);
Tommif7b22c62024-02-15 08:18:3437}
38
39TEST(CandidateTest, TypeName) {
40 Candidate c;
41 // The `type_name()` property defaults to "host".
42 EXPECT_EQ(c.type_name(), "host");
Tommic7a4b2a2024-02-25 22:34:3943 EXPECT_EQ(c.type(), IceCandidateType::kHost);
Tommif7b22c62024-02-15 08:18:3444
Tommic7a4b2a2024-02-25 22:34:3945 c.set_type(IceCandidateType::kSrflx);
Tommif7b22c62024-02-15 08:18:3446 EXPECT_EQ(c.type_name(), "srflx");
Tommic7a4b2a2024-02-25 22:34:3947 EXPECT_EQ(c.type(), IceCandidateType::kSrflx);
Tommif7b22c62024-02-15 08:18:3448
Tommic7a4b2a2024-02-25 22:34:3949 c.set_type(IceCandidateType::kPrflx);
Tommif7b22c62024-02-15 08:18:3450 EXPECT_EQ(c.type_name(), "prflx");
Tommic7a4b2a2024-02-25 22:34:3951 EXPECT_EQ(c.type(), IceCandidateType::kPrflx);
Tommif7b22c62024-02-15 08:18:3452
Tommic7a4b2a2024-02-25 22:34:3953 c.set_type(IceCandidateType::kRelay);
Tommif7b22c62024-02-15 08:18:3454 EXPECT_EQ(c.type_name(), "relay");
Tommic7a4b2a2024-02-25 22:34:3955 EXPECT_EQ(c.type(), IceCandidateType::kRelay);
Tommif7b22c62024-02-15 08:18:3456}
57
Tommif2431a92024-02-20 14:27:4458TEST(CandidateTest, Foundation) {
59 Candidate c;
60 EXPECT_TRUE(c.foundation().empty());
61 c.set_protocol("udp");
62 c.set_relay_protocol("udp");
63
64 rtc::SocketAddress address("99.99.98.1", 1024);
65 c.set_address(address);
66 c.ComputeFoundation(c.address(), 1);
67 std::string foundation1 = c.foundation();
68 EXPECT_FALSE(foundation1.empty());
69
70 // Change the tiebreaker.
71 c.ComputeFoundation(c.address(), 2);
72 std::string foundation2 = c.foundation();
73 EXPECT_NE(foundation1, foundation2);
74
75 // Provide a different base address.
76 address.SetIP("100.100.100.1");
77 c.ComputeFoundation(address, 1); // Same tiebreaker as for foundation1.
78 foundation2 = c.foundation();
79 EXPECT_NE(foundation1, foundation2);
80
81 // Consistency check (just in case the algorithm ever changes to random!).
82 c.ComputeFoundation(c.address(), 1);
83 foundation2 = c.foundation();
84 EXPECT_EQ(foundation1, foundation2);
85
86 // Changing the protocol should affect the foundation.
87 auto prev_protocol = c.protocol();
88 c.set_protocol("tcp");
89 ASSERT_NE(prev_protocol, c.protocol());
90 c.ComputeFoundation(c.address(), 1);
91 EXPECT_NE(foundation1, c.foundation());
92 c.set_protocol(prev_protocol);
93
94 // Changing the relay protocol should affect the foundation.
95 prev_protocol = c.relay_protocol();
96 c.set_relay_protocol("tcp");
97 ASSERT_NE(prev_protocol, c.relay_protocol());
98 c.ComputeFoundation(c.address(), 1);
99 EXPECT_NE(foundation1, c.foundation());
100}
101
Tommif7b22c62024-02-15 08:18:34102} // namespace cricket