1 /*
2  *  Copyright 2013 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 package org.webrtc;
12 
13 import static java.util.Collections.singletonList;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertFalse;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertNull;
18 import static org.junit.Assert.assertTrue;
19 import static org.mockito.Mockito.mock;
20 
21 import android.support.test.InstrumentationRegistry;
22 import android.support.test.filters.SmallTest;
23 import java.util.Arrays;
24 import java.util.List;
25 import org.chromium.base.test.BaseJUnit4ClassRunner;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.webrtc.PeerConnection.TlsCertPolicy;
30 
31 /** Unit tests for {@link PeerConnection}. */
32 @RunWith(BaseJUnit4ClassRunner.class)
33 public class PeerConnectionTest {
34   @Before
setUp()35   public void setUp() {
36     PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions
37                                          .builder(InstrumentationRegistry.getTargetContext())
38                                          .setNativeLibraryName(TestConstants.NATIVE_LIBRARY)
39                                          .createInitializationOptions());
40   }
41 
42   @Test
43   @SmallTest
testIceServerChanged()44   public void testIceServerChanged() throws Exception {
45     PeerConnection.IceServer iceServer1 =
46         PeerConnection.IceServer.builder("turn:fake.example.com")
47             .setUsername("fakeUsername")
48             .setPassword("fakePassword")
49             .setTlsCertPolicy(TlsCertPolicy.TLS_CERT_POLICY_SECURE)
50             .setHostname("fakeHostname")
51             .setTlsAlpnProtocols(singletonList("fakeTlsAlpnProtocol"))
52             .setTlsEllipticCurves(singletonList("fakeTlsEllipticCurve"))
53             .createIceServer();
54     // Same as iceServer1.
55     PeerConnection.IceServer iceServer2 =
56         PeerConnection.IceServer.builder("turn:fake.example.com")
57             .setUsername("fakeUsername")
58             .setPassword("fakePassword")
59             .setTlsCertPolicy(TlsCertPolicy.TLS_CERT_POLICY_SECURE)
60             .setHostname("fakeHostname")
61             .setTlsAlpnProtocols(singletonList("fakeTlsAlpnProtocol"))
62             .setTlsEllipticCurves(singletonList("fakeTlsEllipticCurve"))
63             .createIceServer();
64     // Differs from iceServer1 by the url.
65     PeerConnection.IceServer iceServer3 =
66         PeerConnection.IceServer.builder("turn:fake.example2.com")
67             .setUsername("fakeUsername")
68             .setPassword("fakePassword")
69             .setTlsCertPolicy(TlsCertPolicy.TLS_CERT_POLICY_SECURE)
70             .setHostname("fakeHostname")
71             .setTlsAlpnProtocols(singletonList("fakeTlsAlpnProtocol"))
72             .setTlsEllipticCurves(singletonList("fakeTlsEllipticCurve"))
73             .createIceServer();
74     // Differs from iceServer1 by the username.
75     PeerConnection.IceServer iceServer4 =
76         PeerConnection.IceServer.builder("turn:fake.example.com")
77             .setUsername("fakeUsername2")
78             .setPassword("fakePassword")
79             .setTlsCertPolicy(TlsCertPolicy.TLS_CERT_POLICY_SECURE)
80             .setHostname("fakeHostname")
81             .setTlsAlpnProtocols(singletonList("fakeTlsAlpnProtocol"))
82             .setTlsEllipticCurves(singletonList("fakeTlsEllipticCurve"))
83             .createIceServer();
84     // Differs from iceServer1 by the password.
85     PeerConnection.IceServer iceServer5 =
86         PeerConnection.IceServer.builder("turn:fake.example.com")
87             .setUsername("fakeUsername")
88             .setPassword("fakePassword2")
89             .setTlsCertPolicy(TlsCertPolicy.TLS_CERT_POLICY_SECURE)
90             .setHostname("fakeHostname")
91             .setTlsAlpnProtocols(singletonList("fakeTlsAlpnProtocol"))
92             .setTlsEllipticCurves(singletonList("fakeTlsEllipticCurve"))
93             .createIceServer();
94     // Differs from iceServer1 by the TLS certificate policy.
95     PeerConnection.IceServer iceServer6 =
96         PeerConnection.IceServer.builder("turn:fake.example.com")
97             .setUsername("fakeUsername")
98             .setPassword("fakePassword")
99             .setTlsCertPolicy(TlsCertPolicy.TLS_CERT_POLICY_INSECURE_NO_CHECK)
100             .setHostname("fakeHostname")
101             .setTlsAlpnProtocols(singletonList("fakeTlsAlpnProtocol"))
102             .setTlsEllipticCurves(singletonList("fakeTlsEllipticCurve"))
103             .createIceServer();
104     // Differs from iceServer1 by the hostname.
105     PeerConnection.IceServer iceServer7 =
106         PeerConnection.IceServer.builder("turn:fake.example.com")
107             .setUsername("fakeUsername")
108             .setPassword("fakePassword")
109             .setTlsCertPolicy(TlsCertPolicy.TLS_CERT_POLICY_INSECURE_NO_CHECK)
110             .setHostname("fakeHostname2")
111             .setTlsAlpnProtocols(singletonList("fakeTlsAlpnProtocol"))
112             .setTlsEllipticCurves(singletonList("fakeTlsEllipticCurve"))
113             .createIceServer();
114     // Differs from iceServer1 by the TLS ALPN.
115     PeerConnection.IceServer iceServer8 =
116         PeerConnection.IceServer.builder("turn:fake.example.com")
117             .setUsername("fakeUsername")
118             .setPassword("fakePassword")
119             .setTlsCertPolicy(TlsCertPolicy.TLS_CERT_POLICY_INSECURE_NO_CHECK)
120             .setHostname("fakeHostname")
121             .setTlsAlpnProtocols(singletonList("fakeTlsAlpnProtocol2"))
122             .setTlsEllipticCurves(singletonList("fakeTlsEllipticCurve"))
123             .createIceServer();
124     // Differs from iceServer1 by the TLS elliptic curve.
125     PeerConnection.IceServer iceServer9 =
126         PeerConnection.IceServer.builder("turn:fake.example.com")
127             .setUsername("fakeUsername")
128             .setPassword("fakePassword")
129             .setTlsCertPolicy(TlsCertPolicy.TLS_CERT_POLICY_INSECURE_NO_CHECK)
130             .setHostname("fakeHostname")
131             .setTlsAlpnProtocols(singletonList("fakeTlsAlpnProtocol"))
132             .setTlsEllipticCurves(singletonList("fakeTlsEllipticCurve2"))
133             .createIceServer();
134 
135     assertTrue(iceServer1.equals(iceServer2));
136     assertFalse(iceServer1.equals(iceServer3));
137     assertFalse(iceServer1.equals(iceServer4));
138     assertFalse(iceServer1.equals(iceServer5));
139     assertFalse(iceServer1.equals(iceServer6));
140     assertFalse(iceServer1.equals(iceServer7));
141     assertFalse(iceServer1.equals(iceServer8));
142     assertFalse(iceServer1.equals(iceServer9));
143   }
144 
145   // TODO(fischman) MOAR test ideas:
146   // - Test that PC.removeStream() works; requires a second
147   //   createOffer/createAnswer dance.
148   // - audit each place that uses |constraints| for specifying non-trivial
149   //   constraints (and ensure they're honored).
150   // - test error cases
151   // - ensure reasonable coverage of jni code is achieved.  Coverage is
152   //   extra-important because of all the free-text (class/method names, etc)
153   //   in JNI-style programming; make sure no typos!
154   // - Test that shutdown mid-interaction is crash-free.
155 
156   // Tests that the JNI glue between Java and C++ does not crash when creating a PeerConnection.
157   @Test
158   @SmallTest
testCreationWithConfig()159   public void testCreationWithConfig() throws Exception {
160     PeerConnectionFactory factory = PeerConnectionFactory.builder().createPeerConnectionFactory();
161     List<PeerConnection.IceServer> iceServers = Arrays.asList(
162         PeerConnection.IceServer.builder("stun:stun.l.google.com:19302").createIceServer(),
163         PeerConnection.IceServer.builder("turn:fake.example.com")
164             .setUsername("fakeUsername")
165             .setPassword("fakePassword")
166             .createIceServer());
167     PeerConnection.RTCConfiguration config = new PeerConnection.RTCConfiguration(iceServers);
168 
169     // Test configuration options.
170     config.continualGatheringPolicy = PeerConnection.ContinualGatheringPolicy.GATHER_CONTINUALLY;
171 
172     PeerConnection offeringPC =
173         factory.createPeerConnection(config, mock(PeerConnection.Observer.class));
174     assertNotNull(offeringPC);
175   }
176 
177   @Test
178   @SmallTest
testCreationWithCertificate()179   public void testCreationWithCertificate() throws Exception {
180     PeerConnectionFactory factory = PeerConnectionFactory.builder().createPeerConnectionFactory();
181     PeerConnection.RTCConfiguration config = new PeerConnection.RTCConfiguration(Arrays.asList());
182 
183     // Test certificate.
184     RtcCertificatePem originalCert = RtcCertificatePem.generateCertificate();
185     config.certificate = originalCert;
186 
187     PeerConnection offeringPC =
188         factory.createPeerConnection(config, mock(PeerConnection.Observer.class));
189 
190     RtcCertificatePem restoredCert = offeringPC.getCertificate();
191     assertEquals(originalCert.privateKey, restoredCert.privateKey);
192     assertEquals(originalCert.certificate, restoredCert.certificate);
193   }
194 
195   @Test
196   @SmallTest
testCreationWithCryptoOptions()197   public void testCreationWithCryptoOptions() throws Exception {
198     PeerConnectionFactory factory = PeerConnectionFactory.builder().createPeerConnectionFactory();
199     PeerConnection.RTCConfiguration config = new PeerConnection.RTCConfiguration(Arrays.asList());
200 
201     assertNull(config.cryptoOptions);
202 
203     CryptoOptions cryptoOptions = CryptoOptions.builder()
204                                       .setEnableGcmCryptoSuites(true)
205                                       .setEnableAes128Sha1_32CryptoCipher(true)
206                                       .setEnableEncryptedRtpHeaderExtensions(true)
207                                       .setRequireFrameEncryption(true)
208                                       .createCryptoOptions();
209     config.cryptoOptions = cryptoOptions;
210 
211     PeerConnection offeringPC =
212         factory.createPeerConnection(config, mock(PeerConnection.Observer.class));
213     assertNotNull(offeringPC);
214   }
215 }
216