Conversion between SecurityOriginData and DatabaseIdentifier is asymmetric when port...
authorsihui_liu@apple.com <sihui_liu@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 22 May 2018 21:13:59 +0000 (21:13 +0000)
committersihui_liu@apple.com <sihui_liu@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 22 May 2018 21:13:59 +0000 (21:13 +0000)
https://bugs.webkit.org/show_bug.cgi?id=185715

Reviewed by Geoffrey Garen.

Source/WebCore:

Fixed the issue of null port when converting between SecurityOriginData and DatabaseIdentifier.

Test: WKWebView.LocalStorageFetchDataRecords.

* page/SecurityOriginData.cpp:
(WebCore::SecurityOriginData::fromDatabaseIdentifier):

Source/WebKit:

Add getter for origins in WKWebsiteDataRecord for testing.

* UIProcess/API/Cocoa/WKWebsiteDataRecord.mm:
(-[WKWebsiteDataRecord _originsString]):
* UIProcess/API/Cocoa/WKWebsiteDataRecordPrivate.h:

Tools:

Add API test coverage.

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WebKitCocoa/LocalStorageDatabaseTracker.mm: Added.
(-[LocalStorageUIDelegate webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:]):
(TEST):

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

Source/WebCore/ChangeLog
Source/WebCore/page/SecurityOriginData.cpp
Source/WebKit/ChangeLog
Source/WebKit/UIProcess/API/Cocoa/WKWebsiteDataRecord.mm
Source/WebKit/UIProcess/API/Cocoa/WKWebsiteDataRecordPrivate.h
Tools/ChangeLog
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/WebKitCocoa/LocalStorageDatabaseTracker.mm [new file with mode: 0644]

index 6cac44d..4deb286 100644 (file)
@@ -1,3 +1,17 @@
+2018-05-22  Sihui Liu  <sihui_liu@apple.com>
+
+        Conversion between SecurityOriginData and DatabaseIdentifier is asymmetric when port is null
+        https://bugs.webkit.org/show_bug.cgi?id=185715
+
+        Reviewed by Geoffrey Garen.
+
+        Fixed the issue of null port when converting between SecurityOriginData and DatabaseIdentifier.
+
+        Test: WKWebView.LocalStorageFetchDataRecords.
+
+        * page/SecurityOriginData.cpp:
+        (WebCore::SecurityOriginData::fromDatabaseIdentifier):
+
 2018-05-22  Keith Miller  <keith_miller@apple.com>
 
         We should have a CoW storage for NewArrayBuffer arrays.
index 2aac8ee..12a99e1 100644 (file)
@@ -114,7 +114,12 @@ std::optional<SecurityOriginData> SecurityOriginData::fromDatabaseIdentifier(con
     if (port < 0 || port > std::numeric_limits<uint16_t>::max())
         return std::nullopt;
     
-    return SecurityOriginData {databaseIdentifier.substring(0, separator1), databaseIdentifier.substring(separator1 + 1, separator2 - separator1 - 1), static_cast<uint16_t>(port)};
+    auto protocol = databaseIdentifier.substring(0, separator1);
+    auto host = databaseIdentifier.substring(separator1 + 1, separator2 - separator1 - 1);
+    if (!port)
+        return SecurityOriginData { protocol, host, std::nullopt };
+
+    return SecurityOriginData { protocol, host, static_cast<uint16_t>(port) };
 }
 
 SecurityOriginData SecurityOriginData::isolatedCopy() const
index 44cedb5..26766b3 100644 (file)
@@ -1,3 +1,17 @@
+2018-05-22  Sihui Liu  <sihui_liu@apple.com>
+
+        Conversion between SecurityOriginData and DatabaseIdentifier is asymmetric when port is null
+        https://bugs.webkit.org/show_bug.cgi?id=185715
+
+        Reviewed by Geoffrey Garen.
+
+        Add getter for origins in WKWebsiteDataRecord for testing.
+
+        * UIProcess/API/Cocoa/WKWebsiteDataRecord.mm:
+        (-[WKWebsiteDataRecord _originsString]):
+        * UIProcess/API/Cocoa/WKWebsiteDataRecordPrivate.h:
+
+
 2018-05-22  Brady Eidson  <beidson@apple.com>
 
         Rename the "Web content is visible" process assertion.
