blob: 5fb870ff4fc40588f076f95d19cf60a290ba5575 [file] [log] [blame]
Gunnar Mills99258572017-07-27 15:22:42 -05001#include "evdev.hpp"
Patrick Venturedace6802018-11-01 16:52:10 -07002#include "monitor.hpp"
3
4#include <linux/input.h>
5#include <sys/types.h>
6
7#include <chrono>
8#include <iostream>
9#include <string>
10
11#include <gtest/gtest.h>
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +053012
13using namespace phosphor::gpio;
14
15// Exit helper. Ideally should be class but need
16// this to be used inside a static method.
Patrick Venturedace6802018-11-01 16:52:10 -070017bool completed{};
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +053018
19class GpioTest : public ::testing::Test
20{
Patrick Venturedace6802018-11-01 16:52:10 -070021 public:
22 static constexpr auto DEVICE = "/tmp/test_fifo";
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +053023
Patrick Venturedace6802018-11-01 16:52:10 -070024 // systemd event handler
25 sd_event* events;
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +053026
Patrick Venturedace6802018-11-01 16:52:10 -070027 // Really needed just for the constructor
28 decltype(input_event::code) code = 10;
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +053029
Patrick Venturedace6802018-11-01 16:52:10 -070030 // Really needed just for the constructor
31 decltype(input_event::value) value = 10;
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +053032
Patrick Venturedace6802018-11-01 16:52:10 -070033 // Need this so that events can be initialized.
34 int rc;
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +053035
Patrick Venturedace6802018-11-01 16:52:10 -070036 // Gets called as part of each TEST_F construction
37 GpioTest() : rc(sd_event_default(&events))
38 {
39 // Check for successful creation of event handler
40 EXPECT_GE(rc, 0);
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +053041
Patrick Venturedace6802018-11-01 16:52:10 -070042 // FIFO created to simulate data available
43 EXPECT_EQ(0, mknod(DEVICE, S_IFIFO | 0666, 0));
44 }
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +053045
Patrick Venturedace6802018-11-01 16:52:10 -070046 // Gets called as part of each TEST_F destruction
47 ~GpioTest()
48 {
49 EXPECT_EQ(0, remove(DEVICE));
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +053050
Patrick Venturedace6802018-11-01 16:52:10 -070051 events = sd_event_unref(events);
52 EXPECT_EQ(events, nullptr);
53 }
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +053054
Patrick Venturedace6802018-11-01 16:52:10 -070055 // Callback handler on data
56 static int callbackHandler(sd_event_source* es, int fd, uint32_t revents,
57 void* userData)
58 {
59 std::cout << "Event fired" << std::endl;
60 completed = true;
61 return 0;
62 }
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +053063};
64
65/** @brief Makes sure that event never comes for 3 seconds
66 */
67TEST_F(GpioTest, noEventIn3Seconds)
68{
69 using namespace std::chrono;
70
Patrick Venturedace6802018-11-01 16:52:10 -070071 phosphor::gpio::EventPtr eventP{events};
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +053072 events = nullptr;
73
74 const std::string emptyTarget = "";
Patrick Venturedace6802018-11-01 16:52:10 -070075 Monitor gpio(DEVICE, code, value, emptyTarget, eventP, false,
76 callbackHandler, false);
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +053077
78 // Waiting 3 seconds and check if the completion status is set
79 int count = 0;
Patrick Venturedace6802018-11-01 16:52:10 -070080 while (count < 3)
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +053081 {
82 // Returns -0- on timeout and positive number on dispatch
83 auto sleepTime = duration_cast<microseconds>(seconds(1));
Patrick Venturedace6802018-11-01 16:52:10 -070084 if (!sd_event_run(eventP.get(), sleepTime.count()))
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +053085 {
86 count++;
87 }
88 }
89 EXPECT_EQ(false, completed);
90
91 // 3 to cater to another uptick that happens prior to breaking.
92 EXPECT_EQ(3, count);
93}
94
95/** @brief Pump data in the middle and expect the callback to be invoked */
96TEST_F(GpioTest, pumpDataAndExpectCallBack)
97{
98 using namespace std::chrono;
99
Patrick Venturedace6802018-11-01 16:52:10 -0700100 phosphor::gpio::EventPtr eventP{events};
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +0530101 events = nullptr;
102
103 const std::string emptyTarget = "";
Patrick Venturedace6802018-11-01 16:52:10 -0700104 Monitor gpio(DEVICE, code, value, emptyTarget, eventP, false,
105 callbackHandler, false);
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +0530106
107 // Pump the data in the middle
108 int count = 0;
Patrick Venturedace6802018-11-01 16:52:10 -0700109 while (count < 2 && !completed)
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +0530110 {
111 if (count == 1)
112 {
113 auto pumpData = std::string("echo 'foo' > ") + DEVICE;
114 EXPECT_GE(0, system(pumpData.c_str()));
115 }
116
117 // Returns -0- on timeout
118 auto sleepTime = duration_cast<microseconds>(seconds(1));
Patrick Venturedace6802018-11-01 16:52:10 -0700119 if (!sd_event_run(eventP.get(), sleepTime.count()))
Vishwanatha Subbanna32c97272017-04-04 14:10:08 +0530120 {
121 count++;
122 }
123 }
124 EXPECT_EQ(true, completed);
125 EXPECT_EQ(1, count);
126}