diff --git a/api/client/unittest/clientcbtest.cpp b/api/client/unittest/clientcbtest.cpp index bebdb2f87..763afe7e1 100644 --- a/api/client/unittest/clientcbtest.cpp +++ b/api/client/unittest/clientcbtest.cpp @@ -259,7 +259,7 @@ TEST_F(TestClientCBWithServer, RemoveAsync) oc_set_delayed_callback(cb, &oc_client_cb_remove_async, 0); EXPECT_TRUE(oc_has_delayed_callback(cb, &oc_client_cb_remove_async, false)); - oc::TestDevice::PoolEventsMsV1(1s); + oc::TestDevice::PoolEventsMsV1(50ms); EXPECT_FALSE(oc_ri_is_client_cb_valid(cb)); } @@ -275,7 +275,7 @@ TEST_F(TestClientCBWithServer, RemoveWithTimeoutAsync) 0); EXPECT_TRUE(oc_has_delayed_callback( cb, &oc_client_cb_remove_with_notify_timeout_async, false)); - oc::TestDevice::PoolEventsMsV1(1s); + oc::TestDevice::PoolEventsMsV1(50ms); EXPECT_TRUE(TestClientCB::responseHandlerInvoked); EXPECT_FALSE(oc_ri_is_client_cb_valid(cb)); diff --git a/api/cloud/oc_cloud.c b/api/cloud/oc_cloud.c index 846b2bc69..b1c486de6 100644 --- a/api/cloud/oc_cloud.c +++ b/api/cloud/oc_cloud.c @@ -158,35 +158,15 @@ cloud_set_cloudconf(oc_cloud_context_t *ctx, const oc_cloud_conf_update_t *data) data->access_token_len); } if (data->ci_server_len > 0) { - oc_set_string(&ctx->store.ci_server, data->ci_server, data->ci_server_len); + oc_cloud_endpoints_reinit( + &ctx->store.ci_servers, + oc_string_view(data->ci_server, data->ci_server_len)); } if (data->sid_len > 0) { oc_set_string(&ctx->store.sid, data->sid, data->sid_len); } } -void -cloud_set_cloudconf2(oc_cloud_context_t *ctx, - const oc_cloud_conf_update2_t *data) -{ - assert(ctx != NULL); - assert(data != NULL); - if (data->ci_servers.endpoints != NULL) { - // TODO - } - if (data->auth_provider.length > 0) { - oc_set_string(&ctx->store.auth_provider, data->auth_provider.data, - data->auth_provider.length); - } - if (data->access_token.length > 0) { - oc_set_string(&ctx->store.access_token, data->access_token.data, - data->access_token.length); - } - if (data->sid.length > 0) { - oc_set_string(&ctx->store.sid, data->sid.data, data->sid.length); - } -} - int oc_cloud_provision_conf_resource_v1(oc_cloud_context_t *ctx, const char *server, size_t server_len, const char *access_token, diff --git a/api/cloud/oc_cloud_apis.c b/api/cloud/oc_cloud_apis.c index 346ba6136..aae1578a0 100644 --- a/api/cloud/oc_cloud_apis.c +++ b/api/cloud/oc_cloud_apis.c @@ -43,8 +43,8 @@ #include -// cloud_deregister might invoke cloud_refresh_token or cloud_login so we might -// have 2 concurrent allocations per device +// cloud_deregister might invoke oc_cloud_do_refresh_token or oc_cloud_do_login +// so we might have 2 concurrent allocations per device OC_MEMB(g_api_params, cloud_api_param_t, OC_MAX_NUM_DEVICES * 2); cloud_api_param_t * @@ -59,39 +59,27 @@ free_api_param(cloud_api_param_t *p) oc_memb_free(&g_api_params, p); } -// bool -// oc_cloud_set_endpoint(oc_cloud_context_t *ctx) -// { -// assert(ctx->cloud_ep != NULL); -// if (!oc_endpoint_is_empty(ctx->cloud_ep)) { -// return true; -// } - -// if (oc_string_to_endpoint(&ctx->store.ci_server, ctx->cloud_ep, NULL) == 0) -// { -// // set device id to cloud endpoint for multiple servers -// ctx->cloud_ep->device = ctx->device; -// return true; -// } - -// return false; -// } - -int -conv_cloud_endpoint(oc_cloud_context_t *ctx) +bool +oc_cloud_set_endpoint(oc_cloud_context_t *ctx) { - int ret = 0; - if (ctx->cloud_ep != NULL && oc_endpoint_is_empty(ctx->cloud_ep)) { - ret = oc_string_to_endpoint(&ctx->store.ci_server, ctx->cloud_ep, NULL); - if (ret == 0) { - // set device id to cloud endpoint for multiple servers - ctx->cloud_ep->device = ctx->device; - } + assert(ctx->cloud_ep != NULL); + if (!oc_endpoint_is_empty(ctx->cloud_ep)) { + return true; + } + + bool success = false; + const oc_string_t *ep_addr = + oc_cloud_endpoint_selected_address(&ctx->store.ci_servers); + if (oc_string_to_endpoint(ep_addr, ctx->cloud_ep, NULL) == 0) { + // set device id to cloud endpoint for multiple servers + ctx->cloud_ep->device = ctx->device; + success = true; + } #ifdef OC_DNS_CACHE - oc_dns_clear_cache(); + oc_dns_clear_cache(); #endif /* OC_DNS_CACHE */ - } - return ret; + + return success; } #ifdef OC_SECURITY @@ -107,8 +95,8 @@ cloud_tls_connected(const oc_endpoint_t *endpoint) #endif /* OC_SECURITY */ int -cloud_register(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, - uint16_t timeout) +oc_cloud_do_register(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, + uint16_t timeout) { if ((ctx->store.status & OC_CLOUD_REGISTERED) != 0) { cb(ctx, ctx->store.status, data); @@ -132,13 +120,9 @@ cloud_register(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, } OC_CLOUD_DBG("try register device %zu", ctx->device); - if (oc_string(ctx->store.ci_server) == NULL || - conv_cloud_endpoint(ctx) != 0) { - cloud_set_last_error(ctx, CLOUD_ERROR_CONNECT); - free_api_param(p); - return -1; + if (!oc_cloud_set_endpoint(ctx)) { + goto error; } - oc_cloud_access_conf_t conf = { .endpoint = ctx->cloud_ep, .device = ctx->device, @@ -154,6 +138,7 @@ cloud_register(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, return 0; } +error: cloud_set_last_error(ctx, CLOUD_ERROR_CONNECT); free_api_param(p); return -1; @@ -165,12 +150,12 @@ oc_cloud_register(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data) if (ctx == NULL || cb == NULL) { return -1; } - return cloud_register(ctx, cb, data, /*timeout*/ 0); + return oc_cloud_do_register(ctx, cb, data, /*timeout*/ 0); } int -cloud_login(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, - uint16_t timeout) +oc_cloud_do_login(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, + uint16_t timeout) { if ((ctx->store.status & OC_CLOUD_LOGGED_IN) != 0) { cb(ctx, ctx->store.status, data); @@ -194,18 +179,17 @@ cloud_login(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, } OC_CLOUD_DBG("try login device %zu", ctx->device); + if (!oc_cloud_set_endpoint(ctx)) { + goto error; + } oc_cloud_access_conf_t conf = { + .endpoint = ctx->cloud_ep, .device = ctx->device, .selected_identity_cred_id = ctx->selected_identity_cred_id, .handler = oc_cloud_login_handler, .user_data = p, .timeout = timeout, }; - if (conv_cloud_endpoint(ctx) != 0) { - goto error; - } - conf.endpoint = ctx->cloud_ep; - if (oc_cloud_access_login(conf, oc_string(ctx->store.uid), oc_string(ctx->store.access_token))) { return 0; @@ -223,7 +207,7 @@ oc_cloud_login(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data) if (ctx == NULL || cb == NULL) { return -1; } - return cloud_login(ctx, cb, data, /*timeout*/ 0); + return oc_cloud_do_login(ctx, cb, data, /*timeout*/ 0); } int @@ -263,8 +247,8 @@ cloud_logout_internal(oc_client_response_t *data) } int -cloud_logout(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, - uint16_t timeout) +oc_cloud_do_logout(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, + uint16_t timeout) { if ((ctx->store.status & OC_CLOUD_LOGGED_IN) == 0) { OC_CLOUD_ERR("invalid cloud status(%d)", (int)ctx->store.status); @@ -281,17 +265,17 @@ cloud_logout(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, p->timeout = timeout; OC_CLOUD_DBG("try logout device %zu", ctx->device); + if (!oc_cloud_set_endpoint(ctx)) { + goto error; + } oc_cloud_access_conf_t conf = { + .endpoint = ctx->cloud_ep, .device = ctx->device, .selected_identity_cred_id = ctx->selected_identity_cred_id, .handler = cloud_logout_internal, .user_data = p, .timeout = timeout, }; - if (conv_cloud_endpoint(ctx) != 0) { - goto error; - } - conf.endpoint = ctx->cloud_ep; if (oc_cloud_access_logout(conf, oc_string(ctx->store.uid), oc_string(ctx->store.access_token))) { return 0; @@ -309,7 +293,7 @@ oc_cloud_logout(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data) if (ctx == NULL || cb == NULL) { return -1; } - return cloud_logout(ctx, cb, data, /*timeout*/ 0); + return oc_cloud_do_logout(ctx, cb, data, /*timeout*/ 0); } int @@ -322,8 +306,8 @@ oc_cloud_deregister(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data) } int -cloud_refresh_token(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, - uint16_t timeout) +oc_cloud_do_refresh_token(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, + uint16_t timeout) { if ((ctx->store.status & OC_CLOUD_REGISTERED) == 0) { return -1; @@ -340,17 +324,17 @@ cloud_refresh_token(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, p->timeout = timeout; OC_CLOUD_DBG("try refresh token for device %zu", ctx->device); + if (!oc_cloud_set_endpoint(ctx)) { + goto error; + } oc_cloud_access_conf_t conf = { + .endpoint = ctx->cloud_ep, .device = ctx->device, .selected_identity_cred_id = ctx->selected_identity_cred_id, .handler = oc_cloud_refresh_token_handler, .user_data = p, .timeout = timeout, }; - if (conv_cloud_endpoint(ctx) != 0) { - goto error; - } - conf.endpoint = ctx->cloud_ep; if (oc_cloud_access_refresh_access_token( conf, oc_string(ctx->store.auth_provider), oc_string(ctx->store.uid), oc_string(ctx->store.refresh_token))) { @@ -369,7 +353,7 @@ oc_cloud_refresh_token(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data) if (!ctx || !cb) { return -1; } - return cloud_refresh_token(ctx, cb, data, 0); + return oc_cloud_do_refresh_token(ctx, cb, data, 0); } int diff --git a/api/cloud/oc_cloud_apis_internal.h b/api/cloud/oc_cloud_apis_internal.h index 4a83ba976..12acb64ee 100644 --- a/api/cloud/oc_cloud_apis_internal.h +++ b/api/cloud/oc_cloud_apis_internal.h @@ -33,16 +33,21 @@ extern "C" { #endif -int conv_cloud_endpoint(oc_cloud_context_t *ctx) OC_NONNULL(); +/** @brief Set cloud endpoint from currently selected cloud server address */ +bool oc_cloud_set_endpoint(oc_cloud_context_t *ctx) OC_NONNULL(); -int cloud_register(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, - uint16_t timeout) OC_NONNULL(1, 2); -int cloud_login(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, - uint16_t timeout) OC_NONNULL(1, 2); -int cloud_logout(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, - uint16_t timeout) OC_NONNULL(1, 2); -int cloud_refresh_token(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, - uint16_t timeout) OC_NONNULL(1, 2); +/** Execute cloud sign up */ +int oc_cloud_do_register(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, + uint16_t timeout) OC_NONNULL(1, 2); +/** Execute cloud sign in */ +int oc_cloud_do_login(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, + uint16_t timeout) OC_NONNULL(1, 2); +/** Execute cloud sign out */ +int oc_cloud_do_logout(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, + uint16_t timeout) OC_NONNULL(1, 2); +/** Execute refreshing of the cloud access token */ +int oc_cloud_do_refresh_token(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, + void *data, uint16_t timeout) OC_NONNULL(1, 2); /** * @brief Send a ping over the cloud connected connection diff --git a/api/cloud/oc_cloud_context.c b/api/cloud/oc_cloud_context.c index 8bb380902..9241d1aef 100644 --- a/api/cloud/oc_cloud_context.c +++ b/api/cloud/oc_cloud_context.c @@ -139,7 +139,9 @@ oc_cloud_get_at(const oc_cloud_context_t *ctx) const char * oc_cloud_get_cis(const oc_cloud_context_t *ctx) { - return oc_cloud_endpoint_selected_address(&ctx->store.ci_servers).data; + return oc_string_view2( + oc_cloud_endpoint_selected_address(&ctx->store.ci_servers)) + .data; } const char * diff --git a/api/cloud/oc_cloud_deregister.c b/api/cloud/oc_cloud_deregister.c index 341582bf0..6ae0f5cf3 100644 --- a/api/cloud/oc_cloud_deregister.c +++ b/api/cloud/oc_cloud_deregister.c @@ -182,19 +182,25 @@ cloud_deregister_by_request(cloud_api_param_t *p, uint16_t timeout, oc_cloud_context_t *ctx = p->ctx; OC_CLOUD_DBG("try deregister device %zu by DELETE request", ctx->device); + if (!oc_cloud_set_endpoint(ctx)) { + goto error; + } +#if OC_INFO_IS_ENABLED + const char *ep_str = ""; + oc_string64_t ep = { 0 }; + if (oc_endpoint_to_string64(ctx->cloud_ep, &ep)) { + ep_str = oc_string(ep); + } + OC_CLOUD_INFO("Deregistering from %s", ep_str); +#endif /* OC_INFO_IS_ENABLED */ oc_cloud_access_conf_t conf = { + .endpoint = ctx->cloud_ep, .device = ctx->device, .selected_identity_cred_id = ctx->selected_identity_cred_id, .handler = cloud_deregistered_internal, .user_data = p, .timeout = timeout, }; - if (oc_string(ctx->store.ci_server) == NULL || - conv_cloud_endpoint(ctx) != 0) { - goto error; - } - OC_CLOUD_INFO("Deregistering from %s", oc_string(ctx->store.ci_server)); - conf.endpoint = ctx->cloud_ep; if (oc_cloud_access_deregister( conf, oc_string(ctx->store.uid), useAccessToken ? oc_string(ctx->store.access_token) : NULL)) { @@ -243,7 +249,8 @@ cloud_deregister_refreshed_token_async(void *data) } // long access token -> we must login and then deregister without token - if (cloud_login(p->ctx, cloud_deregister_try_logged_in, p, p->timeout) != 0) { + if (oc_cloud_do_login(p->ctx, cloud_deregister_try_logged_in, p, + p->timeout) != 0) { OC_CLOUD_ERR("Failed to login to cloud for deregister"); free_api_param(p); cloud_context_clear(p->ctx); @@ -321,8 +328,8 @@ cloud_deregister(oc_cloud_context_t *ctx, bool sync, uint16_t timeout, bool hasRefreshToken = cloud_context_has_refresh_token(ctx) && !cloud_context_has_permanent_access_token(ctx); if (hasRefreshToken) { - if (cloud_refresh_token(ctx, cloud_deregister_try_refreshed_token, p, - timeout) != 0) { + if (oc_cloud_do_refresh_token(ctx, cloud_deregister_try_refreshed_token, p, + timeout) != 0) { OC_CLOUD_ERR("Failed to refresh token for deregister"); free_api_param(p); return -1; @@ -331,7 +338,7 @@ cloud_deregister(oc_cloud_context_t *ctx, bool sync, uint16_t timeout, } // otherwise try full log in - if (cloud_login(ctx, cloud_deregister_try_logged_in, p, timeout) != 0) { + if (oc_cloud_do_login(ctx, cloud_deregister_try_logged_in, p, timeout) != 0) { OC_CLOUD_ERR("Failed to login to cloud for deregister"); free_api_param(p); return -1; diff --git a/api/cloud/oc_cloud_endpoint.c b/api/cloud/oc_cloud_endpoint.c index 9897f98e3..03849aa91 100644 --- a/api/cloud/oc_cloud_endpoint.c +++ b/api/cloud/oc_cloud_endpoint.c @@ -80,6 +80,14 @@ oc_cloud_endpoints_deinit(oc_cloud_endpoints_t *ce) } } +bool +oc_cloud_endpoints_reinit(oc_cloud_endpoints_t *ce, + oc_string_view_t default_uri) +{ + oc_cloud_endpoints_deinit(ce); + return oc_cloud_endpoints_init(ce, default_uri); +} + bool oc_cloud_endpoints_is_empty(const oc_cloud_endpoints_t *ce) { @@ -192,11 +200,11 @@ oc_cloud_endpoint_select(oc_cloud_endpoints_t *ce, oc_string_view_t uri) return true; } -oc_string_view_t +const oc_string_t * oc_cloud_endpoint_selected_address(const oc_cloud_endpoints_t *ce) { if (ce->selected == NULL) { - return OC_STRING_VIEW_NULL; + return NULL; } - return oc_string_view2(&ce->selected->endpoint.uri); + return &ce->selected->endpoint.uri; } diff --git a/api/cloud/oc_cloud_endpoint_internal.h b/api/cloud/oc_cloud_endpoint_internal.h index 62806a339..dc18dec90 100644 --- a/api/cloud/oc_cloud_endpoint_internal.h +++ b/api/cloud/oc_cloud_endpoint_internal.h @@ -65,6 +65,10 @@ bool oc_cloud_endpoints_init(oc_cloud_endpoints_t *ce, /** Deinitialize cloud server endpoints */ void oc_cloud_endpoints_deinit(oc_cloud_endpoints_t *ce) OC_NONNULL(); +/** Deinitialize and reinitialize cloud server endpoints */ +bool oc_cloud_endpoints_reinit(oc_cloud_endpoints_t *ce, + oc_string_view_t default_uri) OC_NONNULL(); + /** Check if the list of cloud server endpoints is empty */ bool oc_cloud_endpoints_is_empty(const oc_cloud_endpoints_t *ce) OC_NONNULL(); @@ -106,7 +110,7 @@ bool oc_cloud_endpoint_select(oc_cloud_endpoints_t *ce, oc_string_view_t uri) OC_NONNULL(); /** Get address of the currently selected cloud server endpoint */ -oc_string_view_t oc_cloud_endpoint_selected_address( +const oc_string_t *oc_cloud_endpoint_selected_address( const oc_cloud_endpoints_t *ce) OC_NONNULL(); /** diff --git a/api/cloud/oc_cloud_internal.h b/api/cloud/oc_cloud_internal.h index 654153804..cea7a3817 100644 --- a/api/cloud/oc_cloud_internal.h +++ b/api/cloud/oc_cloud_internal.h @@ -120,31 +120,6 @@ void cloud_set_cloudconf(oc_cloud_context_t *ctx, void oc_cloud_update_by_resource(oc_cloud_context_t *ctx, const oc_cloud_conf_update_t *data); -typedef struct -{ - oc_string_view_t - access_token; /**< Access Token resolved with an auth code. */ - oc_string_view_t auth_provider; /**< Auth Provider ID*/ - oc_string_view_t - sid; /**< OCF Cloud Identity as defined in OCF CNC 2.0 Spec. */ - struct - { - oc_string_view_t selected; /** The selected Cloud Interface Server URL to - which an Enrollee is going to registered */ - oc_string_view_list_item_t *endpoints; /**< Linked list of Cloud Interface - Server URLs. */ - } ci_servers; -} oc_cloud_conf_update2_t; - -/** - * @brief Set cloud configuration. - * - * @param ctx cloud context (cannot be NULL) - * @param data configuration update (cannot be NULL) - */ -void cloud_set_cloudconf2(oc_cloud_context_t *ctx, - const oc_cloud_conf_update2_t *data) OC_NONNULL(); - #ifdef __cplusplus } #endif diff --git a/api/cloud/oc_cloud_manager.c b/api/cloud/oc_cloud_manager.c index 74ce79492..4c4893049 100644 --- a/api/cloud/oc_cloud_manager.c +++ b/api/cloud/oc_cloud_manager.c @@ -434,20 +434,26 @@ cloud_manager_register_async(void *data) } OC_CLOUD_DBG("try register(%d)", ctx->retry_count); + if (!oc_cloud_set_endpoint(ctx)) { + OC_CLOUD_ERR("invalid cloud server"); + goto retry; + } +#if OC_INFO_IS_ENABLED + const char *ep_str = ""; + oc_string64_t ep = { 0 }; + if (oc_endpoint_to_string64(ctx->cloud_ep, &ep)) { + ep_str = oc_string(ep); + } + OC_CLOUD_INFO("Registering to %s", ep_str); +#endif /* OC_INFO_IS_ENABLED */ oc_cloud_access_conf_t conf = { + .endpoint = ctx->cloud_ep, .device = ctx->device, .selected_identity_cred_id = ctx->selected_identity_cred_id, .handler = cloud_manager_register_handler, .user_data = data, .timeout = ctx->schedule_action.timeout, }; - if (oc_string(ctx->store.ci_server) == NULL || - conv_cloud_endpoint(ctx) != 0) { - OC_CLOUD_ERR("invalid cloud server"); - goto retry; - } - OC_CLOUD_INFO("Registering to %s", oc_string(ctx->store.ci_server)); - conf.endpoint = ctx->cloud_ep; if (!oc_cloud_access_register(conf, oc_string(ctx->store.auth_provider), NULL, oc_string(ctx->store.uid), oc_string(ctx->store.access_token))) { @@ -660,22 +666,27 @@ cloud_manager_login_async(void *data) return OC_EVENT_DONE; } - OC_CLOUD_DBG("try login (%d)", ctx->retry_count); - + OC_CLOUD_DBG("try login(%d)", ctx->retry_count); + if (!oc_cloud_set_endpoint(ctx)) { + OC_CLOUD_ERR("invalid cloud server"); + goto retry; + } +#if OC_INFO_IS_ENABLED + const char *ep_str = ""; + oc_string64_t ep = { 0 }; + if (oc_endpoint_to_string64(ctx->cloud_ep, &ep)) { + ep_str = oc_string(ep); + } + OC_CLOUD_INFO("Login to %s", ep_str); +#endif /* OC_INFO_IS_ENABLED */ oc_cloud_access_conf_t conf = { + .endpoint = ctx->cloud_ep, .device = ctx->device, .selected_identity_cred_id = ctx->selected_identity_cred_id, .handler = cloud_manager_login_handler, .user_data = ctx, .timeout = ctx->schedule_action.timeout, }; - if (oc_string(ctx->store.ci_server) == NULL || - conv_cloud_endpoint(ctx) != 0) { - OC_CLOUD_ERR("invalid cloud server"); - goto retry; - } - OC_CLOUD_INFO("Login to %s", oc_string(ctx->store.ci_server)); - conf.endpoint = ctx->cloud_ep; if (!oc_cloud_access_login(conf, oc_string(ctx->store.uid), oc_string(ctx->store.access_token))) { OC_CLOUD_ERR("failed to sent sign in request to cloud"); @@ -886,23 +897,28 @@ cloud_manager_refresh_token_async(void *data) return OC_EVENT_DONE; } oc_remove_delayed_callback(ctx, cloud_manager_send_ping_async); - OC_CLOUD_DBG("try refresh token(%d)", ctx->retry_refresh_token_count); + OC_CLOUD_DBG("try refresh token(%d)", ctx->retry_refresh_token_count); + if (!oc_cloud_set_endpoint(ctx)) { + OC_CLOUD_ERR("invalid cloud server"); + goto retry; + } +#if OC_INFO_IS_ENABLED + const char *ep_str = ""; + oc_string64_t ep = { 0 }; + if (oc_endpoint_to_string64(ctx->cloud_ep, &ep)) { + ep_str = oc_string(ep); + } + OC_CLOUD_INFO("Refreshing access token for %s", ep_str); +#endif /* OC_INFO_IS_ENABLED */ oc_cloud_access_conf_t conf = { + .endpoint = ctx->cloud_ep, .device = ctx->device, .selected_identity_cred_id = ctx->selected_identity_cred_id, .handler = cloud_manager_refresh_token_handler, .user_data = ctx, .timeout = ctx->schedule_action.timeout, }; - if (oc_string(ctx->store.ci_server) == NULL || - conv_cloud_endpoint(ctx) != 0) { - OC_CLOUD_ERR("invalid cloud server"); - goto retry; - } - OC_CLOUD_INFO("Refreshing access token for %s", - oc_string(ctx->store.ci_server)); - conf.endpoint = ctx->cloud_ep; if (!oc_cloud_access_refresh_access_token( conf, oc_string(ctx->store.auth_provider), oc_string(ctx->store.uid), oc_string(ctx->store.refresh_token))) { diff --git a/api/cloud/oc_cloud_resource.c b/api/cloud/oc_cloud_resource.c index 9670ecffc..bdf5fe90b 100644 --- a/api/cloud/oc_cloud_resource.c +++ b/api/cloud/oc_cloud_resource.c @@ -65,8 +65,10 @@ cloud_encode(const oc_cloud_context_t *ctx) oc_rep_set_text_string_v1(root, apn, auth_provider.data, auth_provider.length); - oc_string_view_t cis = oc_string_view2(&ctx->store.ci_server); + oc_string_view_t cis = + oc_string_view2(oc_cloud_endpoint_selected_address(&ctx->store.ci_servers)); oc_rep_set_text_string_v1(root, cis, cis.data, cis.length); + // TODO add array of cis servers oc_string_view_t sid = oc_string_view2(&ctx->store.sid); oc_rep_set_text_string_v1(root, sid, sid.data, sid.length); diff --git a/api/cloud/oc_cloud_store.c b/api/cloud/oc_cloud_store.c index 79a9b45f3..a5ccfa134 100644 --- a/api/cloud/oc_cloud_store.c +++ b/api/cloud/oc_cloud_store.c @@ -77,8 +77,9 @@ void oc_cloud_store_encode(const oc_cloud_store_t *store) { oc_rep_start_root_object(); - rep_set_text_string(oc_rep_object(root), OC_STRING_VIEW(CLOUD_CI_SERVER), - oc_cloud_endpoint_selected_address(&store->ci_servers)); + rep_set_text_string( + oc_rep_object(root), OC_STRING_VIEW(CLOUD_CI_SERVER), + oc_string_view2(oc_cloud_endpoint_selected_address(&store->ci_servers))); rep_set_text_string(oc_rep_object(root), OC_STRING_VIEW(CLOUD_AUTH_PROVIDER), oc_string_view2(&store->auth_provider)); rep_set_text_string(oc_rep_object(root), OC_STRING_VIEW(CLOUD_UID), @@ -350,8 +351,8 @@ void oc_cloud_store_initialize(oc_cloud_store_t *store) { oc_cloud_store_deinitialize(store); - oc_set_string(&store->ci_server, OCF_COAPCLOUDCONF_DEFAULT_CIS, - OC_CHAR_ARRAY_LEN(OCF_COAPCLOUDCONF_DEFAULT_CIS)); + // oc_set_string(&store->ci_server, OCF_COAPCLOUDCONF_DEFAULT_CIS, + // OC_CHAR_ARRAY_LEN(OCF_COAPCLOUDCONF_DEFAULT_CIS)); oc_cloud_endpoints_init(&store->ci_servers, OC_STRING_VIEW(OCF_COAPCLOUDCONF_DEFAULT_CIS)); oc_set_string(&store->sid, OCF_COAPCLOUDCONF_DEFAULT_SID, diff --git a/api/cloud/unittest/cloud_endpoint_test.cpp b/api/cloud/unittest/cloud_endpoint_test.cpp index 4c26eb758..913cda26a 100644 --- a/api/cloud/unittest/cloud_endpoint_test.cpp +++ b/api/cloud/unittest/cloud_endpoint_test.cpp @@ -39,7 +39,8 @@ TEST_F(TestCloudEndpoint, InitWithDefault) oc_cloud_endpoints_init(&ce, OC_STRING_VIEW("/default")); EXPECT_TRUE(oc_cloud_endpoint_exists(&ce, OC_STRING_VIEW("/default"))); auto selected = oc_cloud_endpoint_selected_address(&ce); - EXPECT_STREQ("/default", selected.data); + ASSERT_NE(nullptr, selected); + EXPECT_STREQ("/default", oc_string(*selected)); oc_cloud_endpoints_deinit(&ce); } @@ -122,12 +123,14 @@ TEST_F(TestCloudEndpoint, Select) EXPECT_TRUE(oc_cloud_endpoint_select(&ce, uri1)); auto selected = oc_cloud_endpoint_selected_address(&ce); - EXPECT_STREQ(uri1.data, selected.data); + ASSERT_NE(nullptr, selected); + EXPECT_STREQ(uri1.data, oc_string(*selected)); // non-existing URI shouldn't change the selection EXPECT_FALSE(oc_cloud_endpoint_select(&ce, OC_STRING_VIEW("/fail"))); selected = oc_cloud_endpoint_selected_address(&ce); - EXPECT_STREQ(uri1.data, selected.data); + ASSERT_NE(nullptr, selected); + EXPECT_STREQ(uri1.data, oc_string(*selected)); #ifdef OC_DYNAMIC_ALLOCATION auto uri2 = OC_STRING_VIEW("/uri/2"); @@ -137,11 +140,13 @@ TEST_F(TestCloudEndpoint, Select) EXPECT_TRUE(oc_cloud_endpoint_select(&ce, uri2)); selected = oc_cloud_endpoint_selected_address(&ce); - EXPECT_STREQ(uri2.data, selected.data); + ASSERT_NE(nullptr, selected); + EXPECT_STREQ(uri2.data, oc_string(*selected)); EXPECT_TRUE(oc_cloud_endpoint_select(&ce, uri3)); selected = oc_cloud_endpoint_selected_address(&ce); - EXPECT_STREQ(uri3.data, selected.data); + ASSERT_NE(nullptr, selected); + EXPECT_STREQ(uri3.data, oc_string(*selected)); #endif /* OC_DYNAMIC_ALLOCATION */ } @@ -153,9 +158,10 @@ TEST_F(TestCloudEndpoint, RemoveSelected) ASSERT_NE(nullptr, oc_cloud_endpoint_add(&ce, uri1)); EXPECT_TRUE(oc_cloud_endpoint_select(&ce, uri1)); auto selected = oc_cloud_endpoint_selected_address(&ce); - EXPECT_STREQ(uri1.data, selected.data); + ASSERT_NE(nullptr, selected); + EXPECT_STREQ(uri1.data, oc_string(*selected)); EXPECT_TRUE(oc_cloud_endpoint_remove(&ce, uri1)); selected = oc_cloud_endpoint_selected_address(&ce); - EXPECT_EQ(nullptr, selected.data); + EXPECT_EQ(nullptr, selected); } diff --git a/api/cloud/unittest/cloud_manager_test.cpp b/api/cloud/unittest/cloud_manager_test.cpp index 63c13e624..b5a2b699d 100644 --- a/api/cloud/unittest/cloud_manager_test.cpp +++ b/api/cloud/unittest/cloud_manager_test.cpp @@ -63,7 +63,7 @@ class TestCloudManager : public testing::Test { { oc_free_string(&m_context.store.refresh_token); oc_free_string(&m_context.store.access_token); - // oc_free_string(&m_context.store.ci_server); + oc_free_string(&m_context.store.ci_server); oc_free_endpoint(m_context.cloud_ep); oc_free_string(&m_context.store.uid); diff --git a/api/cloud/unittest/cloud_store_test.cpp b/api/cloud/unittest/cloud_store_test.cpp index 7536349fa..77931488b 100644 --- a/api/cloud/unittest/cloud_store_test.cpp +++ b/api/cloud/unittest/cloud_store_test.cpp @@ -100,7 +100,7 @@ class TestCloudStore : public testing::Test { const oc_cloud_store_t *s2) { compareEndpoints(s1->ci_servers, s2->ci_servers); - EXPECT_STREQ(oc_string(s1->ci_server), oc_string(s2->ci_server)); + // EXPECT_STREQ(oc_string(s1->ci_server), oc_string(s2->ci_server)); EXPECT_STREQ(oc_string(s1->auth_provider), oc_string(s2->auth_provider)); EXPECT_STREQ(oc_string(s1->uid), oc_string(s2->uid)); EXPECT_STREQ(oc_string(s1->access_token), oc_string(s2->access_token)); diff --git a/api/cloud/unittest/cloud_test.cpp b/api/cloud/unittest/cloud_test.cpp index e3d62a717..1062f3fca 100644 --- a/api/cloud/unittest/cloud_test.cpp +++ b/api/cloud/unittest/cloud_test.cpp @@ -31,6 +31,8 @@ #include "security/oc_pstat_internal.h" #endif +using namespace std::chrono_literals; + static constexpr size_t kDeviceID{ 0 }; class TestCloud : public testing::Test { @@ -132,10 +134,10 @@ TEST_F(TestCloud, oc_cloud_provision_conf_resource) EXPECT_EQ(-1, oc_cloud_provision_conf_resource(ctx, ci_server, access_token, sid, invalid.c_str())); - EXPECT_STREQ(access_token, oc_string(ctx->store.access_token)); - EXPECT_STREQ(auth_provider, oc_string(ctx->store.auth_provider)); - EXPECT_STREQ(ci_server, oc_string(ctx->store.ci_server)); - EXPECT_STREQ(sid, oc_string(ctx->store.sid)); + EXPECT_STREQ(access_token, oc_cloud_get_at(ctx)); + EXPECT_STREQ(auth_provider, oc_cloud_get_apn(ctx)); + EXPECT_STREQ(ci_server, oc_cloud_get_cis(ctx)); + EXPECT_STREQ(sid, oc_cloud_get_sid(ctx)); EXPECT_EQ(OC_CLOUD_INITIALIZED, ctx->store.status); } @@ -158,7 +160,7 @@ TEST_F(TestCloud, oc_cloud_action_to_str) EXPECT_EQ(OC_CLOUD_ACTION_UNKNOWN_STR, v); } -TEST_F(TestCloud, cloud_register) +TEST_F(TestCloud, oc_cloud_do_register) { #ifdef OC_SECURITY oc_sec_pstat_t *pstat = oc_sec_get_pstat(kDeviceID); @@ -182,14 +184,14 @@ TEST_F(TestCloud, cloud_register) *called = true; }; - int ret = cloud_register(ctx, cbk, &cbk_called, 1); - EXPECT_EQ(0, ret); + auto timeout = 1s; + EXPECT_EQ(0, oc_cloud_do_register(ctx, cbk, &cbk_called, timeout.count())); - oc::TestDevice::PoolEvents(2); + oc::TestDevice::PoolEventsMsV1(timeout, true); EXPECT_TRUE(cbk_called); } -TEST_F(TestCloud, cloud_login) +TEST_F(TestCloud, oc_cloud_do_login) { #ifdef OC_SECURITY oc_sec_pstat_t *pstat = oc_sec_get_pstat(kDeviceID); @@ -217,14 +219,14 @@ TEST_F(TestCloud, cloud_login) *called = true; }; - int ret = cloud_login(ctx, cbk, &cbk_called, 1); - EXPECT_EQ(0, ret); + auto timeout = 1s; + EXPECT_EQ(0, oc_cloud_do_login(ctx, cbk, &cbk_called, timeout.count())); - oc::TestDevice::PoolEvents(2); + oc::TestDevice::PoolEventsMsV1(timeout, true); EXPECT_TRUE(cbk_called); } -TEST_F(TestCloud, cloud_refresh_token) +TEST_F(TestCloud, oc_cloud_do_refresh_token) { #ifdef OC_SECURITY oc_sec_pstat_t *pstat = oc_sec_get_pstat(kDeviceID); @@ -259,9 +261,10 @@ TEST_F(TestCloud, cloud_refresh_token) *called = true; }; - int ret = cloud_refresh_token(ctx, cbk, &cbk_called, 1); - EXPECT_EQ(0, ret); + auto timeout = 1s; + EXPECT_EQ(0, + oc_cloud_do_refresh_token(ctx, cbk, &cbk_called, timeout.count())); - oc::TestDevice::PoolEvents(2); + oc::TestDevice::PoolEventsMsV1(timeout, true); EXPECT_TRUE(cbk_called); } \ No newline at end of file diff --git a/security/unittest/csrtest.cpp b/security/unittest/csrtest.cpp index ac950b283..38c2b99d4 100644 --- a/security/unittest/csrtest.cpp +++ b/security/unittest/csrtest.cpp @@ -50,14 +50,16 @@ class TestCSRWithDevice : public testing::Test { g_ocf_ecs.push_back(ec); } } + EXPECT_TRUE(oc::TestDevice::StartServer()); } - void SetUp() override { EXPECT_TRUE(oc::TestDevice::StartServer()); } + static void TearDownTestCase() { oc::TestDevice::StopServer(); } + + void SetUp() override {} void TearDown() override { oc::TestDevice::Reset(); - oc::TestDevice::StopServer(); oc_sec_certs_default(); } diff --git a/security/unittest/dtlstest.cpp b/security/unittest/dtlstest.cpp index fc528abee..026dc84d8 100644 --- a/security/unittest/dtlstest.cpp +++ b/security/unittest/dtlstest.cpp @@ -99,7 +99,7 @@ dtls_thread_win32(void *data) TEST_F(TestDTLSWithServer, DTLSInactivityMonitor) { oc_clock_time_t timeout_default = oc_dtls_inactivity_timeout(); - oc_dtls_set_inactivity_timeout(2 * OC_CLOCK_SECOND); + oc_dtls_set_inactivity_timeout(OC_CLOCK_SECOND); // DTLS endpoint auto epOpt = oc::TestDevice::GetEndpoint(kDeviceID, SECURED, TCP); @@ -139,7 +139,7 @@ TEST_F(TestDTLSWithServer, DTLSInactivityMonitor) #endif /* MINGW_WINTHREAD */ while (dtls_status.load() == DTLS_STATUS::DTLS_INIT) { - oc::TestDevice::PoolEventsMs(200); + oc::TestDevice::PoolEventsMs(50); } ASSERT_EQ(1, oc_tls_num_peers(kDeviceID)); diff --git a/security/unittest/obt_certstest.cpp b/security/unittest/obt_certstest.cpp index 13d56ca32..772169842 100644 --- a/security/unittest/obt_certstest.cpp +++ b/security/unittest/obt_certstest.cpp @@ -581,10 +581,14 @@ class TestObtCertsWithDevice : public testing::Test { // allow all ocf-supported MDs and ECs oc_sec_certs_md_set_algorithms_allowed(OCF_CERTS_SUPPORTED_MDS); oc_sec_certs_ecp_set_group_ids_allowed(OCF_CERTS_SUPPORTED_ELLIPTIC_CURVES); + + EXPECT_TRUE(oc::TestDevice::StartServer()); } static void TearDownTestCase() { + oc::TestDevice::StopServer(); + // restore defaults oc_sec_certs_md_set_algorithms_allowed( MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256)); @@ -594,8 +598,6 @@ class TestObtCertsWithDevice : public testing::Test { void SetUp() override { - EXPECT_TRUE(oc::TestDevice::StartServer()); - oc_uuid_t uuid{}; oc_gen_uuid(&uuid); std::array buf; @@ -606,11 +608,7 @@ class TestObtCertsWithDevice : public testing::Test { roles_.Add("user", ""); } - void TearDown() override - { - oc::TestDevice::Reset(); - oc::TestDevice::StopServer(); - } + void TearDown() override { oc::TestDevice::Reset(); } std::string uuid_{}; oc::Roles roles_{};