Title: [254678] trunk
Revision
254678
Author
[email protected]
Date
2020-01-16 07:01:52 -0800 (Thu, 16 Jan 2020)

Log Message

KeyedDecoderGeneric crashes when it accesses data with non-existing key
https://bugs.webkit.org/show_bug.cgi?id=205902

Patch by Takashi Komori <[email protected]> on 2020-01-16
Reviewed by Fujii Hironori.

Source/WebCore:

Add null check function for KeyedDecoderGeneric.

Test: TestWebKitAPI/Tests/WebCore/KeyedCodingGeneric.cpp

* platform/generic/KeyedDecoderGeneric.cpp:
(WebCore::KeyedDecoderGeneric::Dictionary::get):
(WebCore::KeyedDecoderGeneric::getPointerFromDictionaryStack):
(WebCore::KeyedDecoderGeneric::decodeSimpleValue):
(WebCore::KeyedDecoderGeneric::decodeBytes):
(WebCore::KeyedDecoderGeneric::decodeBool):
(WebCore::KeyedDecoderGeneric::decodeUInt32):
(WebCore::KeyedDecoderGeneric::decodeUInt64):
(WebCore::KeyedDecoderGeneric::decodeInt32):
(WebCore::KeyedDecoderGeneric::decodeInt64):
(WebCore::KeyedDecoderGeneric::decodeFloat):
(WebCore::KeyedDecoderGeneric::decodeDouble):
(WebCore::KeyedDecoderGeneric::decodeString):
(WebCore::KeyedDecoderGeneric::beginObject):
(WebCore::KeyedDecoderGeneric::beginArray):
* platform/generic/KeyedDecoderGeneric.h:

Tools:

* TestWebKitAPI/CMakeLists.txt:
* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WebCore/KeyedCoding.cpp: Added.
(TestWebKitAPI::checkDecodedBytes):
(TestWebKitAPI::TEST):
(TestWebKitAPI::testSimpleValue):
(TestWebKitAPI::KeyedCodingTestObject::encode):
(TestWebKitAPI::KeyedCodingTestObject::decode):
(TestWebKitAPI::KeyedCodingTestObject::KeyedCodingTestObject):
(TestWebKitAPI::KeyedCodingTestObject::equals const):
(TestWebKitAPI::operator==):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (254677 => 254678)


--- trunk/Source/WebCore/ChangeLog	2020-01-16 14:47:40 UTC (rev 254677)
+++ trunk/Source/WebCore/ChangeLog	2020-01-16 15:01:52 UTC (rev 254678)
@@ -1,3 +1,31 @@
+2020-01-16  Takashi Komori  <[email protected]>
+
+        KeyedDecoderGeneric crashes when it accesses data with non-existing key
+        https://bugs.webkit.org/show_bug.cgi?id=205902
+
+        Reviewed by Fujii Hironori.
+
+        Add null check function for KeyedDecoderGeneric.
+
+        Test: TestWebKitAPI/Tests/WebCore/KeyedCodingGeneric.cpp
+
+        * platform/generic/KeyedDecoderGeneric.cpp:
+        (WebCore::KeyedDecoderGeneric::Dictionary::get):
+        (WebCore::KeyedDecoderGeneric::getPointerFromDictionaryStack):
+        (WebCore::KeyedDecoderGeneric::decodeSimpleValue):
+        (WebCore::KeyedDecoderGeneric::decodeBytes):
+        (WebCore::KeyedDecoderGeneric::decodeBool):
+        (WebCore::KeyedDecoderGeneric::decodeUInt32):
+        (WebCore::KeyedDecoderGeneric::decodeUInt64):
+        (WebCore::KeyedDecoderGeneric::decodeInt32):
+        (WebCore::KeyedDecoderGeneric::decodeInt64):
+        (WebCore::KeyedDecoderGeneric::decodeFloat):
+        (WebCore::KeyedDecoderGeneric::decodeDouble):
+        (WebCore::KeyedDecoderGeneric::decodeString):
+        (WebCore::KeyedDecoderGeneric::beginObject):
+        (WebCore::KeyedDecoderGeneric::beginArray):
+        * platform/generic/KeyedDecoderGeneric.h:
+
 2020-01-16  Carlos Garcia Campos  <[email protected]>
 
         [GTK][WPE] Password field doesn't get input method

