1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /**
18 * @file Utilities for working with raw CHPP packets in a test setting
19 */
20
21 #include <array>
22 #include <cinttypes>
23 #include <iostream>
24 #include <vector>
25
26 #include <gtest/gtest.h>
27
28 #include "chpp/app.h"
29 #include "chpp/crc.h"
30 #include "chpp/transport.h"
31
32 namespace chpp::test {
33
34 // Note: the preamble is actually sent in the reverse byte order one might
35 // expect (0x68 'h', 0x43 'C'); the simplification below assumes little endian
36 constexpr uint16_t kPreamble =
37 (CHPP_PREAMBLE_BYTE_SECOND << 8) | CHPP_PREAMBLE_BYTE_FIRST;
38
39 struct ChppEmptyPacket {
40 uint16_t preamble;
41 ChppTransportHeader header;
42 ChppTransportFooter footer;
43 } CHPP_PACKED_ATTR;
44
45 struct ChppResetPacket {
46 uint16_t preamble;
47 ChppTransportHeader header;
48 ChppTransportConfiguration config;
49 ChppTransportFooter footer;
50 } CHPP_PACKED_ATTR;
51
52 struct ChppPacketPrefix {
53 uint16_t preamble;
54 ChppTransportHeader header;
55 uint8_t payload[1]; // Variable size per header.length
56 } CHPP_PACKED_ATTR;
57
58 template <size_t kPayloadSize>
59 struct ChppPacketWithPayload {
60 uint16_t preamble;
61 ChppTransportHeader header;
62 uint8_t payload[kPayloadSize];
63 ChppTransportFooter footer;
64 } CHPP_PACKED_ATTR;
65
66 struct ChppPacketWithAppHeader {
67 uint16_t preamble;
68 ChppTransportHeader transportHeader;
69 ChppAppHeader appHeader;
70 uint8_t payload[];
71 };
72
73 // Utilities for packet creation -----------------------------------------------
74
75 //! Computes the CRC of one of the complete packet types defined above
76 template <typename PktType>
computeCrc(const PktType & pkt)77 uint32_t computeCrc(const PktType &pkt) {
78 return chppCrc32(0, reinterpret_cast<const uint8_t *>(&pkt.header),
79 sizeof(pkt) - sizeof(pkt.preamble) - sizeof(pkt.footer));
80 }
81
82 ChppResetPacket generateResetPacket(uint8_t ackSeq = 0, uint8_t seq = 0);
83 ChppResetPacket generateResetAckPacket(uint8_t ackSeq = 1, uint8_t seq = 0);
84 ChppEmptyPacket generateEmptyPacket(uint8_t ackSeq = 1, uint8_t seq = 0,
85 uint8_t error = CHPP_TRANSPORT_ERROR_NONE);
86
87 //! Create an empty ACK packet for the given packet
88 ChppEmptyPacket generateAck(const std::vector<uint8_t> &pkt);
89
90 //! Create a packet with payload of the given size. If a payload array is not
91 //! provided, it is set to all-zeros.
92 template <size_t kPayloadSize>
93 ChppPacketWithPayload<kPayloadSize> generatePacketWithPayload(
94 uint8_t ackSeq = 0, uint8_t seq = 0,
95 const std::span<uint8_t, kPayloadSize> *payload = nullptr) {
96 // clang-format off
97 ChppPacketWithPayload<kPayloadSize> pkt = {
98 .preamble = kPreamble,
99 .header = {
100 .flags = CHPP_TRANSPORT_FLAG_FINISHED_DATAGRAM,
101 .packetCode = static_cast<uint8_t>(CHPP_ATTR_AND_ERROR_TO_PACKET_CODE(
102 CHPP_TRANSPORT_ATTR_NONE, CHPP_TRANSPORT_ERROR_NONE)),
103 .ackSeq = ackSeq,
104 .seq = seq,
105 .length = kPayloadSize,
106 .reserved = 0,
107 },
108 };
109 // clang-format on
110 if (payload != nullptr) {
111 std::memcpy(pkt.payload, payload->data(), sizeof(pkt.payload));
112 }
113 pkt.footer.checksum = computeCrc(pkt);
114 return pkt;
115 }
116
117 // Utilities for packet parsing ------------------------------------------------
118
asEmptyPacket(const std::vector<uint8_t> & pkt)119 inline const ChppEmptyPacket &asEmptyPacket(const std::vector<uint8_t> &pkt) {
120 EXPECT_EQ(pkt.size(), sizeof(ChppEmptyPacket));
121 return *reinterpret_cast<const ChppEmptyPacket *>(pkt.data());
122 }
123
asResetPacket(const std::vector<uint8_t> & pkt)124 inline const ChppResetPacket &asResetPacket(const std::vector<uint8_t> &pkt) {
125 EXPECT_EQ(pkt.size(), sizeof(ChppResetPacket));
126 return *reinterpret_cast<const ChppResetPacket *>(pkt.data());
127 }
128
asChpp(const std::vector<uint8_t> & pkt)129 inline const ChppPacketPrefix &asChpp(const std::vector<uint8_t> &pkt) {
130 EXPECT_GE(pkt.size(), sizeof(ChppEmptyPacket));
131 return *reinterpret_cast<const ChppPacketPrefix *>(pkt.data());
132 }
133
getHeader(const std::vector<uint8_t> & pkt)134 inline const ChppTransportHeader &getHeader(const std::vector<uint8_t> &pkt) {
135 static_assert(CHPP_PREAMBLE_LEN_BYTES == sizeof(uint16_t));
136 EXPECT_GE(pkt.size(), sizeof(uint16_t) + sizeof(ChppTransportHeader));
137 return *reinterpret_cast<const ChppTransportHeader *>(&pkt[sizeof(uint16_t)]);
138 }
139
asApp(const std::vector<uint8_t> & pkt)140 inline const ChppPacketWithAppHeader &asApp(const std::vector<uint8_t> &pkt) {
141 EXPECT_GE(pkt.size(),
142 sizeof(ChppPacketWithAppHeader) + sizeof(ChppTransportFooter));
143 return *reinterpret_cast<const ChppPacketWithAppHeader *>(pkt.data());
144 }
145
146 // Utilities for debugging -----------------------------------------------------
147
148 const char *appErrorCodeToStr(uint8_t error);
149 const char *appMessageTypeToStr(uint8_t type);
150 const char *handleToStr(uint8_t handle);
151 const char *packetAttrToStr(uint8_t attr);
152 const char *transportErrorToStr(uint8_t error);
153
154 //! Tuned for outputting a raw binary buffer (e.g. payload or full packet)
155 void dumpRaw(std::ostream &os, const void *ptr, size_t len);
156
157 void dumpPreamble(std::ostream &os, uint16_t preamble);
158 void dumpHeader(std::ostream &os, const ChppTransportHeader &hdr);
159 void dumpConfig(std::ostream &os, const ChppTransportConfiguration &cfg);
160
161 template <typename PktType>
dumpFooter(std::ostream & os,const PktType & pkt)162 void dumpFooter(std::ostream &os, const PktType &pkt) {
163 os << "CRC: 0x" << std::hex << pkt.footer.checksum;
164 uint32_t computed = computeCrc(pkt);
165 if (pkt.footer.checksum != computed) {
166 os << " (invalid, expected " << computed << ")";
167 } else {
168 os << " (ok)";
169 }
170 os << std::endl;
171 }
172
173 void dumpEmptyPacket(std::ostream &os, const ChppEmptyPacket &pkt);
174 void dumpResetPacket(std::ostream &os, const ChppResetPacket &pkt);
175 void dumpPacket(std::ostream &os, const ChppPacketPrefix &pkt);
176
177 std::ostream &operator<<(std::ostream &os, const ChppEmptyPacket &pkt);
178 std::ostream &operator<<(std::ostream &os, const ChppResetPacket &pkt);
179 std::ostream &operator<<(std::ostream &os, const ChppPacketPrefix &pkt);
180
181 // Utilities for gtest packet checking -----------------------------------------
182
183 //! Confirms that the supplied packet has a valid preamble, CRC, length, etc.,
184 //! raising a gtest failure (via EXPECT_*) if not
185 void checkPacketValidity(std::vector<uint8_t> &received);
186
187 // These return true if the packets are the same, false otherwise
188
189 bool comparePacketHeader(const ChppTransportHeader &rx,
190 const ChppTransportHeader &expected);
191
192 bool comparePacket(const std::vector<uint8_t> &received,
193 const ChppEmptyPacket &expected);
194 bool comparePacket(const std::vector<uint8_t> &received,
195 const ChppResetPacket &expected);
196
197 } // namespace chpp::test