Skip to content

Commit

Permalink
Add rollover test
Browse files Browse the repository at this point in the history
  • Loading branch information
tatowicz committed Oct 14, 2023
1 parent 8d43acc commit 8cb72a8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
1 change: 0 additions & 1 deletion ctp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ $ make test
## TODO
* Send file or pipe support in cli
* HAL to abstract away drivers for supported platforms
* Test 255 sequence limit rollover.

## License

Expand Down
1 change: 1 addition & 0 deletions ctp/ctp.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ typedef struct {

// Protocol interface functions
void ctp_send_frame(const CTP_Frame *frame, uint8_t len);
uint32_t ctp_send_data_sequence(uint32_t id, uint8_t *data, uint16_t length, bool fd);
uint32_t ctp_send(uint32_t id, uint8_t *data, uint32_t length, bool fd);
int32_t ctp_receive_seq(uint8_t* buffer, uint32_t buffer_size, bool fd);
int32_t ctp_receive(uint8_t *buffer, uint32_t length, bool fd);
Expand Down
50 changes: 48 additions & 2 deletions ctp/test_ctp.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "ctp.h"

#define MOCK_QUEUE_LEN 350

// Flag to capture what the mock driver send function sends
// Think of these as testing probes
Expand All @@ -19,18 +20,20 @@ typedef struct {
} MockFrame;

// Mock frame queue
MockFrame mock_frames[150];
MockFrame mock_frames[MOCK_QUEUE_LEN];
int mock_frame_count = 0;
int mock_frame_index = 0;

// Function to enqueue a mock frame
void enqueue_mock_frame(uint32_t id, uint8_t *data, uint8_t length) {

if (mock_frame_count < 150) {
if (mock_frame_count < MOCK_QUEUE_LEN) {
mock_frames[mock_frame_count].id = id;
memcpy(mock_frames[mock_frame_count].data, data, length);
mock_frames[mock_frame_count].length = length;
mock_frame_count++;
} else {
printf("[DEBUG] Mock frame queue full\n");
}
}

Expand Down Expand Up @@ -359,6 +362,9 @@ bool test_ctp_receive_self() {
assert(memcmp(received_data, expected_data2, data_len) == 0);
printf("SEQ: 0 Passed\n");

mock_frame_count = 0;
mock_frame_index = 0;

uint8_t data3[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99,
0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99,
0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99,
Expand Down Expand Up @@ -447,6 +453,40 @@ bool ctp_test_small_first_frame() {
return true;
}

bool test_ctp_seq_rollover() {
mock_frame_count = 0;
mock_frame_index = 0;

uint32_t test_id = 123;
uint8_t received_data[2048];
uint8_t expected_data[2048];

for (int i = 0; i < sizeof(expected_data); i++) {
expected_data[i] = i;
}

uint32_t bytes_sent = ctp_send(test_id, expected_data, sizeof(expected_data), false);
uint32_t data_len = ctp_receive(received_data, sizeof(expected_data), false);

printf("Bytes sent: %d\n", bytes_sent);
printf("Data len: %d\n", data_len);
assert(bytes_sent == data_len);
assert(data_len == sizeof(expected_data));
assert(memcmp(received_data, expected_data, data_len) == 0);

mock_frame_count = 0;
mock_frame_index = 0;

bytes_sent = ctp_send_data_sequence(test_id, expected_data, sizeof(expected_data), false);
data_len = ctp_receive(received_data, sizeof(expected_data), false);

assert(bytes_sent == data_len);
assert(data_len == sizeof(expected_data));
assert(memcmp(received_data, expected_data, data_len) == 0);

return true;
}


int main() {
if (test_send()) {
Expand Down Expand Up @@ -497,5 +537,11 @@ int main() {
printf("Test Receive Self FAILED.\n");
}

if (test_ctp_seq_rollover()) {
printf("Test Seq Rollover PASSED.\n");
} else {
printf("Test Seq Rollover FAILED.\n");
}

return 0;
}

0 comments on commit 8cb72a8

Please sign in to comment.