[Cocoa] Teach SharedBuffer to return an NSArray of data segments to avoid flattening
authoraestes@apple.com <aestes@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 5 Jan 2017 01:32:02 +0000 (01:32 +0000)
committeraestes@apple.com <aestes@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 5 Jan 2017 01:32:02 +0000 (01:32 +0000)
https://bugs.webkit.org/show_bug.cgi?id=166696

Reviewed by Tim Horton.

Source/WebCore:

Existing methods of extracting NSData from a SharedBuffer require either merging or copying
data segments. Since data segments are stored in CFDataRefs on Cocoa platforms, it should be
possible to retrieve an NSArray of the segments without having to first flatten to a single
buffer.

This patch implements such a method. This will be used in a follow-on patch, where I will
need to pass SharedBuffer data to QuickLook via an NSArray of NSData.

New API test: SharedBufferTest.createNSDataArray.

* platform/SharedBuffer.h: Declared createNSArrayData(), and exported two functions needed
by the API test.
* platform/cocoa/SharedBufferCocoa.mm:
(WebCore::SharedBuffer::createNSDataArray): Added. Returns m_cfData or a copy of m_fileData
if either exist. Otherwise, adds m_buffer (wrapped in a WebCoreSharedBufferData object) and
the objects in m_dataArray to a NSMutableArray and returns it.

Tools:

* TestWebKitAPI/PlatformGTK.cmake:
* TestWebKitAPI/PlatformWin.cmake:
* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WebCore/SharedBuffer.cpp: Moved class SharedBufferTest to SharedBufferTest.{cpp,h}.
* TestWebKitAPI/Tests/WebCore/SharedBufferTest.cpp: Moved class SharedBufferTest from SharedBuffer.cpp.
* TestWebKitAPI/Tests/WebCore/SharedBufferTest.h: Ditto.
* TestWebKitAPI/Tests/WebCore/cocoa/SharedBuffer.mm: Added.
(TestWebKitAPI::expectDataArraysEqual): Added a helper to assert that two NSArrays of NSData are equal.
(TestWebKitAPI::TEST_F): Added a test for createNSDataArray.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@210310 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/SharedBuffer.h
Source/WebCore/platform/cocoa/SharedBufferCocoa.mm
Tools/ChangeLog
Tools/TestWebKitAPI/PlatformGTK.cmake
Tools/TestWebKitAPI/PlatformWin.cmake
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/WebCore/SharedBuffer.cpp
Tools/TestWebKitAPI/Tests/WebCore/SharedBufferTest.cpp [new file with mode: 0644]
Tools/TestWebKitAPI/Tests/WebCore/SharedBufferTest.h [new file with mode: 0644]
Tools/TestWebKitAPI/Tests/WebCore/cocoa/SharedBuffer.mm [new file with mode: 0644]

index 1594c45..3c27712 100644 (file)
@@ -1,3 +1,27 @@
+2017-01-04  Andy Estes  <aestes@apple.com>
+
+        [Cocoa] Teach SharedBuffer to return an NSArray of data segments to avoid flattening
+        https://bugs.webkit.org/show_bug.cgi?id=166696
+
+        Reviewed by Tim Horton.
+
+        Existing methods of extracting NSData from a SharedBuffer require either merging or copying
+        data segments. Since data segments are stored in CFDataRefs on Cocoa platforms, it should be
+        possible to retrieve an NSArray of the segments without having to first flatten to a single
+        buffer.
+
+        This patch implements such a method. This will be used in a follow-on patch, where I will
+        need to pass SharedBuffer data to QuickLook via an NSArray of NSData.
+
+        New API test: SharedBufferTest.createNSDataArray.
+
+        * platform/SharedBuffer.h: Declared createNSArrayData(), and exported two functions needed
+        by the API test.
+        * platform/cocoa/SharedBufferCocoa.mm:
+        (WebCore::SharedBuffer::createNSDataArray): Added. Returns m_cfData or a copy of m_fileData
+        if either exist. Otherwise, adds m_buffer (wrapped in a WebCoreSharedBufferData object) and
+        the objects in m_dataArray to a NSMutableArray and returns it.
+
 2017-01-04  Ryan Haddad  <ryanhaddad@apple.com>
 
         Unreviewed, rolling out r210296.
