Add initial control protocol test.
[oweals/dinit.git] / src / tests / cptests / cptests.cc
1 #include <cassert>
2 #include <iostream>
3
4 #include "dinit.h"
5 #include "service.h"
6 #include "baseproc-sys.h"
7 #include "control.h"
8
9 #define RUN_TEST(name) \
10     std::cout << #name "... "; \
11     name(); \
12     std::cout << "PASSED" << std::endl;
13
14 void cptest1()
15 {
16         service_set sset;
17         int fd = bp_sys::allocfd();
18         auto *cc = new control_conn_t(event_loop, &sset, fd);
19
20         bp_sys::supply_read_data(fd, { DINIT_CP_QUERYVERSION });
21
22         event_loop.regd_bidi_watchers[fd]->read_ready(event_loop, fd);
23
24         // Write will process immediately, so there's no need for this:
25         //event_loop.regd_bidi_watchers[fd]->write_ready(event_loop, fd);
26
27         // We expect a version number back:
28         std::vector<char> wdata;
29         bp_sys::extract_written_data(fd, wdata);
30
31         assert(wdata.size() == 5);
32         assert(wdata[0] == DINIT_RP_CPVERSION);
33
34         delete cc;
35 }
36
37 int main(int argc, char **argv)
38 {
39     RUN_TEST(cptest1);
40     return 0;
41 }