David Pursell | eaae97e | 2016-04-07 11:25:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "adb_listeners.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | #include <android-base/stringprintf.h> |
| 22 | #include <android-base/strings.h> |
| 23 | |
| 24 | #include "fdevent.h" |
| 25 | #include "sysdeps.h" |
| 26 | #include "transport.h" |
| 27 | |
| 28 | // Returns true if the given listener is present in format_listeners(). Empty parameters will |
| 29 | // be ignored. |
| 30 | static bool listener_is_installed(const std::string& serial, const std::string& source, |
| 31 | const std::string& dest) { |
| 32 | // format_listeners() gives lines of "<serial> <source> <dest>\n". |
| 33 | for (const std::string& line : android::base::Split(format_listeners(), "\n")) { |
| 34 | std::vector<std::string> info = android::base::Split(line, " "); |
| 35 | if (info.size() == 3 && |
| 36 | (serial.empty() || info[0] == serial) && |
| 37 | (source.empty() || info[1] == source) && |
| 38 | (dest.empty() || info[2] == dest)) { |
| 39 | return true; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | class AdbListenersTest : public ::testing::Test { |
| 47 | public: |
| 48 | void SetUp() override { |
| 49 | // We don't need an fdevent loop, but adding/removing listeners must be done from the |
| 50 | // fdevent thread if one exists. Since previously run tests may have created an fdevent |
| 51 | // thread, we need to reset to prevent the thread check. |
| 52 | fdevent_reset(); |
| 53 | } |
| 54 | |
| 55 | void TearDown() override { |
| 56 | // Clean up any listeners that may have been installed. |
| 57 | remove_all_listeners(); |
| 58 | |
| 59 | // Make sure we didn't leave any dangling events. |
| 60 | ASSERT_EQ(0u, fdevent_installed_count()); |
| 61 | } |
| 62 | |
| 63 | protected: |
| 64 | atransport transport_; |
| 65 | }; |
| 66 | |
| 67 | TEST_F(AdbListenersTest, test_install_listener) { |
| 68 | std::string error; |
| 69 | |
| 70 | ASSERT_EQ(INSTALL_STATUS_OK, |
| 71 | install_listener("tcp:9000", "tcp:9000", &transport_, false, nullptr, &error)); |
| 72 | ASSERT_TRUE(error.empty()); |
| 73 | |
| 74 | ASSERT_TRUE(listener_is_installed("", "tcp:9000", "tcp:9000")); |
| 75 | } |
| 76 | |
| 77 | TEST_F(AdbListenersTest, test_install_listener_rebind) { |
| 78 | std::string error; |
| 79 | |
| 80 | ASSERT_EQ(INSTALL_STATUS_OK, |
| 81 | install_listener("tcp:9000", "tcp:9000", &transport_, false, nullptr, &error)); |
| 82 | ASSERT_TRUE(error.empty()); |
| 83 | |
| 84 | ASSERT_EQ(INSTALL_STATUS_OK, |
| 85 | install_listener("tcp:9000", "tcp:9001", &transport_, false, nullptr, &error)); |
| 86 | ASSERT_TRUE(error.empty()); |
| 87 | |
| 88 | ASSERT_TRUE(listener_is_installed("", "tcp:9000", "tcp:9001")); |
| 89 | } |
| 90 | |
| 91 | TEST_F(AdbListenersTest, test_install_listener_no_rebind) { |
| 92 | std::string error; |
| 93 | |
| 94 | ASSERT_EQ(INSTALL_STATUS_OK, |
| 95 | install_listener("tcp:9000", "tcp:9000", &transport_, true, nullptr, &error)); |
| 96 | ASSERT_TRUE(error.empty()); |
| 97 | |
| 98 | ASSERT_EQ(INSTALL_STATUS_CANNOT_REBIND, |
| 99 | install_listener("tcp:9000", "tcp:9001", &transport_, true, nullptr, &error)); |
| 100 | ASSERT_FALSE(error.empty()); |
| 101 | |
| 102 | ASSERT_TRUE(listener_is_installed("", "tcp:9000", "tcp:9000")); |
| 103 | } |
| 104 | |
| 105 | TEST_F(AdbListenersTest, test_install_listener_tcp_port_0) { |
| 106 | int port = 0; |
| 107 | std::string error; |
| 108 | |
| 109 | ASSERT_EQ(INSTALL_STATUS_OK, |
| 110 | install_listener("tcp:0", "tcp:9000", &transport_, true, &port, &error)); |
| 111 | ASSERT_TRUE(error.empty()); |
| 112 | |
| 113 | ASSERT_TRUE(listener_is_installed("", android::base::StringPrintf("tcp:%d", port), "tcp:9000")); |
| 114 | } |
| 115 | |
| 116 | TEST_F(AdbListenersTest, test_remove_listener) { |
| 117 | std::string error; |
| 118 | |
| 119 | ASSERT_EQ(INSTALL_STATUS_OK, |
| 120 | install_listener("tcp:9000", "tcp:9000", &transport_, false, nullptr, &error)); |
| 121 | ASSERT_TRUE(error.empty()); |
| 122 | |
| 123 | ASSERT_EQ(INSTALL_STATUS_OK, remove_listener("tcp:9000", &transport_)); |
| 124 | ASSERT_TRUE(format_listeners().empty()); |
| 125 | } |
| 126 | |
| 127 | TEST_F(AdbListenersTest, test_remove_nonexistent_listener) { |
| 128 | std::string error; |
| 129 | |
| 130 | ASSERT_EQ(INSTALL_STATUS_OK, |
| 131 | install_listener("tcp:9000", "tcp:9000", &transport_, false, nullptr, &error)); |
| 132 | ASSERT_TRUE(error.empty()); |
| 133 | |
| 134 | ASSERT_EQ(INSTALL_STATUS_LISTENER_NOT_FOUND, remove_listener("tcp:1", &transport_)); |
| 135 | ASSERT_TRUE(listener_is_installed("", "tcp:9000", "tcp:9000")); |
| 136 | } |
| 137 | |
| 138 | TEST_F(AdbListenersTest, test_remove_all_listeners) { |
| 139 | std::string error; |
| 140 | |
| 141 | ASSERT_EQ(INSTALL_STATUS_OK, |
| 142 | install_listener("tcp:9000", "tcp:9000", &transport_, false, nullptr, &error)); |
| 143 | ASSERT_TRUE(error.empty()); |
| 144 | |
| 145 | ASSERT_EQ(INSTALL_STATUS_OK, |
| 146 | install_listener("tcp:9001", "tcp:9001", &transport_, false, nullptr, &error)); |
| 147 | ASSERT_TRUE(error.empty()); |
| 148 | |
| 149 | remove_all_listeners(); |
| 150 | ASSERT_TRUE(format_listeners().empty()); |
| 151 | } |
| 152 | |
| 153 | TEST_F(AdbListenersTest, test_transport_disconnect) { |
| 154 | std::string error; |
| 155 | |
| 156 | ASSERT_EQ(INSTALL_STATUS_OK, |
| 157 | install_listener("tcp:9000", "tcp:9000", &transport_, false, nullptr, &error)); |
| 158 | ASSERT_TRUE(error.empty()); |
| 159 | |
| 160 | ASSERT_EQ(INSTALL_STATUS_OK, |
| 161 | install_listener("tcp:9001", "tcp:9001", &transport_, false, nullptr, &error)); |
| 162 | ASSERT_TRUE(error.empty()); |
| 163 | |
| 164 | transport_.RunDisconnects(); |
| 165 | ASSERT_TRUE(format_listeners().empty()); |
| 166 | } |