index d92e3ab..83a4638 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006 Apple Inc.  All rights reserved.
+ * Copyright (C) 2006-2017 Apple Inc. All rights reserved.
  * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -44,6 +44,7 @@
 #endif
 
 #if USE(FOUNDATION)
+OBJC_CLASS NSArray;
 OBJC_CLASS NSData;
 #endif
 
@@ -64,6 +65,7 @@ public:
     
 #if USE(FOUNDATION)
     WEBCORE_EXPORT RetainPtr<NSData> createNSData();
+    WEBCORE_EXPORT RetainPtr<NSArray> createNSDataArray();
     WEBCORE_EXPORT static Ref<SharedBuffer> wrapNSData(NSData *);
 #endif
 #if USE(CF)
@@ -99,8 +101,8 @@ public:
     unsigned platformDataSize() const;
 
 #if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
-    static Ref<SharedBuffer> wrapCFDataArray(CFArrayRef);
-    void append(CFDataRef);
+    WEBCORE_EXPORT static Ref<SharedBuffer> wrapCFDataArray(CFArrayRef);
+    WEBCORE_EXPORT void append(CFDataRef);
 #endif
 
     WEBCORE_EXPORT Ref<SharedBuffer> copy() const;
index 0cc81ba..4a269ef 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006 Apple Inc.  All rights reserved.
+ * Copyright (C) 2006-2017 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -123,4 +123,28 @@ RefPtr<SharedBuffer> SharedBuffer::createFromReadingFile(const String& filePath)
     return nullptr;
 }
 
+RetainPtr<NSArray> SharedBuffer::createNSDataArray()
+{
+    if (auto platformData = (NSData *)m_cfData.get())
+        return @[ platformData ];
+
+    if (m_fileData)
+        return @[ [NSData dataWithBytes:m_fileData.data() length:m_fileData.size()] ];
+
+    auto dataArray = adoptNS([[NSMutableArray alloc] init]);
+    if (m_buffer->data.size())
+        [dataArray addObject:adoptNS([[WebCoreSharedBufferData alloc] initWithSharedBufferDataBuffer:m_buffer.ptr()]).get()];
+
+#if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
+    for (auto& data : m_dataArray)
+        [dataArray addObject:(NSData *)data.get()];
+#else
+    // Cocoa platforms all currently USE(NETWORK_CFDATA_ARRAY_CALLBACK), so implementing a code path for copying segments would be dead code.
+    // If this ever changes, the following static_assert will detect it.
+    static_assert(false, "FIXME: Copy the segments into an array of NSData objects.");
+#endif
+
+    return WTFMove(dataArray);
+}
+
 }
index 4aac134..2aecae1 100644 (file)
@@ -1,3 +1,20 @@
+2017-01-04  Andy Estes  <aestes@apple.com>
+
+        [Cocoa] Teach SharedBuffer to return an NSArray of data segments to avoid flattening
+        https://bugs.webkit.org/show_bug.cgi?id=166696
+
+        Reviewed by Tim Horton.
+
+        * TestWebKitAPI/PlatformGTK.cmake:
+        * TestWebKitAPI/PlatformWin.cmake:
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WebCore/SharedBuffer.cpp: Moved class SharedBufferTest to SharedBufferTest.{cpp,h}.
+        * TestWebKitAPI/Tests/WebCore/SharedBufferTest.cpp: Moved class SharedBufferTest from SharedBuffer.cpp.
+        * TestWebKitAPI/Tests/WebCore/SharedBufferTest.h: Ditto.
+        * TestWebKitAPI/Tests/WebCore/cocoa/SharedBuffer.mm: Added.
+        (TestWebKitAPI::expectDataArraysEqual): Added a helper to assert that two NSArrays of NSData are equal.
+        (TestWebKitAPI::TEST_F): Added a test for createNSDataArray.
+
 2017-01-03  Brian Burg  <bburg@apple.com>
 
         Web Inspector: teach the protocol generator about platform-specific types, events, and commands
