blob: 12f58feea56a12e4743c879298f620202ce13375 [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
28#include "talk/app/webrtc/dtlsidentitystore.h"
29
30#include "talk/app/webrtc/webrtcsessiondescriptionfactory.h"
31#include "webrtc/base/gunit.h"
32#include "webrtc/base/logging.h"
33#include "webrtc/base/ssladapter.h"
34
35using webrtc::DtlsIdentityStore;
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000036
37static const int kTimeoutMs = 10000;
38
39class MockDtlsIdentityRequestObserver :
40 public webrtc::DTLSIdentityRequestObserver {
41 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,
50 const std::string& der_private_key) {
51 LOG(LS_WARNING) << "The string version of OnSuccess is called unexpectedly";
52 EXPECT_TRUE(false);
53 }
54 void OnSuccessWithIdentityObj(
55 rtc::scoped_ptr<rtc::SSLIdentity> identity) override {
56 EXPECT_FALSE(call_back_called_);
57 call_back_called_ = true;
58 last_request_success_ = true;
59 }
60
61 void Reset() {
62 call_back_called_ = false;
63 last_request_success_ = false;
64 }
65
66 bool LastRequestSucceeded() const {
67 return call_back_called_ && last_request_success_;
68 }
69
70 bool call_back_called() const {
71 return call_back_called_;
72 }
73
74 private:
75 bool call_back_called_;
76 bool last_request_success_;
77};
78
79class DtlsIdentityStoreTest : public testing::Test {
80 protected:
81 DtlsIdentityStoreTest()
jiayl@webrtc.orgd83f4ef2015-03-13 21:26:12 +000082 : worker_thread_(new rtc::Thread()),
83 store_(new DtlsIdentityStore(rtc::Thread::Current(),
84 worker_thread_.get())),
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000085 observer_(
86 new rtc::RefCountedObject<MockDtlsIdentityRequestObserver>()) {
jiayl@webrtc.orgd83f4ef2015-03-13 21:26:12 +000087 CHECK(worker_thread_->Start());
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000088 store_->Initialize();
89 }
90 ~DtlsIdentityStoreTest() {}
91
92 static void SetUpTestCase() {
93 rtc::InitializeSSL();
94 }
95 static void TearDownTestCase() {
96 rtc::CleanupSSL();
97 }
98
jiayl@webrtc.orgd83f4ef2015-03-13 21:26:12 +000099 rtc::scoped_ptr<rtc::Thread> worker_thread_;
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000100 rtc::scoped_ptr<DtlsIdentityStore> store_;
101 rtc::scoped_refptr<MockDtlsIdentityRequestObserver> observer_;
102};
103
104TEST_F(DtlsIdentityStoreTest, RequestIdentitySuccess) {
105 EXPECT_TRUE_WAIT(store_->HasFreeIdentityForTesting(), kTimeoutMs);
106
107 store_->RequestIdentity(observer_.get());
108 EXPECT_TRUE_WAIT(observer_->LastRequestSucceeded(), kTimeoutMs);
109
110 EXPECT_TRUE_WAIT(store_->HasFreeIdentityForTesting(), kTimeoutMs);
jiayl@webrtc.orgd83f4ef2015-03-13 21:26:12 +0000111
112 observer_->Reset();
113
114 // Verifies that the callback is async when a free identity is ready.
115 store_->RequestIdentity(observer_.get());
116 EXPECT_FALSE(observer_->call_back_called());
117 EXPECT_TRUE_WAIT(observer_->LastRequestSucceeded(), kTimeoutMs);
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000118}
jiayl@webrtc.orgd83f4ef2015-03-13 21:26:12 +0000119
120TEST_F(DtlsIdentityStoreTest, DeleteStoreEarlyNoCrash) {
121 EXPECT_FALSE(store_->HasFreeIdentityForTesting());
122
123 store_->RequestIdentity(observer_.get());
124 store_.reset();
125
126 worker_thread_->Stop();
127 EXPECT_FALSE(observer_->call_back_called());
128}
129