Skip to content

Commit

Permalink
Merge branch 'zak-chunk-rx' of https://github.com/blues/note-c into m…
Browse files Browse the repository at this point in the history
…dm-card-binary-test
  • Loading branch information
m-mcgowan committed Sep 5, 2023
2 parents 5f22112 + dfc4533 commit 7acfc90
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 46 deletions.
60 changes: 51 additions & 9 deletions n_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,48 @@ NOTE_C_STATIC int ytodays(int year);

static const char BINARY_EOP = '\n';

//**************************************************************************/
/*!
@brief Get the length of the data stored on the Notecard. If there's no data
stored on the Notecard, then `*len` will return 0.
@param len [out] the length of the decoded contents of the Notecard's binary
data store.
@returns An error string on error and NULL on success.
*/
/**************************************************************************/
const char * NoteBinaryDataLength(size_t *len)
{
// Validate parameter(s)
if (!len) {
NOTE_C_LOG_ERROR("len cannot be NULL");
return ERRSTR("len cannot be NULL", c_err);
}

// Issue a "card.binary" request.
J *rsp = NoteRequestResponse(NoteNewRequest("card.binary"));
if (!rsp) {
NOTE_C_LOG_ERROR("unable to issue binary request");
return ERRSTR("unable to issue binary request", c_err);
}

// Ensure the transaction doesn't return an error and confirm the binary
// feature is available.
if (NoteResponseError(rsp)) {
const char *err = JGetString(rsp, "err");
NOTE_C_LOG_ERROR(err);
JDelete(rsp);
NOTE_C_LOG_ERROR("unexpected error received during handshake");
return ERRSTR("unexpected error received during handshake", c_bad);
}

// Examine "length" from the response to evaluate the length of the decoded
// data residing on the Notecard.
*len = JGetInt(rsp, "length");
JDelete(rsp);

return NULL;
}

