Skip to content

Commit

Permalink
Fix issues reported by SonarCloud and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Mar 15, 2024
1 parent 7a89f4c commit 3aa722b
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 59 deletions.
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
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
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
2 changes: 1 addition & 1 deletion port/unittest/clocktest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ TEST_F(TestClock, oc_clock_wait)
oc_clock_wait(wait_time);
oc_clock_time_t cur_stamp = oc_clock_time();

int seconds = (cur_stamp - prev_stamp) / OC_CLOCK_SECOND;
long seconds = static_cast<long>((cur_stamp - prev_stamp) / OC_CLOCK_SECOND);
EXPECT_EQ(1, seconds);
}

Expand Down
21 changes: 7 additions & 14 deletions security/unittest/keypairtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,25 +276,18 @@ TEST_F(TestKeyPair, GenerateForDevice)
{
auto generate_keypair = [](mbedtls_ecp_group_id grpid) {
OC_DBG("generate ecdsa keypair with elliptic-curve %d", (int)grpid);
EXPECT_EQ(0, oc_sec_ecdsa_count_keypairs())
<< "error for ec(" << grpid << ")";
EXPECT_EQ(0, oc_sec_ecdsa_count_keypairs());
EXPECT_TRUE(
oc_sec_ecdsa_update_or_generate_keypair_for_device(grpid,
/*device*/ 0))
<< "error for ec(" << grpid << ")";
EXPECT_EQ(1, oc_sec_ecdsa_count_keypairs())
<< "error for ec(" << grpid << ")";
/*device*/ 0));
EXPECT_EQ(1, oc_sec_ecdsa_count_keypairs());
EXPECT_TRUE(
oc_sec_ecdsa_update_or_generate_keypair_for_device(grpid,
/*device*/ 0))
<< "error for ec(" << grpid << ")";
EXPECT_EQ(1, oc_sec_ecdsa_count_keypairs())
<< "error for ec(" << grpid << ")";
/*device*/ 0));
EXPECT_EQ(1, oc_sec_ecdsa_count_keypairs());

EXPECT_NE(nullptr, oc_sec_ecdsa_get_keypair(/*device*/ 0))
<< "error for ec(" << grpid << ")";
EXPECT_EQ(nullptr, oc_sec_ecdsa_get_keypair(/*device*/ 1))
<< "error for ec(" << grpid << ")";
EXPECT_NE(nullptr, oc_sec_ecdsa_get_keypair(/*device*/ 0));
EXPECT_EQ(nullptr, oc_sec_ecdsa_get_keypair(/*device*/ 1));

oc_sec_ecdsa_free_keypairs();
};
Expand Down
16 changes: 8 additions & 8 deletions util/oc_atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ extern "C" {
(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)))

#define OC_ATOMIC
#define OC_ATOMIC_INT8_T int8_t
#define OC_ATOMIC_UINT8_T uint8_t
#define OC_ATOMIC_INT32_T int32_t
#define OC_ATOMIC_UINT32_T uint32_t
typedef int8_t OC_ATOMIC_INT8_T;
typedef uint8_t OC_ATOMIC_UINT8_T;
typedef int32_t OC_ATOMIC_INT32_T;
typedef uint32_t OC_ATOMIC_UINT32_T;

#define OC_ATOMIC_LOAD32(x) __atomic_load_n(&(x), __ATOMIC_SEQ_CST)

Expand Down Expand Up @@ -80,10 +80,10 @@ extern "C" {
#include <intrin.h>

#define OC_ATOMIC
#define OC_ATOMIC_INT8_T int8_t
#define OC_ATOMIC_UINT8_T uint8_t
#define OC_ATOMIC_INT32_T int32_t
#define OC_ATOMIC_UINT32_T uint32_t
typedef int8_t OC_ATOMIC_INT8_T;
typedef uint8_t OC_ATOMIC_UINT8_T;
typedef int32_t OC_ATOMIC_INT32_T;
typedef uint32_t OC_ATOMIC_UINT32_T;

#define OC_ATOMIC_LOAD8(x) _InterlockedOr8((&x), 0)
#define OC_ATOMIC_LOAD32(x) _InterlockedOr((&x), 0)
Expand Down
124 changes: 124 additions & 0 deletions util/unittest/memblocktest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/****************************************************************************
*
* Copyright (c) 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_memb.h"

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

struct test_data_t
{
int a;
int b;
};

class TestMemoryBlock : public testing::Test {
public:
};

TEST_F(TestMemoryBlock, Init)
{
OC_MEMB_LOCAL(oc_test_data, test_data_t, 13);
EXPECT_EQ(oc_test_data.size, sizeof(test_data_t));
oc_memb_init(&oc_test_data);
#ifdef OC_DYNAMIC_ALLOCATION
EXPECT_EQ(oc_test_data.num, 0);
#else /* !OC_DYNAMIC_ALLOCATION */
EXPECT_EQ(oc_test_data.num, 13);
#endif /* OC_DYNAMIC_ALLOCATION */
}

