blob: bc1347336c60f80213ca17dcdfa9984b26de4a37 [file] [log] [blame]
Michael Iedemaccee56b2018-07-05 15:28:24 +02001/*
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 Leec8a6fb22022-05-13 19:59:49 +090012#import <XCTest/XCTest.h>
Michael Iedemaccee56b2018-07-05 15:28:24 +020013
14#include <vector>
15
16#include "rtc_base/gunit.h"
17
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020018#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 Titov63ee39d2022-05-13 14:46:42 +000024#import "helpers/NSString+StdString.h"
Michael Iedemaccee56b2018-07-05 15:28:24 +020025
Byoungchan Leec8a6fb22022-05-13 19:59:49 +090026@interface RTCCertificateTest : XCTestCase
Michael Iedemaccee56b2018-07-05 15:28:24 +020027@end
28
29@implementation RTCCertificateTest
30
31- (void)testCertificateIsUsedInConfig {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020032 RTC_OBJC_TYPE(RTCConfiguration) *originalConfig = [[RTC_OBJC_TYPE(RTCConfiguration) alloc] init];
Michael Iedemaccee56b2018-07-05 15:28:24 +020033
34 NSArray *urlStrings = @[ @"stun:stun1.example.net" ];
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020035 RTC_OBJC_TYPE(RTCIceServer) *server =
36 [[RTC_OBJC_TYPE(RTCIceServer) alloc] initWithURLStrings:urlStrings];
Michael Iedemaccee56b2018-07-05 15:28:24 +020037 originalConfig.iceServers = @[ server ];
38
39 // Generate a new certificate.
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020040 RTC_OBJC_TYPE(RTCCertificate) *originalCertificate = [RTC_OBJC_TYPE(RTCCertificate)
41 generateCertificateWithParams:@{@"expires" : @100000, @"name" : @"RSASSA-PKCS1-v1_5"}];
Michael Iedemaccee56b2018-07-05 15:28:24 +020042
43 // Store certificate in configuration.
44 originalConfig.certificate = originalCertificate;
45
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020046 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 Iedemaccee56b2018-07-05 15:28:24 +020051
52 // Create PeerConnection with this certificate.
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020053 RTC_OBJC_TYPE(RTCPeerConnection) *peerConnection =
Michael Iedemaccee56b2018-07-05 15:28:24 +020054 [factory peerConnectionWithConfiguration:originalConfig constraints:contraints delegate:nil];
55
56 // Retrieve certificate from the configuration.
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020057 RTC_OBJC_TYPE(RTCConfiguration) *retrievedConfig = peerConnection.configuration;
Michael Iedemaccee56b2018-07-05 15:28:24 +020058
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 Bonadeia81e9c82020-05-04 16:14:32 +020064 RTC_OBJC_TYPE(RTCCertificate) *retrievedCertificate = retrievedConfig.certificate;
Michael Iedemaccee56b2018-07-05 15:28:24 +020065 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