• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                                     uint8_t error = CHPP_TRANSPORT_ERROR_NONE);
84 ChppResetPacket generateResetAckPacket(uint8_t ackSeq = 1, uint8_t seq = 0);
85 ChppEmptyPacket generateEmptyPacket(uint8_t ackSeq = 1, uint8_t seq = 0,
86                                     uint8_t error = CHPP_TRANSPORT_ERROR_NONE);
87 
88 //! Create an empty ACK packet for the given packet
89 ChppEmptyPacket generateAck(const std::vector<uint8_t> &pkt);
90 
91 //! Create a packet with payload of the given size. If a payload array is not
92 //! provided, it is set to all-zeros.
93 template <size_t kPayloadSize>
94 ChppPacketWithPayload<kPayloadSize> generatePacketWithPayload(
95     uint8_t ackSeq = 0, uint8_t seq = 0,
96     const std::span<uint8_t, kPayloadSize> *payload = nullptr) {
97   // clang-format off
98   ChppPacketWithPayload<kPayloadSize> pkt = {
99     .preamble = kPreamble,
100     .header = {
101       .flags = CHPP_TRANSPORT_FLAG_FINISHED_DATAGRAM,
102       .packetCode = static_cast<uint8_t>(CHPP_ATTR_AND_ERROR_TO_PACKET_CODE(
103           CHPP_TRANSPORT_ATTR_NONE, CHPP_TRANSPORT_ERROR_NONE)),
104       .ackSeq = ackSeq,
105       .seq = seq,
106       .length = kPayloadSize,
107       .reserved = 0,
108     },
109   };
110   // clang-format on
111   if (payload != nullptr) {
112     std::memcpy(pkt.payload, payload->data(), sizeof(pkt.payload));
113   }
114   pkt.footer.checksum = computeCrc(pkt);
115   return pkt;
116 }
117 
118 // Utilities for packet parsing ------------------------------------------------
119 
asEmptyPacket(const std::vector<uint8_t> & pkt)120 inline const ChppEmptyPacket &asEmptyPacket(const std::vector<uint8_t> &pkt) {
121   EXPECT_EQ(pkt.size(), sizeof(ChppEmptyPacket));
122   return *reinterpret_cast<const ChppEmptyPacket *>(pkt.data());
123 }
124 
asResetPacket(const std::vector<uint8_t> & pkt)125 inline const ChppResetPacket &asResetPacket(const std::vector<uint8_t> &pkt) {
126   EXPECT_EQ(pkt.size(), sizeof(ChppResetPacket));
127   return *reinterpret_cast<const ChppResetPacket *>(pkt.data());
128 }
129 
asChpp(const std::vector<uint8_t> & pkt)130 inline const ChppPacketPrefix &asChpp(const std::vector<uint8_t> &pkt) {
131   EXPECT_GE(pkt.size(), sizeof(ChppEmptyPacket));
132   return *reinterpret_cast<const ChppPacketPrefix *>(pkt.data());
133 }
134 
getHeader(const std::vector<uint8_t> & pkt)135 inline const ChppTransportHeader &getHeader(const std::vector<uint8_t> &pkt) {
136   static_assert(CHPP_PREAMBLE_LEN_BYTES == sizeof(uint16_t));
137   EXPECT_GE(pkt.size(), sizeof(uint16_t) + sizeof(ChppTransportHeader));
138   return *reinterpret_cast<const ChppTransportHeader *>(&pkt[sizeof(uint16_t)]);
139 }
140 
asApp(const std::vector<uint8_t> & pkt)141 inline const ChppPacketWithAppHeader &asApp(const std::vector<uint8_t> &pkt) {
142   EXPECT_GE(pkt.size(),
143             sizeof(ChppPacketWithAppHeader) + sizeof(ChppTransportFooter));
144   return *reinterpret_cast<const ChppPacketWithAppHeader *>(pkt.data());
145 }
146 
147 // Utilities for debugging -----------------------------------------------------
148 
149 const char *appErrorCodeToStr(uint8_t error);
150 const char *appMessageTypeToStr(uint8_t type);
151 const char *handleToStr(uint8_t handle);
152 const char *packetAttrToStr(uint8_t attr);
153 const char *transportErrorToStr(uint8_t error);
154 
155 //! Tuned for outputting a raw binary buffer (e.g. payload or full packet)
156 void dumpRaw(std::ostream &os, const void *ptr, size_t len);
157 
158 void dumpPreamble(std::ostream &os, uint16_t preamble);
159 void dumpHeader(std::ostream &os, const ChppTransportHeader &hdr);
160 void dumpConfig(std::ostream &os, const ChppTransportConfiguration &cfg);
161 
162 template <typename PktType>
dumpFooter(std::ostream & os,const PktType & pkt)163 void dumpFooter(std::ostream &os, const PktType &pkt) {
164   os << "CRC: 0x" << std::hex << pkt.footer.checksum;
165   uint32_t computed = computeCrc(pkt);
166   if (pkt.footer.checksum != computed) {
167     os << " (invalid, expected " << computed << ")";
168   } else {
169     os << " (ok)";
170   }
171   os << std::endl;
172 }
173 
174 void dumpEmptyPacket(std::ostream &os, const ChppEmptyPacket &pkt);
175 void dumpResetPacket(std::ostream &os, const ChppResetPacket &pkt);
176 void dumpPacket(std::ostream &os, const ChppPacketPrefix &pkt);
177 
178 std::ostream &operator<<(std::ostream &os, const ChppEmptyPacket &pkt);
179 std::ostream &operator<<(std::ostream &os, const ChppResetPacket &pkt);
180 std::ostream &operator<<(std::ostream &os, const ChppPacketPrefix &pkt);
181 
182 // Utilities for gtest packet checking -----------------------------------------
183 
184 //! Confirms that the supplied packet has a valid preamble, CRC, length, etc.,
185 //! raising a gtest failure (via EXPECT_*) if not
186 void checkPacketValidity(std::vector<uint8_t> &received);
187 
188 // These return true if the packets are the same, false otherwise
189 
190 bool comparePacketHeader(const ChppTransportHeader &rx,
191                          const ChppTransportHeader &expected);
192 
193 bool comparePacket(const std::vector<uint8_t> &received,
194                    const ChppEmptyPacket &expected);
195 bool comparePacket(const std::vector<uint8_t> &received,
196                    const ChppResetPacket &expected);
197 
198 }  // namespace chpp::test