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