Title: [267553] trunk/Tools
Revision
267553
Author
[email protected]
Date
2020-09-24 16:59:10 -0700 (Thu, 24 Sep 2020)

Log Message

InAppBrowserPrivacy HTTPCookie tests incorrectly use the IsInAppBrowserPrivacyEnabled NSUserDefaults value
https://bugs.webkit.org/show_bug.cgi?id=216896
<rdar://problem/69456713>

Reviewed by Brady Eidson.

The IsInAppBrowserPrivacyEnabled UserDefaults value no longer enables
App-Bound Domains, so the tests should not use it in this way.
Changing the bundle identifier using
cleanUpInAppBrowserPrivacyTestSettings() and
initializeInAppBrowserPrivacyTestSettings() is enough to
enable/disable App-Bound Domains.

This patch also adds a WKHTTPCookieStoreObserver to test setting
app-bound cookies. This ensures we don't get a false positive from
the test by being allowed to set but not retrieve app-bound cookies.

* TestWebKitAPI/Tests/WebKitCocoa/InAppBrowserPrivacy.mm:
(-[InAppBrowserPrivacyCookieObserver cookiesDidChangeInCookieStore:]):
(setUpCookieTestWithWebsiteDataStore):
(TEST):
(setUpCookieTest): Deleted.
Changed name and simplified.

Modified Paths

Diff

Modified: trunk/Tools/ChangeLog (267552 => 267553)


--- trunk/Tools/ChangeLog	2020-09-24 23:56:05 UTC (rev 267552)
+++ trunk/Tools/ChangeLog	2020-09-24 23:59:10 UTC (rev 267553)
@@ -1,3 +1,29 @@
+2020-09-24  Kate Cheney  <[email protected]>
+
+        InAppBrowserPrivacy HTTPCookie tests incorrectly use the IsInAppBrowserPrivacyEnabled NSUserDefaults value
+        https://bugs.webkit.org/show_bug.cgi?id=216896
+        <rdar://problem/69456713>
+
+        Reviewed by Brady Eidson.
+
+        The IsInAppBrowserPrivacyEnabled UserDefaults value no longer enables
+        App-Bound Domains, so the tests should not use it in this way.
+        Changing the bundle identifier using
+        cleanUpInAppBrowserPrivacyTestSettings() and
+        initializeInAppBrowserPrivacyTestSettings() is enough to
+        enable/disable App-Bound Domains.
+
+        This patch also adds a WKHTTPCookieStoreObserver to test setting
+        app-bound cookies. This ensures we don't get a false positive from
+        the test by being allowed to set but not retrieve app-bound cookies.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/InAppBrowserPrivacy.mm:
+        (-[InAppBrowserPrivacyCookieObserver cookiesDidChangeInCookieStore:]):
+        (setUpCookieTestWithWebsiteDataStore):
+        (TEST):
+        (setUpCookieTest): Deleted.
+        Changed name and simplified.
+
 2020-09-24  Jonathan Bedard  <[email protected]>
 
         [results.webkit.org] Drop old archive chunks

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/InAppBrowserPrivacy.mm (267552 => 267553)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/InAppBrowserPrivacy.mm	2020-09-24 23:56:05 UTC (rev 267552)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/InAppBrowserPrivacy.mm	2020-09-24 23:59:10 UTC (rev 267553)
@@ -510,27 +510,35 @@
 
 static RetainPtr<WKHTTPCookieStore> globalCookieStore;
 static bool gotFlag = false;
+static uint64_t observerCallbacks;
 
