OLD | NEW |
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
(...skipping 14 matching lines...) Expand all Loading... |
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 */ | 26 */ |
27 | 27 |
28 #include "talk/app/webrtc/dtlsidentitystore.h" | 28 #include "talk/app/webrtc/dtlsidentitystore.h" |
29 | 29 |
30 #include "talk/app/webrtc/webrtcsessiondescriptionfactory.h" | 30 #include "talk/app/webrtc/webrtcsessiondescriptionfactory.h" |
31 #include "webrtc/base/gunit.h" | 31 #include "webrtc/base/gunit.h" |
32 #include "webrtc/base/logging.h" | 32 #include "webrtc/base/logging.h" |
33 #include "webrtc/base/ssladapter.h" | 33 #include "webrtc/base/ssladapter.h" |
34 | 34 |
35 using webrtc::DtlsIdentityStore; | 35 using webrtc::DtlsIdentityStoreImpl; |
36 | 36 |
37 static const int kTimeoutMs = 10000; | 37 static const int kTimeoutMs = 10000; |
38 | 38 |
39 class MockDtlsIdentityRequestObserver : | 39 class MockDtlsIdentityRequestObserver : |
40 public webrtc::DTLSIdentityRequestObserver { | 40 public webrtc::DtlsIdentityRequestObserver { |
41 public: | 41 public: |
42 MockDtlsIdentityRequestObserver() | 42 MockDtlsIdentityRequestObserver() |
43 : call_back_called_(false), last_request_success_(false) {} | 43 : call_back_called_(false), last_request_success_(false) {} |
44 void OnFailure(int error) override { | 44 void OnFailure(int error) override { |
45 EXPECT_FALSE(call_back_called_); | 45 EXPECT_FALSE(call_back_called_); |
46 call_back_called_ = true; | 46 call_back_called_ = true; |
47 last_request_success_ = false; | 47 last_request_success_ = false; |
48 } | 48 } |
49 void OnSuccess(const std::string& der_cert, | 49 void OnSuccess(const std::string& der_cert, |
50 const std::string& der_private_key) { | 50 const std::string& der_private_key) override { |
51 LOG(LS_WARNING) << "The string version of OnSuccess is called unexpectedly"; | 51 LOG(LS_WARNING) << "The string version of OnSuccess is called unexpectedly"; |
52 EXPECT_TRUE(false); | 52 EXPECT_TRUE(false); |
53 } | 53 } |
54 void OnSuccessWithIdentityObj( | 54 void OnSuccess(rtc::scoped_ptr<rtc::SSLIdentity> identity) override { |
55 rtc::scoped_ptr<rtc::SSLIdentity> identity) override { | |
56 EXPECT_FALSE(call_back_called_); | 55 EXPECT_FALSE(call_back_called_); |
57 call_back_called_ = true; | 56 call_back_called_ = true; |
58 last_request_success_ = true; | 57 last_request_success_ = true; |
59 } | 58 } |
60 | 59 |
61 void Reset() { | 60 void Reset() { |
62 call_back_called_ = false; | 61 call_back_called_ = false; |
63 last_request_success_ = false; | 62 last_request_success_ = false; |
64 } | 63 } |
65 | 64 |
66 bool LastRequestSucceeded() const { | 65 bool LastRequestSucceeded() const { |
67 return call_back_called_ && last_request_success_; | 66 return call_back_called_ && last_request_success_; |
68 } | 67 } |
69 | 68 |
70 bool call_back_called() const { | 69 bool call_back_called() const { |
71 return call_back_called_; | 70 return call_back_called_; |
72 } | 71 } |
73 | 72 |
74 private: | 73 private: |
75 bool call_back_called_; | 74 bool call_back_called_; |
76 bool last_request_success_; | 75 bool last_request_success_; |
77 }; | 76 }; |
78 | 77 |
79 class DtlsIdentityStoreTest : public testing::Test { | 78 class DtlsIdentityStoreTest : public testing::Test { |
80 protected: | 79 protected: |
81 DtlsIdentityStoreTest() | 80 DtlsIdentityStoreTest() |
82 : worker_thread_(new rtc::Thread()), | 81 : worker_thread_(new rtc::Thread()), |
83 store_(new DtlsIdentityStore(rtc::Thread::Current(), | 82 store_(new DtlsIdentityStoreImpl(rtc::Thread::Current(), |
84 worker_thread_.get())), | 83 worker_thread_.get())), |
85 observer_( | 84 observer_( |
86 new rtc::RefCountedObject<MockDtlsIdentityRequestObserver>()) { | 85 new rtc::RefCountedObject<MockDtlsIdentityRequestObserver>()) { |
87 CHECK(worker_thread_->Start()); | 86 CHECK(worker_thread_->Start()); |
88 store_->Initialize(); | |
89 } | 87 } |
90 ~DtlsIdentityStoreTest() {} | 88 ~DtlsIdentityStoreTest() {} |
91 | 89 |
92 static void SetUpTestCase() { | 90 static void SetUpTestCase() { |
93 rtc::InitializeSSL(); | 91 rtc::InitializeSSL(); |
94 } | 92 } |
95 static void TearDownTestCase() { | 93 static void TearDownTestCase() { |
96 rtc::CleanupSSL(); | 94 rtc::CleanupSSL(); |
97 } | 95 } |
98 | 96 |
99 rtc::scoped_ptr<rtc::Thread> worker_thread_; | 97 rtc::scoped_ptr<rtc::Thread> worker_thread_; |
100 rtc::scoped_ptr<DtlsIdentityStore> store_; | 98 rtc::scoped_ptr<DtlsIdentityStoreImpl> store_; |
101 rtc::scoped_refptr<MockDtlsIdentityRequestObserver> observer_; | 99 rtc::scoped_refptr<MockDtlsIdentityRequestObserver> observer_; |
102 }; | 100 }; |
103 | 101 |
104 TEST_F(DtlsIdentityStoreTest, RequestIdentitySuccess) { | 102 TEST_F(DtlsIdentityStoreTest, RequestIdentitySuccessRSA) { |
105 EXPECT_TRUE_WAIT(store_->HasFreeIdentityForTesting(), kTimeoutMs); | 103 EXPECT_TRUE_WAIT(store_->HasFreeIdentityForTesting(rtc::KT_RSA), kTimeoutMs); |
106 | 104 |
107 store_->RequestIdentity(observer_.get()); | 105 store_->RequestIdentity(rtc::KT_RSA, observer_.get()); |
108 EXPECT_TRUE_WAIT(observer_->LastRequestSucceeded(), kTimeoutMs); | 106 EXPECT_TRUE_WAIT(observer_->LastRequestSucceeded(), kTimeoutMs); |
109 | 107 |
110 EXPECT_TRUE_WAIT(store_->HasFreeIdentityForTesting(), kTimeoutMs); | 108 EXPECT_TRUE_WAIT(store_->HasFreeIdentityForTesting(rtc::KT_RSA), kTimeoutMs); |
111 | 109 |
112 observer_->Reset(); | 110 observer_->Reset(); |
113 | 111 |
114 // Verifies that the callback is async when a free identity is ready. | 112 // Verifies that the callback is async when a free identity is ready. |
115 store_->RequestIdentity(observer_.get()); | 113 store_->RequestIdentity(rtc::KT_RSA, observer_.get()); |
116 EXPECT_FALSE(observer_->call_back_called()); | 114 EXPECT_FALSE(observer_->call_back_called()); |
117 EXPECT_TRUE_WAIT(observer_->LastRequestSucceeded(), kTimeoutMs); | 115 EXPECT_TRUE_WAIT(observer_->LastRequestSucceeded(), kTimeoutMs); |
118 } | 116 } |
119 | 117 |
120 TEST_F(DtlsIdentityStoreTest, DeleteStoreEarlyNoCrash) { | 118 TEST_F(DtlsIdentityStoreTest, RequestIdentitySuccessECDSA) { |
121 EXPECT_FALSE(store_->HasFreeIdentityForTesting()); | 119 // Since store currently does not preemptively generate free ECDSA identities |
| 120 // we do not invoke HasFreeIdentityForTesting between requests. |
122 | 121 |
123 store_->RequestIdentity(observer_.get()); | 122 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 |
| 133 TEST_F(DtlsIdentityStoreTest, DeleteStoreEarlyNoCrashRSA) { |
| 134 EXPECT_FALSE(store_->HasFreeIdentityForTesting(rtc::KT_RSA)); |
| 135 |
| 136 store_->RequestIdentity(rtc::KT_RSA, observer_.get()); |
124 store_.reset(); | 137 store_.reset(); |
125 | 138 |
126 worker_thread_->Stop(); | 139 worker_thread_->Stop(); |
| 140 EXPECT_FALSE(observer_->call_back_called()); |
| 141 } |
| 142 |
| 143 TEST_F(DtlsIdentityStoreTest, DeleteStoreEarlyNoCrashECDSA) { |
| 144 EXPECT_FALSE(store_->HasFreeIdentityForTesting(rtc::KT_ECDSA)); |
| 145 |
| 146 store_->RequestIdentity(rtc::KT_ECDSA, observer_.get()); |
| 147 store_.reset(); |
| 148 |
| 149 worker_thread_->Stop(); |
127 EXPECT_FALSE(observer_->call_back_called()); | 150 EXPECT_FALSE(observer_->call_back_called()); |
128 } | 151 } |
129 | 152 |
OLD | NEW |