blob: 10872ac3ea04b4bf0dbaf7fc11f5f9d6f28a3a1c [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
147TEST(transport, add_feature) {
148 atransport t;
149 ASSERT_EQ(0U, t.features().size());
150
151 t.add_feature("foo");
152 ASSERT_EQ(1U, t.features().size());
153 ASSERT_TRUE(t.has_feature("foo"));
154
155 t.add_feature("bar");
156 ASSERT_EQ(2U, t.features().size());
157 ASSERT_TRUE(t.has_feature("foo"));
158 ASSERT_TRUE(t.has_feature("bar"));
159
160 t.add_feature("foo");
161 ASSERT_EQ(2U, t.features().size());
162 ASSERT_TRUE(t.has_feature("foo"));
163 ASSERT_TRUE(t.has_feature("bar"));
164}
165
166TEST(transport, parse_banner_no_features) {
167 atransport t;
168
169 parse_banner("host::", &t);
170
171 ASSERT_EQ(0U, t.features().size());
172 ASSERT_EQ(kCsHost, t.connection_state);
173
174 ASSERT_EQ(nullptr, t.product);
175 ASSERT_EQ(nullptr, t.model);
176 ASSERT_EQ(nullptr, t.device);
177}
178
179TEST(transport, parse_banner_product_features) {
180 atransport t;
181
182 const char banner[] =
183 "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;";
184 parse_banner(banner, &t);
185
186 ASSERT_EQ(kCsHost, t.connection_state);
187
188 ASSERT_EQ(0U, t.features().size());
189
190 ASSERT_EQ(std::string("foo"), t.product);
191 ASSERT_EQ(std::string("bar"), t.model);
192 ASSERT_EQ(std::string("baz"), t.device);
193}
194
195TEST(transport, parse_banner_features) {
196 atransport t;
197
198 const char banner[] =
199 "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;"
200 "features=woodly,doodly";
201 parse_banner(banner, &t);
202
203 ASSERT_EQ(kCsHost, t.connection_state);
204
205 ASSERT_EQ(2U, t.features().size());
206 ASSERT_TRUE(t.has_feature("woodly"));
207 ASSERT_TRUE(t.has_feature("doodly"));
208
209 ASSERT_EQ(std::string("foo"), t.product);
210 ASSERT_EQ(std::string("bar"), t.model);
211 ASSERT_EQ(std::string("baz"), t.device);
Dan Albert055f1aa2015-02-20 17:24:58 -0800212}