index b1bd757..0e401d5 100644 (file)
@@ -137,6 +137,7 @@ add_executable(TestWebCore
     ${TESTWEBKITAPI_DIR}/Tests/WebCore/PublicSuffix.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WebCore/SecurityOrigin.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WebCore/SharedBuffer.cpp
+    ${TESTWEBKITAPI_DIR}/Tests/WebCore/SharedBufferTest.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WebCore/URL.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WebCore/URLParser.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WebCore/UserAgentQuirks.cpp
index 489baf1..e30c4a3 100644 (file)
@@ -58,6 +58,7 @@ set(TestWebCoreLib_SOURCES
     ${TESTWEBKITAPI_DIR}/Tests/WebCore/ParsedContentRange.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WebCore/SecurityOrigin.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WebCore/SharedBuffer.cpp
+    ${TESTWEBKITAPI_DIR}/Tests/WebCore/SharedBufferTest.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WebCore/TimeRanges.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WebCore/TransformationMatrix.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WebCore/URL.cpp
index 38eac0c..e409d18 100644 (file)
                A155022A1E05020B00A24C57 /* DuplicateCompletionHandlerCalls.mm in Sources */ = {isa = PBXBuildFile; fileRef = A15502281E05020B00A24C57 /* DuplicateCompletionHandlerCalls.mm */; };
                A155022C1E050D0300A24C57 /* duplicate-completion-handler-calls.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = A155022B1E050BC500A24C57 /* duplicate-completion-handler-calls.html */; };
                A16F66BA1C40EB4F00BD4D24 /* ContentFiltering.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = A16F66B91C40EA2000BD4D24 /* ContentFiltering.html */; };
+               A17991881E1C994E00A505ED /* SharedBuffer.mm in Sources */ = {isa = PBXBuildFile; fileRef = A17991861E1C994E00A505ED /* SharedBuffer.mm */; };
+               A179918B1E1CA24100A505ED /* SharedBufferTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A17991891E1CA24100A505ED /* SharedBufferTest.cpp */; };
                A1C4FB731BACD1CA003742D0 /* pages.pages in Copy Resources */ = {isa = PBXBuildFile; fileRef = A1C4FB721BACD1B7003742D0 /* pages.pages */; };
                A1DF74321C41B65800A2F4D0 /* AlwaysRevalidatedURLSchemes.mm in Sources */ = {isa = PBXBuildFile; fileRef = A1DF74301C41B65800A2F4D0 /* AlwaysRevalidatedURLSchemes.mm */; };
                A57A34F216AF6B2B00C2501F /* PageVisibilityStateWithWindowChanges.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = A57A34F116AF69E200C2501F /* PageVisibilityStateWithWindowChanges.html */; };
                A15502281E05020B00A24C57 /* DuplicateCompletionHandlerCalls.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = DuplicateCompletionHandlerCalls.mm; sourceTree = "<group>"; };
                A155022B1E050BC500A24C57 /* duplicate-completion-handler-calls.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "duplicate-completion-handler-calls.html"; sourceTree = "<group>"; };
                A16F66B91C40EA2000BD4D24 /* ContentFiltering.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = ContentFiltering.html; sourceTree = "<group>"; };
+               A17991861E1C994E00A505ED /* SharedBuffer.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = SharedBuffer.mm; sourceTree = "<group>"; };
+               A17991891E1CA24100A505ED /* SharedBufferTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SharedBufferTest.cpp; sourceTree = "<group>"; };
+               A179918A1E1CA24100A505ED /* SharedBufferTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SharedBufferTest.h; sourceTree = "<group>"; };
                A18AA8CC1C3FA218009B2B97 /* ContentFiltering.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ContentFiltering.h; sourceTree = "<group>"; };
                A1A4FE5D18DD3DB700B5EA8A /* Download.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = Download.mm; sourceTree = "<group>"; };
                A1C4FB6C1BACCE50003742D0 /* QuickLook.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = QuickLook.mm; sourceTree = "<group>"; };
                                CD225C071C45A69200140761 /* ParsedContentRange.cpp */,
                                CE06DF9A1E1851F200E570C9 /* SecurityOrigin.cpp */,
                                41973B5C1AF22875006C7B36 /* SharedBuffer.cpp */,
