1055f1aa4ff58ba71133d506b202ad46612758dedDan Albert/*
2055f1aa4ff58ba71133d506b202ad46612758dedDan Albert * Copyright (C) 2015 The Android Open Source Project
3055f1aa4ff58ba71133d506b202ad46612758dedDan Albert *
4055f1aa4ff58ba71133d506b202ad46612758dedDan Albert * Licensed under the Apache License, Version 2.0 (the "License");
5055f1aa4ff58ba71133d506b202ad46612758dedDan Albert * you may not use this file except in compliance with the License.
6055f1aa4ff58ba71133d506b202ad46612758dedDan Albert * You may obtain a copy of the License at
7055f1aa4ff58ba71133d506b202ad46612758dedDan Albert *
8055f1aa4ff58ba71133d506b202ad46612758dedDan Albert *      http://www.apache.org/licenses/LICENSE-2.0
9055f1aa4ff58ba71133d506b202ad46612758dedDan Albert *
10055f1aa4ff58ba71133d506b202ad46612758dedDan Albert * Unless required by applicable law or agreed to in writing, software
11055f1aa4ff58ba71133d506b202ad46612758dedDan Albert * distributed under the License is distributed on an "AS IS" BASIS,
12055f1aa4ff58ba71133d506b202ad46612758dedDan Albert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13055f1aa4ff58ba71133d506b202ad46612758dedDan Albert * See the License for the specific language governing permissions and
14055f1aa4ff58ba71133d506b202ad46612758dedDan Albert * limitations under the License.
15055f1aa4ff58ba71133d506b202ad46612758dedDan Albert */
16055f1aa4ff58ba71133d506b202ad46612758dedDan Albert
17055f1aa4ff58ba71133d506b202ad46612758dedDan Albert#include "transport.h"
18055f1aa4ff58ba71133d506b202ad46612758dedDan Albert
19055f1aa4ff58ba71133d506b202ad46612758dedDan Albert#include <gtest/gtest.h>
20055f1aa4ff58ba71133d506b202ad46612758dedDan Albert
21055f1aa4ff58ba71133d506b202ad46612758dedDan Albert#include "adb.h"
22055f1aa4ff58ba71133d506b202ad46612758dedDan Albert
23055f1aa4ff58ba71133d506b202ad46612758dedDan AlbertTEST(transport, kick_transport) {
247f27490e7f386401dc38287a67dcb8826e2260c5Yabin Cui  atransport t;
257f27490e7f386401dc38287a67dcb8826e2260c5Yabin Cui  static size_t kick_count;
267f27490e7f386401dc38287a67dcb8826e2260c5Yabin Cui  kick_count = 0;
27055f1aa4ff58ba71133d506b202ad46612758dedDan Albert  // Mutate some member so we can test that the function is run.
287f27490e7f386401dc38287a67dcb8826e2260c5Yabin Cui  t.SetKickFunction([](atransport* trans) { kick_count++; });
297f27490e7f386401dc38287a67dcb8826e2260c5Yabin Cui  ASSERT_FALSE(t.IsKicked());
307f27490e7f386401dc38287a67dcb8826e2260c5Yabin Cui  t.Kick();
317f27490e7f386401dc38287a67dcb8826e2260c5Yabin Cui  ASSERT_TRUE(t.IsKicked());
327f27490e7f386401dc38287a67dcb8826e2260c5Yabin Cui  ASSERT_EQ(1u, kick_count);
337f27490e7f386401dc38287a67dcb8826e2260c5Yabin Cui  // A transport can only be kicked once.
347f27490e7f386401dc38287a67dcb8826e2260c5Yabin Cui  t.Kick();
357f27490e7f386401dc38287a67dcb8826e2260c5Yabin Cui  ASSERT_TRUE(t.IsKicked());
367f27490e7f386401dc38287a67dcb8826e2260c5Yabin Cui  ASSERT_EQ(1u, kick_count);
37055f1aa4ff58ba71133d506b202ad46612758dedDan Albert}
38055f1aa4ff58ba71133d506b202ad46612758dedDan Albert
39b329824e6c5373ae303269dca285d835ce57e514Yabin Cuistatic void DisconnectFunc(void* arg, atransport*) {
40b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    int* count = reinterpret_cast<int*>(arg);
41b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    ++*count;
42b329824e6c5373ae303269dca285d835ce57e514Yabin Cui}
43b329824e6c5373ae303269dca285d835ce57e514Yabin Cui
44b329824e6c5373ae303269dca285d835ce57e514Yabin CuiTEST(transport, RunDisconnects) {
451792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    atransport t;
46b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    // RunDisconnects() can be called with an empty atransport.
47b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    t.RunDisconnects();
48b329824e6c5373ae303269dca285d835ce57e514Yabin Cui
49b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    int count = 0;
50b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    adisconnect disconnect;
51b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    disconnect.func = DisconnectFunc;
52b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    disconnect.opaque = &count;
53b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    t.AddDisconnect(&disconnect);
54b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    t.RunDisconnects();
55b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    ASSERT_EQ(1, count);
56b329824e6c5373ae303269dca285d835ce57e514Yabin Cui
57b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    // disconnect should have been removed automatically.
58b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    t.RunDisconnects();
59b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    ASSERT_EQ(1, count);
60b329824e6c5373ae303269dca285d835ce57e514Yabin Cui
61b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    count = 0;
62b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    t.AddDisconnect(&disconnect);
63b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    t.RemoveDisconnect(&disconnect);
64b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    t.RunDisconnects();
65b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    ASSERT_EQ(0, count);
661792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert}
671792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
684e2fd36bc8c16147cab323b0418a7666812d3bc7David PursellTEST(transport, SetFeatures) {
691792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    atransport t;
701792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(0U, t.features().size());
711792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
724e2fd36bc8c16147cab323b0418a7666812d3bc7David Pursell    t.SetFeatures(FeatureSetToString(FeatureSet{"foo"}));
731792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(1U, t.features().size());
741792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_TRUE(t.has_feature("foo"));
751792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
764e2fd36bc8c16147cab323b0418a7666812d3bc7David Pursell    t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar"}));
771792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(2U, t.features().size());
781792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_TRUE(t.has_feature("foo"));
791792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_TRUE(t.has_feature("bar"));
801792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
814e2fd36bc8c16147cab323b0418a7666812d3bc7David Pursell    t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar", "foo"}));
821792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(2U, t.features().size());
831792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_TRUE(t.has_feature("foo"));
841792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_TRUE(t.has_feature("bar"));
854e2fd36bc8c16147cab323b0418a7666812d3bc7David Pursell
864e2fd36bc8c16147cab323b0418a7666812d3bc7David Pursell    t.SetFeatures(FeatureSetToString(FeatureSet{"bar", "baz"}));
874e2fd36bc8c16147cab323b0418a7666812d3bc7David Pursell    ASSERT_EQ(2U, t.features().size());
884e2fd36bc8c16147cab323b0418a7666812d3bc7David Pursell    ASSERT_FALSE(t.has_feature("foo"));
894e2fd36bc8c16147cab323b0418a7666812d3bc7David Pursell    ASSERT_TRUE(t.has_feature("bar"));
904e2fd36bc8c16147cab323b0418a7666812d3bc7David Pursell    ASSERT_TRUE(t.has_feature("baz"));
91d2b588e23901538f4b459a71fefdac6fc2748f7eDavid Pursell
92d2b588e23901538f4b459a71fefdac6fc2748f7eDavid Pursell    t.SetFeatures("");
93d2b588e23901538f4b459a71fefdac6fc2748f7eDavid Pursell    ASSERT_EQ(0U, t.features().size());
941792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert}
951792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
961792c23cb892ab58590b2cdfce0d0ece30c21787Dan AlbertTEST(transport, parse_banner_no_features) {
971792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    atransport t;
981792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
991792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    parse_banner("host::", &t);
1001792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
1011792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(0U, t.features().size());
1021792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(kCsHost, t.connection_state);
1031792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
1041792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(nullptr, t.product);
1051792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(nullptr, t.model);
1061792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(nullptr, t.device);
1071792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert}
1081792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
1091792c23cb892ab58590b2cdfce0d0ece30c21787Dan AlbertTEST(transport, parse_banner_product_features) {
1101792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    atransport t;
1111792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
1121792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    const char banner[] =
1131792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert        "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;";
1141792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    parse_banner(banner, &t);
1151792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
1161792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(kCsHost, t.connection_state);
1171792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
1181792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(0U, t.features().size());
1191792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
1201792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(std::string("foo"), t.product);
1211792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(std::string("bar"), t.model);
1221792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(std::string("baz"), t.device);
1231792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert}
1241792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
1251792c23cb892ab58590b2cdfce0d0ece30c21787Dan AlbertTEST(transport, parse_banner_features) {
1261792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    atransport t;
1271792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
1281792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    const char banner[] =
1291792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert        "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;"
1301792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert        "features=woodly,doodly";
1311792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    parse_banner(banner, &t);
1321792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
1331792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(kCsHost, t.connection_state);
1341792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
1351792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(2U, t.features().size());
1361792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_TRUE(t.has_feature("woodly"));
1371792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_TRUE(t.has_feature("doodly"));
1381792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
1391792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(std::string("foo"), t.product);
1401792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(std::string("bar"), t.model);
1411792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(std::string("baz"), t.device);
142055f1aa4ff58ba71133d506b202ad46612758dedDan Albert}
1433f902aad5b427a8162bf860a758878b55b13e775David Pursell
1443f902aad5b427a8162bf860a758878b55b13e775David PursellTEST(transport, test_matches_target) {
1453f902aad5b427a8162bf860a758878b55b13e775David Pursell    std::string serial = "foo";
1463f902aad5b427a8162bf860a758878b55b13e775David Pursell    std::string devpath = "/path/to/bar";
1473f902aad5b427a8162bf860a758878b55b13e775David Pursell    std::string product = "test_product";
1483f902aad5b427a8162bf860a758878b55b13e775David Pursell    std::string model = "test_model";
1493f902aad5b427a8162bf860a758878b55b13e775David Pursell    std::string device = "test_device";
1503f902aad5b427a8162bf860a758878b55b13e775David Pursell
1513f902aad5b427a8162bf860a758878b55b13e775David Pursell    atransport t;
1523f902aad5b427a8162bf860a758878b55b13e775David Pursell    t.serial = &serial[0];
1533f902aad5b427a8162bf860a758878b55b13e775David Pursell    t.devpath = &devpath[0];
1543f902aad5b427a8162bf860a758878b55b13e775David Pursell    t.product = &product[0];
1553f902aad5b427a8162bf860a758878b55b13e775David Pursell    t.model = &model[0];
1563f902aad5b427a8162bf860a758878b55b13e775David Pursell    t.device = &device[0];
1573f902aad5b427a8162bf860a758878b55b13e775David Pursell
1583f902aad5b427a8162bf860a758878b55b13e775David Pursell    // These tests should not be affected by the transport type.
1593f902aad5b427a8162bf860a758878b55b13e775David Pursell    for (TransportType type : {kTransportAny, kTransportLocal}) {
1603f902aad5b427a8162bf860a758878b55b13e775David Pursell        t.type = type;
1613f902aad5b427a8162bf860a758878b55b13e775David Pursell
1623f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_TRUE(t.MatchesTarget(serial));
1633f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_TRUE(t.MatchesTarget(devpath));
1643f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_TRUE(t.MatchesTarget("product:" + product));
1653f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_TRUE(t.MatchesTarget("model:" + model));
1663f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_TRUE(t.MatchesTarget("device:" + device));
1673f902aad5b427a8162bf860a758878b55b13e775David Pursell
1683f902aad5b427a8162bf860a758878b55b13e775David Pursell        // Product, model, and device don't match without the prefix.
1693f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_FALSE(t.MatchesTarget(product));
1703f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_FALSE(t.MatchesTarget(model));
1713f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_FALSE(t.MatchesTarget(device));
1723f902aad5b427a8162bf860a758878b55b13e775David Pursell    }
1733f902aad5b427a8162bf860a758878b55b13e775David Pursell}
1743f902aad5b427a8162bf860a758878b55b13e775David Pursell
1753f902aad5b427a8162bf860a758878b55b13e775David PursellTEST(transport, test_matches_target_local) {
1763f902aad5b427a8162bf860a758878b55b13e775David Pursell    std::string serial = "100.100.100.100:5555";
1773f902aad5b427a8162bf860a758878b55b13e775David Pursell
1783f902aad5b427a8162bf860a758878b55b13e775David Pursell    atransport t;
1793f902aad5b427a8162bf860a758878b55b13e775David Pursell    t.serial = &serial[0];
1803f902aad5b427a8162bf860a758878b55b13e775David Pursell
1813f902aad5b427a8162bf860a758878b55b13e775David Pursell    // Network address matching should only be used for local transports.
1823f902aad5b427a8162bf860a758878b55b13e775David Pursell    for (TransportType type : {kTransportAny, kTransportLocal}) {
1833f902aad5b427a8162bf860a758878b55b13e775David Pursell        t.type = type;
1843f902aad5b427a8162bf860a758878b55b13e775David Pursell        bool should_match = (type == kTransportLocal);
1853f902aad5b427a8162bf860a758878b55b13e775David Pursell
1863f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_EQ(should_match, t.MatchesTarget("100.100.100.100"));
1873f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_EQ(should_match, t.MatchesTarget("tcp:100.100.100.100"));
1883f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_EQ(should_match, t.MatchesTarget("tcp:100.100.100.100:5555"));
1893f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_EQ(should_match, t.MatchesTarget("udp:100.100.100.100"));
1903f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_EQ(should_match, t.MatchesTarget("udp:100.100.100.100:5555"));
1913f902aad5b427a8162bf860a758878b55b13e775David Pursell
1923f902aad5b427a8162bf860a758878b55b13e775David Pursell        // Wrong protocol, hostname, or port should never match.
1933f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_FALSE(t.MatchesTarget("100.100.100"));
1943f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_FALSE(t.MatchesTarget("100.100.100.100:"));
1953f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_FALSE(t.MatchesTarget("100.100.100.100:-1"));
1963f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_FALSE(t.MatchesTarget("100.100.100.100:5554"));
1973f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_FALSE(t.MatchesTarget("abc:100.100.100.100"));
1983f902aad5b427a8162bf860a758878b55b13e775David Pursell    }
1993f902aad5b427a8162bf860a758878b55b13e775David Pursell}
200