• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
28 using android::base::testing::Ok;
29 using testing::Optional;
30 
31 namespace android {
32 
TEST(UtilsHost,ExecuteImmediately)33 TEST(UtilsHost, ExecuteImmediately) {
34     auto result = execute({"echo", "foo"}, nullptr);
35     ASSERT_TRUE(result.has_value());
36     EXPECT_THAT(result->exitCode, Optional(EX_OK));
37     EXPECT_EQ(result->stdoutStr, "foo\n");
38 }
39 
40 template <typename T>
millisSince(std::chrono::time_point<T> now)41 auto millisSince(std::chrono::time_point<T> now) {
42     auto elapsed = std::chrono::system_clock::now() - now;
43     return std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count();
44 }
45 
TEST(UtilsHost,ExecuteLongRunning)46 TEST(UtilsHost, ExecuteLongRunning) {
47     auto start = std::chrono::system_clock::now();
48 
49     {
50         std::vector<std::string>
51                 args{"sh", "-c", "sleep 0.5 && echo -n f && sleep 0.5 && echo oo && sleep 100"};
52         auto result = execute(std::move(args), [&](const CommandResult& commandResult) {
53             std::cout << millisSince(start)
54                       << "ms: GOT PARTIAL COMMAND RESULT:" << commandResult.stdoutStr << std::endl;
55             return android::base::EndsWith(commandResult.stdoutStr, "\n");
56         });
57         auto elapsedMs = millisSince(start);
58         EXPECT_GE(elapsedMs, 1000);
59         EXPECT_LT(elapsedMs, 2000);
60 
61         ASSERT_TRUE(result.has_value());
62         EXPECT_EQ(std::nullopt, result->exitCode);
63         EXPECT_EQ(result->stdoutStr, "foo\n");
64     }
65 
66     // ~CommandResult() called, child process is killed.
67     // Assert that the second sleep does not finish.
68     EXPECT_LT(millisSince(start), 2000);
69 }
70 
TEST(UtilsHost,ExecuteLongRunning2)71 TEST(UtilsHost, ExecuteLongRunning2) {
72     auto start = std::chrono::system_clock::now();
73 
74     {
75         std::vector<std::string> args{"sh", "-c",
76                                       "sleep 2 && echo -n f && sleep 2 && echo oo && sleep 100"};
77         auto result = execute(std::move(args), [&](const CommandResult& commandResult) {
78             std::cout << millisSince(start)
79                       << "ms: GOT PARTIAL COMMAND RESULT:" << commandResult.stdoutStr << std::endl;
80             return android::base::EndsWith(commandResult.stdoutStr, "\n");
81         });
82         auto elapsedMs = millisSince(start);
83         EXPECT_GE(elapsedMs, 4000);
84         EXPECT_LT(elapsedMs, 6000);
85 
86         ASSERT_TRUE(result.has_value());
87         EXPECT_EQ(std::nullopt, result->exitCode);
88         EXPECT_EQ(result->stdoutStr, "foo\n");
89     }
90 
91     // ~CommandResult() called, child process is killed.
92     // Assert that the second sleep does not finish.
93     EXPECT_LT(millisSince(start), 6000);
94 }
95 
TEST(UtilsHost,KillWithSigKill)96 TEST(UtilsHost, KillWithSigKill) {
97     std::vector<std::string> args{"sh", "-c", "echo foo && sleep 10"};
98     auto result = execute(std::move(args), [](const CommandResult& commandResult) {
99         // FOR TEST PURPOSE ONLY. DON'T DO THIS!
100         if (commandResult.pid.has_value()) {
101             (void)kill(*commandResult.pid, SIGKILL);
102         }
103         // FOR TEST PURPOSE ONLY. DON'T DO THIS!
104         return false;
105     });
106 
107     ASSERT_TRUE(result.has_value());
108     EXPECT_EQ(std::nullopt, result->exitCode);
109     EXPECT_THAT(result->signal, Optional(SIGKILL));
110 }
111 
112 } // namespace android
113