Deprecate all WKCookieManagerRef functions
[WebKit-https.git] / Tools / WebKitTestRunner / cocoa / TestControllerCocoa.mm
1 /*
2  * Copyright (C) 2015-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 #import "TestController.h"
28
29 #import "CrashReporterInfo.h"
30 #import "PlatformWebView.h"
31 #import "StringFunctions.h"
32 #import "TestInvocation.h"
33 #import "TestRunnerWKWebView.h"
34 #import "TestWebsiteDataStoreDelegate.h"
35 #import <Foundation/Foundation.h>
36 #import <Security/SecItem.h>
37 #import <WebKit/WKContextConfigurationRef.h>
38 #import <WebKit/WKContextPrivate.h>
39 #import <WebKit/WKPreferencesRefPrivate.h>
40 #import <WebKit/WKProcessPoolPrivate.h>
41 #import <WebKit/WKStringCF.h>
42 #import <WebKit/WKUserContentControllerPrivate.h>
43 #import <WebKit/WKWebView.h>
44 #import <WebKit/WKWebViewConfiguration.h>
45 #import <WebKit/WKWebViewConfigurationPrivate.h>
46 #import <WebKit/WKWebViewPrivate.h>
47 #import <WebKit/WKWebsiteDataRecordPrivate.h>
48 #import <WebKit/WKWebsiteDataStorePrivate.h>
49 #import <WebKit/WKWebsiteDataStoreRef.h>
50 #import <WebKit/_WKApplicationManifest.h>
51 #import <WebKit/_WKUserContentExtensionStore.h>
52 #import <WebKit/_WKUserContentExtensionStorePrivate.h>
53 #import <wtf/MainThread.h>
54 #import <wtf/spi/cocoa/SecuritySPI.h>
55
56 namespace WTR {
57
58 static WKWebViewConfiguration *globalWebViewConfiguration;
59 static TestWebsiteDataStoreDelegate *globalWebsiteDataStoreDelegateClient;
60
61 void initializeWebViewConfiguration(const char* libraryPath, WKStringRef injectedBundlePath, WKContextRef context, WKContextConfigurationRef contextConfiguration)
62 {
63     [globalWebViewConfiguration release];
64     globalWebViewConfiguration = [[WKWebViewConfiguration alloc] init];
65
66     globalWebViewConfiguration.processPool = (__bridge WKProcessPool *)context;
67     globalWebViewConfiguration.websiteDataStore = (__bridge WKWebsiteDataStore *)WKContextGetWebsiteDataStore(context);
68     globalWebViewConfiguration._allowUniversalAccessFromFileURLs = YES;
69     globalWebViewConfiguration._applePayEnabled = YES;
70
71     WKContextSetStorageAccessAPIEnabled(context, true);
72
73     WKWebsiteDataStore* poolWebsiteDataStore = (__bridge WKWebsiteDataStore *)WKContextGetWebsiteDataStore((__bridge WKContextRef)globalWebViewConfiguration.processPool);
74     if (libraryPath) {
75         String cacheStorageDirectory = String(libraryPath) + '/' + "CacheStorage";
76         [poolWebsiteDataStore _setCacheStorageDirectory: cacheStorageDirectory];
77
78         String serviceWorkerRegistrationDirectory = String(libraryPath) + '/' + "ServiceWorkers";
79         [poolWebsiteDataStore _setServiceWorkerRegistrationDirectory: serviceWorkerRegistrationDirectory];
80     }
81
82     [globalWebViewConfiguration.websiteDataStore _setResourceLoadStatisticsEnabled:YES];
83     [globalWebViewConfiguration.websiteDataStore _resourceLoadStatisticsSetShouldSubmitTelemetry:NO];
84
85     [globalWebsiteDataStoreDelegateClient release];
86     globalWebsiteDataStoreDelegateClient = [[TestWebsiteDataStoreDelegate alloc] init];
87     [globalWebViewConfiguration.websiteDataStore set_delegate:globalWebsiteDataStoreDelegateClient];
88
89 #if PLATFORM(IOS_FAMILY)
90     globalWebViewConfiguration.allowsInlineMediaPlayback = YES;
91     globalWebViewConfiguration._inlineMediaPlaybackRequiresPlaysInlineAttribute = NO;
92     globalWebViewConfiguration._invisibleAutoplayNotPermitted = NO;
93     globalWebViewConfiguration._mediaDataLoadsAutomatically = YES;
94     globalWebViewConfiguration.requiresUserActionForMediaPlayback = NO;
95 #endif
96     globalWebViewConfiguration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
97
98 #if USE(SYSTEM_PREVIEW)
99     globalWebViewConfiguration._systemPreviewEnabled = YES;
100 #endif
101 }
102
103 void TestController::cocoaPlatformInitialize()
104 {
105     const char* dumpRenderTreeTemp = libraryPathForTesting();
106     if (!dumpRenderTreeTemp)
107         return;
108
109     String resourceLoadStatisticsFolder = String(dumpRenderTreeTemp) + '/' + "ResourceLoadStatistics";
110     [[NSFileManager defaultManager] createDirectoryAtPath:resourceLoadStatisticsFolder withIntermediateDirectories:YES attributes:nil error: nil];
111     String fullBrowsingSessionResourceLog = resourceLoadStatisticsFolder + '/' + "full_browsing_session_resourceLog.plist";
112     NSDictionary *resourceLogPlist = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt:1], @"version", nil];
113     if (![resourceLogPlist writeToFile:fullBrowsingSessionResourceLog atomically:YES])
114         WTFCrash();
115     [resourceLogPlist release];
116 }
117
118 WKContextRef TestController::platformContext()
119 {
120     return (__bridge WKContextRef)globalWebViewConfiguration.processPool;
121 }
122
123 WKPreferencesRef TestController::platformPreferences()
124 {
125     return (__bridge WKPreferencesRef)globalWebViewConfiguration.preferences;
126 }
127
128 void TestController::platformAddTestOptions(TestOptions& options) const
129 {
130     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EnableProcessSwapOnNavigation"])
131         options.contextOptions.enableProcessSwapOnNavigation = true;
132     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EnableProcessSwapOnWindowOpen"])
133         options.contextOptions.enableProcessSwapOnWindowOpen = true;
134 }
135
136 void TestController::platformCreateWebView(WKPageConfigurationRef, const TestOptions& options)
137 {
138     RetainPtr<WKWebViewConfiguration> copiedConfiguration = adoptNS([globalWebViewConfiguration copy]);
139
140 #if PLATFORM(IOS_FAMILY)
141     if (options.useDataDetection)
142         [copiedConfiguration setDataDetectorTypes:WKDataDetectorTypeAll];
143     if (options.ignoresViewportScaleLimits)
144         [copiedConfiguration setIgnoresViewportScaleLimits:YES];
145     if (options.useCharacterSelectionGranularity)
146         [copiedConfiguration setSelectionGranularity:WKSelectionGranularityCharacter];
147     if (options.useCharacterSelectionGranularity)
148         [copiedConfiguration setSelectionGranularity:WKSelectionGranularityCharacter];
149 #endif
150
151     if (options.enableAttachmentElement)
152         [copiedConfiguration _setAttachmentElementEnabled:YES];
153
154     if (options.enableColorFilter)
155         [copiedConfiguration _setColorFilterEnabled:YES];
156
157     if (options.enableEditableImages)
158         [copiedConfiguration _setEditableImagesEnabled:YES];
159
160     if (options.enableUndoManagerAPI)
161         [copiedConfiguration _setUndoManagerAPIEnabled:YES];
162         
163     if (options.useEphemeralSession)
164         [copiedConfiguration setWebsiteDataStore:[WKWebsiteDataStore nonPersistentDataStore]];
165
166     configureContentMode(copiedConfiguration.get(), options);
167
168     if (options.applicationManifest.length()) {
169         auto manifestPath = [NSString stringWithUTF8String:options.applicationManifest.c_str()];
170         NSString *text = [NSString stringWithContentsOfFile:manifestPath usedEncoding:nullptr error:nullptr];
171         [copiedConfiguration _setApplicationManifest:[_WKApplicationManifest applicationManifestFromJSON:text manifestURL:nil documentURL:nil]];
172     }
173
174     m_mainWebView = makeUnique<PlatformWebView>(copiedConfiguration.get(), options);
175     finishCreatingPlatformWebView(m_mainWebView.get(), options);
176
177     if (options.punchOutWhiteBackgroundsInDarkMode)
178         m_mainWebView->setDrawsBackground(false);
179
180     if (options.editable)
181         m_mainWebView->setEditable(true);
182
183     m_mainWebView->platformView().allowsLinkPreview = options.allowsLinkPreview;
184 }
185
186 PlatformWebView* TestController::platformCreateOtherPage(PlatformWebView* parentView, WKPageConfigurationRef, const TestOptions& options)
187 {
188     WKWebViewConfiguration *newConfiguration = [[globalWebViewConfiguration copy] autorelease];
189     newConfiguration._relatedWebView = static_cast<WKWebView*>(parentView->platformView());
190     PlatformWebView* view = new PlatformWebView(newConfiguration, options);
191     finishCreatingPlatformWebView(view, options);
192     return view;
193 }
194
195 // Code that needs to run after TestController::m_mainWebView is initialized goes into this function.
196 void TestController::finishCreatingPlatformWebView(PlatformWebView* view, const TestOptions& options)
197 {
198 #if PLATFORM(MAC)
199     if (options.shouldShowWebView)
200         [view->platformWindow() orderFront:nil];
201     else
202         [view->platformWindow() orderBack:nil];
203 #endif
204 }
205
206 WKContextRef TestController::platformAdjustContext(WKContextRef context, WKContextConfigurationRef contextConfiguration)
207 {
208     initializeWebViewConfiguration(libraryPathForTesting(), injectedBundlePath(), context, contextConfiguration);
209     return (__bridge WKContextRef)globalWebViewConfiguration.processPool;
210 }
211
212 void TestController::platformRunUntil(bool& done, WTF::Seconds timeout)
213 {
214     NSDate *endDate = (timeout > 0_s) ? [NSDate dateWithTimeIntervalSinceNow:timeout.seconds()] : [NSDate distantFuture];
215
216     while (!done && [endDate compare:[NSDate date]] == NSOrderedDescending)
217         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:endDate];
218 }
219
220 static NSCalendar *swizzledCalendar()
221 {
222     return [NSCalendar calendarWithIdentifier:TestController::singleton().getOverriddenCalendarIdentifier().get()];
223 }
224     
225 RetainPtr<NSString> TestController::getOverriddenCalendarIdentifier() const
226 {
227     return m_overriddenCalendarIdentifier;
228 }
229
230 void TestController::setDefaultCalendarType(NSString *identifier)
231 {
232     m_overriddenCalendarIdentifier = identifier;
233     if (!m_calendarSwizzler)
234         m_calendarSwizzler = makeUnique<ClassMethodSwizzler>([NSCalendar class], @selector(currentCalendar), reinterpret_cast<IMP>(swizzledCalendar));
235 }
236
237 void TestController::resetContentExtensions()
238 {
239     __block bool doneRemoving = false;
240     [[_WKUserContentExtensionStore defaultStore] removeContentExtensionForIdentifier:@"TestContentExtensions" completionHandler:^(NSError *error) {
241         doneRemoving = true;
242     }];
243     platformRunUntil(doneRemoving, noTimeout);
244     [[_WKUserContentExtensionStore defaultStore] _removeAllContentExtensions];
245
246     if (auto* webView = mainWebView()) {
247         TestRunnerWKWebView *platformView = webView->platformView();
248         [platformView.configuration.userContentController _removeAllUserContentFilters];
249     }
250 }
251
252 void TestController::cocoaResetStateToConsistentValues(const TestOptions& options)
253 {
254     m_calendarSwizzler = nullptr;
255     m_overriddenCalendarIdentifier = nil;
256     
257     if (auto* webView = mainWebView()) {
258         TestRunnerWKWebView *platformView = webView->platformView();
259         platformView._viewScale = 1;
260         platformView._minimumEffectiveDeviceWidth = 0;
261
262         // Toggle on before the test, and toggle off after the test.
263         if (options.shouldShowSpellCheckingDots)
264             [platformView toggleContinuousSpellChecking:nil];
265     }
266
267     [globalWebsiteDataStoreDelegateClient setAllowRaisingQuota: true];
268 }
269
270 void TestController::platformWillRunTest(const TestInvocation& testInvocation)
271 {
272     setCrashReportApplicationSpecificInformationToURL(testInvocation.url());
273 }
274
275 static NSString * const WebArchivePboardType = @"Apple Web Archive pasteboard type";
276 static NSString * const WebSubresourcesKey = @"WebSubresources";
277 static NSString * const WebSubframeArchivesKey = @"WebResourceMIMEType like 'image*'";
278
279 unsigned TestController::imageCountInGeneralPasteboard() const
280 {
281 #if PLATFORM(MAC)
282     NSData *data = [[NSPasteboard generalPasteboard] dataForType:WebArchivePboardType];
283 #elif PLATFORM(IOS_FAMILY)
284     NSData *data = [[UIPasteboard generalPasteboard] valueForPasteboardType:WebArchivePboardType];
285 #endif
286     if (!data)
287         return 0;
288     
289     NSError *error = nil;
290     id webArchive = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:&error];
291     if (error) {
292         NSLog(@"Encountered error while serializing Web Archive pasteboard data: %@", error);
293         return 0;
294     }
295     
296     NSArray *subItems = [NSArray arrayWithArray:[webArchive objectForKey:WebSubresourcesKey]];
297     NSPredicate *predicate = [NSPredicate predicateWithFormat:WebSubframeArchivesKey];
298     NSArray *imagesArray = [subItems filteredArrayUsingPredicate:predicate];
299     
300     if (!imagesArray)
301         return 0;
302     
303     return imagesArray.count;
304 }
305
306 void TestController::removeAllSessionCredentials()
307 {
308     auto types = adoptNS([[NSSet alloc] initWithObjects:_WKWebsiteDataTypeCredentials, nil]);
309     [globalWebViewConfiguration.websiteDataStore removeDataOfTypes:types.get() modifiedSince:[NSDate distantPast] completionHandler:^() {
310         m_currentInvocation->didRemoveAllSessionCredentials();
311     }];
312 }
313
314 void TestController::getAllStorageAccessEntries()
315 {
316     auto* parentView = mainWebView();
317     if (!parentView)
318         return;
319
320     [globalWebViewConfiguration.websiteDataStore _getAllStorageAccessEntriesFor:parentView->platformView() completionHandler:^(NSArray<NSString *> *nsDomains) {
321         Vector<String> domains;
322         domains.reserveInitialCapacity(nsDomains.count);
323         for (NSString *domain : nsDomains)
324             domains.uncheckedAppend(domain);
325         m_currentInvocation->didReceiveAllStorageAccessEntries(domains);
326     }];
327 }
328
329 void TestController::injectUserScript(WKStringRef script)
330 {
331     auto userScript = adoptNS([[WKUserScript alloc] initWithSource: toWTFString(script) injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO]);
332
333     [[globalWebViewConfiguration userContentController] addUserScript: userScript.get()];
334 }
335
336 void TestController::addTestKeyToKeychain(const String& privateKeyBase64, const String& attrLabel, const String& applicationTagBase64)
337 {
338     NSDictionary* options = @{
339         (id)kSecAttrKeyType: (id)kSecAttrKeyTypeECSECPrimeRandom,
340         (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPrivate,
341         (id)kSecAttrKeySizeInBits: @256
342     };
343     CFErrorRef errorRef = nullptr;
344     auto key = adoptCF(SecKeyCreateWithData(
345         (__bridge CFDataRef)adoptNS([[NSData alloc] initWithBase64EncodedString:privateKeyBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters]).get(),
346         (__bridge CFDictionaryRef)options,
347         &errorRef
348     ));
349     ASSERT(!errorRef);
350
351     NSDictionary* addQuery = @{
352         (id)kSecValueRef: (id)key.get(),
353         (id)kSecClass: (id)kSecClassKey,
354         (id)kSecAttrLabel: attrLabel,
355         (id)kSecAttrApplicationTag: adoptNS([[NSData alloc] initWithBase64EncodedString:applicationTagBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters]).get(),
356 #if HAVE(DATA_PROTECTION_KEYCHAIN)
357         (id)kSecUseDataProtectionKeychain: @YES
358 #else
359         (id)kSecAttrNoLegacy: @YES
360 #endif
361     };
362     OSStatus status = SecItemAdd((__bridge CFDictionaryRef)addQuery, NULL);
363     ASSERT_UNUSED(status, !status);
364 }
365
366 void TestController::cleanUpKeychain(const String& attrLabel)
367 {
368     NSDictionary* deleteQuery = @{
369         (id)kSecClass: (id)kSecClassKey,
370         (id)kSecAttrLabel: attrLabel,
371 #if HAVE(DATA_PROTECTION_KEYCHAIN)
372         (id)kSecUseDataProtectionKeychain: @YES
373 #else
374         (id)kSecAttrNoLegacy: @YES
375 #endif
376     };
377     SecItemDelete((__bridge CFDictionaryRef)deleteQuery);
378 }
379
380 bool TestController::keyExistsInKeychain(const String& attrLabel, const String& applicationTagBase64)
381 {
382     NSDictionary *query = @{
383         (id)kSecClass: (id)kSecClassKey,
384         (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPrivate,
385         (id)kSecAttrLabel: attrLabel,
386         (id)kSecAttrApplicationTag: adoptNS([[NSData alloc] initWithBase64EncodedString:applicationTagBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters]).get(),
387 #if HAVE(DATA_PROTECTION_KEYCHAIN)
388         (id)kSecUseDataProtectionKeychain: @YES
389 #else
390         (id)kSecAttrNoLegacy: @YES
391 #endif
392     };
393     OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, NULL);
394     if (!status)
395         return true;
396     ASSERT(status == errSecItemNotFound);
397     return false;
398 }
399
400 void TestController::setAllowStorageQuotaIncrease(bool value)
401 {
402     [globalWebsiteDataStoreDelegateClient setAllowRaisingQuota: value];
403 }
404
405 void TestController::setAllowsAnySSLCertificate(bool allows)
406 {
407     m_allowsAnySSLCertificate = allows;
408     WKContextSetAllowsAnySSLCertificateForWebSocketTesting(platformContext(), allows);
409     [globalWebsiteDataStoreDelegateClient setAllowAnySSLCertificate: allows];
410 }
411
412 bool TestController::canDoServerTrustEvaluationInNetworkProcess() const
413 {
414 #if HAVE(CFNETWORK_NSURLSESSION_STRICTRUSTEVALUATE)
415     return true;
416 #else
417     return false;
418 #endif
419 }
420
421 void TestController::installCustomMenuAction(const String& name, bool dismissesAutomatically)
422 {
423 #if PLATFORM(IOS_FAMILY)
424     auto* invocation = m_currentInvocation.get();
425     [m_mainWebView->platformView() installCustomMenuAction:name dismissesAutomatically:dismissesAutomatically callback:[invocation] {
426         if (TestController::singleton().isCurrentInvocation(invocation))
427             invocation->performCustomMenuAction();
428     }];
429 #else
430     UNUSED_PARAM(name);
431     UNUSED_PARAM(dismissesAutomatically);
432 #endif
433 }
434
435 void TestController::setAllowedMenuActions(const Vector<String>& actions)
436 {
437 #if PLATFORM(IOS_FAMILY)
438     auto actionNames = adoptNS([[NSMutableArray<NSString *> alloc] initWithCapacity:actions.size()]);
439     for (auto action : actions)
440         [actionNames addObject:action];
441     [m_mainWebView->platformView() setAllowedMenuActions:actionNames.get()];
442 #else
443     UNUSED_PARAM(actions);
444 #endif
445 }
446
447 bool TestController::isDoingMediaCapture() const
448 {
449     return m_mainWebView->platformView()._mediaCaptureState != _WKMediaCaptureStateNone;
450 }
451
452 #if PLATFORM(IOS_FAMILY)
453
454 static WKContentMode contentMode(const TestOptions& options)
455 {
456     if (options.contentMode == "desktop"_s)
457         return WKContentModeDesktop;
458
459     if (options.contentMode == "mobile"_s)
460         return WKContentModeMobile;
461
462     return WKContentModeRecommended;
463 }
464
465 #endif // PLATFORM(IOS_FAMILY)
466
467 void TestController::configureContentMode(WKWebViewConfiguration *configuration, const TestOptions& options)
468 {
469     auto webpagePreferences = adoptNS([[WKWebpagePreferences alloc] init]);
470 #if PLATFORM(IOS_FAMILY)
471     [webpagePreferences setPreferredContentMode:contentMode(options)];
472 #else
473     UNUSED_PARAM(options);
474 #endif
475     configuration.defaultWebpagePreferences = webpagePreferences.get();
476 }
477
478 } // namespace WTR