From 50d79d0b87e92f6e5177f99eeeb4a143efa0f46a Mon Sep 17 00:00:00 2001 From: Daniel Adam Date: Wed, 8 Nov 2023 18:25:26 +0100 Subject: [PATCH] fixup! api: refactor mnt resource --- api/oc_mnt.c | 21 +++++++----- api/unittest/maintenancetest.cpp | 59 +++++++++++++++++++++++++++----- 2 files changed, 63 insertions(+), 17 deletions(-) diff --git a/api/oc_mnt.c b/api/oc_mnt.c index 46429a66e..db3b3f7f1 100644 --- a/api/oc_mnt.c +++ b/api/oc_mnt.c @@ -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; @@ -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) { @@ -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); @@ -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); } diff --git a/api/unittest/maintenancetest.cpp b/api/unittest/maintenancetest.cpp index 9ec445839..926ded258 100644 --- a/api/unittest/maintenancetest.cpp +++ b/api/unittest/maintenancetest.cpp @@ -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 #include @@ -55,12 +59,8 @@ 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, @@ -68,11 +68,15 @@ class TestMaintenanceWithServer : public testing::Test { #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); } }; @@ -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(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)