1022d447e9efcff59e22f0ab13764282116f235ddJosh Gao/*
2022d447e9efcff59e22f0ab13764282116f235ddJosh Gao * Copyright (C) 2016 The Android Open Source Project
3022d447e9efcff59e22f0ab13764282116f235ddJosh Gao *
4022d447e9efcff59e22f0ab13764282116f235ddJosh Gao * Licensed under the Apache License, Version 2.0 (the "License");
5022d447e9efcff59e22f0ab13764282116f235ddJosh Gao * you may not use this file except in compliance with the License.
6022d447e9efcff59e22f0ab13764282116f235ddJosh Gao * You may obtain a copy of the License at
7022d447e9efcff59e22f0ab13764282116f235ddJosh Gao *
8022d447e9efcff59e22f0ab13764282116f235ddJosh Gao *      http://www.apache.org/licenses/LICENSE-2.0
9022d447e9efcff59e22f0ab13764282116f235ddJosh Gao *
10022d447e9efcff59e22f0ab13764282116f235ddJosh Gao * Unless required by applicable law or agreed to in writing, software
11022d447e9efcff59e22f0ab13764282116f235ddJosh Gao * distributed under the License is distributed on an "AS IS" BASIS,
12022d447e9efcff59e22f0ab13764282116f235ddJosh Gao * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13022d447e9efcff59e22f0ab13764282116f235ddJosh Gao * See the License for the specific language governing permissions and
14022d447e9efcff59e22f0ab13764282116f235ddJosh Gao * limitations under the License.
15022d447e9efcff59e22f0ab13764282116f235ddJosh Gao */
16022d447e9efcff59e22f0ab13764282116f235ddJosh Gao
17022d447e9efcff59e22f0ab13764282116f235ddJosh Gao#include <gtest/gtest.h>
18022d447e9efcff59e22f0ab13764282116f235ddJosh Gao
19022d447e9efcff59e22f0ab13764282116f235ddJosh Gao#include "socket.h"
20022d447e9efcff59e22f0ab13764282116f235ddJosh Gao#include "sysdeps.h"
21022d447e9efcff59e22f0ab13764282116f235ddJosh Gao
22022d447e9efcff59e22f0ab13764282116f235ddJosh Gaoclass FdeventTest : public ::testing::Test {
23022d447e9efcff59e22f0ab13764282116f235ddJosh Gao  protected:
24022d447e9efcff59e22f0ab13764282116f235ddJosh Gao    int dummy = -1;
25022d447e9efcff59e22f0ab13764282116f235ddJosh Gao
26022d447e9efcff59e22f0ab13764282116f235ddJosh Gao    static void SetUpTestCase() {
27022d447e9efcff59e22f0ab13764282116f235ddJosh Gao#if !defined(_WIN32)
28022d447e9efcff59e22f0ab13764282116f235ddJosh Gao        ASSERT_NE(SIG_ERR, signal(SIGPIPE, SIG_IGN));
29022d447e9efcff59e22f0ab13764282116f235ddJosh Gao#endif
30022d447e9efcff59e22f0ab13764282116f235ddJosh Gao    }
31022d447e9efcff59e22f0ab13764282116f235ddJosh Gao
32022d447e9efcff59e22f0ab13764282116f235ddJosh Gao    void SetUp() override {
33022d447e9efcff59e22f0ab13764282116f235ddJosh Gao        fdevent_reset();
34022d447e9efcff59e22f0ab13764282116f235ddJosh Gao        ASSERT_EQ(0u, fdevent_installed_count());
35022d447e9efcff59e22f0ab13764282116f235ddJosh Gao    }
36022d447e9efcff59e22f0ab13764282116f235ddJosh Gao
37022d447e9efcff59e22f0ab13764282116f235ddJosh Gao    // Register a dummy socket used to wake up the fdevent loop to tell it to die.
38022d447e9efcff59e22f0ab13764282116f235ddJosh Gao    void PrepareThread() {
39022d447e9efcff59e22f0ab13764282116f235ddJosh Gao        int dummy_fds[2];
40022d447e9efcff59e22f0ab13764282116f235ddJosh Gao        if (adb_socketpair(dummy_fds) != 0) {
41022d447e9efcff59e22f0ab13764282116f235ddJosh Gao            FAIL() << "failed to create socketpair: " << strerror(errno);
42022d447e9efcff59e22f0ab13764282116f235ddJosh Gao        }
43022d447e9efcff59e22f0ab13764282116f235ddJosh Gao
44022d447e9efcff59e22f0ab13764282116f235ddJosh Gao        asocket* dummy_socket = create_local_socket(dummy_fds[1]);
45022d447e9efcff59e22f0ab13764282116f235ddJosh Gao        if (!dummy_socket) {
46022d447e9efcff59e22f0ab13764282116f235ddJosh Gao            FAIL() << "failed to create local socket: " << strerror(errno);
47022d447e9efcff59e22f0ab13764282116f235ddJosh Gao        }
48022d447e9efcff59e22f0ab13764282116f235ddJosh Gao        dummy_socket->ready(dummy_socket);
49022d447e9efcff59e22f0ab13764282116f235ddJosh Gao        dummy = dummy_fds[0];
50022d447e9efcff59e22f0ab13764282116f235ddJosh Gao    }
51022d447e9efcff59e22f0ab13764282116f235ddJosh Gao
522407d7c3d936eb9649d2e1614f440f89bf9d5bedYabin Cui    size_t GetAdditionalLocalSocketCount() {
532407d7c3d936eb9649d2e1614f440f89bf9d5bedYabin Cui#if ADB_HOST
542407d7c3d936eb9649d2e1614f440f89bf9d5bedYabin Cui        // dummy socket installed in PrepareThread()
552407d7c3d936eb9649d2e1614f440f89bf9d5bedYabin Cui        return 1;
562407d7c3d936eb9649d2e1614f440f89bf9d5bedYabin Cui#else
572407d7c3d936eb9649d2e1614f440f89bf9d5bedYabin Cui        // dummy socket and one more socket installed in fdevent_subproc_setup()
582407d7c3d936eb9649d2e1614f440f89bf9d5bedYabin Cui        return 2;
592407d7c3d936eb9649d2e1614f440f89bf9d5bedYabin Cui#endif
602407d7c3d936eb9649d2e1614f440f89bf9d5bedYabin Cui    }
612407d7c3d936eb9649d2e1614f440f89bf9d5bedYabin Cui
62022d447e9efcff59e22f0ab13764282116f235ddJosh Gao    void TerminateThread(adb_thread_t thread) {
63022d447e9efcff59e22f0ab13764282116f235ddJosh Gao        fdevent_terminate_loop();
64022d447e9efcff59e22f0ab13764282116f235ddJosh Gao        ASSERT_TRUE(WriteFdExactly(dummy, "", 1));
65022d447e9efcff59e22f0ab13764282116f235ddJosh Gao        ASSERT_TRUE(adb_thread_join(thread));
66022d447e9efcff59e22f0ab13764282116f235ddJosh Gao        ASSERT_EQ(0, adb_close(dummy));
67022d447e9efcff59e22f0ab13764282116f235ddJosh Gao    }
68022d447e9efcff59e22f0ab13764282116f235ddJosh Gao};
69