//**************************************************************************/
/*!
@brief Decode a binary payload received from the Notecard.
Expand All @@ -98,8 +140,8 @@ static const char BINARY_EOP = '\n';
@returns NULL on success, else an error string pointer.
*/
/**************************************************************************/
const char *NoteBinaryDecode(const uint8_t *inBuf, uint32_t inLen,
uint8_t *outBuf, uint32_t *outLen)
const char * NoteBinaryDecode(const uint8_t *inBuf, uint32_t inLen,
uint8_t *outBuf, uint32_t *outLen)
{
if (inBuf == NULL || outBuf == NULL || outLen == NULL) {
NOTE_C_LOG_ERROR("NULL parameter");
Expand Down Expand Up @@ -128,8 +170,8 @@ const char *NoteBinaryDecode(const uint8_t *inBuf, uint32_t inLen,
@returns NULL on success, else an error string pointer.
*/
/**************************************************************************/
const char *NoteBinaryEncode(const uint8_t *inBuf, uint32_t inLen,
uint8_t *outBuf, uint32_t *outLen)
const char * NoteBinaryEncode(const uint8_t *inBuf, uint32_t inLen,
uint8_t *outBuf, uint32_t *outLen)
{
if (inBuf == NULL || outBuf == NULL || outLen == NULL) {
NOTE_C_LOG_ERROR("NULL parameter");
Expand Down Expand Up @@ -310,10 +352,10 @@ size_t NoteBinaryRequiredBuffer(size_t dataLen)
//**************************************************************************/
/*!
@brief Get the required buffer size to receive the entire binary object
stored on the Notecard. If there's no data to stored on the Notecard,
*size will return 0.
@param size [out] size required to hold the entire contents of the
Notecard's binary store.
stored on the Notecard. If there's no data stored on the Notecard,
then `*size` will return 0.
@param size [out] the size required to hold the entire contents of the
Notecard's binary data store.
@returns An error string on error and NULL on success.
*/
/**************************************************************************/
Expand Down Expand Up @@ -343,7 +385,7 @@ const char * NoteBinaryRequiredRxMaxBuffer(size_t *size)
}

// Examine "cobs" from the response to evaluate the space required to hold
// the COBS-encoded data received from the Notecard.
// the COBS-encoded data to be received from the Notecard.
long int cobs = JGetInt(rsp, "cobs");
JDelete(rsp);
if (!cobs) {
Expand Down
15 changes: 8 additions & 7 deletions note.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,19 +318,20 @@ 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 * NoteBinaryDataLength(size_t *len);
const char * NoteBinaryDecode(const uint8_t *inBuf, uint32_t inLen,
uint8_t *outBuf, uint32_t *outLen);
const char * NoteBinaryEncode(const uint8_t *inBuf, uint32_t inLen,
uint8_t *outBuf, uint32_t *outLen);
uint32_t NoteBinaryEncodedLength(const uint8_t *buf, uint32_t len);
uint32_t NoteBinaryEncodedMaxLength(uint32_t len);
const char * NoteBinaryReceive(uint8_t *buffer, size_t bufLen,
size_t offset, size_t *dataLen);
const char * NoteBinaryRequiredRxMaxBuffer(size_t *size);
size_t NoteBinaryRequiredBuffer(size_t dataLen);
const char * NoteBinaryRequiredRxMaxBuffer(size_t *size);
const char * NoteBinaryReset(void);
const char * NoteBinaryTransmit(uint8_t *data, size_t dataLen,
size_t bufLen, size_t offset);
const char *NoteBinaryDecode(const uint8_t *inBuf, uint32_t inLen,
uint8_t *outBuf, uint32_t *outLen);
const char *NoteBinaryEncode(const uint8_t *inBuf, uint32_t inLen,
uint8_t *outBuf, uint32_t *outLen);
uint32_t NoteBinaryEncodedLength(const uint8_t *buf, uint32_t len);
uint32_t NoteBinaryEncodedMaxLength(uint32_t len);
uint32_t NoteSetSTSecs(uint32_t secs);
bool NoteTimeValid(void);
bool NoteTimeValidST(void);
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ add_test(i2cChunkedReceive_test)
add_test(i2cChunkedTransmit_test)
add_test(NoteBinaryReceive_test)
add_test(NoteBinaryTransmit_test)
add_test(NoteBinaryRequiredRxBuffer_test)
add_test(NoteBinaryRequiredRxMaxBuffer_test)
add_test(NoteBinaryReset_test)

if(NOTE_C_COVERAGE)
Expand Down
40 changes: 21 additions & 19 deletions test/src/NoteBinaryReceive_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

DEFINE_FFF_GLOBALS
FAKE_VALUE_FUNC(J *, NoteNewRequest, const char *)
FAKE_VALUE_FUNC(const char *, NoteBinaryRequiredRxBuffer, size_t *)
FAKE_VALUE_FUNC(const char *, NoteBinaryRequiredRxMaxBuffer, size_t *)
FAKE_VALUE_FUNC(J *, NoteRequestResponse, J *)
FAKE_VALUE_FUNC(const char *, NoteChunkedReceive, uint8_t *, size_t *, bool,
size_t, uint32_t *)
Expand All @@ -43,23 +43,25 @@ size_t rawMsgLen = strlen(rawMsg);
namespace
{

SCENARIO("NoteBinaryReceive")
SCENARIO("NoteBinaryReceive all")
{
RESET_FAKE(NoteNewRequest);
RESET_FAKE(NoteBinaryRequiredRxBuffer);
RESET_FAKE(NoteBinaryRequiredRxMaxBuffer);
RESET_FAKE(NoteRequestResponse);
RESET_FAKE(NoteChunkedReceive);
RESET_FAKE(NoteLockNote);
RESET_FAKE(NoteUnlockNote);

const size_t offset = 0;

NoteSetFnDefault(malloc, free, NULL, NULL);

// These fakes are the default. Tests below may override them to exercise
// different scenarios.
NoteNewRequest_fake.custom_fake = [](const char *req) -> J* {
return JCreateObject();
};
NoteBinaryRequiredRxBuffer_fake.custom_fake = [](size_t *size)
NoteBinaryRequiredRxMaxBuffer_fake.custom_fake = [](size_t *size)
-> const char * {
*size = bufLen;

Expand All @@ -76,29 +78,29 @@ SCENARIO("NoteBinaryReceive")
return rsp;
};

GIVEN("NoteBinaryRequiredRxBuffer fails") {
NoteBinaryRequiredRxBuffer_fake.custom_fake = [](size_t *size)
GIVEN("NoteBinaryRequiredRxMaxBuffer fails") {
NoteBinaryRequiredRxMaxBuffer_fake.custom_fake = [](size_t *size)
-> const char * {
*size = 0;

return NULL;
};

WHEN("NoteBinaryReceive is called") {
const char *err = NoteBinaryReceive(buf, bufLen, &dataLen);
const char *err = NoteBinaryReceive(buf, bufLen, offset, &dataLen);

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

GIVEN("NoteBinaryRequiredRxBuffer indicates there's no binary data") {
NoteBinaryRequiredRxBuffer_fake.custom_fake = NULL;
NoteBinaryRequiredRxBuffer_fake.return_val = "some error";
GIVEN("NoteBinaryRequiredRxMaxBuffer indicates there's no binary data") {
NoteBinaryRequiredRxMaxBuffer_fake.custom_fake = NULL;
NoteBinaryRequiredRxMaxBuffer_fake.return_val = "some error";

WHEN("NoteBinaryReceive is called") {
const char *err = NoteBinaryReceive(buf, bufLen, &dataLen);
const char *err = NoteBinaryReceive(buf, bufLen, offset, &dataLen);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -107,15 +109,15 @@ SCENARIO("NoteBinaryReceive")
}

GIVEN("The receive buffer isn't big enough") {
NoteBinaryRequiredRxBuffer_fake.custom_fake = [](size_t *size)
NoteBinaryRequiredRxMaxBuffer_fake.custom_fake = [](size_t *size)
-> const char * {
*size = bufLen + 1;

return NULL;
};

WHEN("NoteBinaryReceive is called") {
const char *err = NoteBinaryReceive(buf, bufLen, &dataLen);
const char *err = NoteBinaryReceive(buf, bufLen, offset, &dataLen);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -128,7 +130,7 @@ SCENARIO("NoteBinaryReceive")
NoteNewRequest_fake.return_val = NULL;

WHEN("NoteBinaryReceive is called") {
const char *err = NoteBinaryReceive(buf, bufLen, &dataLen);
const char *err = NoteBinaryReceive(buf, bufLen, offset, &dataLen);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -146,7 +148,7 @@ SCENARIO("NoteBinaryReceive")
};

WHEN("NoteBinaryReceive is called") {
const char *err = NoteBinaryReceive(buf, bufLen, &dataLen);
const char *err = NoteBinaryReceive(buf, bufLen, offset, &dataLen);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -158,7 +160,7 @@ SCENARIO("NoteBinaryReceive")
NoteChunkedReceive_fake.return_val = "some error";

WHEN("NoteBinaryReceive is called") {
const char *err = NoteBinaryReceive(buf, bufLen, &dataLen);
const char *err = NoteBinaryReceive(buf, bufLen, offset, &dataLen);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -176,7 +178,7 @@ SCENARIO("NoteBinaryReceive")
};

WHEN("NoteBinaryReceive is called") {
const char *err = NoteBinaryReceive(buf, bufLen, &dataLen);
const char *err = NoteBinaryReceive(buf, bufLen, offset, &dataLen);

THEN("An error is returned") {
CHECK(err != NULL);
Expand Down Expand Up @@ -207,7 +209,7 @@ SCENARIO("NoteBinaryReceive")
};

WHEN("NoteBinaryReceive is called") {
const char *err = NoteBinaryReceive(buf, bufLen, &dataLen);
const char *err = NoteBinaryReceive(buf, bufLen, offset, &dataLen);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -217,7 +219,7 @@ SCENARIO("NoteBinaryReceive")

AND_GIVEN("The computed MD5 matches the status field") {
WHEN("NoteBinaryReceive is called") {
const char *err = NoteBinaryReceive(buf, bufLen, &dataLen);
const char *err = NoteBinaryReceive(buf, bufLen, offset, &dataLen);

THEN("No error is returned") {
CHECK(err == NULL);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* @file NoteBinaryRequiredRxBuffer_test.cpp
* @file NoteBinaryRequiredRxMaxBuffer_test.cpp
*
* Written by the Blues Inc. team.
*
Expand All @@ -26,7 +26,7 @@ const size_t cobsLen = 10;
namespace
{

SCENARIO("NoteBinaryRequiredRxBuffer")
SCENARIO("NoteBinaryRequiredRxMaxBuffer")
{
RESET_FAKE(NoteRequestResponse);

Expand All @@ -41,8 +41,8 @@ SCENARIO("NoteBinaryRequiredRxBuffer")
return NULL;
};

WHEN("NoteBinaryRequiredRxBuffer is called") {
const char *err = NoteBinaryRequiredRxBuffer(&size);
WHEN("NoteBinaryRequiredRxMaxBuffer is called") {
const char *err = NoteBinaryRequiredRxMaxBuffer(&size);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -59,8 +59,8 @@ SCENARIO("NoteBinaryRequiredRxBuffer")
return rsp;
};

WHEN("NoteBinaryRequiredRxBuffer is called") {
const char *err = NoteBinaryRequiredRxBuffer(&size);
WHEN("NoteBinaryRequiredRxMaxBuffer is called") {
const char *err = NoteBinaryRequiredRxMaxBuffer(&size);

THEN("An error is returned") {
CHECK(err != NULL);
Expand All @@ -78,8 +78,8 @@ SCENARIO("NoteBinaryRequiredRxBuffer")
return rsp;
};

WHEN("NoteBinaryRequiredRxBuffer is called") {
const char *err = NoteBinaryRequiredRxBuffer(&size);
WHEN("NoteBinaryRequiredRxMaxBuffer is called") {
const char *err = NoteBinaryRequiredRxMaxBuffer(&size);

THEN("An error is not returned") {
CHECK(err == NULL);
Expand All @@ -101,8 +101,8 @@ SCENARIO("NoteBinaryRequiredRxBuffer")
return rsp;
};

WHEN("NoteBinaryRequiredRxBuffer is called") {
const char *err = NoteBinaryRequiredRxBuffer(&size);
WHEN("NoteBinaryRequiredRxMaxBuffer is called") {
const char *err = NoteBinaryRequiredRxMaxBuffer(&size);

THEN("An error is not returned") {
CHECK(err == NULL);
Expand Down

0 comments on commit 7acfc90

Please sign in to comment.