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