Chromium Code Reviews| Index: content/browser/media/webrtc_identity_store_unittest.cc |
| diff --git a/content/browser/media/webrtc_identity_store_unittest.cc b/content/browser/media/webrtc_identity_store_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..83361d569744759073866ce4f48ce402b748192c |
| --- /dev/null |
| +++ b/content/browser/media/webrtc_identity_store_unittest.cc |
| @@ -0,0 +1,214 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/bind.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/message_loop.h" |
| +#include "base/run_loop.h" |
| +#include "base/synchronization/waitable_event.h" |
| +#include "base/threading/sequenced_worker_pool.h" |
| +#include "content/browser/media/webrtc_identity_store.h" |
| +#include "content/browser/renderer_host/media/webrtc_identity_service_host.h" |
| +#include "content/common/media/webrtc_identity_messages.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "content/public/test/test_utils.h" |
| +#include "googleurl/src/gurl.h" |
| +#include "net/base/net_errors.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace content { |
| + |
| +class WebRTCIdentityServiceHostForTest : public WebRTCIdentityServiceHost { |
| + public: |
| + explicit WebRTCIdentityServiceHostForTest( |
| + WebRTCIdentityStore* identity_store) |
| + : WebRTCIdentityServiceHost(identity_store) {} |
| + |
| + // content::BrowserMessageFilter override. |
| + virtual bool OnMessageReceived(const IPC::Message& message, |
| + bool* message_was_ok) OVERRIDE { |
| + return WebRTCIdentityServiceHost::OnMessageReceived(message, |
| + message_was_ok); |
|
Ryan Sleevi
2013/06/27 00:14:55
Why OVERRIDE this, when you're just forwarding to
|
| + } |
| + |
| + virtual void OnComplete(int request_id, |
| + int error, |
| + const std::string& certificate, |
| + const std::string& private_key) OVERRIDE { |
| + request_result_map_[request_id] = error; |
| + WebRTCIdentityServiceHost::OnComplete( |
| + request_id, error, certificate, private_key); |
| + } |
| + |
| + int GetRequestResultError(int request_id) { |
| + 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
|
| + return request_result_map_[request_id]; |
| + } |
| + |
| + private: |
| + virtual ~WebRTCIdentityServiceHostForTest() {} |
| + |
| + std::map<int, int> request_result_map_; |
| +}; |
| + |
| +class WebRTCIdentityStoreTest : public testing::Test { |
| + public: |
| + WebRTCIdentityStoreTest() |
| + : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| + sequenced_worker_pool_( |
| + new base::SequencedWorkerPool(3, "ServerBoundCertServiceTest")) { |
| + webrtc_identity_store_.reset( |
| + new WebRTCIdentityStore(sequenced_worker_pool_)); |
| + } |
| + |
| + virtual ~WebRTCIdentityStoreTest() { sequenced_worker_pool_->Shutdown(); } |
| + |
| + virtual void SignalWaitableEvent(const base::Closure& quit_task) { |
| + quit_task.Run(); |
| + } |
| + |
| + protected: |
| + TestBrowserThreadBundle browser_thread_bundle_; |
| + scoped_refptr<base::SequencedWorkerPool> sequenced_worker_pool_; |
| + scoped_ptr<WebRTCIdentityStore> webrtc_identity_store_; |
| +}; |
| + |
| +void OnRequestCompleted(bool* completed, |
| + int error, |
| + const std::string& certificate, |
| + const std::string& private_key) { |
| + ASSERT_EQ(net::OK, error); |
| + ASSERT_NE("", certificate); |
| + ASSERT_NE("", private_key); |
| + *completed = true; |
| +} |
| + |
| +TEST_F(WebRTCIdentityStoreTest, RequestOverMaxWithinOneSecRejected) { |
| + scoped_refptr<WebRTCIdentityServiceHostForTest> service_host( |
| + new WebRTCIdentityServiceHostForTest(webrtc_identity_store_.get())); |
| + |
| + int request_id = 0; |
| + const GURL origin("http://google.com"); |
| + const std::string identity_name = "a"; |
| + const std::string common_name = "b"; |
| + bool message_was_ok = true; |
| + |
| + while (request_id <= WebRTCIdentityServiceHost::MAX_REQUESTS_PER_SECOND) { |
| + service_host->OnMessageReceived(WebRTCIdentityMsg_RequestIdentity( |
| + 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
|
| + request_id++; |
| + } |
| + sequenced_worker_pool_->FlushForTesting(); |
| + base::RunLoop().RunUntilIdle(); |
| + for (request_id = 0; |
| + request_id <= WebRTCIdentityServiceHost::MAX_REQUESTS_PER_SECOND; |
| + request_id ++) { |
|
Ryan Sleevi
2013/06/27 00:14:55
style: eliminate extra whitespace
|
| + if (request_id < WebRTCIdentityServiceHost::MAX_REQUESTS_PER_SECOND) { |
| + EXPECT_EQ(net::OK, service_host->GetRequestResultError(request_id)); |
| + } else { |
| + EXPECT_EQ(net::ERR_INSUFFICIENT_RESOURCES, |
| + service_host->GetRequestResultError(request_id)); |
| + } |
| + } |
| +} |
| + |
| +TEST_F(WebRTCIdentityStoreTest, RequestOverMaxAfterOneSecAccepted) { |
| + scoped_refptr<WebRTCIdentityServiceHostForTest> service_host( |
| + new WebRTCIdentityServiceHostForTest(webrtc_identity_store_.get())); |
| + |
| + int request_id = 0; |
| + const GURL origin("http://google.com"); |
| + const std::string identity_name = "a"; |
| + const std::string common_name = "b"; |
| + bool message_was_ok = true; |
| + |
| + base::Time start = base::Time::NowFromSystemTime(); |
| + while (request_id < WebRTCIdentityServiceHost::MAX_REQUESTS_PER_SECOND) { |
| + service_host->OnMessageReceived(WebRTCIdentityMsg_RequestIdentity( |
| + request_id, origin, identity_name, common_name), &message_was_ok); |
| + request_id++; |
| + } |
| + sequenced_worker_pool_->FlushForTesting(); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + // Wait until 1 second elapses after the first request. |
| + base::TimeDelta delay = base::TimeDelta::FromSeconds(1) - |
| + (base::Time::NowFromSystemTime() - start); |
| + if (delay > base::TimeDelta()) { |
| + scoped_refptr<content::MessageLoopRunner> runner = |
| + new content::MessageLoopRunner; |
| + base::Timer timer(false, false); |
| + timer.Start(FROM_HERE, delay, |
| + base::Bind(&WebRTCIdentityStoreTest::SignalWaitableEvent, |
| + base::Unretained(this), runner->QuitClosure())); |
| + runner->Run(); |
| + } |
|
Ryan Sleevi
2013/06/27 00:14:55
This form of testing (effectively sleep(1)) is str
|
| + // This request should be accepted. |
| + service_host->OnMessageReceived(WebRTCIdentityMsg_RequestIdentity( |
| + request_id, origin, identity_name, common_name), &message_was_ok); |
| + |
| + sequenced_worker_pool_->FlushForTesting(); |
| + base::RunLoop().RunUntilIdle(); |
| + for (request_id = 0; |
| + request_id <= WebRTCIdentityServiceHost::MAX_REQUESTS_PER_SECOND; |
| + request_id ++) { |
| + EXPECT_EQ(net::OK, service_host->GetRequestResultError(request_id)); |
| + } |
| +} |
| + |
| +TEST_F(WebRTCIdentityStoreTest, RequestIdentity) { |
| + scoped_ptr<bool> completed(new bool(false)); |
| + base::Closure cancel_callback; |
| + webrtc_identity_store_->RequestIdentity( |
| + GURL("http://google.com"), |
| + "a", |
| + "b", |
| + base::Bind(&OnRequestCompleted, base::Unretained(completed.get())), |
| + &cancel_callback); |
| + sequenced_worker_pool_->FlushForTesting(); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(true, *completed); |
| +} |
| + |
| +TEST_F(WebRTCIdentityStoreTest, CancelRequest) { |
| + scoped_ptr<bool> completed(new bool(false)); |
| + base::Closure cancel_callback; |
| + bool success = webrtc_identity_store_->RequestIdentity( |
| + GURL("http://google.com"), |
| + "a", |
| + "b", |
| + base::Bind(&OnRequestCompleted, base::Unretained(completed.get())), |
| + &cancel_callback); |
| + EXPECT_TRUE(success); |
| + cancel_callback.Run(); |
| + sequenced_worker_pool_->FlushForTesting(); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(false, *completed); |
| +} |
| + |
| +TEST_F(WebRTCIdentityStoreTest, MultipleRequests) { |
| + scoped_ptr<bool> completed_1(new bool(false)); |
| + scoped_ptr<bool> completed_2(new bool(false)); |
| + base::Closure cancel_callback; |
| + webrtc_identity_store_->RequestIdentity( |
| + GURL("http://foo.com"), |
| + "a", |
| + "b", |
| + base::Bind(&OnRequestCompleted, base::Unretained(completed_1.get())), |
| + &cancel_callback); |
| + |
| + webrtc_identity_store_->RequestIdentity( |
| + GURL("http://bar.com"), |
| + "a", |
| + "b", |
| + base::Bind(&OnRequestCompleted, base::Unretained(completed_2.get())), |
| + &cancel_callback); |
| + |
| + sequenced_worker_pool_->FlushForTesting(); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(true, *completed_1); |
| + EXPECT_EQ(true, *completed_2); |
| +} |
| + |
| +} // namespace content |