2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "transport.h"
19 #include <gtest/gtest.h>
22 #include "fdevent_test.h"
24 struct TransportTest : public FdeventTest {};
26 static void DisconnectFunc(void* arg, atransport*) {
27 int* count = reinterpret_cast<int*>(arg);
31 TEST_F(TransportTest, RunDisconnects) {
33 // RunDisconnects() can be called with an empty atransport.
37 adisconnect disconnect;
38 disconnect.func = DisconnectFunc;
39 disconnect.opaque = &count;
40 t.AddDisconnect(&disconnect);
44 // disconnect should have been removed automatically.
49 t.AddDisconnect(&disconnect);
50 t.RemoveDisconnect(&disconnect);
55 TEST_F(TransportTest, SetFeatures) {
57 ASSERT_EQ(0U, t.features().size());
59 t.SetFeatures(FeatureSetToString(FeatureSet{"foo"}));
60 ASSERT_EQ(1U, t.features().size());
61 ASSERT_TRUE(t.has_feature("foo"));
63 t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar"}));
64 ASSERT_EQ(2U, t.features().size());
65 ASSERT_TRUE(t.has_feature("foo"));
66 ASSERT_TRUE(t.has_feature("bar"));
68 t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar", "foo"}));
69 ASSERT_EQ(2U, t.features().size());
70 ASSERT_TRUE(t.has_feature("foo"));
71 ASSERT_TRUE(t.has_feature("bar"));
73 t.SetFeatures(FeatureSetToString(FeatureSet{"bar", "baz"}));
74 ASSERT_EQ(2U, t.features().size());
75 ASSERT_FALSE(t.has_feature("foo"));
76 ASSERT_TRUE(t.has_feature("bar"));
77 ASSERT_TRUE(t.has_feature("baz"));
80 ASSERT_EQ(0U, t.features().size());
83 TEST_F(TransportTest, parse_banner_no_features) {
86 parse_banner("host::", &t);
88 ASSERT_EQ(0U, t.features().size());
89 ASSERT_EQ(kCsHost, t.GetConnectionState());
91 ASSERT_EQ(std::string(), t.product);
92 ASSERT_EQ(std::string(), t.model);
93 ASSERT_EQ(std::string(), t.device);
96 TEST_F(TransportTest, parse_banner_product_features) {
100 "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;";
101 parse_banner(banner, &t);
103 ASSERT_EQ(kCsHost, t.GetConnectionState());
105 ASSERT_EQ(0U, t.features().size());
107 ASSERT_EQ(std::string("foo"), t.product);
108 ASSERT_EQ(std::string("bar"), t.model);
109 ASSERT_EQ(std::string("baz"), t.device);
112 TEST_F(TransportTest, parse_banner_features) {
114 const char banner[] =
115 "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;"
116 "features=woodly,doodly";
117 parse_banner(banner, &t);
119 ASSERT_EQ(kCsHost, t.GetConnectionState());
121 ASSERT_EQ(2U, t.features().size());
122 ASSERT_TRUE(t.has_feature("woodly"));
123 ASSERT_TRUE(t.has_feature("doodly"));
125 ASSERT_EQ(std::string("foo"), t.product);
126 ASSERT_EQ(std::string("bar"), t.model);
127 ASSERT_EQ(std::string("baz"), t.device);
130 TEST_F(TransportTest, test_matches_target) {
131 std::string serial = "foo";
132 std::string devpath = "/path/to/bar";
133 std::string product = "test_product";
134 std::string model = "test_model";
135 std::string device = "test_device";
138 t.serial = &serial[0];
139 t.devpath = &devpath[0];
140 t.product = &product[0];
142 t.device = &device[0];
144 // These tests should not be affected by the transport type.
145 for (TransportType type : {kTransportAny, kTransportLocal}) {
148 EXPECT_TRUE(t.MatchesTarget(serial));
149 EXPECT_TRUE(t.MatchesTarget(devpath));
150 EXPECT_TRUE(t.MatchesTarget("product:" + product));
151 EXPECT_TRUE(t.MatchesTarget("model:" + model));
152 EXPECT_TRUE(t.MatchesTarget("device:" + device));
154 // Product, model, and device don't match without the prefix.
155 EXPECT_FALSE(t.MatchesTarget(product));
156 EXPECT_FALSE(t.MatchesTarget(model));
157 EXPECT_FALSE(t.MatchesTarget(device));
161 TEST_F(TransportTest, test_matches_target_local) {
162 std::string serial = "100.100.100.100:5555";
165 t.serial = &serial[0];
167 // Network address matching should only be used for local transports.
168 for (TransportType type : {kTransportAny, kTransportLocal}) {
170 bool should_match = (type == kTransportLocal);
172 EXPECT_EQ(should_match, t.MatchesTarget("100.100.100.100"));
173 EXPECT_EQ(should_match, t.MatchesTarget("tcp:100.100.100.100"));
174 EXPECT_EQ(should_match, t.MatchesTarget("tcp:100.100.100.100:5555"));
175 EXPECT_EQ(should_match, t.MatchesTarget("udp:100.100.100.100"));
176 EXPECT_EQ(should_match, t.MatchesTarget("udp:100.100.100.100:5555"));
178 // Wrong protocol, hostname, or port should never match.
179 EXPECT_FALSE(t.MatchesTarget("100.100.100"));
180 EXPECT_FALSE(t.MatchesTarget("100.100.100.100:"));
181 EXPECT_FALSE(t.MatchesTarget("100.100.100.100:-1"));
182 EXPECT_FALSE(t.MatchesTarget("100.100.100.100:5554"));
183 EXPECT_FALSE(t.MatchesTarget("abc:100.100.100.100"));