Skip to content

Commit

Permalink
Finish refactoring tests around wire_proto.c
Browse files Browse the repository at this point in the history
This finishes what started the previous changeset: mock the wire_proto
module and properly do unit testing.
  • Loading branch information
lopter committed Nov 17, 2015
1 parent 8499995 commit 02341a4
Show file tree
Hide file tree
Showing 17 changed files with 507 additions and 108 deletions.
2 changes: 1 addition & 1 deletion core/daemon.c
Expand Up @@ -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)
);
}

Expand Down
9 changes: 9 additions & 0 deletions 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)
------------------

Expand Down
9 changes: 8 additions & 1 deletion lifx/gateway.c
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/core/daemon/test_daemon_randuint32.c
Expand Up @@ -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;

Expand Down
64 changes: 48 additions & 16 deletions tests/core/proto/test_proto_set_light_from_hsbk.c
Expand Up @@ -7,13 +7,26 @@
#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"

#define MOCKED_CLIENT_SEND_RESPONSE
#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,
Expand All @@ -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)
{
Expand All @@ -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
Expand All @@ -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;
}
64 changes: 48 additions & 16 deletions tests/core/proto/test_proto_set_light_from_hsbk_on_routing_error.c
Expand Up @@ -7,13 +7,26 @@
#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"

#define MOCKED_CLIENT_SEND_RESPONSE
#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,
Expand All @@ -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)
{
Expand All @@ -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
Expand All @@ -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;
}
79 changes: 54 additions & 25 deletions tests/core/proto/test_proto_set_waveform.c
Expand Up @@ -7,13 +7,26 @@
#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"

#define MOCKED_CLIENT_SEND_RESPONSE
#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,
Expand All @@ -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)
{
Expand All @@ -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
Expand All @@ -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;
}

0 comments on commit 02341a4

Please sign in to comment.