blob: 1a047aa3fdea717d43ccedf85479ef3754430840 [file] [log] [blame]
Dan Albert4895c522015-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"
Josh Gaob51193a2019-06-28 13:50:37 -070022#include "fdevent/fdevent_test.h"
Josh Gaoa9655ad2018-10-19 15:24:53 -070023
Josh Gao6256d992021-05-20 18:28:13 -070024TEST(ConnectionStateTest, to_string) {
25 ASSERT_EQ("offline", to_string(ConnectionState::kCsOffline));
26 ASSERT_EQ("bootloader", to_string(ConnectionState::kCsBootloader));
27 ASSERT_EQ("device", to_string(ConnectionState::kCsDevice));
28 ASSERT_EQ("host", to_string(ConnectionState::kCsHost));
29 ASSERT_EQ("recovery", to_string(ConnectionState::kCsRecovery));
30 ASSERT_EQ("rescue", to_string(ConnectionState::kCsRescue));
31 ASSERT_EQ("sideload", to_string(ConnectionState::kCsSideload));
32 ASSERT_EQ("unauthorized", to_string(ConnectionState::kCsUnauthorized));
33 ASSERT_EQ("authorizing", to_string(ConnectionState::kCsAuthorizing));
34 ASSERT_EQ("connecting", to_string(ConnectionState::kCsConnecting));
35}
36
Josh Gaoa9655ad2018-10-19 15:24:53 -070037struct TransportTest : public FdeventTest {};
Dan Albert4895c522015-02-20 17:24:58 -080038
Yabin Cui2d4c1982015-08-28 15:09:44 -070039static void DisconnectFunc(void* arg, atransport*) {
40 int* count = reinterpret_cast<int*>(arg);
41 ++*count;
42}
43
Josh Gaoa9655ad2018-10-19 15:24:53 -070044TEST_F(TransportTest, RunDisconnects) {
Dan Albertbe8e54b2015-05-18 13:06:53 -070045 atransport t;
Yabin Cui2d4c1982015-08-28 15:09:44 -070046 // RunDisconnects() can be called with an empty atransport.
47 t.RunDisconnects();
48
49 int count = 0;
50 adisconnect disconnect;
51 disconnect.func = DisconnectFunc;
52 disconnect.opaque = &count;
53 t.AddDisconnect(&disconnect);
54 t.RunDisconnects();
55 ASSERT_EQ(1, count);
56
57 // disconnect should have been removed automatically.
58 t.RunDisconnects();
59 ASSERT_EQ(1, count);
60
61 count = 0;
62 t.AddDisconnect(&disconnect);
63 t.RemoveDisconnect(&disconnect);
64 t.RunDisconnects();
65 ASSERT_EQ(0, count);
Dan Albertbe8e54b2015-05-18 13:06:53 -070066}
67
Josh Gaoa9655ad2018-10-19 15:24:53 -070068TEST_F(TransportTest, SetFeatures) {
Dan Albertbe8e54b2015-05-18 13:06:53 -070069 atransport t;
70 ASSERT_EQ(0U, t.features().size());
71
David Pursella07dbad2015-09-22 10:43:08 -070072 t.SetFeatures(FeatureSetToString(FeatureSet{"foo"}));
Dan Albertbe8e54b2015-05-18 13:06:53 -070073 ASSERT_EQ(1U, t.features().size());
74 ASSERT_TRUE(t.has_feature("foo"));
75
David Pursella07dbad2015-09-22 10:43:08 -070076 t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar"}));
Dan Albertbe8e54b2015-05-18 13:06:53 -070077 ASSERT_EQ(2U, t.features().size());
78 ASSERT_TRUE(t.has_feature("foo"));
79 ASSERT_TRUE(t.has_feature("bar"));
80
David Pursella07dbad2015-09-22 10:43:08 -070081 t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar", "foo"}));
Yurii Zubrytskyi816c2d22020-03-26 18:19:28 -070082 ASSERT_LE(2U, t.features().size());
Dan Albertbe8e54b2015-05-18 13:06:53 -070083 ASSERT_TRUE(t.has_feature("foo"));
84 ASSERT_TRUE(t.has_feature("bar"));
David Pursella07dbad2015-09-22 10:43:08 -070085
86 t.SetFeatures(FeatureSetToString(FeatureSet{"bar", "baz"}));
87 ASSERT_EQ(2U, t.features().size());
88 ASSERT_FALSE(t.has_feature("foo"));
89 ASSERT_TRUE(t.has_feature("bar"));
90 ASSERT_TRUE(t.has_feature("baz"));
David Pursell3d9072b2015-09-25 13:04:21 -070091
92 t.SetFeatures("");
93 ASSERT_EQ(0U, t.features().size());
Dan Albertbe8e54b2015-05-18 13:06:53 -070094}
95
Josh Gaoa9655ad2018-10-19 15:24:53 -070096TEST_F(TransportTest, parse_banner_no_features) {
Dan Albertbe8e54b2015-05-18 13:06:53 -070097 atransport t;
98
99 parse_banner("host::", &t);
100
101 ASSERT_EQ(0U, t.features().size());
Yabin Cui3cf1b362017-03-10 16:01:01 -0800102 ASSERT_EQ(kCsHost, t.GetConnectionState());
Dan Albertbe8e54b2015-05-18 13:06:53 -0700103
Luis Hector Chavezb4edbdf2018-07-18 21:18:27 -0700104 ASSERT_EQ(std::string(), t.product);
105 ASSERT_EQ(std::string(), t.model);
106 ASSERT_EQ(std::string(), t.device);
Dan Albertbe8e54b2015-05-18 13:06:53 -0700107}
108
Josh Gaoa9655ad2018-10-19 15:24:53 -0700109TEST_F(TransportTest, parse_banner_product_features) {
Dan Albertbe8e54b2015-05-18 13:06:53 -0700110 atransport t;
111
112 const char banner[] =
113 "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;";
114 parse_banner(banner, &t);
115
Yabin Cui3cf1b362017-03-10 16:01:01 -0800116 ASSERT_EQ(kCsHost, t.GetConnectionState());
Dan Albertbe8e54b2015-05-18 13:06:53 -0700117
118 ASSERT_EQ(0U, t.features().size());
119
120 ASSERT_EQ(std::string("foo"), t.product);
121 ASSERT_EQ(std::string("bar"), t.model);
122 ASSERT_EQ(std::string("baz"), t.device);
123}
124
Josh Gaoa9655ad2018-10-19 15:24:53 -0700125TEST_F(TransportTest, parse_banner_features) {
Dan Albertbe8e54b2015-05-18 13:06:53 -0700126 atransport t;
Dan Albertbe8e54b2015-05-18 13:06:53 -0700127 const char banner[] =
128 "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;"
129 "features=woodly,doodly";
130 parse_banner(banner, &t);
131
Yabin Cui3cf1b362017-03-10 16:01:01 -0800132 ASSERT_EQ(kCsHost, t.GetConnectionState());
Dan Albertbe8e54b2015-05-18 13:06:53 -0700133
134 ASSERT_EQ(2U, t.features().size());
135 ASSERT_TRUE(t.has_feature("woodly"));
136 ASSERT_TRUE(t.has_feature("doodly"));
137
138 ASSERT_EQ(std::string("foo"), t.product);
139 ASSERT_EQ(std::string("bar"), t.model);
140 ASSERT_EQ(std::string("baz"), t.device);
Dan Albert4895c522015-02-20 17:24:58 -0800141}
David Pursellc929c6f2016-03-01 08:58:26 -0800142
Josh Gao65d18e22020-04-22 20:57:26 -0700143#if ADB_HOST
Josh Gaoa9655ad2018-10-19 15:24:53 -0700144TEST_F(TransportTest, test_matches_target) {
David Pursellc929c6f2016-03-01 08:58:26 -0800145 std::string serial = "foo";
146 std::string devpath = "/path/to/bar";
147 std::string product = "test_product";
148 std::string model = "test_model";
149 std::string device = "test_device";
150
151 atransport t;
152 t.serial = &serial[0];
153 t.devpath = &devpath[0];
154 t.product = &product[0];
155 t.model = &model[0];
156 t.device = &device[0];
157
158 // These tests should not be affected by the transport type.
159 for (TransportType type : {kTransportAny, kTransportLocal}) {
160 t.type = type;
161
162 EXPECT_TRUE(t.MatchesTarget(serial));
163 EXPECT_TRUE(t.MatchesTarget(devpath));
164 EXPECT_TRUE(t.MatchesTarget("product:" + product));
165 EXPECT_TRUE(t.MatchesTarget("model:" + model));
166 EXPECT_TRUE(t.MatchesTarget("device:" + device));
167
168 // Product, model, and device don't match without the prefix.
169 EXPECT_FALSE(t.MatchesTarget(product));
170 EXPECT_FALSE(t.MatchesTarget(model));
171 EXPECT_FALSE(t.MatchesTarget(device));
172 }
173}
174
Josh Gaoa9655ad2018-10-19 15:24:53 -0700175TEST_F(TransportTest, test_matches_target_local) {
David Pursellc929c6f2016-03-01 08:58:26 -0800176 std::string serial = "100.100.100.100:5555";
177
178 atransport t;
179 t.serial = &serial[0];
180
181 // Network address matching should only be used for local transports.
182 for (TransportType type : {kTransportAny, kTransportLocal}) {
183 t.type = type;
184 bool should_match = (type == kTransportLocal);
185
186 EXPECT_EQ(should_match, t.MatchesTarget("100.100.100.100"));
187 EXPECT_EQ(should_match, t.MatchesTarget("tcp:100.100.100.100"));
188 EXPECT_EQ(should_match, t.MatchesTarget("tcp:100.100.100.100:5555"));
189 EXPECT_EQ(should_match, t.MatchesTarget("udp:100.100.100.100"));
190 EXPECT_EQ(should_match, t.MatchesTarget("udp:100.100.100.100:5555"));
191
192 // Wrong protocol, hostname, or port should never match.
193 EXPECT_FALSE(t.MatchesTarget("100.100.100"));
194 EXPECT_FALSE(t.MatchesTarget("100.100.100.100:"));
195 EXPECT_FALSE(t.MatchesTarget("100.100.100.100:-1"));
196 EXPECT_FALSE(t.MatchesTarget("100.100.100.100:5554"));
197 EXPECT_FALSE(t.MatchesTarget("abc:100.100.100.100"));
198 }
199}
Josh Gao65d18e22020-04-22 20:57:26 -0700200#endif