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

chore: eliminate [in/out] parameters #94

Merged
merged 24 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ target_compile_options(
-Wextra
-Wpedantic
-Werror
-Og
-ggdb
)
target_include_directories(
note_c
Expand Down
183 changes: 125 additions & 58 deletions n_helpers.c

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions note.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,18 +317,21 @@ void NoteMD5HashToString(unsigned char *hash, char *strbuf, unsigned long buflen

// High-level helper functions that are both useful and serve to show developers
// how to call the API
#define NOTE_C_BINARY_RX_ALL 0
const char * NoteBinaryDataDecodedLength(uint32_t *len);
const char * NoteBinaryDataEncodedLength(uint32_t *len);
const char * NoteBinaryDataReset(void);
const char * NoteBinaryDecode(const uint8_t *inBuf, uint32_t inLen,
uint8_t *outBuf, uint32_t *outLen);
uint8_t *outBuf, uint32_t outLen,
uint32_t *decLen);
const char * NoteBinaryEncode(const uint8_t *inBuf, uint32_t inLen,
uint8_t *outBuf, uint32_t *outLen);
uint8_t *outBuf, uint32_t outLen,
uint32_t *encLen);
uint32_t NoteBinaryMaxDecodedLength(uint32_t bufferSize);
uint32_t NoteBinaryMaxEncodedLength(uint32_t unencodedLength);
const char * NoteBinaryReceive(uint8_t *buffer, uint32_t bufLen,
uint32_t decodedOffset, uint32_t *decodedLen);
uint32_t decodedOffset, uint32_t decodedLen);
const char * NoteBinaryReceiveAll(uint8_t *buffer, uint32_t bufLen,
uint32_t *dataLen);
const char * NoteBinaryTransmit(uint8_t *unencodedData, uint32_t unencodedLen,
uint32_t bufLen, uint32_t notecardOffset);
uint32_t NoteSetSTSecs(uint32_t secs);
Expand Down
5 changes: 5 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ macro(add_test TEST_NAME)
PRIVATE note_c
PRIVATE Catch2::Catch2WithMain
)
target_compile_options(${TEST_NAME}
PRIVATE -Og
PRIVATE -ggdb
)

list(APPEND TEST_TARGETS ${TEST_NAME})

Expand Down Expand Up @@ -152,6 +156,7 @@ add_test(NoteBinaryEncode_test)
add_test(NoteBinaryMaxEncodedLength_test)
add_test(NoteBinaryMaxDecodedLength_test)
add_test(NoteBinaryReceive_test)
add_test(NoteBinaryReceiveAll_test)
add_test(NoteBinaryTransmit_test)

if(NOTE_C_COVERAGE)
Expand Down
22 changes: 11 additions & 11 deletions test/src/NoteBinaryDecode_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,44 @@ DEFINE_FFF_GLOBALS
FAKE_VALUE_FUNC(uint32_t, cobsDecode, uint8_t *, uint32_t, uint8_t, uint8_t *)

uint8_t inBuf[12];
uint32_t inLen;
const uint32_t inLen = sizeof(inBuf);
uint8_t outBuf[10];
uint32_t outLen;
const uint32_t outLen = sizeof(outBuf);
uint32_t decLen;