index f7a6bfb..a8e40d0 100644 (file)
@@ -29,6 +29,8 @@
 #if WK_API_ENABLED
 
 #import "_WKWebsiteDataSizeInternal.h"
+#import <WebCore/SecurityOriginData.h>
+#import <wtf/HashSet.h>
 
 NSString * const WKWebsiteDataTypeFetchCache = @"WKWebsiteDataTypeFetchCache";
 NSString * const WKWebsiteDataTypeDiskCache = @"WKWebsiteDataTypeDiskCache";
@@ -146,6 +148,15 @@ static NSString *dataTypesToString(NSSet *dataTypes)
     return [[[_WKWebsiteDataSize alloc] initWithSize:*size] autorelease];
 }
 
+- (NSArray<NSString *> *)_originsStrings
+{
+    auto origins = _websiteDataRecord->websiteDataRecord().origins;
+    NSMutableArray<NSString *> *array = [[NSMutableArray alloc] initWithCapacity:origins.size()];
+    for (auto& origin : origins)
+        [array addObject:(NSString *)origin.toString()];
+    return [array autorelease];
+}
+
 @end
 
 #endif
index 536cbcd..f7b2eed 100644 (file)
@@ -46,6 +46,8 @@ WK_EXTERN NSString * const _WKWebsiteDataTypePlugInData WK_API_AVAILABLE(macosx(
 
 @property (nullable, nonatomic, readonly) _WKWebsiteDataSize *_dataSize;
 
+- (NSArray<NSString *> *)_originsStrings WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));
+
 @end
 
 NS_ASSUME_NONNULL_END
index 6a36cd3..12cd205 100644 (file)
@@ -1,5 +1,19 @@
 2018-05-22  Sihui Liu  <sihui_liu@apple.com>
 
+        Conversion between SecurityOriginData and DatabaseIdentifier is asymmetric when port is null
+        https://bugs.webkit.org/show_bug.cgi?id=185715
+
+        Reviewed by Geoffrey Garen.
+
+        Add API test coverage.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WebKitCocoa/LocalStorageDatabaseTracker.mm: Added.
+        (-[LocalStorageUIDelegate webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:]):
+        (TEST):
+
+2018-05-22  Sihui Liu  <sihui_liu@apple.com>
+
         Unreviewed, added myself as a WebKit committer.
 
         * Scripts/webkitpy/common/config/contributors.json:
index 0372673..766a5d2 100644 (file)
                C9C60E651E53A9DC006DA181 /* autoplay-check-frame.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = C9C60E631E53A9BA006DA181 /* autoplay-check-frame.html */; };
                C9C60E661E53A9DC006DA181 /* autoplay-check-in-iframe.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = C9C60E641E53A9BA006DA181 /* autoplay-check-in-iframe.html */; };
                C9E6DD351EA97D0800DD78AA /* FirstResponderSuppression.mm in Sources */ = {isa = PBXBuildFile; fileRef = C9E6DD311EA972D800DD78AA /* FirstResponderSuppression.mm */; };
+               CA38459620AE17A900990D3B /* LocalStorageDatabaseTracker.mm in Sources */ = {isa = PBXBuildFile; fileRef = CA38459520AE012E00990D3B /* LocalStorageDatabaseTracker.mm */; };
                CD0BD0A61F79924D001AB2CF /* ContextMenuImgWithVideo.mm in Sources */ = {isa = PBXBuildFile; fileRef = CD0BD0A51F799220001AB2CF /* ContextMenuImgWithVideo.mm */; };
                CD0BD0A81F79982D001AB2CF /* ContextMenuImgWithVideo.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = CD0BD0A71F7997C2001AB2CF /* ContextMenuImgWithVideo.html */; };
                CD321B041E3A85FA00EB21C8 /* video-with-muted-audio-and-webaudio.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = CD321B031E3A84B700EB21C8 /* video-with-muted-audio-and-webaudio.html */; };
                C9C60E631E53A9BA006DA181 /* autoplay-check-frame.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "autoplay-check-frame.html"; sourceTree = "<group>"; };
                C9C60E641E53A9BA006DA181 /* autoplay-check-in-iframe.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "autoplay-check-in-iframe.html"; sourceTree = "<group>"; };
                C9E6DD311EA972D800DD78AA /* FirstResponderSuppression.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = FirstResponderSuppression.mm; sourceTree = "<group>"; };
+               CA38459520AE012E00990D3B /* LocalStorageDatabaseTracker.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = LocalStorageDatabaseTracker.mm; sourceTree = "<group>"; };
                CD0BD0A51F799220001AB2CF /* ContextMenuImgWithVideo.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = ContextMenuImgWithVideo.mm; sourceTree = "<group>"; };
                CD0BD0A71F7997C2001AB2CF /* ContextMenuImgWithVideo.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = ContextMenuImgWithVideo.html; sourceTree = "<group>"; };
                CD225C071C45A69200140761 /* ParsedContentRange.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ParsedContentRange.cpp; sourceTree = "<group>"; };
                                A125478D1DB18B9400358564 /* LoadDataWithNilMIMEType.mm */,
                                57901FAC1CAF12C200ED64F9 /* LoadInvalidURLRequest.mm */,
                                51E6A8921D2F1BEC00C004B6 /* LocalStorageClear.mm */,
