summaryrefslogtreecommitdiff
path: root/brillo/asynchronous_signal_handler_unittest.cc
blob: ec3b061856e6352d6226b2c1767fcf8191ff979c (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "brillo/asynchronous_signal_handler.h"

#include <signal.h>
#include <sys/types.h>
#include <unistd.h>

#include <vector>

#include <base/bind.h>
#include <base/macros.h>
#include <base/message_loop/message_loop.h>
#include <base/run_loop.h>
#include <brillo/message_loops/base_message_loop.h>
#include <gtest/gtest.h>

namespace brillo {

class AsynchronousSignalHandlerTest : public ::testing::Test {
 public:
  AsynchronousSignalHandlerTest() {}
  virtual ~AsynchronousSignalHandlerTest() {}

  virtual void SetUp() {
    brillo_loop_.SetAsCurrent();
    handler_.Init();
  }

  virtual void TearDown() {}

  bool RecordInfoAndQuit(bool response, const struct signalfd_siginfo& info) {
    infos_.push_back(info);
    brillo_loop_.PostTask(FROM_HERE, brillo_loop_.QuitClosure());
    return response;
  }

 protected:
  base::MessageLoopForIO base_loop_;
  BaseMessageLoop brillo_loop_{&base_loop_};
  std::vector<struct signalfd_siginfo> infos_;
  AsynchronousSignalHandler handler_;

 private:
  DISALLOW_COPY_AND_ASSIGN(AsynchronousSignalHandlerTest);
};

TEST_F(AsynchronousSignalHandlerTest, CheckTerm) {
  handler_.RegisterHandler(
      SIGTERM,
      base::Bind(&AsynchronousSignalHandlerTest::RecordInfoAndQuit,
                 base::Unretained(this),
                 true));
  EXPECT_EQ(0, infos_.size());
  EXPECT_EQ(0, kill(getpid(), SIGTERM));

  // Spin the message loop.
  MessageLoop::current()->Run();

  ASSERT_EQ(1, infos_.size());
  EXPECT_EQ(SIGTERM, infos_[0].ssi_signo);
}

TEST_F(AsynchronousSignalHandlerTest, CheckSignalUnregistration) {
  handler_.RegisterHandler(
      SIGCHLD,
      base::Bind(&AsynchronousSignalHandlerTest::RecordInfoAndQuit,
                 base::Unretained(this),
                 true));
  EXPECT_EQ(0, infos_.size());
  EXPECT_EQ(0, kill(getpid(), SIGCHLD));

  // Spin the message loop.
  MessageLoop::current()->Run();

  ASSERT_EQ(1, infos_.size());
  EXPECT_EQ(SIGCHLD, infos_[0].ssi_signo);

  EXPECT_EQ(0, kill(getpid(), SIGCHLD));

  // Run the loop with a timeout, as no message are expected.
  brillo_loop_.PostDelayedTask(FROM_HERE,
                               base::Bind(&MessageLoop::BreakLoop,
                                          base::Unretained(&brillo_loop_)),
                               base::TimeDelta::FromMilliseconds(10));
  MessageLoop::current()->Run();

  // The signal handle should have been unregistered. No new message are
  // expected.
  EXPECT_EQ(1, infos_.size());
}

TEST_F(AsynchronousSignalHandlerTest, CheckMultipleSignal) {
  const uint8_t NB_SIGNALS = 5;
  handler_.RegisterHandler(
      SIGCHLD,
      base::Bind(&AsynchronousSignalHandlerTest::RecordInfoAndQuit,
                 base::Unretained(this),
                 false));
  EXPECT_EQ(0, infos_.size());
  for (int i = 0; i < NB_SIGNALS; ++i) {
    EXPECT_EQ(0, kill(getpid(), SIGCHLD));

    // Spin the message loop.
    MessageLoop::current()->Run();
  }

  ASSERT_EQ(NB_SIGNALS, infos_.size());
  for (int i = 0; i < NB_SIGNALS; ++i) {
    EXPECT_EQ(SIGCHLD, infos_[i].ssi_signo);
  }
}

TEST_F(AsynchronousSignalHandlerTest, CheckChld) {
  handler_.RegisterHandler(
      SIGCHLD,
      base::Bind(&AsynchronousSignalHandlerTest::RecordInfoAndQuit,
                 base::Unretained(this),
                 false));
  pid_t child_pid = fork();
  if (child_pid == 0) {
    _Exit(EXIT_SUCCESS);
  }

  EXPECT_EQ(0, infos_.size());
  // Spin the message loop.
  MessageLoop::current()->Run();

  ASSERT_EQ(1, infos_.size());
  EXPECT_EQ(SIGCHLD, infos_[0].ssi_signo);
  EXPECT_EQ(child_pid, infos_[0].ssi_pid);
  EXPECT_EQ(static_cast<int>(CLD_EXITED), infos_[0].ssi_code);
  EXPECT_EQ(EXIT_SUCCESS, infos_[0].ssi_status);
}

}  // namespace brillo