From f8e5b3dee3a2774d2ce5a4e21cf256f3c19cc586 Mon Sep 17 00:00:00 2001 From: Ken Bannister Date: Mon, 21 May 2018 12:59:57 -0400 Subject: [PATCH] net/gcoap: update tests for nanocoap options API --- tests/unittests/tests-gcoap/tests-gcoap.c | 62 ++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/tests/unittests/tests-gcoap/tests-gcoap.c b/tests/unittests/tests-gcoap/tests-gcoap.c index fd20ae4b99..e76dc77a4d 100644 --- a/tests/unittests/tests-gcoap/tests-gcoap.c +++ b/tests/unittests/tests-gcoap/tests-gcoap.c @@ -83,7 +83,10 @@ static void test_gcoap__client_get_req(void) TEST_ASSERT_EQUAL_INT(GCOAP_TOKENLEN, coap_get_token_len(&pdu)); TEST_ASSERT_EQUAL_INT(hdr_fixed_len + GCOAP_TOKENLEN, coap_get_total_hdr_len(&pdu)); TEST_ASSERT_EQUAL_INT(COAP_TYPE_NON, coap_get_type(&pdu)); - TEST_ASSERT_EQUAL_STRING(&path[0], (char *)&pdu.url[0]); + + char uri[10] = {0}; + coap_get_uri(&pdu, (uint8_t *)&uri[0]); + TEST_ASSERT_EQUAL_STRING(&path[0], &uri[0]); TEST_ASSERT_EQUAL_INT(0, pdu.payload_len); TEST_ASSERT_EQUAL_INT(sizeof(pdu_data), len); } @@ -124,6 +127,61 @@ static void test_gcoap__client_get_resp(void) } } +/* + * Client PUT request success case. Test request generation. + * Set value of /riot/value resource to 1 from nanocoap server example. + */ +static void test_gcoap__client_put_req(void) +{ + uint8_t buf[GCOAP_PDU_BUF_SIZE]; + coap_pkt_t pdu; + size_t len; + char path[] = "/riot/value"; + char payload[] = "1"; + + gcoap_req_init(&pdu, buf, GCOAP_PDU_BUF_SIZE, COAP_METHOD_PUT, path); + memcpy(pdu.payload, payload, 1); + len = gcoap_finish(&pdu, 1, COAP_FORMAT_TEXT); + + coap_parse(&pdu, buf, len); + + TEST_ASSERT_EQUAL_INT(COAP_METHOD_PUT, coap_get_code(&pdu)); + TEST_ASSERT_EQUAL_INT(1, pdu.payload_len); + TEST_ASSERT_EQUAL_INT('1', (char)*pdu.payload); +} + +/* + * Builds on get_req test, to test gcoap_add_qstring() to add Uri-Query + * options. + */ +static void test_gcoap__client_get_query(void) +{ + uint8_t buf[GCOAP_PDU_BUF_SIZE]; + coap_pkt_t pdu; + char path[] = "/time"; + char key1[] = "ab"; + char val1[] = "cde"; + char key2[] = "f"; + char expected[] = "ab=cde&f"; + int optlen; + + gcoap_req_init(&pdu, buf, GCOAP_PDU_BUF_SIZE, COAP_METHOD_GET, path); + + optlen = gcoap_add_qstring(&pdu, key1, val1); + TEST_ASSERT_EQUAL_INT(7, optlen); + optlen = gcoap_add_qstring(&pdu, key2, NULL); + TEST_ASSERT_EQUAL_INT(2, optlen); + + size_t len = gcoap_finish(&pdu, 0, COAP_FORMAT_NONE); + + coap_parse(&pdu, buf, len); + + char query[20] = {0}; + coap_get_uri_query(&pdu, (uint8_t *)&query[0]); + /* skip initial '&' from coap_get_uri_query() */ + TEST_ASSERT_EQUAL_STRING(&expected[0], &query[1]); +} + /* * Helper for server_get tests below. * Request from libcoap example for gcoap_cli /cli/stats resource @@ -283,6 +341,8 @@ Test *tests_gcoap_tests(void) EMB_UNIT_TESTFIXTURES(fixtures) { new_TestFixture(test_gcoap__client_get_req), new_TestFixture(test_gcoap__client_get_resp), + new_TestFixture(test_gcoap__client_put_req), + new_TestFixture(test_gcoap__client_get_query), new_TestFixture(test_gcoap__server_get_req), new_TestFixture(test_gcoap__server_get_resp), new_TestFixture(test_gcoap__server_con_req),