blob: 2028eccbbf0029228b8d1100aa4a19ff4007c86d [file] [log] [blame]
Dan Albert055f1aa2015-02-20 17:24:58 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "transport.h"
18
19#include <gtest/gtest.h>
20
21#include "adb.h"
22
Dan Albertc7915a32015-05-18 16:46:31 -070023class TestTransport : public atransport {
24public:
25 bool operator==(const atransport& rhs) const {
26 EXPECT_EQ(read_from_remote, rhs.read_from_remote);
27 EXPECT_EQ(write_to_remote, rhs.write_to_remote);
28 EXPECT_EQ(close, rhs.close);
29 EXPECT_EQ(kick, rhs.kick);
30
31 EXPECT_EQ(fd, rhs.fd);
32 EXPECT_EQ(transport_socket, rhs.transport_socket);
33
34 EXPECT_EQ(
35 0, memcmp(&transport_fde, &rhs.transport_fde, sizeof(fdevent)));
36
37 EXPECT_EQ(ref_count, rhs.ref_count);
38 EXPECT_EQ(sync_token, rhs.sync_token);
39 EXPECT_EQ(connection_state, rhs.connection_state);
40 EXPECT_EQ(online, rhs.online);
41 EXPECT_EQ(type, rhs.type);
42
43 EXPECT_EQ(usb, rhs.usb);
44 EXPECT_EQ(sfd, rhs.sfd);
45
46 EXPECT_EQ(serial, rhs.serial);
47 EXPECT_EQ(product, rhs.product);
48 EXPECT_EQ(model, rhs.model);
49 EXPECT_EQ(device, rhs.device);
50 EXPECT_EQ(devpath, rhs.devpath);
51 EXPECT_EQ(adb_port, rhs.adb_port);
52 EXPECT_EQ(kicked, rhs.kicked);
53
Dan Albertc7915a32015-05-18 16:46:31 -070054 EXPECT_EQ(key, rhs.key);
55 EXPECT_EQ(0, memcmp(token, rhs.token, TOKEN_SIZE));
Dan Albertc7915a32015-05-18 16:46:31 -070056 EXPECT_EQ(failed_auth_attempts, rhs.failed_auth_attempts);
57
Dan Albert1792c232015-05-18 13:06:53 -070058 EXPECT_EQ(features(), rhs.features());
59
Dan Albertc7915a32015-05-18 16:46:31 -070060 return true;
61 }
62};
63
Spencer Lowcf168a82015-05-24 15:36:28 -070064class TransportSetup {
65public:
66 TransportSetup() {
67#ifdef _WIN32
68 // Use extern instead of including sysdeps.h which brings in various macros
69 // that conflict with APIs used in this file.
70 extern void adb_sysdeps_init(void);
71 adb_sysdeps_init();
72#else
73 // adb_sysdeps_init() is an inline function that we cannot link against.
74#endif
75 }
76};
77
78// Static initializer will call adb_sysdeps_init() before main() to initialize
79// the transport mutex before it is used in the tests. Alternatives would be to
80// use __attribute__((constructor)) here or to use that or a static initializer
81// for adb_sysdeps_init() itself in sysdeps_win32.cpp (caveats of unclear
82// init order), or to use a test fixture whose SetUp() could do the init once.
83static TransportSetup g_TransportSetup;
84
Dan Albert055f1aa2015-02-20 17:24:58 -080085TEST(transport, kick_transport) {
Dan Albertc7915a32015-05-18 16:46:31 -070086 TestTransport t;
87
Dan Albert055f1aa2015-02-20 17:24:58 -080088 // Mutate some member so we can test that the function is run.
89 t.kick = [](atransport* trans) { trans->fd = 42; };
Dan Albertc7915a32015-05-18 16:46:31 -070090
91 TestTransport expected;
92 expected.kick = t.kick;
Dan Albert055f1aa2015-02-20 17:24:58 -080093 expected.fd = 42;
94 expected.kicked = 1;
Dan Albertc7915a32015-05-18 16:46:31 -070095
Dan Albert055f1aa2015-02-20 17:24:58 -080096 kick_transport(&t);
97 ASSERT_EQ(42, t.fd);
98 ASSERT_EQ(1, t.kicked);
Dan Albertc7915a32015-05-18 16:46:31 -070099 ASSERT_EQ(expected, t);
Dan Albert055f1aa2015-02-20 17:24:58 -0800100}
101
102TEST(transport, kick_transport_already_kicked) {
103 // Ensure that the transport is not modified if the transport has already been
104 // kicked.
Dan Albertc7915a32015-05-18 16:46:31 -0700105 TestTransport t;
Dan Albert055f1aa2015-02-20 17:24:58 -0800106 t.kicked = 1;
107 t.kick = [](atransport*) { FAIL() << "Kick should not have been called"; };
Dan Albertc7915a32015-05-18 16:46:31 -0700108
109 TestTransport expected;
110 expected.kicked = 1;
111 expected.kick = t.kick;
112
Dan Albert055f1aa2015-02-20 17:24:58 -0800113 kick_transport(&t);
Dan Albertc7915a32015-05-18 16:46:31 -0700114 ASSERT_EQ(expected, t);
Dan Albert055f1aa2015-02-20 17:24:58 -0800115}
116
Yabin Cuib3298242015-08-28 15:09:44 -0700117static void DisconnectFunc(void* arg, atransport*) {
118 int* count = reinterpret_cast<int*>(arg);
119 ++*count;
120}
121
122TEST(transport, RunDisconnects) {
Dan Albert1792c232015-05-18 13:06:53 -0700123 atransport t;
Yabin Cuib3298242015-08-28 15:09:44 -0700124 // RunDisconnects() can be called with an empty atransport.
125 t.RunDisconnects();
126
127 int count = 0;
128 adisconnect disconnect;
129 disconnect.func = DisconnectFunc;
130 disconnect.opaque = &count;
131 t.AddDisconnect(&disconnect);
132 t.RunDisconnects();
133 ASSERT_EQ(1, count);
134
135 // disconnect should have been removed automatically.
136 t.RunDisconnects();
137 ASSERT_EQ(1, count);
138
139 count = 0;
140 t.AddDisconnect(&disconnect);
141 t.RemoveDisconnect(&disconnect);
142 t.RunDisconnects();
143 ASSERT_EQ(0, count);
Dan Albert1792c232015-05-18 13:06:53 -0700144}
145
David Pursell4e2fd362015-09-22 10:43:08 -0700146TEST(transport, SetFeatures) {
Dan Albert1792c232015-05-18 13:06:53 -0700147 atransport t;
148 ASSERT_EQ(0U, t.features().size());
149
David Pursell4e2fd362015-09-22 10:43:08 -0700150 t.SetFeatures(FeatureSetToString(FeatureSet{"foo"}));
Dan Albert1792c232015-05-18 13:06:53 -0700151 ASSERT_EQ(1U, t.features().size());
152 ASSERT_TRUE(t.has_feature("foo"));
153
David Pursell4e2fd362015-09-22 10:43:08 -0700154 t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar"}));
Dan Albert1792c232015-05-18 13:06:53 -0700155 ASSERT_EQ(2U, t.features().size());
156 ASSERT_TRUE(t.has_feature("foo"));
157 ASSERT_TRUE(t.has_feature("bar"));
158
David Pursell4e2fd362015-09-22 10:43:08 -0700159 t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar", "foo"}));
Dan Albert1792c232015-05-18 13:06:53 -0700160 ASSERT_EQ(2U, t.features().size());
161 ASSERT_TRUE(t.has_feature("foo"));
162 ASSERT_TRUE(t.has_feature("bar"));
David Pursell4e2fd362015-09-22 10:43:08 -0700163
164 t.SetFeatures(FeatureSetToString(FeatureSet{"bar", "baz"}));
165 ASSERT_EQ(2U, t.features().size());
166 ASSERT_FALSE(t.has_feature("foo"));
167 ASSERT_TRUE(t.has_feature("bar"));
168 ASSERT_TRUE(t.has_feature("baz"));
David Purselld2b588e2015-09-25 13:04:21 -0700169
170 t.SetFeatures("");
171 ASSERT_EQ(0U, t.features().size());
Dan Albert1792c232015-05-18 13:06:53 -0700172}
173
174TEST(transport, parse_banner_no_features) {
175 atransport t;
176
177 parse_banner("host::", &t);
178
179 ASSERT_EQ(0U, t.features().size());
180 ASSERT_EQ(kCsHost, t.connection_state);
181
182 ASSERT_EQ(nullptr, t.product);
183 ASSERT_EQ(nullptr, t.model);
184 ASSERT_EQ(nullptr, t.device);
185}
186
187TEST(transport, parse_banner_product_features) {
188 atransport t;
189
190 const char banner[] =
191 "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;";
192 parse_banner(banner, &t);
193
194 ASSERT_EQ(kCsHost, t.connection_state);
195
196 ASSERT_EQ(0U, t.features().size());
197
198 ASSERT_EQ(std::string("foo"), t.product);
199 ASSERT_EQ(std::string("bar"), t.model);
200 ASSERT_EQ(std::string("baz"), t.device);
201}
202
203TEST(transport, parse_banner_features) {
204 atransport t;
205
206 const char banner[] =
207 "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;"
208 "features=woodly,doodly";
209 parse_banner(banner, &t);
210
211 ASSERT_EQ(kCsHost, t.connection_state);
212
213 ASSERT_EQ(2U, t.features().size());
214 ASSERT_TRUE(t.has_feature("woodly"));
215 ASSERT_TRUE(t.has_feature("doodly"));
216
217 ASSERT_EQ(std::string("foo"), t.product);
218 ASSERT_EQ(std::string("bar"), t.model);
219 ASSERT_EQ(std::string("baz"), t.device);
Dan Albert055f1aa2015-02-20 17:24:58 -0800220}
David Pursell802c54e2016-03-01 08:58:26 -0800221
222TEST(transport, test_matches_target) {
223 std::string serial = "foo";
224 std::string devpath = "/path/to/bar";
225 std::string product = "test_product";
226 std::string model = "test_model";
227 std::string device = "test_device";
228
229 atransport t;
230 t.serial = &serial[0];
231 t.devpath = &devpath[0];
232 t.product = &product[0];
233 t.model = &model[0];
234 t.device = &device[0];
235
236 // These tests should not be affected by the transport type.
237 for (TransportType type : {kTransportAny, kTransportLocal}) {
238 t.type = type;
239
240 EXPECT_TRUE(t.MatchesTarget(serial));
241 EXPECT_TRUE(t.MatchesTarget(devpath));
242 EXPECT_TRUE(t.MatchesTarget("product:" + product));
243 EXPECT_TRUE(t.MatchesTarget("model:" + model));
244 EXPECT_TRUE(t.MatchesTarget("device:" + device));
245
246 // Product, model, and device don't match without the prefix.
247 EXPECT_FALSE(t.MatchesTarget(product));
248 EXPECT_FALSE(t.MatchesTarget(model));
249 EXPECT_FALSE(t.MatchesTarget(device));
250 }
251}
252
253TEST(transport, test_matches_target_local) {
254 std::string serial = "100.100.100.100:5555";
255
256 atransport t;
257 t.serial = &serial[0];
258
259 // Network address matching should only be used for local transports.
260 for (TransportType type : {kTransportAny, kTransportLocal}) {
261 t.type = type;
262 bool should_match = (type == kTransportLocal);
263
264 EXPECT_EQ(should_match, t.MatchesTarget("100.100.100.100"));
265 EXPECT_EQ(should_match, t.MatchesTarget("tcp:100.100.100.100"));
266 EXPECT_EQ(should_match, t.MatchesTarget("tcp:100.100.100.100:5555"));
267 EXPECT_EQ(should_match, t.MatchesTarget("udp:100.100.100.100"));
268 EXPECT_EQ(should_match, t.MatchesTarget("udp:100.100.100.100:5555"));
269
270 // Wrong protocol, hostname, or port should never match.
271 EXPECT_FALSE(t.MatchesTarget("100.100.100"));
272 EXPECT_FALSE(t.MatchesTarget("100.100.100.100:"));
273 EXPECT_FALSE(t.MatchesTarget("100.100.100.100:-1"));
274 EXPECT_FALSE(t.MatchesTarget("100.100.100.100:5554"));
275 EXPECT_FALSE(t.MatchesTarget("abc:100.100.100.100"));
276 }
277}