forked from bakercp/PacketSerial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestSuite.cpp
More file actions
221 lines (174 loc) · 6.82 KB
/
Copy pathTestSuite.cpp
File metadata and controls
221 lines (174 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <stdint.h>
#include <iostream>
#include <iomanip>
#include <etl/array.h>
#include <etl/span.h>
#include <etl/algorithm.h>
#include <etl/crc16.h>
#include <etl/crc32.h>
#include "../src/PacketSerial.h"
#include "../src/Codecs/COBS.h"
#include "../src/Codecs/SLIP.h"
#include "../src/Mocks/MockStream.h"
using namespace PacketSerial2;
using namespace PacketSerial2::Mocks;
// --- Test Infrastructure ---
struct TestResult {
const char* name;
bool success;
};
#define RUN_TEST(test_func) { #test_func, test_func() }
void print_result(const TestResult& result) {
std::cout << std::left << std::setw(40) << result.name
<< ": " << (result.success ? "PASSED" : "FAILED") << std::endl;
}
// --- Global Flags for Callbacks ---
static bool packet_received = false;
static size_t last_packet_size = 0;
static ErrorCode last_error = ErrorCode::None;
void reset_flags() {
packet_received = false;
last_packet_size = 0;
last_error = ErrorCode::None;
}
void onPacket(etl::span<const uint8_t> packet) {
packet_received = true;
last_packet_size = packet.size();
}
void onError(ErrorCode error) {
last_error = error;
}
// --- Codec Roundtrip Tests (Low Level) ---
bool test_cobs_roundtrip() {
COBS codec;
uint8_t raw[] = { 0x01, 0x00, 0x02, 0x00, 0x03 };
etl::array<uint8_t, 5> input;
etl::copy(raw, raw + 5, input.begin());
etl::array<uint8_t, 16> enc, dec;
auto res_e = codec.encode(input, enc);
if (!res_e.has_value()) return false;
auto res_d = codec.decode(etl::span<const uint8_t>(enc.data(), res_e.value()), dec);
return res_d.has_value() && res_d.value() == 5 && etl::equal(input.begin(), input.end(), dec.begin());
}
bool test_slip_roundtrip() {
SLIP codec;
uint8_t raw[] = { 0x01, 0xC0, 0xDB, 0x02 };
etl::array<uint8_t, 4> input;
etl::copy(raw, raw + 4, input.begin());
etl::array<uint8_t, 16> enc, dec;
auto res_e = codec.encode(input, enc);
if (!res_e.has_value()) return false;
auto res_d = codec.decode(etl::span<const uint8_t>(enc.data(), res_e.value()), dec);
return res_d.has_value() && res_d.value() == 4 && etl::equal(input.begin(), input.end(), dec.begin());
}
// --- High Level Integration Tests ---
bool test_ps_cobs_no_crc() {
etl::array<uint8_t, 64> rx; etl::array<uint8_t, 128> work;
PacketSerial<COBS, NoCRC> ps(rx, work);
MockStream<256> stream;
reset_flags();
ps.addPacketHandler(etl::make_delegate<onPacket>());
uint8_t data[] = { 1, 2, 3 };
ps.send(stream, etl::span<const uint8_t>(data, 3));
stream.injectIncomingData(etl::span<const uint8_t>(stream.getTransmittedData().data(), stream.getTransmittedData().size()));
ps.update(stream);
return packet_received && last_packet_size == 3;
}
bool test_ps_cobs_crc32() {
etl::array<uint8_t, 64> rx; etl::array<uint8_t, 128> work;
PacketSerial<COBS, etl::crc32> ps(rx, work);
MockStream<256> stream;
reset_flags();
ps.addPacketHandler(etl::make_delegate<onPacket>());
uint8_t data[] = { 0xAA, 0xBB, 0xCC, 0xDD };
ps.send(stream, etl::span<const uint8_t>(data, 4));
stream.injectIncomingData(etl::span<const uint8_t>(stream.getTransmittedData().data(), stream.getTransmittedData().size()));
ps.update(stream);
return packet_received && last_packet_size == 4;
}
bool test_ps_slip_no_crc() {
etl::array<uint8_t, 64> rx; etl::array<uint8_t, 128> work;
PacketSerial<SLIP, NoCRC> ps(rx, work);
MockStream<256> stream;
reset_flags();
ps.addPacketHandler(etl::make_delegate<onPacket>());
uint8_t data[] = { 0xC0, 0x01, 0xDB };
ps.send(stream, etl::span<const uint8_t>(data, 3));
stream.injectIncomingData(etl::span<const uint8_t>(stream.getTransmittedData().data(), stream.getTransmittedData().size()));
ps.update(stream);
return packet_received && last_packet_size == 3;
}
bool test_ps_slip_crc16() {
etl::array<uint8_t, 64> rx; etl::array<uint8_t, 128> work;
PacketSerial<SLIP, etl::crc16> ps(rx, work);
MockStream<256> stream;
reset_flags();
ps.addPacketHandler(etl::make_delegate<onPacket>());
uint8_t data[] = { 10, 20, 30 };
ps.send(stream, etl::span<const uint8_t>(data, 3));
stream.injectIncomingData(etl::span<const uint8_t>(stream.getTransmittedData().data(), stream.getTransmittedData().size()));
ps.update(stream);
return packet_received && last_packet_size == 3;
}
bool test_ps_invalid_crc() {
etl::array<uint8_t, 64> rx; etl::array<uint8_t, 128> work;
PacketSerial<COBS, etl::crc16> ps(rx, work);
MockStream<256> stream;
reset_flags();
ps.addPacketHandler(etl::make_delegate<onPacket>());
ps.setErrorHandler(etl::make_delegate<onError>());
uint8_t data[] = { 1, 2, 3, 4 };
ps.send(stream, etl::span<const uint8_t>(data, 4));
// Corrupt one byte of the transmitted data (the CRC part or payload)
auto& tx = const_cast<etl::vector<uint8_t, 256>&>(stream.getTransmittedData());
tx[2] ^= 0xFF;
stream.injectIncomingData(etl::span<const uint8_t>(tx.data(), tx.size()));
ps.update(stream);
return !packet_received && last_error == ErrorCode::InvalidChecksum;
}
// --- Multi-Subscriber Test ---
static bool second_handler_called = false;
void onPacketSecond(etl::span<const uint8_t>) {
second_handler_called = true;
}
bool test_ps_multi_subscriber() {
etl::array<uint8_t, 64> rx; etl::array<uint8_t, 128> work;
PacketSerial<COBS, NoCRC> ps(rx, work);
MockStream<256> stream;
reset_flags();
second_handler_called = false;
ps.addPacketHandler(etl::make_delegate<onPacket>());
ps.addPacketHandler(etl::make_delegate<onPacketSecond>());
uint8_t data[] = { 1, 2, 3 };
ps.send(stream, etl::span<const uint8_t>(data, 3));
stream.injectIncomingData(etl::span<const uint8_t>(stream.getTransmittedData().data(), stream.getTransmittedData().size()));
ps.update(stream);
return packet_received && second_handler_called;
}
// --- Main Runner ---
int main() {
std::cout << "PacketSerial2 Extended Test Suite" << std::endl;
std::cout << "=================================" << std::endl;
etl::array<TestResult, 8> results = {{
RUN_TEST(test_cobs_roundtrip),
RUN_TEST(test_slip_roundtrip),
RUN_TEST(test_ps_cobs_no_crc),
RUN_TEST(test_ps_cobs_crc32),
RUN_TEST(test_ps_slip_no_crc),
RUN_TEST(test_ps_slip_crc16),
RUN_TEST(test_ps_invalid_crc),
RUN_TEST(test_ps_multi_subscriber)
}};
bool all_passed = true;
etl::for_each(results.begin(), results.end(), [&](const TestResult& r) {
print_result(r);
if (!r.success) all_passed = false;
});
std::cout << "=================================" << std::endl;
if (all_passed) {
std::cout << "ALL TESTS PASSED" << std::endl;
} else {
std::cout << "SOME TESTS FAILED" << std::endl;
}
return all_passed ? 0 : 1;
}