Skip to content

Commit

Permalink
fixup! Migrate device provisioning service
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Sep 3, 2024
1 parent f7dac75 commit 563973b
Show file tree
Hide file tree
Showing 16 changed files with 2,344 additions and 17 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/cmake-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ jobs:
- args: "-DOC_IPV4_ENABLED=ON -DOC_TCP_ENABLED=ON -DOC_PKI_ENABLED=OFF"
# cloud on (ipv4+tcp on), dynamic allocation off, push notifications off
- args: "-DOC_CLOUD_ENABLED=ON -DOC_DYNAMIC_ALLOCATION_ENABLED=OFF -DOC_PUSH_ENABLED=OFF"
# cloud on (ipv4+tcp on), collections create on, oscore off, device provisioning on
- args: "-DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_OSCORE_ENABLED=OFF -DPLGD_DEV_DEVICE_PROVISIONING_ENABLED=ON"
# cloud on (ipv4+tcp on), collections create on, custom message buffer size, custom message buffer pool size, custom app data buffer size, custom app data buffer pool size
- args: "-DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_INOUT_BUFFER_SIZE=2048 -DOC_INOUT_BUFFER_POOL=4 -DOC_APP_DATA_BUFFER_SIZE=2048 -DOC_APP_DATA_BUFFER_POOL=4"
# debug on
Expand Down Expand Up @@ -138,7 +136,7 @@ jobs:
# install_faketime: true
uses: ./.github/workflows/unit-test-with-cfg.yml
with:
build_args: -DOC_LOG_MAXIMUM_LOG_LEVEL=INFO -DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_MNT_ENABLED=ON -DOC_WKCORE_ENABLED=ON -DOC_SOFTWARE_UPDATE_ENABLED=ON -DOC_DISCOVERY_RESOURCE_OBSERVABLE_ENABLED=ON -DOC_PUSH_ENABLED=ON -DOC_RESOURCE_ACCESS_IN_RFOTM_ENABLED=ON -DPLGD_DEV_TIME_ENABLED=ON -DOC_ETAG_ENABLED=ON ${{ matrix.args }}
build_args: -DOC_LOG_MAXIMUM_LOG_LEVEL=INFO -DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_MNT_ENABLED=ON -DOC_WKCORE_ENABLED=ON -DOC_SOFTWARE_UPDATE_ENABLED=ON -DOC_DISCOVERY_RESOURCE_OBSERVABLE_ENABLED=ON -DOC_PUSH_ENABLED=ON -DOC_RESOURCE_ACCESS_IN_RFOTM_ENABLED=ON -DPLGD_DEV_TIME_ENABLED=ON -DOC_ETAG_ENABLED=ON -DPLGD_DEV_DEVICE_PROVISIONING_ENABLED=ON -DPLGD_DEV_DEVICE_PROVISIONING_TEST_PROPERTIES_ENABLED=ON ${{ matrix.args }}
build_type: ${{ (github.event_name == 'workflow_dispatch' && inputs.build_type) || 'Debug' }}
clang: ${{ ((github.event_name == 'workflow_dispatch' && inputs.clang) || matrix.clang) || false }}
coverage: false
Expand Down
16 changes: 9 additions & 7 deletions .github/workflows/sonar-cloud-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ concurrency:
cancel-in-progress: ${{ github.ref_name != 'master' }}

