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