Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/bind.h" | |
| 6 #include "base/memory/scoped_ptr.h" | |
| 7 #include "base/message_loop.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "base/synchronization/waitable_event.h" | |
| 10 #include "base/threading/sequenced_worker_pool.h" | |
| 11 #include "content/browser/media/webrtc_identity_store.h" | |
| 12 #include "content/browser/renderer_host/media/webrtc_identity_service_host.h" | |
| 13 #include "content/common/media/webrtc_identity_messages.h" | |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | |
| 15 #include "content/public/test/test_utils.h" | |
| 16 #include "googleurl/src/gurl.h" | |
| 17 #include "net/base/net_errors.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 namespace content { | |
| 21 | |
| 22 class WebRTCIdentityServiceHostForTest : public WebRTCIdentityServiceHost { | |
| 23 public: | |
| 24 explicit WebRTCIdentityServiceHostForTest( | |
| 25 WebRTCIdentityStore* identity_store) | |
| 26 : WebRTCIdentityServiceHost(identity_store) {} | |
| 27 | |
| 28 // content::BrowserMessageFilter override. | |
| 29 virtual bool OnMessageReceived(const IPC::Message& message, | |
| 30 bool* message_was_ok) OVERRIDE { | |
| 31 return WebRTCIdentityServiceHost::OnMessageReceived(message, | |
| 32 message_was_ok); | |
|
Ryan Sleevi
2013/06/27 00:14:55
Why OVERRIDE this, when you're just forwarding to
| |
| 33 } | |
| 34 | |
| 35 virtual void OnComplete(int request_id, | |
| 36 int error, | |
| 37 const std::string& certificate, | |
| 38 const std::string& private_key) OVERRIDE { | |
| 39 request_result_map_[request_id] = error; | |
| 40 WebRTCIdentityServiceHost::OnComplete( | |
| 41 request_id, error, certificate, private_key); | |
| 42 } | |
| 43 | |
| 44 int GetRequestResultError(int request_id) { | |
| 45 DCHECK(request_result_map_.find(request_id) != request_result_map_.end()); | |
|
Ryan Sleevi
2013/06/27 00:14:55
Don't use DCHECK in tests - it will crash the harn
| |
| 46 return request_result_map_[request_id]; | |
| 47 } | |
| 48 | |
| 49 private: | |
| 50 virtual ~WebRTCIdentityServiceHostForTest() {} | |
| 51 | |
| 52 std::map<int, int> request_result_map_; | |
| 53 }; | |
| 54 | |
| 55 class WebRTCIdentityStoreTest : public testing::Test { | |
| 56 public: | |
| 57 WebRTCIdentityStoreTest() | |
| 58 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | |
| 59 sequenced_worker_pool_( | |
| 60 new base::SequencedWorkerPool(3, "ServerBoundCertServiceTest")) { | |
| 61 webrtc_identity_store_.reset( | |
| 62 new WebRTCIdentityStore(sequenced_worker_pool_)); | |
| 63 } | |
| 64 | |
| 65 virtual ~WebRTCIdentityStoreTest() { sequenced_worker_pool_->Shutdown(); } | |
| 66 | |
| 67 virtual void SignalWaitableEvent(const base::Closure& quit_task) { | |
| 68 quit_task.Run(); | |
| 69 } | |
| 70 | |
| 71 protected: | |
| 72 TestBrowserThreadBundle browser_thread_bundle_; | |
| 73 scoped_refptr<base::SequencedWorkerPool> sequenced_worker_pool_; | |
| 74 scoped_ptr<WebRTCIdentityStore> webrtc_identity_store_; | |
| 75 }; | |
| 76 | |
| 77 void OnRequestCompleted(bool* completed, | |
| 78 int error, | |
| 79 const std::string& certificate, | |
| 80 const std::string& private_key) { | |
| 81 ASSERT_EQ(net::OK, error); | |
| 82 ASSERT_NE("", certificate); | |
| 83 ASSERT_NE("", private_key); | |
| 84 *completed = true; | |
| 85 } | |
| 86 | |
| 87 TEST_F(WebRTCIdentityStoreTest, RequestOverMaxWithinOneSecRejected) { | |
| 88 scoped_refptr<WebRTCIdentityServiceHostForTest> service_host( | |
| 89 new WebRTCIdentityServiceHostForTest(webrtc_identity_store_.get())); | |
| 90 | |
| 91 int request_id = 0; | |
| 92 const GURL origin("http://google.com"); | |
| 93 const std::string identity_name = "a"; | |
| 94 const std::string common_name = "b"; | |
| 95 bool message_was_ok = true; | |
| 96 | |
| 97 while (request_id <= WebRTCIdentityServiceHost::MAX_REQUESTS_PER_SECOND) { | |
| 98 service_host->OnMessageReceived(WebRTCIdentityMsg_RequestIdentity( | |
| 99 request_id, origin, identity_name, common_name), &message_was_ok); | |
|
Ryan Sleevi
2013/06/27 00:14:55
This seems like you're actually going to kick off
| |
| 100 request_id++; | |
| 101 } | |
| 102 sequenced_worker_pool_->FlushForTesting(); | |
| 103 base::RunLoop().RunUntilIdle(); | |
| 104 for (request_id = 0; | |
| 105 request_id <= WebRTCIdentityServiceHost::MAX_REQUESTS_PER_SECOND; | |
| 106 request_id ++) { | |
|
Ryan Sleevi
2013/06/27 00:14:55
style: eliminate extra whitespace
| |
| 107 if (request_id < WebRTCIdentityServiceHost::MAX_REQUESTS_PER_SECOND) { | |
| 108 EXPECT_EQ(net::OK, service_host->GetRequestResultError(request_id)); | |
| 109 } else { | |
| 110 EXPECT_EQ(net::ERR_INSUFFICIENT_RESOURCES, | |
| 111 service_host->GetRequestResultError(request_id)); | |
| 112 } | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 TEST_F(WebRTCIdentityStoreTest, RequestOverMaxAfterOneSecAccepted) { | |
| 117 scoped_refptr<WebRTCIdentityServiceHostForTest> service_host( | |
| 118 new WebRTCIdentityServiceHostForTest(webrtc_identity_store_.get())); | |
| 119 | |
| 120 int request_id = 0; | |
| 121 const GURL origin("http://google.com"); | |
| 122 const std::string identity_name = "a"; | |
| 123 const std::string common_name = "b"; | |
| 124 bool message_was_ok = true; | |
| 125 | |
| 126 base::Time start = base::Time::NowFromSystemTime(); | |
| 127 while (request_id < WebRTCIdentityServiceHost::MAX_REQUESTS_PER_SECOND) { | |
| 128 service_host->OnMessageReceived(WebRTCIdentityMsg_RequestIdentity( | |
| 129 request_id, origin, identity_name, common_name), &message_was_ok); | |
| 130 request_id++; | |
| 131 } | |
| 132 sequenced_worker_pool_->FlushForTesting(); | |
| 133 base::RunLoop().RunUntilIdle(); | |
| 134 | |
| 135 // Wait until 1 second elapses after the first request. | |
| 136 base::TimeDelta delay = base::TimeDelta::FromSeconds(1) - | |
| 137 (base::Time::NowFromSystemTime() - start); | |
| 138 if (delay > base::TimeDelta()) { | |
| 139 scoped_refptr<content::MessageLoopRunner> runner = | |
| 140 new content::MessageLoopRunner; | |
| 141 base::Timer timer(false, false); | |
| 142 timer.Start(FROM_HERE, delay, | |
| 143 base::Bind(&WebRTCIdentityStoreTest::SignalWaitableEvent, | |
| 144 base::Unretained(this), runner->QuitClosure())); | |
| 145 runner->Run(); | |
| 146 } | |
|
Ryan Sleevi
2013/06/27 00:14:55
This form of testing (effectively sleep(1)) is str
| |
| 147 // This request should be accepted. | |
| 148 service_host->OnMessageReceived(WebRTCIdentityMsg_RequestIdentity( | |
| 149 request_id, origin, identity_name, common_name), &message_was_ok); | |
| 150 | |
| 151 sequenced_worker_pool_->FlushForTesting(); | |
| 152 base::RunLoop().RunUntilIdle(); | |
| 153 for (request_id = 0; | |
| 154 request_id <= WebRTCIdentityServiceHost::MAX_REQUESTS_PER_SECOND; | |
| 155 request_id ++) { | |
| 156 EXPECT_EQ(net::OK, service_host->GetRequestResultError(request_id)); | |
| 157 } | |
| 158 } | |
| 159 | |
| 160 TEST_F(WebRTCIdentityStoreTest, RequestIdentity) { | |
| 161 scoped_ptr<bool> completed(new bool(false)); | |
| 162 base::Closure cancel_callback; | |
| 163 webrtc_identity_store_->RequestIdentity( | |
| 164 GURL("http://google.com"), | |
| 165 "a", | |
| 166 "b", | |
| 167 base::Bind(&OnRequestCompleted, base::Unretained(completed.get())), | |
| 168 &cancel_callback); | |
| 169 sequenced_worker_pool_->FlushForTesting(); | |
| 170 base::RunLoop().RunUntilIdle(); | |
| 171 EXPECT_EQ(true, *completed); | |
| 172 } | |
| 173 | |
| 174 TEST_F(WebRTCIdentityStoreTest, CancelRequest) { | |
| 175 scoped_ptr<bool> completed(new bool(false)); | |
| 176 base::Closure cancel_callback; | |
| 177 bool success = webrtc_identity_store_->RequestIdentity( | |
| 178 GURL("http://google.com"), | |
| 179 "a", | |
| 180 "b", | |
| 181 base::Bind(&OnRequestCompleted, base::Unretained(completed.get())), | |
| 182 &cancel_callback); | |
| 183 EXPECT_TRUE(success); | |
| 184 cancel_callback.Run(); | |
| 185 sequenced_worker_pool_->FlushForTesting(); | |
| 186 base::RunLoop().RunUntilIdle(); | |
| 187 EXPECT_EQ(false, *completed); | |
| 188 } | |
| 189 | |
| 190 TEST_F(WebRTCIdentityStoreTest, MultipleRequests) { | |
| 191 scoped_ptr<bool> completed_1(new bool(false)); | |
| 192 scoped_ptr<bool> completed_2(new bool(false)); | |
| 193 base::Closure cancel_callback; | |
| 194 webrtc_identity_store_->RequestIdentity( | |
| 195 GURL("http://foo.com"), | |
| 196 "a", | |
| 197 "b", | |
| 198 base::Bind(&OnRequestCompleted, base::Unretained(completed_1.get())), | |
| 199 &cancel_callback); | |
| 200 | |
| 201 webrtc_identity_store_->RequestIdentity( | |
| 202 GURL("http://bar.com"), | |
| 203 "a", | |
| 204 "b", | |
| 205 base::Bind(&OnRequestCompleted, base::Unretained(completed_2.get())), | |
| 206 &cancel_callback); | |
| 207 | |
| 208 sequenced_worker_pool_->FlushForTesting(); | |
| 209 base::RunLoop().RunUntilIdle(); | |
| 210 EXPECT_EQ(true, *completed_1); | |
| 211 EXPECT_EQ(true, *completed_2); | |
| 212 } | |
| 213 | |
| 214 } // namespace content | |
| OLD | NEW |