From caa6aba20383c531216f4c4e46ef1d00f8e58d90 Mon Sep 17 00:00:00 2001 From: Davin McCall Date: Sun, 8 Jul 2018 20:02:51 +0100 Subject: [PATCH] tests: add c.p. test for list services command. --- src/tests/cptests/cptests.cc | 78 +++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 6 deletions(-) diff --git a/src/tests/cptests/cptests.cc b/src/tests/cptests/cptests.cc index 01dc9c6..00a8176 100644 --- a/src/tests/cptests/cptests.cc +++ b/src/tests/cptests/cptests.cc @@ -1,17 +1,17 @@ #include #include +#include +#include +#include #include "dinit.h" #include "service.h" #include "baseproc-sys.h" #include "control.h" -#define RUN_TEST(name) \ - std::cout << #name "... "; \ - name(); \ - std::cout << "PASSED" << std::endl; +// Control protocol tests. -void cptest1() +void cptest_queryver() { service_set sset; int fd = bp_sys::allocfd(); @@ -34,8 +34,74 @@ void cptest1() delete cc; } +void cptest_listservices() +{ + service_set sset; + + service_record *s1 = new service_record(&sset, "test-service-1", service_type_t::INTERNAL, {}); + sset.add_service(s1); + service_record *s2 = new service_record(&sset, "test-service-2", service_type_t::INTERNAL, {}); + sset.add_service(s2); + service_record *s3 = new service_record(&sset, "test-service-3", service_type_t::INTERNAL, {}); + sset.add_service(s3); + + int fd = bp_sys::allocfd(); + auto *cc = new control_conn_t(event_loop, &sset, fd); + + bp_sys::supply_read_data(fd, { DINIT_CP_LISTSERVICES }); + + event_loop.regd_bidi_watchers[fd]->read_ready(event_loop, fd); + + // Write will process immediately, so there's no need for this: + //event_loop.regd_bidi_watchers[fd]->write_ready(event_loop, fd); + + // We expect, for each service: + // (1 byte) DINIT_RP_SVCINFO + // (1 byte) service name length + // (1 byte) state + // (1 byte) target state + // (1 byte) flags: has console, waiting for console, start skipped + // (1 byte) stop reason + // (2 bytes) reserved + // (? bytes) exit status (int) / process id (pid_t) + // (N bytes) service name + + std::vector wdata; + bp_sys::extract_written_data(fd, wdata); + + std::set names = {"test-service-1", "test-service-2", "test-service-3"}; + + int pos = 0; + for (int i = 0; i < 3; i++) { + assert(wdata[pos++] == DINIT_RP_SVCINFO); + unsigned char name_len_c = wdata[pos++]; + pos += 6; + + pos += std::max(sizeof(int), sizeof(pid_t)); + + std::string name; + for (int j = 0; j < (int)name_len_c; j++) { + name += wdata[pos++]; + } + + // Check the service name matches one from the set, and remove it: + auto fn = names.find(name); + assert (fn != names.end()); + names.erase(fn); + } + + delete cc; +} + + +#define RUN_TEST(name, spacing) \ + std::cout << #name "..." spacing; \ + name(); \ + std::cout << "PASSED" << std::endl; + int main(int argc, char **argv) { - RUN_TEST(cptest1); + RUN_TEST(cptest_queryver, " "); + RUN_TEST(cptest_listservices, ""); return 0; } -- 2.25.1