tests: modules: Add nanopb tests

Add test cases for nanopb for simple and nested complex messages.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
This commit is contained in:
Pieter De Gendt 2023-09-14 21:06:55 +02:00 committed by Carles Cufí
commit b255182a13
9 changed files with 141 additions and 0 deletions

View file

@ -0,0 +1,19 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(nanopb_tests)
list(APPEND CMAKE_MODULE_PATH ${ZEPHYR_BASE}/modules/nanopb)
include(nanopb)
zephyr_nanopb_sources(app proto/simple.proto)
zephyr_nanopb_sources(app
proto/complex.proto
proto/sub/nested.proto
)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

@ -0,0 +1,4 @@
CONFIG_ZTEST=y
CONFIG_ZTEST_NEW_API=y
CONFIG_NANOPB=y

View file

@ -0,0 +1,13 @@
/*
* Copyright (c) 2023 Basalte bv
*
* SPDX-License-Identifier: Apache-2.0
*/
syntax = "proto3";
import "sub/nested.proto";
message ComplexMessage {
NestedMessage nested = 1;
};

View file

@ -0,0 +1 @@
SimpleMessage.buffer max_size:8 fixed_length:true

View file

@ -0,0 +1,11 @@
/*
* Copyright (c) 2023 Basalte bv
*
* SPDX-License-Identifier: Apache-2.0
*/
syntax = "proto3";
message SimpleMessage {
bytes buffer = 1;
};

View file

@ -0,0 +1 @@
NestedMessage.name max_size:32

View file

@ -0,0 +1,12 @@
/*
* Copyright (c) 2023 Basalte bv
*
* SPDX-License-Identifier: Apache-2.0
*/
syntax = "proto3";
message NestedMessage {
uint32 id = 1;
string name = 2;
};

View file

@ -0,0 +1,71 @@
/*
* Copyright (c) 2023 Basalte bv
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <zephyr/kernel.h>
#include <zephyr/ztest.h>
#include <pb_encode.h>
#include <pb_decode.h>
#include <proto/simple.pb.h>
#include <proto/complex.pb.h>
ZTEST(nanopb_tests, test_nanopb_simple)
{
uint8_t buffer[SimpleMessage_size];
SimpleMessage msg = SimpleMessage_init_zero;
for (size_t i = 0; i < sizeof(msg.buffer); ++i) {
msg.buffer[i] = i;
}
pb_ostream_t ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
zassert_true(pb_encode(&ostream, SimpleMessage_fields, &msg),
"Encoding failed: %s", PB_GET_ERROR(&ostream));
/* Sanity check, clear data */
memset(&msg, 0, sizeof(SimpleMessage));
pb_istream_t istream = pb_istream_from_buffer(buffer, ostream.bytes_written);
zassert_true(pb_decode(&istream, SimpleMessage_fields, &msg),
"Decoding failed: %s", PB_GET_ERROR(&ostream));
for (size_t i = 0; i < sizeof(msg.buffer); ++i) {
zassert_equal(msg.buffer[i], i);
}
}
ZTEST(nanopb_tests, test_nanopb_nested)
{
uint8_t buffer[ComplexMessage_size];
ComplexMessage msg = ComplexMessage_init_zero;
msg.has_nested = true;
msg.nested.id = 42;
strcpy(msg.nested.name, "Test name");
pb_ostream_t ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
zassert_true(pb_encode(&ostream, ComplexMessage_fields, &msg),
"Encoding failed: %s", PB_GET_ERROR(&ostream));
/* Sanity check, clear data */
memset(&msg, 0, sizeof(ComplexMessage));
pb_istream_t istream = pb_istream_from_buffer(buffer, ostream.bytes_written);
zassert_true(pb_decode(&istream, ComplexMessage_fields, &msg),
"Decoding failed: %s", PB_GET_ERROR(&istream));
zassert_equal(42, msg.nested.id);
zassert_true(msg.has_nested);
zassert_equal(0, strcmp(msg.nested.name, "Test name"));
}
ZTEST_SUITE(nanopb_tests, NULL, NULL, NULL, NULL, NULL);

View file

@ -0,0 +1,9 @@
tests:
libraries.nanopb:
modules:
- nanopb
tags:
- nanopb
integration_platforms:
- native_posix
- native_posix_64