Add missing wifi targets for some DIY modules (#1210)
[ExpressLRS.git] / src / test / msp_native / msp_tests.cpp
blobfe48efd3c2024ede54c439b7eac9dd5c7ad35aa7
1 #include <unity.h>
2 #include "msp.h"
3 #include "mock_serial.h"
5 MSP MSPProtocol;
7 void test_msp_receive(void)
9 // TEST CASE:
10 // GIVEN an instance of the MSP class has been instantiated
11 // WHEN a valid packet is send to processReceivedByte() one byte at a time
12 // THEN the complete packet will be available via getReceivedPacket()
13 // AND the packet values will match the values from the sent bytes
15 uint8_t c;
16 bool packetComplete;
18 c = '$';
19 packetComplete = MSPProtocol.processReceivedByte(c);
20 TEST_ASSERT_EQUAL(false, packetComplete);
22 c = 'X';
23 packetComplete = MSPProtocol.processReceivedByte(c);
24 TEST_ASSERT_EQUAL(false, packetComplete);
26 c = '<'; // command
27 packetComplete = MSPProtocol.processReceivedByte(c);
28 TEST_ASSERT_EQUAL(false, packetComplete);
30 c = 0; // flags
31 packetComplete = MSPProtocol.processReceivedByte(c);
32 TEST_ASSERT_EQUAL(false, packetComplete);
34 c = 1; // function b1
35 packetComplete = MSPProtocol.processReceivedByte(c);
36 TEST_ASSERT_EQUAL(false, packetComplete);
38 c = 0; // function b2
39 packetComplete = MSPProtocol.processReceivedByte(c);
40 TEST_ASSERT_EQUAL(false, packetComplete);
42 c = 1; // payload size b1
43 packetComplete = MSPProtocol.processReceivedByte(c);
44 TEST_ASSERT_EQUAL(false, packetComplete);
46 c = 0; // payload size b2
47 packetComplete = MSPProtocol.processReceivedByte(c);
48 TEST_ASSERT_EQUAL(false, packetComplete);
50 c = 'A'; // payload
51 packetComplete = MSPProtocol.processReceivedByte(c);
52 TEST_ASSERT_EQUAL(false, packetComplete);
54 c = 125; // crc
55 packetComplete = MSPProtocol.processReceivedByte(c);
56 TEST_ASSERT_EQUAL(true, packetComplete);
58 // Assert that values in packet match sent values
59 mspPacket_t* packet = MSPProtocol.getReceivedPacket();
60 TEST_ASSERT_EQUAL(MSP_PACKET_COMMAND, packet->type);
61 TEST_ASSERT_EQUAL(0, packet->flags);
62 TEST_ASSERT_EQUAL(1, packet->function);
63 TEST_ASSERT_EQUAL(1, packet->payloadSize);
64 TEST_ASSERT_EQUAL('A', (char)packet->payload[0]);
67 void test_msp_send(void)
69 // TEST CASE:
70 // GIVEN an instance of the MSP class has been instantiated
71 // WHEN a valid packet is passed as an argument to sendPacket()
72 // THEN the complete packet will be transmitted to the passed in Stream object
73 // AND the return value will be true
75 // Build an MSP reply packet
76 mspPacket_t packet;
77 packet.reset();
78 packet.makeResponse();
79 packet.flags = 0;
80 packet.function = 1;
81 packet.addByte(1);
83 // Mock out the serial port using a string stream
84 std::string buf;
85 StringStream ss(buf);
87 // Ask the MSP class to send the packet to the stream
88 bool result = MSPProtocol.sendPacket(&packet, &ss);
89 TEST_ASSERT_EQUAL(true, result); // success?
91 // Assert that each byte sent matches expected
92 TEST_ASSERT_EQUAL('$', buf[0]);
93 TEST_ASSERT_EQUAL('X', buf[1]);
94 TEST_ASSERT_EQUAL('>', buf[2]);
95 TEST_ASSERT_EQUAL(0, buf[3]); // flags
96 TEST_ASSERT_EQUAL(1, buf[4]); // lower byte of function
97 TEST_ASSERT_EQUAL(0, buf[5]); // upper byte of function
98 TEST_ASSERT_EQUAL(1, buf[6]); // lower byte of payload size
99 TEST_ASSERT_EQUAL(0, buf[7]); // upper byte of payload size
100 TEST_ASSERT_EQUAL(1, buf[8]); // payload
101 TEST_ASSERT_EQUAL(224, (uint8_t)buf[9]); // crc
104 extern void test_encapsulated_msp_send(void);
105 extern void test_encapsulated_msp_send_too_long(void);
107 int main(int argc, char **argv)
109 UNITY_BEGIN();
110 RUN_TEST(test_msp_receive);
111 RUN_TEST(test_msp_send);
113 RUN_TEST(test_encapsulated_msp_send);
114 RUN_TEST(test_encapsulated_msp_send_too_long);
116 UNITY_END();
118 return 0;