Modified: trunk/Source/WebCore/platform/generic/KeyedDecoderGeneric.cpp (254677 => 254678)


--- trunk/Source/WebCore/platform/generic/KeyedDecoderGeneric.cpp	2020-01-16 14:47:40 UTC (rev 254677)
+++ trunk/Source/WebCore/platform/generic/KeyedDecoderGeneric.cpp	2020-01-16 15:01:52 UTC (rev 254678)
@@ -42,7 +42,7 @@
 
     template <typename T>
     void add(const String& key, T&& value) { m_map.add(key, makeUnique<Node>(std::forward<T>(value))); }
-    Node& get(const String& key) { return *m_map.get(key); }
+    Node* get(const String& key) { return m_map.get(key); }
 
 private:
     HashMap<String, std::unique_ptr<Node>> m_map;
@@ -181,11 +181,35 @@
         m_arrayStack.removeLast();
 }
 
+template<typename T>
+const T* KeyedDecoderGeneric::getPointerFromDictionaryStack(const String& key)
+{
+    auto& dictionary = m_dictionaryStack.last();
+
+    auto node = dictionary->get(key);
+    if (!node)
+        return nullptr;
+
+    return WTF::get_if<T>(*node);
+}
+
+template<typename T>
+bool KeyedDecoderGeneric::decodeSimpleValue(const String& key, T& result)
+{
+    auto value = getPointerFromDictionaryStack<T>(key);
+    if (!value)
+        return false;
+
+    result = *value;
+    return true;
+}
+
 bool KeyedDecoderGeneric::decodeBytes(const String& key, const uint8_t*& data, size_t& size)
 {
-    auto* value = WTF::get_if<Vector<uint8_t>>(m_dictionaryStack.last()->get(key));
+    auto value = getPointerFromDictionaryStack<Vector<uint8_t>>(key);
     if (!value)
         return false;
+
     data = ""
     size = value->size();
     return true;
@@ -193,81 +217,50 @@
 
 bool KeyedDecoderGeneric::decodeBool(const String& key, bool& result)
 {
-    auto* value = WTF::get_if<bool>(m_dictionaryStack.last()->get(key));
-    if (!value)
-        return false;
-    result = *value;
-    return true;
+    return decodeSimpleValue(key, result);
 }
 
 bool KeyedDecoderGeneric::decodeUInt32(const String& key, uint32_t& result)
 {
-    auto* value = WTF::get_if<uint32_t>(m_dictionaryStack.last()->get(key));
-    if (!value)
-        return false;
-    result = *value;
-    return true;
+    return decodeSimpleValue(key, result);
 }
 
 bool KeyedDecoderGeneric::decodeUInt64(const String& key, uint64_t& result)
 {
-    auto* value = WTF::get_if<uint64_t>(m_dictionaryStack.last()->get(key));
-    if (!value)
-        return false;
-    result = *value;
-    return true;
+    return decodeSimpleValue(key, result);
 }
 
 bool KeyedDecoderGeneric::decodeInt32(const String& key, int32_t& result)
 {
-    auto* value = WTF::get_if<int32_t>(m_dictionaryStack.last()->get(key));
-    if (!value)
-        return false;
-    result = *value;
-    return true;
+    return decodeSimpleValue(key, result);
 }
 
 bool KeyedDecoderGeneric::decodeInt64(const String& key, int64_t& result)
 {
-    auto* value = WTF::get_if<int64_t>(m_dictionaryStack.last()->get(key));
-    if (!value)
-        return false;
-    result = *value;
-    return true;
+    return decodeSimpleValue(key, result);
 }
 
 bool KeyedDecoderGeneric::decodeFloat(const String& key, float& result)
 {
-    auto* value = WTF::get_if<float>(m_dictionaryStack.last()->get(key));
-    if (!value)
-        return false;
-    result = *value;
-    return true;
+    return decodeSimpleValue(key, result);
 }
 
 bool KeyedDecoderGeneric::decodeDouble(const String& key, double& result)
 {
-    auto* value = WTF::get_if<double>(m_dictionaryStack.last()->get(key));
-    if (!value)
-        return false;
-    result = *value;
-    return true;
+    return decodeSimpleValue(key, result);
 }
 
 bool KeyedDecoderGeneric::decodeString(const String& key, String& result)
 {
-    auto* value = WTF::get_if<String>(m_dictionaryStack.last()->get(key));
-    if (!value)
-        return false;
-    result = *value;
-    return true;
+    return decodeSimpleValue(key, result);
 }
 
 bool KeyedDecoderGeneric::beginObject(const String& key)
 {
-    auto* value = WTF::get_if<std::unique_ptr<Dictionary>>(m_dictionaryStack.last()->get(key));
+    auto value = getPointerFromDictionaryStack<std::unique_ptr<Dictionary>>(key);
     if (!value)
         return false;
+
     m_dictionaryStack.append(value->get());
     return true;
 }
@@ -279,9 +272,10 @@
 
 bool KeyedDecoderGeneric::beginArray(const String& key)
 {
-    auto* value = WTF::get_if<std::unique_ptr<Array>>(m_dictionaryStack.last()->get(key));
+    auto value = getPointerFromDictionaryStack<std::unique_ptr<Array>>(key);
     if (!value)
         return false;
+
     m_arrayStack.append(value->get());
     m_arrayIndexStack.append(0);
     return true;

Modified: trunk/Source/WebCore/platform/generic/KeyedDecoderGeneric.h (254677 => 254678)


--- trunk/Source/WebCore/platform/generic/KeyedDecoderGeneric.h	2020-01-16 14:47:40 UTC (rev 254677)
+++ trunk/Source/WebCore/platform/generic/KeyedDecoderGeneric.h	2020-01-16 15:01:52 UTC (rev 254678)
@@ -55,6 +55,12 @@
     void endArrayElement() override;
     void endArray() override;
 
+    template<typename T>
+    const T* getPointerFromDictionaryStack(const String& key);
+
+    template<typename T>
+    bool decodeSimpleValue(const String& key, T& result);
+
     std::unique_ptr<Dictionary> m_rootDictionary;
     Vector<Dictionary*, 16> m_dictionaryStack;
     Vector<Array*, 16> m_arrayStack;

Modified: trunk/Tools/ChangeLog (254677 => 254678)


--- trunk/Tools/ChangeLog	2020-01-16 14:47:40 UTC (rev 254677)
+++ trunk/Tools/ChangeLog	2020-01-16 15:01:52 UTC (rev 254678)
@@ -1,3 +1,22 @@
+2020-01-16  Takashi Komori  <[email protected]>
+
+        KeyedDecoderGeneric crashes when it accesses data with non-existing key
+        https://bugs.webkit.org/show_bug.cgi?id=205902
+
+        Reviewed by Fujii Hironori.
+
+        * TestWebKitAPI/CMakeLists.txt:
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WebCore/KeyedCoding.cpp: Added.
+        (TestWebKitAPI::checkDecodedBytes):
+        (TestWebKitAPI::TEST):
+        (TestWebKitAPI::testSimpleValue):
+        (TestWebKitAPI::KeyedCodingTestObject::encode):
+        (TestWebKitAPI::KeyedCodingTestObject::decode):
+        (TestWebKitAPI::KeyedCodingTestObject::KeyedCodingTestObject):
+        (TestWebKitAPI::KeyedCodingTestObject::equals const):
+        (TestWebKitAPI::operator==):
+
 2020-01-16  Carlos Garcia Campos  <[email protected]>
 
         [GTK][WPE] Password field doesn't get input method

Modified: trunk/Tools/TestWebKitAPI/CMakeLists.txt (254677 => 254678)


--- trunk/Tools/TestWebKitAPI/CMakeLists.txt	2020-01-16 14:47:40 UTC (rev 254677)
+++ trunk/Tools/TestWebKitAPI/CMakeLists.txt	2020-01-16 15:01:52 UTC (rev 254678)
@@ -138,6 +138,7 @@
         Tests/WebCore/IntPoint.cpp
         Tests/WebCore/IntRect.cpp
         Tests/WebCore/IntSize.cpp
+        Tests/WebCore/KeyedCoding.cpp
         Tests/WebCore/LayoutUnit.cpp
         Tests/WebCore/MIMETypeRegistry.cpp
         Tests/WebCore/ParsedContentRange.cpp

Modified: trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (254677 => 254678)


--- trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2020-01-16 14:47:40 UTC (rev 254677)
+++ trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2020-01-16 15:01:52 UTC (rev 254678)
@@ -120,6 +120,7 @@
 		26F52EB218288F240023D412 /* geolocationWatchPosition.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 26F52EB018288F0F0023D412 /* geolocationWatchPosition.html */; };
 		26F52EB318288F240023D412 /* geolocationWatchPositionWithHighAccuracy.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 26F52EB118288F0F0023D412 /* geolocationWatchPositionWithHighAccuracy.html */; };
 		272A691022F012DA000FDABB /* PageLoadState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 272A690F22F012C7000FDABB /* PageLoadState.cpp */; };