TEST_F(TestMemoryBlock, Alloc_Fail)
{
EXPECT_EQ(nullptr, oc_memb_alloc(nullptr));
}

TEST_F(TestMemoryBlock, Alloc)
{
OC_MEMB_LOCAL(oc_test_data, test_data_t, 13);
oc_memb_init(&oc_test_data);
auto *block1 = static_cast<test_data_t *>(oc_memb_alloc(&oc_test_data));
EXPECT_NE(nullptr, block1);
auto *block2 = static_cast<test_data_t *>(oc_memb_alloc(&oc_test_data));
EXPECT_NE(nullptr, block2);
EXPECT_NE(block1, block2);
// Add more assertions as necessary to test initialization of allocated blocks
EXPECT_FALSE(oc_memb_free(&oc_test_data, block2));
EXPECT_FALSE(oc_memb_free(&oc_test_data, block1));
}

TEST_F(TestMemoryBlock, Dealloc_Fail)
{
EXPECT_EQ(-1, oc_memb_free(nullptr, nullptr));
}

TEST_F(TestMemoryBlock, AllocExceedsLimit)
{
OC_MEMB_LOCAL(oc_test_data, test_data_t, 13);
oc_memb_init(&oc_test_data);
std::array<test_data_t *, 14> blocks;
for (int i = 0; i < 13; ++i) {
blocks[i] = static_cast<test_data_t *>(oc_memb_alloc(&oc_test_data));
EXPECT_NE(nullptr, blocks[i]);
}
blocks[13] = static_cast<test_data_t *>(oc_memb_alloc(&oc_test_data));
#ifdef OC_DYNAMIC_ALLOCATION
EXPECT_NE(nullptr, blocks[13]); // Expecting the allocation to succeed
oc_memb_free(&oc_test_data, blocks[13]);
#else /* !OC_DYNAMIC_ALLOCATION */
EXPECT_EQ(nullptr,
blocks[13]); // Expecting the allocation to fail beyond the limit
#endif /* OC_DYNAMIC_ALLOCATION */
for (int i = 0; i < 13; ++i) {
oc_memb_free(&oc_test_data, blocks[i]);
}
}

#ifndef OC_DYNAMIC_ALLOCATION

TEST_F(TestMemoryBlock, Realloc)
{
OC_MEMB_LOCAL(oc_test_data, test_data_t, 13);
oc_memb_init(&oc_test_data);
auto *block = static_cast<test_data_t *>(oc_memb_alloc(&oc_test_data));
EXPECT_NE(block, nullptr);
oc_memb_free(&oc_test_data, block);
auto *block_realloc =
static_cast<test_data_t *>(oc_memb_alloc(&oc_test_data));
EXPECT_EQ(block, block_realloc);
oc_memb_free(&oc_test_data, block_realloc);
}

TEST_F(TestMemoryBlock, InBlock)
{
OC_MEMB_LOCAL(oc_test_data1, test_data_t, 13);
oc_memb_init(&oc_test_data1);
OC_MEMB_LOCAL(oc_test_data2, test_data_t, 37);
oc_memb_init(&oc_test_data2);
auto td1 = static_cast<test_data_t *>(oc_memb_alloc(&oc_test_data1));
EXPECT_TRUE(oc_memb_inmemb(&oc_test_data1, td1));
EXPECT_FALSE(oc_memb_inmemb(&oc_test_data2, td1));
auto td2 = static_cast<test_data_t *>(oc_memb_alloc(&oc_test_data2));
EXPECT_TRUE(oc_memb_inmemb(&oc_test_data2, td2));
EXPECT_FALSE(oc_memb_inmemb(&oc_test_data1, td2));

oc_memb_free(&oc_test_data2, td2);
oc_memb_free(&oc_test_data1, td1);
}

#endif /* !OC_DYNAMIC_ALLOCATION */

0 comments on commit 3aa722b

Please sign in to comment.