OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 <stddef.h> | |
6 #include <memory> | |
7 #include <string> | |
8 #include <utility> | |
9 | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/test/test_mock_time_task_runner.h" | |
12 #include "base/threading/thread_task_runner_handle.h" | |
13 #include "blimp/common/create_blimp_message.h" | |
14 #include "blimp/common/proto/blimp_message.pb.h" | |
15 #include "blimp/common/protocol_version.h" | |
16 #include "blimp/net/blimp_connection.h" | |
17 #include "blimp/net/blimp_transport.h" | |
18 #include "blimp/net/common.h" | |
19 #include "blimp/net/connection_error_observer.h" | |
20 #include "blimp/net/engine_authentication_handler.h" | |
21 #include "blimp/net/test_common.h" | |
22 #include "net/base/completion_callback.h" | |
23 #include "net/base/net_errors.h" | |
24 #include "net/base/test_completion_callback.h" | |
25 #include "testing/gmock/include/gmock/gmock.h" | |
26 #include "testing/gtest/include/gtest/gtest.h" | |
27 | |
28 using testing::_; | |
29 using testing::Eq; | |
30 using testing::Return; | |
31 using testing::SaveArg; | |
32 | |
33 namespace blimp { | |
34 namespace { | |
35 const char client_auth_token[] = "valid token"; | |
36 } // namespace | |
37 | |
38 class EngineAuthenticationHandlerTest : public testing::Test { | |
39 public: | |
40 EngineAuthenticationHandlerTest() | |
41 : runner_(new base::TestMockTimeTaskRunner), | |
42 runner_handle_(runner_), | |
43 auth_handler_(new EngineAuthenticationHandler(&connection_handler_, | |
44 client_auth_token)), | |
45 connection_(new testing::StrictMock<MockBlimpConnection>()) {} | |
46 | |
47 ~EngineAuthenticationHandlerTest() override {} | |
48 | |
49 protected: | |
50 void ExpectOnConnection() { | |
51 EXPECT_CALL(*connection_, AddConnectionErrorObserver(_)) | |
52 .WillOnce(SaveArg<0>(&error_observer_)); | |
53 EXPECT_CALL(*connection_, SetIncomingMessageProcessor(_)) | |
54 .WillOnce(SaveArg<0>(&incoming_message_processor_)); | |
55 } | |
56 | |
57 scoped_refptr<base::TestMockTimeTaskRunner> runner_; | |
58 base::ThreadTaskRunnerHandle runner_handle_; | |
59 testing::StrictMock<MockConnectionHandler> connection_handler_; | |
60 std::unique_ptr<EngineAuthenticationHandler> auth_handler_; | |
61 std::unique_ptr<testing::StrictMock<MockBlimpConnection>> connection_; | |
62 ConnectionErrorObserver* error_observer_ = nullptr; | |
63 BlimpMessageProcessor* incoming_message_processor_ = nullptr; | |
64 }; | |
65 | |
66 TEST_F(EngineAuthenticationHandlerTest, AuthenticationSucceeds) { | |
67 ExpectOnConnection(); | |
68 EXPECT_CALL(*connection_, RemoveConnectionErrorObserver(_)); | |
69 EXPECT_CALL(connection_handler_, HandleConnectionPtr(Eq(connection_.get()))); | |
70 auth_handler_->HandleConnection(std::move(connection_)); | |
71 EXPECT_NE(nullptr, error_observer_); | |
72 EXPECT_NE(nullptr, incoming_message_processor_); | |
73 | |
74 std::unique_ptr<BlimpMessage> blimp_message = | |
75 CreateStartConnectionMessage(client_auth_token, kProtocolVersion); | |
76 net::TestCompletionCallback process_message_cb; | |
77 incoming_message_processor_->ProcessMessage(std::move(blimp_message), | |
78 process_message_cb.callback()); | |
79 EXPECT_EQ(net::OK, process_message_cb.WaitForResult()); | |
80 } | |
81 | |
82 TEST_F(EngineAuthenticationHandlerTest, ProtocolMismatch) { | |
83 const int kInvalidProtocolVersion = -1; | |
84 | |
85 BlimpMessage end_connection_message; | |
86 MockBlimpMessageProcessor message_processor; | |
87 EXPECT_CALL(message_processor, MockableProcessMessage(_, _)) | |
88 .WillOnce(SaveArg<0>(&end_connection_message)); | |
89 EXPECT_CALL(*connection_, GetOutgoingMessageProcessor()) | |
90 .WillOnce(Return(&message_processor)); | |
91 | |
92 ExpectOnConnection(); | |
93 auth_handler_->HandleConnection(std::move(connection_)); | |
94 | |
95 std::unique_ptr<BlimpMessage> blimp_message = | |
96 CreateStartConnectionMessage(client_auth_token, kInvalidProtocolVersion); | |
97 net::TestCompletionCallback process_message_cb; | |
98 incoming_message_processor_->ProcessMessage(std::move(blimp_message), | |
99 process_message_cb.callback()); | |
100 EXPECT_EQ(net::OK, process_message_cb.WaitForResult()); | |
101 | |
102 EXPECT_EQ(BlimpMessage::kProtocolControl, | |
103 end_connection_message.feature_case()); | |
104 EXPECT_EQ( | |
105 ProtocolControlMessage::kEndConnection, | |
106 end_connection_message.protocol_control().connection_message_case()); | |
107 EXPECT_EQ( | |
108 EndConnectionMessage::PROTOCOL_MISMATCH, | |
109 end_connection_message.protocol_control().end_connection().reason()); | |
110 } | |
111 | |
112 TEST_F(EngineAuthenticationHandlerTest, AuthenticationFailed) { | |
113 ExpectOnConnection(); | |
114 auth_handler_->HandleConnection(std::move(connection_)); | |
115 | |
116 std::unique_ptr<BlimpMessage> blimp_message = | |
117 CreateStartConnectionMessage("invalid token", kProtocolVersion); | |
118 net::TestCompletionCallback process_message_cb; | |
119 incoming_message_processor_->ProcessMessage(std::move(blimp_message), | |
120 process_message_cb.callback()); | |
121 EXPECT_EQ(net::OK, process_message_cb.WaitForResult()); | |
122 } | |
123 | |
124 TEST_F(EngineAuthenticationHandlerTest, WrongMessageReceived) { | |
125 ExpectOnConnection(); | |
126 auth_handler_->HandleConnection(std::move(connection_)); | |
127 | |
128 InputMessage* input_message; | |
129 std::unique_ptr<BlimpMessage> blimp_message = | |
130 CreateBlimpMessage(&input_message); | |
131 net::TestCompletionCallback process_message_cb; | |
132 incoming_message_processor_->ProcessMessage(std::move(blimp_message), | |
133 process_message_cb.callback()); | |
134 EXPECT_EQ(net::OK, process_message_cb.WaitForResult()); | |
135 } | |
136 | |
137 TEST_F(EngineAuthenticationHandlerTest, ConnectionError) { | |
138 ExpectOnConnection(); | |
139 auth_handler_->HandleConnection(std::move(connection_)); | |
140 EXPECT_NE(nullptr, error_observer_); | |
141 EXPECT_NE(nullptr, incoming_message_processor_); | |
142 error_observer_->OnConnectionError(net::ERR_FAILED); | |
143 } | |
144 | |
145 TEST_F(EngineAuthenticationHandlerTest, Timeout) { | |
146 ExpectOnConnection(); | |
147 auth_handler_->HandleConnection(std::move(connection_)); | |
148 EXPECT_NE(nullptr, error_observer_); | |
149 EXPECT_NE(nullptr, incoming_message_processor_); | |
150 | |
151 runner_->FastForwardBy(base::TimeDelta::FromSeconds(11)); | |
152 } | |
153 | |
154 TEST_F(EngineAuthenticationHandlerTest, AuthHandlerDeletedFirst) { | |
155 ExpectOnConnection(); | |
156 auth_handler_->HandleConnection(std::move(connection_)); | |
157 auth_handler_.reset(); | |
158 | |
159 std::unique_ptr<BlimpMessage> blimp_message = | |
160 CreateStartConnectionMessage(client_auth_token, kProtocolVersion); | |
161 net::TestCompletionCallback process_message_cb; | |
162 incoming_message_processor_->ProcessMessage(std::move(blimp_message), | |
163 process_message_cb.callback()); | |
164 EXPECT_EQ(net::OK, process_message_cb.WaitForResult()); | |
165 } | |
166 | |
167 } // namespace blimp | |
OLD | NEW |