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