07f2e7da53c6970fc7f467ad92a07869c06084a4
[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     RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
65
66     NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"LocalStorageClear" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
67     [webView loadRequest:request];
68
69     TestWebKitAPI::Util::run(&readyToContinue);
70     readyToContinue = false;
71
72     webView = nil;
73
74     NSString *dbPath = [@"~/Library/WebKit/TestWebKitAPI/WebsiteData/LocalStorage/file__0.localstorage" stringByExpandingTildeInPath];
75     NSString *dbSHMPath = [@"~/Library/WebKit/TestWebKitAPI/WebsiteData/LocalStorage/file__0.localstorage-shm" stringByExpandingTildeInPath];
76     NSString *dbWALPath = [@"~/Library/WebKit/TestWebKitAPI/WebsiteData/LocalStorage/file__0.localstorage-wal" stringByExpandingTildeInPath];
77     NSString *trackerPath = [@"~/Library/WebKit/TestWebKitAPI/WebsiteData/LocalStorage/StorageTracker.db" stringByExpandingTildeInPath];
78     NSString *trackerSHMPath = [@"~/Library/WebKit/TestWebKitAPI/WebsiteData/LocalStorage/StorageTracker.db-shm" stringByExpandingTildeInPath];
79     NSString *trackerWALPath = [@"~/Library/WebKit/TestWebKitAPI/WebsiteData/LocalStorage/StorageTracker.db-wal" stringByExpandingTildeInPath];
80
81     [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^() {
82         EXPECT_FALSE([[NSFileManager defaultManager] fileExistsAtPath:dbPath]);
83         EXPECT_FALSE([[NSFileManager defaultManager] fileExistsAtPath:dbSHMPath]);
84         EXPECT_FALSE([[NSFileManager defaultManager] fileExistsAtPath:dbWALPath]);
85         EXPECT_FALSE([[NSFileManager defaultManager] fileExistsAtPath:trackerPath]);
86         EXPECT_FALSE([[NSFileManager defaultManager] fileExistsAtPath:trackerSHMPath]);
87         EXPECT_FALSE([[NSFileManager defaultManager] fileExistsAtPath:trackerWALPath]);
88         readyToContinue = true;
89     }];
90
91     TestWebKitAPI::Util::run(&readyToContinue);
92 }
93
94 static long long fileSize(NSURL* url)
95 {
96     NSError *error = nil;
97     NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:url.path error:&error];
98     if (error)
99         return -1;
100     return [[fileAttributes objectForKey:NSFileSize] longLongValue];
101 }
102
103 NSString *swizzledBundleIdentifierWebBookmarksD()
104 {
105     return @"com.apple.webbookmarksd";
106 }
107
108 NSString *defaultApplicationCacheDirectory()
109 {
110 #if PLATFORM(IOS)
111     // This is to mirror a quirk in WebsiteDataStore::defaultApplicationCacheDirectory and catch any regressions.
112     return [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/com.apple.WebAppCache"];
113 #else
114     return @"~/Library/Caches/TestWebKitAPI/WebKit/OfflineWebApplicationCache";
115 #endif
116 }
117
118 TEST(WKWebView, ClearAppCache)
119 {
120 #if PLATFORM(IOS)
121     // On iOS, MobileSafari and webbookmarksd need to share the same AppCache directory.
122     TestWebKitAPI::InstanceMethodSwizzler swizzle([NSBundle class], @selector(bundleIdentifier), reinterpret_cast<IMP>(swizzledBundleIdentifierWebBookmarksD));
123 #endif
124     NSURL *dbResourceURL = [[NSBundle mainBundle] URLForResource:@"ApplicationCache" withExtension:@"db" subdirectory:@"TestWebKitAPI.resources"];
125     NSURL *shmResourceURL = [[NSBundle mainBundle] URLForResource:@"ApplicationCache" withExtension:@"db-shm" subdirectory:@"TestWebKitAPI.resources"];
126     NSURL *walResourceURL = [[NSBundle mainBundle] URLForResource:@"ApplicationCache" withExtension:@"db-wal" subdirectory:@"TestWebKitAPI.resources"];
127     
128     NSURL *targetURL = [NSURL fileURLWithPath:[defaultApplicationCacheDirectory() stringByExpandingTildeInPath]];
129     [[NSFileManager defaultManager] createDirectoryAtURL:targetURL withIntermediateDirectories:YES attributes:nil error:nil];
130     
131     NSURL *dbTargetURL = [targetURL URLByAppendingPathComponent:@"ApplicationCache.db"];
132     NSURL *walTargetURL = [targetURL URLByAppendingPathComponent:@"ApplicationCache.db-wal"];
133     NSURL *shmTargetURL = [targetURL URLByAppendingPathComponent:@"ApplicationCache.db-shm"];
134
135     // Clean up any files that may have been left from this test failing before.
136     [[NSFileManager defaultManager] removeItemAtURL:dbTargetURL error:nil];
137     [[NSFileManager defaultManager] removeItemAtURL:walTargetURL error:nil];
138     [[NSFileManager defaultManager] removeItemAtURL:shmTargetURL error:nil];
139     EXPECT_EQ(fileSize(dbTargetURL), -1);
140     EXPECT_EQ(fileSize(walTargetURL), -1);
141     EXPECT_EQ(fileSize(shmTargetURL), -1);
142     
143     // Copy the resources from the bundle to ~/Library/...
144     [[NSFileManager defaultManager] copyItemAtURL:dbResourceURL toURL:dbTargetURL error:nil];
145     [[NSFileManager defaultManager] copyItemAtURL:shmResourceURL toURL:shmTargetURL error:nil];
146     [[NSFileManager defaultManager] copyItemAtURL:walResourceURL toURL:walTargetURL error:nil];
147     EXPECT_GT(fileSize(dbTargetURL), 0);
148     EXPECT_GT(fileSize(shmTargetURL), 0);
149     EXPECT_GT(fileSize(walTargetURL), 0);
150
151     static size_t originalWebsiteDataRecordCount;
152
153     // Make sure there is a record in the WKWebsiteDataStore.
154     readyToContinue = false;
155     [[WKWebsiteDataStore defaultDataStore] fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] completionHandler:^(NSArray<WKWebsiteDataRecord *> *websiteDataRecords)
156     {
157         EXPECT_GT(websiteDataRecords.count, 0ul);
158         originalWebsiteDataRecordCount = websiteDataRecords.count;
159         readyToContinue = true;
160     }];
161     TestWebKitAPI::Util::run(&readyToContinue);
162
163     readyToContinue = false;
164     [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^()
165     {
166         NSURL *targetURL = [NSURL fileURLWithPath:[defaultApplicationCacheDirectory() stringByExpandingTildeInPath]];
167         NSURL *walTargetURL = [targetURL URLByAppendingPathComponent:@"ApplicationCache.db-wal"];
168         
169         // Make sure there is no record in the WKWebsiteDataStore.
170         EXPECT_EQ(fileSize(walTargetURL), 0);
171         [[WKWebsiteDataStore defaultDataStore] fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] completionHandler:^(NSArray<WKWebsiteDataRecord *> *websiteDataRecords)
172         {
173             EXPECT_EQ(websiteDataRecords.count, originalWebsiteDataRecordCount - 1);
174             readyToContinue = true;
175         }];
176     }];
177     TestWebKitAPI::Util::run(&readyToContinue);
178 }
179 #endif