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 "blimp/net/client_connection_manager.h" | 5 #include "blimp/net/client_connection_manager.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "blimp/common/create_blimp_message.h" | 14 #include "blimp/common/create_blimp_message.h" |
15 #include "blimp/common/proto/blimp_message.pb.h" | 15 #include "blimp/common/proto/blimp_message.pb.h" |
16 #include "blimp/common/protocol_version.h" | 16 #include "blimp/common/protocol_version.h" |
17 #include "blimp/net/test_common.h" | 17 #include "blimp/net/test_common.h" |
18 #include "net/base/completion_callback.h" | 18 #include "net/base/completion_callback.h" |
19 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
20 #include "net/base/test_completion_callback.h" | 20 #include "net/base/test_completion_callback.h" |
| 21 #include "testing/gmock/include/gmock/gmock-more-actions.h" |
21 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
23 | 24 |
24 using testing::_; | 25 using testing::_; |
25 using testing::Eq; | 26 using testing::Eq; |
26 using testing::Return; | 27 using testing::Return; |
27 using testing::SaveArg; | 28 using testing::SaveArg; |
28 | 29 |
29 namespace blimp { | 30 namespace blimp { |
30 namespace { | 31 namespace { |
31 const char kDummyClientAuthToken[] = "dummy-client-token"; | 32 const char kDummyClientAuthToken[] = "dummy-client-token"; |
32 } // namespace | 33 } // namespace |
33 | 34 |
34 class ClientConnectionManagerTest : public testing::Test { | 35 class ClientConnectionManagerTest : public testing::Test { |
35 public: | 36 public: |
36 ClientConnectionManagerTest() | 37 ClientConnectionManagerTest() |
37 : manager_(new ClientConnectionManager(&connection_handler_)), | 38 : manager_(new ClientConnectionManager(&connection_handler_)), |
38 transport1_(new testing::StrictMock<MockTransport>), | 39 transport1_(new testing::StrictMock<MockTransport>), |
39 transport2_(new testing::StrictMock<MockTransport>), | 40 transport2_(new testing::StrictMock<MockTransport>), |
40 reader_(new testing::StrictMock<MockPacketReader>), | 41 connection_(new testing::StrictMock<MockBlimpConnection>), |
41 writer_(new testing::StrictMock<MockPacketWriter>), | |
42 start_connection_message_( | 42 start_connection_message_( |
43 CreateStartConnectionMessage(kDummyClientAuthToken, | 43 CreateStartConnectionMessage(kDummyClientAuthToken, |
44 kProtocolVersion)) { | 44 kProtocolVersion)), |
| 45 message_capture_(new testing::StrictMock<MockBlimpMessageProcessor>) { |
45 manager_->set_client_auth_token(kDummyClientAuthToken); | 46 manager_->set_client_auth_token(kDummyClientAuthToken); |
46 } | 47 } |
47 | 48 |
48 ~ClientConnectionManagerTest() override {} | 49 ~ClientConnectionManagerTest() override {} |
49 | 50 |
50 protected: | 51 protected: |
51 base::MessageLoop message_loop_; | 52 base::MessageLoop message_loop_; |
52 testing::StrictMock<MockConnectionHandler> connection_handler_; | 53 testing::StrictMock<MockConnectionHandler> connection_handler_; |
53 std::unique_ptr<ClientConnectionManager> manager_; | 54 std::unique_ptr<ClientConnectionManager> manager_; |
54 std::unique_ptr<testing::StrictMock<MockTransport>> transport1_; | 55 std::unique_ptr<testing::StrictMock<MockTransport>> transport1_; |
55 std::unique_ptr<testing::StrictMock<MockTransport>> transport2_; | 56 std::unique_ptr<testing::StrictMock<MockTransport>> transport2_; |
56 std::unique_ptr<MockPacketReader> reader_; | 57 std::unique_ptr<testing::StrictMock<MockBlimpConnection>> connection_; |
57 std::unique_ptr<MockPacketWriter> writer_; | |
58 std::unique_ptr<BlimpMessage> start_connection_message_; | 58 std::unique_ptr<BlimpMessage> start_connection_message_; |
| 59 std::unique_ptr<testing::StrictMock<MockBlimpMessageProcessor>> |
| 60 message_capture_; |
59 }; | 61 }; |
60 | 62 |
61 // The 1st transport connects, and the 2nd transport is not used. | 63 // Tests that the transport connection works. |
62 TEST_F(ClientConnectionManagerTest, FirstTransportConnects) { | 64 TEST_F(ClientConnectionManagerTest, FirstTransportConnects) { |
63 net::CompletionCallback write_cb; | 65 net::CompletionCallback write_cb; |
64 net::CompletionCallback connect_cb_1; | 66 net::CompletionCallback connect_cb_1; |
65 EXPECT_CALL(*transport1_, Connect(_)).WillOnce(SaveArg<0>(&connect_cb_1)); | 67 EXPECT_CALL(*transport1_, Connect(_)).WillOnce(SaveArg<0>(&connect_cb_1)); |
66 EXPECT_CALL(connection_handler_, HandleConnectionPtr(_)); | 68 EXPECT_CALL(connection_handler_, HandleConnectionPtr(_)); |
67 EXPECT_CALL(*writer_, | 69 EXPECT_CALL( |
68 WritePacket(BufferEqualsProto(*start_connection_message_), _)) | 70 *message_capture_, |
| 71 MockableProcessMessage(EqualsProto(*start_connection_message_), _)) |
69 .WillOnce(SaveArg<1>(&write_cb)); | 72 .WillOnce(SaveArg<1>(&write_cb)); |
| 73 EXPECT_CALL(*connection_, AddConnectionErrorObserver(_)); |
| 74 EXPECT_CALL(*connection_, GetOutgoingMessageProcessor()) |
| 75 .WillOnce(Return(message_capture_.get())); |
70 | 76 |
71 EXPECT_CALL(*transport1_, TakeMessagePortPtr()) | 77 transport1_->SetMockConnection(std::move(connection_)); |
72 .WillOnce( | |
73 Return(new MessagePort(std::move(reader_), std::move(writer_)))); | |
74 | |
75 EXPECT_TRUE(connect_cb_1.is_null()); | 78 EXPECT_TRUE(connect_cb_1.is_null()); |
76 manager_->AddTransport(std::move(transport1_)); | 79 manager_->AddTransport(std::move(transport1_)); |
77 manager_->AddTransport(std::move(transport2_)); | 80 manager_->AddTransport(std::move(transport2_)); |
78 manager_->Connect(); | 81 manager_->Connect(); |
79 EXPECT_FALSE(connect_cb_1.is_null()); | 82 EXPECT_FALSE(connect_cb_1.is_null()); |
80 base::ResetAndReturn(&connect_cb_1).Run(net::OK); | 83 base::ResetAndReturn(&connect_cb_1).Run(net::OK); |
81 base::ResetAndReturn(&write_cb).Run(net::OK); | 84 base::ResetAndReturn(&write_cb).Run(net::OK); |
82 } | 85 } |
83 | 86 |
84 // The 1st transport fails to connect, and the 2nd transport connects. | 87 // The 1st transport fails to connect, and the 2nd transport connects. |
85 TEST_F(ClientConnectionManagerTest, SecondTransportConnects) { | 88 TEST_F(ClientConnectionManagerTest, SecondTransportConnects) { |
86 net::CompletionCallback write_cb; | 89 net::CompletionCallback write_cb; |
87 net::CompletionCallback connect_cb_1; | 90 net::CompletionCallback connect_cb_1; |
88 EXPECT_CALL(*transport1_, Connect(_)).WillOnce(SaveArg<0>(&connect_cb_1)); | 91 EXPECT_CALL(*transport1_, Connect(_)).WillOnce(SaveArg<0>(&connect_cb_1)); |
89 net::CompletionCallback connect_cb_2; | 92 net::CompletionCallback connect_cb_2; |
90 EXPECT_CALL(*transport2_, Connect(_)).WillOnce(SaveArg<0>(&connect_cb_2)); | 93 EXPECT_CALL(*transport2_, Connect(_)).WillOnce(SaveArg<0>(&connect_cb_2)); |
91 EXPECT_CALL(*writer_, | 94 EXPECT_CALL( |
92 WritePacket(BufferEqualsProto(*start_connection_message_), _)) | 95 *message_capture_, |
| 96 MockableProcessMessage(EqualsProto(*start_connection_message_), _)) |
93 .WillOnce(SaveArg<1>(&write_cb)); | 97 .WillOnce(SaveArg<1>(&write_cb)); |
94 EXPECT_CALL(connection_handler_, HandleConnectionPtr(_)); | 98 EXPECT_CALL(*connection_, AddConnectionErrorObserver(_)); |
95 EXPECT_CALL(*transport2_, TakeMessagePortPtr()) | 99 EXPECT_CALL(*connection_, GetOutgoingMessageProcessor()) |
96 .WillOnce( | 100 .WillOnce(Return(message_capture_.get())); |
97 Return(new MessagePort(std::move(reader_), std::move(writer_)))); | 101 |
| 102 BlimpConnection* actual_connection = nullptr; |
| 103 BlimpConnection* expected_connection = connection_.get(); |
| 104 EXPECT_CALL(connection_handler_, HandleConnectionPtr(_)) |
| 105 .WillOnce(SaveArg<0>(&actual_connection)); |
| 106 |
| 107 transport2_->SetMockConnection(std::move(connection_)); |
98 | 108 |
99 EXPECT_TRUE(connect_cb_1.is_null()); | 109 EXPECT_TRUE(connect_cb_1.is_null()); |
100 EXPECT_TRUE(connect_cb_2.is_null()); | 110 EXPECT_TRUE(connect_cb_2.is_null()); |
101 manager_->AddTransport(std::move(transport1_)); | 111 manager_->AddTransport(std::move(transport1_)); |
102 manager_->AddTransport(std::move(transport2_)); | 112 manager_->AddTransport(std::move(transport2_)); |
103 manager_->Connect(); | 113 manager_->Connect(); |
104 EXPECT_FALSE(connect_cb_1.is_null()); | 114 EXPECT_FALSE(connect_cb_1.is_null()); |
105 base::ResetAndReturn(&connect_cb_1).Run(net::ERR_FAILED); | 115 base::ResetAndReturn(&connect_cb_1).Run(net::ERR_FAILED); |
106 EXPECT_FALSE(connect_cb_2.is_null()); | 116 EXPECT_FALSE(connect_cb_2.is_null()); |
107 base::ResetAndReturn(&connect_cb_2).Run(net::OK); | 117 base::ResetAndReturn(&connect_cb_2).Run(net::OK); |
108 base::ResetAndReturn(&write_cb).Run(net::OK); | 118 base::ResetAndReturn(&write_cb).Run(net::OK); |
| 119 EXPECT_EQ(expected_connection, actual_connection); |
109 } | 120 } |
110 | 121 |
111 // Both transports fail to connect. | 122 // Both transports fail to connect. |
112 TEST_F(ClientConnectionManagerTest, BothTransportsFailToConnect) { | 123 TEST_F(ClientConnectionManagerTest, BothTransportsFailToConnect) { |
113 net::CompletionCallback connect_cb_1; | 124 net::CompletionCallback connect_cb_1; |
114 EXPECT_CALL(*transport1_, Connect(_)).WillOnce(SaveArg<0>(&connect_cb_1)); | 125 EXPECT_CALL(*transport1_, Connect(_)).WillOnce(SaveArg<0>(&connect_cb_1)); |
115 net::CompletionCallback connect_cb_2; | 126 net::CompletionCallback connect_cb_2; |
116 EXPECT_CALL(*transport2_, Connect(_)).WillOnce(SaveArg<0>(&connect_cb_2)); | 127 EXPECT_CALL(*transport2_, Connect(_)).WillOnce(SaveArg<0>(&connect_cb_2)); |
117 | 128 |
118 EXPECT_TRUE(connect_cb_1.is_null()); | 129 EXPECT_TRUE(connect_cb_1.is_null()); |
119 EXPECT_TRUE(connect_cb_2.is_null()); | 130 EXPECT_TRUE(connect_cb_2.is_null()); |
120 manager_->AddTransport(std::move(transport1_)); | 131 manager_->AddTransport(std::move(transport1_)); |
121 manager_->AddTransport(std::move(transport2_)); | 132 manager_->AddTransport(std::move(transport2_)); |
122 manager_->Connect(); | 133 manager_->Connect(); |
123 EXPECT_FALSE(connect_cb_1.is_null()); | 134 EXPECT_FALSE(connect_cb_1.is_null()); |
124 EXPECT_TRUE(connect_cb_2.is_null()); | 135 EXPECT_TRUE(connect_cb_2.is_null()); |
125 base::ResetAndReturn(&connect_cb_1).Run(net::ERR_FAILED); | 136 base::ResetAndReturn(&connect_cb_1).Run(net::ERR_FAILED); |
126 EXPECT_FALSE(connect_cb_2.is_null()); | 137 EXPECT_FALSE(connect_cb_2.is_null()); |
127 base::ResetAndReturn(&connect_cb_2).Run(net::ERR_FAILED); | 138 base::ResetAndReturn(&connect_cb_2).Run(net::ERR_FAILED); |
128 } | 139 } |
129 | 140 |
130 } // namespace blimp | 141 } // namespace blimp |
OLD | NEW |