Skip to content

Commit

Permalink
fixup! api: refactor mnt resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Nov 8, 2023
1 parent edda3b3 commit 50d79d0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 17 deletions.
21 changes: 12 additions & 9 deletions api/oc_mnt.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ mnt_resource_get(oc_request_t *request, oc_interface_mask_t iface, void *data)
}

static void
post_mnt(oc_request_t *request, oc_interface_mask_t iface_mask, void *data)
mnt_resource_post(oc_request_t *request, oc_interface_mask_t iface_mask,
void *data)
{
(void)iface_mask;
(void)data;
Expand All @@ -70,9 +71,9 @@ post_mnt(oc_request_t *request, oc_interface_mask_t iface_mask, void *data)
if (oc_rep_get_bool(request->request_payload, "fr", &fr) && fr) {
#ifdef OC_SECURITY
success = oc_reset_device_v1(request->resource->device, false);
#else /* OC_SECURITY */
#else /* !OC_SECURITY */
success = true;
#endif /* !OC_SECURITY */
#endif /* OC_SECURITY */
}

if (!success) {
Expand All @@ -84,13 +85,15 @@ post_mnt(oc_request_t *request, oc_interface_mask_t iface_mask, void *data)
oc_rep_new_realloc_v1(&request->response->response_buffer->buffer,
request->response->response_buffer->buffer_size,
OC_MAX_APP_DATA_SIZE);
#else /* OC_DYNAMIC_ALLOCATION */
#else /* !OC_DYNAMIC_ALLOCATION */
oc_rep_new_v1(request->response->response_buffer->buffer,
request->response->response_buffer->buffer_size);
#endif /* !OC_DYNAMIC_ALLOCATION */
oc_rep_start_root_object();
oc_rep_set_boolean(root, fr, false);
oc_rep_end_root_object();
#endif /* OC_DYNAMIC_ALLOCATION */
int err = mnt_encode(request->resource, iface_mask);
if (err != CborNoError) {
OC_ERR("encoding maintenance resource failed(error=%d)", (int)err);
return;
}
#ifdef OC_DYNAMIC_ALLOCATION
request->response->response_buffer->buffer =
oc_rep_shrink_encoder_buf(request->response->response_buffer->buffer);
Expand All @@ -108,7 +111,7 @@ oc_create_maintenance_resource(size_t device)
int properties = OC_SECURE | OC_DISCOVERABLE;
oc_core_populate_resource(OCF_MNT, device, OCF_MNT_URI, interfaces,
default_interface, properties, mnt_resource_get,
/*put*/ NULL, post_mnt, /*delete*/ NULL, 1,
/*put*/ NULL, mnt_resource_post, /*delete*/ NULL, 1,
OCF_MNT_RT);
}

Expand Down
59 changes: 51 additions & 8 deletions api/unittest/maintenancetest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#include "tests/gtest/RepPool.h"
#include "tests/gtest/Resource.h"

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

#include <gtest/gtest.h>
#include <string>

Expand Down Expand Up @@ -55,24 +59,24 @@ TEST_F(TestMaintenance, IsMaintenanceURI_P)

class TestMaintenanceWithServer : public testing::Test {
public:
static void SetUpTestCase()
static void setupDevice()
{
// TODO rm
oc_log_set_level(OC_LOG_LEVEL_DEBUG);

ASSERT_TRUE(oc::TestDevice::StartServer());
#ifdef OC_HAS_FEATURE_RESOURCE_ACCESS_IN_RFOTM
ASSERT_TRUE(
oc::SetAccessInRFOTM(OCF_MNT, kDeviceID, true,
OC_PERM_RETRIEVE | OC_PERM_UPDATE | OC_PERM_DELETE));
#endif /* OC_HAS_FEATURE_RESOURCE_ACCESS_IN_RFOTM */
}

static void SetUpTestCase()
{
ASSERT_TRUE(oc::TestDevice::StartServer());
setupDevice();
}

static void TearDownTestCase()
{
oc::TestDevice::StopServer();

oc_log_set_level(OC_LOG_LEVEL_INFO);
}
};

Expand Down Expand Up @@ -151,7 +155,46 @@ TEST_F(TestMaintenanceWithServer, GetRequestBaseline)

TEST_F(TestMaintenanceWithServer, PostRequest)
{
// TODO
auto epOpt = oc::TestDevice::GetEndpoint(kDeviceID);
ASSERT_TRUE(epOpt.has_value());
auto ep = std::move(*epOpt);

auto post_handler = [](oc_client_response_t *data) {
oc::TestDevice::Terminate();
ASSERT_EQ(OC_STATUS_CHANGED, data->code);
*static_cast<bool *>(data->user_data) = true;
OC_DBG("POST payload: %s",
oc::RepPool::GetJson(data->payload, true).data());
};

#ifdef OC_TEST
oc_pstat_set_reset_delay_ms(100);
#endif /* OC_TEST */

bool invoked = false;
ASSERT_TRUE(
oc_init_post(OCF_MNT_URI, &ep, nullptr, post_handler, HIGH_QOS, &invoked));
oc_rep_start_root_object();
oc_rep_set_boolean(root, fr, true);
oc_rep_end_root_object();
auto timeout = 1s;
ASSERT_TRUE(oc_do_post_with_timeout(timeout.count()));
oc::TestDevice::PoolEventsMsV1(timeout, true);
EXPECT_TRUE(invoked);

#ifdef OC_SECURITY
ASSERT_TRUE(oc_reset_in_progress(kDeviceID));

// wait for the device to handle the factory reset
#ifdef OC_TEST
oc::TestDevice::PoolEventsMsV1(100ms, true);
#else /* !OC_TEST */
oc::TestDevice::PoolEventsMs(OC_PSTAT_RESET_DELAY_MS, true);
#endif /* OC_TEST */
ASSERT_FALSE(oc_reset_in_progress(kDeviceID));

setupDevice();
#endif /* OC_SECURITY */
}

TEST_F(TestMaintenanceWithServer, PutRequest_FailMethodNotSupported)
Expand Down

0 comments on commit 50d79d0

Please sign in to comment.