From 3677caa37e9685bf55eb529906a14b8c7a67187f Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Wed, 24 May 2017 13:44:13 +0300 Subject: [PATCH] tests: net: zoap: Add path uri matching tests Test the match_path_uri() function that was fixed by previous commit. Signed-off-by: Jukka Rissanen --- subsys/net/lib/zoap/Kconfig | 8 +++ subsys/net/lib/zoap/zoap_link_format.c | 9 ++++ tests/net/lib/zoap/prj.conf | 1 + tests/net/lib/zoap/src/main.c | 70 ++++++++++++++++++++++++++ 4 files changed, 88 insertions(+) diff --git a/subsys/net/lib/zoap/Kconfig b/subsys/net/lib/zoap/Kconfig index c83f421f9f7..29ee61ba707 100644 --- a/subsys/net/lib/zoap/Kconfig +++ b/subsys/net/lib/zoap/Kconfig @@ -13,6 +13,14 @@ config ZOAP help This option enables the Zoap implementation of CoAP. +# This setting is only used by unit test. Do not enable it in applications +config ZOAP_TEST_API_ENABLE + bool "Enable test API for ZoAP unit tests" + default n + depends on ZOAP + help + Do not enable this for normal use. + config ZOAP_WELL_KNOWN_BLOCK_WISE bool prompt "CoAP ./well-known/core services block wise support" diff --git a/subsys/net/lib/zoap/zoap_link_format.c b/subsys/net/lib/zoap/zoap_link_format.c index 2b9e176e3a1..dbf1873d412 100644 --- a/subsys/net/lib/zoap/zoap_link_format.c +++ b/subsys/net/lib/zoap/zoap_link_format.c @@ -715,3 +715,12 @@ done: return r; } #endif + +/* Exposing some of the APIs to ZoAP unit tests in tests/net/lib/zoap */ +#if defined(CONFIG_ZOAP_TEST_API_ENABLE) +bool _zoap_match_path_uri(const char * const *path, + const char *uri, u16_t len) +{ + return match_path_uri(path, uri, len); +} +#endif diff --git a/tests/net/lib/zoap/prj.conf b/tests/net/lib/zoap/prj.conf index 39085e54ad3..65c916f76a2 100644 --- a/tests/net/lib/zoap/prj.conf +++ b/tests/net/lib/zoap/prj.conf @@ -5,3 +5,4 @@ CONFIG_NETWORKING_WITH_IPV6=y CONFIG_RANDOM_GENERATOR=y CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_ZOAP=y +CONFIG_ZOAP_TEST_API_ENABLE=y diff --git a/tests/net/lib/zoap/src/main.c b/tests/net/lib/zoap/src/main.c index f043ae77541..479433faf15 100644 --- a/tests/net/lib/zoap/src/main.c +++ b/tests/net/lib/zoap/src/main.c @@ -39,6 +39,10 @@ static struct zoap_pending pendings[NUM_PENDINGS]; static struct zoap_observer observers[NUM_OBSERVERS]; static struct zoap_reply replies[NUM_REPLIES]; +/* This is exposed for this test in subsys/net/lib/zoap/zoap_link_format.c */ +bool _zoap_match_path_uri(const char * const *path, + const char *uri, u16_t len); + /* Some forward declarations */ static void server_notify_callback(struct zoap_resource *resource, struct zoap_observer *observer); @@ -1060,6 +1064,71 @@ done: return result; } +static int test_match_path_uri(void) +{ + int result = TC_FAIL; + const char * const resource_path[] = { + "s", + "1", + "foobar", + "foobar3a", + "foobar3", + "devnull", + NULL + }; + const char *uri; + + uri = "/k"; + if (_zoap_match_path_uri(resource_path, uri, strlen(uri))) { + TC_PRINT("Matching %s failed\n", uri); + goto out; + } + + uri = "/s"; + if (!_zoap_match_path_uri(resource_path, uri, strlen(uri))) { + TC_PRINT("Matching %s failed\n", uri); + goto out; + } + + uri = "/foobar"; + if (!_zoap_match_path_uri(resource_path, uri, strlen(uri))) { + TC_PRINT("Matching %s failed\n", uri); + goto out; + } + + uri = "/foobar2"; + if (_zoap_match_path_uri(resource_path, uri, strlen(uri))) { + TC_PRINT("Matching %s failed\n", uri); + goto out; + } + + uri = "/foobar*"; + if (!_zoap_match_path_uri(resource_path, uri, strlen(uri))) { + TC_PRINT("Matching %s failed\n", uri); + goto out; + } + + uri = "/foobar3*"; + if (!_zoap_match_path_uri(resource_path, uri, strlen(uri))) { + TC_PRINT("Matching %s failed\n", uri); + goto out; + } + + uri = "/devnull*"; + if (_zoap_match_path_uri(resource_path, uri, strlen(uri))) { + TC_PRINT("Matching %s failed\n", uri); + goto out; + } + + result = TC_PASS; + +out: + TC_END_RESULT(result); + + return result; + +} + static const struct { const char *name; int (*func)(void); @@ -1073,6 +1142,7 @@ static const struct { { "Test observer server", test_observer_server, }, { "Test observer client", test_observer_client, }, { "Test block sized transfer", test_block_size, }, + { "Test match path uri", test_match_path_uri, }, }; int main(int argc, char *argv[])