Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues reported by SonarCloud and add tests #617

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions api/cloud/oc_cloud_rd.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ static void
cloud_publish_resources(oc_cloud_context_t *ctx)
{
#ifdef OC_SECURITY
if (!oc_sec_pstat_is_in_dos_state(ctx->device,
OC_PSTAT_DOS_ID_FLAG(OC_DOS_RFNOP))) {
if (!oc_device_is_in_dos_state(ctx->device,
OC_PSTAT_DOS_ID_FLAG(OC_DOS_RFNOP))) {
OC_CLOUD_DBG("cannot publish resource links when not in RFNOP");
return;
}
Expand Down Expand Up @@ -253,8 +253,8 @@ cloud_delete_resources(oc_cloud_context_t *ctx)
{
assert(ctx->rd_delete_resources != NULL);
#ifdef OC_SECURITY
if (!oc_sec_pstat_is_in_dos_state(ctx->device,
OC_PSTAT_DOS_ID_FLAG(OC_DOS_RFNOP))) {
if (!oc_device_is_in_dos_state(ctx->device,
OC_PSTAT_DOS_ID_FLAG(OC_DOS_RFNOP))) {
OC_CLOUD_DBG("cannot unpublish resource links when not in RFNOP");
return;
}
Expand Down
1 change: 1 addition & 0 deletions api/oc_collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#ifdef OC_SECURITY
#include "security/oc_acl_internal.h"
#include "security/oc_acl_util_internal.h"
#endif /* OC_SECURITY */

#ifdef OC_HAS_FEATURE_ETAG
Expand Down
28 changes: 14 additions & 14 deletions api/oc_core_res.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#endif /* OC_MNT */

#ifdef OC_SECURITY
#include "security/oc_acl_internal.h"
#include "security/oc_doxm_internal.h"
#include "security/oc_pstat_internal.h"
#include "security/oc_roles_internal.h"
Expand Down Expand Up @@ -86,6 +87,12 @@ static oc_device_info_t g_oc_device_info[OC_MAX_NUM_DEVICES] = { 0 };
static int g_res_latency = 0;
static OC_ATOMIC_UINT32_T g_device_count = 0;

bool
oc_is_device_resource_uri(oc_string_view_t uri)
{
return oc_resource_match_uri(OC_STRING_VIEW(OCF_D_URI), uri);
}

void
oc_core_init(void)
{
Expand Down Expand Up @@ -687,8 +694,7 @@ oc_core_get_resource_type_by_uri(const char *uri, size_t uri_len)
if (oc_is_platform_resource_uri(oc_string_view(uri, uri_len))) {
return OCF_P;
}
if (core_is_resource_uri(uri, uri_len, OCF_D_URI,
OC_CHAR_ARRAY_LEN(OCF_D_URI))) {
if (oc_is_device_resource_uri(oc_string_view(uri, uri_len))) {
return OCF_D;
}
if (oc_is_discovery_resource_uri(oc_string_view(uri, uri_len))) {
Expand All @@ -710,14 +716,12 @@ oc_core_get_resource_type_by_uri(const char *uri, size_t uri_len)
}
#endif /* OC_INTROSPECTION */
#ifdef OC_HAS_FEATURE_PLGD_TIME
if (core_is_resource_uri(uri, uri_len, PLGD_TIME_URI,
OC_CHAR_ARRAY_LEN(PLGD_TIME_URI))) {
if (plgd_is_time_resource_uri(oc_string_view(uri, uri_len))) {
return PLGD_TIME;
}
#endif /* OC_HAS_FEATURE_PLGD_TIME */
#ifdef OC_WKCORE
if (core_is_resource_uri(uri, uri_len, OC_WELLKNOWNCORE_URI,
OC_CHAR_ARRAY_LEN(OC_WELLKNOWNCORE_URI))) {
if (oc_is_wkcore_resource_uri(oc_string_view(uri, uri_len))) {
return WELLKNOWNCORE;
}
#endif /* OC_WKCORE */
Expand All @@ -733,19 +737,16 @@ oc_core_get_resource_type_by_uri(const char *uri, size_t uri_len)
}
#endif /* OC_CLIENT && OC_SERVER && OC_CLOUD */
#ifdef OC_SECURITY
if (core_is_resource_uri(uri, uri_len, "/oic/sec/pstat",
OC_CHAR_ARRAY_LEN("/oic/sec/pstat"))) {
if (oc_sec_is_pstat_resource_uri(oc_string_view(uri, uri_len))) {
return OCF_SEC_PSTAT;
}
if (oc_sec_is_doxm_resource_uri(oc_string_view(uri, uri_len))) {
return OCF_SEC_DOXM;
}
if (core_is_resource_uri(uri, uri_len, "/oic/sec/acl2",
OC_CHAR_ARRAY_LEN("/oic/sec/acl2"))) {
if (oc_sec_is_acl_resource_uri(oc_string_view(uri, uri_len))) {
return OCF_SEC_ACL;
}
if (core_is_resource_uri(uri, uri_len, "/oic/sec/cred",
OC_CHAR_ARRAY_LEN("/oic/sec/cred"))) {
if (oc_sec_is_cred_resource_uri(oc_string_view(uri, uri_len))) {
return OCF_SEC_CRED;
}
if (core_is_resource_uri(uri, uri_len, "/oic/sec/ael",
Expand All @@ -761,8 +762,7 @@ oc_core_get_resource_type_by_uri(const char *uri, size_t uri_len)
OC_CHAR_ARRAY_LEN(OCF_SEC_CSR_URI))) {
return OCF_SEC_CSR;
}
if (core_is_resource_uri(uri, uri_len, OCF_SEC_ROLES_URI,
OC_CHAR_ARRAY_LEN(OCF_SEC_ROLES_URI))) {
if (oc_sec_is_roles_resource_uri(oc_string_view(uri, uri_len))) {
return OCF_SEC_ROLES;
}
#endif /* OC_PKI */
Expand Down
6 changes: 6 additions & 0 deletions api/oc_core_res_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef OC_CORE_RES_INTERNAL_H
#define OC_CORE_RES_INTERNAL_H

#include "api/oc_helpers_internal.h"
Danielius1922 marked this conversation as resolved.
Show resolved Hide resolved
#include "oc_api.h"
#include "oc_core_res.h"
#include "oc_helpers.h"
Expand All @@ -37,6 +38,11 @@ extern "C" {
#define OCF_D_URI "/oic/d"
#define OCF_D_RT "oic.wk.d"

/** @brief Check if the URI matches the device resource URI (with or without
* the leading slash)
*/
bool oc_is_device_resource_uri(oc_string_view_t uri);

/**
* @brief initialize the core functionality
*/
Expand Down
7 changes: 7 additions & 0 deletions api/oc_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "security/oc_tls_internal.h"
#ifdef OC_RES_BATCH_SUPPORT
#include "security/oc_acl_internal.h"
#include "security/oc_acl_util_internal.h"
#endif /* OC_RES_BATCH_SUPPORT*/
#endif /* OC_SECURITY */

Expand Down Expand Up @@ -1266,6 +1267,12 @@ oc_create_wkcore_resource(size_t device)
OC_WELLKNOWNCORE_RT);
}

bool
oc_is_wkcore_resource_uri(oc_string_view_t uri)
{
return oc_resource_match_uri(OC_STRING_VIEW(OC_WELLKNOWNCORE_URI), uri);
}

#endif /* OC_WKCORE */

void
Expand Down
6 changes: 6 additions & 0 deletions api/oc_discovery_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,14 @@ uint64_t oc_discovery_get_batch_etag(const oc_endpoint_t *endpoint,
#define OC_WELLKNOWNCORE_URI "/.well-known/core"
#define OC_WELLKNOWNCORE_RT "wk"

/** @brief Create /.well-known/core resource */
void oc_create_wkcore_resource(size_t device);

/** @brief Check if the URI matches the wkcore resource URI (with or without
* the leading slash)
*/
bool oc_is_wkcore_resource_uri(oc_string_view_t uri);

#endif /* OC_WKCORE */

#ifdef __cplusplus
Expand Down
4 changes: 4 additions & 0 deletions api/oc_enums_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
extern "C" {
#endif

#define OC_PERM_ALL \
(OC_PERM_CREATE | OC_PERM_RETRIEVE | OC_PERM_UPDATE | OC_PERM_DELETE | \
OC_PERM_NOTIFY)

/** @brief Convert enum value to string view */
oc_string_view_t oc_enum_to_string_view(oc_enum_t val);

Expand Down
6 changes: 2 additions & 4 deletions api/oc_etag.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ bool
oc_etag_dump_ignore_resource(const char *uri, size_t uri_len)
{
#ifdef OC_WKCORE
if (uri_len == OC_CHAR_ARRAY_LEN(OC_WELLKNOWNCORE_URI) &&
memcmp(uri, OC_WELLKNOWNCORE_URI, uri_len) == 0) {
if (oc_is_wkcore_resource_uri(oc_string_view(uri, uri_len))) {
Danielius1922 marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
#endif /* OC_WKCORE */
Expand Down Expand Up @@ -441,8 +440,7 @@ static bool
etag_can_update_device(size_t device)
{
#ifdef OC_SECURITY
return oc_sec_pstat_is_in_dos_state(device,
OC_PSTAT_DOS_ID_FLAG(OC_DOS_RFNOP));
return oc_device_is_in_dos_state(device, OC_PSTAT_DOS_ID_FLAG(OC_DOS_RFNOP));
Danielius1922 marked this conversation as resolved.
Show resolved Hide resolved
#else /* OC_SECURITY */
(void)device;
return true;
Expand Down
1 change: 1 addition & 0 deletions api/oc_ri.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@

#ifdef OC_SECURITY
#include "security/oc_acl_internal.h"
#include "security/oc_acl_util_internal.h"
#include "security/oc_audit_internal.h"
#include "security/oc_pstat_internal.h"
#include "security/oc_roles_internal.h"
Expand Down
6 changes: 6 additions & 0 deletions api/plgd/plgd_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,12 @@ plgd_time_create_resource(void)
/*delete*/ NULL, 1, PLGD_TIME_RT);
}

bool
plgd_is_time_resource_uri(oc_string_view_t uri)
{
return oc_resource_match_uri(OC_STRING_VIEW(PLGD_TIME_URI), uri);
}

void
plgd_time_configure(bool use_in_mbedtls,
plgd_set_system_time_fn_t set_system_time,
Expand Down
6 changes: 6 additions & 0 deletions api/plgd/plgd_time_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#ifdef OC_HAS_FEATURE_PLGD_TIME

#include "api/oc_helpers_internal.h"
#include "oc_rep.h"
#include "oc_ri.h"
#include "plgd/plgd_time.h"
Expand Down Expand Up @@ -56,6 +57,11 @@ extern "C" {
*/
void plgd_time_create_resource(void);

/** @brief Check if the URI matches the plgd time resource URI (with or without
* the leading slash)
*/
bool plgd_is_time_resource_uri(oc_string_view_t uri);

typedef struct plgd_time_store_t
{
oc_clock_time_t last_synced_time;
Expand Down
12 changes: 4 additions & 8 deletions api/unittest/etagtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,13 +594,9 @@ isETagStorageEmpty(size_t device, bool platform = false)
std::string store =
platform ? OC_ETAG_PLATFORM_STORE_NAME : OC_ETAG_STORE_NAME;

long ret = oc_storage_data_load(
store.c_str(), device, [](const oc_rep_t *, size_t, void *) { return 0; },
nullptr);
if (ret > 0) {
return false;
}
return true;
return oc_storage_data_load(
store.c_str(), device,
[](const oc_rep_t *, size_t, void *) { return 0; }, nullptr) <= 0;
}

TEST_F(TestETagWithServer, DumpAndLoad)
Expand Down Expand Up @@ -688,7 +684,7 @@ TEST_F(TestETagWithServer, DumpAndLoad)
#ifdef OC_SECURITY

static bool
isPlatformResourceURI(const std::string &uri)
isPlatformResourceURI(std::string_view uri)
{
return uri == "/oic/p"
#ifdef OC_HAS_FEATURE_PLGD_TIME
Expand Down
14 changes: 8 additions & 6 deletions docker/apps/Dockerfile.cloud-server
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
FROM alpine:latest AS build
FROM alpine:3.19 AS build
ARG BUILD_TYPE=Release
ARG BUILD_ARGS
RUN apk add --no-cache cmake curl git build-base gcc linux-headers patch perl python3
COPY ./ /iotivity-lite/
RUN cd /iotivity-lite/ && git submodule update --recursive
RUN mkdir /iotivity-lite/build && \
cd /iotivity-lite/build && \
cmake -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DCMAKE_VERBOSE_MAKEFILE=ON -DBUILD_TESTING=OFF -DOC_CLOUD_ENABLED=ON ${BUILD_ARGS} .. && \
WORKDIR /iotivity-lite
RUN git submodule update --recursive
RUN mkdir /iotivity-lite/build
WORKDIR /iotivity-lite/build
RUN cmake -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DCMAKE_VERBOSE_MAKEFILE=ON -DBUILD_TESTING=OFF -DOC_CLOUD_ENABLED=ON ${BUILD_ARGS} .. && \
cmake --build . --target cloud_server

# install libfaketime
WORKDIR /
# enable struct stat64 by adding -D__USE_LARGEFILE64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
Danielius1922 marked this conversation as resolved.
Show resolved Hide resolved
RUN git clone https://github.com/wolfcw/libfaketime.git && \
cd /libfaketime/src && \
make install FAKETIME_COMPILE_CFLAGS="-DFAKE_SETTIME -DFAKE_STATELESS -D__USE_LARGEFILE64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE"

FROM alpine:latest AS service
FROM alpine:3.19 AS service
RUN apk add --no-cache bash
COPY --from=build /iotivity-lite/build/apps/cloud_server /iotivity-lite/port/linux/service
COPY --from=build /usr/local/lib/faketime/libfaketimeMT.so.1 /usr/local/lib/faketime/libfaketimeMT.so.1
Expand Down
13 changes: 8 additions & 5 deletions docker/apps/Dockerfile.cloud-server-debug
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ FROM ubuntu:22.04 AS service
ARG BUILD_TYPE=Release
ARG BUILD_ARGS
RUN apt-get update -y && \
DEBIAN_FRONTEND="noninteractive" apt-get install -y bash ca-certificates cmake curl gdb git-core gcovr g++ make patch python3 --no-install-recommends
DEBIAN_FRONTEND="noninteractive" apt-get install --no-install-recommends -y bash ca-certificates cmake curl gdb git-core gcovr g++ make patch python3 && \
apt-get clean
Danielius1922 marked this conversation as resolved.
Show resolved Hide resolved
COPY ./ /iotivity-lite/
RUN cd /iotivity-lite/ && git submodule update --recursive
RUN mkdir /iotivity-lite/build && \
cd /iotivity-lite/build && \
cmake -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DCMAKE_VERBOSE_MAKEFILE=ON -DBUILD_TESTING=OFF -DOC_CLOUD_ENABLED=ON ${BUILD_ARGS} .. && \
WORKDIR /iotivity-lite
RUN git submodule update --recursive
RUN mkdir /iotivity-lite/build
WORKDIR /iotivity-lite/build
RUN cmake -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DCMAKE_VERBOSE_MAKEFILE=ON -DBUILD_TESTING=OFF -DOC_CLOUD_ENABLED=ON ${BUILD_ARGS} .. && \
cmake --build . --target cloud_server
RUN cp /iotivity-lite/build/apps/cloud_server /iotivity-lite/port/linux/service

# install libfaketime
WORKDIR /
RUN git clone https://github.com/wolfcw/libfaketime.git && \
cd /libfaketime/src && \
make install FAKETIME_COMPILE_CFLAGS="-DFAKE_SETTIME -DFAKE_STATELESS"
Expand Down
21 changes: 13 additions & 8 deletions docker/apps/Dockerfile.cloud-server-debug-clang
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@ FROM ubuntu:22.04 AS service
ARG BUILD_TYPE=Release
ARG BUILD_ARGS
RUN apt-get update -y && \
DEBIAN_FRONTEND="noninteractive" apt-get install -y bash ca-certificates cmake curl gdb git-core gcovr g++ make patch python3 --no-install-recommends
RUN DEBIAN_FRONTEND="noninteractive" apt-get install -y clang-15
RUN update-alternatives --install /usr/bin/clang clang /usr/bin/clang-15 100 && \
DEBIAN_FRONTEND="noninteractive" apt-get install --no-install-recommends -y bash ca-certificates cmake gdb git-core make patch python3 && \
apt-get clean
# install clang
RUN DEBIAN_FRONTEND="noninteractive" apt-get install --no-install-recommends -y clang-15 && \
apt-get clean && \
update-alternatives --install /usr/bin/clang clang /usr/bin/clang-15 100 && \
update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-15 100
COPY ./ /iotivity-lite/
RUN cd /iotivity-lite/ && git submodule update --recursive
RUN mkdir /iotivity-lite/build && \
cd /iotivity-lite/build && \
cmake -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang -DBUILD_TESTING=OFF -DOC_CLOUD_ENABLED=ON ${BUILD_ARGS} .. && \
WORKDIR /iotivity-lite
RUN git submodule update --recursive
RUN mkdir /iotivity-lite/build
WORKDIR /iotivity-lite/build
RUN cmake -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang -DBUILD_TESTING=OFF -DOC_CLOUD_ENABLED=ON ${BUILD_ARGS} .. && \
cmake --build . --target cloud_server
RUN cp /iotivity-lite/build/apps/cloud_server /iotivity-lite/port/linux/service

# install libfaketime
WORKDIR /
RUN git clone https://github.com/wolfcw/libfaketime.git && \
cd /libfaketime/src && \
make install FAKETIME_COMPILE_CFLAGS="-DFAKE_SETTIME -DFAKE_STATELESS"
make CC=clang install FAKETIME_COMPILE_CFLAGS="-DFAKE_SETTIME -DFAKE_STATELESS"

COPY /docker/logbt /usr/local/bin/logbt
RUN logbt --version
Expand Down
20 changes: 11 additions & 9 deletions include/oc_rep.h
Original file line number Diff line number Diff line change
Expand Up @@ -1189,20 +1189,22 @@ typedef enum {
OC_REP_OBJECT_ARRAY = 0x0E
} oc_rep_value_type_t;

typedef union oc_rep_value {
int64_t integer;
bool boolean;
double double_p;
oc_string_t string;
oc_array_t array;
struct oc_rep_s *object;
struct oc_rep_s *object_array;
} oc_rep_value_t;

typedef struct oc_rep_s
{
oc_rep_value_type_t type;
struct oc_rep_s *next;
oc_string_t name;
union oc_rep_value {
int64_t integer;
bool boolean;
double double_p;
oc_string_t string;
oc_array_t array;
struct oc_rep_s *object;
struct oc_rep_s *object_array;
} value;
oc_rep_value_t value;
} oc_rep_t;

/**
Expand Down
1 change: 1 addition & 0 deletions messaging/coap/observe.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@

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

Expand Down
Loading
Loading