Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 1 | // Copyright 2020 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
| 15 | #include <cinttypes> |
| 16 | #include <cstdint> |
| 17 | #include <cstring> |
| 18 | |
| 19 | #include "gtest/gtest.h" |
| 20 | #include "pw_tokenizer/tokenize_to_global_handler.h" |
| 21 | #include "pw_tokenizer/tokenize_to_global_handler_with_payload.h" |
| 22 | #include "pw_tokenizer_private/tokenize_test.h" |
| 23 | |
| 24 | namespace pw::tokenizer { |
| 25 | namespace { |
| 26 | |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 27 | // Constructs an array with the hashed string followed by the provided bytes. |
Ewout van Bekkum | 5ea3340 | 2021-03-31 11:00:02 -0700 | [diff] [blame] | 28 | template <uint8_t... data, size_t kSize> |
Wyatt Hepler | 4b62b89 | 2021-03-04 10:03:43 -0800 | [diff] [blame] | 29 | constexpr auto ExpectedData( |
Ewout van Bekkum | 5ea3340 | 2021-03-31 11:00:02 -0700 | [diff] [blame] | 30 | const char (&format)[kSize], |
Wyatt Hepler | 4b62b89 | 2021-03-04 10:03:43 -0800 | [diff] [blame] | 31 | uint32_t token_mask = std::numeric_limits<uint32_t>::max()) { |
| 32 | const uint32_t value = Hash(format) & token_mask; |
| 33 | return std::array<uint8_t, sizeof(uint32_t) + sizeof...(data)>{ |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 34 | static_cast<uint8_t>(value & 0xff), |
| 35 | static_cast<uint8_t>(value >> 8 & 0xff), |
| 36 | static_cast<uint8_t>(value >> 16 & 0xff), |
| 37 | static_cast<uint8_t>(value >> 24 & 0xff), |
Wyatt Hepler | 4b62b89 | 2021-03-04 10:03:43 -0800 | [diff] [blame] | 38 | data...}; |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | // Test fixture for both global handler functions. Both need a global message |
| 42 | // buffer. To keep the message buffers separate, template this on the derived |
| 43 | // class type. |
| 44 | template <typename Impl> |
| 45 | class GlobalMessage : public ::testing::Test { |
| 46 | public: |
| 47 | static void SetMessage(const uint8_t* message, size_t size) { |
| 48 | ASSERT_LE(size, sizeof(message_)); |
| 49 | std::memcpy(message_, message, size); |
| 50 | message_size_bytes_ = size; |
| 51 | } |
| 52 | |
| 53 | protected: |
| 54 | GlobalMessage() { |
| 55 | std::memset(message_, 0, sizeof(message_)); |
| 56 | message_size_bytes_ = 0; |
| 57 | } |
| 58 | |
| 59 | static uint8_t message_[256]; |
| 60 | static size_t message_size_bytes_; |
| 61 | }; |
| 62 | |
| 63 | template <typename Impl> |
| 64 | uint8_t GlobalMessage<Impl>::message_[256] = {}; |
| 65 | template <typename Impl> |
| 66 | size_t GlobalMessage<Impl>::message_size_bytes_ = 0; |
| 67 | |
| 68 | class TokenizeToGlobalHandler : public GlobalMessage<TokenizeToGlobalHandler> { |
| 69 | }; |
| 70 | |
| 71 | TEST_F(TokenizeToGlobalHandler, Variety) { |
| 72 | PW_TOKENIZE_TO_GLOBAL_HANDLER("%x%lld%1.2f%s", 0, 0ll, -0.0, ""); |
| 73 | const auto expected = |
| 74 | ExpectedData<0, 0, 0x00, 0x00, 0x00, 0x80, 0>("%x%lld%1.2f%s"); |
| 75 | ASSERT_EQ(expected.size(), message_size_bytes_); |
| 76 | EXPECT_EQ(std::memcmp(expected.data(), message_, expected.size()), 0); |
| 77 | } |
| 78 | |
| 79 | TEST_F(TokenizeToGlobalHandler, Strings) { |
| 80 | PW_TOKENIZE_TO_GLOBAL_HANDLER("The answer is: %s", "5432!"); |
| 81 | constexpr std::array<uint8_t, 10> expected = |
| 82 | ExpectedData<5, '5', '4', '3', '2', '!'>("The answer is: %s"); |
| 83 | ASSERT_EQ(expected.size(), message_size_bytes_); |
| 84 | EXPECT_EQ(std::memcmp(expected.data(), message_, expected.size()), 0); |
| 85 | } |
| 86 | |
Wyatt Hepler | d58eef9 | 2020-05-08 10:39:56 -0700 | [diff] [blame] | 87 | TEST_F(TokenizeToGlobalHandler, Domain_Strings) { |
| 88 | PW_TOKENIZE_TO_GLOBAL_HANDLER_DOMAIN( |
| 89 | "TEST_DOMAIN", "The answer is: %s", "5432!"); |
| 90 | constexpr std::array<uint8_t, 10> expected = |
| 91 | ExpectedData<5, '5', '4', '3', '2', '!'>("The answer is: %s"); |
| 92 | ASSERT_EQ(expected.size(), message_size_bytes_); |
| 93 | EXPECT_EQ(std::memcmp(expected.data(), message_, expected.size()), 0); |
| 94 | } |
| 95 | |
Wyatt Hepler | 4b62b89 | 2021-03-04 10:03:43 -0800 | [diff] [blame] | 96 | TEST_F(TokenizeToGlobalHandler, Mask) { |
| 97 | PW_TOKENIZE_TO_GLOBAL_HANDLER_MASK( |
| 98 | "TEST_DOMAIN", 0x00FFF000, "The answer is: %s", "5432!"); |
| 99 | constexpr std::array<uint8_t, 10> expected = |
| 100 | ExpectedData<5, '5', '4', '3', '2', '!'>("The answer is: %s", 0x00FFF000); |
| 101 | ASSERT_EQ(expected.size(), message_size_bytes_); |
| 102 | EXPECT_EQ(std::memcmp(expected.data(), message_, expected.size()), 0); |
| 103 | } |
| 104 | |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 105 | TEST_F(TokenizeToGlobalHandler, C_SequentialZigZag) { |
Wyatt Hepler | 7a5e4d6 | 2020-08-31 08:39:16 -0700 | [diff] [blame] | 106 | pw_tokenizer_ToGlobalHandlerTest_SequentialZigZag(); |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 107 | |
| 108 | constexpr std::array<uint8_t, 18> expected = |
| 109 | ExpectedData<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13>( |
| 110 | TEST_FORMAT_SEQUENTIAL_ZIG_ZAG); |
| 111 | ASSERT_EQ(expected.size(), message_size_bytes_); |
| 112 | EXPECT_EQ(std::memcmp(expected.data(), message_, expected.size()), 0); |
| 113 | } |
| 114 | |
Wyatt Hepler | 7a5e4d6 | 2020-08-31 08:39:16 -0700 | [diff] [blame] | 115 | extern "C" void pw_tokenizer_HandleEncodedMessage( |
| 116 | const uint8_t* encoded_message, size_t size_bytes) { |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 117 | TokenizeToGlobalHandler::SetMessage(encoded_message, size_bytes); |
| 118 | } |
| 119 | |
| 120 | class TokenizeToGlobalHandlerWithPayload |
| 121 | : public GlobalMessage<TokenizeToGlobalHandlerWithPayload> { |
| 122 | public: |
Wyatt Hepler | 7a5e4d6 | 2020-08-31 08:39:16 -0700 | [diff] [blame] | 123 | static void SetPayload(pw_tokenizer_Payload payload) { |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 124 | payload_ = static_cast<intptr_t>(payload); |
| 125 | } |
| 126 | |
| 127 | protected: |
| 128 | TokenizeToGlobalHandlerWithPayload() { payload_ = {}; } |
| 129 | |
| 130 | static intptr_t payload_; |
| 131 | }; |
| 132 | |
| 133 | intptr_t TokenizeToGlobalHandlerWithPayload::payload_; |
| 134 | |
| 135 | TEST_F(TokenizeToGlobalHandlerWithPayload, Variety) { |
| 136 | ASSERT_NE(payload_, 123); |
| 137 | |
| 138 | const auto expected = |
| 139 | ExpectedData<0, 0, 0x00, 0x00, 0x00, 0x80, 0>("%x%lld%1.2f%s"); |
| 140 | |
| 141 | PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD( |
Wyatt Hepler | 7a5e4d6 | 2020-08-31 08:39:16 -0700 | [diff] [blame] | 142 | static_cast<pw_tokenizer_Payload>(123), |
| 143 | "%x%lld%1.2f%s", |
| 144 | 0, |
| 145 | 0ll, |
| 146 | -0.0, |
| 147 | ""); |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 148 | ASSERT_EQ(expected.size(), message_size_bytes_); |
| 149 | EXPECT_EQ(std::memcmp(expected.data(), message_, expected.size()), 0); |
| 150 | EXPECT_EQ(payload_, 123); |
| 151 | |
| 152 | PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD( |
Wyatt Hepler | 7a5e4d6 | 2020-08-31 08:39:16 -0700 | [diff] [blame] | 153 | static_cast<pw_tokenizer_Payload>(-543), |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 154 | "%x%lld%1.2f%s", |
| 155 | 0, |
| 156 | 0ll, |
| 157 | -0.0, |
| 158 | ""); |
| 159 | ASSERT_EQ(expected.size(), message_size_bytes_); |
| 160 | EXPECT_EQ(std::memcmp(expected.data(), message_, expected.size()), 0); |
| 161 | EXPECT_EQ(payload_, -543); |
| 162 | } |
| 163 | |
Wyatt Hepler | d58eef9 | 2020-05-08 10:39:56 -0700 | [diff] [blame] | 164 | constexpr std::array<uint8_t, 10> kExpected = |
| 165 | ExpectedData<5, '5', '4', '3', '2', '!'>("The answer is: %s"); |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 166 | |
Wyatt Hepler | d58eef9 | 2020-05-08 10:39:56 -0700 | [diff] [blame] | 167 | TEST_F(TokenizeToGlobalHandlerWithPayload, Strings_ZeroPayload) { |
| 168 | PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD({}, "The answer is: %s", "5432!"); |
| 169 | |
| 170 | ASSERT_EQ(kExpected.size(), message_size_bytes_); |
| 171 | EXPECT_EQ(std::memcmp(kExpected.data(), message_, kExpected.size()), 0); |
| 172 | EXPECT_EQ(payload_, 0); |
| 173 | } |
| 174 | |
| 175 | TEST_F(TokenizeToGlobalHandlerWithPayload, Strings_NonZeroPayload) { |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 176 | PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD( |
Wyatt Hepler | 7a5e4d6 | 2020-08-31 08:39:16 -0700 | [diff] [blame] | 177 | static_cast<pw_tokenizer_Payload>(5432), "The answer is: %s", "5432!"); |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 178 | |
Wyatt Hepler | d58eef9 | 2020-05-08 10:39:56 -0700 | [diff] [blame] | 179 | ASSERT_EQ(kExpected.size(), message_size_bytes_); |
| 180 | EXPECT_EQ(std::memcmp(kExpected.data(), message_, kExpected.size()), 0); |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 181 | EXPECT_EQ(payload_, 5432); |
Wyatt Hepler | d58eef9 | 2020-05-08 10:39:56 -0700 | [diff] [blame] | 182 | } |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 183 | |
Wyatt Hepler | d58eef9 | 2020-05-08 10:39:56 -0700 | [diff] [blame] | 184 | TEST_F(TokenizeToGlobalHandlerWithPayload, Domain_Strings) { |
| 185 | PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD_DOMAIN( |
| 186 | "TEST_DOMAIN", |
Wyatt Hepler | 7a5e4d6 | 2020-08-31 08:39:16 -0700 | [diff] [blame] | 187 | static_cast<pw_tokenizer_Payload>(5432), |
Wyatt Hepler | d58eef9 | 2020-05-08 10:39:56 -0700 | [diff] [blame] | 188 | "The answer is: %s", |
| 189 | "5432!"); |
| 190 | ASSERT_EQ(kExpected.size(), message_size_bytes_); |
| 191 | EXPECT_EQ(std::memcmp(kExpected.data(), message_, kExpected.size()), 0); |
| 192 | EXPECT_EQ(payload_, 5432); |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Wyatt Hepler | 4b62b89 | 2021-03-04 10:03:43 -0800 | [diff] [blame] | 195 | TEST_F(TokenizeToGlobalHandlerWithPayload, Mask) { |
| 196 | PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD_MASK( |
| 197 | "TEST_DOMAIN", |
| 198 | 0x12345678, |
| 199 | static_cast<pw_tokenizer_Payload>(5432), |
| 200 | "The answer is: %s", |
| 201 | "5432!"); |
| 202 | constexpr std::array<uint8_t, 10> expected = |
| 203 | ExpectedData<5, '5', '4', '3', '2', '!'>("The answer is: %s", 0x12345678); |
| 204 | ASSERT_EQ(expected.size(), message_size_bytes_); |
| 205 | EXPECT_EQ(std::memcmp(expected.data(), message_, expected.size()), 0); |
| 206 | EXPECT_EQ(payload_, 5432); |
| 207 | } |
| 208 | |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 209 | struct Foo { |
| 210 | unsigned char a; |
| 211 | bool b; |
| 212 | }; |
| 213 | |
| 214 | TEST_F(TokenizeToGlobalHandlerWithPayload, PointerToStack) { |
| 215 | Foo foo{254u, true}; |
| 216 | |
| 217 | PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD( |
Wyatt Hepler | 7a5e4d6 | 2020-08-31 08:39:16 -0700 | [diff] [blame] | 218 | reinterpret_cast<pw_tokenizer_Payload>(&foo), "Boring!"); |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 219 | |
| 220 | constexpr auto expected = ExpectedData("Boring!"); |
| 221 | static_assert(expected.size() == 4); |
| 222 | ASSERT_EQ(expected.size(), message_size_bytes_); |
| 223 | EXPECT_EQ(std::memcmp(expected.data(), message_, expected.size()), 0); |
| 224 | |
| 225 | Foo* payload_foo = reinterpret_cast<Foo*>(payload_); |
| 226 | ASSERT_EQ(&foo, payload_foo); |
| 227 | EXPECT_EQ(payload_foo->a, 254u); |
| 228 | EXPECT_TRUE(payload_foo->b); |
| 229 | } |
| 230 | |
| 231 | TEST_F(TokenizeToGlobalHandlerWithPayload, C_SequentialZigZag) { |
Wyatt Hepler | 7a5e4d6 | 2020-08-31 08:39:16 -0700 | [diff] [blame] | 232 | pw_tokenizer_ToGlobalHandlerWithPayloadTest_SequentialZigZag(); |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 233 | |
| 234 | constexpr std::array<uint8_t, 18> expected = |
| 235 | ExpectedData<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13>( |
| 236 | TEST_FORMAT_SEQUENTIAL_ZIG_ZAG); |
| 237 | ASSERT_EQ(expected.size(), message_size_bytes_); |
| 238 | EXPECT_EQ(std::memcmp(expected.data(), message_, expected.size()), 0); |
| 239 | EXPECT_EQ(payload_, 600613); |
| 240 | } |
| 241 | |
Wyatt Hepler | 7a5e4d6 | 2020-08-31 08:39:16 -0700 | [diff] [blame] | 242 | extern "C" void pw_tokenizer_HandleEncodedMessageWithPayload( |
| 243 | pw_tokenizer_Payload payload, |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 244 | const uint8_t* encoded_message, |
| 245 | size_t size_bytes) { |
| 246 | TokenizeToGlobalHandlerWithPayload::SetMessage(encoded_message, size_bytes); |
| 247 | TokenizeToGlobalHandlerWithPayload::SetPayload(payload); |
| 248 | } |
| 249 | |
Wyatt Hepler | eb020a1 | 2020-10-28 14:01:51 -0700 | [diff] [blame] | 250 | // Hijack an internal macro to capture the tokenizer domain. |
| 251 | #undef _PW_TOKENIZER_RECORD_ORIGINAL_STRING |
| 252 | #define _PW_TOKENIZER_RECORD_ORIGINAL_STRING(token, domain, string) \ |
| 253 | tokenizer_domain = domain; \ |
Wyatt Hepler | d58eef9 | 2020-05-08 10:39:56 -0700 | [diff] [blame] | 254 | string_literal = string |
| 255 | |
| 256 | TEST_F(TokenizeToGlobalHandler, Domain_Default) { |
| 257 | const char* tokenizer_domain = nullptr; |
| 258 | const char* string_literal = nullptr; |
| 259 | |
| 260 | PW_TOKENIZE_TO_GLOBAL_HANDLER("404"); |
| 261 | |
| 262 | EXPECT_STREQ(tokenizer_domain, PW_TOKENIZER_DEFAULT_DOMAIN); |
| 263 | EXPECT_STREQ(string_literal, "404"); |
| 264 | } |
| 265 | |
| 266 | TEST_F(TokenizeToGlobalHandler, Domain_Specified) { |
| 267 | const char* tokenizer_domain = nullptr; |
| 268 | const char* string_literal = nullptr; |
| 269 | |
| 270 | PW_TOKENIZE_TO_GLOBAL_HANDLER_DOMAIN("www.google.com", "404"); |
| 271 | |
| 272 | EXPECT_STREQ(tokenizer_domain, "www.google.com"); |
| 273 | EXPECT_STREQ(string_literal, "404"); |
| 274 | } |
| 275 | |
| 276 | TEST_F(TokenizeToGlobalHandlerWithPayload, Domain_Default) { |
| 277 | const char* tokenizer_domain = nullptr; |
| 278 | const char* string_literal = nullptr; |
| 279 | |
| 280 | PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD( |
Wyatt Hepler | 7a5e4d6 | 2020-08-31 08:39:16 -0700 | [diff] [blame] | 281 | static_cast<pw_tokenizer_Payload>(123), "Wow%s", "???"); |
Wyatt Hepler | d58eef9 | 2020-05-08 10:39:56 -0700 | [diff] [blame] | 282 | |
| 283 | EXPECT_STREQ(tokenizer_domain, PW_TOKENIZER_DEFAULT_DOMAIN); |
| 284 | EXPECT_STREQ(string_literal, "Wow%s"); |
| 285 | } |
| 286 | |
| 287 | TEST_F(TokenizeToGlobalHandlerWithPayload, Domain_Specified) { |
| 288 | const char* tokenizer_domain = nullptr; |
| 289 | const char* string_literal = nullptr; |
| 290 | |
| 291 | PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD_DOMAIN( |
Wyatt Hepler | 7a5e4d6 | 2020-08-31 08:39:16 -0700 | [diff] [blame] | 292 | "THEDOMAIN", static_cast<pw_tokenizer_Payload>(123), "1234567890"); |
Wyatt Hepler | d58eef9 | 2020-05-08 10:39:56 -0700 | [diff] [blame] | 293 | |
| 294 | EXPECT_STREQ(tokenizer_domain, "THEDOMAIN"); |
| 295 | EXPECT_STREQ(string_literal, "1234567890"); |
| 296 | } |
| 297 | |
Wyatt Hepler | 6639c45 | 2020-05-06 11:43:07 -0700 | [diff] [blame] | 298 | } // namespace |
| 299 | } // namespace pw::tokenizer |