1 /*
2 * Copyright (C) 2015 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 <gtest/gtest.h>
18
19 #include <signal.h>
20
21 #include "IOEventLoop.h"
22 #include "utils.h"
23 #include "workload.h"
24
TEST(workload,success)25 TEST(workload, success) {
26 IOEventLoop loop;
27 ASSERT_TRUE(loop.AddSignalEvent(SIGCHLD, [&]() {
28 return loop.ExitLoop();
29 }));
30 auto workload = Workload::CreateWorkload({"sleep", "1"});
31 ASSERT_TRUE(workload != nullptr);
32 ASSERT_TRUE(workload->GetPid() != 0);
33 ASSERT_TRUE(workload->Start());
34 ASSERT_TRUE(loop.RunLoop());
35 }
36
TEST(workload,execvp_failure)37 TEST(workload, execvp_failure) {
38 auto workload = Workload::CreateWorkload({"/dev/null"});
39 ASSERT_TRUE(workload != nullptr);
40 ASSERT_FALSE(workload->Start());
41 }
42
run_signaled_workload()43 static void run_signaled_workload() {
44 {
45 IOEventLoop loop;
46 ASSERT_TRUE(loop.AddSignalEvent(SIGCHLD, [&]() {
47 return loop.ExitLoop();
48 }));
49 auto workload = Workload::CreateWorkload({"sleep", "10"});
50 ASSERT_TRUE(workload != nullptr);
51 ASSERT_TRUE(workload->Start());
52 ASSERT_EQ(0, kill(workload->GetPid(), SIGKILL));
53 ASSERT_TRUE(loop.RunLoop());
54 }
55 // Make sure all destructors are called before exit().
56 exit(0);
57 }
58
TEST(workload,signaled_warning)59 TEST(workload, signaled_warning) {
60 ASSERT_EXIT(run_signaled_workload(), testing::ExitedWithCode(0),
61 "child process was terminated by signal");
62 }
63
run_exit_nonzero_workload()64 static void run_exit_nonzero_workload() {
65 {
66 IOEventLoop loop;
67 ASSERT_TRUE(loop.AddSignalEvent(SIGCHLD, [&]() {
68 return loop.ExitLoop();
69 }));
70 auto workload = Workload::CreateWorkload({"ls", "nonexistdir"});
71 ASSERT_TRUE(workload != nullptr);
72 ASSERT_TRUE(workload->Start());
73 ASSERT_TRUE(loop.RunLoop());
74 }
75 // Make sure all destructors are called before exit().
76 exit(0);
77 }
78
TEST(workload,exit_nonzero_warning)79 TEST(workload, exit_nonzero_warning) {
80 ASSERT_EXIT(run_exit_nonzero_workload(), testing::ExitedWithCode(0),
81 "child process exited with exit code");
82 }
83