Exemplo n.º 1
0
int
main(void)
{
    lgtd_lifx_wire_load_packet_info_map();

    struct lgtd_lifx_gateway *gw_1 = lgtd_tests_insert_mock_gateway(1);
    struct lgtd_lifx_bulb *bulb_1 = lgtd_tests_insert_mock_bulb(gw_1, 1);
    struct lgtd_lifx_gateway *gw_2 = lgtd_tests_insert_mock_gateway(2);
    lgtd_tests_insert_mock_bulb(gw_2, 2);

    struct lgtd_lifx_packet_power_state payload = {
        .power = LGTD_LIFX_POWER_ON
    };
    struct lgtd_proto_target_list *targets;
    targets = lgtd_tests_build_target_list("1", NULL);
    lgtd_router_send(targets, LGTD_LIFX_SET_POWER_STATE, &payload);

    if (lgtd_tests_gw_pkt_queue_size != 1) {
        lgtd_errx(1, "1 packet should have been sent");
    }

    struct lgtd_lifx_gateway *recpt_gw = lgtd_tests_gw_pkt_queue[0].gw;
    struct lgtd_lifx_packet_header *hdr_queued = lgtd_tests_gw_pkt_queue[0].hdr;
    const void *pkt_queued = lgtd_tests_gw_pkt_queue[0].pkt;
    int pkt_size = lgtd_tests_gw_pkt_queue[0].pkt_size;

    if (recpt_gw != gw_1) {
        lgtd_errx(1, "the packet has been sent to the wrong gateway");
    }

    if (!lgtd_tests_lifx_header_has_flags(hdr_queued, LGTD_LIFX_ADDRESSABLE)) {
        lgtd_errx(1, "the packet header doesn't have the right protocol flags");
    }

    if (memcmp(hdr_queued->target.device_addr, bulb_1->addr, sizeof(bulb_1->addr))) {
        lgtd_errx(1, "the packet header doesn't have the right target address");
    }

    if (memcmp(gw_1->site.as_array, hdr_queued->site, sizeof(hdr_queued->site))) {
        lgtd_errx(1, "incorrect site in the headers");
    }

    if (pkt_queued != &payload) {
        lgtd_errx(1, "invalid payload");
    }

    if (pkt_size != sizeof(payload)) {
        lgtd_errx(
            1, "unexpected pkt size %d (expected %ju)",
            pkt_size, (uintmax_t)sizeof(payload)
        );
    }

    return 0;
}
int
main(void)
{
    struct lgtd_lifx_gateway gw;
    memset(&gw, 0, sizeof(gw));
    test_bulb = lgtd_tests_insert_mock_bulb(&gw, 75);
    union lgtd_timer_ctx ctx = { .as_uint = 1 };

    // make sure it handles non-existent bulbs:
    lgtd_lifx_bulb_fetch_hardware_info(FAKE_TIMER, ctx);
    if (timer_stop_call_count != 1) {
        errx(1, "timer_stop wasn't called");
    }

    ctx.as_uint = 0;
    memcpy(&ctx.as_uint, test_bulb->addr, LGTD_LIFX_ADDR_LENGTH);
    lgtd_lifx_bulb_fetch_hardware_info(FAKE_TIMER, ctx);
    test_counters(1, 1, 0);

    memset(&test_bulb->product_info, 1, sizeof(test_bulb->product_info));
    lgtd_lifx_bulb_fetch_hardware_info(FAKE_TIMER, ctx);
    test_counters(1, 2, 0);
    struct lgtd_lifx_bulb_ip *ip = &test_bulb->ips[LGTD_LIFX_BULB_MCU_IP];
    memset(ip, 1, sizeof(*ip));

    // the retry logic for the wifi firmware is a bit more complex because of
    // that v1.1 bug for non-gateway bulbs:

    // state_updated_at for the mcu ip is at zero so we give up and stop the
    // timer instead of sending a new packet:
    ip->state_updated_at = 0;
    lgtd_lifx_bulb_fetch_hardware_info(FAKE_TIMER, ctx);
    test_counters(1, 2, 0);
    if (timer_stop_call_count != 2) {
        errx(1, "timer_stop wasn't called");
    }

    // set it to the current time, the packet should be sent again:
    ip = &test_bulb->ips[LGTD_LIFX_BULB_MCU_IP];
    ip->state_updated_at = lgtd_time_monotonic_msecs();
    lgtd_lifx_bulb_fetch_hardware_info(FAKE_TIMER, ctx);
    test_counters(1, 2, 1);
    if (timer_stop_call_count != 2) {
        errx(1, "timer_stop shouldn't have been called");
    }

    // finally make sure we stop the timer if we got the info alright as it
    // should just be without the bug:
    ip = &test_bulb->ips[LGTD_LIFX_BULB_WIFI_IP];
    memset(ip, 1, sizeof(*ip));
    lgtd_lifx_bulb_fetch_hardware_info(FAKE_TIMER, ctx);
    test_counters(1, 2, 1);
    if (timer_stop_call_count != 3) {
        errx(1, "timer_stop wasn't called");
    }

    return 0;
}
int
main(void)
{
    lgtd_daemon_proctitle_initialized = true;

    expected = "bulbs(found=0, on=0); clients(connected=0)";
    lgtd_daemon_update_proctitle();
    if (setproctitle_call_count != 1) {
        errx(1, "setproctitle should have been called");
    }

    expected = (
        "lifx_gateways(found=1); "
        "bulbs(found=0, on=0); "
        "clients(connected=0)"
    );
    struct lgtd_lifx_gateway *gw_1 = lgtd_tests_insert_mock_gateway(1);
    if (setproctitle_call_count != 2) {
        errx(1, "setproctitle should have been called");
    }

    expected = (
        "lifx_gateways(found=1); "
        "bulbs(found=1, on=0); "
        "clients(connected=0)"
    );
    lgtd_tests_insert_mock_bulb(gw_1, 2);
    expected = (
        "lifx_gateways(found=1); "
        "bulbs(found=2, on=0); "
        "clients(connected=0)"
    );
    lgtd_tests_insert_mock_bulb(gw_1, 3);
    if (setproctitle_call_count != 4) {
        errx(1, "setproctitle should have been called");
    }

    expected = (
        "listening_on([::ffff:127.0.0.1]:1234); "
        "lifx_gateways(found=1); "
        "bulbs(found=2, on=0); "
        "clients(connected=0)"
    );
    lgtd_tests_insert_mock_listener("127.0.0.1", 1234);
    lgtd_daemon_update_proctitle();
    if (setproctitle_call_count != 5) {
        errx(1, "setproctitle should have been called");
    }

    expected = (
        "listening_on([::ffff:127.0.0.1]:1234); "
        "lifx_gateways(found=1); "
        "bulbs(found=2, on=1); "
        "clients(connected=0)"
    );
    LGTD_STATS_ADD_AND_UPDATE_PROCTITLE(bulbs_powered_on, 1);
    if (setproctitle_call_count != 6) {
        errx(1, "setproctitle should have been called");
    }

    expected = (
        "listening_on([::ffff:127.0.0.1]:1234); "
        "lifx_gateways(found=1); "
        "bulbs(found=2, on=1); "
        "clients(connected=1)"
    );
    LGTD_STATS_ADD_AND_UPDATE_PROCTITLE(clients, 1);
    if (setproctitle_call_count != 7) {
        errx(1, "setproctitle should have been called");
    }

    return 0;
}
Exemplo n.º 4
0
int
main(void)
{
    lgtd_lifx_wire_setup();

    struct lgtd_lifx_gateway *gw_1 = lgtd_tests_insert_mock_gateway(1);
    struct lgtd_lifx_bulb *bulb_1 = lgtd_tests_insert_mock_bulb(gw_1, 1);
    struct lgtd_lifx_gateway *gw_2 = lgtd_tests_insert_mock_gateway(2);
    struct lgtd_lifx_bulb *bulb_2 = lgtd_tests_insert_mock_bulb(gw_2, 2);
    struct lgtd_lifx_bulb *bulb_3 = lgtd_tests_insert_mock_bulb(gw_2, 3);

    const char *label = "feed";
    strcpy(bulb_1->state.label, label);
    strcpy(bulb_3->state.label, label);
    strcpy(bulb_2->state.label, "trololo");

    struct lgtd_lifx_packet_power_state payload = {
        .power = LGTD_LIFX_POWER_ON
    };
    struct lgtd_proto_target_list *targets;
    targets = lgtd_tests_build_target_list(label, NULL);
    lgtd_router_send(targets, LGTD_LIFX_SET_POWER_STATE, &payload);

    if (lgtd_tests_gw_pkt_queue_size != 2) {
        lgtd_errx(1, "2 packet should have been sent");
    }

    for (int i = 0; i != lgtd_tests_gw_pkt_queue_size; i++) {
        struct lgtd_lifx_gateway *recpt_gw = lgtd_tests_gw_pkt_queue[0].gw;
        struct lgtd_lifx_packet_header *hdr_queued = lgtd_tests_gw_pkt_queue[0].hdr;
        const void *pkt_queued = lgtd_tests_gw_pkt_queue[0].pkt;
        int pkt_size = lgtd_tests_gw_pkt_queue[0].pkt_size;


    int expected_flags = LGTD_LIFX_ADDRESSABLE|LGTD_LIFX_RES_REQUIRED;
        if (!lgtd_tests_lifx_header_has_flags(hdr_queued, expected_flags)) {
            lgtd_errx(1, "the packet header doesn't have the right protocol flags");
        }
        if (pkt_queued != &payload) {
            lgtd_errx(1, "invalid payload");
        }
        if (pkt_size != sizeof(payload)) {
            lgtd_errx(
                1, "unexpected pkt size %d (expected %ju)",
                pkt_size, (uintmax_t)sizeof(payload)
            );
        }

        if (recpt_gw == gw_1) {
            if (memcmp(hdr_queued->target.device_addr, bulb_1->addr, sizeof(bulb_1->addr))) {
                lgtd_errx(1, "the packet header doesn't have the right target address");
            }
            if (memcmp(gw_1->site.as_array, hdr_queued->site, sizeof(hdr_queued->site))) {
                lgtd_errx(1, "incorrect site in the headers");
            }
        } else if (recpt_gw == gw_2) {
            if (memcmp(hdr_queued->target.device_addr, bulb_3->addr, sizeof(bulb_3->addr))) {
                lgtd_errx(1, "the packet header doesn't have the right target address");
            }
            if (memcmp(gw_2->site.as_array, hdr_queued->site, sizeof(hdr_queued->site))) {
                lgtd_errx(1, "incorrect site in the headers");
            }
        }
    }

    return 0;
}