diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-12 14:27:29 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-13 09:35:20 +0000 |
commit | c30a6232df03e1efbd9f3b226777b07e087a1122 (patch) | |
tree | e992f45784689f373bcc38d1b79a239ebe17ee23 /chromium/base/util/values | |
parent | 7b5b123ac58f58ffde0f4f6e488bcd09aa4decd3 (diff) | |
download | qtwebengine-chromium-85-based.tar.gz |
BASELINE: Update Chromium to 85.0.4183.14085-based
Change-Id: Iaa42f4680837c57725b1344f108c0196741f6057
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'chromium/base/util/values')
-rw-r--r-- | chromium/base/util/values/BUILD.gn | 3 | ||||
-rw-r--r-- | chromium/base/util/values/values_util.cc | 59 | ||||
-rw-r--r-- | chromium/base/util/values/values_util.h | 42 | ||||
-rw-r--r-- | chromium/base/util/values/values_util_unittest.cc | 59 |
4 files changed, 144 insertions, 19 deletions
diff --git a/chromium/base/util/values/BUILD.gn b/chromium/base/util/values/BUILD.gn index 5048d86c208..f14d26426bd 100644 --- a/chromium/base/util/values/BUILD.gn +++ b/chromium/base/util/values/BUILD.gn @@ -8,7 +8,7 @@ source_set("values_util") { "values_util.h", ] - deps = [ "//base:base" ] + deps = [ "//base" ] } source_set("unittests") { @@ -17,6 +17,7 @@ source_set("unittests") { deps = [ ":values_util", + "//base", "//testing/gtest", ] } diff --git a/chromium/base/util/values/values_util.cc b/chromium/base/util/values/values_util.cc index 43b317b3100..fc9341035ce 100644 --- a/chromium/base/util/values/values_util.cc +++ b/chromium/base/util/values/values_util.cc @@ -4,10 +4,33 @@ #include "base/util/values/values_util.h" +#include "base/files/file_path.h" #include "base/strings/string_number_conversions.h" +#include "base/time/time.h" +#include "base/unguessable_token.h" + +// Warning: The Values involved could be stored on persistent storage like files +// on disks. Therefore, changes in implementation could lead to data corruption +// and must be done with caution. namespace util { +namespace { + +// Helper to serialize/deserialize an UnguessableToken. +// +// It assumes a little-endian CPU, which is arguably a bug. +union UnguessableTokenRepresentation { + struct Field { + uint64_t high; + uint64_t low; + } field; + + uint8_t buffer[sizeof(Field)]; +}; + +} // namespace + base::Value Int64ToValue(int64_t integer) { return base::Value(base::NumberToString(integer)); } @@ -57,4 +80,40 @@ base::Optional<base::Time> ValueToTime(const base::Value& value) { return base::Time::FromDeltaSinceWindowsEpoch(*time_delta); } +base::Value FilePathToValue(base::FilePath file_path) { + return base::Value(file_path.AsUTF8Unsafe()); +} + +base::Optional<base::FilePath> ValueToFilePath(const base::Value* value) { + return value ? ValueToFilePath(*value) : base::nullopt; +} + +base::Optional<base::FilePath> ValueToFilePath(const base::Value& value) { + if (!value.is_string()) + return base::nullopt; + return base::FilePath::FromUTF8Unsafe(value.GetString()); +} + +base::Value UnguessableTokenToValue(base::UnguessableToken token) { + UnguessableTokenRepresentation repr; + repr.field.high = token.GetHighForSerialization(); + repr.field.low = token.GetLowForSerialization(); + return base::Value(base::HexEncode(repr.buffer, sizeof(repr.buffer))); +} + +base::Optional<base::UnguessableToken> ValueToUnguessableToken( + const base::Value* value) { + return value ? ValueToUnguessableToken(*value) : base::nullopt; +} + +base::Optional<base::UnguessableToken> ValueToUnguessableToken( + const base::Value& value) { + if (!value.is_string()) + return base::nullopt; + UnguessableTokenRepresentation repr; + if (!base::HexStringToSpan(value.GetString(), repr.buffer)) + return base::nullopt; + return base::UnguessableToken::Deserialize(repr.field.high, repr.field.low); +} + } // namespace util diff --git a/chromium/base/util/values/values_util.h b/chromium/base/util/values/values_util.h index de9fd1b9dc4..2958ae01405 100644 --- a/chromium/base/util/values/values_util.h +++ b/chromium/base/util/values/values_util.h @@ -6,31 +6,57 @@ #define BASE_UTIL_VALUES_VALUES_UTIL_H_ #include "base/optional.h" -#include "base/time/time.h" #include "base/values.h" +namespace base { +class FilePath; +class Time; +class TimeDelta; +class UnguessableToken; +} // namespace base + namespace util { -// Simple helper functions for converting int64_t, base::TimeDelta and -// base::Time to numeric string base::Values. -// Because base::TimeDelta and base::Time share the same internal representation -// as int64_t they are stored using the exact same numeric string format. +// Simple helper functions for converting between base::Value and other types. +// The base::Value representation is stable, suitable for persistent storage +// e.g. as JSON on disk. +// +// It is valid to pass nullptr to the ValueToEtc functions. They will just +// return base::nullopt. -// Stores the int64_t as a string. +// Converts between an int64_t and a string-flavored base::Value (a human +// readable string of that number). base::Value Int64ToValue(int64_t integer); base::Optional<int64_t> ValueToInt64(const base::Value* value); base::Optional<int64_t> ValueToInt64(const base::Value& value); -// Converts the TimeDelta to an int64_t of microseconds. +// Converts between a base::TimeDelta (an int64_t number of microseconds) and a +// string-flavored base::Value (a human readable string of that number). base::Value TimeDeltaToValue(base::TimeDelta time_delta); base::Optional<base::TimeDelta> ValueToTimeDelta(const base::Value* value); base::Optional<base::TimeDelta> ValueToTimeDelta(const base::Value& value); -// Converts the Time to a TimeDelta from the Windows epoch. +// Converts between a base::Time (an int64_t number of microseconds since the +// Windows epoch) and a string-flavored base::Value (a human readable string of +// that number). base::Value TimeToValue(base::Time time); base::Optional<base::Time> ValueToTime(const base::Value* value); base::Optional<base::Time> ValueToTime(const base::Value& value); +// Converts between a base::FilePath (a std::string or base::string16) and a +// string-flavored base::Value (the UTF-8 representation). +base::Value FilePathToValue(base::FilePath file_path); +base::Optional<base::FilePath> ValueToFilePath(const base::Value* value); +base::Optional<base::FilePath> ValueToFilePath(const base::Value& value); + +// Converts between a base::UnguessableToken (128 bits) and a string-flavored +// base::Value (32 hexadecimal digits). +base::Value UnguessableTokenToValue(base::UnguessableToken token); +base::Optional<base::UnguessableToken> ValueToUnguessableToken( + const base::Value* value); +base::Optional<base::UnguessableToken> ValueToUnguessableToken( + const base::Value& value); + } // namespace util #endif // BASE_UTIL_VALUES_VALUES_UTIL_H_ diff --git a/chromium/base/util/values/values_util_unittest.cc b/chromium/base/util/values/values_util_unittest.cc index ee543c18d84..d2954a6c2fd 100644 --- a/chromium/base/util/values/values_util_unittest.cc +++ b/chromium/base/util/values/values_util_unittest.cc @@ -2,28 +2,32 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include <limits> - #include "base/util/values/values_util.h" +#include <limits> + +#include "base/files/file_path.h" +#include "base/strings/string_piece.h" +#include "base/time/time.h" +#include "base/unguessable_token.h" #include "testing/gtest/include/gtest/gtest.h" namespace util { namespace { -TEST(ValuesUtilTest, BasicLimits) { - struct { +TEST(ValuesUtilTest, BasicInt64Limits) { + constexpr struct { int64_t input; - const char* expected; - } test_cases[] = { + base::StringPiece expected; + } kTestCases[] = { {0, "0"}, {-1234, "-1234"}, {5678, "5678"}, {std::numeric_limits<int64_t>::lowest(), "-9223372036854775808"}, {std::numeric_limits<int64_t>::max(), "9223372036854775807"}, }; - for (const auto& test_case : test_cases) { + for (const auto& test_case : kTestCases) { int64_t input = test_case.input; base::TimeDelta time_delta_input = base::TimeDelta::FromMicroseconds(input); base::Time time_input = @@ -43,8 +47,8 @@ TEST(ValuesUtilTest, BasicLimits) { } } -TEST(ValuesUtilTest, InvalidValues) { - std::unique_ptr<base::Value> test_cases[] = { +TEST(ValuesUtilTest, InvalidInt64Values) { + const std::unique_ptr<base::Value> kTestCases[] = { nullptr, std::make_unique<base::Value>(), std::make_unique<base::Value>(0), @@ -59,13 +63,48 @@ TEST(ValuesUtilTest, InvalidValues) { std::make_unique<base::Value>("1234a"), std::make_unique<base::Value>("a1234"), }; - for (const auto& test_case : test_cases) { + for (const auto& test_case : kTestCases) { EXPECT_FALSE(ValueToInt64(test_case.get())); EXPECT_FALSE(ValueToTimeDelta(test_case.get())); EXPECT_FALSE(ValueToTime(test_case.get())); } } +TEST(ValuesUtilTest, FilePath) { + // Ω is U+03A9 GREEK CAPITAL LETTER OMEGA, a non-ASCII character. + constexpr base::StringPiece kTestCases[] = { + "/unix/Ω/path.dat", + "C:\\windows\\Ω\\path.dat", + }; + for (auto test_case : kTestCases) { + base::FilePath input = base::FilePath::FromUTF8Unsafe(test_case); + base::Value expected(test_case); + SCOPED_TRACE(testing::Message() << "test_case: " << test_case); + + EXPECT_EQ(FilePathToValue(input), expected); + EXPECT_EQ(*ValueToFilePath(&expected), input); + } +} + +TEST(ValuesUtilTest, UnguessableToken) { + constexpr struct { + uint64_t high; + uint64_t low; + base::StringPiece expected; + } kTestCases[] = { + {0x123456u, 0x9ABCu, "5634120000000000BC9A000000000000"}, + }; + for (const auto& test_case : kTestCases) { + base::UnguessableToken input = + base::UnguessableToken::Deserialize(test_case.high, test_case.low); + base::Value expected(test_case.expected); + SCOPED_TRACE(testing::Message() << "expected: " << test_case.expected); + + EXPECT_EQ(UnguessableTokenToValue(input), expected); + EXPECT_EQ(*ValueToUnguessableToken(&expected), input); + } +} + } // namespace } // namespace util |