2020-03-30 22:46:41 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 HAW Hamburg
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2020-04-03 14:53:33 +02:00
|
|
|
#include <stdbool.h>
|
2020-03-30 22:46:41 +02:00
|
|
|
#include "embUnit.h"
|
|
|
|
|
|
|
|
#include "uri_parser.h"
|
|
|
|
|
|
|
|
#include "unittests-constants.h"
|
|
|
|
#include "tests-uri_parser.h"
|
|
|
|
|
2020-10-29 14:25:14 +01:00
|
|
|
#define VEC(u, f, s, us, h, v6a, z, po, pa, q, e) \
|
2020-04-03 14:53:33 +02:00
|
|
|
{ .uri = u, .full_uri = f, .scheme = s, .userinfo = us, .host = h, \
|
2020-10-29 14:25:14 +01:00
|
|
|
.ipv6addr = v6a, .zoneid = z, .port = po, .path = pa, \
|
|
|
|
.query = q, .expected = e}
|
2020-03-30 22:46:41 +02:00
|
|
|
|
2020-04-01 08:42:25 +02:00
|
|
|
#define VEC_CHECK(comp, i, vec_msg) \
|
|
|
|
do { \
|
|
|
|
if (ures.comp == NULL) { \
|
|
|
|
TEST_ASSERT(validate_uris[i].comp[0] == '\0'); \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
TEST_ASSERT_EQUAL_INT(strlen(validate_uris[i].comp), \
|
|
|
|
ures.comp##_len); \
|
|
|
|
vec_msg[0] = '\0'; \
|
|
|
|
stdimpl_strcat(vec_msg, "Unexpected " # comp " member \""); \
|
|
|
|
stdimpl_strcat(vec_msg, validate_uris[i].comp); \
|
|
|
|
stdimpl_strcat(vec_msg, "\" for \""); \
|
|
|
|
stdimpl_strcat(vec_msg, validate_uris[i].uri); \
|
|
|
|
stdimpl_strcat(vec_msg, "\""); \
|
|
|
|
TEST_ASSERT_MESSAGE(0 == \
|
|
|
|
strncmp(ures.comp, \
|
|
|
|
validate_uris[i].comp, \
|
|
|
|
strlen(validate_uris[i].comp)), \
|
|
|
|
vec_msg); \
|
|
|
|
} \
|
2020-03-30 22:46:41 +02:00
|
|
|
} while (0)
|
|
|
|
|
2022-05-13 11:47:17 +02:00
|
|
|
#define VEC_MSG_LEN (256)
|
2020-04-01 08:42:25 +02:00
|
|
|
|
2020-03-30 22:46:41 +02:00
|
|
|
typedef struct {
|
|
|
|
char uri[64];
|
2020-04-03 14:53:33 +02:00
|
|
|
bool full_uri;
|
2020-03-30 22:46:41 +02:00
|
|
|
char scheme[8];
|
|
|
|
char userinfo[16];
|
2020-10-29 14:25:14 +01:00
|
|
|
char host[24];
|
|
|
|
char ipv6addr[16];
|
|
|
|
char zoneid[8];
|
2020-03-30 22:46:41 +02:00
|
|
|
char port[32];
|
|
|
|
char path[48];
|
|
|
|
char query[32];
|
|
|
|
int expected;
|
|
|
|
} validate_t;
|
|
|
|
|
|
|
|
/*
|
|
|
|
VEC(uri_to_parse,
|
|
|
|
scheme, userinfo, host, port,
|
|
|
|
path, query, expected return value)
|
|
|
|
*/
|
2020-10-29 14:25:14 +01:00
|
|
|
static const validate_t validate_uris[] = {
|
2020-03-30 22:46:41 +02:00
|
|
|
/* uri to parse */
|
2020-10-29 14:25:14 +01:00
|
|
|
VEC("coap://RIOT:test@[fe80:db8::1%tap0]:5683/.well-known/core?v=1",
|
2020-04-03 14:53:33 +02:00
|
|
|
/* is URI */
|
|
|
|
true,
|
2020-03-30 22:46:41 +02:00
|
|
|
/* parsed scheme */
|
|
|
|
"coap",
|
|
|
|
/* parsed userinfo */
|
|
|
|
"RIOT:test",
|
|
|
|
/* parsed host */
|
2020-10-29 14:25:14 +01:00
|
|
|
"[fe80:db8::1%tap0]",
|
|
|
|
/* parsed host without zoneid */
|
|
|
|
"fe80:db8::1",
|
|
|
|
/* parsed zoneid */
|
|
|
|
"tap0",
|
2020-03-30 22:46:41 +02:00
|
|
|
/* parsed port */
|
|
|
|
"5683",
|
|
|
|
/* parsed path */
|
|
|
|
"/.well-known/core",
|
|
|
|
/* parsed query */
|
|
|
|
"v=1",
|
|
|
|
/* expected return value */
|
|
|
|
0),
|
2020-10-29 14:25:14 +01:00
|
|
|
VEC("coap://RIOT:test@[fe80:db8::1%]:5683/.well-known/core?v=1",
|
|
|
|
true,
|
|
|
|
"coap",
|
|
|
|
"RIOT:test",
|
|
|
|
"[fe80:db8::1%]",
|
|
|
|
"fe80:db8::1",
|
|
|
|
"",
|
|
|
|
"5683",
|
|
|
|
"/.well-known/core",
|
|
|
|
"v=1",
|
|
|
|
-1),
|
|
|
|
VEC("coap://[fe80::1]/foo%20bar",
|
|
|
|
true,
|
|
|
|
"coap",
|
|
|
|
"",
|
|
|
|
"[fe80::1]",
|
|
|
|
"fe80::1",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"/foo%20bar",
|
|
|
|
"",
|
|
|
|
0),
|
2020-03-30 22:46:41 +02:00
|
|
|
VEC("/.well-known/core?v=1",
|
2020-04-03 14:53:33 +02:00
|
|
|
false,
|
2020-03-30 22:46:41 +02:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"/.well-known/core",
|
|
|
|
"v=1",
|
|
|
|
0),
|
|
|
|
VEC("coap://R@[2001:db8::1]:5own/v=1",
|
2020-04-03 14:53:33 +02:00
|
|
|
true,
|
2020-03-30 22:46:41 +02:00
|
|
|
"coap",
|
|
|
|
"R",
|
|
|
|
"[2001:db8::1]",
|
2020-10-29 14:25:14 +01:00
|
|
|
"2001:db8::1",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"5own",
|
|
|
|
"/v=1",
|
|
|
|
"",
|
|
|
|
0),
|
|
|
|
VEC("coap://R@[2001:db8::1]:5own/:v=1",
|
2020-04-03 14:53:33 +02:00
|
|
|
true,
|
2020-03-30 22:46:41 +02:00
|
|
|
"coap",
|
|
|
|
"R",
|
|
|
|
"[2001:db8::1]",
|
2020-10-29 14:25:14 +01:00
|
|
|
"2001:db8::1",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"5own",
|
|
|
|
"/:v=1",
|
|
|
|
"",
|
|
|
|
0),
|
|
|
|
VEC("cap://R@[2001:db8::1]:5own/?v=1",
|
2020-04-03 14:53:33 +02:00
|
|
|
true,
|
2020-03-30 22:46:41 +02:00
|
|
|
"cap",
|
|
|
|
"R",
|
|
|
|
"[2001:db8::1]",
|
2020-10-29 14:25:14 +01:00
|
|
|
"2001:db8::1",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"5own",
|
|
|
|
"/",
|
|
|
|
"v=1",
|
|
|
|
0),
|
|
|
|
VEC("oap://Y2001:db8::1]:5own/av=1",
|
2020-04-03 14:53:33 +02:00
|
|
|
true,
|
2020-03-30 22:46:41 +02:00
|
|
|
"oap",
|
|
|
|
"",
|
|
|
|
"Y2001:db8::1]",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"5own",
|
|
|
|
"/av=1",
|
|
|
|
"",
|
|
|
|
0),
|
|
|
|
VEC("//Rb[ʰ00J:d/5v=0",
|
2020-04-03 14:53:33 +02:00
|
|
|
false,
|
2020-03-30 22:46:41 +02:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"//Rb[ʰ00J:d/5v=0",
|
|
|
|
"",
|
|
|
|
0),
|
|
|
|
VEC("coap://oap://P@[2001:b",
|
2020-04-03 14:53:33 +02:00
|
|
|
true,
|
2020-03-30 22:46:41 +02:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
-1),
|
|
|
|
VEC("coap:///R@[2008::1]:5own//R@[2008::1]:5own/?v=1",
|
2020-04-03 14:53:33 +02:00
|
|
|
true,
|
2020-03-30 22:46:41 +02:00
|
|
|
"coap",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"/R@[2008::1]:5own//R@[2008::1]:5own/",
|
|
|
|
"v=1",
|
|
|
|
0),
|
|
|
|
VEC("coaP://R/RZ[2001[8:01[8::1]:5o:1]:5oTMv=1",
|
2020-04-03 14:53:33 +02:00
|
|
|
true,
|
2020-03-30 22:46:41 +02:00
|
|
|
"coaP",
|
|
|
|
"",
|
|
|
|
"R",
|
|
|
|
"",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"/RZ[2001[8:01[8::1]:5o:1]:5oTMv=1",
|
|
|
|
"",
|
|
|
|
0),
|
|
|
|
VEC("coap://R@////////////////7///v=1",
|
2020-04-03 14:53:33 +02:00
|
|
|
true,
|
2021-02-04 16:20:01 +01:00
|
|
|
"coap",
|
|
|
|
"R",
|
2020-03-30 22:46:41 +02:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2021-02-04 16:20:01 +01:00
|
|
|
"////////////////7///v=1",
|
2020-03-30 22:46:41 +02:00
|
|
|
"",
|
2021-02-04 16:20:01 +01:00
|
|
|
0),
|
2020-03-30 22:46:41 +02:00
|
|
|
VEC("coa[:////[2001:db5ow:5own/Ov=1",
|
2020-04-03 15:19:35 +02:00
|
|
|
false,
|
2020-03-30 22:46:41 +02:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-04-03 15:19:35 +02:00
|
|
|
"coa[:////[2001:db5ow:5own/Ov=1",
|
2020-03-30 22:46:41 +02:00
|
|
|
"",
|
|
|
|
0),
|
|
|
|
VEC("tel:+1-816-555-1212",
|
2020-04-03 14:53:33 +02:00
|
|
|
true,
|
2020-03-30 22:46:41 +02:00
|
|
|
"tel",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"+1-816-555-1212",
|
|
|
|
"",
|
|
|
|
0),
|
|
|
|
VEC("sms:+15105550101,+15105550102?body=hello%20there",
|
2020-04-03 14:53:33 +02:00
|
|
|
true,
|
2020-03-30 22:46:41 +02:00
|
|
|
"sms",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"+15105550101,+15105550102",
|
|
|
|
"body=hello%20there",
|
|
|
|
0),
|
|
|
|
VEC("a",
|
2020-04-03 14:53:33 +02:00
|
|
|
false,
|
2020-03-30 22:46:41 +02:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"a",
|
|
|
|
"",
|
|
|
|
0),
|
|
|
|
VEC("mailto:test@example.com",
|
2020-04-03 14:53:33 +02:00
|
|
|
true,
|
2020-03-30 22:46:41 +02:00
|
|
|
"mailto",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"test@example.com",
|
|
|
|
"",
|
|
|
|
0),
|
|
|
|
VEC("ftp://ftp.is.co.za/rfc/rfc1808.txt",
|
2020-04-03 14:53:33 +02:00
|
|
|
true,
|
2020-03-30 22:46:41 +02:00
|
|
|
"ftp",
|
|
|
|
"",
|
|
|
|
"ftp.is.co.za",
|
|
|
|
"",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"/rfc/rfc1808.txt",
|
|
|
|
"",
|
|
|
|
0),
|
|
|
|
VEC("http://www.ietf.org/rfc/rfc2396.txt",
|
2020-04-03 14:53:33 +02:00
|
|
|
true,
|
2020-03-30 22:46:41 +02:00
|
|
|
"http",
|
|
|
|
"",
|
|
|
|
"www.ietf.org",
|
|
|
|
"",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"/rfc/rfc2396.txt",
|
|
|
|
"",
|
|
|
|
0),
|
|
|
|
VEC("ldap://[2001:db8::7]/c=GB?objectClass?one",
|
2020-04-03 14:53:33 +02:00
|
|
|
true,
|
2020-03-30 22:46:41 +02:00
|
|
|
"ldap",
|
|
|
|
"",
|
|
|
|
"[2001:db8::7]",
|
2020-10-29 14:25:14 +01:00
|
|
|
"2001:db8::7",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"",
|
|
|
|
"/c=GB",
|
|
|
|
"objectClass?one",
|
|
|
|
0),
|
|
|
|
VEC("mailto:John.Doe@example.com",
|
2020-04-03 14:53:33 +02:00
|
|
|
true,
|
2020-03-30 22:46:41 +02:00
|
|
|
"mailto",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"John.Doe@example.com",
|
|
|
|
"",
|
|
|
|
0),
|
|
|
|
VEC("news:comp.infosystems.www.servers.unix",
|
2020-04-03 14:53:33 +02:00
|
|
|
true,
|
2020-03-30 22:46:41 +02:00
|
|
|
"news",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"comp.infosystems.www.servers.unix",
|
|
|
|
"",
|
|
|
|
0),
|
|
|
|
VEC("tel:+1-816-555-1212",
|
2020-04-03 14:53:33 +02:00
|
|
|
true,
|
2020-03-30 22:46:41 +02:00
|
|
|
"tel",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"+1-816-555-1212",
|
|
|
|
"",
|
|
|
|
0),
|
|
|
|
VEC("telnet://192.0.2.16:80/",
|
2020-04-03 14:53:33 +02:00
|
|
|
true,
|
2020-03-30 22:46:41 +02:00
|
|
|
"telnet",
|
|
|
|
"",
|
|
|
|
"192.0.2.16",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"80",
|
|
|
|
"/",
|
|
|
|
"",
|
|
|
|
0),
|
|
|
|
VEC("urn:oasis:names:specification:docbook:dtd:xml:4.1.2",
|
2020-04-03 14:53:33 +02:00
|
|
|
true,
|
2020-03-30 22:46:41 +02:00
|
|
|
"urn",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"oasis:names:specification:docbook:dtd:xml:4.1.2",
|
|
|
|
"",
|
|
|
|
0),
|
|
|
|
VEC("",
|
2020-04-03 14:53:33 +02:00
|
|
|
false,
|
2020-03-30 22:46:41 +02:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
-1),
|
|
|
|
VEC("/",
|
2020-04-03 14:53:33 +02:00
|
|
|
false,
|
2020-03-30 22:46:41 +02:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-03-30 22:46:41 +02:00
|
|
|
"/",
|
|
|
|
"",
|
|
|
|
0),
|
2020-04-03 15:19:35 +02:00
|
|
|
VEC("./this:that",
|
|
|
|
false,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
2020-10-29 14:25:14 +01:00
|
|
|
"",
|
|
|
|
"",
|
2020-04-03 15:19:35 +02:00
|
|
|
"./this:that",
|
|
|
|
"",
|
|
|
|
0),
|
2021-02-04 20:08:26 +01:00
|
|
|
VEC("pP://",
|
|
|
|
true,
|
|
|
|
"pP",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
0),
|
2021-02-04 16:20:01 +01:00
|
|
|
VEC("A://@",
|
|
|
|
true,
|
|
|
|
"A",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
0),
|
2022-05-11 21:20:53 +02:00
|
|
|
VEC("coap://example.com:1234568910",
|
|
|
|
/* is URI */
|
|
|
|
true,
|
|
|
|
/* parsed scheme */
|
|
|
|
"coap",
|
|
|
|
/* parsed userinfo */
|
|
|
|
"",
|
|
|
|
/* parsed host */
|
|
|
|
"example.com",
|
|
|
|
/* parsed host without zoneid */
|
|
|
|
"",
|
|
|
|
/* parsed zoneid */
|
|
|
|
"",
|
|
|
|
/* parsed port */
|
|
|
|
"",
|
|
|
|
/* parsed path */
|
|
|
|
"",
|
|
|
|
/* parsed query */
|
|
|
|
"",
|
|
|
|
/* expected return value */
|
|
|
|
-1),
|
2020-03-30 22:46:41 +02:00
|
|
|
};
|
|
|
|
|
2020-04-01 08:42:25 +02:00
|
|
|
static char _failure_msg[VEC_MSG_LEN];
|
|
|
|
|
2020-03-30 22:46:41 +02:00
|
|
|
static void test_uri_parser__validate(void)
|
|
|
|
{
|
|
|
|
uri_parser_result_t ures;
|
|
|
|
for (unsigned i = 0; i < ARRAY_SIZE(validate_uris); i++) {
|
|
|
|
int res = uri_parser_process_string(&ures, validate_uris[i].uri);
|
2020-04-03 14:53:33 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT(validate_uris[i].full_uri,
|
|
|
|
uri_parser_is_absolute_string(validate_uris[i].uri));
|
2020-03-30 22:46:41 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT(validate_uris[i].expected, res);
|
|
|
|
if (res == 0) {
|
2020-04-01 08:42:25 +02:00
|
|
|
VEC_CHECK(scheme, i, _failure_msg);
|
|
|
|
VEC_CHECK(userinfo, i, _failure_msg);
|
|
|
|
VEC_CHECK(host, i, _failure_msg);
|
2020-10-29 14:25:14 +01:00
|
|
|
VEC_CHECK(ipv6addr, i, _failure_msg);
|
|
|
|
VEC_CHECK(zoneid, i, _failure_msg);
|
2020-04-01 08:42:25 +02:00
|
|
|
VEC_CHECK(port, i, _failure_msg);
|
|
|
|
VEC_CHECK(path, i, _failure_msg);
|
|
|
|
VEC_CHECK(query, i, _failure_msg);
|
2020-03-30 22:46:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 10:03:05 +02:00
|
|
|
static void test_uri_parser__unterminated_string(void)
|
|
|
|
{
|
|
|
|
uri_parser_result_t ures;
|
|
|
|
char uri[64];
|
|
|
|
/* initialize with a non-null character */
|
|
|
|
memset(uri, 'Z', sizeof(uri));
|
|
|
|
|
|
|
|
memcpy(uri, validate_uris[0].uri, strlen(validate_uris[0].uri));
|
|
|
|
|
|
|
|
int res = uri_parser_process(&ures, uri, strlen(validate_uris[0].uri));
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, res);
|
|
|
|
VEC_CHECK(scheme, 0, _failure_msg);
|
|
|
|
VEC_CHECK(userinfo, 0, _failure_msg);
|
|
|
|
VEC_CHECK(host, 0, _failure_msg);
|
2020-10-29 14:25:14 +01:00
|
|
|
VEC_CHECK(ipv6addr, 0, _failure_msg);
|
|
|
|
VEC_CHECK(zoneid, 0, _failure_msg);
|
2020-04-02 10:03:05 +02:00
|
|
|
VEC_CHECK(port, 0, _failure_msg);
|
|
|
|
VEC_CHECK(path, 0, _failure_msg);
|
|
|
|
VEC_CHECK(query, 0, _failure_msg);
|
|
|
|
}
|
|
|
|
|
2020-03-30 22:46:41 +02:00
|
|
|
Test *tests_uri_parser_tests(void)
|
|
|
|
{
|
|
|
|
EMB_UNIT_TESTFIXTURES(fixtures) {
|
|
|
|
new_TestFixture(test_uri_parser__validate),
|
2020-04-02 10:03:05 +02:00
|
|
|
new_TestFixture(test_uri_parser__unterminated_string),
|
2020-03-30 22:46:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
EMB_UNIT_TESTCALLER(uri_parser_tests, NULL, NULL, fixtures);
|
|
|
|
|
|
|
|
return (Test *)&uri_parser_tests;
|
|
|
|
}
|
|
|
|
|
2021-07-30 14:42:23 +02:00
|
|
|
#define PARAMS_NUMOF (4U)
|
|
|
|
#define INIT_URI_RESULTS(q) \
|
|
|
|
_uri_results.query = (q); \
|
|
|
|
_uri_results.query_len = sizeof(q) - 1
|
|
|
|
#define TEST_ASSERT_PARAM(exp, idx, comp) \
|
|
|
|
TEST_ASSERT_EQUAL_INT(sizeof(exp) - 1, _params[idx].comp ## _len); \
|
|
|
|
TEST_ASSERT_MESSAGE(!strncmp(exp, _params[idx].comp, _params[idx].comp ## _len), \
|
|
|
|
#comp " was not " exp);
|
|
|
|
|
|
|
|
static uri_parser_query_param_t _params[4U];
|
|
|
|
static uri_parser_result_t _uri_results;
|
|
|
|
|
|
|
|
static void _setup_query(void)
|
|
|
|
{
|
|
|
|
memset(_params, 0, sizeof(_params));
|
|
|
|
memset(&_uri_results, 0, sizeof(_uri_results));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_split_query__broken_input(void)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
INIT_URI_RESULTS("&");
|
|
|
|
res = uri_parser_split_query(&_uri_results, _params, ARRAY_SIZE(_params));
|
|
|
|
TEST_ASSERT_EQUAL_INT(-1, res);
|
|
|
|
|
|
|
|
INIT_URI_RESULTS("=&");
|
|
|
|
res = uri_parser_split_query(&_uri_results, _params, ARRAY_SIZE(_params));
|
|
|
|
TEST_ASSERT_EQUAL_INT(-1, res);
|
|
|
|
|
|
|
|
INIT_URI_RESULTS("=&&");
|
|
|
|
res = uri_parser_split_query(&_uri_results, _params, ARRAY_SIZE(_params));
|
|
|
|
TEST_ASSERT_EQUAL_INT(-1, res);
|
|
|
|
|
|
|
|
INIT_URI_RESULTS("==");
|
|
|
|
res = uri_parser_split_query(&_uri_results, _params, ARRAY_SIZE(_params));
|
|
|
|
TEST_ASSERT_EQUAL_INT(-1, res);
|
|
|
|
|
|
|
|
INIT_URI_RESULTS("key=value&name=value=another");
|
|
|
|
res = uri_parser_split_query(&_uri_results, _params, ARRAY_SIZE(_params));
|
|
|
|
TEST_ASSERT_EQUAL_INT(-1, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_split_query__truncated(void)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
INIT_URI_RESULTS("this=0&is=1&a=very&long=3&query=foo");
|
|
|
|
TEST_ASSERT_EQUAL_INT(4, ARRAY_SIZE(_params));
|
|
|
|
res = uri_parser_split_query(&_uri_results, _params, ARRAY_SIZE(_params));
|
|
|
|
TEST_ASSERT_EQUAL_INT(-2, res);
|
|
|
|
TEST_ASSERT_PARAM("this", 0, name);
|
|
|
|
TEST_ASSERT_PARAM("0", 0, value);
|
|
|
|
TEST_ASSERT_PARAM("is", 1, name);
|
|
|
|
TEST_ASSERT_PARAM("1", 1, value);
|
|
|
|
TEST_ASSERT_PARAM("a", 2, name);
|
|
|
|
TEST_ASSERT_PARAM("very", 2, value);
|
|
|
|
TEST_ASSERT_PARAM("long", 3, name);
|
|
|
|
TEST_ASSERT_PARAM("3", 3, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_split_query__success(void)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
INIT_URI_RESULTS("foo=&=&bar=1");
|
|
|
|
res = uri_parser_split_query(&_uri_results, _params, ARRAY_SIZE(_params));
|
|
|
|
TEST_ASSERT_EQUAL_INT(3, res);
|
|
|
|
TEST_ASSERT_PARAM("foo", 0, name);
|
|
|
|
TEST_ASSERT_PARAM("", 0, value);
|
|
|
|
TEST_ASSERT_PARAM("", 1, name);
|
|
|
|
TEST_ASSERT_PARAM("", 1, value);
|
|
|
|
TEST_ASSERT_PARAM("bar", 2, name);
|
|
|
|
TEST_ASSERT_PARAM("1", 2, value);
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, _params[3].name_len);
|
|
|
|
TEST_ASSERT_NULL(_params[3].name);
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, _params[3].value_len);
|
|
|
|
TEST_ASSERT_NULL(_params[3].value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Test *tests_query_split_tests(void)
|
|
|
|
{
|
|
|
|
EMB_UNIT_TESTFIXTURES(fixtures) {
|
|
|
|
new_TestFixture(test_split_query__broken_input),
|
|
|
|
new_TestFixture(test_split_query__truncated),
|
|
|
|
new_TestFixture(test_split_query__success),
|
|
|
|
};
|
|
|
|
|
|
|
|
EMB_UNIT_TESTCALLER(query_split_tests, _setup_query, NULL, fixtures);
|
|
|
|
|
|
|
|
return (Test *)&query_split_tests;
|
|
|
|
}
|
|
|
|
|
2020-03-30 22:46:41 +02:00
|
|
|
void tests_uri_parser(void)
|
|
|
|
{
|
|
|
|
TESTS_RUN(tests_uri_parser_tests());
|
2021-07-30 14:42:23 +02:00
|
|
|
TESTS_RUN(tests_query_split_tests());
|
2020-03-30 22:46:41 +02:00
|
|
|
}
|
|
|
|
/** @} */
|