diff --git a/api/oc_endpoint.c b/api/oc_endpoint.c index 5e873cd7cb..a021a9bbab 100644 --- a/api/oc_endpoint.c +++ b/api/oc_endpoint.c @@ -194,11 +194,11 @@ oc_endpoint_to_string(oc_endpoint_t *endpoint, oc_string_t *endpoint_str) } #ifdef OC_IPV4 -static void +static int oc_parse_ipv4_address(const char *address, size_t len, oc_endpoint_t *endpoint) { if (!address || !endpoint) { - return; + return -1; } uint8_t *addr = endpoint->addr.ipv4.address; size_t str_idx = 0, addr_idx = 0; @@ -209,6 +209,7 @@ oc_parse_ipv4_address(const char *address, size_t len, oc_endpoint_t *endpoint) str_idx += next_seg - &address[str_idx] + 1; } } + return 0; } #endif /* OC_IPV4 */ @@ -284,11 +285,11 @@ hex_to_bin(const char *hex, size_t len) return b; } -static void +static int oc_parse_ipv6_address(const char *address, size_t len, oc_endpoint_t *endpoint) { if (!address || !endpoint) { - return; + return -1; } uint8_t *addr = endpoint->addr.ipv6.address; memset(addr, 0, OC_IPV6_ADDRLEN); @@ -331,7 +332,8 @@ oc_parse_ipv6_address(const char *address, size_t len, oc_endpoint_t *endpoint) str_idx++; } break; default: - break; + OC_ERR("invalid ipv6 address segment(%s)", next_seg); + return -1; } } if (split != -1) { @@ -344,6 +346,7 @@ oc_parse_ipv6_address(const char *address, size_t len, oc_endpoint_t *endpoint) addr_idx--; } } + return 0; } typedef struct endpoint_uri_t @@ -546,13 +549,19 @@ oc_parse_endpoint_string(oc_string_t *endpoint_str, oc_endpoint_t *endpoint, if (host_len > 1 && address[0] == '[' && address[host_len - 1] == ']') { endpoint->flags = ep_uri.scheme_flags | IPV6; endpoint->addr.ipv6.port = ep_uri.port; - oc_parse_ipv6_address(&address[1], host_len - 2, endpoint); + if (oc_parse_ipv6_address(&address[1], host_len - 2, endpoint) != 0) { + OC_ERR("invalid ipv6 address(%s)", address); + return -1; + } } #ifdef OC_IPV4 else { endpoint->flags = ep_uri.scheme_flags | IPV4; endpoint->addr.ipv4.port = ep_uri.port; - oc_parse_ipv4_address(address, host_len, endpoint); + if (oc_parse_ipv4_address(address, host_len, endpoint) != 0) { + OC_ERR("invalid ipv4 address(%s)", address); + return -1; + } } #else /* OC_IPV4 */ else { diff --git a/api/unittest/eptest.cpp b/api/unittest/eptest.cpp index 59f1f58675..07ff140f70 100644 --- a/api/unittest/eptest.cpp +++ b/api/unittest/eptest.cpp @@ -25,13 +25,26 @@ #include #endif /* _WIN32 */ -TEST(OCEndpoints, StringToEndpoint) -{ +class TestOCEndpoint : public testing::Test { +protected: + void SetUp() override + { #ifdef _WIN32 - WSADATA wsaData; - WSAStartup(MAKEWORD(2, 2), &wsaData); + WSADATA wsaData; + WSAStartup(MAKEWORD(2, 2), &wsaData); #endif /* _WIN32 */ + } + + void TearDown() override + { +#ifdef _WIN32 + WSACleanup(); +#endif /* _WIN32 */ + } +}; +TEST_F(TestOCEndpoint, StringToEndpointBadFormat) +{ /* bad format */ std::vector espu = { nullptr, @@ -59,7 +72,10 @@ TEST(OCEndpoints, StringToEndpoint) oc_free_string(&s); oc_free_string(&uri); } +} +TEST_F(TestOCEndpoint, StringToEndpointCoap) +{ #ifdef OC_IPV4 std::vector spu0 = { "coaps://10.211.55.3:56789/a/light" }; for (size_t i = 0; i < spu0.size(); i++) { @@ -113,9 +129,6 @@ TEST(OCEndpoints, StringToEndpoint) memset(&empty, 0, sizeof(oc_endpoint_t)); EXPECT_TRUE(memcmp(&empty, &ep, sizeof(oc_endpoint_t)) == 0); EXPECT_EQ(nullptr, oc_string(uri)); -#ifdef _WIN32 - WSACleanup(); -#endif /* _WIN32 */ oc_free_string(&s); oc_free_string(&uri); continue; @@ -161,8 +174,11 @@ TEST(OCEndpoints, StringToEndpoint) oc_free_string(&s); oc_free_string(&uri); } +} #ifdef OC_TCP +TEST_F(TestOCEndpoint, StringToEndpointCoapTcp) +{ #ifdef OC_IPV4 std::vector spu2 = { "coaps+tcp://10.211.55.3/a/light", @@ -279,12 +295,18 @@ TEST(OCEndpoints, StringToEndpoint) oc_free_string(&s); oc_free_string(&uri); } +} +#endif /* OC_TCP */ +TEST_F(TestOCEndpoint, StringToEndpointNoURI) +{ // test dns lookup when uri is NULL std::vector spu4 = { #ifdef OC_IPV4 "coap://10.211.55.3:56789/a/light", +#ifdef OC_TCP "coaps+tcp://10.211.55.3/a/light", +#endif /* OC_TCP */ #endif /* OC_IPV4 */ #if defined(OC_IPV4) || defined(OC_DNS_LOOKUP_IPV6) "coap://openconnectivity.org/alpha", @@ -300,10 +322,22 @@ TEST(OCEndpoints, StringToEndpoint) EXPECT_EQ(ret, 0) << "spu4[" << i << "] " << spu4[i]; oc_free_string(&s); } -#endif /* OC_TCP */ -#ifdef _WIN32 - WSACleanup(); -#endif /* _WIN32 */ +} + +TEST_F(TestOCEndpoint, StringToEndpointWithScope) +{ + std::vector spu5 = { + "coap://[ff02::158%1]", + }; + for (size_t i = 0; i < spu5.size(); i++) { + oc_string_t s; + oc_new_string(&s, spu5[i].c_str(), spu5[i].length()); + oc_endpoint_t ep; + memset(&ep, 0, sizeof(oc_endpoint_t)); + int ret = oc_string_to_endpoint(&s, &ep, NULL); + EXPECT_EQ(ret, 0) << "spu5[" << i << "] " << spu5[i]; + oc_free_string(&s); + } } TEST(OCEndpoints, EndpointStringParsePath)