Skip to content

Commit

Permalink
refactor: renaming, further utility functions
Browse files Browse the repository at this point in the history
Co-authored-by: aw <[email protected]>
Signed-off-by: Sebastian Lukas <[email protected]>
  • Loading branch information
SebaLukas and a-w50 committed Jun 19, 2024
1 parent 18a50e4 commit 284082d
Show file tree
Hide file tree
Showing 14 changed files with 141 additions and 172 deletions.
7 changes: 5 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ FetchContent_MakeAvailable(Catch2)
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
include(Catch)

# source helper functions and library
add_subdirectory(test_utils)

add_subdirectory(app_handshake)
add_subdirectory(DIN)
add_subdirectory(ISO20)
add_subdirectory(din)
add_subdirectory(iso20)
20 changes: 0 additions & 20 deletions tests/DIN/CMakeLists.txt

This file was deleted.

29 changes: 0 additions & 29 deletions tests/ISO20/CMakeLists.txt

This file was deleted.

10 changes: 1 addition & 9 deletions tests/app_handshake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
add_executable(test_app_handshake app_handshake.cpp)

target_link_libraries(test_app_handshake
PRIVATE
cbv2g::din
Catch2::Catch2WithMain
)

catch_discover_tests(test_app_handshake)
add_codec_test(app_handshake)
26 changes: 10 additions & 16 deletions tests/app_handshake/app_handshake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <cbv2g/app_handshake/appHand_Datatypes.h>
#include <cbv2g/app_handshake/appHand_Decoder.h>

#include "test_utils/codec.hpp"

