transport_test.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #include "transport.h"
  17. #include <gtest/gtest.h>
  18. #include "adb.h"
  19. #include "fdevent_test.h"
  20. struct TransportTest : public FdeventTest {};
  21. static void DisconnectFunc(void* arg, atransport*) {
  22. int* count = reinterpret_cast<int*>(arg);
  23. ++*count;
  24. }
  25. TEST_F(TransportTest, RunDisconnects) {
  26. atransport t;
  27. // RunDisconnects() can be called with an empty atransport.
  28. t.RunDisconnects();
  29. int count = 0;
  30. adisconnect disconnect;
  31. disconnect.func = DisconnectFunc;
  32. disconnect.opaque = &count;
  33. t.AddDisconnect(&disconnect);
  34. t.RunDisconnects();
  35. ASSERT_EQ(1, count);
  36. // disconnect should have been removed automatically.
  37. t.RunDisconnects();
  38. ASSERT_EQ(1, count);
  39. count = 0;
  40. t.AddDisconnect(&disconnect);
  41. t.RemoveDisconnect(&disconnect);
  42. t.RunDisconnects();
  43. ASSERT_EQ(0, count);
  44. }
  45. TEST_F(TransportTest, SetFeatures) {
  46. atransport t;
  47. ASSERT_EQ(0U, t.features().size());
  48. t.SetFeatures(FeatureSetToString(FeatureSet{"foo"}));
  49. ASSERT_EQ(1U, t.features().size());
  50. ASSERT_TRUE(t.has_feature("foo"));
  51. t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar"}));
  52. ASSERT_EQ(2U, t.features().size());
  53. ASSERT_TRUE(t.has_feature("foo"));
  54. ASSERT_TRUE(t.has_feature("bar"));
  55. t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar", "foo"}));
  56. ASSERT_EQ(2U, t.features().size());
  57. ASSERT_TRUE(t.has_feature("foo"));
  58. ASSERT_TRUE(t.has_feature("bar"));
  59. t.SetFeatures(FeatureSetToString(FeatureSet{"bar", "baz"}));
  60. ASSERT_EQ(2U, t.features().size());
  61. ASSERT_FALSE(t.has_feature("foo"));
  62. ASSERT_TRUE(t.has_feature("bar"));
  63. ASSERT_TRUE(t.has_feature("baz"));
  64. t.SetFeatures("");
  65. ASSERT_EQ(0U, t.features().size());
  66. }
  67. TEST_F(TransportTest, parse_banner_no_features) {
  68. atransport t;
  69. parse_banner("host::", &t);
  70. ASSERT_EQ(0U, t.features().size());
  71. ASSERT_EQ(kCsHost, t.GetConnectionState());
  72. ASSERT_EQ(std::string(), t.product);
  73. ASSERT_EQ(std::string(), t.model);
  74. ASSERT_EQ(std::string(), t.device);
  75. }
  76. TEST_F(TransportTest, parse_banner_product_features) {
  77. atransport t;
  78. const char banner[] =
  79. "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;";
  80. parse_banner(banner, &t);
  81. ASSERT_EQ(kCsHost, t.GetConnectionState());
  82. ASSERT_EQ(0U, t.features().size());
  83. ASSERT_EQ(std::string("foo"), t.product);
  84. ASSERT_EQ(std::string("bar"), t.model);
  85. ASSERT_EQ(std::string("baz"), t.device);
  86. }
  87. TEST_F(TransportTest, parse_banner_features) {
  88. atransport t;
  89. const char banner[] =
  90. "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;"
  91. "features=woodly,doodly";
  92. parse_banner(banner, &t);
  93. ASSERT_EQ(kCsHost, t.GetConnectionState());
  94. ASSERT_EQ(2U, t.features().size());
  95. ASSERT_TRUE(t.has_feature("woodly"));
  96. ASSERT_TRUE(t.has_feature("doodly"));
  97. ASSERT_EQ(std::string("foo"), t.product);
  98. ASSERT_EQ(std::string("bar"), t.model);
  99. ASSERT_EQ(std::string("baz"), t.device);
  100. }
  101. TEST_F(TransportTest, test_matches_target) {
  102. std::string serial = "foo";
  103. std::string devpath = "/path/to/bar";
  104. std::string product = "test_product";
  105. std::string model = "test_model";
  106. std::string device = "test_device";
  107. atransport t;
  108. t.serial = &serial[0];
  109. t.devpath = &devpath[0];
  110. t.product = &product[0];
  111. t.model = &model[0];
  112. t.device = &device[0];
  113. // These tests should not be affected by the transport type.
  114. for (TransportType type : {kTransportAny, kTransportLocal}) {
  115. t.type = type;
  116. EXPECT_TRUE(t.MatchesTarget(serial));
  117. EXPECT_TRUE(t.MatchesTarget(devpath));
  118. EXPECT_TRUE(t.MatchesTarget("product:" + product));
  119. EXPECT_TRUE(t.MatchesTarget("model:" + model));
  120. EXPECT_TRUE(t.MatchesTarget("device:" + device));
  121. // Product, model, and device don't match without the prefix.
  122. EXPECT_FALSE(t.MatchesTarget(product));
  123. EXPECT_FALSE(t.MatchesTarget(model));
  124. EXPECT_FALSE(t.MatchesTarget(device));
  125. }
  126. }
  127. TEST_F(TransportTest, test_matches_target_local) {
  128. std::string serial = "100.100.100.100:5555";
  129. atransport t;
  130. t.serial = &serial[0];
  131. // Network address matching should only be used for local transports.
  132. for (TransportType type : {kTransportAny, kTransportLocal}) {
  133. t.type = type;
  134. bool should_match = (type == kTransportLocal);
  135. EXPECT_EQ(should_match, t.MatchesTarget("100.100.100.100"));
  136. EXPECT_EQ(should_match, t.MatchesTarget("tcp:100.100.100.100"));
  137. EXPECT_EQ(should_match, t.MatchesTarget("tcp:100.100.100.100:5555"));
  138. EXPECT_EQ(should_match, t.MatchesTarget("udp:100.100.100.100"));
  139. EXPECT_EQ(should_match, t.MatchesTarget("udp:100.100.100.100:5555"));
  140. // Wrong protocol, hostname, or port should never match.
  141. EXPECT_FALSE(t.MatchesTarget("100.100.100"));
  142. EXPECT_FALSE(t.MatchesTarget("100.100.100.100:"));
  143. EXPECT_FALSE(t.MatchesTarget("100.100.100.100:-1"));
  144. EXPECT_FALSE(t.MatchesTarget("100.100.100.100:5554"));
  145. EXPECT_FALSE(t.MatchesTarget("abc:100.100.100.100"));
  146. }
  147. }