REGRESSION: API test failure: WKWebView.LocalStorageClear
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebKit2Cocoa / LocalStorageClear.mm
1 /*
2  * Copyright (C) 2016 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27
28 #import "InstanceMethodSwizzler.h"
29 #import "PlatformUtilities.h"
30 #import "Test.h"
31 #import <WebKit/WKProcessPoolPrivate.h>
32 #import <WebKit/WKUserContentControllerPrivate.h>
33 #import <WebKit/WKWebsiteDataStore.h>
34 #import <WebKit/WKWebViewConfigurationPrivate.h>
35 #import <WebKit/WebKit.h>
36 #import <WebKit/_WKProcessPoolConfiguration.h>
37 #import <wtf/RetainPtr.h>
38
39 #if WK_API_ENABLED
40
41 static bool readyToContinue;
42
43 @interface LocalStorageClearMessageHandler : NSObject <WKScriptMessageHandler>
44 @end
45
46 @implementation LocalStorageClearMessageHandler
47
48 - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
49 {
50     readyToContinue = true;
51 }
52
53 @end
54
55 TEST(WKWebView, LocalStorageClear)
56 {
57     RetainPtr<LocalStorageClearMessageHandler> handler = adoptNS([[LocalStorageClearMessageHandler alloc] init]);
58     RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
59     [[configuration userContentController] addScriptMessageHandler:handler.get() name:@"testHandler"];
60
61     [configuration _setNeedsStorageAccessFromFileURLsQuirk:NO];
62     [configuration _setAllowUniversalAccessFromFileURLs:YES];
63
64     @autoreleasepool {
65         RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
66
67         NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"LocalStorageClear" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
68         [webView loadRequest:request];
69
70         TestWebKitAPI::Util::run(&readyToContinue);
71         readyToContinue = false;
72
73         webView = nil;
74     }
75
76     NSString *dbPath = [@"~/Library/WebKit/TestWebKitAPI/WebsiteData/LocalStorage/file__0.localstorage" stringByExpandingTildeInPath];
77     NSString *dbSHMPath = [@"~/Library/WebKit/TestWebKitAPI/WebsiteData/LocalStorage/file__0.localstorage-shm" stringByExpandingTildeInPath];
78     NSString *dbWALPath = [@"~/Library/WebKit/TestWebKitAPI/WebsiteData/LocalStorage/file__0.localstorage-wal" stringByExpandingTildeInPath];
79     NSString *trackerPath = [@"~/Library/WebKit/TestWebKitAPI/WebsiteData/LocalStorage/StorageTracker.db" stringByExpandingTildeInPath];
80     NSString *trackerSHMPath = [@"~/Library/WebKit/TestWebKitAPI/WebsiteData/LocalStorage/StorageTracker.db-shm" stringByExpandingTildeInPath];
81     NSString *trackerWALPath = [@"~/Library/WebKit/TestWebKitAPI/WebsiteData/LocalStorage/StorageTracker.db-wal" stringByExpandingTildeInPath];
82
83     [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^() {
84         EXPECT_FALSE([[NSFileManager defaultManager] fileExistsAtPath:dbPath]);
85         EXPECT_FALSE([[NSFileManager defaultManager] fileExistsAtPath:dbSHMPath]);
86         EXPECT_FALSE([[NSFileManager defaultManager] fileExistsAtPath:dbWALPath]);
87         EXPECT_FALSE([[NSFileManager defaultManager] fileExistsAtPath:trackerPath]);
88         EXPECT_FALSE([[NSFileManager defaultManager] fileExistsAtPath:trackerSHMPath]);
89         EXPECT_FALSE([[NSFileManager defaultManager] fileExistsAtPath:trackerWALPath]);
90         readyToContinue = true;
91     }];
92
93     TestWebKitAPI::Util::run(&readyToContinue);
94 }
95
96 static long long fileSize(NSURL* url)
97 {
98     NSError *error = nil;
99     NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:url.path error:&error];
100     if (error)
101         return -1;
102     return [[fileAttributes objectForKey:NSFileSize] longLongValue];
103 }
104
105 NSString *swizzledBundleIdentifierWebBookmarksD()
106 {
107     return @"com.apple.webbookmarksd";
108 }
109
110 NSString *defaultApplicationCacheDirectory()
111 {
112 #if PLATFORM(IOS)
113     // This is to mirror a quirk in WebsiteDataStore::defaultApplicationCacheDirectory and catch any regressions.
114     return [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/com.apple.WebAppCache"];
115 #else
116     return @"~/Library/Caches/TestWebKitAPI/WebKit/OfflineWebApplicationCache";
117 #endif
118 }
119
120 TEST(WKWebView, ClearAppCache)
121 {
122 #if PLATFORM(IOS)
123     // On iOS, MobileSafari and webbookmarksd need to share the same AppCache directory.
124     TestWebKitAPI::InstanceMethodSwizzler swizzle([NSBundle class], @selector(bundleIdentifier), reinterpret_cast<IMP>(swizzledBundleIdentifierWebBookmarksD));
125 #endif
126     NSURL *dbResourceURL = [[NSBundle mainBundle] URLForResource:@"ApplicationCache" withExtension:@"db" subdirectory:@"TestWebKitAPI.resources"];
127     NSURL *shmResourceURL = [[NSBundle mainBundle] URLForResource:@"ApplicationCache" withExtension:@"db-shm" subdirectory:@"TestWebKitAPI.resources"];
128     NSURL *walResourceURL = [[NSBundle mainBundle] URLForResource:@"ApplicationCache" withExtension:@"db-wal" subdirectory:@"TestWebKitAPI.resources"];
129     
130     NSURL *targetURL = [NSURL fileURLWithPath:[defaultApplicationCacheDirectory() stringByExpandingTildeInPath]];
131     [[NSFileManager defaultManager] createDirectoryAtURL:targetURL withIntermediateDirectories:YES attributes:nil error:nil];
132     
133     NSURL *dbTargetURL = [targetURL URLByAppendingPathComponent:@"ApplicationCache.db"];
134     NSURL *walTargetURL = [targetURL URLByAppendingPathComponent:@"ApplicationCache.db-wal"];
135     NSURL *shmTargetURL = [targetURL URLByAppendingPathComponent:@"ApplicationCache.db-shm"];
136
137     // Clean up any files that may have been left from this test failing before.
138     [[NSFileManager defaultManager] removeItemAtURL:dbTargetURL error:nil];
139     [[NSFileManager defaultManager] removeItemAtURL:walTargetURL error:nil];
140     [[NSFileManager defaultManager] removeItemAtURL:shmTargetURL error:nil];
141     EXPECT_EQ(fileSize(dbTargetURL), -1);
142     EXPECT_EQ(fileSize(walTargetURL), -1);
143     EXPECT_EQ(fileSize(shmTargetURL), -1);
144     
145     // Copy the resources from the bundle to ~/Library/...
146     [[NSFileManager defaultManager] copyItemAtURL:dbResourceURL toURL:dbTargetURL error:nil];
147     [[NSFileManager defaultManager] copyItemAtURL:shmResourceURL toURL:shmTargetURL error:nil];
148     [[NSFileManager defaultManager] copyItemAtURL:walResourceURL toURL:walTargetURL error:nil];
149     EXPECT_GT(fileSize(dbTargetURL), 0);
150     EXPECT_GT(fileSize(shmTargetURL), 0);
151     EXPECT_GT(fileSize(walTargetURL), 0);
152
153     static size_t originalWebsiteDataRecordCount;
154
155     // Make sure there is a record in the WKWebsiteDataStore.
156     readyToContinue = false;
157     [[WKWebsiteDataStore defaultDataStore] fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] completionHandler:^(NSArray<WKWebsiteDataRecord *> *websiteDataRecords)
158     {
159         EXPECT_GT(websiteDataRecords.count, 0ul);
160         originalWebsiteDataRecordCount = websiteDataRecords.count;
161         readyToContinue = true;
162     }];
163     TestWebKitAPI::Util::run(&readyToContinue);
164
165     readyToContinue = false;
166     [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^()
167     {
168         NSURL *targetURL = [NSURL fileURLWithPath:[defaultApplicationCacheDirectory() stringByExpandingTildeInPath]];
169         NSURL *walTargetURL = [targetURL URLByAppendingPathComponent:@"ApplicationCache.db-wal"];
170         
171         // Make sure there is no record in the WKWebsiteDataStore.
172         EXPECT_EQ(fileSize(walTargetURL), 0);
173         [[WKWebsiteDataStore defaultDataStore] fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] completionHandler:^(NSArray<WKWebsiteDataRecord *> *websiteDataRecords)
174         {
175             EXPECT_EQ(websiteDataRecords.count, originalWebsiteDataRecordCount - 1);
176             readyToContinue = true;
177         }];
178     }];
179     TestWebKitAPI::Util::run(&readyToContinue);
180 }
181 #endif