+                               CA38459520AE012E00990D3B /* LocalStorageDatabaseTracker.mm */,
                                46C519D81D355A7300DAA51A /* LocalStorageNullEntries.mm */,
                                8C10AF96206467770018FD90 /* LocalStoragePersistence.mm */,
                                7A6A2C6F1DCCF87B00C0D085 /* LocalStorageQuirkTest.mm */,
                                7CCE7F001A411AE600447C4C /* LoadPageOnCrash.cpp in Sources */,
                                574F55CF204D37C5002948C6 /* LocalAuthenticator.mm in Sources */,
                                51E6A8941D2F1C0A00C004B6 /* LocalStorageClear.mm in Sources */,
+                               CA38459620AE17A900990D3B /* LocalStorageDatabaseTracker.mm in Sources */,
                                46C519DA1D355AB200DAA51A /* LocalStorageNullEntries.mm in Sources */,
                                8C10AF99206467A90018FD90 /* LocalStoragePersistence.mm in Sources */,
                                7A6A2C701DCCFA8C00C0D085 /* LocalStorageQuirkTest.mm in Sources */,
diff --git a/Tools/TestWebKitAPI/Tests/WebKitCocoa/LocalStorageDatabaseTracker.mm b/Tools/TestWebKitAPI/Tests/WebKitCocoa/LocalStorageDatabaseTracker.mm
new file mode 100644 (file)
index 0000000..7b84a3d
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2018 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 "Test.h"
+#import <WebKit/WKWebsiteDataRecordPrivate.h>
+#import <WebKit/WebKit.h>
+#import <wtf/text/WTFString.h>
+
+#if WK_API_ENABLED
+
+static bool readyToContinue;
+
+@interface LocalStorageUIDelegate : NSObject <WKUIDelegate>
+@end
+
+@implementation LocalStorageUIDelegate
+- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
+{
+    EXPECT_STREQ("testValue", message.UTF8String);
+    readyToContinue = true;
+    completionHandler();
+}
+@end
+
+TEST(WKWebView, LocalStorageFetchDataRecords)
+{
+    readyToContinue = false;
+    [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^() {
+        readyToContinue = true;
+    }];
+    TestWebKitAPI::Util::run(&readyToContinue);
+
+    readyToContinue = false;
+    auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+    webView.get().UIDelegate = [[[LocalStorageUIDelegate alloc] init] autorelease];
+    [webView loadHTMLString:@"<script>localStorage.setItem('testKey', 'testValue');alert(localStorage.getItem('testKey'));</script>" baseURL:[NSURL URLWithString:@"http://localhost"]];
+    TestWebKitAPI::Util::run(&readyToContinue);
+
+    // Local storage database update interval is 1 second.
+    TestWebKitAPI::Util::sleep(1);
+
+    readyToContinue = false;
+    [[WKWebsiteDataStore defaultDataStore] fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] completionHandler:^(NSArray<WKWebsiteDataRecord *> *dataRecords) {
+        readyToContinue = true;
+        ASSERT_EQ(1u, dataRecords.count);
+        auto origins = [[dataRecords objectAtIndex:0] _originsStrings];
+        ASSERT_EQ(1u, origins.count);
+        EXPECT_STREQ("http://localhost", [origins objectAtIndex:0].UTF8String);
+    }];
+    TestWebKitAPI::Util::run(&readyToContinue);
+}
+
+#endif
+