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 <cinttypes>
22 #include <iostream>
23 #include <vector>
24
25 #include <gtest/gtest.h>
26
27 #include "chpp/crc.h"
28 #include "chpp/transport.h"
29
30 namespace chpp::test {
31
32 // Note: the preamble is actually sent in the reverse byte order one might
33 // expect (0x68 'h', 0x43 'C'); the simplification below assumes little endian
34 constexpr uint16_t kPreamble =
35 (CHPP_PREAMBLE_BYTE_SECOND << 8) | CHPP_PREAMBLE_BYTE_FIRST;
36
37 struct ChppEmptyPacket {
38 uint16_t preamble;
39 ChppTransportHeader header;
40 ChppTransportFooter footer;
41 } CHPP_PACKED_ATTR;
42
43 struct ChppResetPacket {
44 uint16_t preamble;
45 ChppTransportHeader header;
46 ChppTransportConfiguration config;
47 ChppTransportFooter footer;
48 } CHPP_PACKED_ATTR;
49
50 struct ChppPacketPrefix {
51 uint16_t preamble;
52 ChppTransportHeader header;
53 uint8_t payload[1]; // Variable size per header.length
54 } CHPP_PACKED_ATTR;
55
56 // Utilities for packet creation -----------------------------------------------
57
58 //! Computes the CRC of one of the complete packet types defined above
59 template <typename PktType>
computeCrc(const PktType & pkt)60 uint32_t computeCrc(const PktType &pkt) {
61 return chppCrc32(0, reinterpret_cast<const uint8_t *>(&pkt.header),
62 sizeof(pkt) - sizeof(pkt.preamble) - sizeof(pkt.footer));
63 }
64
65 ChppResetPacket generateResetPacket(uint8_t ackSeq = 0, uint8_t seq = 0);
66 ChppResetPacket generateResetAckPacket(uint8_t ackSeq = 1, uint8_t seq = 0);
67 ChppEmptyPacket generateEmptyPacket(uint8_t ackSeq = 1, uint8_t seq = 0,
68 uint8_t error = CHPP_TRANSPORT_ERROR_NONE);
69
70 //! Create an empty ACK packet for the given packet
71 ChppEmptyPacket generateAck(std::vector<uint8_t> &pkt);
72
73 // Utilities for packet parsing ------------------------------------------------
74
asEmptyPacket(std::vector<uint8_t> & pkt)75 inline ChppEmptyPacket &asEmptyPacket(std::vector<uint8_t> &pkt) {
76 EXPECT_EQ(pkt.size(), sizeof(ChppEmptyPacket));
77 return *reinterpret_cast<ChppEmptyPacket *>(pkt.data());
78 }
79
asResetPacket(std::vector<uint8_t> & pkt)80 inline ChppResetPacket &asResetPacket(std::vector<uint8_t> &pkt) {
81 EXPECT_EQ(pkt.size(), sizeof(ChppResetPacket));
82 return *reinterpret_cast<ChppResetPacket *>(pkt.data());
83 }
84
asChpp(std::vector<uint8_t> & pkt)85 inline ChppPacketPrefix &asChpp(std::vector<uint8_t> &pkt) {
86 EXPECT_GE(pkt.size(), sizeof(ChppEmptyPacket));
87 return *reinterpret_cast<ChppPacketPrefix *>(pkt.data());
88 }
89
getHeader(std::vector<uint8_t> & pkt)90 inline ChppTransportHeader &getHeader(std::vector<uint8_t> &pkt) {
91 static_assert(CHPP_PREAMBLE_LEN_BYTES == sizeof(uint16_t));
92 EXPECT_GE(pkt.size(), sizeof(uint16_t) + sizeof(ChppTransportHeader));
93 return *reinterpret_cast<ChppTransportHeader *>(&pkt[sizeof(uint16_t)]);
94 }
95
96 // Utilities for debugging -----------------------------------------------------
97
98 //! Tuned for outputting a raw binary buffer (e.g. payload or full packet)
99 void dumpRaw(std::ostream &os, const void *ptr, size_t len);
100
101 void dumpPreamble(std::ostream &os, uint16_t preamble);
102 void dumpHeader(std::ostream &os, const ChppTransportHeader &hdr);
103 void dumpConfig(std::ostream &os, const ChppTransportConfiguration &cfg);
104
105 template <typename PktType>
dumpFooter(std::ostream & os,const PktType & pkt)106 void dumpFooter(std::ostream &os, const PktType &pkt) {
107 os << "CRC: 0x" << std::hex << pkt.footer.checksum;
108 uint32_t computed = computeCrc(pkt);
109 if (pkt.footer.checksum != computed) {
110 os << " (invalid, expected " << computed << ")";
111 } else {
112 os << " (ok)";
113 }
114 os << std::endl;
115 }
116
117 void dumpEmptyPacket(std::ostream &os, const ChppEmptyPacket &pkt);
118 void dumpResetPacket(std::ostream &os, const ChppResetPacket &pkt);
119 void dumpPacket(std::ostream &os, const ChppPacketPrefix &pkt);
120
121 std::ostream &operator<<(std::ostream &os, const ChppEmptyPacket &pkt);
122 std::ostream &operator<<(std::ostream &os, const ChppResetPacket &pkt);
123 std::ostream &operator<<(std::ostream &os, const ChppPacketPrefix &pkt);
124
125 // Utilities for gtest packet checking -----------------------------------------
126
127 //! Confirms that the supplied packet has a valid preamble, CRC, length, etc.,
128 //! raising a gtest failure (via EXPECT_*) if not
129 void checkPacketValidity(std::vector<uint8_t> &received);
130
131 // These return true if the packets are the same, false otherwise
132
133 bool comparePacketHeader(const ChppTransportHeader &rx,
134 const ChppTransportHeader &expected);
135
136 bool comparePacket(const std::vector<uint8_t> &received,
137 const ChppEmptyPacket &expected);
138 bool comparePacket(const std::vector<uint8_t> &received,
139 const ChppResetPacket &expected);
140
141 } // namespace chpp::test