blob: 97fc0698d4eb9a5adb48cd550e0d8e0f71311fe6 [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));
56 EXPECT_EQ(0, memcmp(&auth_fde, &rhs.auth_fde, sizeof(fdevent)));
57 EXPECT_EQ(failed_auth_attempts, rhs.failed_auth_attempts);
58
Dan Albert1792c232015-05-18 13:06:53 -070059 EXPECT_EQ(features(), rhs.features());
60
Dan Albertc7915a32015-05-18 16:46:31 -070061 return true;
62 }
63};
64
Spencer Lowcf168a82015-05-24 15:36:28 -070065class TransportSetup {
66public:
67 TransportSetup() {
68#ifdef _WIN32
69 // Use extern instead of including sysdeps.h which brings in various macros
70 // that conflict with APIs used in this file.
71 extern void adb_sysdeps_init(void);
72 adb_sysdeps_init();
73#else
74 // adb_sysdeps_init() is an inline function that we cannot link against.
75#endif
76 }
77};
78
79// Static initializer will call adb_sysdeps_init() before main() to initialize
80// the transport mutex before it is used in the tests. Alternatives would be to
81// use __attribute__((constructor)) here or to use that or a static initializer
82// for adb_sysdeps_init() itself in sysdeps_win32.cpp (caveats of unclear
83// init order), or to use a test fixture whose SetUp() could do the init once.
84static TransportSetup g_TransportSetup;
85
Dan Albert055f1aa2015-02-20 17:24:58 -080086TEST(transport, kick_transport) {
Dan Albertc7915a32015-05-18 16:46:31 -070087 TestTransport t;
88
Dan Albert055f1aa2015-02-20 17:24:58 -080089 // Mutate some member so we can test that the function is run.
90 t.kick = [](atransport* trans) { trans->fd = 42; };
Dan Albertc7915a32015-05-18 16:46:31 -070091
92 TestTransport expected;
93 expected.kick = t.kick;
Dan Albert055f1aa2015-02-20 17:24:58 -080094 expected.fd = 42;
95 expected.kicked = 1;
Dan Albertc7915a32015-05-18 16:46:31 -070096
Dan Albert055f1aa2015-02-20 17:24:58 -080097 kick_transport(&t);
98 ASSERT_EQ(42, t.fd);
99 ASSERT_EQ(1, t.kicked);
Dan Albertc7915a32015-05-18 16:46:31 -0700100 ASSERT_EQ(expected, t);
Dan Albert055f1aa2015-02-20 17:24:58 -0800101}
102
103TEST(transport, kick_transport_already_kicked) {
104 // Ensure that the transport is not modified if the transport has already been
105 // kicked.
Dan Albertc7915a32015-05-18 16:46:31 -0700106 TestTransport t;
Dan Albert055f1aa2015-02-20 17:24:58 -0800107 t.kicked = 1;
108 t.kick = [](atransport*) { FAIL() << "Kick should not have been called"; };
Dan Albertc7915a32015-05-18 16:46:31 -0700109
110 TestTransport expected;
111 expected.kicked = 1;
112 expected.kick = t.kick;
113
Dan Albert055f1aa2015-02-20 17:24:58 -0800114 kick_transport(&t);
Dan Albertc7915a32015-05-18 16:46:31 -0700115 ASSERT_EQ(expected, t);
Dan Albert055f1aa2015-02-20 17:24:58 -0800116}
117
Yabin Cuib3298242015-08-28 15:09:44 -0700118static void DisconnectFunc(void* arg, atransport*) {
119 int* count = reinterpret_cast<int*>(arg);
120 ++*count;
121}
122
123TEST(transport, RunDisconnects) {
Dan Albert1792c232015-05-18 13:06:53 -0700124 atransport t;
Yabin Cuib3298242015-08-28 15:09:44 -0700125 // RunDisconnects() can be called with an empty atransport.
126 t.RunDisconnects();
127
128 int count = 0;
129 adisconnect disconnect;
130 disconnect.func = DisconnectFunc;
131 disconnect.opaque = &count;
132 t.AddDisconnect(&disconnect);
133 t.RunDisconnects();
134 ASSERT_EQ(1, count);
135
136 // disconnect should have been removed automatically.
137 t.RunDisconnects();
138 ASSERT_EQ(1, count);
139
140 count = 0;
141 t.AddDisconnect(&disconnect);
142 t.RemoveDisconnect(&disconnect);
143 t.RunDisconnects();
144 ASSERT_EQ(0, count);
Dan Albert1792c232015-05-18 13:06:53 -0700145}
146
David Pursell4e2fd362015-09-22 10:43:08 -0700147TEST(transport, SetFeatures) {
Dan Albert1792c232015-05-18 13:06:53 -0700148 atransport t;
149 ASSERT_EQ(0U, t.features().size());
150
David Pursell4e2fd362015-09-22 10:43:08 -0700151 t.SetFeatures(FeatureSetToString(FeatureSet{"foo"}));
Dan Albert1792c232015-05-18 13:06:53 -0700152 ASSERT_EQ(1U, t.features().size());
153 ASSERT_TRUE(t.has_feature("foo"));
154
David Pursell4e2fd362015-09-22 10:43:08 -0700155 t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar"}));
Dan Albert1792c232015-05-18 13:06:53 -0700156 ASSERT_EQ(2U, t.features().size());
157 ASSERT_TRUE(t.has_feature("foo"));
158 ASSERT_TRUE(t.has_feature("bar"));
159
David Pursell4e2fd362015-09-22 10:43:08 -0700160 t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar", "foo"}));
Dan Albert1792c232015-05-18 13:06:53 -0700161 ASSERT_EQ(2U, t.features().size());
162 ASSERT_TRUE(t.has_feature("foo"));
163 ASSERT_TRUE(t.has_feature("bar"));
David Pursell4e2fd362015-09-22 10:43:08 -0700164
165 t.SetFeatures(FeatureSetToString(FeatureSet{"bar", "baz"}));
166 ASSERT_EQ(2U, t.features().size());
167 ASSERT_FALSE(t.has_feature("foo"));
168 ASSERT_TRUE(t.has_feature("bar"));
169 ASSERT_TRUE(t.has_feature("baz"));
David Purselld2b588e2015-09-25 13:04:21 -0700170
171 t.SetFeatures("");
172 ASSERT_EQ(0U, t.features().size());
Dan Albert1792c232015-05-18 13:06:53 -0700173}
174
175TEST(transport, parse_banner_no_features) {
176 atransport t;
177
178 parse_banner("host::", &t);
179
180 ASSERT_EQ(0U, t.features().size());
181 ASSERT_EQ(kCsHost, t.connection_state);
182
183 ASSERT_EQ(nullptr, t.product);
184 ASSERT_EQ(nullptr, t.model);
185 ASSERT_EQ(nullptr, t.device);
186}
187
188TEST(transport, parse_banner_product_features) {
189 atransport t;
190
191 const char banner[] =
192 "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;";
193 parse_banner(banner, &t);
194
195 ASSERT_EQ(kCsHost, t.connection_state);
196
197 ASSERT_EQ(0U, t.features().size());
198
199 ASSERT_EQ(std::string("foo"), t.product);
200 ASSERT_EQ(std::string("bar"), t.model);
201 ASSERT_EQ(std::string("baz"), t.device);
202}
203
204TEST(transport, parse_banner_features) {
205 atransport t;
206
207 const char banner[] =
208 "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;"
209 "features=woodly,doodly";
210 parse_banner(banner, &t);
211
212 ASSERT_EQ(kCsHost, t.connection_state);
213
214 ASSERT_EQ(2U, t.features().size());
215 ASSERT_TRUE(t.has_feature("woodly"));
216 ASSERT_TRUE(t.has_feature("doodly"));
217
218 ASSERT_EQ(std::string("foo"), t.product);
219 ASSERT_EQ(std::string("bar"), t.model);
220 ASSERT_EQ(std::string("baz"), t.device);
Dan Albert055f1aa2015-02-20 17:24:58 -0800221}