From 4da338ab75b5189ee7bba1b0429a26594f8ae9cc Mon Sep 17 00:00:00 2001 From: Davin McCall Date: Tue, 10 Jul 2018 17:37:13 +0100 Subject: [PATCH] tests: add control protocol test (start/stop service). --- src/tests/cptests/cptests.cc | 89 ++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/src/tests/cptests/cptests.cc b/src/tests/cptests/cptests.cc index 322f4aa..417fd06 100644 --- a/src/tests/cptests/cptests.cc +++ b/src/tests/cptests/cptests.cc @@ -323,6 +323,94 @@ void cptest_loadservice() delete cc; } +void cptest_startstop() +{ + service_set sset; + + const char * const service_name = "test-service-1"; + + service_record *s1 = new service_record(&sset, "test-service-1", service_type_t::INTERNAL, {}); + sset.add_service(s1); + + int fd = bp_sys::allocfd(); + auto *cc = new control_conn_t(event_loop, &sset, fd); + + // Get a service handle: + std::vector cmd = { DINIT_CP_FINDSERVICE }; + uint16_t name_len = strlen(service_name); + char *name_len_cptr = reinterpret_cast(&name_len); + cmd.insert(cmd.end(), name_len_cptr, name_len_cptr + sizeof(name_len)); + cmd.insert(cmd.end(), service_name, service_name + name_len); + + bp_sys::supply_read_data(fd, std::move(cmd)); + + event_loop.regd_bidi_watchers[fd]->read_ready(event_loop, fd); + + // We expect: + // (1 byte) DINIT_RP_SERVICERECORD + // (1 byte) state + // (handle_t) handle + // (1 byte) target state + + std::vector wdata; + bp_sys::extract_written_data(fd, wdata); + + assert(wdata.size() == 3 + sizeof(control_conn_t::handle_t)); + assert(wdata[0] == DINIT_RP_SERVICERECORD); + service_state_t s = static_cast(wdata[1]); + assert(s == service_state_t::STOPPED); + service_state_t ts = static_cast(wdata[6]); + assert(ts == service_state_t::STOPPED); + + control_conn_t::handle_t h; + std::copy(wdata.data() + 2, wdata.data() + 2 + sizeof(h), reinterpret_cast(&h)); + + // Issue start: + cmd = { DINIT_CP_STARTSERVICE, 0 /* don't pin */ }; + char * h_cp = reinterpret_cast(&h); + cmd.insert(cmd.end(), h_cp, h_cp + sizeof(h)); + + bp_sys::supply_read_data(fd, std::move(cmd)); + + event_loop.regd_bidi_watchers[fd]->read_ready(event_loop, fd); + + bp_sys::extract_written_data(fd, wdata); + assert(wdata.size() == 1 + 7 /* ACK reply + info packet */); + assert(wdata[0] == DINIT_IP_SERVICEEVENT); + // packetsize, key (handle), event + assert(wdata[1] == 7); + control_conn_t::handle_t ip_h; + std::copy(wdata.data() + 2, wdata.data() + 2 + sizeof(ip_h), reinterpret_cast(&ip_h)); + assert(ip_h == h); + assert(wdata[6] == static_cast(service_event_t::STARTED)); + // we get ALREADYSS since it started immediately: + assert(wdata[7] == DINIT_RP_ALREADYSS); + assert(s1->get_state() == service_state_t::STARTED); + + // Issue stop: + cmd = { DINIT_CP_STOPSERVICE, 0 /* don't pin */ }; + cmd.insert(cmd.end(), h_cp, h_cp + sizeof(h)); + + bp_sys::supply_read_data(fd, std::move(cmd)); + + event_loop.regd_bidi_watchers[fd]->read_ready(event_loop, fd); + + bp_sys::extract_written_data(fd, wdata); + assert(wdata.size() == 1 + 7); + assert(wdata[0] == DINIT_IP_SERVICEEVENT); + // packetsize, key (handle), event + assert(wdata[1] == 7); + std::copy(wdata.data() + 2, wdata.data() + 2 + sizeof(ip_h), reinterpret_cast(&ip_h)); + assert(ip_h == h); + assert(wdata[6] == static_cast(service_event_t::STOPPED)); + // we get ALREADYSS since it stopped immediately: + assert(wdata[7] == DINIT_RP_ALREADYSS); + assert(s1->get_state() == service_state_t::STOPPED); + + delete cc; +} + + #define RUN_TEST(name, spacing) \ std::cout << #name "..." spacing; \ name(); \ @@ -336,5 +424,6 @@ int main(int argc, char **argv) RUN_TEST(cptest_findservice2, ""); RUN_TEST(cptest_findservice3, ""); RUN_TEST(cptest_loadservice, " "); + RUN_TEST(cptest_startstop, " "); return 0; } -- 2.25.1