/* * Copyright (C) 2019 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #import "config.h" #import "Test.h" #if HAVE(APP_SSO) #import "ClassMethodSwizzler.h" #import "InstanceMethodSwizzler.h" #import "PlatformUtilities.h" #import "TCPServer.h" #import "TestWKWebView.h" #import #import #import #import #import #import #import #import #import #import #import #import static bool navigationCompleted = false; static bool authorizationPerformed = false; static bool authorizationCancelled = false; static bool uiShowed = false; static bool newWindowCreated = false; static bool haveHttpBody = false; static bool navigationPolicyDecided = false; static String finalURL; static SOAuthorization* gAuthorization; static id gDelegate; #if PLATFORM(MAC) static BlockPtr gNotificationCallback; #endif static RetainPtr gNewWindow; static const char* openerTemplate = "" "" "" ""; static const char* newWindowResponseTemplate = "" "" ""; static const char* parentTemplate = "" "" "" "" ""; static const char* iframeTemplate = "" "" ""; static const char* samlResponse = "" "" ""; @interface TestSOAuthorizationBasicDelegate : NSObject @end @implementation TestSOAuthorizationBasicDelegate - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { EXPECT_FALSE(navigationCompleted); navigationCompleted = true; finalURL = navigation._request.URL.absoluteString; } @end @interface TestSOAuthorizationDelegate : NSObject @property bool isDefaultPolicy; @property bool shouldOpenExternalSchemes; @property bool allowSOAuthorizationLoad; @property bool isAsyncExecution; - (instancetype)init; @end @implementation TestSOAuthorizationDelegate - (instancetype)init { if (self = [super init]) { self.isDefaultPolicy = true; self.shouldOpenExternalSchemes = false; self.allowSOAuthorizationLoad = true; self.isAsyncExecution = false; } return self; } - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { EXPECT_FALSE(navigationCompleted); navigationCompleted = true; finalURL = navigation._request.URL.absoluteString; } - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { navigationPolicyDecided = true; EXPECT_EQ(navigationAction._shouldOpenExternalSchemes, self.shouldOpenExternalSchemes); if (self.isDefaultPolicy) { decisionHandler(WKNavigationActionPolicyAllow); return; } decisionHandler(_WKNavigationActionPolicyAllowWithoutTryingAppLink); } - (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures { EXPECT_FALSE(newWindowCreated); newWindowCreated = true; gNewWindow = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration]; [gNewWindow setNavigationDelegate:self]; return gNewWindow.get(); } - (void)_webView:(WKWebView *)webView decidePolicyForSOAuthorizationLoadWithCurrentPolicy:(_WKSOAuthorizationLoadPolicy)policy forExtension:(NSString *)extension completionHandler:(void (^)(_WKSOAuthorizationLoadPolicy policy))completionHandler { EXPECT_EQ(policy, _WKSOAuthorizationLoadPolicyAllow); EXPECT_TRUE([extension isEqual:@""]); if (!self.isAsyncExecution) { if (self.allowSOAuthorizationLoad) completionHandler(policy); else completionHandler(_WKSOAuthorizationLoadPolicyIgnore); return; } auto allowSOAuthorizationLoad = self.allowSOAuthorizationLoad; dispatch_async(dispatch_get_main_queue(), ^() { if (allowSOAuthorizationLoad) completionHandler(_WKSOAuthorizationLoadPolicyAllow); else completionHandler(_WKSOAuthorizationLoadPolicyIgnore); }); } #if PLATFORM(IOS) - (UIViewController *)_presentingViewControllerForWebView:(WKWebView *)webView { return nil; } #endif @end #if PLATFORM(MAC) @interface TestSOAuthorizationViewController : NSViewController #elif PLATFORM(IOS) @interface TestSOAuthorizationViewController : UIViewController #endif @end @implementation TestSOAuthorizationViewController - (void)viewDidAppear { EXPECT_FALSE(uiShowed); uiShowed = true; } - (void)viewDidDisappear { EXPECT_TRUE(uiShowed); uiShowed = false; } @end static bool overrideCanPerformAuthorizationWithURL(id, SEL, NSURL *url, NSInteger) { if (![url.lastPathComponent isEqual:@"simple.html"] && ![url.host isEqual:@"www.example.com"] && ![url.lastPathComponent isEqual:@"GetSessionCookie.html"]) return false; return true; } static void overrideSetDelegate(id self, SEL, id delegate) { gAuthorization = self; gDelegate = delegate; } static void overrideBeginAuthorizationWithURL(id, SEL, NSURL *url, NSDictionary *headers, NSData *body) { EXPECT_TRUE(headers != nil); EXPECT_TRUE((body == nil) ^ haveHttpBody); EXPECT_FALSE(authorizationPerformed); authorizationPerformed = true; } static void overrideCancelAuthorization(id, SEL) { EXPECT_FALSE(authorizationCancelled); authorizationCancelled = true; } #if PLATFORM(MAC) using NotificationCallback = void (^)(NSNotification *note); static id overrideAddObserverForName(id, SEL, NSNotificationName name, id, NSOperationQueue *, NotificationCallback block) { if ([name isEqual:NSWindowWillCloseNotification]) gNotificationCallback = makeBlockPtr(block); return @YES; } #endif static bool overrideIsURLFromAppleOwnedDomain(id, SEL, NSURL *) { return true; } static void resetState() { navigationCompleted = false; authorizationPerformed = false; authorizationCancelled = false; uiShowed = false; newWindowCreated = false; haveHttpBody = false; navigationPolicyDecided = false; finalURL = emptyString(); gAuthorization = nullptr; gDelegate = nullptr; #if PLATFORM(MAC) gNotificationCallback = nullptr; #endif gNewWindow = nullptr; } enum class OpenExternalSchemesPolicy : bool { Allow, Disallow }; static void configureSOAuthorizationWebView(TestWKWebView *webView, TestSOAuthorizationDelegate *delegate, OpenExternalSchemesPolicy policy = OpenExternalSchemesPolicy::Disallow) { [webView setNavigationDelegate:delegate]; [webView setUIDelegate:delegate]; delegate.shouldOpenExternalSchemes = policy == OpenExternalSchemesPolicy::Allow; } static String generateHtml(const char* templateHtml, const String& substitute, const String& optionalSubstitute1 = emptyString(), const String& optionalSubstitute2 = emptyString()) { StringPrintStream stream; stream.printf(templateHtml, substitute.utf8().data(), optionalSubstitute1.utf8().data(), optionalSubstitute2.utf8().data()); return stream.toString(); } // FIXME: Replace the below with AppSSO constants. static void checkAuthorizationOptions(bool userActionInitiated, String initiatorOrigin, int initiatingAction) { EXPECT_TRUE(gAuthorization); // FIXME: Remove the selector once the iOS bots has been updated to a recent SDK. auto selector = NSSelectorFromString(@"authorizationOptions"); if ([gAuthorization respondsToSelector:selector]) { EXPECT_EQ(((NSNumber *)[gAuthorization performSelector:selector][SOAuthorizationOptionUserActionInitiated]).boolValue, userActionInitiated); EXPECT_WK_STREQ([gAuthorization performSelector:selector][@"initiatorOrigin"], initiatorOrigin); EXPECT_EQ(((NSNumber *)[gAuthorization performSelector:selector][@"initiatingAction"]).intValue, initiatingAction); } } namespace TestWebKitAPI { TEST(SOAuthorizationRedirect, NoInterceptions) { resetState(); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&navigationCompleted); EXPECT_WK_STREQ(testURL.get().absoluteString, finalURL); } TEST(SOAuthorizationRedirect, InterceptionError) { resetState(); ClassMethodSwizzler swizzler(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&navigationCompleted); EXPECT_WK_STREQ(testURL.get().absoluteString, finalURL); } TEST(SOAuthorizationRedirect, InterceptionDoNotHandle) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); checkAuthorizationOptions(false, "", 0); [gDelegate authorizationDidNotHandle:gAuthorization]; Util::run(&navigationCompleted); EXPECT_WK_STREQ(testURL.get().absoluteString, finalURL); } TEST(SOAuthorizationRedirect, InterceptionCancel) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); checkAuthorizationOptions(false, "", 0); [gDelegate authorizationDidCancel:gAuthorization]; // FIXME: Find a delegate method that can detect load cancels. Util::sleep(0.5); EXPECT_WK_STREQ("", webView.get()._committedURL.absoluteString); } TEST(SOAuthorizationRedirect, InterceptionCompleteWithoutData) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); checkAuthorizationOptions(false, "", 0); [gDelegate authorizationDidComplete:gAuthorization]; Util::run(&navigationCompleted); EXPECT_WK_STREQ(testURL.get().absoluteString, finalURL); } TEST(SOAuthorizationRedirect, InterceptionUnexpectedCompletion) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); checkAuthorizationOptions(false, "", 0); [gDelegate authorization:gAuthorization didCompleteWithHTTPAuthorizationHeaders:adoptNS([[NSDictionary alloc] init]).get()]; Util::run(&navigationCompleted); EXPECT_WK_STREQ(testURL.get().absoluteString, finalURL); } // { Default delegate method, No App Links } TEST(SOAuthorizationRedirect, InterceptionSucceed1) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationBasicDelegate alloc] init]); [webView setNavigationDelegate:delegate.get()]; [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); checkAuthorizationOptions(false, "", 0); #if PLATFORM(MAC) EXPECT_TRUE(gAuthorization.enableEmbeddedAuthorizationViewController); #elif PLATFORM(IOS) EXPECT_FALSE(gAuthorization.enableEmbeddedAuthorizationViewController); #endif RetainPtr redirectURL = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto response = adoptNS([[NSHTTPURLResponse alloc] initWithURL:testURL.get() statusCode:302 HTTPVersion:@"HTTP/1.1" headerFields:@{ @"Location" : [redirectURL absoluteString] }]); [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] init]).get()]; Util::run(&navigationCompleted); #if PLATFORM(IOS) navigationCompleted = false; Util::run(&navigationCompleted); #endif EXPECT_WK_STREQ(redirectURL.get().absoluteString, finalURL); } // { Default delegate method, With App Links } TEST(SOAuthorizationRedirect, InterceptionSucceed2) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationBasicDelegate alloc] init]); [webView setNavigationDelegate:delegate.get()]; // Force App Links with a request.URL that has a different host than the current one (empty host) and ShouldOpenExternalURLsPolicy::ShouldAllow. auto testURL = URL(URL(), "https://www.example.com"); #if PLATFORM(MAC) [webView _loadRequest:[NSURLRequest requestWithURL:testURL] shouldOpenExternalURLs:YES]; #elif PLATFORM(IOS) // Here we force RedirectSOAuthorizationSession to go to the with user gestures route. [webView evaluateJavaScript: [NSString stringWithFormat:@"location = '%@'", (id)testURL.string()] completionHandler:nil]; #endif Util::run(&authorizationPerformed); #if PLATFORM(MAC) checkAuthorizationOptions(false, "", 0); #elif PLATFORM(IOS) checkAuthorizationOptions(true, "null", 0); #endif RetainPtr redirectURL = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto response = adoptNS([[NSHTTPURLResponse alloc] initWithURL:testURL statusCode:302 HTTPVersion:@"HTTP/1.1" headerFields:@{ @"Location" : [redirectURL absoluteString] }]); [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] init]).get()]; Util::run(&navigationCompleted); EXPECT_WK_STREQ(redirectURL.get().absoluteString, finalURL); } // { Custom delegate method, With App Links } TEST(SOAuthorizationRedirect, InterceptionSucceed3) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); EXPECT_TRUE(gAuthorization.enableEmbeddedAuthorizationViewController); // Force App Links with a request.URL that has a different host than the current one (empty host) and ShouldOpenExternalURLsPolicy::ShouldAllow. auto testURL = URL(URL(), "https://www.example.com"); #if PLATFORM(MAC) [webView _loadRequest:[NSURLRequest requestWithURL:testURL] shouldOpenExternalURLs:YES]; #elif PLATFORM(IOS) // Here we force RedirectSOAuthorizationSession to go to the with user gestures route. [webView evaluateJavaScript: [NSString stringWithFormat:@"location = '%@'", (id)testURL.string()] completionHandler:nil]; #endif Util::run(&authorizationPerformed); #if PLATFORM(MAC) checkAuthorizationOptions(false, "", 0); #elif PLATFORM(IOS) checkAuthorizationOptions(true, "null", 0); #endif RetainPtr redirectURL = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto response = adoptNS([[NSHTTPURLResponse alloc] initWithURL:testURL statusCode:302 HTTPVersion:@"HTTP/1.1" headerFields:@{ @"Location" : [redirectURL absoluteString] }]); [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] init]).get()]; Util::run(&navigationCompleted); EXPECT_WK_STREQ(redirectURL.get().absoluteString, finalURL); } // { _WKNavigationActionPolicyAllowWithoutTryingAppLink, No App Links } TEST(SOAuthorizationRedirect, InterceptionSucceed4) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); // A separate delegate that implements decidePolicyForNavigationAction. auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [delegate setIsDefaultPolicy:false]; [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); checkAuthorizationOptions(false, "", 0); RetainPtr redirectURL = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto response = adoptNS([[NSHTTPURLResponse alloc] initWithURL:testURL.get() statusCode:302 HTTPVersion:@"HTTP/1.1" headerFields:@{ @"Location" : [redirectURL absoluteString] }]); [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] init]).get()]; Util::run(&navigationCompleted); #if PLATFORM(IOS) navigationCompleted = false; Util::run(&navigationCompleted); #endif EXPECT_WK_STREQ(redirectURL.get().absoluteString, finalURL); } TEST(SOAuthorizationRedirect, InterceptionSucceedWithOtherHttpStatusCode) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); checkAuthorizationOptions(false, "", 0); RetainPtr redirectURL = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto response = adoptNS([[NSHTTPURLResponse alloc] initWithURL:testURL.get() statusCode:400 HTTPVersion:@"HTTP/1.1" headerFields:nil]); [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] init]).get()]; Util::run(&navigationCompleted); EXPECT_WK_STREQ(testURL.get().absoluteString, finalURL); } TEST(SOAuthorizationRedirect, InterceptionSucceedWithCookie) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); checkAuthorizationOptions(false, "", 0); RetainPtr redirectURL = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto response = adoptNS([[NSHTTPURLResponse alloc] initWithURL:testURL.get() statusCode:302 HTTPVersion:@"HTTP/1.1" headerFields:@{ @"Set-Cookie" : @"sessionid=38afes7a8;", @"Location" : [redirectURL absoluteString] }]); [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] init]).get()]; Util::run(&navigationCompleted); #if PLATFORM(IOS) navigationCompleted = false; Util::run(&navigationCompleted); #endif EXPECT_WK_STREQ(redirectURL.get().absoluteString, finalURL); navigationCompleted = false; [webView evaluateJavaScript: @"document.cookie" completionHandler:^(NSString *result, NSError *) { EXPECT_WK_STREQ("sessionid=38afes7a8", [result UTF8String]); navigationCompleted = true; }]; Util::run(&navigationCompleted); } TEST(SOAuthorizationRedirect, InterceptionSucceedWithCookies) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); checkAuthorizationOptions(false, "", 0); RetainPtr redirectURL = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto response = adoptNS([[NSHTTPURLResponse alloc] initWithURL:testURL.get() statusCode:302 HTTPVersion:@"HTTP/1.1" headerFields:@{ @"Set-Cookie" : @"sessionid=38afes7a8, qwerty=219ffwef9w0f, id=a3fWa;", @"Location" : [redirectURL absoluteString] }]); [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] init]).get()]; Util::run(&navigationCompleted); #if PLATFORM(IOS) navigationCompleted = false; Util::run(&navigationCompleted); #endif EXPECT_WK_STREQ(redirectURL.get().absoluteString, finalURL); navigationCompleted = false; [webView evaluateJavaScript: @"document.cookie" completionHandler:^(NSString *result, NSError *) { EXPECT_WK_STREQ("id=a3fWa; qwerty=219ffwef9w0f; sessionid=38afes7a8", [result UTF8String]); navigationCompleted = true; }]; Util::run(&navigationCompleted); } // The redirected URL has a different domain than the test URL. TEST(SOAuthorizationRedirect, InterceptionSucceedWithRedirectionAndCookie) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); auto testURL = URL(URL(), "https://www.example.com"); #if PLATFORM(MAC) [webView loadRequest:[NSURLRequest requestWithURL:testURL]]; #elif PLATFORM(IOS) // Here we force RedirectSOAuthorizationSession to go to the with user gestures route. [webView evaluateJavaScript: [NSString stringWithFormat:@"location = '%@'", (id)testURL.string()] completionHandler:nil]; #endif Util::run(&authorizationPerformed); #if PLATFORM(MAC) checkAuthorizationOptions(false, "", 0); #elif PLATFORM(IOS) checkAuthorizationOptions(true, "null", 0); #endif RetainPtr redirectURL = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto response = adoptNS([[NSHTTPURLResponse alloc] initWithURL:testURL statusCode:302 HTTPVersion:@"HTTP/1.1" headerFields:@{ @"Set-Cookie" : @"sessionid=38afes7a8;", @"Location" : [redirectURL absoluteString] }]); [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] init]).get()]; Util::run(&navigationCompleted); EXPECT_WK_STREQ(redirectURL.get().absoluteString, finalURL); navigationCompleted = false; [webView evaluateJavaScript: @"document.cookie" completionHandler:^(NSString *result, NSError *) { EXPECT_WK_STREQ("", [result UTF8String]); navigationCompleted = true; }]; Util::run(&navigationCompleted); } TEST(SOAuthorizationRedirect, InterceptionSucceedWithDifferentOrigin) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); checkAuthorizationOptions(false, "", 0); auto redirectURL = URL(URL(), "https://www.example.com"); auto response = adoptNS([[NSHTTPURLResponse alloc] initWithURL:redirectURL statusCode:302 HTTPVersion:@"HTTP/1.1" headerFields:@{ @"Location" : redirectURL.string() }]); [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] init]).get()]; Util::run(&navigationCompleted); EXPECT_WK_STREQ(testURL.get().absoluteString, finalURL); } TEST(SOAuthorizationRedirect, InterceptionSucceedWithWaitingSession) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); // The session will be waiting since the web view is is not in the window. [webView removeFromSuperview]; [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&navigationPolicyDecided); EXPECT_FALSE(authorizationPerformed); // Should activate the session. [webView addToTestWindow]; Util::run(&authorizationPerformed); checkAuthorizationOptions(false, "", 0); RetainPtr redirectURL = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto response = adoptNS([[NSHTTPURLResponse alloc] initWithURL:testURL.get() statusCode:302 HTTPVersion:@"HTTP/1.1" headerFields:@{ @"Location" : [redirectURL absoluteString] }]); [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] init]).get()]; Util::run(&navigationCompleted); #if PLATFORM(IOS) navigationCompleted = false; Util::run(&navigationCompleted); #endif EXPECT_WK_STREQ(redirectURL.get().absoluteString, finalURL); } TEST(SOAuthorizationRedirect, InterceptionAbortedWithWaitingSession) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL1 = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; RetainPtr testURL2 = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); // The session will be waiting since the web view is is not in the window. [webView removeFromSuperview]; [webView loadRequest:[NSURLRequest requestWithURL:testURL1.get()]]; Util::run(&navigationPolicyDecided); EXPECT_FALSE(authorizationPerformed); [webView loadRequest:[NSURLRequest requestWithURL:testURL2.get()]]; // Should activate the session. [webView addToTestWindow]; // The waiting session should be aborted as the previous navigation is overwritten by a new navigation. Util::run(&navigationCompleted); EXPECT_FALSE(authorizationPerformed); } TEST(SOAuthorizationRedirect, InterceptionSucceedWithActiveSessionDidMoveWindow) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); checkAuthorizationOptions(false, "", 0); // Should be a no op. [webView addToTestWindow]; RetainPtr redirectURL = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto response = adoptNS([[NSHTTPURLResponse alloc] initWithURL:testURL.get() statusCode:302 HTTPVersion:@"HTTP/1.1" headerFields:@{ @"Location" : [redirectURL absoluteString] }]); [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] init]).get()]; Util::run(&navigationCompleted); #if PLATFORM(IOS) navigationCompleted = false; Util::run(&navigationCompleted); #endif EXPECT_WK_STREQ(redirectURL.get().absoluteString, finalURL); } TEST(SOAuthorizationRedirect, InterceptionSucceedTwice) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); for (int i = 0; i < 2; i++) { authorizationPerformed = false; [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); checkAuthorizationOptions(false, "", 0); navigationCompleted = false; RetainPtr redirectURL = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto response = adoptNS([[NSHTTPURLResponse alloc] initWithURL:testURL.get() statusCode:302 HTTPVersion:@"HTTP/1.1" headerFields:@{ @"Location" : [redirectURL absoluteString] }]); [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] init]).get()]; Util::run(&navigationCompleted); #if PLATFORM(IOS) navigationCompleted = false; Util::run(&navigationCompleted); #endif EXPECT_WK_STREQ(redirectURL.get().absoluteString, finalURL); } } TEST(SOAuthorizationRedirect, InterceptionSucceedSuppressActiveSession) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); InstanceMethodSwizzler swizzler4(PAL::getSOAuthorizationClass(), @selector(cancelAuthorization), reinterpret_cast(overrideCancelAuthorization)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); checkAuthorizationOptions(false, "", 0); // Suppress the last active session. authorizationPerformed = false; [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationCancelled); Util::run(&authorizationPerformed); checkAuthorizationOptions(false, "", 0); RetainPtr redirectURL = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto response = adoptNS([[NSHTTPURLResponse alloc] initWithURL:testURL.get() statusCode:302 HTTPVersion:@"HTTP/1.1" headerFields:@{ @"Location" : [redirectURL absoluteString] }]); [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] init]).get()]; Util::run(&navigationCompleted); #if PLATFORM(IOS) navigationCompleted = false; Util::run(&navigationCompleted); #endif EXPECT_WK_STREQ(redirectURL.get().absoluteString, finalURL); } TEST(SOAuthorizationRedirect, InterceptionSucceedSuppressWaitingSession) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); // The session will be waiting since the web view is is not int the window. [webView removeFromSuperview]; [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&navigationPolicyDecided); EXPECT_FALSE(authorizationPerformed); // Suppress the last waiting session. navigationPolicyDecided = false; [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&navigationPolicyDecided); EXPECT_FALSE(authorizationPerformed); // Activate the last session. [webView addToTestWindow]; Util::run(&authorizationPerformed); checkAuthorizationOptions(false, "", 0); RetainPtr redirectURL = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto response = adoptNS([[NSHTTPURLResponse alloc] initWithURL:testURL.get() statusCode:302 HTTPVersion:@"HTTP/1.1" headerFields:@{ @"Location" : [redirectURL absoluteString] }]); [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] init]).get()]; Util::run(&navigationCompleted); #if PLATFORM(IOS) navigationCompleted = false; Util::run(&navigationCompleted); #endif EXPECT_WK_STREQ(redirectURL.get().absoluteString, finalURL); } TEST(SOAuthorizationRedirect, InterceptionSucceedSAML) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); // Add a http body to the request to mimic a SAML request. auto request = adoptNS([NSMutableURLRequest requestWithURL:testURL.get()]); [request setHTTPBody:adoptNS([[NSData alloc] init]).get()]; haveHttpBody = true; [webView loadRequest:request.get()]; Util::run(&authorizationPerformed); checkAuthorizationOptions(false, "", 0); // Pass a HTTP 200 response with a html to mimic a SAML response. auto response = adoptNS([[NSHTTPURLResponse alloc] initWithURL:testURL.get() statusCode:200 HTTPVersion:@"HTTP/1.1" headerFields:nil]); [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] initWithBytes:samlResponse length:strlen(samlResponse)]).get()]; [webView waitForMessage:@"SAML"]; } TEST(SOAuthorizationRedirect, AuthorizationOptions) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get()); [webView loadHTMLString:@"" baseURL:(NSURL *)URL(URL(), "http://www.webkit.org")]; Util::run(&navigationCompleted); [delegate setShouldOpenExternalSchemes:true]; [webView evaluateJavaScript: @"location = 'http://www.example.com'" completionHandler:nil]; Util::run(&authorizationPerformed); checkAuthorizationOptions(true, "http://www.webkit.org", 0); } TEST(SOAuthorizationRedirect, InterceptionDidNotHandleTwice) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); // Test passes if no crashes. [gDelegate authorizationDidNotHandle:gAuthorization]; [gDelegate authorizationDidNotHandle:gAuthorization]; } TEST(SOAuthorizationRedirect, InterceptionCompleteTwice) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); // Test passes if no crashes. RetainPtr redirectURL = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto response = adoptNS([[NSHTTPURLResponse alloc] initWithURL:testURL.get() statusCode:302 HTTPVersion:@"HTTP/1.1" headerFields:@{ @"Location" : [redirectURL absoluteString] }]); [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] init]).get()]; [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] init]).get()]; } TEST(SOAuthorizationRedirect, SOAuthorizationLoadPolicyIgnore) { resetState(); ClassMethodSwizzler swizzler(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [delegate setAllowSOAuthorizationLoad:false]; [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&navigationCompleted); EXPECT_WK_STREQ(testURL.get().absoluteString, finalURL); } TEST(SOAuthorizationRedirect, SOAuthorizationLoadPolicyAllowAsync) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); checkAuthorizationOptions(false, "", 0); RetainPtr redirectURL = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto response = adoptNS([[NSHTTPURLResponse alloc] initWithURL:testURL.get() statusCode:302 HTTPVersion:@"HTTP/1.1" headerFields:@{ @"Location" : [redirectURL absoluteString] }]); [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] init]).get()]; Util::run(&navigationCompleted); #if PLATFORM(IOS) navigationCompleted = false; Util::run(&navigationCompleted); #endif EXPECT_WK_STREQ(redirectURL.get().absoluteString, finalURL); } TEST(SOAuthorizationRedirect, SOAuthorizationLoadPolicyIgnoreAsync) { resetState(); ClassMethodSwizzler swizzler(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [delegate setAllowSOAuthorizationLoad:false]; [delegate setIsAsyncExecution:true]; [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&navigationCompleted); EXPECT_WK_STREQ(testURL.get().absoluteString, finalURL); } // FIXME(175204): Enable the iOS tests once the bug is fixed. #if PLATFORM(MAC) TEST(SOAuthorizationRedirect, InterceptionSucceedWithUI) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); auto viewController = adoptNS([[TestSOAuthorizationViewController alloc] init]); auto view = adoptNS([[NSView alloc] initWithFrame:NSZeroRect]); [viewController setView:view.get()]; [gDelegate authorization:gAuthorization presentViewController:viewController.get() withCompletion:^(BOOL success, NSError *) { EXPECT_TRUE(success); }]; Util::run(&uiShowed); RetainPtr redirectURL = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto response = adoptNS([[NSHTTPURLResponse alloc] initWithURL:testURL.get() statusCode:302 HTTPVersion:@"HTTP/1.1" headerFields:@{ @"Location" : [redirectURL absoluteString] }]); [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] init]).get()]; Util::run(&navigationCompleted); EXPECT_WK_STREQ(redirectURL.get().absoluteString, finalURL); EXPECT_FALSE(uiShowed); } TEST(SOAuthorizationRedirect, InterceptionCancelWithUI) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); auto viewController = adoptNS([[TestSOAuthorizationViewController alloc] init]); auto view = adoptNS([[NSView alloc] initWithFrame:NSZeroRect]); [viewController setView:view.get()]; [gDelegate authorization:gAuthorization presentViewController:viewController.get() withCompletion:^(BOOL success, NSError *) { EXPECT_TRUE(success); }]; Util::run(&uiShowed); [gDelegate authorizationDidCancel:gAuthorization]; // FIXME: Find a delegate method that can detect load cancels. Util::sleep(0.5); EXPECT_WK_STREQ("", webView.get()._committedURL.absoluteString); EXPECT_FALSE(uiShowed); } TEST(SOAuthorizationRedirect, InterceptionErrorWithUI) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); auto viewController = adoptNS([[TestSOAuthorizationViewController alloc] init]); auto view = adoptNS([[NSView alloc] initWithFrame:NSZeroRect]); [viewController setView:view.get()]; [gDelegate authorization:gAuthorization presentViewController:viewController.get() withCompletion:^(BOOL success, NSError *) { EXPECT_TRUE(success); }]; Util::run(&uiShowed); [gDelegate authorization:gAuthorization didCompleteWithError:adoptNS([[NSError alloc] initWithDomain:NSCocoaErrorDomain code:0 userInfo:nil]).get()]; Util::run(&navigationCompleted); EXPECT_WK_STREQ(testURL.get().absoluteString, finalURL); EXPECT_FALSE(uiShowed); } TEST(SOAuthorizationRedirect, InterceptionSucceedSuppressActiveSessionWithUI) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); InstanceMethodSwizzler swizzler4(PAL::getSOAuthorizationClass(), @selector(cancelAuthorization), reinterpret_cast(overrideCancelAuthorization)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); auto viewController = adoptNS([[TestSOAuthorizationViewController alloc] init]); auto view = adoptNS([[NSView alloc] initWithFrame:NSZeroRect]); [viewController setView:view.get()]; [gDelegate authorization:gAuthorization presentViewController:viewController.get() withCompletion:^(BOOL success, NSError *) { EXPECT_TRUE(success); }]; Util::run(&uiShowed); // Suppress the last active session. authorizationPerformed = false; [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationCancelled); Util::run(&authorizationPerformed); EXPECT_FALSE(uiShowed); RetainPtr redirectURL = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto response = adoptNS([[NSHTTPURLResponse alloc] initWithURL:testURL.get() statusCode:302 HTTPVersion:@"HTTP/1.1" headerFields:@{ @"Location" : [redirectURL absoluteString] }]); [gDelegate authorization:gAuthorization didCompleteWithHTTPResponse:response.get() httpBody:adoptNS([[NSData alloc] init]).get()]; Util::run(&navigationCompleted); EXPECT_WK_STREQ(redirectURL.get().absoluteString, finalURL); } TEST(SOAuthorizationRedirect, ShowUITwice) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); auto viewController = adoptNS([[TestSOAuthorizationViewController alloc] init]); auto view = adoptNS([[NSView alloc] initWithFrame:NSZeroRect]); [viewController setView:view.get()]; [gDelegate authorization:gAuthorization presentViewController:viewController.get() withCompletion:^(BOOL success, NSError *) { EXPECT_TRUE(success); }]; Util::run(&uiShowed); uiShowed = false; [gDelegate authorization:gAuthorization presentViewController:viewController.get() withCompletion:^(BOOL success, NSError *error) { EXPECT_FALSE(success); EXPECT_EQ(error.code, kSOErrorAuthorizationPresentationFailed); EXPECT_WK_STREQ(error.domain, "com.apple.AppSSO.AuthorizationError"); uiShowed = true; }]; Util::run(&uiShowed); } #endif #if PLATFORM(MAC) TEST(SOAuthorizationRedirect, NSNotificationCenter) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get(), OpenExternalSchemesPolicy::Allow); [webView loadRequest:[NSURLRequest requestWithURL:testURL.get()]]; Util::run(&authorizationPerformed); auto viewController = adoptNS([[TestSOAuthorizationViewController alloc] init]); auto view = adoptNS([[NSView alloc] initWithFrame:NSZeroRect]); { InstanceMethodSwizzler swizzler4([NSNotificationCenter class], @selector(addObserverForName:object:queue:usingBlock:), reinterpret_cast(overrideAddObserverForName)); [viewController setView:view.get()]; [gDelegate authorization:gAuthorization presentViewController:viewController.get() withCompletion:^(BOOL success, NSError *) { EXPECT_TRUE(success); }]; Util::run(&uiShowed); } gNotificationCallback(nullptr); EXPECT_FALSE(uiShowed); } #endif TEST(SOAuthorizationPopUp, NoInterceptions) { resetState(); RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto testHtml = generateHtml(openerTemplate, testURL.get().absoluteString); auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)]); auto delegate = adoptNS([[TestSOAuthorizationDelegate alloc] init]); configureSOAuthorizationWebView(webView.get(), delegate.get()); [webView loadHTMLString:testHtml baseURL:testURL.get()]; Util::run(&navigationCompleted); [delegate setShouldOpenExternalSchemes:true]; navigationCompleted = false; #if PLATFORM(MAC) [webView sendClicksAtPoint:NSMakePoint(200, 200) numberOfClicks:1]; #elif PLATFORM(IOS) [webView evaluateJavaScript: @"clickMe()" completionHandler:nil]; #endif Util::run(&newWindowCreated); Util::run(&navigationCompleted); EXPECT_WK_STREQ(testURL.get().absoluteString, finalURL); } // FIXME(172614): Enable the following test for iOS once the bug is fixed. #if PLATFORM(MAC) TEST(SOAuthorizationPopUp, NoInterceptionsSubFrame) { resetState(); ClassMethodSwizzler swizzler1(PAL::getSOAuthorizationClass(), @selector(canPerformAuthorizationWithURL:responseCode:), reinterpret_cast(overrideCanPerformAuthorizationWithURL)); InstanceMethodSwizzler swizzler2(PAL::getSOAuthorizationClass(), @selector(setDelegate:), reinterpret_cast(overrideSetDelegate)); InstanceMethodSwizzler swizzler3(PAL::getSOAuthorizationClass(), @selector(beginAuthorizationWithURL:httpHeaders:httpBody:), reinterpret_cast(overrideBeginAuthorizationWithURL)); RetainPtr baseURL = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; RetainPtr testURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; auto iframeTestHtml = generateHtml(openerTemplate, testURL.get().absoluteString); auto testHtml = makeString("