+		278E3E1923CD842F005A6B80 /* KeyedCoding.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 278E3E1823CD82FA005A6B80 /* KeyedCoding.cpp */; };
 		290A9BB91735F63800D71BBC /* OpenNewWindow.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 290A9BB81735F42300D71BBC /* OpenNewWindow.html */; };
 		290F4275172A221C00939FF0 /* custom-protocol-sync-xhr.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 290F4274172A1FDE00939FF0 /* custom-protocol-sync-xhr.html */; };
 		297234B7173AFAC700983601 /* CustomProtocolsInvalidScheme_Bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 297234B5173AFAC700983601 /* CustomProtocolsInvalidScheme_Bundle.cpp */; };
@@ -1629,6 +1630,7 @@
 		26F6E1EF1ADC749B00DE696B /* DFAMinimizer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DFAMinimizer.cpp; sourceTree = "<group>"; };
 		272A690F22F012C7000FDABB /* PageLoadState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PageLoadState.cpp; sourceTree = "<group>"; };
 		278DE64B22B8D611004E0E7A /* CrossThreadCopier.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CrossThreadCopier.cpp; sourceTree = "<group>"; };
+		278E3E1823CD82FA005A6B80 /* KeyedCoding.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = KeyedCoding.cpp; sourceTree = "<group>"; };
 		290A9BB51735DE8A00D71BBC /* CloseNewWindowInNavigationPolicyDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CloseNewWindowInNavigationPolicyDelegate.mm; sourceTree = "<group>"; };
 		290A9BB81735F42300D71BBC /* OpenNewWindow.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = OpenNewWindow.html; sourceTree = "<group>"; };
 		290F4274172A1FDE00939FF0 /* custom-protocol-sync-xhr.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "custom-protocol-sync-xhr.html"; sourceTree = "<group>"; };
@@ -3150,6 +3152,7 @@
 				7A909A741D877475007E10F8 /* IntRect.cpp */,
 				7A909A751D877475007E10F8 /* IntSize.cpp */,
 				CD5FF4962162E27E004BD86F /* ISOBox.cpp */,
