Skip to content

Commit

Permalink
Add OC_API annotations to oc_helpers.h
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Sep 29, 2024
1 parent fae64b4 commit 65d0f64
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 92 deletions.
8 changes: 5 additions & 3 deletions api/oc_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "port/oc_random.h"
#include "util/oc_macros_internal.h"
#include "util/oc_mmem_internal.h"
#include "util/oc_secure_string_internal.h"

#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
Expand Down Expand Up @@ -141,7 +143,7 @@ oc_set_string(oc_string_t *dst, const char *str, size_t str_len)
// oc_free_string
oc_new_string(&copy, str, str_len);
oc_free_string(dst);
oc_new_string(dst, oc_string(copy), oc_string_len(copy));
oc_new_string(dst, oc_string(copy), oc_string_len_unsafe(copy));
oc_free_string(&copy);
#endif /* OC_DYNAMIC_ALLOCATION */
}
Expand Down Expand Up @@ -311,7 +313,7 @@ bool
_oc_copy_string_to_array(oc_string_array_t *ocstringarray, const char str[],
size_t index)
{
size_t len = strlen(str);
size_t len = oc_strnlen(str, STRING_ARRAY_ITEM_MAX_LEN);
if (len >= STRING_ARRAY_ITEM_MAX_LEN) {
return false;
}
Expand Down Expand Up @@ -351,7 +353,7 @@ oc_join_string_array(oc_string_array_t *ocstringarray, oc_string_t *ocstring)
++i) {
const char *item =
(const char *)oc_string_array_get_item(*ocstringarray, i);
size_t item_len = strlen(item);
size_t item_len = oc_strnlen(item, STRING_ARRAY_ITEM_MAX_LEN);
if (item_len != 0) {
if (len > 0) {
oc_string(*ocstring)[len] = ' ';
Expand Down
26 changes: 15 additions & 11 deletions api/unittest/buffersettingstest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#ifdef OC_DYNAMIC_ALLOCATION

TEST(BufferSettings, SetMTUSize)
TEST(TestBufferSettings, SetMTUSize)
{
#ifdef OC_INOUT_BUFFER_SIZE
EXPECT_EQ(-1, oc_set_mtu_size(42));
Expand All @@ -36,49 +36,53 @@ TEST(BufferSettings, SetMTUSize)
#endif /* OC_INOUT_BUFFER_SIZE */
}

TEST(BufferSettings, SetMaxAppDataSize)
#ifndef OC_APP_DATA_BUFFER_SIZE

TEST(TestBufferSettings, SetMaxAppDataSize)
{
size_t max_app_size = static_cast<size_t>(oc_get_max_app_data_size());
auto max_app_size = static_cast<size_t>(oc_get_max_app_data_size());
oc_set_max_app_data_size(42);
EXPECT_EQ(42, oc_get_max_app_data_size());

oc_set_max_app_data_size(max_app_size);
}

#if !defined(OC_APP_DATA_BUFFER_SIZE) && defined(OC_REP_ENCODING_REALLOC)
#ifdef OC_REP_ENCODING_REALLOC

TEST(BufferSettings, SetMinAppDataSize)
TEST(TestBufferSettings, SetMinAppDataSize)
{
size_t min_app_size = static_cast<size_t>(oc_get_min_app_data_size());
auto min_app_size = static_cast<size_t>(oc_get_min_app_data_size());
oc_set_min_app_data_size(42);
EXPECT_EQ(42, oc_get_min_app_data_size());

oc_set_min_app_data_size(min_app_size);
}

#endif /* !OC_APP_DATA_BUFFER_SIZE && OC_REP_ENCODING_REALLOC */
#endif /* OC_REP_ENCODING_REALLOC */

#endif /* !OC_APP_DATA_BUFFER_SIZE */

#else /* !OC_DYNAMIC_ALLOCATION */

TEST(BufferSettings, SetMTUSize)
TEST(TestBufferSettings, SetMTUSize)
{
EXPECT_EQ(-1, oc_set_mtu_size(42));
EXPECT_EQ(-1, oc_get_mtu_size());
}

TEST(BufferSettings, SetMaxAppDataSize)
TEST(TestBufferSettings, SetMaxAppDataSize)
{
oc_set_max_app_data_size(42);
EXPECT_EQ(-1, oc_get_max_app_data_size());
}

TEST(BufferSettings, SetMinAppDataSize)
TEST(TestBufferSettings, SetMinAppDataSize)
{
oc_set_min_app_data_size(42);
EXPECT_EQ(-1, oc_get_min_app_data_size());
}

TEST(BufferSettings, GetBlockSize)
TEST(TestBufferSettings, GetBlockSize)
{
EXPECT_EQ(-1, oc_get_block_size());
}
Expand Down
82 changes: 82 additions & 0 deletions api/unittest/helperstest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,85 @@ TEST(Helpers, RandomBuffer)

oc_random_destroy();
}

// Test error when provided hex string buffer is too small
TEST(Helpers, HexStringTooSmallError)
{
std::array<uint8_t, 4> array{ 0x1f, 0xa0, 0x5b, 0xff };
char hex_str[5]; // Too small to fit the full hex representation (8 chars +
// null terminator)
size_t hex_str_len = sizeof(hex_str);
int result = oc_conv_byte_array_to_hex_string(array.data(), array.size(),
hex_str, &hex_str_len);
ASSERT_EQ(result, -1);
}

// Test successful conversion of a byte array to hex string
TEST(Helpers, ConvertByteArrayToHexStringSuccess)
{
std::array<uint8_t, 4> array{ 0x1f, 0xa0, 0x5b, 0xff };
char hex_str[9]; // Must hold 8 characters (2 per byte) + null terminator
size_t hex_str_len = sizeof(hex_str);
int result = oc_conv_byte_array_to_hex_string(array.data(), array.size(),
hex_str, &hex_str_len);
ASSERT_EQ(result, 0);
EXPECT_STREQ(hex_str, "1fa05bff");
EXPECT_EQ(hex_str_len, 8);
}

// Test empty hex string input
TEST(Helpers, EmptyHexStringError)
{
std::string hex_str = "";
std::array<uint8_t, 4> array;
size_t array_len = array.size();

int result = oc_conv_hex_string_to_byte_array(
hex_str.c_str(), hex_str.length(), &array[0], &array_len);
ASSERT_EQ(result, -1);
}

// Test when array buffer is too small
TEST(Helpers, ArrayBufferTooSmallError)
{
std::string hex_str = "1fa05bff";
std::array<uint8_t, 2>
array; // Too small to hold the expected byte array (needs 4 bytes)
size_t array_len = array.size();

int result = oc_conv_hex_string_to_byte_array(
hex_str.c_str(), hex_str.length(), &array[0], &array_len);
ASSERT_EQ(result, -1);
}

// Test successful conversion of an even-length hex string to byte array
TEST(Helpers, EvenLengthHexStringSuccess)
{
std::string hex_str = "1fa05bff";
std::array<uint8_t, 4> array;
size_t array_len = array.size();

int result = oc_conv_hex_string_to_byte_array(
hex_str.c_str(), hex_str.length(), &array[0], &array_len);

ASSERT_EQ(result, 0);
EXPECT_EQ(array_len, 4);
std::array<uint8_t, 4> expected_array = { 0x1f, 0xa0, 0x5b, 0xff };
EXPECT_EQ(expected_array, array);
}

// Test successful conversion of an odd-length hex string to byte array
TEST(Helpers, OddLengthHexStringSuccess)
{
std::string hex_str = "fa05bf";
std::array<uint8_t, 3> array;
size_t array_len = array.size();

int result = oc_conv_hex_string_to_byte_array(
hex_str.c_str(), hex_str.length(), &array[0], &array_len);

ASSERT_EQ(result, 0);
EXPECT_EQ(array_len, 3);
std::array<uint8_t, 3> expected_array = { 0xfa, 0x05, 0xbf };
EXPECT_EQ(expected_array, array);
}
Loading

0 comments on commit 65d0f64

Please sign in to comment.