Skip to content

Commit

Permalink
test: improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zfields committed Sep 8, 2023
1 parent d48d585 commit 97ee798
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions test/src/NoteBinaryReceiveRange_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ FAKE_VOID_FUNC(NoteUnlockNote)
// a compiler error.
uint8_t buf[32];
uint32_t bufLen = sizeof(buf);
uint32_t dataLen = 17;
uint32_t decodedOffset = 0;
uint32_t decodedLen = 17;

char rawMsg[] = "Hello Blues!";
uint32_t rawMsgLen = strlen(rawMsg);
Expand All @@ -52,8 +53,6 @@ SCENARIO("NoteBinaryReceiveRange")
RESET_FAKE(NoteLockNote);
RESET_FAKE(NoteUnlockNote);

const uint32_t OFFSET_ZERO = 0;

NoteSetFnDefault(malloc, free, NULL, NULL);

// These fakes are the default. Tests below may override them to exercise
Expand All @@ -78,12 +77,36 @@ SCENARIO("NoteBinaryReceiveRange")
return rsp;
};

GIVEN("Bad parameters are supplied") {
WHEN("buffer is NULL") {
const char *err = NoteBinaryReceiveRange(NULL, bufLen, decodedOffset, decodedLen);

THEN("An error is returned") {
CHECK(err != NULL);
}
}
WHEN("bufLen is too small") {
const char *err = NoteBinaryReceiveRange(buf, bufLen, decodedOffset, bufLen);

THEN("An error is returned") {
CHECK(err != NULL);
}
}
WHEN("decodedLen is zero") {
const char *err = NoteBinaryReceiveRange(buf, bufLen, decodedOffset, 0);

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

GIVEN("Allocating the card.binary.get request fails") {
NoteNewRequest_fake.custom_fake = NULL;
NoteNewRequest_fake.return_val = NULL;

WHEN("NoteBinaryReceiveRange is called") {
const char *err = NoteBinaryReceiveRange(buf, bufLen, OFFSET_ZERO, dataLen);
const char *err = NoteBinaryReceiveRange(buf, bufLen, decodedOffset, decodedLen);

REQUIRE(NoteNewRequest_fake.call_count > 0);
THEN("An error is returned") {
Expand All @@ -102,7 +125,7 @@ SCENARIO("NoteBinaryReceiveRange")
};

WHEN("NoteBinaryReceiveRange is called") {
const char *err = NoteBinaryReceiveRange(buf, bufLen, OFFSET_ZERO, dataLen);
const char *err = NoteBinaryReceiveRange(buf, bufLen, decodedOffset, decodedLen);

REQUIRE(NoteRequestResponse_fake.call_count > 0);
THEN("An error is returned") {
Expand All @@ -115,7 +138,7 @@ SCENARIO("NoteBinaryReceiveRange")
NoteChunkedReceive_fake.return_val = "some error";

WHEN("NoteBinaryReceiveRange is called") {
const char *err = NoteBinaryReceiveRange(buf, bufLen, OFFSET_ZERO, dataLen);
const char *err = NoteBinaryReceiveRange(buf, bufLen, decodedOffset, decodedLen);

REQUIRE(NoteChunkedReceive_fake.call_count > 0);
THEN("An error is returned") {
Expand All @@ -134,7 +157,7 @@ SCENARIO("NoteBinaryReceiveRange")
};

WHEN("NoteBinaryReceiveRange is called") {
const char *err = NoteBinaryReceiveRange(buf, bufLen, OFFSET_ZERO, dataLen);
const char *err = NoteBinaryReceiveRange(buf, bufLen, decodedOffset, decodedLen);

REQUIRE(NoteChunkedReceive_fake.call_count > 0);
THEN("An error is returned") {
Expand Down Expand Up @@ -166,7 +189,7 @@ SCENARIO("NoteBinaryReceiveRange")
};

WHEN("NoteBinaryReceiveRange is called") {
const char *err = NoteBinaryReceiveRange(buf, bufLen, OFFSET_ZERO, dataLen);
const char *err = NoteBinaryReceiveRange(buf, bufLen, decodedOffset, decodedLen);

REQUIRE(NoteChunkedReceive_fake.call_count > 0);
REQUIRE(NoteRequestResponse_fake.call_count > 0);
Expand All @@ -178,8 +201,8 @@ SCENARIO("NoteBinaryReceiveRange")

AND_GIVEN("The computed MD5 matches the status field") {
WHEN("NoteBinaryReceiveRange is called") {
uint32_t dataLen = rawMsgLen;
const char *err = NoteBinaryReceiveRange(buf, bufLen, OFFSET_ZERO, dataLen);
uint32_t decodedLen = rawMsgLen;
const char *err = NoteBinaryReceiveRange(buf, bufLen, decodedOffset, decodedLen);

REQUIRE(NoteChunkedReceive_fake.call_count > 0);
THEN("No error is returned") {
Expand All @@ -188,12 +211,11 @@ SCENARIO("NoteBinaryReceiveRange")

THEN("The decoded payload is as expected, with no trailing "
"newline") {
CHECK(memcmp(buf, rawMsg, dataLen) == 0);
CHECK(memcmp(buf, rawMsg, decodedLen) == 0);
}
}
}
}
CHECK(NoteLockNote_fake.call_count > 0);
CHECK(NoteLockNote_fake.call_count == NoteUnlockNote_fake.call_count);
}

Expand Down

0 comments on commit 97ee798

Please sign in to comment.