Skip to content

Commit

Permalink
fixup! cloud: set/get function for timeouts of the default retry action
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Apr 5, 2024
1 parent 5bb36cf commit 5a909ea
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 26 deletions.
15 changes: 11 additions & 4 deletions api/cloud/oc_cloud.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,10 +505,17 @@ cloud_deinit_devices(size_t devices)
static bool
cloud_init_device(size_t device)
{
return cloud_context_init(device) != NULL &&
oc_cloud_add_resource(oc_core_get_resource_by_index(OCF_P, 0)) == 0 &&
oc_cloud_add_resource(oc_core_get_resource_by_index(OCF_D, device)) ==
0;
if (cloud_context_init(device) == NULL) {
return false;
}

if (oc_cloud_add_resource(oc_core_get_resource_by_index(OCF_D, device)) !=
0 ||
oc_cloud_add_resource(oc_core_get_resource_by_index(OCF_P, 0)) != 0) {
cloud_context_deinit(oc_cloud_get_context(device));
return false;
}
return true;
}

bool
Expand Down
78 changes: 74 additions & 4 deletions api/cloud/unittest/cloud_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@
#include "api/cloud/oc_cloud_internal.h"
#include "api/cloud/oc_cloud_resource_internal.h"
#include "api/cloud/oc_cloud_schedule_internal.h"
#include "api/oc_core_res_internal.h"
#include "api/oc_ri_internal.h"
#include "api/oc_runtime_internal.h"
#include "messaging/coap/conf.h"
#include "oc_api.h"
#include "oc_cloud.h"
#include "oc_uuid.h"
#include "port/oc_network_event_handler_internal.h"
#include "tests/gtest/Device.h"
#include "tests/gtest/RepPool.h"
#include "util/oc_secure_string_internal.h"
#include "util/oc_features.h"

#include <array>
#include <gtest/gtest.h>
Expand All @@ -40,8 +44,13 @@

#ifdef OC_SECURITY
#include "security/oc_pstat_internal.h"
#include "security/oc_svr_internal.h"
#endif /* OC_SECURITY */

#ifdef OC_HAS_FEATURE_PUSH
#include "api/oc_push_internal.h"
#endif /* OC_HAS_FEATURE_PUSH */

using namespace std::chrono_literals;

static constexpr size_t kDeviceID{ 0 };
Expand Down Expand Up @@ -104,20 +113,81 @@ TEST_F(TestCloud, oc_cloud_get_retry_timeouts)
oc_cloud_get_retry_timeouts(result.data(), result.size()));
}

#if !defined(OC_DYNAMIC_ALLOCATION) && !defined(OC_SECURITY)
TEST_F(TestCloud, oc_cloud_init_fail)
{
ASSERT_TRUE(oc_cloud_init());
// multiple initialization is not allowed
EXPECT_FALSE(oc_cloud_init());

oc_cloud_shutdown();
}

TEST_F(TestCloud, oc_cloud_shutdown_fail)
{
oc_network_event_handler_mutex_init();
oc_ri_init();
oc_core_init();
ASSERT_EQ(0, oc_init_platform("OCFTest", nullptr, nullptr));
ASSERT_EQ(
0, oc_add_device(
oc::DefaultDevice.uri.c_str(), oc::DefaultDevice.rt.c_str(),
oc::DefaultDevice.name.c_str(), oc::DefaultDevice.spec_version.c_str(),
oc::DefaultDevice.data_model_version.c_str(), nullptr, nullptr));
#ifdef OC_SECURITY
oc_sec_svr_create();
#endif /* OC_SECURITY */
ASSERT_TRUE(oc_cloud_init());

oc_cloud_shutdown();
// multiple shutdown is not allowed, but the app should not crash
oc_cloud_shutdown();

#ifdef OC_SECURITY
oc_sec_svr_free();
#endif /* OC_SECURITY */
#ifdef OC_HAS_FEATURE_PUSH
oc_push_free();
#endif /* OC_HAS_FEATURE_PUSH */
oc_connectivity_shutdown(/*device*/ 0);
oc_core_shutdown();
oc_ri_shutdown();
oc_network_event_handler_mutex_destroy();
}

#if !defined(OC_SECURITY)
// we need static allocation to ensure that allocation fails
// we need insecure build, because for secure build we need to have a valid
// pstat, which is not the case here
TEST_F(TestCloud, cloud_init_devices)
{
EXPECT_FALSE(cloud_init_devices(1));

ASSERT_TRUE(oc::TestDevice::StartServer());
oc_ri_init();
oc_core_init();
// add device 0, but platform resource is missing
ASSERT_EQ(
0, oc_add_device(
oc::DefaultDevice.uri.c_str(), oc::DefaultDevice.rt.c_str(),
oc::DefaultDevice.name.c_str(), oc::DefaultDevice.spec_version.c_str(),
oc::DefaultDevice.data_model_version.c_str(), nullptr, nullptr));
EXPECT_FALSE(cloud_init_devices(1));

ASSERT_EQ(0, oc_init_platform("OCFTest", nullptr, nullptr));
EXPECT_TRUE(cloud_init_devices(1));
cloud_deinit_devices(1);

EXPECT_FALSE(cloud_init_devices(2));
oc::TestDevice::StopServer();

oc_cloud_shutdown();
#ifdef OC_HAS_FEATURE_PUSH
oc_push_free();
#endif /* OC_HAS_FEATURE_PUSH */
oc_connectivity_shutdown(/*device*/ 0);
oc_core_shutdown();
oc_ri_shutdown();
}

#endif /* !OC_DYNAMIC_ALLOCATION && !OC_SECURITY */
#endif /* !OC_SECURITY */

