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
13#include <vector>
14
15#include "rtc_base/gunit.h"
16
17#import "api/peerconnection/RTCIceServer+Private.h"
18#import "api/peerconnection/RTCIceServer.h"
19#import "helpers/NSString+StdString.h"
20
21@interface RTCIceServerTest : NSObject
22- (void)testOneURLServer;
23- (void)testTwoURLServer;
24- (void)testPasswordCredential;
25- (void)testInitFromNativeServer;
26@end
27
28@implementation RTCIceServerTest
29
30- (void)testOneURLServer {
31  RTC_OBJC_TYPE(RTCIceServer) *server =
32      [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:@[ @"stun:stun1.example.net" ]];
33
34  webrtc::PeerConnectionInterface::IceServer iceStruct = server.nativeServer;
35  EXPECT_EQ(1u, iceStruct.urls.size());
36  EXPECT_EQ("stun:stun1.example.net", iceStruct.urls.front());
37  EXPECT_EQ("", iceStruct.username);
38  EXPECT_EQ("", iceStruct.password);
39}
40
41- (void)testTwoURLServer {
42  RTC_OBJC_TYPE(RTCIceServer) *server = [[RTC_OBJC_TYPE(RTCIceServer) alloc]
43      initWithURLStrings:@[ @"turn1:turn1.example.net", @"turn2:turn2.example.net" ]];
44
45  webrtc::PeerConnectionInterface::IceServer iceStruct = server.nativeServer;
46  EXPECT_EQ(2u, iceStruct.urls.size());
47  EXPECT_EQ("turn1:turn1.example.net", iceStruct.urls.front());
48  EXPECT_EQ("turn2:turn2.example.net", iceStruct.urls.back());
49  EXPECT_EQ("", iceStruct.username);
50  EXPECT_EQ("", iceStruct.password);
51}
52
53- (void)testPasswordCredential {
54  RTC_OBJC_TYPE(RTCIceServer) *server =
55      [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:@[ @"turn1:turn1.example.net" ]
56                                                     username:@"username"
57                                                   credential:@"credential"];
58  webrtc::PeerConnectionInterface::IceServer iceStruct = server.nativeServer;
59  EXPECT_EQ(1u, iceStruct.urls.size());
60  EXPECT_EQ("turn1:turn1.example.net", iceStruct.urls.front());
61  EXPECT_EQ("username", iceStruct.username);
62  EXPECT_EQ("credential", iceStruct.password);
63}
64
65- (void)testHostname {
66  RTC_OBJC_TYPE(RTCIceServer) *server =
67      [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:@[ @"turn1:turn1.example.net" ]
68                                                     username:@"username"
69                                                   credential:@"credential"
70                                                tlsCertPolicy:RTCTlsCertPolicySecure
71                                                     hostname:@"hostname"];
72  webrtc::PeerConnectionInterface::IceServer iceStruct = server.nativeServer;
73  EXPECT_EQ(1u, iceStruct.urls.size());
74  EXPECT_EQ("turn1:turn1.example.net", iceStruct.urls.front());
75  EXPECT_EQ("username", iceStruct.username);
76  EXPECT_EQ("credential", iceStruct.password);
77  EXPECT_EQ("hostname", iceStruct.hostname);
78}
79
80- (void)testTlsAlpnProtocols {
81  RTC_OBJC_TYPE(RTCIceServer) *server =
82      [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:@[ @"turn1:turn1.example.net" ]
83                                                     username:@"username"
84                                                   credential:@"credential"
85                                                tlsCertPolicy:RTCTlsCertPolicySecure
86                                                     hostname:@"hostname"
87                                             tlsAlpnProtocols:@[ @"proto1", @"proto2" ]];
88  webrtc::PeerConnectionInterface::IceServer iceStruct = server.nativeServer;
89  EXPECT_EQ(1u, iceStruct.urls.size());
90  EXPECT_EQ("turn1:turn1.example.net", iceStruct.urls.front());
91  EXPECT_EQ("username", iceStruct.username);
92  EXPECT_EQ("credential", iceStruct.password);
93  EXPECT_EQ("hostname", iceStruct.hostname);
94  EXPECT_EQ(2u, iceStruct.tls_alpn_protocols.size());
95}
96
97- (void)testTlsEllipticCurves {
98  RTC_OBJC_TYPE(RTCIceServer) *server =
99      [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:@[ @"turn1:turn1.example.net" ]
100                                                     username:@"username"
101                                                   credential:@"credential"
102                                                tlsCertPolicy:RTCTlsCertPolicySecure
103                                                     hostname:@"hostname"
104                                             tlsAlpnProtocols:@[ @"proto1", @"proto2" ]
105                                            tlsEllipticCurves:@[ @"curve1", @"curve2" ]];
106  webrtc::PeerConnectionInterface::IceServer iceStruct = server.nativeServer;
107  EXPECT_EQ(1u, iceStruct.urls.size());
108  EXPECT_EQ("turn1:turn1.example.net", iceStruct.urls.front());
109  EXPECT_EQ("username", iceStruct.username);
110  EXPECT_EQ("credential", iceStruct.password);
111  EXPECT_EQ("hostname", iceStruct.hostname);
112  EXPECT_EQ(2u, iceStruct.tls_alpn_protocols.size());
113  EXPECT_EQ(2u, iceStruct.tls_elliptic_curves.size());
114}
115
116- (void)testInitFromNativeServer {
117  webrtc::PeerConnectionInterface::IceServer nativeServer;
118  nativeServer.username = "username";
119  nativeServer.password = "password";
120  nativeServer.urls.push_back("stun:stun.example.net");
121  nativeServer.hostname = "hostname";
122  nativeServer.tls_alpn_protocols.push_back("proto1");
123  nativeServer.tls_alpn_protocols.push_back("proto2");
124  nativeServer.tls_elliptic_curves.push_back("curve1");
125  nativeServer.tls_elliptic_curves.push_back("curve2");
126
127  RTC_OBJC_TYPE(RTCIceServer) *iceServer =
128      [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithNativeServer:nativeServer];
129  EXPECT_EQ(1u, iceServer.urlStrings.count);
130  EXPECT_EQ("stun:stun.example.net",
131      [NSString stdStringForString:iceServer.urlStrings.firstObject]);
132  EXPECT_EQ("username", [NSString stdStringForString:iceServer.username]);
133  EXPECT_EQ("password", [NSString stdStringForString:iceServer.credential]);
134  EXPECT_EQ("hostname", [NSString stdStringForString:iceServer.hostname]);
135  EXPECT_EQ(2u, iceServer.tlsAlpnProtocols.count);
136  EXPECT_EQ(2u, iceServer.tlsEllipticCurves.count);
137}
138
139@end
140
141TEST(RTCIceServerTest, OneURLTest) {
142  @autoreleasepool {
143    RTCIceServerTest *test = [[RTCIceServerTest alloc] init];
144    [test testOneURLServer];
145  }
146}
147
148TEST(RTCIceServerTest, TwoURLTest) {
149  @autoreleasepool {
150    RTCIceServerTest *test = [[RTCIceServerTest alloc] init];
151    [test testTwoURLServer];
152  }
153}
154
155TEST(RTCIceServerTest, PasswordCredentialTest) {
156  @autoreleasepool {
157    RTCIceServerTest *test = [[RTCIceServerTest alloc] init];
158    [test testPasswordCredential];
159  }
160}
161
162TEST(RTCIceServerTest, HostnameTest) {
163  @autoreleasepool {
164    RTCIceServerTest *test = [[RTCIceServerTest alloc] init];
165    [test testHostname];
166  }
167}
168
169TEST(RTCIceServerTest, InitFromNativeServerTest) {
170  @autoreleasepool {
171    RTCIceServerTest *test = [[RTCIceServerTest alloc] init];
172    [test testInitFromNativeServer];
173  }
174}
175