values_util_unittest.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/json/values_util.h"
  5. #include <limits>
  6. #include "base/files/file_path.h"
  7. #include "base/strings/string_piece.h"
  8. #include "base/time/time.h"
  9. #include "base/unguessable_token.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace base {
  12. namespace {
  13. TEST(ValuesUtilTest, BasicInt64Limits) {
  14. constexpr struct {
  15. int64_t input;
  16. StringPiece expected;
  17. } kTestCases[] = {
  18. {0, "0"},
  19. {-1234, "-1234"},
  20. {5678, "5678"},
  21. {std::numeric_limits<int64_t>::lowest(), "-9223372036854775808"},
  22. {std::numeric_limits<int64_t>::max(), "9223372036854775807"},
  23. };
  24. for (const auto& test_case : kTestCases) {
  25. int64_t input = test_case.input;
  26. TimeDelta time_delta_input = Microseconds(input);
  27. Time time_input = Time::FromDeltaSinceWindowsEpoch(time_delta_input);
  28. Value expected(test_case.expected);
  29. SCOPED_TRACE(testing::Message()
  30. << "input: " << input << ", expected: " << expected);
  31. EXPECT_EQ(Int64ToValue(input), expected);
  32. EXPECT_EQ(*ValueToInt64(&expected), input);
  33. EXPECT_EQ(TimeDeltaToValue(time_delta_input), expected);
  34. EXPECT_EQ(*ValueToTimeDelta(&expected), time_delta_input);
  35. EXPECT_EQ(TimeToValue(time_input), expected);
  36. EXPECT_EQ(*ValueToTime(&expected), time_input);
  37. }
  38. }
  39. TEST(ValuesUtilTest, InvalidInt64Values) {
  40. const std::unique_ptr<Value> kTestCases[] = {
  41. nullptr,
  42. std::make_unique<Value>(),
  43. std::make_unique<Value>(0),
  44. std::make_unique<Value>(1234),
  45. std::make_unique<Value>(true),
  46. std::make_unique<Value>(Value::Type::BINARY),
  47. std::make_unique<Value>(Value::Type::LIST),
  48. std::make_unique<Value>(Value::Type::DICTIONARY),
  49. std::make_unique<Value>(""),
  50. std::make_unique<Value>("abcd"),
  51. std::make_unique<Value>("1234.0"),
  52. std::make_unique<Value>("1234a"),
  53. std::make_unique<Value>("a1234"),
  54. };
  55. for (const auto& test_case : kTestCases) {
  56. EXPECT_FALSE(ValueToInt64(test_case.get()));
  57. EXPECT_FALSE(ValueToTimeDelta(test_case.get()));
  58. EXPECT_FALSE(ValueToTime(test_case.get()));
  59. }
  60. }
  61. TEST(ValuesUtilTest, FilePath) {
  62. // Ω is U+03A9 GREEK CAPITAL LETTER OMEGA, a non-ASCII character.
  63. constexpr StringPiece kTestCases[] = {
  64. "/unix/Ω/path.dat",
  65. "C:\\windows\\Ω\\path.dat",
  66. };
  67. for (auto test_case : kTestCases) {
  68. FilePath input = FilePath::FromUTF8Unsafe(test_case);
  69. Value expected(test_case);
  70. SCOPED_TRACE(testing::Message() << "test_case: " << test_case);
  71. EXPECT_EQ(FilePathToValue(input), expected);
  72. EXPECT_EQ(*ValueToFilePath(&expected), input);
  73. }
  74. }
  75. TEST(ValuesUtilTest, UnguessableToken) {
  76. constexpr struct {
  77. uint64_t high;
  78. uint64_t low;
  79. StringPiece expected;
  80. } kTestCases[] = {
  81. {0x123456u, 0x9ABCu, "5634120000000000BC9A000000000000"},
  82. };
  83. for (const auto& test_case : kTestCases) {
  84. UnguessableToken input =
  85. UnguessableToken::Deserialize(test_case.high, test_case.low);
  86. Value expected(test_case.expected);
  87. SCOPED_TRACE(testing::Message() << "expected: " << test_case.expected);
  88. EXPECT_EQ(UnguessableTokenToValue(input), expected);
  89. EXPECT_EQ(*ValueToUnguessableToken(&expected), input);
  90. }
  91. }
  92. } // namespace
  93. } // namespace base