class TestCloudWithServer : public testing::Test {
public:
Expand Down
2 changes: 1 addition & 1 deletion api/oc_ri_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ bool
oc_ri_on_delete_resource_add_callback(oc_ri_delete_resource_cb_t cb)
{
if (oc_ri_on_delete_resource_find_callback(cb) != NULL) {
OC_WRN("delete resource callback already exists");
OC_ERR("delete resource callback already exists");
return false;
}
oc_ri_on_delete_resource_t *item = oc_memb_alloc(&g_on_delete_resource_cb_s);
Expand Down
12 changes: 7 additions & 5 deletions util/oc_endpoint_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,18 @@ endpoint_address_is_valid(oc_string_view_t uri)
return uri.length > 0 && uri.length < OC_ENDPOINT_MAX_ENDPOINT_URI_LENGTH;
}

static void
static bool
endpoint_address_select(oc_endpoint_addresses_t *eas,
const oc_endpoint_address_t *selected)
{
if (eas->selected == selected) {
return;
return false;
}
eas->selected = selected;
if (eas->on_selected_change.cb != NULL) {
eas->on_selected_change.cb(eas->on_selected_change.cb_data);
}
return true;
}

static bool
Expand Down Expand Up @@ -436,13 +437,14 @@ oc_endpoint_addresses_select_by_uri(oc_endpoint_addresses_t *eas,
return true;
}

void
bool
oc_endpoint_addresses_select_next(oc_endpoint_addresses_t *eas)
{
if (eas->selected == NULL) {
return;
return false;
}
endpoint_address_select(eas, endpoint_address_next(eas, eas->selected->next));
return endpoint_address_select(
eas, endpoint_address_next(eas, eas->selected->next));
}

bool
Expand Down
2 changes: 1 addition & 1 deletion util/oc_endpoint_address_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ bool oc_endpoint_addresses_select_by_uri(oc_endpoint_addresses_t *eas,
oc_string_view_t uri) OC_NONNULL();

/** Select the next endpoint address from the list of endpoint addresses */
void oc_endpoint_addresses_select_next(oc_endpoint_addresses_t *eas)
bool oc_endpoint_addresses_select_next(oc_endpoint_addresses_t *eas)
OC_NONNULL();

/** Check if an endpoint address matching the given URI is selected */
Expand Down
24 changes: 13 additions & 11 deletions util/unittest/endpoint_address_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,14 @@ TEST_F(TestEndpointAddresses, Select)
auto on_selected_change = oc_endpoint_addresses_get_on_selected_change(&ea);
ASSERT_EQ(nullptr, on_selected_change.cb);
ASSERT_EQ(nullptr, on_selected_change.cb_data);
bool selected_changed = false;
bool on_change_invoked = false;
oc_endpoint_addresses_set_on_selected_change(
&ea, [](void *data) { *static_cast<bool *>(data) = true; },
&selected_changed);
&on_change_invoked);

oc_endpoint_addresses_select_next(&ea);
bool selected_changed = oc_endpoint_addresses_select_next(&ea);
EXPECT_FALSE(selected_changed);
EXPECT_FALSE(on_change_invoked);
auto *selected_addr = oc_endpoint_addresses_selected_uri(&ea);
EXPECT_EQ(nullptr, selected_addr);
auto *selected_uuid = oc_endpoint_addresses_selected_uuid(&ea);
Expand All @@ -401,10 +402,9 @@ TEST_F(TestEndpointAddresses, Select)
ASSERT_NE(nullptr,
oc_endpoint_addresses_add(
&ea, oc_endpoint_address_make_view_with_uuid(uri1, id1)));

// when adding to an empty list, the first added item is automatically
// selected
EXPECT_TRUE(selected_changed);
EXPECT_TRUE(on_change_invoked);
EXPECT_TRUE(oc_endpoint_addresses_is_selected(&ea, uri1));
selected_addr = oc_endpoint_addresses_selected_uri(&ea);
ASSERT_NE(nullptr, selected_addr);
Expand All @@ -417,10 +417,10 @@ TEST_F(TestEndpointAddresses, Select)
ASSERT_EQ(nullptr, selected_name);

// non-existing URI shouldn't change the selection
selected_changed = false;
on_change_invoked = false;
EXPECT_FALSE(
oc_endpoint_addresses_select_by_uri(&ea, OC_STRING_VIEW("/fail")));
EXPECT_FALSE(selected_changed);
EXPECT_FALSE(on_change_invoked);
EXPECT_FALSE(oc_endpoint_addresses_is_selected(&ea, OC_STRING_VIEW("/fail")));
EXPECT_TRUE(oc_endpoint_addresses_is_selected(&ea, uri1));
selected_addr = oc_endpoint_addresses_selected_uri(&ea);
Expand Down Expand Up @@ -465,14 +465,16 @@ TEST_F(TestEndpointAddresses, Select)
EXPECT_STREQ(name3.data, oc_string(*selected_name));

// rotate back to uri1
selected_changed = false;
oc_endpoint_addresses_select_next(&ea);
on_change_invoked = false;
selected_changed = oc_endpoint_addresses_select_next(&ea);
EXPECT_TRUE(selected_changed);
EXPECT_TRUE(on_change_invoked);
#else /* !OC_DYNAMIC_ALLOCATION */
// the list has a single element, so the selection should stay at uri1
selected_changed = false;
oc_endpoint_addresses_select_next(&ea);
on_change_invoked = false;
selected_changed = oc_endpoint_addresses_select_next(&ea);
EXPECT_FALSE(selected_changed);
EXPECT_FALSE(on_change_invoked);
#endif /* OC_DYNAMIC_ALLOCATION */

EXPECT_TRUE(oc_endpoint_addresses_is_selected(&ea, uri1));
Expand Down

0 comments on commit 5a909ea

Please sign in to comment.