namespace
{

SCENARIO("NoteBinaryDecode")
{
RESET_FAKE(cobsDecode);
uint32_t inLen = sizeof(inBuf);
uint32_t outLen = sizeof(outBuf);
decLen = 0;

GIVEN("Bad parameters are supplied") {
WHEN("inBuf is NULL") {
const char *err = NoteBinaryDecode(NULL, inLen, outBuf, &outLen);
const char *err = NoteBinaryDecode(NULL, inLen, outBuf, outLen, &decLen);

THEN("An error is returned") {
CHECK(err != NULL);
}
}
WHEN("outBuf is NULL") {
const char *err = NoteBinaryDecode(inBuf, inLen, NULL, &outLen);
const char *err = NoteBinaryDecode(inBuf, inLen, NULL, outLen, &decLen);

THEN("An error is returned") {
CHECK(err != NULL);
}
}
WHEN("outLen is NULL") {
const char *err = NoteBinaryDecode(inBuf, inLen, outBuf, NULL);
WHEN("decLen is NULL") {
const char *err = NoteBinaryDecode(inBuf, inLen, outBuf, outLen, NULL);

THEN("An error is returned") {
CHECK(err != NULL);
}
}
WHEN("outLen is less than the size required for the worst-case decoding") {
uint32_t badOutLen = (cobsGuaranteedFit(inLen) - 1);
const char *err = NoteBinaryDecode(inBuf, inLen, outBuf, &badOutLen);
const char *err = NoteBinaryDecode(inBuf, inLen, outBuf, badOutLen, &decLen);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -70,7 +70,7 @@ SCENARIO("NoteBinaryDecode")
GIVEN("Parameters are in order") {
const uint32_t EXPECTED_RESULT = 79;
cobsDecode_fake.return_val = EXPECTED_RESULT;
const char *err = NoteBinaryDecode(inBuf, inLen, outBuf, &outLen);
const char *err = NoteBinaryDecode(inBuf, inLen, outBuf, outLen, &decLen);

THEN("cobsDecode is invoked") {
CHECK(cobsDecode_fake.call_count > 0);
Expand All @@ -84,7 +84,7 @@ SCENARIO("NoteBinaryDecode")
}

THEN("The result is returned without modification") {
CHECK(EXPECTED_RESULT == outLen);
CHECK(EXPECTED_RESULT == decLen);
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions test/src/NoteBinaryEncode_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,44 @@ DEFINE_FFF_GLOBALS
FAKE_VALUE_FUNC(uint32_t, cobsEncode, uint8_t *, uint32_t, uint8_t, uint8_t *)

uint8_t inBuf[10] = "Hi there!";
uint32_t inLen;
uint32_t inLen = strlen((const char *)inBuf);
uint8_t outBuf[12];
uint32_t outLen;
uint32_t outLen = sizeof(outBuf);
uint32_t encLen;

namespace
{

SCENARIO("NoteBinaryEncode")
{
RESET_FAKE(cobsEncode);
uint32_t inLen = strlen((const char *)inBuf);
uint32_t outLen = sizeof(outBuf);
encLen = 0;

GIVEN("Bad parameters are supplied") {
WHEN("inBuf is NULL") {
const char *err = NoteBinaryEncode(NULL, inLen, outBuf, &outLen);
const char *err = NoteBinaryEncode(NULL, inLen, outBuf, outLen, &encLen);

THEN("An error is returned") {
CHECK(err != NULL);
}
}
WHEN("outBuf is NULL") {
const char *err = NoteBinaryEncode(inBuf, inLen, NULL, &outLen);
const char *err = NoteBinaryEncode(inBuf, inLen, NULL, outLen, &encLen);

THEN("An error is returned") {
CHECK(err != NULL);
}
}
WHEN("outLen is NULL") {
const char *err = NoteBinaryEncode(inBuf, inLen, outBuf, NULL);
WHEN("encLen is NULL") {
const char *err = NoteBinaryEncode(inBuf, inLen, outBuf, outLen, NULL);

THEN("An error is returned") {
CHECK(err != NULL);
}
}
WHEN("outLen is less than the size required for in place encoding") {
uint32_t badOutLen = (cobsEncodedLength(inBuf, inLen) - 1);
const char *err = NoteBinaryEncode(inBuf, inLen, outBuf, &badOutLen);
const char *err = NoteBinaryEncode(inBuf, inLen, outBuf, badOutLen, &encLen);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -72,7 +72,7 @@ SCENARIO("NoteBinaryEncode")
GIVEN("Parameters are in order") {
const uint32_t EXPECTED_RESULT = 79;
cobsEncode_fake.return_val = EXPECTED_RESULT;
const char *err = NoteBinaryEncode(inBuf, inLen, outBuf, &outLen);
const char *err = NoteBinaryEncode(inBuf, inLen, outBuf, outLen, &encLen);

THEN("cobsEncode is invoked") {
CHECK(cobsEncode_fake.call_count > 0);
Expand All @@ -86,7 +86,7 @@ SCENARIO("NoteBinaryEncode")
}

THEN("The result is returned without modification") {
CHECK(EXPECTED_RESULT == outLen);
CHECK(EXPECTED_RESULT == encLen);
}
}
}
Expand Down
124 changes: 124 additions & 0 deletions test/src/NoteBinaryReceiveAll_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*!
* @file NoteBinaryReceiveAll_test.cpp
*
* Written by the Blues Inc. team.
*
* Copyright (c) 2023 Blues Inc. MIT License. Use of this source code is
* governed by licenses granted by the copyright holder including that found in
* the
* <a href="https://github.com/blues/note-c/blob/master/LICENSE">LICENSE</a>
* file.
*
*/

#ifdef NOTE_C_TEST

#include <catch2/catch_test_macros.hpp>
#include "fff.h"

#include "n_lib.h"

DEFINE_FFF_GLOBALS
FAKE_VALUE_FUNC(const char *, NoteBinaryDataDecodedLength, uint32_t *)
FAKE_VALUE_FUNC(const char *, NoteBinaryReceive, uint8_t *, uint32_t, uint32_t, uint32_t)

uint8_t buffer[12];
const uint32_t bufLen = sizeof(buffer);
uint32_t dataLen = 0;

namespace
{

SCENARIO("NoteBinaryReceiveAll")
{
RESET_FAKE(NoteBinaryDataDecodedLength);
RESET_FAKE(NoteBinaryReceive);
dataLen = 17;

GIVEN("Bad parameters are supplied") {
WHEN("buffer is NULL") {
const char *err = NoteBinaryReceiveAll(NULL, bufLen, &dataLen);

THEN("An error is returned") {
CHECK(err != NULL);
}
}
WHEN("dataLen is NULL") {
const char *err = NoteBinaryReceiveAll(buffer, bufLen, NULL);

THEN("An error is returned") {
CHECK(err != NULL);
}
}
}

GIVEN("NoteBinaryDataDecodedLength() is invoked") {
WHEN("An error is encountered") {
const char *errMsg = "ERROR! Hacking too much time!";
NoteBinaryDataDecodedLength_fake.return_val = errMsg;
const char *err = NoteBinaryReceiveAll(buffer, bufLen, &dataLen);

REQUIRE(NoteBinaryDataDecodedLength_fake.call_count > 0);
THEN("NoteBinaryReceive() is not invoked") {
CHECK(NoteBinaryReceive_fake.call_count == 0);
}
THEN("The dataLen is set to zero") {
CHECK(dataLen == 0);
}
THEN("The error is returned") {
CHECK(!strcmp(err,errMsg));
}
}
WHEN("No error is encountered") {
const uint32_t DECODED_LEN = 79;
NoteBinaryDataDecodedLength_fake.custom_fake = [](uint32_t *len) -> const char * {
*len = DECODED_LEN;
return NULL;
};
const char *err = NoteBinaryReceiveAll(buffer, bufLen, &dataLen);

REQUIRE(NoteBinaryDataDecodedLength_fake.call_count > 0);
THEN("NoteBinaryReceive() is invoked") {
CHECK(NoteBinaryReceive_fake.call_count > 0);
}
THEN("The decoded length is passed to NoteBinaryReceive()") {
CHECK(NoteBinaryReceive_fake.arg3_history[0] == DECODED_LEN);
}
}
}
GIVEN("NoteBinaryReceive() is invoked") {
const uint32_t DECODED_LEN = 79;
NoteBinaryDataDecodedLength_fake.custom_fake = [](uint32_t *len) -> const char * {
*len = DECODED_LEN;
return NULL;
};
WHEN("An error is encountered") {
const char *errMsg = "ERROR! Hacking too much time!";
NoteBinaryReceive_fake.return_val = errMsg;
const char *err = NoteBinaryReceiveAll(buffer, bufLen, &dataLen);

REQUIRE(NoteBinaryReceive_fake.call_count > 0);
THEN("The dataLen is set to zero") {
CHECK(dataLen == 0);
}
THEN("The error is returned") {
CHECK(!strcmp(err,errMsg));
}
}
WHEN("No error is encountered") {
const char *err = NoteBinaryReceiveAll(buffer, bufLen, &dataLen);

REQUIRE(NoteBinaryReceive_fake.call_count > 0);
THEN("The decoded length is returned") {
CHECK(dataLen == DECODED_LEN);
}
THEN("The return value is NULL") {
CHECK(err == NULL);
}
}
}
}

}

#endif // NOTE_C_TEST
Loading
Loading