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 | |
| 54 | EXPECT_EQ( |
| 55 | 0, memcmp(&disconnects, &rhs.disconnects, sizeof(adisconnect))); |
| 56 | |
| 57 | EXPECT_EQ(key, rhs.key); |
| 58 | EXPECT_EQ(0, memcmp(token, rhs.token, TOKEN_SIZE)); |
| 59 | EXPECT_EQ(0, memcmp(&auth_fde, &rhs.auth_fde, sizeof(fdevent))); |
| 60 | EXPECT_EQ(failed_auth_attempts, rhs.failed_auth_attempts); |
| 61 | |
| 62 | return true; |
| 63 | } |
| 64 | }; |
| 65 | |
Spencer Low | c9ddd81 | 2015-05-24 15:36:28 -0700 | [diff] [blame] | 66 | class TransportSetup { |
| 67 | public: |
| 68 | TransportSetup() { |
| 69 | #ifdef _WIN32 |
| 70 | // Use extern instead of including sysdeps.h which brings in various macros |
| 71 | // that conflict with APIs used in this file. |
| 72 | extern void adb_sysdeps_init(void); |
| 73 | adb_sysdeps_init(); |
| 74 | #else |
| 75 | // adb_sysdeps_init() is an inline function that we cannot link against. |
| 76 | #endif |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | // Static initializer will call adb_sysdeps_init() before main() to initialize |
| 81 | // the transport mutex before it is used in the tests. Alternatives would be to |
| 82 | // use __attribute__((constructor)) here or to use that or a static initializer |
| 83 | // for adb_sysdeps_init() itself in sysdeps_win32.cpp (caveats of unclear |
| 84 | // init order), or to use a test fixture whose SetUp() could do the init once. |
| 85 | static TransportSetup g_TransportSetup; |
| 86 | |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 87 | TEST(transport, kick_transport) { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 88 | TestTransport t; |
| 89 | |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 90 | // Mutate some member so we can test that the function is run. |
| 91 | t.kick = [](atransport* trans) { trans->fd = 42; }; |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 92 | |
| 93 | TestTransport expected; |
| 94 | expected.kick = t.kick; |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 95 | expected.fd = 42; |
| 96 | expected.kicked = 1; |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 97 | |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 98 | kick_transport(&t); |
| 99 | ASSERT_EQ(42, t.fd); |
| 100 | ASSERT_EQ(1, t.kicked); |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 101 | ASSERT_EQ(expected, t); |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | TEST(transport, kick_transport_already_kicked) { |
| 105 | // Ensure that the transport is not modified if the transport has already been |
| 106 | // kicked. |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 107 | TestTransport t; |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 108 | t.kicked = 1; |
| 109 | t.kick = [](atransport*) { FAIL() << "Kick should not have been called"; }; |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 110 | |
| 111 | TestTransport expected; |
| 112 | expected.kicked = 1; |
| 113 | expected.kick = t.kick; |
| 114 | |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 115 | kick_transport(&t); |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 116 | ASSERT_EQ(expected, t); |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Disabled because the function currently segfaults for a zeroed atransport. I |
| 120 | // want to make sure I understand how this is working at all before I try fixing |
| 121 | // that. |
| 122 | TEST(transport, DISABLED_run_transport_disconnects_zeroed_atransport) { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 123 | atransport t; |
Dan Albert | 4895c52 | 2015-02-20 17:24:58 -0800 | [diff] [blame] | 124 | run_transport_disconnects(&t); |
| 125 | } |