+                               A17991891E1CA24100A505ED /* SharedBufferTest.cpp */,
+                               A179918A1E1CA24100A505ED /* SharedBufferTest.h */,
                                CDC2C7141797089D00E627FB /* TimeRanges.cpp */,
                                7AD3FE8D1D75FB8D00B169A4 /* TransformationMatrix.cpp */,
                                440A1D3814A0103A008A66F2 /* URL.cpp */,
                        isa = PBXGroup;
                        children = (
                                5769C50A1D9B0001000847FB /* SerializedCryptoKeyWrap.mm */,
+                               A17991861E1C994E00A505ED /* SharedBuffer.mm */,
                                93A7EB3C18FA63A4009E7670 /* URLExtras.mm */,
                                CD89D0381C4EDB2A00040A04 /* WebCoreNSURLSession.mm */,
                        );
                                510477781D29923B009747EB /* IDBDeleteRecovery.mm in Sources */,
                                51A587861D273AA9004BA9AF /* IndexedDBDatabaseProcessKill.mm in Sources */,
                                7C83E0BE1D0A651300FEBCF3 /* IndexedDBMultiProcess.mm in Sources */,
+                               A179918B1E1CA24100A505ED /* SharedBufferTest.cpp in Sources */,
                                7C83E0BF1D0A652200FEBCF3 /* IndexedDBPersistence.mm in Sources */,
                                7CCE7EFB1A411AE600447C4C /* InjectedBundleBasic.cpp in Sources */,
                                8349D3C21DB96DDE004A9F65 /* ContextMenuDownload.mm in Sources */,
                                7CCE7ED81A411A7E00447C4C /* WillSendSubmitEvent.mm in Sources */,
                                7CCE7ED91A411A7E00447C4C /* WindowlessWebViewWithMedia.mm in Sources */,
                                1F83571B1D3FFB2300E3967B /* WKBackForwardList.mm in Sources */,
+                               A17991881E1C994E00A505ED /* SharedBuffer.mm in Sources */,
                                2EFF06D71D8AF34A0004BB30 /* WKWebViewCandidateTests.mm in Sources */,
                                7CCE7F2E1A411B1000447C4C /* WKBrowsingContextGroupTest.mm in Sources */,
                                7CCE7F2F1A411B1000447C4C /* WKBrowsingContextLoadDelegateTest.mm in Sources */,
index 775cab3..543ef23 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
  * Copyright (C) 2015 Canon Inc. All rights reserved.
  * Copyright (C) 2013 Google Inc. All rights reserved.
  *
@@ -27,6 +27,7 @@
 
 #include "config.h"
 
+#include "SharedBufferTest.h"
 #include "Test.h"
 #include <WebCore/SharedBuffer.h>
 #include <wtf/MainThread.h>
