OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 <stddef.h> | 5 #include <stddef.h> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/test/test_mock_time_task_runner.h" | 9 #include "base/test/test_mock_time_task_runner.h" |
10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
11 #include "blimp/common/create_blimp_message.h" | 11 #include "blimp/common/create_blimp_message.h" |
12 #include "blimp/common/proto/blimp_message.pb.h" | 12 #include "blimp/common/proto/blimp_message.pb.h" |
| 13 #include "blimp/common/protocol_version.h" |
13 #include "blimp/net/blimp_connection.h" | 14 #include "blimp/net/blimp_connection.h" |
14 #include "blimp/net/blimp_transport.h" | 15 #include "blimp/net/blimp_transport.h" |
15 #include "blimp/net/common.h" | 16 #include "blimp/net/common.h" |
16 #include "blimp/net/connection_error_observer.h" | 17 #include "blimp/net/connection_error_observer.h" |
17 #include "blimp/net/engine_authentication_handler.h" | 18 #include "blimp/net/engine_authentication_handler.h" |
18 #include "blimp/net/test_common.h" | 19 #include "blimp/net/test_common.h" |
19 #include "net/base/completion_callback.h" | 20 #include "net/base/completion_callback.h" |
20 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
21 #include "net/base/test_completion_callback.h" | 22 #include "net/base/test_completion_callback.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
24 | 25 |
25 using testing::_; | 26 using testing::_; |
26 using testing::Eq; | 27 using testing::Eq; |
| 28 using testing::Return; |
27 using testing::SaveArg; | 29 using testing::SaveArg; |
28 | 30 |
29 namespace blimp { | 31 namespace blimp { |
30 namespace { | 32 namespace { |
31 const char client_token[] = "valid token"; | 33 const char client_token[] = "valid token"; |
32 } // namespace | 34 } // namespace |
33 | 35 |
34 class EngineAuthenticationHandlerTest : public testing::Test { | 36 class EngineAuthenticationHandlerTest : public testing::Test { |
35 public: | 37 public: |
36 EngineAuthenticationHandlerTest() | 38 EngineAuthenticationHandlerTest() |
(...skipping 24 matching lines...) Expand all Loading... |
61 | 63 |
62 TEST_F(EngineAuthenticationHandlerTest, AuthenticationSucceeds) { | 64 TEST_F(EngineAuthenticationHandlerTest, AuthenticationSucceeds) { |
63 ExpectOnConnection(); | 65 ExpectOnConnection(); |
64 EXPECT_CALL(*connection_, RemoveConnectionErrorObserver(_)); | 66 EXPECT_CALL(*connection_, RemoveConnectionErrorObserver(_)); |
65 EXPECT_CALL(connection_handler_, HandleConnectionPtr(Eq(connection_.get()))); | 67 EXPECT_CALL(connection_handler_, HandleConnectionPtr(Eq(connection_.get()))); |
66 auth_handler_->HandleConnection(std::move(connection_)); | 68 auth_handler_->HandleConnection(std::move(connection_)); |
67 EXPECT_NE(nullptr, error_observer_); | 69 EXPECT_NE(nullptr, error_observer_); |
68 EXPECT_NE(nullptr, incoming_message_processor_); | 70 EXPECT_NE(nullptr, incoming_message_processor_); |
69 | 71 |
70 std::unique_ptr<BlimpMessage> blimp_message = | 72 std::unique_ptr<BlimpMessage> blimp_message = |
71 CreateStartConnectionMessage(client_token, 0); | 73 CreateStartConnectionMessage(client_token, kProtocolVersion); |
72 net::TestCompletionCallback process_message_cb; | 74 net::TestCompletionCallback process_message_cb; |
73 incoming_message_processor_->ProcessMessage(std::move(blimp_message), | 75 incoming_message_processor_->ProcessMessage(std::move(blimp_message), |
74 process_message_cb.callback()); | 76 process_message_cb.callback()); |
75 EXPECT_EQ(net::OK, process_message_cb.WaitForResult()); | 77 EXPECT_EQ(net::OK, process_message_cb.WaitForResult()); |
76 } | 78 } |
77 | 79 |
| 80 TEST_F(EngineAuthenticationHandlerTest, ProtocolMismatch) { |
| 81 const int kInvalidProtocolVersion = -1; |
| 82 |
| 83 BlimpMessage end_connection_message; |
| 84 MockBlimpMessageProcessor message_processor; |
| 85 EXPECT_CALL(message_processor, MockableProcessMessage(_, _)) |
| 86 .WillOnce(SaveArg<0>(&end_connection_message)); |
| 87 EXPECT_CALL(*connection_, GetOutgoingMessageProcessor()) |
| 88 .WillOnce(Return(&message_processor)); |
| 89 |
| 90 ExpectOnConnection(); |
| 91 auth_handler_->HandleConnection(std::move(connection_)); |
| 92 |
| 93 std::unique_ptr<BlimpMessage> blimp_message = |
| 94 CreateStartConnectionMessage(client_token, kInvalidProtocolVersion); |
| 95 net::TestCompletionCallback process_message_cb; |
| 96 incoming_message_processor_->ProcessMessage(std::move(blimp_message), |
| 97 process_message_cb.callback()); |
| 98 EXPECT_EQ(net::OK, process_message_cb.WaitForResult()); |
| 99 |
| 100 EXPECT_EQ(BlimpMessage::kProtocolControl, |
| 101 end_connection_message.feature_case()); |
| 102 EXPECT_EQ( |
| 103 ProtocolControlMessage::kEndConnection, |
| 104 end_connection_message.protocol_control().connection_message_case()); |
| 105 EXPECT_EQ( |
| 106 EndConnectionMessage::PROTOCOL_MISMATCH, |
| 107 end_connection_message.protocol_control().end_connection().reason()); |
| 108 } |
| 109 |
78 TEST_F(EngineAuthenticationHandlerTest, AuthenticationFailed) { | 110 TEST_F(EngineAuthenticationHandlerTest, AuthenticationFailed) { |
79 ExpectOnConnection(); | 111 ExpectOnConnection(); |
80 auth_handler_->HandleConnection(std::move(connection_)); | 112 auth_handler_->HandleConnection(std::move(connection_)); |
81 | 113 |
82 std::unique_ptr<BlimpMessage> blimp_message = | 114 std::unique_ptr<BlimpMessage> blimp_message = |
83 CreateStartConnectionMessage("invalid token", 0); | 115 CreateStartConnectionMessage("invalid token", kProtocolVersion); |
84 net::TestCompletionCallback process_message_cb; | 116 net::TestCompletionCallback process_message_cb; |
85 incoming_message_processor_->ProcessMessage(std::move(blimp_message), | 117 incoming_message_processor_->ProcessMessage(std::move(blimp_message), |
86 process_message_cb.callback()); | 118 process_message_cb.callback()); |
87 EXPECT_EQ(net::OK, process_message_cb.WaitForResult()); | 119 EXPECT_EQ(net::OK, process_message_cb.WaitForResult()); |
88 } | 120 } |
89 | 121 |
90 TEST_F(EngineAuthenticationHandlerTest, WrongMessageReceived) { | 122 TEST_F(EngineAuthenticationHandlerTest, WrongMessageReceived) { |
91 ExpectOnConnection(); | 123 ExpectOnConnection(); |
92 auth_handler_->HandleConnection(std::move(connection_)); | 124 auth_handler_->HandleConnection(std::move(connection_)); |
93 | 125 |
(...skipping 22 matching lines...) Expand all Loading... |
116 | 148 |
117 runner_->FastForwardBy(base::TimeDelta::FromSeconds(11)); | 149 runner_->FastForwardBy(base::TimeDelta::FromSeconds(11)); |
118 } | 150 } |
119 | 151 |
120 TEST_F(EngineAuthenticationHandlerTest, AuthHandlerDeletedFirst) { | 152 TEST_F(EngineAuthenticationHandlerTest, AuthHandlerDeletedFirst) { |
121 ExpectOnConnection(); | 153 ExpectOnConnection(); |
122 auth_handler_->HandleConnection(std::move(connection_)); | 154 auth_handler_->HandleConnection(std::move(connection_)); |
123 auth_handler_.reset(); | 155 auth_handler_.reset(); |
124 | 156 |
125 std::unique_ptr<BlimpMessage> blimp_message = | 157 std::unique_ptr<BlimpMessage> blimp_message = |
126 CreateStartConnectionMessage(client_token, 0); | 158 CreateStartConnectionMessage(client_token, kProtocolVersion); |
127 net::TestCompletionCallback process_message_cb; | 159 net::TestCompletionCallback process_message_cb; |
128 incoming_message_processor_->ProcessMessage(std::move(blimp_message), | 160 incoming_message_processor_->ProcessMessage(std::move(blimp_message), |
129 process_message_cb.callback()); | 161 process_message_cb.callback()); |
130 EXPECT_EQ(net::OK, process_message_cb.WaitForResult()); | 162 EXPECT_EQ(net::OK, process_message_cb.WaitForResult()); |
131 } | 163 } |
132 | 164 |
133 } // namespace blimp | 165 } // namespace blimp |
OLD | NEW |