blob: d987d4fa527774c65fd8022e4738b19894f985c7 [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
Yabin Cuib3298242015-08-28 15:09:44 -070023static void DisconnectFunc(void* arg, atransport*) {
24 int* count = reinterpret_cast<int*>(arg);
25 ++*count;
26}
27
28TEST(transport, RunDisconnects) {
Dan Albert1792c232015-05-18 13:06:53 -070029 atransport t;
Yabin Cuib3298242015-08-28 15:09:44 -070030 // RunDisconnects() can be called with an empty atransport.
31 t.RunDisconnects();
32
33 int count = 0;
34 adisconnect disconnect;
35 disconnect.func = DisconnectFunc;
36 disconnect.opaque = &count;
37 t.AddDisconnect(&disconnect);
38 t.RunDisconnects();
39 ASSERT_EQ(1, count);
40
41 // disconnect should have been removed automatically.
42 t.RunDisconnects();
43 ASSERT_EQ(1, count);
44
45 count = 0;
46 t.AddDisconnect(&disconnect);
47 t.RemoveDisconnect(&disconnect);
48 t.RunDisconnects();
49 ASSERT_EQ(0, count);
Dan Albert1792c232015-05-18 13:06:53 -070050}
51
David Pursell4e2fd362015-09-22 10:43:08 -070052TEST(transport, SetFeatures) {
Dan Albert1792c232015-05-18 13:06:53 -070053 atransport t;
54 ASSERT_EQ(0U, t.features().size());
55
David Pursell4e2fd362015-09-22 10:43:08 -070056 t.SetFeatures(FeatureSetToString(FeatureSet{"foo"}));
Dan Albert1792c232015-05-18 13:06:53 -070057 ASSERT_EQ(1U, t.features().size());
58 ASSERT_TRUE(t.has_feature("foo"));
59
David Pursell4e2fd362015-09-22 10:43:08 -070060 t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar"}));
Dan Albert1792c232015-05-18 13:06:53 -070061 ASSERT_EQ(2U, t.features().size());
62 ASSERT_TRUE(t.has_feature("foo"));
63 ASSERT_TRUE(t.has_feature("bar"));
64
David Pursell4e2fd362015-09-22 10:43:08 -070065 t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar", "foo"}));
Dan Albert1792c232015-05-18 13:06:53 -070066 ASSERT_EQ(2U, t.features().size());
67 ASSERT_TRUE(t.has_feature("foo"));
68 ASSERT_TRUE(t.has_feature("bar"));
David Pursell4e2fd362015-09-22 10:43:08 -070069
70 t.SetFeatures(FeatureSetToString(FeatureSet{"bar", "baz"}));
71 ASSERT_EQ(2U, t.features().size());
72 ASSERT_FALSE(t.has_feature("foo"));
73 ASSERT_TRUE(t.has_feature("bar"));
74 ASSERT_TRUE(t.has_feature("baz"));
David Purselld2b588e2015-09-25 13:04:21 -070075
76 t.SetFeatures("");
77 ASSERT_EQ(0U, t.features().size());
Dan Albert1792c232015-05-18 13:06:53 -070078}
79
80TEST(transport, parse_banner_no_features) {
Yabin Cuib5e11412017-03-10 16:01:01 -080081 set_main_thread();
Dan Albert1792c232015-05-18 13:06:53 -070082 atransport t;
83
84 parse_banner("host::", &t);
85
86 ASSERT_EQ(0U, t.features().size());
Yabin Cuib5e11412017-03-10 16:01:01 -080087 ASSERT_EQ(kCsHost, t.GetConnectionState());
Dan Albert1792c232015-05-18 13:06:53 -070088
89 ASSERT_EQ(nullptr, t.product);
90 ASSERT_EQ(nullptr, t.model);
91 ASSERT_EQ(nullptr, t.device);
92}
93
94TEST(transport, parse_banner_product_features) {
95 atransport t;
96
97 const char banner[] =
98 "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;";
99 parse_banner(banner, &t);
100
Yabin Cuib5e11412017-03-10 16:01:01 -0800101 ASSERT_EQ(kCsHost, t.GetConnectionState());
Dan Albert1792c232015-05-18 13:06:53 -0700102
103 ASSERT_EQ(0U, t.features().size());
104
105 ASSERT_EQ(std::string("foo"), t.product);
106 ASSERT_EQ(std::string("bar"), t.model);
107 ASSERT_EQ(std::string("baz"), t.device);
108}
109
110TEST(transport, parse_banner_features) {
111 atransport t;
112
113 const char banner[] =
114 "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;"
115 "features=woodly,doodly";
116 parse_banner(banner, &t);
117
Yabin Cuib5e11412017-03-10 16:01:01 -0800118 ASSERT_EQ(kCsHost, t.GetConnectionState());
Dan Albert1792c232015-05-18 13:06:53 -0700119
120 ASSERT_EQ(2U, t.features().size());
121 ASSERT_TRUE(t.has_feature("woodly"));
122 ASSERT_TRUE(t.has_feature("doodly"));
123
124 ASSERT_EQ(std::string("foo"), t.product);
125 ASSERT_EQ(std::string("bar"), t.model);
126 ASSERT_EQ(std::string("baz"), t.device);
Dan Albert055f1aa2015-02-20 17:24:58 -0800127}
David Pursell3f902aa2016-03-01 08:58:26 -0800128
129TEST(transport, test_matches_target) {
130 std::string serial = "foo";
131 std::string devpath = "/path/to/bar";
132 std::string product = "test_product";
133 std::string model = "test_model";
134 std::string device = "test_device";
135
136 atransport t;
137 t.serial = &serial[0];
138 t.devpath = &devpath[0];
139 t.product = &product[0];
140 t.model = &model[0];
141 t.device = &device[0];
142
143 // These tests should not be affected by the transport type.
144 for (TransportType type : {kTransportAny, kTransportLocal}) {
145 t.type = type;
146
147 EXPECT_TRUE(t.MatchesTarget(serial));
148 EXPECT_TRUE(t.MatchesTarget(devpath));
149 EXPECT_TRUE(t.MatchesTarget("product:" + product));
150 EXPECT_TRUE(t.MatchesTarget("model:" + model));
151 EXPECT_TRUE(t.MatchesTarget("device:" + device));
152
153 // Product, model, and device don't match without the prefix.
154 EXPECT_FALSE(t.MatchesTarget(product));
155 EXPECT_FALSE(t.MatchesTarget(model));
156 EXPECT_FALSE(t.MatchesTarget(device));
157 }
158}
159
160TEST(transport, test_matches_target_local) {
161 std::string serial = "100.100.100.100:5555";
162
163 atransport t;
164 t.serial = &serial[0];
165
166 // Network address matching should only be used for local transports.
167 for (TransportType type : {kTransportAny, kTransportLocal}) {
168 t.type = type;
169 bool should_match = (type == kTransportLocal);
170
171 EXPECT_EQ(should_match, t.MatchesTarget("100.100.100.100"));
172 EXPECT_EQ(should_match, t.MatchesTarget("tcp:100.100.100.100"));
173 EXPECT_EQ(should_match, t.MatchesTarget("tcp:100.100.100.100:5555"));
174 EXPECT_EQ(should_match, t.MatchesTarget("udp:100.100.100.100"));
175 EXPECT_EQ(should_match, t.MatchesTarget("udp:100.100.100.100:5555"));
176
177 // Wrong protocol, hostname, or port should never match.
178 EXPECT_FALSE(t.MatchesTarget("100.100.100"));
179 EXPECT_FALSE(t.MatchesTarget("100.100.100.100:"));
180 EXPECT_FALSE(t.MatchesTarget("100.100.100.100:-1"));
181 EXPECT_FALSE(t.MatchesTarget("100.100.100.100:5554"));
182 EXPECT_FALSE(t.MatchesTarget("abc:100.100.100.100"));
183 }
184}