Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 1 | /* |
| 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 Gao | a9655ad | 2018-10-19 15:24:53 -0700 | [diff] [blame^] | 22 | #include "fdevent_test.h" |
| 23 | |
| 24 | struct TransportTest : public FdeventTest {}; |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 25 | |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 26 | static void DisconnectFunc(void* arg, atransport*) { |
| 27 | int* count = reinterpret_cast<int*>(arg); |
| 28 | ++*count; |
| 29 | } |
| 30 | |
Josh Gao | a9655ad | 2018-10-19 15:24:53 -0700 | [diff] [blame^] | 31 | TEST_F(TransportTest, RunDisconnects) { |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 32 | atransport t; |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 33 | // RunDisconnects() can be called with an empty atransport. |
| 34 | t.RunDisconnects(); |
| 35 | |
| 36 | int count = 0; |
| 37 | adisconnect disconnect; |
| 38 | disconnect.func = DisconnectFunc; |
| 39 | disconnect.opaque = &count; |
| 40 | t.AddDisconnect(&disconnect); |
| 41 | t.RunDisconnects(); |
| 42 | ASSERT_EQ(1, count); |
| 43 | |
| 44 | // disconnect should have been removed automatically. |
| 45 | t.RunDisconnects(); |
| 46 | ASSERT_EQ(1, count); |
| 47 | |
| 48 | count = 0; |
| 49 | t.AddDisconnect(&disconnect); |
| 50 | t.RemoveDisconnect(&disconnect); |
| 51 | t.RunDisconnects(); |
| 52 | ASSERT_EQ(0, count); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Josh Gao | a9655ad | 2018-10-19 15:24:53 -0700 | [diff] [blame^] | 55 | TEST_F(TransportTest, SetFeatures) { |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 56 | atransport t; |
| 57 | ASSERT_EQ(0U, t.features().size()); |
| 58 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 59 | t.SetFeatures(FeatureSetToString(FeatureSet{"foo"})); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 60 | ASSERT_EQ(1U, t.features().size()); |
| 61 | ASSERT_TRUE(t.has_feature("foo")); |
| 62 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 63 | t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar"})); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 64 | ASSERT_EQ(2U, t.features().size()); |
| 65 | ASSERT_TRUE(t.has_feature("foo")); |
| 66 | ASSERT_TRUE(t.has_feature("bar")); |
| 67 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 68 | t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar", "foo"})); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 69 | ASSERT_EQ(2U, t.features().size()); |
| 70 | ASSERT_TRUE(t.has_feature("foo")); |
| 71 | ASSERT_TRUE(t.has_feature("bar")); |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 72 | |
| 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")); |
David Pursell | 3d9072b | 2015-09-25 13:04:21 -0700 | [diff] [blame] | 78 | |
| 79 | t.SetFeatures(""); |
| 80 | ASSERT_EQ(0U, t.features().size()); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Josh Gao | a9655ad | 2018-10-19 15:24:53 -0700 | [diff] [blame^] | 83 | TEST_F(TransportTest, parse_banner_no_features) { |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 84 | atransport t; |
| 85 | |
| 86 | parse_banner("host::", &t); |
| 87 | |
| 88 | ASSERT_EQ(0U, t.features().size()); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 89 | ASSERT_EQ(kCsHost, t.GetConnectionState()); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 90 | |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 91 | ASSERT_EQ(std::string(), t.product); |
| 92 | ASSERT_EQ(std::string(), t.model); |
| 93 | ASSERT_EQ(std::string(), t.device); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Josh Gao | a9655ad | 2018-10-19 15:24:53 -0700 | [diff] [blame^] | 96 | TEST_F(TransportTest, parse_banner_product_features) { |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 97 | atransport t; |
| 98 | |
| 99 | const char banner[] = |
| 100 | "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;"; |
| 101 | parse_banner(banner, &t); |
| 102 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 103 | ASSERT_EQ(kCsHost, t.GetConnectionState()); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 104 | |
| 105 | ASSERT_EQ(0U, t.features().size()); |
| 106 | |
| 107 | ASSERT_EQ(std::string("foo"), t.product); |
| 108 | ASSERT_EQ(std::string("bar"), t.model); |
| 109 | ASSERT_EQ(std::string("baz"), t.device); |
| 110 | } |
| 111 | |
Josh Gao | a9655ad | 2018-10-19 15:24:53 -0700 | [diff] [blame^] | 112 | TEST_F(TransportTest, parse_banner_features) { |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 113 | atransport t; |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 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); |
| 118 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 119 | ASSERT_EQ(kCsHost, t.GetConnectionState()); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 120 | |
| 121 | ASSERT_EQ(2U, t.features().size()); |
| 122 | ASSERT_TRUE(t.has_feature("woodly")); |
| 123 | ASSERT_TRUE(t.has_feature("doodly")); |
| 124 | |
| 125 | ASSERT_EQ(std::string("foo"), t.product); |
| 126 | ASSERT_EQ(std::string("bar"), t.model); |
| 127 | ASSERT_EQ(std::string("baz"), t.device); |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 128 | } |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 129 | |
Josh Gao | a9655ad | 2018-10-19 15:24:53 -0700 | [diff] [blame^] | 130 | TEST_F(TransportTest, test_matches_target) { |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 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"; |
| 136 | |
| 137 | atransport t; |
| 138 | t.serial = &serial[0]; |
| 139 | t.devpath = &devpath[0]; |
| 140 | t.product = &product[0]; |
| 141 | t.model = &model[0]; |
| 142 | t.device = &device[0]; |
| 143 | |
| 144 | // These tests should not be affected by the transport type. |
| 145 | for (TransportType type : {kTransportAny, kTransportLocal}) { |
| 146 | t.type = type; |
| 147 | |
| 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)); |
| 153 | |
| 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)); |
| 158 | } |
| 159 | } |
| 160 | |
Josh Gao | a9655ad | 2018-10-19 15:24:53 -0700 | [diff] [blame^] | 161 | TEST_F(TransportTest, test_matches_target_local) { |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 162 | std::string serial = "100.100.100.100:5555"; |
| 163 | |
| 164 | atransport t; |
| 165 | t.serial = &serial[0]; |
| 166 | |
| 167 | // Network address matching should only be used for local transports. |
| 168 | for (TransportType type : {kTransportAny, kTransportLocal}) { |
| 169 | t.type = type; |
| 170 | bool should_match = (type == kTransportLocal); |
| 171 | |
| 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")); |
| 177 | |
| 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")); |
| 184 | } |
| 185 | } |