Commit 62c29f82 authored by Kishen Maloor's avatar Kishen Maloor

Merge branch 'master' into fargo

Signed-off-by: Kishen Maloor's avatarKishen Maloor <kishen.maloor@intel.com>
parents 8a659eef 21fe3cb6
Pipeline #222 passed with stage
in 49 seconds
......@@ -21,43 +21,20 @@
#include "oc_api.h"
#include "oc_cloud_internal.h"
#include "oc_rep.h"
#include "oc_config.h"
#ifdef OC_DYNAMIC_ALLOCATION
#include <stdlib.h>
#endif /* OC_DYNAMIC_ALLOCATION */
#ifndef OC_STORAGE
#error Preprocessor macro OC_CLOUD is defined but OC_STORAGE is not defined \
check oc_config.h and make sure OC_STORAGE is defined if OC_CLOUD is defined.
#endif
#define CLOUD_STORE_NAME "cloud"
#define CLOUD_TAG_MAX (32)
#ifndef OC_SECURITY
// dummy storage
int
oc_storage_config(const char *store)
{
(void)store;
return 0;
}
long
oc_storage_read(const char *store, uint8_t *buf, size_t size)
{
(void)store;
(void)buf;
(void)size;
return -1;
}
long
oc_storage_write(const char *store, uint8_t *buf, size_t size)
{
(void)store;
(void)buf;
(void)size;
return -1;
}
#endif
static int cloud_store_load_internal(const char *store_name,
oc_cloud_store_t *store);
static void gen_cloud_tag(const char *name, size_t device, char *cloud_tag);
......
......@@ -496,6 +496,52 @@ oc_string_to_endpoint(oc_string_t *endpoint_str, oc_endpoint_t *endpoint,
return -1;
}
int
oc_endpoint_string_parse_path(oc_string_t *endpoint_str, oc_string_t *path)
{
if (!endpoint_str) {
return -1;
}
if (!path) {
return -1;
}
const char *address = NULL;
address = strstr(oc_string(*endpoint_str), "://");
if(!address) {
return -1;
}
// 3 is string length of "://"
address += 3;
size_t len = oc_string_len(*endpoint_str) - (address - oc_string(*endpoint_str));
// the smallest possible address is '0' anything smaller is invalid.
if(len < 1) {
return -1;
}
/* Extract a uri path if available */
const char *path_start = NULL;
const char *query_start = NULL;
path_start = memchr(address, '/', len);
if (!path_start) {
// no path found return error
return -1;
}
query_start = memchr((address + (path_start - address)), '?',
(len - (path_start - address)));
if (query_start) {
oc_new_string(path, path_start, (query_start - path_start));
} else {
oc_new_string(path, path_start, (len - (path_start - address)));
}
return 0;
}
int
oc_ipv6_endpoint_is_link_local(oc_endpoint_t *endpoint)
{
......
......@@ -22,14 +22,19 @@
#include "oc_introspection_internal.h"
#include <inttypes.h>
#include <stdio.h>
#include "oc_config.h"
#ifndef OC_IDD_API
#include "server_introspection.dat.h"
#else /* OC_IDD_API */
#if !defined(OC_STORAGE) && defined(OC_IDD_API)
#error Preprocessor macro OC_IDD_API is defined but OC_STORAGE is not defined \
check oc_config.h and make sure OC_STORAGE is defined if OC_IDD_API is defined.
#endif
#define MAX_TAG_LENGTH 20
#ifdef OC_SECURITY
static void
gen_idd_tag(const char *name, size_t device_index, char *idd_tag)
{
......@@ -39,19 +44,13 @@ gen_idd_tag(const char *name, size_t device_index, char *idd_tag)
(idd_tag_len < MAX_TAG_LENGTH) ? idd_tag_len + 1 : MAX_TAG_LENGTH;
idd_tag[idd_tag_len] = '\0';
}
#endif /* OC_SECURITY */
void
oc_set_introspection_data(size_t device, uint8_t *IDD, size_t IDD_size)
{
(void) device;
(void) IDD;
(void) IDD_size;
#ifdef OC_SECURITY
char idd_tag[MAX_TAG_LENGTH];
gen_idd_tag("IDD", device, idd_tag);
oc_storage_write(idd_tag, IDD, IDD_size);
#endif /* OC_SECURITY */
}
#endif /*OC_IDD_API*/
......@@ -73,13 +72,11 @@ oc_core_introspection_data_handler(oc_request_t *request,
} else {
IDD_size = -1;
}
#else /* OC_IDD_API */
#ifdef OC_SECURITY
#else /* OC_IDD_API */
char idd_tag[MAX_TAG_LENGTH];
gen_idd_tag("IDD", request->resource->device, idd_tag);
IDD_size = oc_storage_read(
idd_tag, request->response->response_buffer->buffer, OC_MAX_APP_DATA_SIZE);
#endif /* OC_SECURITY */
#endif /* OC_IDD_API */
if (IDD_size >= 0 && IDD_size < OC_MAX_APP_DATA_SIZE) {
......
......@@ -1229,7 +1229,7 @@ oc_ri_find_client_cb_by_token(uint8_t *token, uint8_t token_len)
return cb;
}
static bool
bool
oc_ri_is_client_cb_valid(oc_client_cb_t *client_cb)
{
oc_client_cb_t *cb = oc_list_head(client_cbs);
......@@ -1316,7 +1316,6 @@ oc_ri_invoke_client_cb(void *response, oc_client_cb_t *cb,
struct oc_memb rep_objects = { sizeof(oc_rep_t), 0, 0, 0, 0 };
#endif /* OC_DYNAMIC_ALLOCATION */
oc_rep_set_pool(&rep_objects);
if (payload_len) {
if (cb->discovery) {
if (oc_ri_process_discovery_payload(payload, payload_len, cb->handler,
......@@ -1401,7 +1400,6 @@ oc_ri_invoke_client_cb(void *response, oc_client_cb_t *cb,
}
}
}
return true;
}
......
......@@ -22,6 +22,11 @@
#include "oc_swupdate_internal.h"
#include "security/oc_pstat.h"
#ifndef OC_STORAGE
#error Preprocessor macro OC_SOFTWARE_UPDATE is defined but OC_STORAGE is not defined \
check oc_config.h and make sure OC_STORAGE is defined if OC_SOFTWARE_UPDATE is defined.
#endif
#ifdef OC_DYNAMIC_ALLOCATION
#include "port/oc_assert.h"
#include <stdlib.h>
......
......@@ -18,49 +18,70 @@
#include <cstdlib>
#include "oc_endpoint.h"
#include "oc_helpers.h"
TEST(OCEndpoints, StringToEndpoint)
{
const char *spu[4] = { "coaps://10.211.55.3:56789/a/light",
"coap://openconnectivity.org",
"coap://openconnectivity.org/alpha",
"coaps://openconnectivity.org:3456/alpha" };
for (int i = 0; i < 4; i++) {
#ifdef OC_IPV4
const char *spu0[1] = { "coaps://10.211.55.3:56789/a/light" };
for (int i = 0; i < 1; i++) {
oc_string_t s;
oc_new_string(&s, spu0[i], strlen(spu0[i]));
oc_endpoint_t ep;
memset(&ep, 0, sizeof(oc_endpoint_t));
oc_string_t uri;
memset(&uri, 0, sizeof(oc_string_t));
int ret = oc_string_to_endpoint(&s, &ep, &uri);
EXPECT_EQ(ret, 0) << "spu0[" << i << "] " << spu0[i];
switch (i) {
case 0: {
EXPECT_TRUE(ep.flags & IPV4);
EXPECT_TRUE(ep.flags & SECURED);
EXPECT_FALSE(ep.flags & TCP);
EXPECT_EQ(ep.addr.ipv4.port, 56789);
EXPECT_STREQ(oc_string(uri), "/a/light");
uint8_t addr[4] = { 10, 211, 55, 3 };
EXPECT_EQ(0, memcmp(ep.addr.ipv4.address, addr, 4));
} break;
default:
break;
}
oc_free_string(&s);
oc_free_string(&uri);
}
#endif /* OC_IPV4 */
const char *spu1[3] = { "coap://openconnectivity.org",
"coap://openconnectivity.org/alpha",
"coaps://openconnectivity.org:3456/alpha" };
for (int i = 0; i < 3; i++) {
oc_string_t s;
oc_new_string(&s, spu[i], strlen(spu[i]));
oc_new_string(&s, spu1[i], strlen(spu1[i]));
oc_endpoint_t ep;
memset(&ep, 0, sizeof(oc_endpoint_t));
oc_string_t uri;
memset(&uri, 0, sizeof(oc_string_t));
int ret = oc_string_to_endpoint(&s, &ep, &uri);
EXPECT_EQ(ret, 0) << "spu[" << i << "] " << spu[i];
EXPECT_EQ(ret, 0) << "spu1[" << i << "] " << spu1[i];
switch (i) {
case 0: {
EXPECT_TRUE(ep.flags & IPV4);
EXPECT_TRUE(ep.flags & SECURED);
EXPECT_FALSE(ep.flags & TCP);
EXPECT_EQ(ep.addr.ipv4.port, 56789);
EXPECT_STREQ(oc_string(uri), "/a/light");
uint8_t addr[4] = { 10, 211, 55, 3 };
EXPECT_EQ(0, memcmp(ep.addr.ipv4.address, addr, 4));
} break;
case 1:
case 0:
ASSERT_TRUE((ep.flags & IPV4) || (ep.flags & IPV6));
ASSERT_FALSE(ep.flags & SECURED);
ASSERT_FALSE(ep.flags & TCP);
EXPECT_EQ(ep.addr.ipv4.port, 5683);
EXPECT_EQ(oc_string_len(uri), 0);
break;
case 2:
case 1:
ASSERT_TRUE((ep.flags & IPV4) || (ep.flags & IPV6));
ASSERT_FALSE(ep.flags & SECURED);
ASSERT_FALSE(ep.flags & TCP);
EXPECT_EQ(ep.addr.ipv4.port, 5683);
EXPECT_STREQ(oc_string(uri), "/alpha");
break;
case 3:
case 2:
ASSERT_TRUE((ep.flags & IPV4) || (ep.flags & IPV6));
ASSERT_TRUE(ep.flags & SECURED);
ASSERT_FALSE(ep.flags & TCP);
......@@ -75,50 +96,70 @@ TEST(OCEndpoints, StringToEndpoint)
}
#ifdef OC_TCP
const char *spu2[6] = { "coaps+tcp://10.211.55.3/a/light",
"coap+tcp://1.2.3.4:2568",
"coaps+tcp://openconnectivity.org:3456",
#ifdef OC_IPV4
const char *spu2[2] = { "coaps+tcp://10.211.55.3/a/light",
"coap+tcp://1.2.3.4:2568"};
for (int i = 0; i < 2; i++) {
oc_string_t s;
oc_new_string(&s, spu2[i], strlen(spu2[i]));
oc_endpoint_t ep;
memset(&ep, 0, sizeof(oc_endpoint_t));
oc_string_t uri;
memset(&uri, 0, sizeof(oc_string_t));
int ret = oc_string_to_endpoint(&s, &ep, &uri);
EXPECT_EQ(ret, 0) << "spu2[" << i << "] " << spu2[i];
switch (i) {
case 0: {
EXPECT_TRUE(ep.flags & IPV4);
EXPECT_TRUE(ep.flags & SECURED);
EXPECT_TRUE(ep.flags & TCP);
EXPECT_EQ(ep.addr.ipv4.port, 5684);
EXPECT_STREQ(oc_string(uri), "/a/light");
uint8_t addr[4] = { 10, 211, 55, 3 };
EXPECT_EQ(0, memcmp(ep.addr.ipv4.address, addr, 4));
} break;
case 1: {
EXPECT_TRUE(ep.flags & IPV4);
EXPECT_FALSE(ep.flags & SECURED);
EXPECT_TRUE(ep.flags & TCP);
EXPECT_EQ(ep.addr.ipv4.port, 2568);
EXPECT_EQ(oc_string_len(uri), 0);
uint8_t addr[4] = { 1, 2, 3, 4 };
EXPECT_EQ(0, memcmp(ep.addr.ipv4.address, addr, 4));
} break;
default:
break;
}
oc_free_string(&s);
oc_free_string(&uri);
}
#endif /* OC_IPV4 */
const char *spu3[4] = { "coaps+tcp://openconnectivity.org:3456",
"coap+tcp://[ff02::158]",
"coaps+tcp://[ff02::158]/a/light",
"coaps+tcp://[fe80::12]:2439/a/light" };
for (int i = 0; i < 6; i++) {
for (int i = 0; i < 4; i++) {
oc_string_t s;
oc_new_string(&s, spu2[i], strlen(spu2[i]));
oc_new_string(&s, spu3[i], strlen(spu3[i]));
oc_endpoint_t ep;
memset(&ep, 0, sizeof(oc_endpoint_t));
oc_string_t uri;
memset(&uri, 0, sizeof(oc_string_t));
int ret = oc_string_to_endpoint(&s, &ep, &uri);
EXPECT_EQ(ret, 0) << "spu2[" << i << "] " << spu2[i];
EXPECT_EQ(ret, 0) << "spu3[" << i << "] " << spu3[i];
switch (i) {
case 0: {
EXPECT_TRUE(ep.flags & IPV4);
EXPECT_TRUE(ep.flags & SECURED);
EXPECT_TRUE(ep.flags & TCP);
EXPECT_EQ(ep.addr.ipv4.port, 5684);
EXPECT_STREQ(oc_string(uri), "/a/light");
uint8_t addr[4] = { 10, 211, 55, 3 };
EXPECT_EQ(0, memcmp(ep.addr.ipv4.address, addr, 4));
} break;
case 1: {
EXPECT_TRUE(ep.flags & IPV4);
EXPECT_FALSE(ep.flags & SECURED);
EXPECT_TRUE(ep.flags & TCP);
EXPECT_EQ(ep.addr.ipv4.port, 2568);
EXPECT_EQ(oc_string_len(uri), 0);
uint8_t addr[4] = { 1, 2, 3, 4 };
EXPECT_EQ(0, memcmp(ep.addr.ipv4.address, addr, 4));
} break;
case 2:
case 0:
ASSERT_TRUE((ep.flags & IPV4) || (ep.flags & IPV6));
ASSERT_TRUE(ep.flags & SECURED);
ASSERT_TRUE(ep.flags & TCP);
EXPECT_EQ(ep.addr.ipv4.port, 3456);
EXPECT_EQ(oc_string_len(uri), 0);
break;
case 3: {
case 1: {
ASSERT_TRUE(ep.flags & IPV6);
ASSERT_FALSE(ep.flags & SECURED);
ASSERT_TRUE(ep.flags & TCP);
......@@ -128,7 +169,7 @@ TEST(OCEndpoints, StringToEndpoint)
0, 0, 0, 0, 0, 0, 0x01, 0x58 };
EXPECT_EQ(0, memcmp(ep.addr.ipv6.address, addr, 16));
} break;
case 4: {
case 2: {
ASSERT_TRUE(ep.flags & IPV6);
ASSERT_TRUE(ep.flags & SECURED);
ASSERT_TRUE(ep.flags & TCP);
......@@ -138,7 +179,7 @@ TEST(OCEndpoints, StringToEndpoint)
EXPECT_EQ(0, memcmp(ep.addr.ipv6.address, addr, 16));
EXPECT_STREQ(oc_string(uri), "/a/light");
} break;
case 5: {
case 3: {
ASSERT_TRUE(ep.flags & IPV6);
ASSERT_TRUE(ep.flags & SECURED);
ASSERT_TRUE(ep.flags & TCP);
......@@ -156,19 +197,141 @@ TEST(OCEndpoints, StringToEndpoint)
}
// test dns lookup when uri is NULL
const char *spu3[4] = { "coap://10.211.55.3:56789/a/light",
const char *spu4[4] = { "coap://10.211.55.3:56789/a/light",
"coaps+tcp://10.211.55.3/a/light",
"coap://openconnectivity.org/alpha",
"coaps://openconnectivity.org:3456/alpha" };
for (int i = 0; i < 4; i++) {
oc_string_t s;
oc_new_string(&s, spu3[i], strlen(spu[i]));
oc_new_string(&s, spu4[i], strlen(spu4[i]));
oc_endpoint_t ep;
memset(&ep, 0, sizeof(oc_endpoint_t));
int ret = oc_string_to_endpoint(&s, &ep, NULL);
EXPECT_EQ(ret, 0) << "spu3[" << i << "] " << spu3[i];
EXPECT_EQ(ret, 0) << "spu4[" << i << "] " << spu4[i];
}
#endif
}
TEST(OCEndpoints, EndpointStringParsePath)
{
const char *spu[12] = { "coaps://10.211.55.3:56789/a/light",
"coap://openconnectivity.org",
"coap://openconnectivity.org/alpha",
"coaps://openconnectivity.org:3456/alpha",
"coaps+tcp://10.211.55.3/a/light",
"coap+tcp://1.2.3.4:2568",
"coaps+tcp://openconnectivity.org:3456",
"coap+tcp://[ff02::158]",
"coaps+tcp://[ff02::158]/a/light",
"coaps+tcp://[fe80::12]:2439/a/light",
"coaps+tcp://[fe80::12]:2439/a/light?s=100",
"coap://0/foo"};
for (int i = 0; i < 12; i++) {
oc_string_t s;
oc_string_t path;
int ret = -1;
oc_new_string(&s, spu[i], strlen(spu[i]));
memset(&path, 0, sizeof(oc_string_t));
switch (i) {
case 0:
ret = oc_endpoint_string_parse_path(&s, &path);
EXPECT_EQ(0, ret) << "spu[" << i << "] " << spu[i];
EXPECT_STREQ(oc_string(path), "/a/light");
break;
case 1:
ret = oc_endpoint_string_parse_path(&s, &path);
EXPECT_EQ(-1, ret) << "spu[" << i << "] " << spu[i];
EXPECT_EQ(path.ptr, NULL);
break;
case 2:
ret = oc_endpoint_string_parse_path(&s, &path);
EXPECT_EQ(0, ret) << "spu[" << i << "] " << spu[i];
EXPECT_STREQ(oc_string(path), "/alpha");
break;
case 3:
ret = oc_endpoint_string_parse_path(&s, &path);
EXPECT_EQ(0, ret) << "spu[" << i << "] " << spu[i];
EXPECT_STREQ(oc_string(path), "/alpha");
break;
case 4:
ret = oc_endpoint_string_parse_path(&s, &path);
EXPECT_EQ(0, ret) << "spu[" << i << "] " << spu[i];
EXPECT_STREQ(oc_string(path), "/a/light");
break;
case 5:
ret = oc_endpoint_string_parse_path(&s, &path);
EXPECT_EQ(-1, ret) << "spu[" << i << "] " << spu[i];
EXPECT_EQ(path.ptr, NULL);
break;
case 6:
ret = oc_endpoint_string_parse_path(&s, &path);
EXPECT_EQ(-1, ret) << "spu[" << i << "] " << spu[i];
EXPECT_EQ(path.ptr, NULL);
break;
case 7:
ret = oc_endpoint_string_parse_path(&s, &path);
EXPECT_EQ(-1, ret) << "spu[" << i << "] " << spu[i];
EXPECT_EQ(path.ptr, NULL);
break;
case 8:
ret = oc_endpoint_string_parse_path(&s, &path);
EXPECT_EQ(0, ret) << "spu[" << i << "] " << spu[i];
EXPECT_STREQ(oc_string(path), "/a/light");
break;
case 9:
ret = oc_endpoint_string_parse_path(&s, &path);
EXPECT_EQ(0, ret) << "spu[" << i << "] " << spu[i];
EXPECT_STREQ(oc_string(path), "/a/light");
break;
case 10:
ret = oc_endpoint_string_parse_path(&s, &path);
EXPECT_EQ(0, ret) << "spu[" << i << "] " << spu[i];
EXPECT_STREQ(oc_string(path), "/a/light");
break;
case 11:
ret = oc_endpoint_string_parse_path(&s, &path);
EXPECT_EQ(0, ret) << "spu[" << i << "] " << spu[i];
EXPECT_STREQ(oc_string(path), "/foo");
break;
default:
break;
}
}
// paths with expected errors
const char *spu2[2] = { "coaps://", // no address
"coaps:/10.211.55.3:56789/a/light" // missing ://
};
for (int i = 0; i < 2; i++) {
oc_string_t s;
oc_new_string(&s, spu2[i], strlen(spu2[i]));
oc_string_t path;
memset(&path, 0, sizeof(oc_string_t));
int ret = oc_endpoint_string_parse_path(&s, &path);
EXPECT_EQ(-1, ret) << "spu2[" << i << "] " << spu2[i];
oc_free_string(&s);
oc_free_string(&path);
}
{
oc_string_t path;
int ret = oc_endpoint_string_parse_path(NULL, &path);
EXPECT_EQ(-1, ret);
if (-1 != ret) {
// If code is working as expected this should never run.
oc_free_string(&path);
}
}
{
oc_string_t s;
oc_new_string(&s, "coap://0/p", strlen("coap://0/p"));
EXPECT_EQ(-1, oc_endpoint_string_parse_path(&s, NULL));
oc_free_string(&s);
}
}
......@@ -176,9 +176,9 @@ main(void)
oc_clock_time_t next_event;
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./client_block_linux_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
oc_set_mtu_size(300);
oc_set_max_app_data_size(2048);
......
......@@ -427,9 +427,9 @@ main(void)
oc_clock_time_t next_event;
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./client_collections_linux_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
init = oc_main_init(&handler);
if (init < 0)
......
......@@ -163,9 +163,9 @@ main(void)
oc_clock_time_t next_event;
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./client_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
oc_set_con_res_announced(false);
init = oc_main_init(&handler);
......
......@@ -394,9 +394,9 @@ main(void)
.signal_event_loop =
signal_event_loop };
#ifdef OC_SECURITY
#ifdef OC_STORAGE
oc_storage_config("./client_multithread_linux_creds");
#endif /* OC_SECURITY */
#endif /* OC_STORAGE */
if (pthread_mutex_init(&mutex, NULL) < 0) {
printf("pthread_mutex_init failed!\n");
......
......@@ -565,7 +565,9 @@ ocf_event_thread(void *data)
.register_resources =
register_resources };
#ifdef OC_STORAGE
oc_storage_config("./cloud_tests_creds");
#endif /* OC_STORAGE */
if (pthread_mutex_init(&mutex, NULL) < 0) {
printf("pthread_mutex_init failed!\n");
......
......@@ -296,7 +296,9 @@ main(void)
.signal_event_loop = signal_event_loop,
.register_resources =
register_resources };
#ifdef OC_STORAGE
oc_storage_config("./cloud_server_creds/");
#endif /* OC_STORAGE */
ret = oc_main_init(&handler);