Skip to content

Commit 691a78d

Browse files
author
Siebren Weertman
committed
Add unit tests to test generated code. SAP and SessionSetupReq
Signed-off-by: Siebren Weertman <siebren.weertman@heliox-energy.com>
1 parent b64f296 commit 691a78d

6 files changed

Lines changed: 392 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ schemas
55
venv
66
*~
77
test/*
8+
tests/build/

tests/CMakeLists.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
3+
project(cbexigen VERSION 1)
4+
5+
set(CMAKE_CXX_STANDARD 20)
6+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
7+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
8+
9+
find_package(GTest)
10+
11+
add_subdirectory(cbv2g)
12+
13+
file(GLOB_RECURSE CBV2G_TEST_SOURCES
14+
./unit/sap/*.cpp
15+
./unit/iso20/common/*.cpp
16+
./unit/iso2/*.cpp
17+
)
18+
19+
add_executable(test-${PROJECT_NAME}
20+
${CBV2G_TEST_SOURCES}
21+
)
22+
23+
target_compile_options(test-${PROJECT_NAME} PRIVATE -Wall -Werror -Wshadow)
24+
25+
target_include_directories(test-${PROJECT_NAME}
26+
PRIVATE
27+
${CMAKE_CURRENT_SOURCE_DIR}
28+
)
29+
30+
target_link_libraries(test-${PROJECT_NAME}
31+
PRIVATE
32+
gtest
33+
gtest_main
34+
gmock
35+
pthread
36+
cbv2g
37+
)
38+
39+
include(GoogleTest)
40+
gtest_discover_tests(test-${PROJECT_NAME}
41+
TEST_PREFIX ${PROJECT_NAME}
42+
TEST_LIST ${PROJECT_NAME}Tests
43+
)
44+
45+
set_tests_properties(${${PROJECT_NAME}Tests} PROPERTIES TIMEOUT 10)

tests/cbv2g/CMakeLists.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
cmake_minimum_required(VERSION 3.18)
2+
3+
set(CMAKE_C_STANDARD 11)
4+
set(CMAKE_C_STANDARD_REQUIRED ON)
5+
6+
set(CBV2G_SOURCE_PATH "../../src/output/c")
7+
8+
file(GLOB_RECURSE CBV2G_SOURCES
9+
${CBV2G_SOURCE_PATH}/common/*.c
10+
${CBV2G_SOURCE_PATH}/appHandshake/*.c
11+
${CBV2G_SOURCE_PATH}/din/*.c
12+
${CBV2G_SOURCE_PATH}/iso-2/*.c
13+
${CBV2G_SOURCE_PATH}/iso-20/*.c
14+
${CBV2G_SOURCE_PATH}/v2gtp/*.c
15+
)
16+
17+
add_library(cbv2g STATIC
18+
${CBV2G_SOURCES}
19+
)
20+
21+
target_compile_options(cbv2g PRIVATE -Werror -Wall -Wextra)
22+
23+
target_include_directories(cbv2g
24+
PUBLIC
25+
${CBV2G_SOURCE_PATH}/
26+
${CBV2G_SOURCE_PATH}/appHandshake
27+
${CBV2G_SOURCE_PATH}/common
28+
${CBV2G_SOURCE_PATH}/din
29+
${CBV2G_SOURCE_PATH}/iso-2
30+
${CBV2G_SOURCE_PATH}/iso-20
31+
${CBV2G_SOURCE_PATH}/v2gtp
32+
)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <gmock/gmock.h>
2+
#include <gtest/gtest.h>
3+
4+
#include <exi_v2gtp.h>
5+
#include <iso20_CommonMessages_Decoder.h>
6+
#include <iso20_CommonMessages_Encoder.h>
7+
8+
#include "../test_header_utils.hpp"
9+
10+
class Test_SessionSetup : public testing::Test {
11+
protected:
12+
uint8_t data[256] = {0};
13+
exi_bitstream_t stream;
14+
};
15+
16+
/** \param xml_input (that was used to generate the EXI binary using EXIficient)
17+
* <?xml version="1.0" encoding="UTF-8"?>
18+
* <ns0:SessionSetupReq xmlns:ns0="urn:iso:std:iso:15118:-20:CommonMessages">
19+
* <ns1:Header xmlns:ns1="urn:iso:std:iso:15118:-20:CommonTypes">
20+
* <ns1:SessionID>3030303030303030</ns1:SessionID>
21+
* <ns1:TimeStamp>1707896956850052</ns1:TimeStamp>
22+
* </ns1:Header>
23+
* <ns0:EVCCID>PIXV12345678901231</ns0:EVCCID>
24+
* </ns0:SessionSetupReq>
25+
*/
26+
TEST_F(Test_SessionSetup, WhenEncodingKnownSessionSetupRequest_ThenResultMatchesExpected) {
27+
static constexpr uint8_t expected[] =
28+
"\x01\xFE\x80\x02\x00\x00\x00\x28" //<- header
29+
"\x80\x8c\x04\x18\x18\x18\x18\x18\x18\x18\x18\x08\x49\xfb\x4f\xba\xba\xa8\x40\x32\x0a\x28\x24\xac\x2b\x18\x99"
30+
"\x19\x9a\x1a\x9b\x1b\x9c\x1c\x98\x18\x99\x19\x98\x80";
31+
static constexpr size_t streamLen = 0x28;
32+
exi_bitstream_init(&stream, data, sizeof(data), 8, NULL);
33+
iso20_exiDocument exiDoc = {};
34+
exiDoc.SessionSetupReq_isUsed = 1;
35+
uint8_t sessionID[] = "\x30\x30\x30\x30\x30\x30\x30\x30";
36+
setHeader(exiDoc.SessionSetupReq.Header, sessionID, 1707896956850052);
37+
setString(exiDoc.SessionSetupReq.EVCCID, "PIXV12345678901231");
38+
39+
int res = encode_iso20_exiDocument(&stream, &exiDoc);
40+
size_t len = exi_bitstream_get_length(&stream);
41+
V2GTP20_WriteHeader(&data[0], len, V2GTP20_MAINSTREAM_PAYLOAD_ID);
42+
43+
ASSERT_EQ(res, 0);
44+
ASSERT_EQ(len, streamLen);
45+
ASSERT_EQ(memcmp(data, expected, sizeof(expected) - 1), 0)
46+
<< std::string("\\x") << toHexStr(data, data + sizeof(expected) - 1, "\\x") << '\n'
47+
<< std::string("\\x") << toHexStr(&expected[0], &expected[0] + sizeof(expected) - 1, "\\x");
48+
}
49+
50+
TEST_F(Test_SessionSetup, WhenDecodingKnownSessionSetupRequest_ThenResultMatchesExpected) {
51+
uint8_t input[] =
52+
"\x01\xFE\x80\x02\x00\x00\x00\x28" //<- header
53+
"\x80\x8c\x04\x18\x18\x18\x18\x18\x18\x18\x18\x08\x49\xfb\x4f\xba\xba\xa8\x40\x32\x0a\x28\x24\xac\x2b\x18\x99"
54+
"\x19\x9a\x1a\x9b\x1b\x9c\x1c\x98\x18\x99\x19\x98\x80";
55+
iso20_exiDocument exiDoc = {};
56+
exi_bitstream_init(&stream, &input[0], sizeof(input), 8, NULL);
57+
uint32_t len = 0;
58+
59+
int res = V2GTP20_ReadHeader(&input[0], &len, V2GTP20_MAINSTREAM_PAYLOAD_ID);
60+
ASSERT_EQ(res, 0);
61+
res = decode_iso20_exiDocument(&stream, &exiDoc);
62+
ASSERT_EQ(res, 0);
63+
64+
static constexpr size_t streamLen = 0x28;
65+
ASSERT_EQ(len, streamLen);
66+
ASSERT_EQ((int)exiDoc.SessionSetupReq_isUsed, 1);
67+
static const uint8_t sessionID[] = "\x30\x30\x30\x30\x30\x30\x30\x30";
68+
ASSERT_ISO20_HEADER_EQ(exiDoc.SessionSetupReq.Header, sessionID, 1707896956850052);
69+
ASSERT_ISO20_STREQ(exiDoc.SessionSetupReq.EVCCID, "PIXV12345678901231");
70+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#pragma once
2+
3+
#include <cstddef>
4+
#include <cstdint>
5+
#include <gtest/gtest.h>
6+
#include <iomanip>
7+
#include <optional>
8+
#include <span>
9+
#include <string>
10+
11+
template <typename HeaderT, size_t Len>
12+
inline void setHeader(HeaderT& header, const uint8_t (&sessionID)[Len], uint64_t timeStamp,
13+
const std::optional<decltype(header.Signature)>& signature = std::nullopt) {
14+
static_assert(Len >= 8, "SessionID must be size 8. Larger is allowed for string literal \\0 term");
15+
header.SessionID.bytesLen = 8;
16+
std::copy(&sessionID[0], &sessionID[0] + 8, header.SessionID.bytes);
17+
header.TimeStamp = timeStamp;
18+
header.Signature_isUsed = false;
19+
if (signature) {
20+
throw std::runtime_error("not supported yet");
21+
}
22+
}
23+
template <typename HeaderT>
24+
inline bool assertHeader(const HeaderT& header, const uint8_t* sessionID, uint64_t timeStamp) {
25+
bool success = false;
26+
[&]() {
27+
ASSERT_EQ(header.SessionID.bytesLen, 8);
28+
ASSERT_EQ(memcmp(header.SessionID.bytes, &sessionID[0], 8), 0);
29+
ASSERT_EQ((int)header.Signature_isUsed, 0);
30+
ASSERT_EQ(header.TimeStamp, timeStamp);
31+
success = true;
32+
}();
33+
return success;
34+
}
35+
#define ASSERT_ISO20_HEADER_EQ(...) \
36+
do { /* NOLINT */ \
37+
if (!assertHeader(__VA_ARGS__)) { \
38+
return; \
39+
} \
40+
} while (0) /* NOLINT */
41+
#define ASSERT_ISO20_STREQ(str, sv) \
42+
do { /* NOLINT */ \
43+
std::string_view svv = sv; \
44+
ASSERT_EQ((str).charactersLen, svv.size()); \
45+
ASSERT_EQ((str).characters, svv); \
46+
} while (0) /* NOLINT */
47+
48+
template <typename StrT>
49+
inline void setString(StrT& out, std::string_view in) {
50+
out.charactersLen = in.size();
51+
std::copy(in.begin(), in.end(), out.characters);
52+
}
53+
54+
template <typename StrT>
55+
inline void setBytes(StrT& out, std::span<uint8_t> in) {
56+
out.bytesLen = in.size();
57+
std::copy(in.begin(), in.end(), out.bytes);
58+
}
59+
60+
template <typename It>
61+
std::string toHexStr(It begin, It end, std::string_view delimit = "\\x") {
62+
std::stringstream ss;
63+
auto it = begin;
64+
for (; it != end; ++it) {
65+
ss << delimit << std::setfill('0') << std::setw(2) << std::hex << (uint16_t)*it;
66+
}
67+
return std::move(ss).str();
68+
}

0 commit comments

Comments
 (0)