Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(572)

Side by Side Diff: remoting/host/security_key/security_key_auth_handler_win_unittest.cc

Issue 2663103003: Fixing Intermittent SecurityKey Unittest Failures (Closed)
Patch Set: Addressing more feedback Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "remoting/host/security_key/security_key_auth_handler.h" 5 #include "remoting/host/security_key/security_key_auth_handler.h"
6 6
7 #include <cstdint> 7 #include <cstdint>
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h" 15 #include "base/run_loop.h"
16 #include "ipc/ipc_channel.h" 16 #include "ipc/ipc_channel.h"
17 #include "ipc/ipc_listener.h" 17 #include "ipc/ipc_listener.h"
18 #include "ipc/ipc_message.h" 18 #include "ipc/ipc_message.h"
19 #include "ipc/ipc_message_macros.h" 19 #include "ipc/ipc_message_macros.h"
20 #include "mojo/edk/embedder/scoped_ipc_support.h"
21 #include "remoting/host/host_mock_objects.h" 20 #include "remoting/host/host_mock_objects.h"
22 #include "remoting/host/security_key/fake_security_key_ipc_client.h" 21 #include "remoting/host/security_key/fake_security_key_ipc_client.h"
23 #include "remoting/host/security_key/fake_security_key_ipc_server.h" 22 #include "remoting/host/security_key/fake_security_key_ipc_server.h"
24 #include "remoting/host/security_key/security_key_ipc_constants.h" 23 #include "remoting/host/security_key/security_key_ipc_constants.h"
25 #include "testing/gmock/include/gmock/gmock.h" 24 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
27 26
28 namespace { 27 namespace {
29 const int kConnectionId1 = 1; 28 const int kConnectionId1 = 1;
30 const int kConnectionId2 = 2; 29 const int kConnectionId2 = 2;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 const base::WeakPtr<FakeSecurityKeyIpcServer>& fake_ipc_server, 80 const base::WeakPtr<FakeSecurityKeyIpcServer>& fake_ipc_server,
82 int connection_id); 81 int connection_id);
83 82
84 // Returns a unique IPC channel handle which prevents conflicts when running 83 // Returns a unique IPC channel handle which prevents conflicts when running
85 // tests concurrently. 84 // tests concurrently.
86 std::string GetUniqueTestChannelHandle(); 85 std::string GetUniqueTestChannelHandle();
87 86
88 // IPC tests require a valid MessageLoop to run. 87 // IPC tests require a valid MessageLoop to run.
89 base::MessageLoopForIO message_loop_; 88 base::MessageLoopForIO message_loop_;
90 89
91 mojo::edk::ScopedIPCSupport ipc_support_;
92
93 // Used to allow |message_loop_| to run during tests. The instance is reset 90 // Used to allow |message_loop_| to run during tests. The instance is reset
94 // after each stage of the tests has been completed. 91 // after each stage of the tests has been completed.
95 std::unique_ptr<base::RunLoop> run_loop_; 92 std::unique_ptr<base::RunLoop> run_loop_;
96 93
97 // The object under test. 94 // The object under test.
98 std::unique_ptr<SecurityKeyAuthHandler> auth_handler_; 95 std::unique_ptr<SecurityKeyAuthHandler> auth_handler_;
99 96
100 // Set as the default factory to create SecurityKeyIpcServerFactory 97 // Set as the default factory to create SecurityKeyIpcServerFactory
101 // instances, this class will track each objects creation and allow the tests 98 // instances, this class will track each objects creation and allow the tests
102 // to access it and use it for driving tests and validating state. 99 // to access it and use it for driving tests and validating state.
103 FakeSecurityKeyIpcServerFactory ipc_server_factory_; 100 FakeSecurityKeyIpcServerFactory ipc_server_factory_;
104 101
105 // Used to validate the object under test uses the correct ID when 102 // Used to validate the object under test uses the correct ID when
106 // communicating over the IPC channel. 103 // communicating over the IPC channel.
107 int last_connection_id_received_ = -1; 104 int last_connection_id_received_ = -1;
108 105
109 // Stores the contents of the last IPC message received for validation. 106 // Stores the contents of the last IPC message received for validation.
110 std::string last_message_received_; 107 std::string last_message_received_;
111 108
112 private: 109 private:
113 testing::NiceMock<MockClientSessionDetails> mock_client_session_details_; 110 testing::NiceMock<MockClientSessionDetails> mock_client_session_details_;
114 111
115 DISALLOW_COPY_AND_ASSIGN(SecurityKeyAuthHandlerWinTest); 112 DISALLOW_COPY_AND_ASSIGN(SecurityKeyAuthHandlerWinTest);
116 }; 113 };
117 114
118 SecurityKeyAuthHandlerWinTest::SecurityKeyAuthHandlerWinTest() 115 SecurityKeyAuthHandlerWinTest::SecurityKeyAuthHandlerWinTest()
119 : ipc_support_(message_loop_.task_runner(), 116 : run_loop_(new base::RunLoop()) {
120 mojo::edk::ScopedIPCSupport::ShutdownPolicy::FAST),
121 run_loop_(new base::RunLoop()) {
122 auth_handler_ = remoting::SecurityKeyAuthHandler::Create( 117 auth_handler_ = remoting::SecurityKeyAuthHandler::Create(
123 &mock_client_session_details_, 118 &mock_client_session_details_,
124 base::Bind(&SecurityKeyAuthHandlerWinTest::SendMessageToClient, 119 base::Bind(&SecurityKeyAuthHandlerWinTest::SendMessageToClient,
125 base::Unretained(this)), 120 base::Unretained(this)),
126 /*file_task_runner=*/nullptr); 121 /*file_task_runner=*/nullptr);
127 } 122 }
128 123
129 SecurityKeyAuthHandlerWinTest::~SecurityKeyAuthHandlerWinTest() {} 124 SecurityKeyAuthHandlerWinTest::~SecurityKeyAuthHandlerWinTest() {}
130 125
131 void SecurityKeyAuthHandlerWinTest::OperationComplete() { 126 void SecurityKeyAuthHandlerWinTest::OperationComplete() {
(...skipping 25 matching lines...) Expand all
157 152
158 void SecurityKeyAuthHandlerWinTest::EstablishIpcConnection( 153 void SecurityKeyAuthHandlerWinTest::EstablishIpcConnection(
159 FakeSecurityKeyIpcClient* fake_ipc_client, 154 FakeSecurityKeyIpcClient* fake_ipc_client,
160 int expected_connection_id, 155 int expected_connection_id,
161 const mojo::edk::NamedPlatformHandle& channel_handle, 156 const mojo::edk::NamedPlatformHandle& channel_handle,
162 bool close_connection) { 157 bool close_connection) {
163 size_t expected_connection_count = 158 size_t expected_connection_count =
164 auth_handler_->GetActiveConnectionCountForTest() + 1; 159 auth_handler_->GetActiveConnectionCountForTest() + 1;
165 160
166 ASSERT_FALSE(auth_handler_->IsValidConnectionId(expected_connection_id)); 161 ASSERT_FALSE(auth_handler_->IsValidConnectionId(expected_connection_id));
162 fake_ipc_client->set_on_channel_connected_callback(
163 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete,
164 base::Unretained(this)));
167 ASSERT_TRUE(fake_ipc_client->ConnectViaIpc(channel_handle)); 165 ASSERT_TRUE(fake_ipc_client->ConnectViaIpc(channel_handle));
168 WaitForOperationComplete(); 166 WaitForOperationComplete();
169 167
168 // Retrieve the IPC server instance created when the client connected.
169 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server =
170 ipc_server_factory_.GetIpcServerObject(expected_connection_id);
171 ASSERT_TRUE(fake_ipc_server.get());
172 fake_ipc_server->SendConnectionReadyMessage();
173 WaitForOperationComplete();
174
170 // Verify the internal state of the SecurityKeyAuthHandler is correct. 175 // Verify the internal state of the SecurityKeyAuthHandler is correct.
171 ASSERT_TRUE(auth_handler_->IsValidConnectionId(expected_connection_id)); 176 ASSERT_TRUE(auth_handler_->IsValidConnectionId(expected_connection_id));
172 ASSERT_EQ(expected_connection_count, 177 ASSERT_EQ(expected_connection_count,
173 auth_handler_->GetActiveConnectionCountForTest()); 178 auth_handler_->GetActiveConnectionCountForTest());
174 179
175 if (close_connection) { 180 if (close_connection) {
176 fake_ipc_client->CloseIpcConnection(); 181 fake_ipc_client->CloseIpcConnection();
177 WaitForOperationComplete(); 182 WaitForOperationComplete();
178 } 183 }
179 } 184 }
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 std::string SecurityKeyAuthHandlerWinTest::GetUniqueTestChannelHandle() { 246 std::string SecurityKeyAuthHandlerWinTest::GetUniqueTestChannelHandle() {
242 std::string channel_name("Uber_Awesome_Super_Mega_Test_Channel."); 247 std::string channel_name("Uber_Awesome_Super_Mega_Test_Channel.");
243 channel_name.append(IPC::Channel::GenerateUniqueRandomChannelID()); 248 channel_name.append(IPC::Channel::GenerateUniqueRandomChannelID());
244 249
245 return channel_name; 250 return channel_name;
246 } 251 }
247 252
248 TEST_F(SecurityKeyAuthHandlerWinTest, HandleSingleSecurityKeyRequest) { 253 TEST_F(SecurityKeyAuthHandlerWinTest, HandleSingleSecurityKeyRequest) {
249 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelHandle()); 254 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelHandle());
250 CreateSecurityKeyConnection(channel_handle); 255 CreateSecurityKeyConnection(channel_handle);
251
252 ASSERT_FALSE(auth_handler_->IsValidConnectionId(kConnectionId1)); 256 ASSERT_FALSE(auth_handler_->IsValidConnectionId(kConnectionId1));
253 257
254 // Create a fake client and connect to the IPC server channel. 258 // Create a fake client and connect to the IPC server channel.
255 FakeSecurityKeyIpcClient fake_ipc_client( 259 FakeSecurityKeyIpcClient fake_ipc_client(
256 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, 260 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete,
257 base::Unretained(this))); 261 base::Unretained(this)));
258 EstablishIpcConnection(&fake_ipc_client, kConnectionId1, channel_handle, 262 EstablishIpcConnection(&fake_ipc_client, kConnectionId1, channel_handle,
259 /*close_connection=*/true); 263 /*close_connection=*/true);
260 264
261 // Retrieve the IPC server instance created when the client connected. 265 // Retrieve the IPC server instance created when the client connected.
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 ASSERT_FALSE(fake_ipc_server.get()); 427 ASSERT_FALSE(fake_ipc_server.get());
424 ASSERT_FALSE(auth_handler_->IsValidConnectionId(kConnectionId1)); 428 ASSERT_FALSE(auth_handler_->IsValidConnectionId(kConnectionId1));
425 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); 429 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest());
426 430
427 // Attempt to connect again after the error. 431 // Attempt to connect again after the error.
428 EstablishIpcConnection(&fake_ipc_client, kConnectionId2, channel_handle, 432 EstablishIpcConnection(&fake_ipc_client, kConnectionId2, channel_handle,
429 /*close_connection=*/true); 433 /*close_connection=*/true);
430 } 434 }
431 435
432 } // namespace remoting 436 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698