| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2015 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 #include "webrtc/api/dtlsidentitystore.h" | |
| 12 | |
| 13 #include <memory> | |
| 14 | |
| 15 #include "webrtc/api/webrtcsessiondescriptionfactory.h" | |
| 16 #include "webrtc/base/gunit.h" | |
| 17 #include "webrtc/base/logging.h" | |
| 18 #include "webrtc/base/ssladapter.h" | |
| 19 | |
| 20 using webrtc::DtlsIdentityStoreImpl; | |
| 21 | |
| 22 static const int kTimeoutMs = 10000; | |
| 23 | |
| 24 class MockDtlsIdentityRequestObserver : | |
| 25 public webrtc::DtlsIdentityRequestObserver { | |
| 26 public: | |
| 27 MockDtlsIdentityRequestObserver() | |
| 28 : call_back_called_(false), last_request_success_(false) {} | |
| 29 void OnFailure(int error) override { | |
| 30 EXPECT_FALSE(call_back_called_); | |
| 31 call_back_called_ = true; | |
| 32 last_request_success_ = false; | |
| 33 } | |
| 34 void OnSuccess(const std::string& der_cert, | |
| 35 const std::string& der_private_key) override { | |
| 36 LOG(LS_WARNING) << "The string version of OnSuccess is called unexpectedly"; | |
| 37 EXPECT_TRUE(false); | |
| 38 } | |
| 39 void OnSuccess(std::unique_ptr<rtc::SSLIdentity> identity) override { | |
| 40 EXPECT_FALSE(call_back_called_); | |
| 41 call_back_called_ = true; | |
| 42 last_request_success_ = true; | |
| 43 } | |
| 44 | |
| 45 void Reset() { | |
| 46 call_back_called_ = false; | |
| 47 last_request_success_ = false; | |
| 48 } | |
| 49 | |
| 50 bool LastRequestSucceeded() const { | |
| 51 return call_back_called_ && last_request_success_; | |
| 52 } | |
| 53 | |
| 54 bool call_back_called() const { | |
| 55 return call_back_called_; | |
| 56 } | |
| 57 | |
| 58 private: | |
| 59 bool call_back_called_; | |
| 60 bool last_request_success_; | |
| 61 }; | |
| 62 | |
| 63 class DtlsIdentityStoreTest : public testing::Test { | |
| 64 protected: | |
| 65 DtlsIdentityStoreTest() | |
| 66 : worker_thread_(new rtc::Thread()), | |
| 67 store_(new DtlsIdentityStoreImpl(rtc::Thread::Current(), | |
| 68 worker_thread_.get())), | |
| 69 observer_( | |
| 70 new rtc::RefCountedObject<MockDtlsIdentityRequestObserver>()) { | |
| 71 RTC_CHECK(worker_thread_->Start()); | |
| 72 } | |
| 73 ~DtlsIdentityStoreTest() {} | |
| 74 | |
| 75 static void SetUpTestCase() { | |
| 76 rtc::InitializeSSL(); | |
| 77 } | |
| 78 static void TearDownTestCase() { | |
| 79 rtc::CleanupSSL(); | |
| 80 } | |
| 81 | |
| 82 std::unique_ptr<rtc::Thread> worker_thread_; | |
| 83 std::unique_ptr<DtlsIdentityStoreImpl> store_; | |
| 84 rtc::scoped_refptr<MockDtlsIdentityRequestObserver> observer_; | |
| 85 }; | |
| 86 | |
| 87 TEST_F(DtlsIdentityStoreTest, RequestIdentitySuccessRSA) { | |
| 88 store_->RequestIdentity(rtc::KeyParams(rtc::KT_RSA), | |
| 89 rtc::Optional<uint64_t>(), | |
| 90 observer_.get()); | |
| 91 EXPECT_TRUE_WAIT(observer_->LastRequestSucceeded(), kTimeoutMs); | |
| 92 | |
| 93 EXPECT_TRUE_WAIT(store_->HasFreeIdentityForTesting(rtc::KT_RSA), kTimeoutMs); | |
| 94 | |
| 95 observer_->Reset(); | |
| 96 | |
| 97 // Verifies that the callback is async when a free identity is ready. | |
| 98 store_->RequestIdentity(rtc::KeyParams(rtc::KT_RSA), | |
| 99 rtc::Optional<uint64_t>(), | |
| 100 observer_.get()); | |
| 101 EXPECT_FALSE(observer_->call_back_called()); | |
| 102 EXPECT_TRUE_WAIT(observer_->LastRequestSucceeded(), kTimeoutMs); | |
| 103 } | |
| 104 | |
| 105 TEST_F(DtlsIdentityStoreTest, RequestIdentitySuccessECDSA) { | |
| 106 store_->RequestIdentity(rtc::KeyParams(rtc::KT_ECDSA), | |
| 107 rtc::Optional<uint64_t>(), | |
| 108 observer_.get()); | |
| 109 EXPECT_TRUE_WAIT(observer_->LastRequestSucceeded(), kTimeoutMs); | |
| 110 | |
| 111 // Since store currently does not preemptively generate free ECDSA identities | |
| 112 // we do not invoke HasFreeIdentityForTesting between requests. | |
| 113 | |
| 114 observer_->Reset(); | |
| 115 | |
| 116 // Verifies that the callback is async when a free identity is ready. | |
| 117 store_->RequestIdentity(rtc::KeyParams(rtc::KT_ECDSA), | |
| 118 rtc::Optional<uint64_t>(), | |
| 119 observer_.get()); | |
| 120 EXPECT_FALSE(observer_->call_back_called()); | |
| 121 EXPECT_TRUE_WAIT(observer_->LastRequestSucceeded(), kTimeoutMs); | |
| 122 } | |
| 123 | |
| 124 TEST_F(DtlsIdentityStoreTest, DeleteStoreEarlyNoCrashRSA) { | |
| 125 EXPECT_FALSE(store_->HasFreeIdentityForTesting(rtc::KT_RSA)); | |
| 126 | |
| 127 store_->RequestIdentity(rtc::KeyParams(rtc::KT_RSA), | |
| 128 rtc::Optional<uint64_t>(), | |
| 129 observer_.get()); | |
| 130 store_.reset(); | |
| 131 | |
| 132 worker_thread_->Stop(); | |
| 133 EXPECT_FALSE(observer_->call_back_called()); | |
| 134 } | |
| 135 | |
| 136 TEST_F(DtlsIdentityStoreTest, DeleteStoreEarlyNoCrashECDSA) { | |
| 137 EXPECT_FALSE(store_->HasFreeIdentityForTesting(rtc::KT_ECDSA)); | |
| 138 | |
| 139 store_->RequestIdentity(rtc::KeyParams(rtc::KT_ECDSA), | |
| 140 rtc::Optional<uint64_t>(), | |
| 141 observer_.get()); | |
| 142 store_.reset(); | |
| 143 | |
| 144 worker_thread_->Stop(); | |
| 145 EXPECT_FALSE(observer_->call_back_called()); | |
| 146 } | |
| 147 | |
| OLD | NEW |