From 02341a42e32bbe29e8b04e48c1fd52b9c48cce51 Mon Sep 17 00:00:00 2001 From: Louis Opter Date: Tue, 17 Nov 2015 02:03:16 -0800 Subject: [PATCH] Finish refactoring tests around wire_proto.c This finishes what started the previous changeset: mock the wire_proto module and properly do unit testing. --- core/daemon.c | 2 +- docs/changelog.rst | 9 +++ lifx/gateway.c | 9 ++- tests/core/daemon/test_daemon_randuint32.c | 2 +- .../proto/test_proto_set_light_from_hsbk.c | 64 +++++++++++---- ...oto_set_light_from_hsbk_on_routing_error.c | 64 +++++++++++---- tests/core/proto/test_proto_set_waveform.c | 79 +++++++++++++------ ...test_proto_set_waveform_on_routing_error.c | 73 +++++++++++------ tests/core/proto/test_proto_tag_create.c | 48 +++++++++-- tests/core/proto/test_proto_tag_update.c | 62 ++++++++++++--- tests/core/proto/test_proto_untag.c | 21 ++++- .../test_gateway_update_tag_refcounts.c | 22 +++++- tests/lifx/mock_wire_proto.h | 12 ++- .../test_wire_proto_encode_light_color.c | 43 ++++++++++ .../test_wire_proto_encode_tag_labels.c | 29 +++++++ .../wire_proto/test_wire_proto_encode_tags.c | 23 ++++++ .../test_wire_proto_encode_waveform.c | 53 +++++++++++++ 17 files changed, 507 insertions(+), 108 deletions(-) create mode 100644 tests/lifx/wire_proto/test_wire_proto_encode_light_color.c create mode 100644 tests/lifx/wire_proto/test_wire_proto_encode_tag_labels.c create mode 100644 tests/lifx/wire_proto/test_wire_proto_encode_tags.c create mode 100644 tests/lifx/wire_proto/test_wire_proto_encode_waveform.c diff --git a/core/daemon.c b/core/daemon.c index ad9bb9d..3300536 100644 --- a/core/daemon.c +++ b/core/daemon.c @@ -414,7 +414,7 @@ lgtd_daemon_randuint32(void) close(fd); lgtd_err( 1, "couln't fetch %ju bytes from /dev/urandom", - sizeof((uintmax_t)rv) + (uintmax_t)sizeof(rv) ); } diff --git a/docs/changelog.rst b/docs/changelog.rst index a92346d..424b40e 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,15 @@ Changelog ========= +1.1.1 (2015-11-17) +------------------ + +- Greatly improve responsiveness by setting the LIFX source identifier [#]_. +- Fix parallel builds in the Debian package & fix the homebrew formulae for OS X + 10.11 (El Capitan). + +.. [#] http://lan.developer.lifx.com/docs/header-description#frame + 1.1.0 (2015-11-07) ------------------ diff --git a/lifx/gateway.c b/lifx/gateway.c index a25198c..f1a139b 100644 --- a/lifx/gateway.c +++ b/lifx/gateway.c @@ -427,7 +427,14 @@ lgtd_lifx_gateway_enqueue_packet(struct lgtd_lifx_gateway *gw, evbuffer_add(gw->write_buf, hdr, sizeof(*hdr)); if (pkt) { - assert(pkt_info->size == le16toh(hdr->size) - sizeof(*hdr)); +#ifndef NDEBUG + // actually decode the header instead of just calling + // le16toh(hdr->size) so it's easier to mock things in the tests: + struct lgtd_lifx_packet_header decoded_hdr; + memcpy(&decoded_hdr, hdr, sizeof(decoded_hdr)); + lgtd_lifx_wire_decode_header(&decoded_hdr); + assert(pkt_info->size == decoded_hdr.size - sizeof(*hdr)); +#endif evbuffer_add(gw->write_buf, pkt, pkt_info->size); } gw->pkt_ring[gw->pkt_ring_head].size = sizeof(*hdr) + pkt_info->size; diff --git a/tests/core/daemon/test_daemon_randuint32.c b/tests/core/daemon/test_daemon_randuint32.c index ea639e3..49c57ab 100644 --- a/tests/core/daemon/test_daemon_randuint32.c +++ b/tests/core/daemon/test_daemon_randuint32.c @@ -16,7 +16,7 @@ int mock_close(int); #include "mock_log.h" #include "mock_timer.h" -static const int MOCK_RANDOM_NUMBER = 0x72616e64; +static const uint32_t MOCK_RANDOM_NUMBER = 0x72616e64; int mock_open_call_count = 0; diff --git a/tests/core/proto/test_proto_set_light_from_hsbk.c b/tests/core/proto/test_proto_set_light_from_hsbk.c index 0b39393..8af1992 100644 --- a/tests/core/proto/test_proto_set_light_from_hsbk.c +++ b/tests/core/proto/test_proto_set_light_from_hsbk.c @@ -7,6 +7,7 @@ #include "mock_event2.h" #include "mock_log.h" #include "mock_timer.h" +#define MOCKED_LGTD_LIFX_WIRE_ENCODE_LIGHT_COLOR #include "mock_wire_proto.h" #include "tests_utils.h" @@ -14,6 +15,18 @@ #define MOCKED_ROUTER_SEND #include "tests_proto_utils.h" +int lifx_wire_encode_light_color_call_count = 0; + +void +lgtd_lifx_wire_encode_light_color(struct lgtd_lifx_packet_light_color *pkt) +{ + (void)pkt; + + lifx_wire_encode_light_color_call_count++; +} + +int router_send_call_count = 0; + bool lgtd_router_send(const struct lgtd_proto_target_list *targets, enum lgtd_lifx_packet_type pkt_type, @@ -34,31 +47,35 @@ lgtd_router_send(const struct lgtd_proto_target_list *targets, } struct lgtd_lifx_packet_light_color *light_color = pkt; - int hue = le16toh(light_color->hue); - int saturation = le16toh(light_color->saturation); - int brightness = le16toh(light_color->brightness); - int kelvin = le16toh(light_color->kelvin); - int transition = htole32(light_color->transition); - - if (hue != 42) { - errx(1, "got hue = %d (expected 42)", hue); + if (lifx_wire_encode_light_color_call_count != 1) { + errx( + 1, "lifx_wire_encode_light_color_call_count = %d (expected 1)", + lifx_wire_encode_light_color_call_count + ); + } + if (light_color->hue != 42) { + errx(1, "got hue = %d (expected 42)", light_color->hue); } - if (saturation != 10000) { - errx(1, "got saturation = %d (expected 10000)", saturation); + if (light_color->saturation != 10000) { + errx(1, "got saturation = %d (expected 10000)", light_color->saturation); } - if (brightness != 20000) { - errx(1, "got brightness = %d (expected 20000)", brightness); + if (light_color->brightness != 20000) { + errx(1, "got brightness = %d (expected 20000)", light_color->brightness); } - if (kelvin != 4500) { - errx(1, "got kelvin = %d (expected 4500)", kelvin); + if (light_color->kelvin != 4500) { + errx(1, "got kelvin = %d (expected 4500)", light_color->kelvin); } - if (transition != 150) { - errx(1, "got transition = %d (expected 150)", transition); + if (light_color->transition != 150) { + errx(1, "got transition = %d (expected 150)", light_color->transition); } + router_send_call_count++; + return true; } +int client_send_response_call_count = 0; + void lgtd_client_send_response(struct lgtd_client *client, const char *msg) { @@ -69,6 +86,8 @@ lgtd_client_send_response(struct lgtd_client *client, const char *msg) if (strcmp(msg, "true")) { errx(1, "unexpected response [%s] (expected [true])", msg); } + + client_send_response_call_count++; } int @@ -83,5 +102,18 @@ main(void) &client, targets, 42, 10000, 20000, 4500, 150 ); + if (router_send_call_count != 1) { + errx( + 1, "router_send_call_count = %d (expected 1)", + router_send_call_count + ); + } + if (client_send_response_call_count != 1) { + errx( + 1, "client_send_response_call_count = %d (expected 1)", + client_send_response_call_count + ); + } + return 0; } diff --git a/tests/core/proto/test_proto_set_light_from_hsbk_on_routing_error.c b/tests/core/proto/test_proto_set_light_from_hsbk_on_routing_error.c index 05538ef..1d7d230 100644 --- a/tests/core/proto/test_proto_set_light_from_hsbk_on_routing_error.c +++ b/tests/core/proto/test_proto_set_light_from_hsbk_on_routing_error.c @@ -7,6 +7,7 @@ #include "mock_event2.h" #include "mock_log.h" #include "mock_timer.h" +#define MOCKED_LGTD_LIFX_WIRE_ENCODE_LIGHT_COLOR #include "mock_wire_proto.h" #include "tests_utils.h" @@ -14,6 +15,18 @@ #define MOCKED_ROUTER_SEND #include "tests_proto_utils.h" +int lifx_wire_encode_light_color_call_count = 0; + +void +lgtd_lifx_wire_encode_light_color(struct lgtd_lifx_packet_light_color *pkt) +{ + (void)pkt; + + lifx_wire_encode_light_color_call_count++; +} + +int router_send_call_count = 0; + bool lgtd_router_send(const struct lgtd_proto_target_list *targets, enum lgtd_lifx_packet_type pkt_type, @@ -34,31 +47,35 @@ lgtd_router_send(const struct lgtd_proto_target_list *targets, } struct lgtd_lifx_packet_light_color *light_color = pkt; - int hue = le16toh(light_color->hue); - int saturation = le16toh(light_color->saturation); - int brightness = le16toh(light_color->brightness); - int kelvin = le16toh(light_color->kelvin); - int transition = htole32(light_color->transition); - - if (hue != 42) { - errx(1, "got hue = %d (expected 42)", hue); + if (lifx_wire_encode_light_color_call_count != 1) { + errx( + 1, "lifx_wire_encode_light_color_call_count = %d (expected 1)", + lifx_wire_encode_light_color_call_count + ); + } + if (light_color->hue != 42) { + errx(1, "got hue = %d (expected 42)", light_color->hue); } - if (saturation != 10000) { - errx(1, "got saturation = %d (expected 10000)", saturation); + if (light_color->saturation != 10000) { + errx(1, "got saturation = %d (expected 10000)", light_color->saturation); } - if (brightness != 20000) { - errx(1, "got brightness = %d (expected 20000)", brightness); + if (light_color->brightness != 20000) { + errx(1, "got brightness = %d (expected 20000)", light_color->brightness); } - if (kelvin != 4500) { - errx(1, "got kelvin = %d (expected 4500)", kelvin); + if (light_color->kelvin != 4500) { + errx(1, "got kelvin = %d (expected 4500)", light_color->kelvin); } - if (transition != 150) { - errx(1, "got transition = %d (expected 150)", transition); + if (light_color->transition != 150) { + errx(1, "got transition = %d (expected 150)", light_color->transition); } + router_send_call_count++; + return false; } +int client_send_response_call_count = 0; + void lgtd_client_send_response(struct lgtd_client *client, const char *msg) { @@ -69,6 +86,8 @@ lgtd_client_send_response(struct lgtd_client *client, const char *msg) if (strcmp(msg, "false")) { errx(1, "unexpected response [%s] (expected [false])", msg); } + + client_send_response_call_count++; } int @@ -83,5 +102,18 @@ main(void) &client, targets, 42, 10000, 20000, 4500, 150 ); + if (router_send_call_count != 1) { + errx( + 1, "router_send_call_count = %d (expected 1)", + router_send_call_count + ); + } + if (client_send_response_call_count != 1) { + errx( + 1, "client_send_response_call_count = %d (expected 1)", + client_send_response_call_count + ); + } + return 0; } diff --git a/tests/core/proto/test_proto_set_waveform.c b/tests/core/proto/test_proto_set_waveform.c index bca4879..9132245 100644 --- a/tests/core/proto/test_proto_set_waveform.c +++ b/tests/core/proto/test_proto_set_waveform.c @@ -7,6 +7,7 @@ #include "mock_event2.h" #include "mock_log.h" #include "mock_timer.h" +#define MOCKED_LGTD_LIFX_WIRE_ENCODE_WAVEFORM #include "mock_wire_proto.h" #include "tests_utils.h" @@ -14,6 +15,18 @@ #define MOCKED_ROUTER_SEND #include "tests_proto_utils.h" +int lifx_wire_encode_waveform_call_count = 0; + +void +lgtd_lifx_wire_encode_waveform(struct lgtd_lifx_packet_waveform *pkt) +{ + (void)pkt; + + lifx_wire_encode_waveform_call_count++; +} + +int router_send_call_count = 0; + bool lgtd_router_send(const struct lgtd_proto_target_list *targets, enum lgtd_lifx_packet_type pkt_type, @@ -34,46 +47,47 @@ lgtd_router_send(const struct lgtd_proto_target_list *targets, } struct lgtd_lifx_packet_waveform *waveform = pkt; - enum lgtd_lifx_waveform_type waveform_type = waveform->waveform; - int hue = le16toh(waveform->hue); - int saturation = le16toh(waveform->saturation); - int brightness = le16toh(waveform->brightness); - int kelvin = le16toh(waveform->kelvin); - int period = le32toh(waveform->period); - float cycles = lgtd_lifx_wire_lefloattoh(waveform->cycles); - int skew_ratio = le16toh(waveform->skew_ratio); - - if (waveform_type != LGTD_LIFX_WAVEFORM_SAW) { + if (lifx_wire_encode_waveform_call_count != 1) { + errx( + 1, "lifx_wire_encode_waveform_call_count = %d (expected 1)", + lifx_wire_encode_waveform_call_count + ); + } + if (waveform->waveform != LGTD_LIFX_WAVEFORM_SAW) { errx( 1, "got waveform = %d (expected %d)", - waveform_type, LGTD_LIFX_WAVEFORM_SAW + waveform->waveform, LGTD_LIFX_WAVEFORM_SAW ); } - if (hue != 42) { - errx(1, "got hue = %d (expected 42)", hue); + if (waveform->hue != 42) { + errx(1, "got hue = %d (expected 42)", waveform->hue); } - if (saturation != 10000) { - errx(1, "got saturation = %d (expected 10000)", saturation); + if (waveform->saturation != 10000) { + errx(1, "got saturation = %d (expected 10000)", waveform->saturation); } - if (brightness != 20000) { - errx(1, "got brightness = %d (expected 20000)", brightness); + if (waveform->brightness != 20000) { + errx(1, "got brightness = %d (expected 20000)", waveform->brightness); } - if (kelvin != 4500) { - errx(1, "got kelvin = %d (expected 4500)", kelvin); + if (waveform->kelvin != 4500) { + errx(1, "got kelvin = %d (expected 4500)", waveform->kelvin); } - if (period != 200) { - errx(1, "got period = %d (expected 200)", period); + if (waveform->period != 200) { + errx(1, "got period = %d (expected 200)", waveform->period); } - if (cycles != 10.) { - errx(1, "got cycles = %f (expected 10)", cycles); + if (waveform->cycles != 10.) { + errx(1, "got cycles = %f (expected 10)", waveform->cycles); } - if (skew_ratio != 0) { - errx(1, "got skew_ratio = %d (expected 0)", skew_ratio); + if (waveform->skew_ratio != 0) { + errx(1, "got skew_ratio = %d (expected 0)", waveform->skew_ratio); } + router_send_call_count++; + return true; } +int client_send_response_call_count = 0; + void lgtd_client_send_response(struct lgtd_client *client, const char *msg) { @@ -84,6 +98,8 @@ lgtd_client_send_response(struct lgtd_client *client, const char *msg) if (strcmp(msg, "true")) { errx(1, "unexpected response [%s] (expected [true])", msg); } + + client_send_response_call_count++; } int @@ -99,5 +115,18 @@ main(void) 42, 10000, 20000, 4500, 200, 10., 0, false ); + if (router_send_call_count != 1) { + errx( + 1, "router_send_call_count = %d (expected 1)", + router_send_call_count + ); + } + if (client_send_response_call_count != 1) { + errx( + 1, "client_send_response_call_count = %d (expected 1)", + client_send_response_call_count + ); + } + return 0; } diff --git a/tests/core/proto/test_proto_set_waveform_on_routing_error.c b/tests/core/proto/test_proto_set_waveform_on_routing_error.c index 9d4350f..ddfc91a 100644 --- a/tests/core/proto/test_proto_set_waveform_on_routing_error.c +++ b/tests/core/proto/test_proto_set_waveform_on_routing_error.c @@ -7,6 +7,7 @@ #include "mock_event2.h" #include "mock_log.h" #include "mock_timer.h" +#define MOCKED_LGTD_LIFX_WIRE_ENCODE_WAVEFORM #include "mock_wire_proto.h" #include "tests_utils.h" @@ -14,6 +15,18 @@ #define MOCKED_ROUTER_SEND #include "tests_proto_utils.h" +int lifx_wire_encode_waveform_call_count = 0; + +void +lgtd_lifx_wire_encode_waveform(struct lgtd_lifx_packet_waveform *pkt) +{ + (void)pkt; + + lifx_wire_encode_waveform_call_count++; +} + +int router_send_call_count = 0; + bool lgtd_router_send(const struct lgtd_proto_target_list *targets, enum lgtd_lifx_packet_type pkt_type, @@ -34,46 +47,41 @@ lgtd_router_send(const struct lgtd_proto_target_list *targets, } struct lgtd_lifx_packet_waveform *waveform = pkt; - enum lgtd_lifx_waveform_type waveform_type = waveform->waveform; - int hue = le16toh(waveform->hue); - int saturation = le16toh(waveform->saturation); - int brightness = le16toh(waveform->brightness); - int kelvin = le16toh(waveform->kelvin); - int period = le32toh(waveform->period); - float cycles = lgtd_lifx_wire_lefloattoh(waveform->cycles); - int skew_ratio = le16toh(waveform->skew_ratio); - - if (waveform_type != LGTD_LIFX_WAVEFORM_SAW) { + if (waveform->waveform != LGTD_LIFX_WAVEFORM_SAW) { errx( 1, "got waveform = %d (expected %d)", - waveform_type, LGTD_LIFX_WAVEFORM_SAW + waveform->waveform, LGTD_LIFX_WAVEFORM_SAW ); } - if (hue != 42) { - errx(1, "got hue = %d (expected 42)", hue); + if (waveform->hue != 42) { + errx(1, "got hue = %d (expected 42)", waveform->hue); } - if (saturation != 10000) { - errx(1, "got saturation = %d (expected 10000)", saturation); + if (waveform->saturation != 10000) { + errx(1, "got saturation = %d (expected 10000)", waveform->saturation); } - if (brightness != 20000) { - errx(1, "got brightness = %d (expected 20000)", brightness); + if (waveform->brightness != 20000) { + errx(1, "got brightness = %d (expected 20000)", waveform->brightness); } - if (kelvin != 4500) { - errx(1, "got kelvin = %d (expected 4500)", kelvin); + if (waveform->kelvin != 4500) { + errx(1, "got kelvin = %d (expected 4500)", waveform->kelvin); } - if (period != 200) { - errx(1, "got period = %d (expected 200)", period); + if (waveform->period != 200) { + errx(1, "got period = %d (expected 200)", waveform->period); } - if (cycles != 10.) { - errx(1, "got cycles = %f (expected 10)", cycles); + if (waveform->cycles != 10.) { + errx(1, "got cycles = %f (expected 10)", waveform->cycles); } - if (skew_ratio != 0) { - errx(1, "got skew_ratio = %d (expected 0)", skew_ratio); + if (waveform->skew_ratio != 0) { + errx(1, "got skew_ratio = %d (expected 0)", waveform->skew_ratio); } + router_send_call_count++; + return false; } +int client_send_response_call_count = 0; + void lgtd_client_send_response(struct lgtd_client *client, const char *msg) { @@ -84,6 +92,8 @@ lgtd_client_send_response(struct lgtd_client *client, const char *msg) if (strcmp(msg, "false")) { errx(1, "unexpected response [%s] (expected [false])", msg); } + + client_send_response_call_count++; } int @@ -99,5 +109,18 @@ main(void) 42, 10000, 20000, 4500, 200, 10., 0, false ); + if (router_send_call_count != 1) { + errx( + 1, "router_send_call_count = %d (expected 1)", + router_send_call_count + ); + } + if (client_send_response_call_count != 1) { + errx( + 1, "client_send_response_call_count = %d (expected 1)", + client_send_response_call_count + ); + } + return 0; } diff --git a/tests/core/proto/test_proto_tag_create.c b/tests/core/proto/test_proto_tag_create.c index fc8ca88..34c0d9d 100644 --- a/tests/core/proto/test_proto_tag_create.c +++ b/tests/core/proto/test_proto_tag_create.c @@ -8,6 +8,8 @@ #include "mock_event2.h" #include "mock_log.h" #include "mock_timer.h" +#define MOCKED_LGTD_LIFX_WIRE_ENCODE_TAG_LABELS +#define MOCKED_LGTD_LIFX_WIRE_ENCODE_TAGS #include "mock_wire_proto.h" #include "tests_utils.h" @@ -23,6 +25,26 @@ static struct lgtd_router_device_list devices = static struct lgtd_router_device_list device_1_only = SLIST_HEAD_INITIALIZER(&device_1_only); +static int lifx_wire_encode_tag_labels_call_count = 0; + +void +lgtd_lifx_wire_encode_tag_labels(struct lgtd_lifx_packet_tag_labels *pkt) +{ + (void)pkt; + + lifx_wire_encode_tag_labels_call_count++; +} + +static int lifx_wire_encode_tags_call_count = 0; + +void +lgtd_lifx_wire_encode_tags(struct lgtd_lifx_packet_tags *pkt) +{ + (void)pkt; + + lifx_wire_encode_tags_call_count++; +} + static bool send_to_device_called = false; void @@ -55,11 +77,16 @@ lgtd_router_send_to_device(struct lgtd_lifx_bulb *bulb, } const struct lgtd_lifx_packet_tags *pkt_tags = pkt; - uint64_t tags = le64toh(pkt_tags->tags); - if (tags != 0x1) { + if (lifx_wire_encode_tags_call_count != 1) { + errx( + 1, "lifx_wire_encode_tags_call_count = %d (expected 1)", + lifx_wire_encode_tags_call_count + ); + } + if (pkt_tags->tags != 0x1) { errx( 1, "invalid SET_TAGS payload=%#jx (expected %#x)", - (uintmax_t)tags, 0x1 + (uintmax_t)pkt_tags->tags, 0x1 ); } @@ -85,11 +112,18 @@ lgtd_lifx_gateway_send_to_site(struct lgtd_lifx_gateway *gw, } const struct lgtd_lifx_packet_tag_labels *pkt_tag_labels = pkt; - uint64_t tags = le64toh(pkt_tag_labels->tags); - if (tags != 0x1) { - errx(1, "got tags %#jx (expected %#x)", (uintmax_t)tags, 0x1); + if (lifx_wire_encode_tag_labels_call_count != 1) { + errx( + 1, "lifx_wire_encode_tag_labels_call_count = %d (expected 1)", + lifx_wire_encode_tag_labels_call_count + ); + } + if (pkt_tag_labels->tags != 0x1) { + errx( + 1, "got tags %#jx (expected %#x)", + (uintmax_t)pkt_tag_labels->tags, 0x1 + ); } - if (strcmp(pkt_tag_labels->label, "dub")) { errx(1, "got label %s (expected dub)", pkt_tag_labels->label); } diff --git a/tests/core/proto/test_proto_tag_update.c b/tests/core/proto/test_proto_tag_update.c index 4de4ef9..c6f09aa 100644 --- a/tests/core/proto/test_proto_tag_update.c +++ b/tests/core/proto/test_proto_tag_update.c @@ -8,6 +8,8 @@ #include "mock_event2.h" #include "mock_log.h" #include "mock_timer.h" +#define MOCKED_LGTD_LIFX_WIRE_ENCODE_TAG_LABELS +#define MOCKED_LGTD_LIFX_WIRE_ENCODE_TAGS #include "mock_wire_proto.h" #include "tests_utils.h" @@ -21,6 +23,26 @@ static struct lgtd_router_device_list devices = SLIST_HEAD_INITIALIZER(&devices); +static int lifx_wire_encode_tag_labels_call_count = 0; + +void +lgtd_lifx_wire_encode_tag_labels(struct lgtd_lifx_packet_tag_labels *pkt) +{ + (void)pkt; + + lifx_wire_encode_tag_labels_call_count++; +} + +static int lifx_wire_encode_tags_call_count = 0; + +void +lgtd_lifx_wire_encode_tags(struct lgtd_lifx_packet_tags *pkt) +{ + (void)pkt; + + lifx_wire_encode_tags_call_count++; +} + static bool send_to_device_called = false; void @@ -57,12 +79,16 @@ lgtd_router_send_to_device(struct lgtd_lifx_bulb *bulb, } const struct lgtd_lifx_packet_tags *pkt_tags = pkt; - uint64_t tags = le64toh(pkt_tags->tags); - - if (tags != 0x7) { + if (lifx_wire_encode_tags_call_count != 1) { + errx( + 1, "lifx_wire_encode_tags_call_count = %d (expected 1)", + lifx_wire_encode_tags_call_count + ); + } + if (pkt_tags->tags != 0x7) { errx( 1, "invalid SET_TAGS payload=%#jx (expected %#x)", - (uintmax_t)tags, 0x7 + (uintmax_t)pkt_tags->tags, 0x7 ); } @@ -89,23 +115,28 @@ lgtd_lifx_gateway_send_to_site(struct lgtd_lifx_gateway *gw, } const struct lgtd_lifx_packet_tag_labels *pkt_tag_labels = pkt; - uint64_t tags = le64toh(pkt_tag_labels->tags); if (strcmp(pkt_tag_labels->label, "dub")) { errx(1, "got label %s (expected dub)", pkt_tag_labels->label); } if (gw->site.as_integer == 42) { - if (tags != 0x1) { - errx(1, "got tags %#jx (expected %#x)", (uintmax_t)tags, 0x1); + if (pkt_tag_labels->tags != 0x1) { + errx( + 1, "got tags %#jx (expected %#x)", + (uintmax_t)pkt_tag_labels->tags, 0x1 + ); } if (gateway_send_to_site_called_for_gw_1) { errx(1, "LGTD_LIFX_SET_TAG_LABELS already called for gw 1"); } gateway_send_to_site_called_for_gw_1 = true; } else if (gw->site.as_integer == 44) { - if (tags != 0x4) { - errx(1, "got tags %#jx (expected %#x)", (uintmax_t)tags, 0x4); + if (pkt_tag_labels->tags != 0x4) { + errx( + 1, "got tags %#jx (expected %#x)", + (uintmax_t)pkt_tag_labels->tags, 0x4 + ); } if (gateway_send_to_site_called_for_gw_2) { errx(1, "LGTD_LIFX_SET_TAG_LABELS already called for gw 2"); @@ -115,6 +146,19 @@ lgtd_lifx_gateway_send_to_site(struct lgtd_lifx_gateway *gw, errx(1, "LGTD_LIFX_SET_TAG_LABELS received an invalid gateway"); } + int expected_tag_encode_tag_labels_call_count = ( + (int)(gateway_send_to_site_called_for_gw_1 == true) + + (int)(gateway_send_to_site_called_for_gw_2 == true) + ); + if (lifx_wire_encode_tag_labels_call_count + != expected_tag_encode_tag_labels_call_count) { + errx( + 1, "lifx_wire_encode_tag_labels_call_count = %d (expected %d)", + lifx_wire_encode_tag_labels_call_count, + expected_tag_encode_tag_labels_call_count + ); + } + return true; } diff --git a/tests/core/proto/test_proto_untag.c b/tests/core/proto/test_proto_untag.c index 38e2df5..2077aaa 100644 --- a/tests/core/proto/test_proto_untag.c +++ b/tests/core/proto/test_proto_untag.c @@ -6,6 +6,7 @@ #include "mock_event2.h" #include "mock_log.h" #include "mock_timer.h" +#define MOCKED_LGTD_LIFX_WIRE_ENCODE_TAGS #include "mock_wire_proto.h" #include "tests_utils.h" @@ -16,6 +17,16 @@ static bool device_list_free_called = false; +static int lifx_wire_encode_tags_call_count = 0; + +void +lgtd_lifx_wire_encode_tags(struct lgtd_lifx_packet_tags *pkt) +{ + (void)pkt; + + lifx_wire_encode_tags_call_count++; +} + void lgtd_router_device_list_free(struct lgtd_router_device_list *devices) { @@ -125,10 +136,16 @@ lgtd_router_send_to_device(struct lgtd_lifx_bulb *bulb, } struct lgtd_lifx_packet_tags *pkt_tags = pkt; - if (le64toh(pkt_tags->tags) != 0x2) { + if (lifx_wire_encode_tags_call_count != 1) { + errx( + 1, "lifx_wire_encode_tags_call_count = %d (expected 1)", + lifx_wire_encode_tags_call_count + ); + } + if (pkt_tags->tags != 0x2) { errx( 1, "invalid SET_TAGS payload=%#jx (expected %#x)", - (uintmax_t)le64toh(pkt_tags->tags), 0x2 + (uintmax_t)pkt_tags->tags, 0x2 ); } diff --git a/tests/lifx/gateway/test_gateway_update_tag_refcounts.c b/tests/lifx/gateway/test_gateway_update_tag_refcounts.c index 3929a2a..ac0d38c 100644 --- a/tests/lifx/gateway/test_gateway_update_tag_refcounts.c +++ b/tests/lifx/gateway/test_gateway_update_tag_refcounts.c @@ -3,8 +3,19 @@ #include "test_gateway_utils.h" #include "mock_log.h" #include "mock_timer.h" +#define MOCKED_LGTD_LIFX_WIRE_ENCODE_TAG_LABELS #include "mock_wire_proto.h" +static int lifx_wire_encode_tag_labels_call_count = 0; + +void +lgtd_lifx_wire_encode_tag_labels(struct lgtd_lifx_packet_tag_labels *pkt) +{ + (void)pkt; + + lifx_wire_encode_tag_labels_call_count++; +} + int main(void) { @@ -73,11 +84,16 @@ main(void) struct lgtd_lifx_packet_tag_labels *pkt = (void *)&gw_write_buf[sizeof(struct lgtd_lifx_packet_header)]; - uint64_t tags = le64toh(pkt->tags); - if (tags != ~2ULL) { + if (lifx_wire_encode_tag_labels_call_count != 1) { + errx( + 1, "lifx_wire_encode_tag_labels_call_count == %d (expected 1)", + lifx_wire_encode_tag_labels_call_count + ); + } + if (pkt->tags != ~2ULL) { errx( 1, "tags on LGTD_LIFX_SET_TAG_LABELS was %#jx (expected %#jx)", - (uintmax_t)tags, (uintmax_t)~2ULL + (uintmax_t)pkt->tags, (uintmax_t)~2ULL ); } const char blank_label[LGTD_LIFX_LABEL_SIZE] = { 0 }; diff --git a/tests/lifx/mock_wire_proto.h b/tests/lifx/mock_wire_proto.h index 91213a1..a0da12b 100644 --- a/tests/lifx/mock_wire_proto.h +++ b/tests/lifx/mock_wire_proto.h @@ -353,11 +353,19 @@ lgtd_lifx_wire_setup_header(struct lgtd_lifx_packet_header *hdr, const uint8_t *site, enum lgtd_lifx_packet_type packet_type) { - (void)hdr; (void)target_type; (void)target; (void)site; - return lgtd_lifx_wire_get_packet_info(packet_type); + + const struct lgtd_lifx_packet_info *pkt_info = + lgtd_lifx_wire_get_packet_info(packet_type); + hdr->size = pkt_info->size + sizeof(*hdr); + hdr->packet_type = packet_type; + if (site) { + memcpy(hdr->site, site, sizeof(hdr->site)); + } + + return pkt_info; } #endif diff --git a/tests/lifx/wire_proto/test_wire_proto_encode_light_color.c b/tests/lifx/wire_proto/test_wire_proto_encode_light_color.c new file mode 100644 index 0000000..4239e6d --- /dev/null +++ b/tests/lifx/wire_proto/test_wire_proto_encode_light_color.c @@ -0,0 +1,43 @@ +#include + +#include "wire_proto.c" +#include "mock_daemon.h" +#include "mock_gateway.h" +#include "mock_log.h" + +int +main(void) +{ + struct lgtd_lifx_packet_light_color pkt = { + .hue = 42, + .saturation = 10000, + .brightness = 20000, + .kelvin = 4500, + .transition = 150 + }; + + lgtd_lifx_wire_encode_light_color(&pkt); + + int hue = le16toh(pkt.hue); + int saturation = le16toh(pkt.saturation); + int brightness = le16toh(pkt.brightness); + int kelvin = le16toh(pkt.kelvin); + int transition = htole32(pkt.transition); + if (hue != 42) { + errx(1, "got hue = %d (expected 42)", hue); + } + if (saturation != 10000) { + errx(1, "got saturation = %d (expected 10000)", saturation); + } + if (brightness != 20000) { + errx(1, "got brightness = %d (expected 20000)", brightness); + } + if (kelvin != 4500) { + errx(1, "got kelvin = %d (expected 4500)", kelvin); + } + if (transition != 150) { + errx(1, "got transition = %d (expected 150)", transition); + } + + return 0; +} diff --git a/tests/lifx/wire_proto/test_wire_proto_encode_tag_labels.c b/tests/lifx/wire_proto/test_wire_proto_encode_tag_labels.c new file mode 100644 index 0000000..a53451b --- /dev/null +++ b/tests/lifx/wire_proto/test_wire_proto_encode_tag_labels.c @@ -0,0 +1,29 @@ +#include + +#include "wire_proto.c" +#include "mock_daemon.h" +#include "mock_gateway.h" +#include "mock_log.h" + +int +main(void) +{ + struct lgtd_lifx_packet_tag_labels pkt = { .tags = 0x2a, .label = "42" }; + + lgtd_lifx_wire_encode_tag_labels(&pkt); + + if (pkt.tags != le64toh(0x2a)) { + errx( + 1, "got tags = %#jx (expected %#jx)", + (uintmax_t)pkt.tags, (uintmax_t)le64toh(0x2a) + ); + } + if (strcmp(pkt.label, "42")) { + errx( + 1, "got label = %.*s (expected 42)", + (int)sizeof(pkt.label), pkt.label + ); + } + + return 0; +} diff --git a/tests/lifx/wire_proto/test_wire_proto_encode_tags.c b/tests/lifx/wire_proto/test_wire_proto_encode_tags.c new file mode 100644 index 0000000..6d979f5 --- /dev/null +++ b/tests/lifx/wire_proto/test_wire_proto_encode_tags.c @@ -0,0 +1,23 @@ +#include + +#include "wire_proto.c" +#include "mock_daemon.h" +#include "mock_gateway.h" +#include "mock_log.h" + +int +main(void) +{ + struct lgtd_lifx_packet_tags pkt = { .tags = 0x2a }; + + lgtd_lifx_wire_encode_tags(&pkt); + + if (pkt.tags != le64toh(0x2a)) { + errx( + 1, "got tags = %#jx (expected 0x2a)", + (uintmax_t)le64toh(pkt.tags) + ); + } + + return 0; +} diff --git a/tests/lifx/wire_proto/test_wire_proto_encode_waveform.c b/tests/lifx/wire_proto/test_wire_proto_encode_waveform.c new file mode 100644 index 0000000..495ac89 --- /dev/null +++ b/tests/lifx/wire_proto/test_wire_proto_encode_waveform.c @@ -0,0 +1,53 @@ +#include + +#include "wire_proto.c" +#include "mock_daemon.h" +#include "mock_gateway.h" +#include "mock_log.h" + +int +main(void) +{ + struct lgtd_lifx_packet_waveform pkt = { + .hue = 42, + .saturation = 10000, + .brightness = 20000, + .kelvin = 4500, + .period = 200, + .cycles = 10., + .skew_ratio = 5000 + }; + + lgtd_lifx_wire_encode_waveform(&pkt); + + int hue = le16toh(pkt.hue); + int saturation = le16toh(pkt.saturation); + int brightness = le16toh(pkt.brightness); + int kelvin = le16toh(pkt.kelvin); + int period = le32toh(pkt.period); + float cycles = lgtd_lifx_wire_lefloattoh(pkt.cycles); + int skew_ratio = le16toh(pkt.skew_ratio); + if (hue != 42) { + errx(1, "got hue = %d (expected 42)", hue); + } + if (saturation != 10000) { + errx(1, "got saturation = %d (expected 10000)", saturation); + } + if (brightness != 20000) { + errx(1, "got brightness = %d (expected 20000)", brightness); + } + if (kelvin != 4500) { + errx(1, "got kelvin = %d (expected 4500)", kelvin); + } + if (period != 200) { + errx(1, "got period = %d (expected 200)", period); + } + if (cycles != 10.) { + errx(1, "got cycles = %f (expected 10)", cycles); + } + if (skew_ratio != 5000) { + errx(1, "got skew_ratio = %d (expected 5000)", skew_ratio); + } + + return 0; +}