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
23b329824e6c5373ae303269dca285d835ce57e514Yabin Cuistatic void DisconnectFunc(void* arg, atransport*) {
24b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    int* count = reinterpret_cast<int*>(arg);
25b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    ++*count;
26b329824e6c5373ae303269dca285d835ce57e514Yabin Cui}
27b329824e6c5373ae303269dca285d835ce57e514Yabin Cui
28b329824e6c5373ae303269dca285d835ce57e514Yabin CuiTEST(transport, RunDisconnects) {
291792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    atransport t;
30b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    // RunDisconnects() can be called with an empty atransport.
31b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    t.RunDisconnects();
32b329824e6c5373ae303269dca285d835ce57e514Yabin Cui
33b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    int count = 0;
34b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    adisconnect disconnect;
35b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    disconnect.func = DisconnectFunc;
36b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    disconnect.opaque = &count;
37b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    t.AddDisconnect(&disconnect);
38b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    t.RunDisconnects();
39b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    ASSERT_EQ(1, count);
40b329824e6c5373ae303269dca285d835ce57e514Yabin Cui
41b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    // disconnect should have been removed automatically.
42b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    t.RunDisconnects();
43b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    ASSERT_EQ(1, count);
44b329824e6c5373ae303269dca285d835ce57e514Yabin Cui
45b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    count = 0;
46b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    t.AddDisconnect(&disconnect);
47b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    t.RemoveDisconnect(&disconnect);
48b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    t.RunDisconnects();
49b329824e6c5373ae303269dca285d835ce57e514Yabin Cui    ASSERT_EQ(0, count);
501792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert}
511792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
524e2fd36bc8c16147cab323b0418a7666812d3bc7David PursellTEST(transport, SetFeatures) {
531792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    atransport t;
541792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(0U, t.features().size());
551792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
564e2fd36bc8c16147cab323b0418a7666812d3bc7David Pursell    t.SetFeatures(FeatureSetToString(FeatureSet{"foo"}));
571792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(1U, t.features().size());
581792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_TRUE(t.has_feature("foo"));
591792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
604e2fd36bc8c16147cab323b0418a7666812d3bc7David Pursell    t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar"}));
611792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(2U, t.features().size());
621792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_TRUE(t.has_feature("foo"));
631792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_TRUE(t.has_feature("bar"));
641792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
654e2fd36bc8c16147cab323b0418a7666812d3bc7David Pursell    t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar", "foo"}));
661792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(2U, t.features().size());
671792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_TRUE(t.has_feature("foo"));
681792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_TRUE(t.has_feature("bar"));
694e2fd36bc8c16147cab323b0418a7666812d3bc7David Pursell
704e2fd36bc8c16147cab323b0418a7666812d3bc7David Pursell    t.SetFeatures(FeatureSetToString(FeatureSet{"bar", "baz"}));
714e2fd36bc8c16147cab323b0418a7666812d3bc7David Pursell    ASSERT_EQ(2U, t.features().size());
724e2fd36bc8c16147cab323b0418a7666812d3bc7David Pursell    ASSERT_FALSE(t.has_feature("foo"));
734e2fd36bc8c16147cab323b0418a7666812d3bc7David Pursell    ASSERT_TRUE(t.has_feature("bar"));
744e2fd36bc8c16147cab323b0418a7666812d3bc7David Pursell    ASSERT_TRUE(t.has_feature("baz"));
75d2b588e23901538f4b459a71fefdac6fc2748f7eDavid Pursell
76d2b588e23901538f4b459a71fefdac6fc2748f7eDavid Pursell    t.SetFeatures("");
77d2b588e23901538f4b459a71fefdac6fc2748f7eDavid Pursell    ASSERT_EQ(0U, t.features().size());
781792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert}
791792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
801792c23cb892ab58590b2cdfce0d0ece30c21787Dan AlbertTEST(transport, parse_banner_no_features) {
81b5e11415d9fdb929321c66889063dac50fb737afYabin Cui    set_main_thread();
821792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    atransport t;
831792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
841792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    parse_banner("host::", &t);
851792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
861792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(0U, t.features().size());
87b5e11415d9fdb929321c66889063dac50fb737afYabin Cui    ASSERT_EQ(kCsHost, t.GetConnectionState());
881792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
891792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(nullptr, t.product);
901792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(nullptr, t.model);
911792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(nullptr, t.device);
921792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert}
931792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
941792c23cb892ab58590b2cdfce0d0ece30c21787Dan AlbertTEST(transport, parse_banner_product_features) {
951792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    atransport t;
961792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
971792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    const char banner[] =
981792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert        "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;";
991792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    parse_banner(banner, &t);
1001792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
101b5e11415d9fdb929321c66889063dac50fb737afYabin Cui    ASSERT_EQ(kCsHost, t.GetConnectionState());
1021792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
1031792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(0U, t.features().size());
1041792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
1051792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(std::string("foo"), t.product);
1061792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(std::string("bar"), t.model);
1071792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(std::string("baz"), t.device);
1081792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert}
1091792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
1101792c23cb892ab58590b2cdfce0d0ece30c21787Dan AlbertTEST(transport, parse_banner_features) {
1111792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    atransport t;
1121792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
1131792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    const char banner[] =
1141792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert        "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;"
1151792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert        "features=woodly,doodly";
1161792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    parse_banner(banner, &t);
1171792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
118b5e11415d9fdb929321c66889063dac50fb737afYabin Cui    ASSERT_EQ(kCsHost, t.GetConnectionState());
1191792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
1201792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(2U, t.features().size());
1211792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_TRUE(t.has_feature("woodly"));
1221792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_TRUE(t.has_feature("doodly"));
1231792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert
1241792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(std::string("foo"), t.product);
1251792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(std::string("bar"), t.model);
1261792c23cb892ab58590b2cdfce0d0ece30c21787Dan Albert    ASSERT_EQ(std::string("baz"), t.device);
127055f1aa4ff58ba71133d506b202ad46612758dedDan Albert}
1283f902aad5b427a8162bf860a758878b55b13e775David Pursell
1293f902aad5b427a8162bf860a758878b55b13e775David PursellTEST(transport, test_matches_target) {
1303f902aad5b427a8162bf860a758878b55b13e775David Pursell    std::string serial = "foo";
1313f902aad5b427a8162bf860a758878b55b13e775David Pursell    std::string devpath = "/path/to/bar";
1323f902aad5b427a8162bf860a758878b55b13e775David Pursell    std::string product = "test_product";
1333f902aad5b427a8162bf860a758878b55b13e775David Pursell    std::string model = "test_model";
1343f902aad5b427a8162bf860a758878b55b13e775David Pursell    std::string device = "test_device";
1353f902aad5b427a8162bf860a758878b55b13e775David Pursell
1363f902aad5b427a8162bf860a758878b55b13e775David Pursell    atransport t;
1373f902aad5b427a8162bf860a758878b55b13e775David Pursell    t.serial = &serial[0];
1383f902aad5b427a8162bf860a758878b55b13e775David Pursell    t.devpath = &devpath[0];
1393f902aad5b427a8162bf860a758878b55b13e775David Pursell    t.product = &product[0];
1403f902aad5b427a8162bf860a758878b55b13e775David Pursell    t.model = &model[0];
1413f902aad5b427a8162bf860a758878b55b13e775David Pursell    t.device = &device[0];
1423f902aad5b427a8162bf860a758878b55b13e775David Pursell
1433f902aad5b427a8162bf860a758878b55b13e775David Pursell    // These tests should not be affected by the transport type.
1443f902aad5b427a8162bf860a758878b55b13e775David Pursell    for (TransportType type : {kTransportAny, kTransportLocal}) {
1453f902aad5b427a8162bf860a758878b55b13e775David Pursell        t.type = type;
1463f902aad5b427a8162bf860a758878b55b13e775David Pursell
1473f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_TRUE(t.MatchesTarget(serial));
1483f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_TRUE(t.MatchesTarget(devpath));
1493f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_TRUE(t.MatchesTarget("product:" + product));
1503f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_TRUE(t.MatchesTarget("model:" + model));
1513f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_TRUE(t.MatchesTarget("device:" + device));
1523f902aad5b427a8162bf860a758878b55b13e775David Pursell
1533f902aad5b427a8162bf860a758878b55b13e775David Pursell        // Product, model, and device don't match without the prefix.
1543f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_FALSE(t.MatchesTarget(product));
1553f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_FALSE(t.MatchesTarget(model));
1563f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_FALSE(t.MatchesTarget(device));
1573f902aad5b427a8162bf860a758878b55b13e775David Pursell    }
1583f902aad5b427a8162bf860a758878b55b13e775David Pursell}
1593f902aad5b427a8162bf860a758878b55b13e775David Pursell
1603f902aad5b427a8162bf860a758878b55b13e775David PursellTEST(transport, test_matches_target_local) {
1613f902aad5b427a8162bf860a758878b55b13e775David Pursell    std::string serial = "100.100.100.100:5555";
1623f902aad5b427a8162bf860a758878b55b13e775David Pursell
1633f902aad5b427a8162bf860a758878b55b13e775David Pursell    atransport t;
1643f902aad5b427a8162bf860a758878b55b13e775David Pursell    t.serial = &serial[0];
1653f902aad5b427a8162bf860a758878b55b13e775David Pursell
1663f902aad5b427a8162bf860a758878b55b13e775David Pursell    // Network address matching should only be used for local transports.
1673f902aad5b427a8162bf860a758878b55b13e775David Pursell    for (TransportType type : {kTransportAny, kTransportLocal}) {
1683f902aad5b427a8162bf860a758878b55b13e775David Pursell        t.type = type;
1693f902aad5b427a8162bf860a758878b55b13e775David Pursell        bool should_match = (type == kTransportLocal);
1703f902aad5b427a8162bf860a758878b55b13e775David Pursell
1713f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_EQ(should_match, t.MatchesTarget("100.100.100.100"));
1723f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_EQ(should_match, t.MatchesTarget("tcp:100.100.100.100"));
1733f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_EQ(should_match, t.MatchesTarget("tcp:100.100.100.100:5555"));
1743f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_EQ(should_match, t.MatchesTarget("udp:100.100.100.100"));
1753f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_EQ(should_match, t.MatchesTarget("udp:100.100.100.100:5555"));
1763f902aad5b427a8162bf860a758878b55b13e775David Pursell
1773f902aad5b427a8162bf860a758878b55b13e775David Pursell        // Wrong protocol, hostname, or port should never match.
1783f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_FALSE(t.MatchesTarget("100.100.100"));
1793f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_FALSE(t.MatchesTarget("100.100.100.100:"));
1803f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_FALSE(t.MatchesTarget("100.100.100.100:-1"));
1813f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_FALSE(t.MatchesTarget("100.100.100.100:5554"));
1823f902aad5b427a8162bf860a758878b55b13e775David Pursell        EXPECT_FALSE(t.MatchesTarget("abc:100.100.100.100"));
1833f902aad5b427a8162bf860a758878b55b13e775David Pursell    }
1843f902aad5b427a8162bf860a758878b55b13e775David Pursell}
185