1 /*
2 * Copyright 2023 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 <android-base/unique_fd.h>
18 #include <gtest/gtest.h>
19 #include <log/log.h>
20
21 #include "testserver/TestServer.h"
22 #include "testserver/TestServerClient.h"
23 #include "testserver/TestServerHost.h"
24
25 using namespace android;
26
27 namespace {
28
29 class TestCaseLogger : public ::testing::EmptyTestEventListener {
OnTestStart(const::testing::TestInfo & testInfo)30 void OnTestStart(const ::testing::TestInfo& testInfo) override {
31 ALOGD("Begin test: %s#%s", testInfo.test_suite_name(), testInfo.name());
32 }
33
OnTestEnd(const testing::TestInfo & testInfo)34 void OnTestEnd(const testing::TestInfo& testInfo) override {
35 ALOGD("End test: %s#%s", testInfo.test_suite_name(), testInfo.name());
36 }
37 };
38
39 } // namespace
40
main(int argc,char ** argv)41 int main(int argc, char** argv) {
42 // There are three modes that we can run in to support the libgui TestServer:
43 //
44 // - libgui_test : normal mode, runs tests and fork/execs the testserver host process
45 // - libgui_test --test-server-host $recvPipeFd $sendPipeFd : TestServerHost mode, listens on
46 // $recvPipeFd for commands and sends responses over $sendPipeFd
47 // - libgui_test --test-server $name : TestServer mode, starts a ITestService binder service
48 // under $name
49 for (int i = 1; i < argc; i++) {
50 std::string arg = argv[i];
51 if (arg == "--test-server-host") {
52 LOG_ALWAYS_FATAL_IF(argc < (i + 2), "--test-server-host requires two pipe fds");
53 // Note that the send/recv are from our perspective.
54 base::unique_fd recvPipeFd = base::unique_fd(atoi(argv[i + 1]));
55 base::unique_fd sendPipeFd = base::unique_fd(atoi(argv[i + 2]));
56 return TestServerHostMain(argv[0], std::move(sendPipeFd), std::move(recvPipeFd));
57 }
58 if (arg == "--test-server") {
59 LOG_ALWAYS_FATAL_IF(argc < (i + 1), "--test-server requires a name");
60 return TestServerMain(argv[i + 1]);
61 }
62 }
63 testing::InitGoogleTest(&argc, argv);
64 testing::UnitTest::GetInstance()->listeners().Append(new TestCaseLogger());
65
66 // This has to be run *before* any test initialization, because it fork/execs a TestServerHost,
67 // which will later create new binder service. You can't do that in a forked thread after you've
68 // initialized any binder stuff, which some tests do.
69 TestServerClient::InitializeOrDie(argv[0]);
70
71 return RUN_ALL_TESTS();
72 }