1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/cbor/writer.h"
#include <cmath>
#include <limits>
#include <string>
#include <string_view>
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
/* Leveraging RFC 7049 examples from
https://github.com/cbor/test-vectors/blob/master/appendix_a.json. */
namespace cbor {
TEST(CBORWriterTest, TestWriteUint) {
struct UintTestCase {
const int64_t value;
const std::string_view cbor;
};
static const UintTestCase kUintTestCases[] = {
// Reminder: must specify length when creating string pieces
// with null bytes, else the string will truncate prematurely.
{0, std::string_view("\x00", 1)},
{1, std::string_view("\x01")},
{10, std::string_view("\x0a")},
{23, std::string_view("\x17")},
{24, std::string_view("\x18\x18")},
{25, std::string_view("\x18\x19")},
{100, std::string_view("\x18\x64")},
{1000, std::string_view("\x19\x03\xe8")},
{1000000, std::string_view("\x1a\x00\x0f\x42\x40", 5)},
{0xFFFFFFFF, std::string_view("\x1a\xff\xff\xff\xff")},
{0x100000000,
std::string_view("\x1b\x00\x00\x00\x01\x00\x00\x00\x00", 9)},
{std::numeric_limits<int64_t>::max(),
std::string_view("\x1b\x7f\xff\xff\xff\xff\xff\xff\xff")}};
for (const UintTestCase& test_case : kUintTestCases) {
auto cbor = Writer::Write(Value(test_case.value));
ASSERT_TRUE(cbor.has_value());
EXPECT_THAT(cbor.value(), testing::ElementsAreArray(test_case.cbor));
}
}
TEST(CBORWriterTest, TestWriteNegativeInteger) {
static const struct {
const int64_t negative_int;
const std::string_view cbor;
} kNegativeIntTestCases[] = {
{-1LL, std::string_view("\x20")},
{-10LL, std::string_view("\x29")},
{-23LL, std::string_view("\x36")},
{-24LL, std::string_view("\x37")},
{-25LL, std::string_view("\x38\x18")},
{-100LL, std::string_view("\x38\x63")},
{-1000LL, std::string_view("\x39\x03\xe7")},
{-4294967296LL, std::string_view("\x3a\xff\xff\xff\xff")},
{-4294967297LL,
std::string_view("\x3b\x00\x00\x00\x01\x00\x00\x00\x00", 9)},
{std::numeric_limits<int64_t>::min(),
std::string_view("\x3b\x7f\xff\xff\xff\xff\xff\xff\xff")},
};
for (const auto& test_case : kNegativeIntTestCases) {
SCOPED_TRACE(testing::Message() << "testing negative int at index: "
<< test_case.negative_int);
auto cbor = Writer::Write(Value(test_case.negative_int));
ASSERT_TRUE(cbor.has_value());
EXPECT_THAT(cbor.value(), testing::ElementsAreArray(test_case.cbor));
}
}
TEST(CBORWriterTest, TestWriteBytes) {
struct BytesTestCase {
const std::vector<uint8_t> bytes;
const std::string_view cbor;
};
static const BytesTestCase kBytesTestCases[] = {
{{}, std::string_view("\x40")},
{{0x01, 0x02, 0x03, 0x04}, std::string_view("\x44\x01\x02\x03\x04")},
};
for (const BytesTestCase& test_case : kBytesTestCases) {
auto cbor = Writer::Write(Value(test_case.bytes));
ASSERT_TRUE(cbor.has_value());
EXPECT_THAT(cbor.value(), testing::ElementsAreArray(test_case.cbor));
}
}
TEST(CBORWriterTest, TestWriteString) {
struct StringTestCase {
const std::string string;
const std::string_view cbor;
};
static const StringTestCase kStringTestCases[] = {
{"", std::string_view("\x60")},
{"a", std::string_view("\x61\x61")},
{"IETF", std::string_view("\x64\x49\x45\x54\x46")},
{"\"\\", std::string_view("\x62\x22\x5c")},
{"\xc3\xbc", std::string_view("\x62\xc3\xbc")},
{"\xe6\xb0\xb4", std::string_view("\x63\xe6\xb0\xb4")},
{"\xf0\x90\x85\x91", std::string_view("\x64\xf0\x90\x85\x91")}};
for (const StringTestCase& test_case : kStringTestCases) {
SCOPED_TRACE(testing::Message()
<< "testing encoding string : " << test_case.string);
auto cbor = Writer::Write(Value(test_case.string));
ASSERT_TRUE(cbor.has_value());
EXPECT_THAT(cbor.value(), testing::ElementsAreArray(test_case.cbor));
}
}
TEST(CBORWriterTest, TestWriteArray) {
static const uint8_t kArrayTestCaseCbor[] = {
// clang-format off
0x98, 0x19, // array of 25 elements
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
0x18, 0x18, 0x19,
// clang-format on
};
std::vector<Value> array;
for (int64_t i = 1; i <= 25; i++) {
array.push_back(Value(i));
}
auto cbor = Writer::Write(Value(array));
ASSERT_TRUE(cbor.has_value());
EXPECT_THAT(cbor.value(),
testing::ElementsAreArray(kArrayTestCaseCbor,
std::size(kArrayTestCaseCbor)));
}
TEST(CBORWriterTest, TestWriteMap) {
static const uint8_t kMapTestCaseCbor[] = {
// clang-format off
0xb8, 0x19, // map of 25 pairs:
0x00, // key 0
0x61, 0x61, // value "a"
0x17, // key 23
0x61, 0x62, // value "b"
0x18, 0x18, // key 24
0x61, 0x63, // value "c"
0x18, 0xFF, // key 255
0x61, 0x64, // value "d"
0x19, 0x01, 0x00, // key 256
0x61, 0x65, // value "e"
0x19, 0xFF, 0xFF, // key 65535
0x61, 0x66, // value "f"
0x1A, 0x00, 0x01, 0x00, 0x00, // key 65536
0x61, 0x67, // value "g"
0x1A, 0xFF, 0xFF, 0xFF, 0xFF, // key 4294967295
0x61, 0x68, // value "h"
// key 4294967296
0x1B, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x61, 0x69, // value "i"
// key INT64_MAX
0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x61, 0x6a, // value "j"
0x20, // key -1
0x61, 0x6b, // value "k"
0x37, // key -24
0x61, 0x6c, // value "l"
0x38, 0x18, // key -25
0x61, 0x6d, // value "m"
0x38, 0xFF, // key -256
0x61, 0x6e, // value "n"
0x39, 0x01, 0x00, // key -257
0x61, 0x6f, // value "o"
0x3A, 0x00, 0x01, 0x00, 0x00, // key -65537
0x61, 0x70, // value "p"
0x3A, 0xFF, 0xFF, 0xFF, 0xFF, // key -4294967296
0x61, 0x71, // value "q"
// key -4294967297
0x3B, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x61, 0x72, // value "r"
// key INT64_MIN
0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x61, 0x73, // value "s"
0x41, 'a', // byte string "a"
0x02,
0x43, 'b', 'a', 'r', // byte string "bar"
0x03,
0x43, 'f', 'o', 'o', // byte string "foo"
0x04,
0x60, // key ""
0x61, 0x2e, // value "."
0x61, 0x65, // key "e"
0x61, 0x45, // value "E"
0x62, 0x61, 0x61, // key "aa"
0x62, 0x41, 0x41, // value "AA"
// clang-format on
};
Value::MapValue map;
// Shorter strings sort first in CTAP, thus the “aa” value should be
// serialised last in the map.
map[Value("aa")] = Value("AA");
map[Value("e")] = Value("E");
// The empty string is shorter than all others, so should appear first among
// the strings.
map[Value("")] = Value(".");
// Map keys are sorted by major type, by byte length, and then by
// byte-wise lexical order. So all integer type keys should appear before
// key "" and all positive integer keys should appear before negative integer
// keys.
map[Value(-1)] = Value("k");
map[Value(-24)] = Value("l");
map[Value(-25)] = Value("m");
map[Value(-256)] = Value("n");
map[Value(-257)] = Value("o");
map[Value(-65537)] = Value("p");
map[Value(int64_t(-4294967296))] = Value("q");
map[Value(int64_t(-4294967297))] = Value("r");
map[Value(std::numeric_limits<int64_t>::min())] = Value("s");
map[Value(Value::BinaryValue{'a'})] = Value(2);
map[Value(Value::BinaryValue{'b', 'a', 'r'})] = Value(3);
map[Value(Value::BinaryValue{'f', 'o', 'o'})] = Value(4);
map[Value(0)] = Value("a");
map[Value(23)] = Value("b");
map[Value(24)] = Value("c");
map[Value(std::numeric_limits<uint8_t>::max())] = Value("d");
map[Value(256)] = Value("e");
map[Value(std::numeric_limits<uint16_t>::max())] = Value("f");
map[Value(65536)] = Value("g");
map[Value(int64_t(std::numeric_limits<uint32_t>::max()))] = Value("h");
map[Value(int64_t(4294967296))] = Value("i");
map[Value(std::numeric_limits<int64_t>::max())] = Value("j");
auto cbor = Writer::Write(Value(map));
ASSERT_TRUE(cbor.has_value());
EXPECT_THAT(cbor.value(), testing::ElementsAreArray(
kMapTestCaseCbor, std::size(kMapTestCaseCbor)));
}
TEST(CBORWriterTest, TestWriteMapWithArray) {
static const uint8_t kMapArrayTestCaseCbor[] = {
// clang-format off
0xa2, // map of 2 pairs
0x61, 0x61, // "a"
0x01,
0x61, 0x62, // "b"
0x82, // array with 2 elements
0x02,
0x03,
// clang-format on
};
Value::MapValue map;
map[Value("a")] = Value(1);
Value::ArrayValue array;
array.push_back(Value(2));
array.push_back(Value(3));
map[Value("b")] = Value(array);
auto cbor = Writer::Write(Value(map));
ASSERT_TRUE(cbor.has_value());
EXPECT_THAT(cbor.value(),
testing::ElementsAreArray(kMapArrayTestCaseCbor,
std::size(kMapArrayTestCaseCbor)));
}
TEST(CBORWriterTest, TestWriteNestedMap) {
static const uint8_t kNestedMapTestCase[] = {
// clang-format off
0xa2, // map of 2 pairs
0x61, 0x61, // "a"
0x01,
0x61, 0x62, // "b"
0xa2, // map of 2 pairs
0x61, 0x63, // "c"
0x02,
0x61, 0x64, // "d"
0x03,
// clang-format on
};
Value::MapValue map;
map[Value("a")] = Value(1);
Value::MapValue nested_map;
nested_map[Value("c")] = Value(2);
nested_map[Value("d")] = Value(3);
map[Value("b")] = Value(nested_map);
auto cbor = Writer::Write(Value(map));
ASSERT_TRUE(cbor.has_value());
EXPECT_THAT(cbor.value(),
testing::ElementsAreArray(kNestedMapTestCase,
std::size(kNestedMapTestCase)));
}
TEST(CBORWriterTest, TestSignedExchangeExample) {
// Example adopted from:
// https://wicg.github.io/webpackage/draft-yasskin-http-origin-signed-responses.html
static const uint8_t kSignedExchangeExample[] = {
// clang-format off
0xa5, // map of 5 pairs
0x0a, // 10
0x01,
0x18, 0x64, // 100
0x02,
0x20, // -1
0x03,
0x61, 'z', // text string "z"
0x04,
0x62, 'a', 'a', // text string "aa"
0x05,
/*
0x81, 0x18, 0x64, // [100] (array as map key is not yet supported)
0x06,
0x81, 0x20, // [-1] (array as map key is not yet supported)
0x07,
0xf4, // false (boolean as map key is not yet supported)
0x08,
*/
// clang-format on
};
Value::MapValue map;
map[Value(10)] = Value(1);
map[Value(100)] = Value(2);
map[Value(-1)] = Value(3);
map[Value("z")] = Value(4);
map[Value("aa")] = Value(5);
auto cbor = Writer::Write(Value(map));
ASSERT_TRUE(cbor.has_value());
EXPECT_THAT(cbor.value(),
testing::ElementsAreArray(kSignedExchangeExample,
std::size(kSignedExchangeExample)));
}
TEST(CBORWriterTest, TestWriteSimpleValue) {
static const struct {
Value::SimpleValue simple_value;
const std::string_view cbor;
} kSimpleTestCase[] = {
{Value::SimpleValue::FALSE_VALUE, std::string_view("\xf4")},
{Value::SimpleValue::TRUE_VALUE, std::string_view("\xf5")},
{Value::SimpleValue::NULL_VALUE, std::string_view("\xf6")},
{Value::SimpleValue::UNDEFINED, std::string_view("\xf7")}};
for (const auto& test_case : kSimpleTestCase) {
auto cbor = Writer::Write(Value(test_case.simple_value));
ASSERT_TRUE(cbor.has_value());
EXPECT_THAT(cbor.value(), testing::ElementsAreArray(test_case.cbor));
}
}
TEST(CBORWriterTest, TestWriteFloat) {
static const struct {
double float_value;
const std::vector<uint8_t> cbor;
} kSimpleTestCase[] = {
// 16 bit floating point values.
{0.0, {0xf9, 0x00, 0x00}},
{-0.0, {0xf9, 0x80, 0x00}},
{std::numeric_limits<double>::infinity(), {0xf9, 0x7c, 0x00}},
{-std::numeric_limits<double>::infinity(), {0xf9, 0xfc, 0x00}},
{std::numeric_limits<double>::quiet_NaN(), {0xf9, 0x7c, 0x01}},
{0.5, {0xf9, 0x38, 0x00}},
{3.140625, {0xf9, 0x42, 0x48}},
{std::ldexp(1023.0, -24), {0xf9, 0x03, 0xFF}},
{65504, {0xf9, 0x7b, 0xff}},
{-65504, {0xf9, 0xfb, 0xff}},
// 32 bit floating point value.
{3.1415927410125732, {0xfa, 0x40, 0x49, 0x0f, 0xdb}},
{std::ldexp(1.0, 32), {0xfa, 0x4f, 0x80, 0x00, 0x00}},
// 64 bit floating point value.
{3.141592653589793,
{0xfb, 0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18}},
{std::ldexp(1.0, 128),
{0xfb, 0x47, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
};
for (const auto& test_case : kSimpleTestCase) {
SCOPED_TRACE(testing::Message()
<< "testing float value: " << test_case.float_value);
auto cbor = Writer::Write(Value(test_case.float_value));
ASSERT_TRUE(cbor.has_value());
EXPECT_EQ(cbor.value(), test_case.cbor);
}
}
// For major type 0, 2, 3, empty CBOR array, and empty CBOR map, the nesting
// depth is expected to be 0 since the CBOR decoder does not need to parse
// any nested CBOR value elements.
TEST(CBORWriterTest, TestWriteSingleLayer) {
const Value simple_uint = Value(1);
const Value simple_string = Value("a");
const std::vector<uint8_t> byte_data = {0x01, 0x02, 0x03, 0x04};
const Value simple_bytestring = Value(byte_data);
Value::ArrayValue empty_cbor_array;
Value::MapValue empty_cbor_map;
const Value empty_array_value = Value(empty_cbor_array);
const Value empty_map_value = Value(empty_cbor_map);
Value::ArrayValue simple_array;
simple_array.push_back(Value(2));
Value::MapValue simple_map;
simple_map[Value("b")] = Value(3);
const Value single_layer_cbor_map = Value(simple_map);
const Value single_layer_cbor_array = Value(simple_array);
EXPECT_TRUE(Writer::Write(simple_uint, 0).has_value());
EXPECT_TRUE(Writer::Write(simple_string, 0).has_value());
EXPECT_TRUE(Writer::Write(simple_bytestring, 0).has_value());
EXPECT_TRUE(Writer::Write(empty_array_value, 0).has_value());
EXPECT_TRUE(Writer::Write(empty_map_value, 0).has_value());
EXPECT_FALSE(Writer::Write(single_layer_cbor_array, 0).has_value());
EXPECT_TRUE(Writer::Write(single_layer_cbor_array, 1).has_value());
EXPECT_FALSE(Writer::Write(single_layer_cbor_map, 0).has_value());
EXPECT_TRUE(Writer::Write(single_layer_cbor_map, 1).has_value());
}
// Major type 5 nested CBOR map value with following structure.
// {"a": 1,
// "b": {"c": 2,
// "d": 3}}
TEST(CBORWriterTest, NestedMaps) {
Value::MapValue cbor_map;
cbor_map[Value("a")] = Value(1);
Value::MapValue nested_map;
nested_map[Value("c")] = Value(2);
nested_map[Value("d")] = Value(3);
cbor_map[Value("b")] = Value(nested_map);
EXPECT_TRUE(Writer::Write(Value(cbor_map), 2).has_value());
EXPECT_FALSE(Writer::Write(Value(cbor_map), 1).has_value());
}
// Testing Write() function for following CBOR structure with depth of 3.
// [1,
// 2,
// 3,
// {"a": 1,
// "b": {"c": 2,
// "d": 3}}]
TEST(CBORWriterTest, UnbalancedNestedContainers) {
Value::ArrayValue cbor_array;
Value::MapValue cbor_map;
Value::MapValue nested_map;
cbor_map[Value("a")] = Value(1);
nested_map[Value("c")] = Value(2);
nested_map[Value("d")] = Value(3);
cbor_map[Value("b")] = Value(nested_map);
cbor_array.push_back(Value(1));
cbor_array.push_back(Value(2));
cbor_array.push_back(Value(3));
cbor_array.push_back(Value(cbor_map));
EXPECT_TRUE(Writer::Write(Value(cbor_array), 3).has_value());
EXPECT_FALSE(Writer::Write(Value(cbor_array), 2).has_value());
}
// Testing Write() function for following CBOR structure.
// {"a": 1,
// "b": {"c": 2,
// "d": 3
// "h": { "e": 4,
// "f": 5,
// "g": [6, 7, [8]]}}}
// Since above CBOR contains 5 nesting levels. Thus, Write() is expected to
// return empty optional object when maximum nesting layer size is set to 4.
TEST(CBORWriterTest, OverlyNestedCBOR) {
Value::MapValue map;
Value::MapValue nested_map;
Value::MapValue inner_nested_map;
Value::ArrayValue inner_array;
Value::ArrayValue array;
map[Value("a")] = Value(1);
nested_map[Value("c")] = Value(2);
nested_map[Value("d")] = Value(3);
inner_nested_map[Value("e")] = Value(4);
inner_nested_map[Value("f")] = Value(5);
inner_array.push_back(Value(6));
array.push_back(Value(6));
array.push_back(Value(7));
array.push_back(Value(inner_array));
inner_nested_map[Value("g")] = Value(array);
nested_map[Value("h")] = Value(inner_nested_map);
map[Value("b")] = Value(nested_map);
EXPECT_TRUE(Writer::Write(Value(map), 5).has_value());
EXPECT_FALSE(Writer::Write(Value(map), 4).has_value());
}
} // namespace cbor
|