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" |
| 22 | |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 23 | class TestTransport : public atransport { |
| 24 | public: |
| 25 | bool operator==(const atransport& rhs) const { |
| 26 | EXPECT_EQ(read_from_remote, rhs.read_from_remote); |
| 27 | EXPECT_EQ(write_to_remote, rhs.write_to_remote); |
| 28 | EXPECT_EQ(close, rhs.close); |
| 29 | EXPECT_EQ(kick, rhs.kick); |
| 30 | |
| 31 | EXPECT_EQ(fd, rhs.fd); |
| 32 | EXPECT_EQ(transport_socket, rhs.transport_socket); |
| 33 | |
| 34 | EXPECT_EQ( |
| 35 | 0, memcmp(&transport_fde, &rhs.transport_fde, sizeof(fdevent))); |
| 36 | |
| 37 | EXPECT_EQ(ref_count, rhs.ref_count); |
| 38 | EXPECT_EQ(sync_token, rhs.sync_token); |
| 39 | EXPECT_EQ(connection_state, rhs.connection_state); |
| 40 | EXPECT_EQ(online, rhs.online); |
| 41 | EXPECT_EQ(type, rhs.type); |
| 42 | |
| 43 | EXPECT_EQ(usb, rhs.usb); |
| 44 | EXPECT_EQ(sfd, rhs.sfd); |
| 45 | |
| 46 | EXPECT_EQ(serial, rhs.serial); |
| 47 | EXPECT_EQ(product, rhs.product); |
| 48 | EXPECT_EQ(model, rhs.model); |
| 49 | EXPECT_EQ(device, rhs.device); |
| 50 | EXPECT_EQ(devpath, rhs.devpath); |
| 51 | EXPECT_EQ(adb_port, rhs.adb_port); |
| 52 | EXPECT_EQ(kicked, rhs.kicked); |
| 53 | |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 54 | EXPECT_EQ(key, rhs.key); |
| 55 | EXPECT_EQ(0, memcmp(token, rhs.token, TOKEN_SIZE)); |
| 56 | EXPECT_EQ(0, memcmp(&auth_fde, &rhs.auth_fde, sizeof(fdevent))); |
| 57 | EXPECT_EQ(failed_auth_attempts, rhs.failed_auth_attempts); |
| 58 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 59 | EXPECT_EQ(features(), rhs.features()); |
| 60 | |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 61 | return true; |
| 62 | } |
| 63 | }; |
| 64 | |
Spencer Low | c9ddd81 | 2015-05-24 15:36:28 -0700 | [diff] [blame] | 65 | class TransportSetup { |
| 66 | public: |
| 67 | TransportSetup() { |
| 68 | #ifdef _WIN32 |
| 69 | // Use extern instead of including sysdeps.h which brings in various macros |
| 70 | // that conflict with APIs used in this file. |
| 71 | extern void adb_sysdeps_init(void); |
| 72 | adb_sysdeps_init(); |
| 73 | #else |
| 74 | // adb_sysdeps_init() is an inline function that we cannot link against. |
| 75 | #endif |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | // Static initializer will call adb_sysdeps_init() before main() to initialize |
| 80 | // the transport mutex before it is used in the tests. Alternatives would be to |
| 81 | // use __attribute__((constructor)) here or to use that or a static initializer |
| 82 | // for adb_sysdeps_init() itself in sysdeps_win32.cpp (caveats of unclear |
| 83 | // init order), or to use a test fixture whose SetUp() could do the init once. |
| 84 | static TransportSetup g_TransportSetup; |
| 85 | |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 86 | TEST(transport, kick_transport) { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 87 | TestTransport t; |
| 88 | |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 89 | // Mutate some member so we can test that the function is run. |
| 90 | t.kick = [](atransport* trans) { trans->fd = 42; }; |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 91 | |
| 92 | TestTransport expected; |
| 93 | expected.kick = t.kick; |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 94 | expected.fd = 42; |
| 95 | expected.kicked = 1; |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 96 | |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 97 | kick_transport(&t); |
| 98 | ASSERT_EQ(42, t.fd); |
| 99 | ASSERT_EQ(1, t.kicked); |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 100 | ASSERT_EQ(expected, t); |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | TEST(transport, kick_transport_already_kicked) { |
| 104 | // Ensure that the transport is not modified if the transport has already been |
| 105 | // kicked. |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 106 | TestTransport t; |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 107 | t.kicked = 1; |
| 108 | t.kick = [](atransport*) { FAIL() << "Kick should not have been called"; }; |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 109 | |
| 110 | TestTransport expected; |
| 111 | expected.kicked = 1; |
| 112 | expected.kick = t.kick; |
| 113 | |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 114 | kick_transport(&t); |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 115 | ASSERT_EQ(expected, t); |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 118 | static void DisconnectFunc(void* arg, atransport*) { |
| 119 | int* count = reinterpret_cast<int*>(arg); |
| 120 | ++*count; |
| 121 | } |
| 122 | |
| 123 | TEST(transport, RunDisconnects) { |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 124 | atransport t; |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 125 | // RunDisconnects() can be called with an empty atransport. |
| 126 | t.RunDisconnects(); |
| 127 | |
| 128 | int count = 0; |
| 129 | adisconnect disconnect; |
| 130 | disconnect.func = DisconnectFunc; |
| 131 | disconnect.opaque = &count; |
| 132 | t.AddDisconnect(&disconnect); |
| 133 | t.RunDisconnects(); |
| 134 | ASSERT_EQ(1, count); |
| 135 | |
| 136 | // disconnect should have been removed automatically. |
| 137 | t.RunDisconnects(); |
| 138 | ASSERT_EQ(1, count); |
| 139 | |
| 140 | count = 0; |
| 141 | t.AddDisconnect(&disconnect); |
| 142 | t.RemoveDisconnect(&disconnect); |
| 143 | t.RunDisconnects(); |
| 144 | ASSERT_EQ(0, count); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 145 | } |
| 146 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 147 | TEST(transport, SetFeatures) { |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 148 | atransport t; |
| 149 | ASSERT_EQ(0U, t.features().size()); |
| 150 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 151 | t.SetFeatures(FeatureSetToString(FeatureSet{"foo"})); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 152 | ASSERT_EQ(1U, t.features().size()); |
| 153 | ASSERT_TRUE(t.has_feature("foo")); |
| 154 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 155 | t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar"})); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 156 | ASSERT_EQ(2U, t.features().size()); |
| 157 | ASSERT_TRUE(t.has_feature("foo")); |
| 158 | ASSERT_TRUE(t.has_feature("bar")); |
| 159 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 160 | t.SetFeatures(FeatureSetToString(FeatureSet{"foo", "bar", "foo"})); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 161 | ASSERT_EQ(2U, t.features().size()); |
| 162 | ASSERT_TRUE(t.has_feature("foo")); |
| 163 | ASSERT_TRUE(t.has_feature("bar")); |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 164 | |
| 165 | t.SetFeatures(FeatureSetToString(FeatureSet{"bar", "baz"})); |
| 166 | ASSERT_EQ(2U, t.features().size()); |
| 167 | ASSERT_FALSE(t.has_feature("foo")); |
| 168 | ASSERT_TRUE(t.has_feature("bar")); |
| 169 | ASSERT_TRUE(t.has_feature("baz")); |
David Pursell | 3d9072b | 2015-09-25 13:04:21 -0700 | [diff] [blame] | 170 | |
| 171 | t.SetFeatures(""); |
| 172 | ASSERT_EQ(0U, t.features().size()); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | TEST(transport, parse_banner_no_features) { |
| 176 | atransport t; |
| 177 | |
| 178 | parse_banner("host::", &t); |
| 179 | |
| 180 | ASSERT_EQ(0U, t.features().size()); |
| 181 | ASSERT_EQ(kCsHost, t.connection_state); |
| 182 | |
| 183 | ASSERT_EQ(nullptr, t.product); |
| 184 | ASSERT_EQ(nullptr, t.model); |
| 185 | ASSERT_EQ(nullptr, t.device); |
| 186 | } |
| 187 | |
| 188 | TEST(transport, parse_banner_product_features) { |
| 189 | atransport t; |
| 190 | |
| 191 | const char banner[] = |
| 192 | "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;"; |
| 193 | parse_banner(banner, &t); |
| 194 | |
| 195 | ASSERT_EQ(kCsHost, t.connection_state); |
| 196 | |
| 197 | ASSERT_EQ(0U, t.features().size()); |
| 198 | |
| 199 | ASSERT_EQ(std::string("foo"), t.product); |
| 200 | ASSERT_EQ(std::string("bar"), t.model); |
| 201 | ASSERT_EQ(std::string("baz"), t.device); |
| 202 | } |
| 203 | |
| 204 | TEST(transport, parse_banner_features) { |
| 205 | atransport t; |
| 206 | |
| 207 | const char banner[] = |
| 208 | "host::ro.product.name=foo;ro.product.model=bar;ro.product.device=baz;" |
| 209 | "features=woodly,doodly"; |
| 210 | parse_banner(banner, &t); |
| 211 | |
| 212 | ASSERT_EQ(kCsHost, t.connection_state); |
| 213 | |
| 214 | ASSERT_EQ(2U, t.features().size()); |
| 215 | ASSERT_TRUE(t.has_feature("woodly")); |
| 216 | ASSERT_TRUE(t.has_feature("doodly")); |
| 217 | |
| 218 | ASSERT_EQ(std::string("foo"), t.product); |
| 219 | ASSERT_EQ(std::string("bar"), t.model); |
| 220 | ASSERT_EQ(std::string("baz"), t.device); |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 221 | } |