SCENARIO("Encode and decode app protocol request messages") {

GIVEN("Decode an AppProtocolReq document") {
Expand All @@ -14,16 +16,12 @@ SCENARIO("Encode and decode app protocol request messages") {
"\xd1\x89\xa9\x89\x89\xc1\xd1\x69\x91\x81\xd2\x0a\x18\x01\x00\x00\x04"
"\x00\x40";

exi_bitstream_t exi_stream_in;
size_t pos1 = 0;
int errn = 0;

exi_bitstream_init(&exi_stream_in, reinterpret_cast<uint8_t*>(doc_raw), sizeof(doc_raw), pos1, nullptr);

THEN("It should be decoded succussfully") {
appHand_exiDocument request;
const auto result = test_utils::decode<appHand_exiDocument>(doc_raw, sizeof(doc_raw));
REQUIRE(result.decoding_successful);

const auto& request = result.value;

REQUIRE(decode_appHand_exiDocument(&exi_stream_in, &request) == 0);
REQUIRE(request.supportedAppProtocolReq_isUsed == 1);

REQUIRE(request.supportedAppProtocolReq.AppProtocol.arrayLen == 1);
Expand All @@ -47,16 +45,12 @@ SCENARIO("Encode and decode app protocol response messages") {

uint8_t doc_raw[] = "\x80\x40\x00\x40";

exi_bitstream_t exi_stream_in;
size_t pos1 = 0;
int errn = 0;

exi_bitstream_init(&exi_stream_in, reinterpret_cast<uint8_t*>(doc_raw), sizeof(doc_raw), pos1, nullptr);

THEN("It should be decoded succussfully") {
appHand_exiDocument response;
const auto result = test_utils::decode<appHand_exiDocument>(doc_raw, sizeof(doc_raw));
REQUIRE(result.decoding_successful);

const auto& response = result.value;

REQUIRE(decode_appHand_exiDocument(&exi_stream_in, &response) == 0);
REQUIRE(response.supportedAppProtocolRes_isUsed == 1);

REQUIRE(response.supportedAppProtocolRes.ResponseCode == appHand_responseCodeType_OK_SuccessfulNegotiation);
Expand Down
2 changes: 2 additions & 0 deletions tests/din/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_codec_test(session_setup)
add_codec_test(service_discovery)
60 changes: 15 additions & 45 deletions tests/DIN/service_discovery.cpp → tests/din/service_discovery.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#include <catch2/catch_test_macros.hpp>

#include <array>
#include <cstdint>
#include <iterator>
#include <string>
#include <vector>

#include <array>
#include <cbv2g/din/din_msgDefDecoder.h>
#include <cbv2g/din/din_msgDefEncoder.h>

#include "test_utils/codec.hpp"

SCENARIO("Encode and decode a DIN service discovery message") {

// Exi Stream: 809a0211d63f74d2297ac9119400
Expand Down Expand Up @@ -52,40 +54,24 @@ SCENARIO("Encode and decode a DIN service discovery message") {
body.ServiceDiscoveryReq.ServiceCategory_isUsed = true;
body.ServiceDiscoveryReq.ServiceCategory = din_serviceCategoryType_EVCharging;

uint8_t stream[256] = {};
exi_bitstream_t exi_stream_in;
size_t pos1 = 0;
int errn = 0;

exi_bitstream_init(&exi_stream_in, stream, sizeof(stream), pos1, nullptr);

THEN("It should be encoded succussfully") {
const auto result = test_utils::encode_and_compare(request, doc_raw, sizeof(doc_raw));

REQUIRE(encode_din_exiDocument(&exi_stream_in, &request) == 0);

const auto encoded_stream =
std::vector<uint8_t>(stream, stream + exi_bitstream_get_length(&exi_stream_in) + 1);

const auto expected_exi_stream = std::vector<uint8_t>(std::begin(doc_raw), std::end(doc_raw));

REQUIRE(encoded_stream == expected_exi_stream);
REQUIRE(result.encoding_successful);
REQUIRE(result.bitstream_match);
}
}

GIVEN("Good case - Decode a ServiceDiscoveryReq document") {

auto expected_session_id = std::vector<uint8_t>{0x47, 0x58, 0xFD, 0xD3, 0x48, 0xA5, 0xEB, 0x24};
uint8_t doc_raw[] = "\x80\x9a\x02\x11\xd6\x3f\x74\xd2\x29\x7a\xc9\x11\x94\x00";
exi_bitstream_t exi_stream_in;
size_t pos1 = 0;
int errn = 0;

exi_bitstream_init(&exi_stream_in, reinterpret_cast<uint8_t*>(doc_raw), sizeof(doc_raw), pos1, nullptr);

THEN("It should be decoded succussfully") {
din_exiDocument request;
const auto result = test_utils::decode<din_exiDocument>(doc_raw, sizeof(doc_raw));
REQUIRE(result.decoding_successful);

REQUIRE(decode_din_exiDocument(&exi_stream_in, &request) == 0);
const auto& request = result.value;

// Check Header
const auto& header = request.V2G_Message.Header;
Expand Down Expand Up @@ -169,39 +155,23 @@ SCENARIO("Encode and decode a DIN service discovery message") {
chargeService.FreeService = false;
chargeService.EnergyTransferType = din_EVSESupportedEnergyTransferType_DC_extended;

uint8_t stream[256] = {};
exi_bitstream_t exi_stream_in;
size_t pos1 = 0;
int errn = 0;

exi_bitstream_init(&exi_stream_in, stream, sizeof(stream), pos1, nullptr);

THEN("It should be encoded succussfully") {
const auto result = test_utils::encode_and_compare(request, doc_raw, sizeof(doc_raw));

REQUIRE(encode_din_exiDocument(&exi_stream_in, &request) == 0);

const auto encoded_stream =
std::vector<uint8_t>(stream, stream + exi_bitstream_get_length(&exi_stream_in) + 1);

const auto expected_exi_stream = std::vector<uint8_t>(std::begin(doc_raw), std::end(doc_raw));

REQUIRE(encoded_stream == expected_exi_stream);
REQUIRE(result.encoding_successful);
REQUIRE(result.bitstream_match);
}
}

GIVEN("Good case - Decode an ServiceDiscoveryRes document") {
auto expected_session_id = std::vector<uint8_t>{0x47, 0x58, 0xFD, 0xD3, 0x48, 0xA5, 0xEB, 0x24};
uint8_t doc_raw[] = "\x80\x9a\x02\x11\xd6\x3f\x74\xd2\x29\x7a\xc9\x11\xa0\x01\x20\x02\x41\x00\xc4";
exi_bitstream_t exi_stream_in;
size_t pos1 = 0;
int errn = 0;

exi_bitstream_init(&exi_stream_in, reinterpret_cast<uint8_t*>(doc_raw), sizeof(doc_raw), pos1, nullptr);

THEN("It should be decoded succussfully") {
din_exiDocument request;
const auto result = test_utils::decode<din_exiDocument>(doc_raw, sizeof(doc_raw));
REQUIRE(result.decoding_successful);

REQUIRE(decode_din_exiDocument(&exi_stream_in, &request) == 0);
const auto& request = result.value;

// Check Header
const auto& header = request.V2G_Message.Header;
Expand Down
34 changes: 10 additions & 24 deletions tests/DIN/session_setup.cpp → tests/din/session_setup.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#include <catch2/catch_test_macros.hpp>

#include <array>
#include <cstdint>
#include <iterator>
#include <string>
#include <vector>

#include <array>
#include <cbv2g/din/din_msgDefDecoder.h>
#include <cbv2g/din/din_msgDefEncoder.h>

#include "test_utils/codec.hpp"

SCENARIO("Encode and decode DIN session setup message") {

// Exi Stream: 809a02000000000000000011d01a121dc983cd6000
Expand Down Expand Up @@ -48,23 +50,11 @@ SCENARIO("Encode and decode DIN session setup message") {
std::copy(evccid.begin(), evccid.end(), body.SessionSetupReq.EVCCID.bytes);
body.SessionSetupReq.EVCCID.bytesLen = 6;

uint8_t stream[256] = {};
exi_bitstream_t exi_stream_in;
size_t pos1 = 0;
int errn = 0;

exi_bitstream_init(&exi_stream_in, stream, sizeof(stream), pos1, nullptr);

THEN("It should be encoded succussfully") {
const auto result = test_utils::encode_and_compare(request, doc_raw, sizeof(doc_raw));

REQUIRE(encode_din_exiDocument(&exi_stream_in, &request) == 0);

const auto encoded_stream =
std::vector<uint8_t>(stream, stream + exi_bitstream_get_length(&exi_stream_in) + 1);

const auto expected_exi_stream = std::vector<uint8_t>(std::begin(doc_raw), std::end(doc_raw));

REQUIRE(encoded_stream == expected_exi_stream);
REQUIRE(result.encoding_successful);
REQUIRE(result.bitstream_match);
}
}

Expand All @@ -73,16 +63,12 @@ SCENARIO("Encode and decode DIN session setup message") {
uint8_t doc_raw[] = "\x80"
"\x9a\x02\x00\x00\x00\x00\x00\x00\x00\x00"
"\x11\xd0\x1a\x12\x1d\xc9\x83\xcd\x60\x00";
exi_bitstream_t exi_stream_in;
size_t pos1 = 0;
int errn = 0;

exi_bitstream_init(&exi_stream_in, reinterpret_cast<uint8_t*>(doc_raw), sizeof(doc_raw), pos1, nullptr);

THEN("It should be decoded succussfully") {
din_exiDocument request;
const auto result = test_utils::decode<din_exiDocument>(doc_raw, sizeof(doc_raw));
REQUIRE(result.decoding_successful);

REQUIRE(decode_din_exiDocument(&exi_stream_in, &request) == 0);
const auto& request = result.value;

// Check Header
auto& header = request.V2G_Message.Header;
Expand Down Expand Up @@ -222,4 +208,4 @@ SCENARIO("Encode and decode DIN session setup message") {
// REQUIRE(session_setup_res.DateTimeNow == 1675074582);
// }
// }
}
}
2 changes: 2 additions & 0 deletions tests/iso20/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_codec_test(ac_charge_loop)
add_codec_test(dc_charge_loop)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <cbv2g/iso_20/iso20_AC_Decoder.h>
#include <cbv2g/iso_20/iso20_AC_Encoder.h>

#include "codec_helper.hpp"
#include "test_utils/codec.hpp"

// Exi Stream: 8008041e9869d6a61dc1ef895b9b4a8062832418640096
// XML:
Expand Down Expand Up @@ -69,7 +69,7 @@ SCENARIO("Encode and decode AC charge loop message") {
charge_loop.BPT_Scheduled_AC_CLReqControlMode.EVPresentActivePower = {3, 200};

THEN("It should be encoded succussfully") {
const auto result = encode_iso20_and_compare(request, doc_raw, sizeof(doc_raw));
const auto result = test_utils::encode_and_compare(request, doc_raw, sizeof(doc_raw));

REQUIRE(result.encoding_successful);
REQUIRE(result.bitstream_match);
Expand Down Expand Up @@ -98,7 +98,7 @@ SCENARIO("Encode and decode AC charge loop message") {
charge_loop.BPT_Scheduled_AC_CLResControlMode = {};

THEN("It should be encoded succussfully") {
const auto result = encode_iso20_and_compare(response, doc_raw, sizeof(doc_raw));
const auto result = test_utils::encode_and_compare(response, doc_raw, sizeof(doc_raw));

REQUIRE(result.encoding_successful);
REQUIRE(result.bitstream_match);
Expand All @@ -112,7 +112,7 @@ SCENARIO("Encode and decode AC charge loop message") {
"\x9b\x4a\x80\x62\x83\x24\x18\x64\x00\x96";

THEN("It should be decoded succussfully") {
const auto result = decode_iso20<iso20_ac_exiDocument>(doc_raw, sizeof(doc_raw));
const auto result = test_utils::decode<iso20_ac_exiDocument>(doc_raw, sizeof(doc_raw));
REQUIRE(result.decoding_successful);

const auto& request = result.value;
Expand Down Expand Up @@ -144,7 +144,7 @@ SCENARIO("Encode and decode AC charge loop message") {
"\x89\x5b\x9b\x4a\x80\x62\x00\x59\x00";

THEN("It should be decoded succussfully") {
const auto result = decode_iso20<iso20_ac_exiDocument>(doc_raw, sizeof(doc_raw));
const auto result = test_utils::decode<iso20_ac_exiDocument>(doc_raw, sizeof(doc_raw));
REQUIRE(result.decoding_successful);

const auto& request = result.value;
Expand Down
Loading

0 comments on commit 284082d

Please sign in to comment.