blob: f96cf572db15424f10cf4d221deabebe0ee6cb82 [file] [log] [blame]
jiayl@webrtc.org25484062015-02-18 23:58:16 +00001/*
2 * libjingle
3 * Copyright 2015 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000027
Henrik Kjellander15583c12016-02-10 10:53:12 +010028#include "webrtc/api/dtlsidentitystore.h"
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000029
Henrik Kjellander15583c12016-02-10 10:53:12 +010030#include "webrtc/api/webrtcsessiondescriptionfactory.h"
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000031#include "webrtc/base/gunit.h"
32#include "webrtc/base/logging.h"
33#include "webrtc/base/ssladapter.h"
34
Henrik Boström5e56c592015-08-11 10:33:13 +020035using webrtc::DtlsIdentityStoreImpl;
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000036
37static const int kTimeoutMs = 10000;
38
39class MockDtlsIdentityRequestObserver :
Henrik Boström5e56c592015-08-11 10:33:13 +020040 public webrtc::DtlsIdentityRequestObserver {
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000041 public:
42 MockDtlsIdentityRequestObserver()
43 : call_back_called_(false), last_request_success_(false) {}
44 void OnFailure(int error) override {
45 EXPECT_FALSE(call_back_called_);
46 call_back_called_ = true;
47 last_request_success_ = false;
48 }
49 void OnSuccess(const std::string& der_cert,
Henrik Boström5e56c592015-08-11 10:33:13 +020050 const std::string& der_private_key) override {
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000051 LOG(LS_WARNING) << "The string version of OnSuccess is called unexpectedly";
52 EXPECT_TRUE(false);
53 }
Henrik Boström5e56c592015-08-11 10:33:13 +020054 void OnSuccess(rtc::scoped_ptr<rtc::SSLIdentity> identity) override {
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000055 EXPECT_FALSE(call_back_called_);
56 call_back_called_ = true;
57 last_request_success_ = true;
58 }
59
60 void Reset() {
61 call_back_called_ = false;
62 last_request_success_ = false;
63 }
64
65 bool LastRequestSucceeded() const {
66 return call_back_called_ && last_request_success_;
67 }
68
69 bool call_back_called() const {
70 return call_back_called_;
71 }
72
73 private:
74 bool call_back_called_;
75 bool last_request_success_;
76};
77
78class DtlsIdentityStoreTest : public testing::Test {
79 protected:
80 DtlsIdentityStoreTest()
jiayl@webrtc.orgd83f4ef2015-03-13 21:26:12 +000081 : worker_thread_(new rtc::Thread()),
Henrik Boström5e56c592015-08-11 10:33:13 +020082 store_(new DtlsIdentityStoreImpl(rtc::Thread::Current(),
83 worker_thread_.get())),
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000084 observer_(
85 new rtc::RefCountedObject<MockDtlsIdentityRequestObserver>()) {
henrikg91d6ede2015-09-17 00:24:34 -070086 RTC_CHECK(worker_thread_->Start());
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000087 }
88 ~DtlsIdentityStoreTest() {}
89
90 static void SetUpTestCase() {
91 rtc::InitializeSSL();
92 }
93 static void TearDownTestCase() {
94 rtc::CleanupSSL();
95 }
96
jiayl@webrtc.orgd83f4ef2015-03-13 21:26:12 +000097 rtc::scoped_ptr<rtc::Thread> worker_thread_;
Henrik Boström5e56c592015-08-11 10:33:13 +020098 rtc::scoped_ptr<DtlsIdentityStoreImpl> store_;
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000099 rtc::scoped_refptr<MockDtlsIdentityRequestObserver> observer_;
100};
101
Henrik Boström5e56c592015-08-11 10:33:13 +0200102TEST_F(DtlsIdentityStoreTest, RequestIdentitySuccessRSA) {
103 EXPECT_TRUE_WAIT(store_->HasFreeIdentityForTesting(rtc::KT_RSA), kTimeoutMs);
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000104
Henrik Boström5e56c592015-08-11 10:33:13 +0200105 store_->RequestIdentity(rtc::KT_RSA, observer_.get());
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000106 EXPECT_TRUE_WAIT(observer_->LastRequestSucceeded(), kTimeoutMs);
107
Henrik Boström5e56c592015-08-11 10:33:13 +0200108 EXPECT_TRUE_WAIT(store_->HasFreeIdentityForTesting(rtc::KT_RSA), kTimeoutMs);
jiayl@webrtc.orgd83f4ef2015-03-13 21:26:12 +0000109
110 observer_->Reset();
111
112 // Verifies that the callback is async when a free identity is ready.
Henrik Boström5e56c592015-08-11 10:33:13 +0200113 store_->RequestIdentity(rtc::KT_RSA, observer_.get());
jiayl@webrtc.orgd83f4ef2015-03-13 21:26:12 +0000114 EXPECT_FALSE(observer_->call_back_called());
115 EXPECT_TRUE_WAIT(observer_->LastRequestSucceeded(), kTimeoutMs);
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000116}
jiayl@webrtc.orgd83f4ef2015-03-13 21:26:12 +0000117
Henrik Boström5e56c592015-08-11 10:33:13 +0200118TEST_F(DtlsIdentityStoreTest, RequestIdentitySuccessECDSA) {
119 // Since store currently does not preemptively generate free ECDSA identities
120 // we do not invoke HasFreeIdentityForTesting between requests.
jiayl@webrtc.orgd83f4ef2015-03-13 21:26:12 +0000121
Henrik Boström5e56c592015-08-11 10:33:13 +0200122 store_->RequestIdentity(rtc::KT_ECDSA, observer_.get());
123 EXPECT_TRUE_WAIT(observer_->LastRequestSucceeded(), kTimeoutMs);
124
125 observer_->Reset();
126
127 // Verifies that the callback is async when a free identity is ready.
128 store_->RequestIdentity(rtc::KT_ECDSA, observer_.get());
129 EXPECT_FALSE(observer_->call_back_called());
130 EXPECT_TRUE_WAIT(observer_->LastRequestSucceeded(), kTimeoutMs);
131}
132
133TEST_F(DtlsIdentityStoreTest, DeleteStoreEarlyNoCrashRSA) {
134 EXPECT_FALSE(store_->HasFreeIdentityForTesting(rtc::KT_RSA));
135
136 store_->RequestIdentity(rtc::KT_RSA, observer_.get());
137 store_.reset();
138
139 worker_thread_->Stop();
140 EXPECT_FALSE(observer_->call_back_called());
141}
142
143TEST_F(DtlsIdentityStoreTest, DeleteStoreEarlyNoCrashECDSA) {
144 EXPECT_FALSE(store_->HasFreeIdentityForTesting(rtc::KT_ECDSA));
145
146 store_->RequestIdentity(rtc::KT_ECDSA, observer_.get());
jiayl@webrtc.orgd83f4ef2015-03-13 21:26:12 +0000147 store_.reset();
148
149 worker_thread_->Stop();
150 EXPECT_FALSE(observer_->call_back_called());
151}
152