Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [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 <gtest/gtest.h> |
| 18 | |
| 19 | #include "socket.h" |
| 20 | #include "sysdeps.h" |
| 21 | |
| 22 | class FdeventTest : public ::testing::Test { |
| 23 | protected: |
| 24 | int dummy = -1; |
| 25 | |
| 26 | static void SetUpTestCase() { |
| 27 | #if !defined(_WIN32) |
| 28 | ASSERT_NE(SIG_ERR, signal(SIGPIPE, SIG_IGN)); |
| 29 | #endif |
| 30 | } |
| 31 | |
| 32 | void SetUp() override { |
| 33 | fdevent_reset(); |
| 34 | ASSERT_EQ(0u, fdevent_installed_count()); |
| 35 | } |
| 36 | |
| 37 | // Register a dummy socket used to wake up the fdevent loop to tell it to die. |
| 38 | void PrepareThread() { |
| 39 | int dummy_fds[2]; |
| 40 | if (adb_socketpair(dummy_fds) != 0) { |
| 41 | FAIL() << "failed to create socketpair: " << strerror(errno); |
| 42 | } |
| 43 | |
| 44 | asocket* dummy_socket = create_local_socket(dummy_fds[1]); |
| 45 | if (!dummy_socket) { |
| 46 | FAIL() << "failed to create local socket: " << strerror(errno); |
| 47 | } |
| 48 | dummy_socket->ready(dummy_socket); |
| 49 | dummy = dummy_fds[0]; |
| 50 | } |
| 51 | |
Yabin Cui | 40a4727 | 2016-04-25 19:48:19 -0700 | [diff] [blame] | 52 | size_t GetAdditionalLocalSocketCount() { |
| 53 | #if ADB_HOST |
| 54 | // dummy socket installed in PrepareThread() |
| 55 | return 1; |
| 56 | #else |
| 57 | // dummy socket and one more socket installed in fdevent_subproc_setup() |
| 58 | return 2; |
| 59 | #endif |
| 60 | } |
| 61 | |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 62 | void TerminateThread(adb_thread_t thread) { |
| 63 | fdevent_terminate_loop(); |
| 64 | ASSERT_TRUE(WriteFdExactly(dummy, "", 1)); |
| 65 | ASSERT_TRUE(adb_thread_join(thread)); |
| 66 | ASSERT_EQ(0, adb_close(dummy)); |
| 67 | } |
| 68 | }; |