on:
# push:
# branches:
# - master
# pull_request:
# types: [opened, synchronize, reopened]
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand All @@ -27,6 +27,8 @@ jobs:
include:
# cloud (ipv4+tcp) on, collection create on, push on, rfotm on
- build_args: "-DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_PUSH_ENABLED=ON -DOC_RESOURCE_ACCESS_IN_RFOTM_ENABLED=ON"
# cloud on (ipv4+tcp on), collections create on, oscore off, device provisioning on
- args: "-DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_OSCORE_ENABLED=OFF -DPLGD_DEV_DEVICE_PROVISIONING_ENABLED=ON -DPLGD_DEV_DEVICE_PROVISIONING_TEST_PROPERTIES_ENABLED=ON"
# security off, ipv4 on, collection create on, push on, max num concurrent requests=1
- build_args: "-DOC_SECURITY_ENABLED=OFF -DOC_IPV4_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_PUSH_ENABLED=ON -DOC_DEVICE_MAX_NUM_CONCURRENT_REQUESTS=1"
# ipv6 dns on, oscore off, rep realloc on, json encoder on, introspection IDD off
Expand Down Expand Up @@ -110,7 +112,7 @@ jobs:
mkdir build && cd build
# sonar-scanner currently cannot handle multi configuration configuration (ie. compilation of the same file with different defines),
# so we enable as many features as possible so we get max. amount of code analysis
cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_VERBOSE_MAKEFILE=ON -DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_MNT_ENABLED=ON -DOC_WKCORE_ENABLED=ON -DOC_SOFTWARE_UPDATE_ENABLED=ON -DOC_DISCOVERY_RESOURCE_OBSERVABLE_ENABLED=ON -DOC_PUSH_ENABLED=ON -DOC_RESOURCE_ACCESS_IN_RFOTM_ENABLED=ON -DPLGD_DEV_TIME_ENABLED=ON -DOC_ETAG_ENABLED=ON -DOC_JSON_ENCODER_ENABLED=ON -DBUILD_TESTING=ON ..
cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_VERBOSE_MAKEFILE=ON -DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_MNT_ENABLED=ON -DOC_WKCORE_ENABLED=ON -DOC_SOFTWARE_UPDATE_ENABLED=ON -DOC_DISCOVERY_RESOURCE_OBSERVABLE_ENABLED=ON -DOC_PUSH_ENABLED=ON -DOC_RESOURCE_ACCESS_IN_RFOTM_ENABLED=ON -DPLGD_DEV_TIME_ENABLED=ON -DOC_ETAG_ENABLED=ON -DOC_JSON_ENCODER_ENABLED=ON -DPLGD_DEV_TIME_ENABLED=ON -DPLGD_DEV_DEVICE_PROVISIONING_ENABLED=ON -DBUILD_TESTING=ON ..
cd ..
# for files defined in multiple cmake targets, sonar-scanner seems to take the configuration from the first compilation of the file,
# so we force client-server target to be compiled first so we get analysis of code with both OC_CLIENT and OC_SERVER enabled
Expand Down Expand Up @@ -138,4 +140,4 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: |
sonar-scanner --define sonar.cfamily.build-wrapper-output="${{ env.BUILD_WRAPPER_OUT_DIR }}"
sonar-scanner --define sonar.cfamily.compile-commands="${{ env.BUILD_WRAPPER_OUT_DIR }}/compile_commands.json"
3 changes: 1 addition & 2 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ jobs:
fail-fast: false
matrix:
include:
- build_args:
- build_args: -DOC_OSCORE_ENABLED=OFF -DPLGD_DEV_TIME_ENABLED=ON -DPLGD_DEV_DEVICE_PROVISIONING_ENABLED=ON
- build_args: -DPLGD_DEV_TIME_ENABLED=ON -DPLGD_DEV_DEVICE_PROVISIONING_ENABLED=ON
- build_args: -DOC_DYNAMIC_ALLOCATION_ENABLED=OFF -DOC_PUSH_ENABLED=OFF -DOC_JSON_ENCODER_ENABLED=OFF -DOC_DEBUG_ENABLED=ON

