Mercurial > louis > mq > lightsd
changeset 422:e262a6cceb20
wip testing network discovery
author | Louis Opter <kalessin@kalessin.fr> |
---|---|
date | Tue, 05 Jan 2016 16:36:04 +0100 |
parents | fc431cab531c |
children | 0a5896d64aaa |
files | optional_jsonrpc_args.patch |
diffstat | 1 files changed, 154 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/optional_jsonrpc_args.patch Mon Jan 04 19:16:27 2016 +0100 +++ b/optional_jsonrpc_args.patch Tue Jan 05 16:36:04 2016 +0100 @@ -1,5 +1,5 @@ # HG changeset patch -# Parent b13532c9f0219cdf0459d1455e613633254d0ce9 +# Parent 4b74a5b40d1d3fdeab6a639975ae941544839e33 Correctly support optional arguments in the JSON-RPC API Passing too many arguments as an array also properly fails now. @@ -89,7 +89,7 @@ skew_ratio -= UINT16_MAX / 2; - bool transient = client->json[params.transient->start] == 't'; + bool transient = params.transient ? -+ client->json[params.transient->start] == 't' : false; ++ client->json[params.transient->start] == 't' : true; struct lgtd_proto_target_list targets = SLIST_HEAD_INITIALIZER(&targets); ok = lgtd_jsonrpc_build_target_list( @@ -245,7 +245,7 @@ end of the waveform, otherwise it will revert back to - its original state. + its original state. This argument is optional and -+ defaults to false. ++ defaults to true. The meaning of the ``skew_ratio`` argument depends on the type of waveform: @@ -491,6 +491,157 @@ errx(1, "lgtd_proto_set_light_from_hsbk wasn't called"); } +diff --git a/tests/core/jsonrpc/test_jsonrpc_check_and_call_set_light_from_hsbk_invalid_params.c b/tests/core/jsonrpc/test_jsonrpc_check_and_call_set_light_from_hsbk_invalid_params.c +--- a/tests/core/jsonrpc/test_jsonrpc_check_and_call_set_light_from_hsbk_invalid_params.c ++++ b/tests/core/jsonrpc/test_jsonrpc_check_and_call_set_light_from_hsbk_invalid_params.c +@@ -133,5 +133,29 @@ + "}," + "\"id\": \"42\"" + "}"); ++ ++ // invalid temperature: ++ test_request("{" ++ "\"jsonrpc\": \"2.0\"," ++ "\"method\": \"set_light_from_hsbk\"," ++ "\"params\": {" ++ "\"target\": \"*\", " ++ "\"hue\": 324.2341514, " ++ "\"saturation\": 0.234, " ++ "\"brightness\": 1.0, " ++ "\"kelvin\": -4200," ++ "\"transition\": 42" ++ "}," ++ "\"id\": \"42\"" ++ "}"); ++ ++ // too many params ++ test_request("{" ++ "\"jsonrpc\": \"2.0\"," ++ "\"method\": \"set_light_from_hsbk\"," ++ "\"params\": [\"*\", 324.2341514, 0.234, 1.0, 4200, 600, \"extraarg\"]," ++ "\"id\": \"42\"" ++ "}"); ++ + return 0; + } +diff --git a/tests/core/jsonrpc/test_jsonrpc_check_and_call_set_waveform.c b/tests/core/jsonrpc/test_jsonrpc_check_and_call_set_waveform.c +--- a/tests/core/jsonrpc/test_jsonrpc_check_and_call_set_waveform.c ++++ b/tests/core/jsonrpc/test_jsonrpc_check_and_call_set_waveform.c +@@ -23,7 +23,7 @@ + return LGTD_LIFX_WAVEFORM_SAW; + } + +-static bool set_waveform_called = false; ++static int set_waveform_call_count = 0; + + void + lgtd_proto_set_waveform(struct lgtd_client *client, +@@ -79,23 +79,38 @@ + if (skew_ratio != 0) { + errx(1, "Invalid skew_ratio: %d, expected: 0", skew_ratio); + } +- if (!transient) { +- errx(1, "transient is false instead of true"); +- } + if (waveform != LGTD_LIFX_WAVEFORM_SAW) { + errx( + 1, "Invalid waveform %d: expected: %d", + waveform, LGTD_LIFX_WAVEFORM_SAW + ); + } +- set_waveform_called = true; ++ switch (set_waveform_call_count++) { ++ case 0: ++ if (transient) { ++ errx(1, "Invalid transient: true (expected false)"); ++ } ++ break; ++ case 1: ++ if (!transient) { ++ errx(1, "Invalid transient: false (expected true)"); ++ } ++ break; ++ default: ++ errx(1, "set_waveform called too many times"); ++ } + } + + int + main(void) + { + jsmntok_t tokens[32]; +- const char json[] = ("{" ++ int parsed; ++ bool ok; ++ struct lgtd_jsonrpc_request req; ++ struct lgtd_client client = { .io = NULL, .current_request = &req }; ++ ++ const char *json = ("{" + "\"jsonrpc\": \"2.0\"," + "\"method\": \"set_waveform\"," + "\"params\": {" +@@ -107,28 +122,49 @@ + "\"cycles\": 10," + "\"period\": 1000," + "\"skew_ratio\": 0.5," +- "\"transient\": true," ++ "\"transient\": false," + "\"waveform\": \"SAW\"" + "}," + "\"id\": \"42\"" + "}"); +- int parsed = parse_json( +- tokens, LGTD_ARRAY_SIZE(tokens), json, sizeof(json) +- ); +- +- bool ok; +- struct lgtd_jsonrpc_request req = TEST_REQUEST_INITIALIZER; +- struct lgtd_client client = { +- .io = NULL, .current_request = &req, .json = json +- }; ++ client.json = json; ++ parsed = parse_json(tokens, LGTD_ARRAY_SIZE(tokens), json, strlen(json)); ++ memset(&req, 0, sizeof(req)); + ok = lgtd_jsonrpc_check_and_extract_request(&req, tokens, parsed, json); + if (!ok) { + errx(1, "can't parse request"); + } ++ lgtd_jsonrpc_check_and_call_set_waveform(&client); ++ if (set_waveform_call_count != 1) { ++ errx(1, "lgtd_proto_set_waveform wasn't called"); ++ } + ++ // optional transient argument ++ json = ("{" ++ "\"jsonrpc\": \"2.0\"," ++ "\"method\": \"set_waveform\"," ++ "\"params\": {" ++ "\"target\": \"*\", " ++ "\"hue\": 324.2341514, " ++ "\"saturation\": 0.234, " ++ "\"brightness\": 1.0, " ++ "\"kelvin\": 4200," ++ "\"cycles\": 10," ++ "\"period\": 1000," ++ "\"skew_ratio\": 0.5," ++ "\"waveform\": \"SAW\"" ++ "}," ++ "\"id\": \"42\"" ++ "}"); ++ client.json = json; ++ parsed = parse_json(tokens, LGTD_ARRAY_SIZE(tokens), json, strlen(json)); ++ memset(&req, 0, sizeof(req)); ++ ok = lgtd_jsonrpc_check_and_extract_request(&req, tokens, parsed, json); ++ if (!ok) { ++ errx(1, "can't parse request"); ++ } + lgtd_jsonrpc_check_and_call_set_waveform(&client); +- +- if (!set_waveform_called) { ++ if (set_waveform_call_count != 2) { + errx(1, "lgtd_proto_set_waveform wasn't called"); + } + diff --git a/tests/core/jsonrpc/test_jsonrpc_dispatch_one_no_params.c b/tests/core/jsonrpc/test_jsonrpc_dispatch_one_no_params.c --- a/tests/core/jsonrpc/test_jsonrpc_dispatch_one_no_params.c +++ b/tests/core/jsonrpc/test_jsonrpc_dispatch_one_no_params.c