-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds test for csp_crc32_verify . Signed-off-by: Gaetan Perrot <[email protected]>
- Loading branch information
1 parent
c6da7df
commit 1afe4b6
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ project(speq) | |
target_sources(app PRIVATE | ||
src/main.c | ||
src/buffer.c | ||
src/crc32.c | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright (c) 2024 Space Cubics, LLC. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
#include <zephyr/ztest.h> | ||
#include <csp/csp.h> | ||
#include <csp/csp_crc32.h> | ||
#include <stdio.h> | ||
#include <endian.h> | ||
#include <csp/csp_id.h> | ||
|
||
struct crc_fixture { | ||
csp_packet_t *buf; | ||
}; | ||
|
||
static struct crc_fixture crc_fixture; | ||
|
||
static void *setup(void) | ||
{ | ||
csp_init(); | ||
|
||
return &crc_fixture; | ||
} | ||
|
||
static void before(void *data) | ||
{ | ||
struct crc_fixture *fixture = data; | ||
|
||
fixture->buf = csp_buffer_get(0); | ||
zassert_true(fixture->buf != NULL, NULL); | ||
} | ||
|
||
static void after(void *data) | ||
{ | ||
struct crc_fixture *fixture = data; | ||
|
||
csp_buffer_free(fixture->buf); | ||
} | ||
|
||
ZTEST_F(crc, test_crc_verify_err) | ||
{ | ||
csp_packet_t *packet = fixture->buf; | ||
int ret; | ||
|
||
/* check error if length < sizeof(crc) */ | ||
ret = csp_crc32_verify(packet); | ||
zassert_equal(ret,CSP_ERR_CRC32, "Error on crc32_append"); | ||
} | ||
|
||
ZTEST_F(crc, test_crc_without_header) | ||
{ | ||
csp_packet_t *packet = fixture->buf; | ||
int ret; | ||
|
||
memcpy(packet->data, "Hello", 5); | ||
packet->length = 5; | ||
|
||
/* Check without header */ | ||
csp_crc32_append(packet); | ||
ret = csp_crc32_verify(packet); | ||
zassert_equal(CSP_ERR_NONE, ret); | ||
} | ||
|
||
ZTEST_F(crc, test_crc_with_header) | ||
{ | ||
csp_packet_t *packet = fixture->buf; | ||
uint32_t crc; | ||
int ret; | ||
|
||
memcpy(packet->data, "Hello", 5); | ||
packet->length = 5; | ||
|
||
/* Check with header */ | ||
/* I simulate CSP_21 define on csp_crc32_append */ | ||
csp_id_prepend(packet); | ||
crc = csp_crc32_memory(packet->frame_begin, packet->frame_length); | ||
printf("crc w/ header %x\n", crc); | ||
/* Convert to network byte order */ | ||
crc = htobe32(crc); | ||
/* Copy checksum to packet */ | ||
memcpy(&packet->data[packet->length], &crc, sizeof(crc)); | ||
packet->length += sizeof(crc); | ||
|
||
ret = csp_crc32_verify(packet); | ||
zassert_equal(0, ret); | ||
} | ||
|
||
ZTEST_SUITE(crc, NULL, setup, before, after, NULL); |