xref: /aosp_15_r20/external/webrtc/sdk/objc/unittests/RTCIceServerTest.mm (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1/*
2 *  Copyright 2015 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#import <Foundation/Foundation.h>
12#import <XCTest/XCTest.h>
13
14#include <vector>
15
16#include "rtc_base/gunit.h"
17
18#import "api/peerconnection/RTCIceServer+Private.h"
19#import "api/peerconnection/RTCIceServer.h"
20#import "helpers/NSString+StdString.h"
21
22@interface RTCIceServerTest : XCTestCase
23@end
24
25@implementation RTCIceServerTest
26
27- (void)testOneURLServer {
28  RTC_OBJC_TYPE(RTCIceServer) *server =
29      [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:@[ @"stun:stun1.example.net" ]];
30
31  webrtc::PeerConnectionInterface::IceServer iceStruct = server.nativeServer;
32  EXPECT_EQ(1u, iceStruct.urls.size());
33  EXPECT_EQ("stun:stun1.example.net", iceStruct.urls.front());
34  EXPECT_EQ("", iceStruct.username);
35  EXPECT_EQ("", iceStruct.password);
36}
37
38- (void)testTwoURLServer {
39  RTC_OBJC_TYPE(RTCIceServer) *server = [[RTC_OBJC_TYPE(RTCIceServer) alloc]
40      initWithURLStrings:@[ @"turn1:turn1.example.net", @"turn2:turn2.example.net" ]];
41
42  webrtc::PeerConnectionInterface::IceServer iceStruct = server.nativeServer;
43  EXPECT_EQ(2u, iceStruct.urls.size());
44  EXPECT_EQ("turn1:turn1.example.net", iceStruct.urls.front());
45  EXPECT_EQ("turn2:turn2.example.net", iceStruct.urls.back());
46  EXPECT_EQ("", iceStruct.username);
47  EXPECT_EQ("", iceStruct.password);
48}
49
50- (void)testPasswordCredential {
51  RTC_OBJC_TYPE(RTCIceServer) *server =
52      [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:@[ @"turn1:turn1.example.net" ]
53                                                     username:@"username"
54                                                   credential:@"credential"];
55  webrtc::PeerConnectionInterface::IceServer iceStruct = server.nativeServer;
56  EXPECT_EQ(1u, iceStruct.urls.size());
57  EXPECT_EQ("turn1:turn1.example.net", iceStruct.urls.front());
58  EXPECT_EQ("username", iceStruct.username);
59  EXPECT_EQ("credential", iceStruct.password);
60}
61
62- (void)testHostname {
63  RTC_OBJC_TYPE(RTCIceServer) *server =
64      [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:@[ @"turn1:turn1.example.net" ]
65                                                     username:@"username"
66                                                   credential:@"credential"
67                                                tlsCertPolicy:RTCTlsCertPolicySecure
68                                                     hostname:@"hostname"];
69  webrtc::PeerConnectionInterface::IceServer iceStruct = server.nativeServer;
70  EXPECT_EQ(1u, iceStruct.urls.size());
71  EXPECT_EQ("turn1:turn1.example.net", iceStruct.urls.front());
72  EXPECT_EQ("username", iceStruct.username);
73  EXPECT_EQ("credential", iceStruct.password);
74  EXPECT_EQ("hostname", iceStruct.hostname);
75}
76
77- (void)testTlsAlpnProtocols {
78  RTC_OBJC_TYPE(RTCIceServer) *server =
79      [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:@[ @"turn1:turn1.example.net" ]
80                                                     username:@"username"
81                                                   credential:@"credential"
82                                                tlsCertPolicy:RTCTlsCertPolicySecure
83                                                     hostname:@"hostname"
84                                             tlsAlpnProtocols:@[ @"proto1", @"proto2" ]];
85  webrtc::PeerConnectionInterface::IceServer iceStruct = server.nativeServer;
86  EXPECT_EQ(1u, iceStruct.urls.size());
87  EXPECT_EQ("turn1:turn1.example.net", iceStruct.urls.front());
88  EXPECT_EQ("username", iceStruct.username);
89  EXPECT_EQ("credential", iceStruct.password);
90  EXPECT_EQ("hostname", iceStruct.hostname);
91  EXPECT_EQ(2u, iceStruct.tls_alpn_protocols.size());
92}
93
94- (void)testTlsEllipticCurves {
95  RTC_OBJC_TYPE(RTCIceServer) *server =
96      [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:@[ @"turn1:turn1.example.net" ]
97                                                     username:@"username"
98                                                   credential:@"credential"
99                                                tlsCertPolicy:RTCTlsCertPolicySecure
100                                                     hostname:@"hostname"
101                                             tlsAlpnProtocols:@[ @"proto1", @"proto2" ]
102                                            tlsEllipticCurves:@[ @"curve1", @"curve2" ]];
103  webrtc::PeerConnectionInterface::IceServer iceStruct = server.nativeServer;
104  EXPECT_EQ(1u, iceStruct.urls.size());
105  EXPECT_EQ("turn1:turn1.example.net", iceStruct.urls.front());
106  EXPECT_EQ("username", iceStruct.username);
107  EXPECT_EQ("credential", iceStruct.password);
108  EXPECT_EQ("hostname", iceStruct.hostname);
109  EXPECT_EQ(2u, iceStruct.tls_alpn_protocols.size());
110  EXPECT_EQ(2u, iceStruct.tls_elliptic_curves.size());
111}
112
113- (void)testInitFromNativeServer {
114  webrtc::PeerConnectionInterface::IceServer nativeServer;
115  nativeServer.username = "username";
116  nativeServer.password = "password";
117  nativeServer.urls.push_back("stun:stun.example.net");
118  nativeServer.hostname = "hostname";
119  nativeServer.tls_alpn_protocols.push_back("proto1");
120  nativeServer.tls_alpn_protocols.push_back("proto2");
121  nativeServer.tls_elliptic_curves.push_back("curve1");
122  nativeServer.tls_elliptic_curves.push_back("curve2");
123
124  RTC_OBJC_TYPE(RTCIceServer) *iceServer =
125      [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithNativeServer:nativeServer];
126  EXPECT_EQ(1u, iceServer.urlStrings.count);
127  EXPECT_EQ("stun:stun.example.net",
128      [NSString stdStringForString:iceServer.urlStrings.firstObject]);
129  EXPECT_EQ("username", [NSString stdStringForString:iceServer.username]);
130  EXPECT_EQ("password", [NSString stdStringForString:iceServer.credential]);
131  EXPECT_EQ("hostname", [NSString stdStringForString:iceServer.hostname]);
132  EXPECT_EQ(2u, iceServer.tlsAlpnProtocols.count);
133  EXPECT_EQ(2u, iceServer.tlsEllipticCurves.count);
134}
135
136@end
137