-static void setUpCookieTest()
+@interface InAppBrowserPrivacyCookieObserver : NSObject<WKHTTPCookieStoreObserver>
+- (void)cookiesDidChangeInCookieStore:(WKHTTPCookieStore *)cookieStore;
+@end
+
+@implementation InAppBrowserPrivacyCookieObserver
+
+- (void)cookiesDidChangeInCookieStore:(WKHTTPCookieStore *)cookieStore
 {
-    globalCookieStore = [[WKWebsiteDataStore defaultDataStore] httpCookieStore];
-    NSArray<NSHTTPCookie *> *cookies = nil;
-    [globalCookieStore getAllCookies:[cookiesPtr = &cookies](NSArray<NSHTTPCookie *> *nsCookies) {
-        *cookiesPtr = [nsCookies retain];
+    ASSERT_EQ(cookieStore, globalCookieStore.get());
+    ++observerCallbacks;
+}
+
+@end
+
+static void setUpCookieTestWithWebsiteDataStore(WKWebsiteDataStore* dataStore)
+{
+    gotFlag = false;
+    // Clear out any website data.
+    [dataStore removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:[] {
         gotFlag = true;
     }];
-
     TestWebKitAPI::Util::run(&gotFlag);
 
-    for (id cookie in cookies) {
-        gotFlag = false;
-        [globalCookieStore deleteCookie:cookie completionHandler:[]() {
-            gotFlag = true;
-        }];
-        TestWebKitAPI::Util::run(&gotFlag);
-    }
+    observerCallbacks = 0;
+    globalCookieStore = dataStore.httpCookieStore;
 
-    cookies = nil;
+    NSArray<NSHTTPCookie *> *cookies = nil;
     gotFlag = false;
     [globalCookieStore getAllCookies:[cookiesPtr = &cookies](NSArray<NSHTTPCookie *> *nsCookies) {
         *cookiesPtr = [nsCookies retain];
@@ -549,7 +557,6 @@
 TEST(InAppBrowserPrivacy, SetCookieForNonAppBoundDomainFails)
 {
     initializeInAppBrowserPrivacyTestSettings();
-    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"WebKitDebugIsInAppBrowserPrivacyEnabled"];
 
     auto dataStore = [WKWebsiteDataStore defaultDataStore];
     auto webView = adoptNS([TestWKWebView new]);
@@ -556,11 +563,10 @@
     [webView loadHTMLString:@"Oh hello" baseURL:[NSURL URLWithString:@"http://webkit.org"]];
     [webView _test_waitForDidFinishNavigation];
 
-    setUpCookieTest();
-    globalCookieStore = [dataStore httpCookieStore];
+    setUpCookieTestWithWebsiteDataStore(dataStore);
+    RetainPtr<InAppBrowserPrivacyCookieObserver> observer = adoptNS([[InAppBrowserPrivacyCookieObserver alloc] init]);
+    [globalCookieStore addObserver:observer.get()];
 
-    NSArray<NSHTTPCookie *> *cookies = nil;
-
     // Non app-bound cookie.
     RetainPtr<NSHTTPCookie> nonAppBoundCookie = [NSHTTPCookie cookieWithProperties:@{
         NSHTTPCookiePath: @"/",
@@ -594,10 +600,10 @@
     TestWebKitAPI::Util::run(&gotFlag);
 
     cleanUpInAppBrowserPrivacyTestSettings();
-    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDebugIsInAppBrowserPrivacyEnabled"];
     gotFlag = false;
 
     // Check the cookie store to make sure only one cookie was set.
+    NSArray<NSHTTPCookie *> *cookies = nil;
     [globalCookieStore getAllCookies:[cookiesPtr = &cookies](NSArray<NSHTTPCookie *> *nsCookies) {
         *cookiesPtr = [nsCookies retain];
         gotFlag = true;
@@ -606,6 +612,9 @@
     TestWebKitAPI::Util::run(&gotFlag);
 
     ASSERT_EQ(cookies.count, 1u);
+    EXPECT_WK_STREQ(cookies[0].domain, @"www.webkit.org");
+    while (observerCallbacks != 1u)
+        TestWebKitAPI::Util::spinRunLoop();
 
     [cookies release];
     gotFlag = false;
@@ -614,6 +623,7 @@
     }];
 
     TestWebKitAPI::Util::run(&gotFlag);
+    [globalCookieStore removeObserver:observer.get()];
 }
 
 TEST(InAppBrowserPrivacy, GetCookieForNonAppBoundDomainFails)
@@ -620,11 +630,9 @@
 {
     // Since we can't set non-app-bound cookies with In-App Browser privacy protections on,
     // we can turn the protections off to set a cookie we will then try to get with protections enabled.
-    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDebugIsInAppBrowserPrivacyEnabled"];
+    cleanUpInAppBrowserPrivacyTestSettings();
 
-    setUpCookieTest();
-    globalCookieStore = [[WKWebsiteDataStore defaultDataStore] httpCookieStore];
-    NSArray<NSHTTPCookie *> *cookies = nil;
+    setUpCookieTestWithWebsiteDataStore([WKWebsiteDataStore defaultDataStore]);
 
     // Non app-bound cookie.
     RetainPtr<NSHTTPCookie> nonAppBoundCookie = [NSHTTPCookie cookieWithProperties:@{
@@ -642,6 +650,7 @@
         NSHTTPCookieDomain: @"www.webkit.org",
     }];
 
+    gotFlag = false;
     auto webView = adoptNS([TestWKWebView new]);
     [webView synchronouslyLoadHTMLString:@"start network process"];
 
@@ -660,6 +669,7 @@
     TestWebKitAPI::Util::run(&gotFlag);
 
     gotFlag = false;
+    NSArray<NSHTTPCookie *> *cookies = nil;
     [globalCookieStore getAllCookies:[cookiesPtr = &cookies](NSArray<NSHTTPCookie *> *nsCookies) {
         *cookiesPtr = [nsCookies retain];
         gotFlag = true;
@@ -671,9 +681,9 @@
     ASSERT_EQ(cookies.count, 2u);
 
     // Now enable protections and ensure we can only retrieve the app-bound cookies.
-    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"WebKitDebugIsInAppBrowserPrivacyEnabled"];
     initializeInAppBrowserPrivacyTestSettings();
 
+    cookies = nil;
     gotFlag = false;
     [globalCookieStore getAllCookies:[cookiesPtr = &cookies](NSArray<NSHTTPCookie *> *nsCookies) {
         *cookiesPtr = [nsCookies retain];
@@ -695,8 +705,6 @@
 
     gotFlag = false;
     [globalCookieStore deleteCookie:appBoundCookie.get() completionHandler:[]() {
-        // Reset flag.
-        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDebugIsInAppBrowserPrivacyEnabled"];
         cleanUpInAppBrowserPrivacyTestSettings();
         gotFlag = true;
     }];
@@ -708,8 +716,8 @@
 {
     // Since we can't set non-app-bound cookies with In-App Browser privacy protections on,
     // we can turn the protections off to set a cookie we will then try to get with protections enabled.
-    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDebugIsInAppBrowserPrivacyEnabled"];
-    setUpCookieTest();
+    cleanUpInAppBrowserPrivacyTestSettings();
+    setUpCookieTestWithWebsiteDataStore([WKWebsiteDataStore defaultDataStore]);
 
     globalCookieStore = [[WKWebsiteDataStore defaultDataStore] httpCookieStore];
     NSHTTPCookie *nonAppBoundCookie = [NSHTTPCookie cookieWithProperties:@{
@@ -733,7 +741,6 @@
         [globalCookieStore setCookie:appBoundCookie completionHandler:^{
 
             // Now enable protections and ensure we can only retrieve the app-bound cookies.
-            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"WebKitDebugIsInAppBrowserPrivacyEnabled"];
             initializeInAppBrowserPrivacyTestSettings();
 
             [globalCookieStore _getCookiesForURL:[NSURL URLWithString:@"https://webkit.org/"] completionHandler:^(NSArray<NSHTTPCookie *> *cookies) {
@@ -743,7 +750,6 @@
                     EXPECT_EQ(cookies.count, 0u);
                     [globalCookieStore deleteCookie:nonAppBoundCookie completionHandler:^{
                         [globalCookieStore deleteCookie:appBoundCookie completionHandler:^{
-                            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDebugIsInAppBrowserPrivacyEnabled"];
                             cleanUpInAppBrowserPrivacyTestSettings();
                             done = true;
                         }];
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to