blob: 4330e3e615d88aa492e5d8d67936f574437de6bb [file] [log] [blame]
Yifan Hongaf766e62021-06-14 13:24:19 -07001/*
2 * Copyright (C) 2021 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 <sysexits.h>
18
19#include <chrono>
20
21#include <android-base/result-gmock.h>
22#include <android-base/strings.h>
23#include <gmock/gmock.h>
24#include <gtest/gtest.h>
25
26#include "../UtilsHost.h"
27
28using android::base::testing::Ok;
29using testing::Optional;
30
31namespace android {
32
33TEST(UtilsHost, ExecuteImmediately) {
34 auto result = execute({"echo", "foo"}, nullptr);
35 ASSERT_THAT(result, Ok());
36 EXPECT_THAT(result->exitCode, Optional(EX_OK));
Colin Crossc9a77aa2021-09-13 16:31:38 -070037 EXPECT_EQ(result->stdoutStr, "foo\n");
Yifan Hongaf766e62021-06-14 13:24:19 -070038}
39
40TEST(UtilsHost, ExecuteLongRunning) {
41 auto now = std::chrono::system_clock::now();
42
43 {
44 std::vector<std::string> args{"sh", "-c",
45 "sleep 0.5 && echo -n f && sleep 0.5 && echo oo && sleep 1"};
46 auto result = execute(std::move(args), [](const CommandResult& commandResult) {
Colin Crossc9a77aa2021-09-13 16:31:38 -070047 return android::base::EndsWith(commandResult.stdoutStr, "\n");
Yifan Hongaf766e62021-06-14 13:24:19 -070048 });
49 auto elapsed = std::chrono::system_clock::now() - now;
50 auto elapsedMs = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count();
51 EXPECT_GE(elapsedMs, 1000);
52 EXPECT_LT(elapsedMs, 2000);
53
54 ASSERT_THAT(result, Ok());
55 EXPECT_EQ(std::nullopt, result->exitCode);
Colin Crossc9a77aa2021-09-13 16:31:38 -070056 EXPECT_EQ(result->stdoutStr, "foo\n");
Yifan Hongaf766e62021-06-14 13:24:19 -070057 }
58
59 // ~CommandResult() called, child process is killed.
60 // Assert that the second sleep does not finish.
61 auto elapsed = std::chrono::system_clock::now() - now;
62 auto elapsedMs = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count();
63 EXPECT_LT(elapsedMs, 2000);
64}
65
66TEST(UtilsHost, ExecuteLongRunning2) {
67 auto now = std::chrono::system_clock::now();
68
69 {
70 std::vector<std::string> args{"sh", "-c",
71 "sleep 2 && echo -n f && sleep 2 && echo oo && sleep 2"};
72 auto result = execute(std::move(args), [](const CommandResult& commandResult) {
Colin Crossc9a77aa2021-09-13 16:31:38 -070073 return android::base::EndsWith(commandResult.stdoutStr, "\n");
Yifan Hongaf766e62021-06-14 13:24:19 -070074 });
75 auto elapsed = std::chrono::system_clock::now() - now;
76 auto elapsedMs = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count();
77 EXPECT_GE(elapsedMs, 4000);
78 EXPECT_LT(elapsedMs, 6000);
79
80 ASSERT_THAT(result, Ok());
81 EXPECT_EQ(std::nullopt, result->exitCode);
Colin Crossc9a77aa2021-09-13 16:31:38 -070082 EXPECT_EQ(result->stdoutStr, "foo\n");
Yifan Hongaf766e62021-06-14 13:24:19 -070083 }
84
85 // ~CommandResult() called, child process is killed.
86 // Assert that the second sleep does not finish.
87 auto elapsed = std::chrono::system_clock::now() - now;
88 auto elapsedMs = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count();
89 EXPECT_LT(elapsedMs, 6000);
90}
91
92TEST(UtilsHost, KillWithSigKill) {
93 std::vector<std::string> args{"sh", "-c", "echo foo && sleep 10"};
94 auto result = execute(std::move(args), [](const CommandResult& commandResult) {
95 // FOR TEST PURPOSE ONLY. DON'T DO THIS!
96 if (commandResult.pid.has_value()) {
97 (void)kill(*commandResult.pid, SIGKILL);
98 }
99 // FOR TEST PURPOSE ONLY. DON'T DO THIS!
100 return false;
101 });
102
103 ASSERT_THAT(result, Ok());
104 EXPECT_EQ(std::nullopt, result->exitCode);
105 EXPECT_THAT(result->signal, Optional(SIGKILL));
106}
107
108} // namespace android