blob: 67b7cb22a79194da8bc0ac7502e1a1cd58dae5f5 [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;
36using webrtc::WebRtcSessionDescriptionFactory;
37
38static const int kTimeoutMs = 10000;
39
40class MockDtlsIdentityRequestObserver :
41 public webrtc::DTLSIdentityRequestObserver {
42 public:
43 MockDtlsIdentityRequestObserver()
44 : call_back_called_(false), last_request_success_(false) {}
45 void OnFailure(int error) override {
46 EXPECT_FALSE(call_back_called_);
47 call_back_called_ = true;
48 last_request_success_ = false;
49 }
50 void OnSuccess(const std::string& der_cert,
51 const std::string& der_private_key) {
52 LOG(LS_WARNING) << "The string version of OnSuccess is called unexpectedly";
53 EXPECT_TRUE(false);
54 }
55 void OnSuccessWithIdentityObj(
56 rtc::scoped_ptr<rtc::SSLIdentity> identity) override {
57 EXPECT_FALSE(call_back_called_);
58 call_back_called_ = true;
59 last_request_success_ = true;
60 }
61
62 void Reset() {
63 call_back_called_ = false;
64 last_request_success_ = false;
65 }
66
67 bool LastRequestSucceeded() const {
68 return call_back_called_ && last_request_success_;
69 }
70
71 bool call_back_called() const {
72 return call_back_called_;
73 }
74
75 private:
76 bool call_back_called_;
77 bool last_request_success_;
78};
79
80class DtlsIdentityStoreTest : public testing::Test {
81 protected:
82 DtlsIdentityStoreTest()
jiayl@webrtc.org83728882015-03-13 00:32:32 +000083 : store_(new DtlsIdentityStore(rtc::Thread::Current(),
84 rtc::Thread::Current())),
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000085 observer_(
86 new rtc::RefCountedObject<MockDtlsIdentityRequestObserver>()) {
87 store_->Initialize();
88 }
89 ~DtlsIdentityStoreTest() {}
90
91 static void SetUpTestCase() {
92 rtc::InitializeSSL();
93 }
94 static void TearDownTestCase() {
95 rtc::CleanupSSL();
96 }
97
98 rtc::scoped_ptr<DtlsIdentityStore> store_;
99 rtc::scoped_refptr<MockDtlsIdentityRequestObserver> observer_;
100};
101
102TEST_F(DtlsIdentityStoreTest, RequestIdentitySuccess) {
103 EXPECT_TRUE_WAIT(store_->HasFreeIdentityForTesting(), kTimeoutMs);
104
105 store_->RequestIdentity(observer_.get());
106 EXPECT_TRUE_WAIT(observer_->LastRequestSucceeded(), kTimeoutMs);
107
108 EXPECT_TRUE_WAIT(store_->HasFreeIdentityForTesting(), kTimeoutMs);
109}