summaryrefslogtreecommitdiffstats
path: root/test/utest.cpp
blob: c6b6f18bb35d0445bef3b4cac1d6306cab50e18c (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
#include <iostream>
#include <sys/types.h>
#include <chrono>
#include <string>
#include <linux/input.h>
#include <gtest/gtest.h>
#include "monitor.hpp"
#include "evdev.hpp"

using namespace phosphor::gpio;

// Exit helper. Ideally should be class but need
// this to be used inside a static method.
bool completed {};

class GpioTest : public ::testing::Test
{
    public:
        static constexpr auto DEVICE = "/tmp/test_fifo";

        // systemd event handler
        sd_event* events;

        // Really needed just for the constructor
        decltype(input_event::code) code = 10;

        // Really needed just for the constructor
        decltype(input_event::value) value = 10;

        // Need this so that events can be initialized.
        int rc;

        // Gets called as part of each TEST_F construction
        GpioTest()
              : rc(sd_event_default(&events))
        {
            // Check for successful creation of event handler
            EXPECT_GE(rc, 0);

            // FIFO created to simulate data available
            EXPECT_EQ(0, mknod(DEVICE, S_IFIFO|0666, 0));
        }

        // Gets called as part of each TEST_F destruction
        ~GpioTest()
        {
            EXPECT_EQ(0, remove(DEVICE));

            events = sd_event_unref(events);
            EXPECT_EQ(events, nullptr);
        }

        // Callback handler on data
        static int callbackHandler(sd_event_source* es, int fd,
                                   uint32_t revents, void* userData)
        {
            std::cout <<"Event fired" << std::endl;
            completed = true;
            return 0;
        }
};

/** @brief Makes sure that event never comes for 3 seconds
 */
TEST_F(GpioTest, noEventIn3Seconds)
{
    using namespace std::chrono;

    phosphor::gpio::EventPtr eventP { events };
    events = nullptr;

    const std::string emptyTarget = "";
    Monitor gpio(DEVICE, code, value, emptyTarget,
                 eventP, callbackHandler, false);

    // Waiting 3 seconds and check if the completion status is set
    int count = 0;
    while(count < 3)
    {
        // Returns -0- on timeout and positive number on dispatch
        auto sleepTime = duration_cast<microseconds>(seconds(1));
        if(!sd_event_run(eventP.get(), sleepTime.count()))
        {
            count++;
        }
    }
    EXPECT_EQ(false, completed);

    // 3 to cater to another uptick that happens prior to breaking.
    EXPECT_EQ(3, count);
}

/** @brief Pump data in the middle and expect the callback to be invoked */
TEST_F(GpioTest, pumpDataAndExpectCallBack)
{
    using namespace std::chrono;

    phosphor::gpio::EventPtr eventP { events };
    events = nullptr;

    const std::string emptyTarget = "";
    Monitor gpio(DEVICE, code, value, emptyTarget,
                 eventP, callbackHandler, false);

    // Pump the data in the middle
    int count = 0;
    while(count < 2 && !completed)
    {
        if (count == 1)
        {
            auto pumpData = std::string("echo 'foo' > ") + DEVICE;
            EXPECT_GE(0, system(pumpData.c_str()));
        }

        // Returns -0- on timeout
        auto sleepTime = duration_cast<microseconds>(seconds(1));
        if(!sd_event_run(eventP.get(), sleepTime.count()))
        {
            count++;
        }
    }
    EXPECT_EQ(true, completed);
    EXPECT_EQ(1, count);
}
OpenPOWER on IntegriCloud