File: binderUtilsHostTest.cpp

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (112 lines) | stat: -rw-r--r-- 3,851 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <sysexits.h>

#include <chrono>

#include <android-base/result-gmock.h>
#include <android-base/strings.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "../UtilsHost.h"

using android::base::testing::Ok;
using testing::Optional;

namespace android {

TEST(UtilsHost, ExecuteImmediately) {
    auto result = execute({"echo", "foo"}, nullptr);
    ASSERT_THAT(result, Ok());
    EXPECT_THAT(result->exitCode, Optional(EX_OK));
    EXPECT_EQ(result->stdoutStr, "foo\n");
}

template <typename T>
auto millisSince(std::chrono::time_point<T> now) {
    auto elapsed = std::chrono::system_clock::now() - now;
    return std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count();
}

TEST(UtilsHost, ExecuteLongRunning) {
    auto start = std::chrono::system_clock::now();

    {
        std::vector<std::string>
                args{"sh", "-c", "sleep 0.5 && echo -n f && sleep 0.5 && echo oo && sleep 100"};
        auto result = execute(std::move(args), [&](const CommandResult& commandResult) {
            std::cout << millisSince(start)
                      << "ms: GOT PARTIAL COMMAND RESULT:" << commandResult.stdoutStr << std::endl;
            return android::base::EndsWith(commandResult.stdoutStr, "\n");
        });
        auto elapsedMs = millisSince(start);
        EXPECT_GE(elapsedMs, 1000);
        EXPECT_LT(elapsedMs, 2000);

        ASSERT_THAT(result, Ok());
        EXPECT_EQ(std::nullopt, result->exitCode);
        EXPECT_EQ(result->stdoutStr, "foo\n");
    }

    // ~CommandResult() called, child process is killed.
    // Assert that the second sleep does not finish.
    EXPECT_LT(millisSince(start), 2000);
}

TEST(UtilsHost, ExecuteLongRunning2) {
    auto start = std::chrono::system_clock::now();

    {
        std::vector<std::string> args{"sh", "-c",
                                      "sleep 2 && echo -n f && sleep 2 && echo oo && sleep 100"};
        auto result = execute(std::move(args), [&](const CommandResult& commandResult) {
            std::cout << millisSince(start)
                      << "ms: GOT PARTIAL COMMAND RESULT:" << commandResult.stdoutStr << std::endl;
            return android::base::EndsWith(commandResult.stdoutStr, "\n");
        });
        auto elapsedMs = millisSince(start);
        EXPECT_GE(elapsedMs, 4000);
        EXPECT_LT(elapsedMs, 6000);

        ASSERT_THAT(result, Ok());
        EXPECT_EQ(std::nullopt, result->exitCode);
        EXPECT_EQ(result->stdoutStr, "foo\n");
    }

    // ~CommandResult() called, child process is killed.
    // Assert that the second sleep does not finish.
    EXPECT_LT(millisSince(start), 6000);
}

TEST(UtilsHost, KillWithSigKill) {
    std::vector<std::string> args{"sh", "-c", "echo foo && sleep 10"};
    auto result = execute(std::move(args), [](const CommandResult& commandResult) {
        // FOR TEST PURPOSE ONLY. DON'T DO THIS!
        if (commandResult.pid.has_value()) {
            (void)kill(*commandResult.pid, SIGKILL);
        }
        // FOR TEST PURPOSE ONLY. DON'T DO THIS!
        return false;
    });

    ASSERT_THAT(result, Ok());
    EXPECT_EQ(std::nullopt, result->exitCode);
    EXPECT_THAT(result->signal, Optional(SIGKILL));
}

} // namespace android