runs-on: ubuntu-22.04
Expand Down
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ set(OC_DEVICE_MAX_NUM_CONCURRENT_REQUESTS "" CACHE STRING "Maximum number of mes
# plgd.dev features
set(PLGD_DEV_TIME_ENABLED OFF CACHE BOOL "Enable plgd time feature.")
set(PLGD_DEV_DEVICE_PROVISIONING_ENABLED OFF CACHE BOOL "Enable plgd's device provisioning feature.")
set(PLGD_DEV_DEVICE_PROVISIONING_TEST_PROPERTIES_ENABLED OFF CACHE BOOL "Enable plgd's device provisioning feature's test properties.")

set(OC_ASAN_ENABLED OFF CACHE BOOL "Enable address sanitizer build.")
set(OC_LSAN_ENABLED OFF CACHE BOOL "Enable leak sanitizer build.")
Expand Down Expand Up @@ -427,6 +428,13 @@ if(PLGD_DEV_DEVICE_PROVISIONING_ENABLED)
list(APPEND PUBLIC_COMPILE_DEFINITIONS "PLGD_DEV_DEVICE_PROVISIONING")
endif()

if(PLGD_DEV_DEVICE_PROVISIONING_TEST_PROPERTIES_ENABLED)
list(APPEND PRIVATE_COMPILE_DEFINITIONS "PLGD_DPS_RESOURCE_TEST_PROPERTIES")
if(BUILD_TESTING)
list(APPEND TEST_COMPILE_DEFINITIONS "PLGD_DPS_RESOURCE_TEST_PROPERTIES")
endif()
endif()

if(BUILD_TESTING)
list(APPEND PRIVATE_COMPILE_DEFINITIONS "OC_TEST")
if(BUILD_MBEDTLS)
Expand Down
1 change: 1 addition & 0 deletions api/plgd/device-provisioning-client/plgd_dps_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "plgd_dps_resource_internal.h"
#include "plgd_dps_store_internal.h"

#include "api/cloud/oc_cloud_schedule_internal.h"
#include "oc_endpoint.h"
#include "oc_session_events.h"
#include "util/oc_list.h"
Expand Down
6 changes: 3 additions & 3 deletions api/plgd/device-provisioning-client/plgd_dps_resource.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ dps_resource_encode_iotivity_retry_timeouts(CborEncoder *encoder,
oc_string_view_t retryKey = OC_STRING_VIEW(PLGD_DPS_TEST_IOTIVITY_RETRY);
g_err |= oc_rep_encode_text_string(encoder, retryKey.data, retryKey.length);
oc_rep_begin_array(encoder, retry);
for (size_t i = 0; i < DPS_ARRAY_SIZE(rtd->iotivity.retry_timeout); ++i) {
for (size_t i = 0; i < OC_ARRAY_SIZE(rtd->iotivity.retry_timeout); ++i) {
if (rtd->iotivity.retry_timeout[i] == 0) {
break;
}
Expand Down Expand Up @@ -278,7 +278,7 @@ dps_resource_encode_response(const plgd_dps_context_t *ctx,
#ifdef PLGD_DPS_RESOURCE_TEST_PROPERTIES
data.test.cloud_status_observer = ctx->cloud_observer.cfg;
oc_cloud_get_retry_timeouts(&data.test.iotivity.retry_timeout[0],
DPS_ARRAY_SIZE(data.test.iotivity.retry_timeout));
OC_ARRAY_SIZE(data.test.iotivity.retry_timeout));
#endif /* PLGD_DPS_RESOURCE_TEST_PROPERTIES */

dps_resource_encode(interface, ctx->conf, &data);
Expand Down Expand Up @@ -311,7 +311,7 @@ dps_update_iotivity(const oc_rep_t *iot)
if (oc_rep_get_int_array(iot, PLGD_DPS_TEST_IOTIVITY_RETRY, &retry,
&retry_size)) {
uint16_t retry_timeout[DPS_CLOUD_RETRY_TIMEOUTS_SIZE] = { 0 };
for (size_t i = 0; i < retry_size && i < DPS_ARRAY_SIZE(retry_timeout);
for (size_t i = 0; i < retry_size && i < OC_ARRAY_SIZE(retry_timeout);
++i) {
retry_timeout[i] = (uint16_t)retry[i];
}
Expand Down
201 changes: 201 additions & 0 deletions api/plgd/unittest/plgd_dps.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/****************************************************************************
*
* Copyright (c) 2022-2024 plgd.dev, s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License"),
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*
****************************************************************************/

#include "util/oc_features.h"

#ifdef OC_HAS_FEATURE_PLGD_DEVICE_PROVISIONING

#include "plgd_dps_test.h"

#include "api/plgd/device-provisioning-client/plgd_dps_internal.h"
#include "api/plgd/device-provisioning-client/plgd_dps_security_internal.h"
#include "oc_api.h"
#include "oc_core_res.h"
#include "oc_uuid.h"

#include "gtest/gtest.h"

#include <array>
#include <cstdint>
#include <cstring>
#include <memory>
#include <sstream>
#include <string>
#include <unordered_set>
#include <vector>

static constexpr size_t kDeviceID = 0;

TEST(DPSApiTest, SetSkipVerify)
{
auto ctx = dps::make_unique_context(kDeviceID);
plgd_dps_set_skip_verify(ctx.get(), true);

EXPECT_TRUE(plgd_dps_get_skip_verify(ctx.get()));
}

TEST(DPSApiTest, StatusToLogString)
{
EXPECT_NE(0, dps_status_to_logstr(0, nullptr, 0));
std::array<char, 1> tooSmall{};
EXPECT_NE(0, dps_status_to_logstr(0, &tooSmall[0], tooSmall.size()));

std::vector<char> buffer;
buffer.resize(1024);
EXPECT_EQ(0, dps_status_to_logstr(0, buffer.data(), buffer.capacity()));
EXPECT_STREQ(kPlgdDpsStatusUninitialized, buffer.data());

EXPECT_NE(0, dps_status_to_logstr(PLGD_DPS_PROVISIONED_ALL_FLAGS,
&tooSmall[0], tooSmall.size()));

EXPECT_EQ(0, dps_status_to_logstr(PLGD_DPS_PROVISIONED_ALL_FLAGS,
buffer.data(), buffer.capacity()));
std::unordered_set<std::string> flags{
kPlgdDpsStatusInitialized, kPlgdDpsStatusGetTime,
kPlgdDpsStatusHasTime, kPlgdDpsStatusGetOwner,
kPlgdDpsStatusHasOwner, kPlgdDpsStatusGetCredentials,
kPlgdDpsStatusHasCredentials, kPlgdDpsStatusGetAcls,
kPlgdDpsStatusHasAcls, kPlgdDpsStatusGetCloud,
kPlgdDpsStatusHasCloud, kPlgdDpsStatusProvisioned,
kPlgdDpsStatusRenewCredentials, kPlgdDpsStatusTransientFailure,
kPlgdDpsStatusFailure,
};

std::stringstream ss{ buffer.data() };
std::string s;
while (std::getline(ss, s, '|')) {
EXPECT_EQ(1, flags.erase(s));
}
if (!flags.empty()) {
std::cout << "missing flags: ";
for (const auto &f : flags) {
std::cout << f << " ";
}
std::cout << std::endl;
}
EXPECT_TRUE(flags.empty());
}

class TestDPSWithDevice : public testing::Test {
private:
static int AppInit()
{
if (oc_init_platform("Samsung", nullptr, nullptr) != 0) {
return -1;
}
if (oc_add_device("/oic/d", "oic.d.light", "Lamp", "ocf.1.0.0",
"ocf.res.1.0.0", nullptr, nullptr) != 0) {
return -1;
}
return 0;
}

static void SignalEventLoop()
{
// no-op for tests
}

public:
void SetUp() override
{
static oc_handler_t handler{};
handler.init = AppInit;
handler.signal_event_loop = SignalEventLoop;
ASSERT_EQ(0, oc_main_init(&handler));
ASSERT_EQ(kDeviceID, oc_core_get_num_devices() - 1);
ASSERT_EQ(0, plgd_dps_init());
}
void TearDown() override
{
plgd_dps_shutdown();
oc_main_shutdown();
}
};

TEST_F(TestDPSWithDevice, GetContext)
{
EXPECT_NE(nullptr, plgd_dps_get_context(kDeviceID));

size_t invalidDeviceID = 42;
EXPECT_EQ(nullptr, plgd_dps_get_context(invalidDeviceID));
}

TEST_F(TestDPSWithDevice, SetSelfOwned)
{
auto ctx = dps::make_unique_context(kDeviceID);
EXPECT_FALSE(dps_is_self_owned(ctx.get()));

EXPECT_TRUE(dps_set_self_owned(ctx.get()));
EXPECT_TRUE(dps_is_self_owned(ctx.get()));
EXPECT_FALSE(dps_has_owner(ctx.get()));
}

TEST_F(TestDPSWithDevice, SetOwned)
{
auto ctx = dps::make_unique_context(kDeviceID);
EXPECT_FALSE(dps_has_owner(ctx.get()));

oc_uuid_t owner;
oc_gen_uuid(&owner);
EXPECT_TRUE(dps_set_owner(ctx.get(), &owner));
EXPECT_FALSE(dps_is_self_owned(ctx.get()));
EXPECT_TRUE(dps_has_owner(ctx.get()));
}

TEST_F(TestDPSWithDevice, SetDpsResource)
{
auto ctx = dps::make_unique_context(kDeviceID);

auto hasDpsResource = [](size_t device) -> bool {
return oc_ri_get_app_resource_by_uri(PLGD_DPS_URI, sizeof(PLGD_DPS_URI) - 1,
device) != nullptr;
};
EXPECT_FALSE(hasDpsResource(kDeviceID));

plgd_dps_set_configuration_resource(ctx.get(), false);
EXPECT_EQ(nullptr, ctx->conf);
EXPECT_FALSE(hasDpsResource(kDeviceID));

plgd_dps_set_configuration_resource(ctx.get(), true);
EXPECT_NE(nullptr, ctx->conf);
EXPECT_TRUE(hasDpsResource(kDeviceID));

plgd_dps_set_configuration_resource(ctx.get(), false);
EXPECT_EQ(nullptr, ctx->conf);
EXPECT_FALSE(hasDpsResource(kDeviceID));
}

TEST_F(TestDPSWithDevice, SetIdentityChain)
{
// invalid deviceID
EXPECT_FALSE(dps_try_set_identity_chain(42));
}

TEST_F(TestDPSWithDevice, CloudAPI)
{
EXPECT_FALSE(dps_cloud_is_started(kDeviceID));
EXPECT_FALSE(dps_cloud_is_registered(kDeviceID));
EXPECT_FALSE(dps_cloud_is_logged_in(kDeviceID));

size_t invalidDeviceID = 42;
EXPECT_FALSE(dps_cloud_is_started(invalidDeviceID));
EXPECT_FALSE(dps_cloud_is_registered(invalidDeviceID));
EXPECT_FALSE(dps_cloud_is_logged_in(invalidDeviceID));
}

#endif /* OC_HAS_FEATURE_PLGD_DEVICE_PROVISIONING */
Loading

0 comments on commit 563973b

Please sign in to comment.