+				278E3E1823CD82FA005A6B80 /* KeyedCoding.cpp */,
 				14464012167A8305000BD218 /* LayoutUnit.cpp */,
 				6BF4A682239ED4CD00E2F45B /* LoggedInStatus.cpp */,
 				076E507E1F45031E006E9F5A /* Logging.cpp */,
@@ -4763,6 +4766,7 @@
 				E35FC7B222B82A7300F32F98 /* JSLockTakesWebThreadLock.mm in Sources */,
 				7CCE7EC41A411A7E00447C4C /* JSWrapperForNodeInWebFrame.mm in Sources */,
 				F45E15732112CE2900307E82 /* KeyboardInputTestsIOS.mm in Sources */,
+				278E3E1923CD842F005A6B80 /* KeyedCoding.cpp in Sources */,
 				7CCE7F061A411AE600447C4C /* LayoutMilestonesWithAllContentInFrame.cpp in Sources */,
 				7CCE7EDF1A411A9200447C4C /* LayoutUnit.cpp in Sources */,
 				F4BFA68E1E4AD08000154298 /* LegacyDragAndDropTests.mm in Sources */,

Added: trunk/Tools/TestWebKitAPI/Tests/WebCore/KeyedCoding.cpp (0 => 254678)


--- trunk/Tools/TestWebKitAPI/Tests/WebCore/KeyedCoding.cpp	                        (rev 0)
+++ trunk/Tools/TestWebKitAPI/Tests/WebCore/KeyedCoding.cpp	2020-01-16 15:01:52 UTC (rev 254678)
@@ -0,0 +1,321 @@
+/*
+ * Copyright (C) 2020 Sony Interactive Entertainment Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#include <WebCore/KeyedCoding.h>
+#include <WebCore/SharedBuffer.h>
+#include <cstdint>
+#include <wtf/text/WTFString.h>
+
+namespace TestWebKitAPI {
+
+static bool checkDecodedBytes(const uint8_t* original, size_t originalSize, const uint8_t* decoded, size_t decodedSize)
+{
+    if (originalSize != decodedSize)
+        return false;
+
+    return !std::memcmp(original, decoded, originalSize);
+}
+
+TEST(KeyedCoding, SetAndGetBytes)
+{
+    const uint8_t data[] = { 0x00, 0x01, 0x02, 0x03, 0xde, 0xad, 0xbe, 0xef };
+    const size_t dataLength = WTF_ARRAY_LENGTH(data);
+    const size_t dataLengthOne = 1;
+    const size_t dataLengthZero = 0;
+
+    auto encoder = WebCore::KeyedEncoder::encoder();
+    encoder->encodeBytes("data", data, dataLength);
+    encoder->encodeBytes("data-size0", data, dataLengthZero);
+    encoder->encodeBytes("data-size1", data, dataLengthOne);
+    auto encodedBuffer = encoder->finishEncoding();
+
+    auto decoder = WebCore::KeyedDecoder::decoder(reinterpret_cast<const uint8_t*>(encodedBuffer->data()), encodedBuffer->size());
+
+    bool success;
+    const uint8_t* buffer;
+    size_t size;
+
+    success = decoder->decodeBytes("data", buffer, size);
+    EXPECT_TRUE(success);
+    EXPECT_EQ(dataLength, size);
+    EXPECT_TRUE(checkDecodedBytes(data, dataLength, buffer, size));
+
+    success = decoder->decodeBytes("data-size0", buffer, size);
+    EXPECT_TRUE(success);
+    EXPECT_EQ(dataLengthZero, size);
+
+    success = decoder->decodeBytes("data-size1", buffer, size);
+    EXPECT_TRUE(success);
+    EXPECT_EQ(dataLengthOne, size);
+    EXPECT_TRUE(checkDecodedBytes(data, dataLengthOne, buffer, size));
+}
+
+template <typename EncodeFunctionType, typename DecodeValueType, typename EncodeValueType>
+bool testSimpleValue(EncodeFunctionType encode, bool (WebCore::KeyedDecoder::* decode)(const String&, DecodeValueType&), EncodeValueType value)
+{
+    auto encoder = WebCore::KeyedEncoder::encoder();
+    (encoder.get()->*encode)("key", value);
+
+    auto encodedBuffer = encoder->finishEncoding();
+    auto decoder = WebCore::KeyedDecoder::decoder(reinterpret_cast<const uint8_t*>(encodedBuffer->data()), encodedBuffer->size());
+
+    DecodeValueType decodedValue;
+    bool success = (decoder.get()->*decode)("key", decodedValue);
+
+    if constexpr (std::is_same_v<EncodeValueType, String> && std::is_same_v<DecodeValueType, String>) {
+        if (value.isEmpty() && decodedValue.isEmpty())
+            success = true;
+        else
+            success = success && decodedValue == value;
+    } else if constexpr (std::is_same_v<EncodeValueType, DecodeValueType>)
+        success = success && decodedValue == value;
+
+    return success;
+}
+
+TEST(KeyedCoding, SetAndGetBool)
+{
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeBool, &WebCore::KeyedDecoder::decodeBool, true));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeBool, &WebCore::KeyedDecoder::decodeBool, false));
+
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeBool, &WebCore::KeyedDecoder::decodeInt32, true));
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeBool, &WebCore::KeyedDecoder::decodeUInt32, true));
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeBool, &WebCore::KeyedDecoder::decodeInt64, true));
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeBool, &WebCore::KeyedDecoder::decodeUInt64, true));
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeBool, &WebCore::KeyedDecoder::decodeFloat, true));
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeBool, &WebCore::KeyedDecoder::decodeDouble, true));
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeBool, &WebCore::KeyedDecoder::decodeString, true));
+}
+
+TEST(KeyedCoding, SetAndGetInt32)
+{
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeInt32, &WebCore::KeyedDecoder::decodeInt32, 0));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeInt32, &WebCore::KeyedDecoder::decodeInt32, 1));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeInt32, &WebCore::KeyedDecoder::decodeInt32, -1));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeInt32, &WebCore::KeyedDecoder::decodeInt32, INT_MIN));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeInt32, &WebCore::KeyedDecoder::decodeInt32, INT_MAX));
+
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeInt32, &WebCore::KeyedDecoder::decodeBool, 0));
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeInt32, &WebCore::KeyedDecoder::decodeString, 0));
+}
+
+TEST(KeyedCoding, SetAndGetUInt32)
+{
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeUInt32, &WebCore::KeyedDecoder::decodeUInt32, uint32_t(0)));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeUInt32, &WebCore::KeyedDecoder::decodeUInt32, uint32_t(1)));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeUInt32, &WebCore::KeyedDecoder::decodeUInt32, UINT32_MAX));
+
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeUInt32, &WebCore::KeyedDecoder::decodeBool, uint32_t(0)));
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeUInt32, &WebCore::KeyedDecoder::decodeString, uint32_t(0)));
+}
+
+TEST(KeyedCoding, SetAndGetInt64)
+{
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeInt64, &WebCore::KeyedDecoder::decodeInt64, int64_t(0)));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeInt64, &WebCore::KeyedDecoder::decodeInt64, int64_t(-2)));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeInt64, &WebCore::KeyedDecoder::decodeInt64, int64_t(2)));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeInt64, &WebCore::KeyedDecoder::decodeInt64, INT64_MIN));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeInt64, &WebCore::KeyedDecoder::decodeInt64, INT64_MAX));
+
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeInt64, &WebCore::KeyedDecoder::decodeBool, int64_t(0)));
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeInt64, &WebCore::KeyedDecoder::decodeString, int64_t(0)));
+}
+
+TEST(KeyedCoding, SetAndGetUInt64)
+{
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeUInt64, &WebCore::KeyedDecoder::decodeUInt64, uint64_t(0)));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeUInt64, &WebCore::KeyedDecoder::decodeUInt64, uint64_t(1)));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeUInt64, &WebCore::KeyedDecoder::decodeUInt64, UINT64_MAX));
+
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeUInt64, &WebCore::KeyedDecoder::decodeBool, uint64_t(0)));
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeUInt64, &WebCore::KeyedDecoder::decodeString, uint64_t(0)));
+}
+
+TEST(KeyedCoding, SetAndGetFloat)
+{
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeFloat, &WebCore::KeyedDecoder::decodeFloat, float(0)));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeFloat, &WebCore::KeyedDecoder::decodeFloat, float(1.5)));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeFloat, &WebCore::KeyedDecoder::decodeFloat, float(-1.5)));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeFloat, &WebCore::KeyedDecoder::decodeFloat, std::numeric_limits<float>::lowest()));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeFloat, &WebCore::KeyedDecoder::decodeFloat, std::numeric_limits<float>::min()));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeFloat, &WebCore::KeyedDecoder::decodeFloat, std::numeric_limits<float>::max()));
+
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeFloat, &WebCore::KeyedDecoder::decodeBool, float(0)));
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeFloat, &WebCore::KeyedDecoder::decodeString, float(0)));
+}
+
+TEST(KeyedCoding, SetAndGetDouble)
+{
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeDouble, &WebCore::KeyedDecoder::decodeDouble, double(0)));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeDouble, &WebCore::KeyedDecoder::decodeDouble, double(1.25)));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeDouble, &WebCore::KeyedDecoder::decodeDouble, double(-1.25)));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeDouble, &WebCore::KeyedDecoder::decodeDouble, std::numeric_limits<double>::lowest()));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeDouble, &WebCore::KeyedDecoder::decodeDouble, std::numeric_limits<double>::min()));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeDouble, &WebCore::KeyedDecoder::decodeDouble, std::numeric_limits<double>::max()));
+
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeDouble, &WebCore::KeyedDecoder::decodeBool, double(0)));
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeDouble, &WebCore::KeyedDecoder::decodeString, double(0)));
+}
+
+TEST(KeyedCoding, SetAndGetString)
+{
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeString, &WebCore::KeyedDecoder::decodeString, String()));
+    EXPECT_TRUE(testSimpleValue(&WebCore::KeyedEncoder::encodeString, &WebCore::KeyedDecoder::decodeString, String("WebKit")));
+
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeString, &WebCore::KeyedDecoder::decodeBool, String()));
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeString, &WebCore::KeyedDecoder::decodeInt32, String()));
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeString, &WebCore::KeyedDecoder::decodeUInt32, String()));
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeString, &WebCore::KeyedDecoder::decodeInt64, String()));
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeString, &WebCore::KeyedDecoder::decodeUInt64, String()));
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeString, &WebCore::KeyedDecoder::decodeFloat, String()));
+    EXPECT_FALSE(testSimpleValue(&WebCore::KeyedEncoder::encodeString, &WebCore::KeyedDecoder::decodeDouble, String()));
+}
+
+TEST(KeyedCoding, GetNonExistingRecord)
+{
+    auto encoder = WebCore::KeyedEncoder::encoder();
+    encoder->encodeBool("bool-true", true);
+
+    auto encodedBuffer = encoder->finishEncoding();
+    auto decoder = WebCore::KeyedDecoder::decoder(reinterpret_cast<const uint8_t*>(encodedBuffer->data()), encodedBuffer->size());
+
+    bool success, boolValue;
+    success = decoder->decodeBool("bool-true", boolValue);
+    EXPECT_TRUE(success);
+    EXPECT_EQ(true, boolValue);
+
+    success = decoder->decodeBool("no-exist-key", boolValue);
+    EXPECT_FALSE(success);
+
+    int32_t int32Value;
+    success = decoder->decodeInt32("no-exist-key", int32Value);
+    EXPECT_FALSE(success);
+
+    uint32_t uint32Value;
+    success = decoder->decodeUInt32("no-exist-key", uint32Value);
+    EXPECT_FALSE(success);
+
+    int64_t int64Value;
+    success = decoder->decodeInt64("no-exist-key", int64Value);
+    EXPECT_FALSE(success);
+
+    uint64_t uint64Value;
+    success = decoder->decodeUInt64("no-exist-key", uint64Value);
+    EXPECT_FALSE(success);
+
+    float floatValue;
+    success = decoder->decodeFloat("no-exist-key", floatValue);
+    EXPECT_FALSE(success);
+
+    double doubleValue;
+    success = decoder->decodeDouble("no-exist-key", doubleValue);
+    EXPECT_FALSE(success);
+
+    const uint8_t* buffer;
+    size_t size;
+    success = decoder->decodeBytes("no-exist-key", buffer, size);
+    EXPECT_FALSE(success);
+}
+
+struct KeyedCodingTestObject {
+    static void encode(WebCore::KeyedEncoder& encoder, const KeyedCodingTestObject& object)
+    {
+        encoder.encodeString("name", object.m_name);
+        encoder.encodeInt32("age", object.m_age);
+    }
+
+    static bool decode(WebCore::KeyedDecoder& decoder, KeyedCodingTestObject& object)
+    {
+        if (!decoder.decodeString("name", object.m_name))
+            return false;
+        if (!decoder.decodeInt32("age", object.m_age))
+            return false;
+        return true;
+    }
+
+    KeyedCodingTestObject() { };
+    KeyedCodingTestObject(String name, int age)
+        : m_name(name)
+        , m_age(age)
+    {
+    }
+
+    bool equals(const KeyedCodingTestObject& other) const
+    {
+        return (m_name == other.m_name) && (m_age == other.m_age);
+    }
+
+    String m_name;
+    int32_t m_age;
+};
+
+static bool operator==(const KeyedCodingTestObject& lObject, const KeyedCodingTestObject& rObject)
+{
+    return lObject.equals(rObject);
+}
+
+TEST(KeyedCoding, SetAndGetObject)
+{
+    const KeyedCodingTestObject user0("Noah", 28);
+    const KeyedCodingTestObject user1("Emma", 31);
+
+    auto encoder = WebCore::KeyedEncoder::encoder();
+    encoder->encodeObject("user0", user0, KeyedCodingTestObject::encode);
+    encoder->encodeObject("user1", user1, KeyedCodingTestObject::encode);
+    auto encodedBuffer = encoder->finishEncoding();
+
+    auto decoder = WebCore::KeyedDecoder::decoder(reinterpret_cast<const uint8_t*>(encodedBuffer->data()), encodedBuffer->size());
+    bool success;
+    KeyedCodingTestObject decodedUser0, decodedUser1;
+
+    success = decoder->decodeObject("user0", decodedUser0, KeyedCodingTestObject::decode);
+    EXPECT_TRUE(success);
+    EXPECT_EQ(decodedUser0, user0);
+
+    success = decoder->decodeObject("user1", decodedUser1, KeyedCodingTestObject::decode);
+    EXPECT_TRUE(success);
+    EXPECT_EQ(decodedUser1, user1);
+}
+
+TEST(KeyedCoding, SetAndGetObjects)
+{
+    Vector<KeyedCodingTestObject> users;
+    for (int i = 0; i < 10; i++)
+        users.append(KeyedCodingTestObject("username", i));
+
+    auto encoder = WebCore::KeyedEncoder::encoder();
+    encoder->encodeObjects("users", users.begin(), users.end(), KeyedCodingTestObject::encode);
+    auto encodedBuffer = encoder->finishEncoding();
+
+    auto decoder = WebCore::KeyedDecoder::decoder(reinterpret_cast<const uint8_t*>(encodedBuffer->data()), encodedBuffer->size());
+    Vector<KeyedCodingTestObject> decodedUsers;
+
+    bool success = decoder->decodeObjects("users", decodedUsers, KeyedCodingTestObject::decode);
+    EXPECT_TRUE(success);
+    EXPECT_EQ(users, decodedUsers);
+}
+}
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to