| Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 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 |  | 
 | 33 | TEST(UtilsHost, ExecuteImmediately) { | 
 | 34 |     auto result = execute({"echo", "foo"}, nullptr); | 
 | 35 |     ASSERT_THAT(result, Ok()); | 
 | 36 |     EXPECT_THAT(result->exitCode, Optional(EX_OK)); | 
| Colin Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 37 |     EXPECT_EQ(result->stdoutStr, "foo\n"); | 
| Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 38 | } | 
 | 39 |  | 
 | 40 | TEST(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 Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 47 |             return android::base::EndsWith(commandResult.stdoutStr, "\n"); | 
| Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 48 |         }); | 
 | 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 Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 56 |         EXPECT_EQ(result->stdoutStr, "foo\n"); | 
| Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 57 |     } | 
 | 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 |  | 
 | 66 | TEST(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 Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 73 |             return android::base::EndsWith(commandResult.stdoutStr, "\n"); | 
| Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 74 |         }); | 
 | 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 Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 82 |         EXPECT_EQ(result->stdoutStr, "foo\n"); | 
| Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 83 |     } | 
 | 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 |  | 
 | 92 | TEST(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 |