@@ -36,38 +37,6 @@ using namespace WebCore;
 
 namespace TestWebKitAPI {
 
-const char* SharedBufferTestData = "This is a test";
-
-class SharedBufferTest : public testing::Test {
-public:
-    void SetUp() override
-    {
-        WTF::initializeMainThread();
-        
-        // create temp file
-        PlatformFileHandle handle;
-        m_tempFilePath = openTemporaryFile("tempTestFile", handle);
-        writeToFile(handle, SharedBufferTestData, strlen(SharedBufferTestData));
-        closeFile(handle); 
-
-        m_tempEmptyFilePath = openTemporaryFile("tempEmptyTestFile", handle);
-        closeFile(handle); 
-    }
-
-    void TearDown() override
-    {
-        deleteFile(m_tempFilePath);
-        deleteFile(m_tempEmptyFilePath);
-    }
-
-    const String& tempFilePath() { return m_tempFilePath; }
-    const String& tempEmptyFilePath() { return m_tempEmptyFilePath; }
-
-private:
-    String m_tempFilePath;
-    String m_tempEmptyFilePath;
-};
-
 TEST_F(SharedBufferTest, createWithContentsOfMissingFile)
 {
     RefPtr<SharedBuffer> buffer = SharedBuffer::createWithContentsOfFile(String("not_existing_file"));
@@ -78,8 +47,8 @@ TEST_F(SharedBufferTest, createWithContentsOfExistingFile)
 {
     RefPtr<SharedBuffer> buffer = SharedBuffer::createWithContentsOfFile(tempFilePath());
     ASSERT_NOT_NULL(buffer);
-    EXPECT_TRUE(buffer->size() == strlen(SharedBufferTestData));
-    EXPECT_TRUE(String(SharedBufferTestData) == String(buffer->data(), buffer->size()));
+    EXPECT_TRUE(buffer->size() == strlen(SharedBufferTest::testData()));
+    EXPECT_TRUE(String(SharedBufferTest::testData()) == String(buffer->data(), buffer->size()));
 }
 
 TEST_F(SharedBufferTest, createWithContentsOfExistingEmptyFile)
@@ -113,9 +82,9 @@ TEST_F(SharedBufferTest, appendBufferCreatedWithContentsOfExistingFile)
     RefPtr<SharedBuffer> buffer = SharedBuffer::createWithContentsOfFile(tempFilePath());
     ASSERT_NOT_NULL(buffer);
     buffer->append("a", 1);
-    EXPECT_TRUE(buffer->size() == (strlen(SharedBufferTestData) + 1));
-    EXPECT_TRUE(!memcmp(buffer->data(), SharedBufferTestData, strlen(SharedBufferTestData)));
-    EXPECT_EQ('a', buffer->data()[strlen(SharedBufferTestData)]);
+    EXPECT_TRUE(buffer->size() == (strlen(SharedBufferTest::testData()) + 1));
+    EXPECT_TRUE(!memcmp(buffer->data(), SharedBufferTest::testData(), strlen(SharedBufferTest::testData())));
+    EXPECT_EQ('a', buffer->data()[strlen(SharedBufferTest::testData())]);
 }
 
 TEST_F(SharedBufferTest, createArrayBuffer)
diff --git a/Tools/TestWebKitAPI/Tests/WebCore/SharedBufferTest.cpp b/Tools/TestWebKitAPI/Tests/WebCore/SharedBufferTest.cpp
new file mode 100644 (file)
index 0000000..a297a55
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * 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 "SharedBufferTest.h"
+
+#include <WebCore/FileSystem.h>
+#include <wtf/MainThread.h>
+
+using namespace WebCore;
+
+namespace TestWebKitAPI {
+
+void SharedBufferTest::SetUp()
+{
+    WTF::initializeMainThread();
+
+    // create temp file
+    PlatformFileHandle handle;
+    m_tempFilePath = openTemporaryFile("tempTestFile", handle);
+    writeToFile(handle, testData(), strlen(testData()));
+    closeFile(handle);
+
+    m_tempEmptyFilePath = openTemporaryFile("tempEmptyTestFile", handle);
+    closeFile(handle);
+}
+
+void SharedBufferTest::TearDown()
+{
+    deleteFile(m_tempFilePath);
+    deleteFile(m_tempEmptyFilePath);
+}
+
+} // namespace TestWebKitAPI
diff --git a/Tools/TestWebKitAPI/Tests/WebCore/SharedBufferTest.h b/Tools/TestWebKitAPI/Tests/WebCore/SharedBufferTest.h
new file mode 100644 (file)
index 0000000..e5d9093
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * 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.
+ */
+
+#pragma once
+
+#include <wtf/text/WTFString.h>
+
+namespace TestWebKitAPI {
+
+class SharedBufferTest : public testing::Test {
+public:
+    void SetUp() override;
+    void TearDown() override;
+
+    static const char* testData() { return "This is a test"; }
+    const String& tempFilePath() { return m_tempFilePath; }
+    const String& tempEmptyFilePath() { return m_tempEmptyFilePath; }
+
+private:
+    String m_tempFilePath;
+    String m_tempEmptyFilePath;
+};
+
+} // namespace TestWebKitAPI
diff --git a/Tools/TestWebKitAPI/Tests/WebCore/cocoa/SharedBuffer.mm b/Tools/TestWebKitAPI/Tests/WebCore/cocoa/SharedBuffer.mm
new file mode 100644 (file)
index 0000000..ccd2876
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * 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.
+ */
+
+#import "config.h"
+
+#import "PlatformUtilities.h"
+#import "SharedBufferTest.h"
+#import <WebCore/SharedBuffer.h>
+
+using namespace WebCore;
+
+namespace TestWebKitAPI {
+
+static void expectDataArraysEqual(NSArray<NSData *> *expected, NSArray<NSData *> *actual)
+{
+    EXPECT_EQ(expected.count, actual.count);
+    for (NSUInteger i = 0; i < expected.count; ++i) {
+        NSData *expectedData = expected[i];
+        NSData *actualData = actual[i];
+        EXPECT_TRUE([expectedData isKindOfClass:[NSData class]]);
+        EXPECT_TRUE([actualData isKindOfClass:[NSData class]]);
+        EXPECT_TRUE([expectedData isEqualToData:actualData]);
+    }
+}
+
+TEST_F(SharedBufferTest, createNSDataArray)
+{
+    @autoreleasepool {
+        auto buffer = SharedBuffer::create();
+        expectDataArraysEqual(nil, buffer->createNSDataArray().get());
+
+        NSData *helloData = [NSData dataWithBytes:"hello" length:5];
+        buffer->append((const char*)helloData.bytes, helloData.length);
+        expectDataArraysEqual(@[ helloData ], buffer->createNSDataArray().get());
+
+        NSData *worldData = [NSData dataWithBytes:"world" length:5];
+        buffer->append((CFDataRef)worldData);
+        expectDataArraysEqual(@[ helloData, worldData ], buffer->createNSDataArray().get());
+
+        expectDataArraysEqual(@[ helloData ], SharedBuffer::wrapNSData(helloData)->createNSDataArray().get());
+        expectDataArraysEqual(@[ worldData ], SharedBuffer::wrapCFData((CFDataRef)worldData)->createNSDataArray().get());
+
+        NSArray<NSData *> *dataArray = @[ [NSData dataWithBytes:"one" length:3], [NSData dataWithBytes:"two" length:3], [NSData dataWithBytes:"three" length:5] ];
+        auto wrappedDataArray = SharedBuffer::wrapCFDataArray((CFArrayRef)dataArray);
+        expectDataArraysEqual(dataArray, wrappedDataArray->createNSDataArray().get());
+
+        NSArray<NSData *> *secondDataArray = @[ [NSData dataWithBytes:"four" length:4], [NSData dataWithBytes:"five" length:4], [NSData dataWithBytes:"six" length:3] ];
+        wrappedDataArray->append(SharedBuffer::wrapCFDataArray((CFArrayRef)secondDataArray).get());
+        expectDataArraysEqual([dataArray arrayByAddingObjectsFromArray:secondDataArray], wrappedDataArray->createNSDataArray().get());
+
+        expectDataArraysEqual(@[ [NSData dataWithContentsOfFile:tempFilePath()] ], SharedBuffer::createWithContentsOfFile(tempFilePath())->createNSDataArray().get());
+    }
+}
+
+}; // namespace TestWebKitAPI