Michael Iedema | ccee56b | 2018-07-05 15:28:24 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame^] | 17 | #import "api/peerconnection/RTCConfiguration+Private.h" |
| 18 | #import "api/peerconnection/RTCConfiguration.h" |
| 19 | #import "api/peerconnection/RTCIceServer.h" |
| 20 | #import "api/peerconnection/RTCMediaConstraints.h" |
| 21 | #import "api/peerconnection/RTCPeerConnection.h" |
| 22 | #import "api/peerconnection/RTCPeerConnectionFactory.h" |
| 23 | #import "helpers/NSString+StdString.h" |
Michael Iedema | ccee56b | 2018-07-05 15:28:24 +0200 | [diff] [blame] | 24 | |
| 25 | @interface RTCCertificateTest : NSObject |
| 26 | - (void)testCertificateIsUsedInConfig; |
| 27 | @end |
| 28 | |
| 29 | @implementation RTCCertificateTest |
| 30 | |
| 31 | - (void)testCertificateIsUsedInConfig { |
| 32 | RTCConfiguration *originalConfig = [[RTCConfiguration alloc] init]; |
| 33 | |
| 34 | NSArray *urlStrings = @[ @"stun:stun1.example.net" ]; |
| 35 | RTCIceServer *server = [[RTCIceServer alloc] initWithURLStrings:urlStrings]; |
| 36 | originalConfig.iceServers = @[ server ]; |
| 37 | |
| 38 | // Generate a new certificate. |
| 39 | RTCCertificate *originalCertificate = [RTCCertificate generateCertificateWithParams:@{ |
| 40 | @"expires" : @100000, |
| 41 | @"name" : @"RSASSA-PKCS1-v1_5" |
| 42 | }]; |
| 43 | |
| 44 | // Store certificate in configuration. |
| 45 | originalConfig.certificate = originalCertificate; |
| 46 | |
| 47 | RTCMediaConstraints *contraints = |
| 48 | [[RTCMediaConstraints alloc] initWithMandatoryConstraints:@{} optionalConstraints:nil]; |
| 49 | RTCPeerConnectionFactory *factory = [[RTCPeerConnectionFactory alloc] init]; |
| 50 | |
| 51 | // Create PeerConnection with this certificate. |
| 52 | RTCPeerConnection *peerConnection = |
| 53 | [factory peerConnectionWithConfiguration:originalConfig constraints:contraints delegate:nil]; |
| 54 | |
| 55 | // Retrieve certificate from the configuration. |
| 56 | RTCConfiguration *retrievedConfig = peerConnection.configuration; |
| 57 | |
| 58 | // Extract PEM strings from original certificate. |
| 59 | std::string originalPrivateKeyField = [[originalCertificate private_key] UTF8String]; |
| 60 | std::string originalCertificateField = [[originalCertificate certificate] UTF8String]; |
| 61 | |
| 62 | // Extract PEM strings from certificate retrieved from configuration. |
| 63 | RTCCertificate *retrievedCertificate = retrievedConfig.certificate; |
| 64 | std::string retrievedPrivateKeyField = [[retrievedCertificate private_key] UTF8String]; |
| 65 | std::string retrievedCertificateField = [[retrievedCertificate certificate] UTF8String]; |
| 66 | |
| 67 | // Check that the original certificate and retrieved certificate match. |
| 68 | EXPECT_EQ(originalPrivateKeyField, retrievedPrivateKeyField); |
| 69 | EXPECT_EQ(retrievedCertificateField, retrievedCertificateField); |
| 70 | } |
| 71 | |
| 72 | @end |
| 73 | |
| 74 | TEST(CertificateTest, CertificateIsUsedInConfig) { |
| 75 | RTCCertificateTest *test = [[RTCCertificateTest alloc] init]; |
| 76 | [test testCertificateIsUsedInConfig]; |
| 77 | } |