OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import <ChromeWebView/ChromeWebView.h> |
| 6 #import <WebKit/WebKit.h> |
| 7 |
| 8 #import "ios/web_view/test/web_view_test.h" |
| 9 #import "ios/web_view/test/web_view_test_util.h" |
| 10 #import "net/base/mac/url_conversions.h" |
| 11 #include "testing/gtest_mac.h" |
| 12 #include "url/gurl.h" |
| 13 |
| 14 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 15 #error "This file requires ARC support." |
| 16 #endif |
| 17 |
| 18 namespace ios_web_view { |
| 19 |
| 20 namespace { |
| 21 |
| 22 // Creates web view with incognito configuration and frame equal to screen |
| 23 // bounds. |
| 24 CWVWebView* CreateIncognitoWebView() { |
| 25 return test::CreateWebView([CWVWebViewConfiguration incognitoConfiguration]); |
| 26 } |
| 27 |
| 28 } // namespace |
| 29 |
| 30 // Test fixture for incognito browsing mode. |
| 31 typedef ios_web_view::WebViewTest WebViewIncognitoTest; |
| 32 |
| 33 // Tests that browsing data (cookie and localStorage) does not leak from |
| 34 // non-incognito to incognito web view. |
| 35 TEST_F(WebViewIncognitoTest, BrowsingDataNotLeakingToIncognito) { |
| 36 // CWVWebView does not allow JavaScript execution if the page was not loaded. |
| 37 GURL url = GetUrlForPageWithHtmlBody(std::string()); |
| 38 ASSERT_TRUE(test::LoadUrl(web_view_, net::NSURLWithGURL(url))); |
| 39 |
| 40 NSError* error = nil; |
| 41 test::EvaluateJavaScript(web_view_, @"localStorage.setItem('k', 'v');", |
| 42 &error); |
| 43 ASSERT_NSEQ(nil, error); |
| 44 test::EvaluateJavaScript(web_view_, @"document.cookie='n=v;'", &error); |
| 45 ASSERT_NSEQ(nil, error); |
| 46 |
| 47 // Create web view with the same configuration, otherwise browswing data may |
| 48 // not be shared immidiately. Make sure that new web view has browsing data |
| 49 // from the previous web view. |
| 50 CWVWebView* non_incognito_web_view = |
| 51 test::CreateWebView([web_view_ configuration]); |
| 52 ASSERT_TRUE(test::LoadUrl(non_incognito_web_view, net::NSURLWithGURL(url))); |
| 53 id localStorageValue = test::EvaluateJavaScript( |
| 54 non_incognito_web_view, @"localStorage.getItem('k');", &error); |
| 55 ASSERT_NSEQ(nil, error); |
| 56 ASSERT_NSEQ(@"v", localStorageValue); |
| 57 id cookie = test::EvaluateJavaScript(non_incognito_web_view, |
| 58 @"document.cookie", &error); |
| 59 ASSERT_NSEQ(nil, error); |
| 60 ASSERT_TRUE([cookie containsString:@"n=v"]); |
| 61 |
| 62 // Verify that incognito web view does not have browsing data from |
| 63 // non-incognito web view. |
| 64 CWVWebView* incognito_web_view = CreateIncognitoWebView(); |
| 65 ASSERT_TRUE(incognito_web_view); |
| 66 ASSERT_TRUE(test::LoadUrl(incognito_web_view, net::NSURLWithGURL(url))); |
| 67 localStorageValue = test::EvaluateJavaScript( |
| 68 incognito_web_view, @"localStorage.getItem('k');", &error); |
| 69 EXPECT_NSEQ(nil, error); |
| 70 ASSERT_NSEQ([NSNull null], localStorageValue); |
| 71 cookie = |
| 72 test::EvaluateJavaScript(incognito_web_view, @"document.cookie", &error); |
| 73 EXPECT_NSEQ(nil, error); |
| 74 ASSERT_NSEQ(@"", cookie); |
| 75 } |
| 76 |
| 77 // Tests that browsing data (cookie and localStorage) does not leak from |
| 78 // incognito to non-incognito web view. |
| 79 TEST_F(WebViewIncognitoTest, BrowsingDataNotLeakingFromIncognito) { |
| 80 // CWVWebView does not allow JavaScript execution if the page was not loaded. |
| 81 CWVWebView* incognito_web_view = CreateIncognitoWebView(); |
| 82 GURL url = GetUrlForPageWithHtmlBody(std::string()); |
| 83 ASSERT_TRUE(test::LoadUrl(incognito_web_view, net::NSURLWithGURL(url))); |
| 84 |
| 85 NSError* error = nil; |
| 86 test::EvaluateJavaScript(incognito_web_view, |
| 87 @"localStorage.setItem('k2', 'v');", &error); |
| 88 // |localStorage.setItem| throws exception in Incognito. |
| 89 ASSERT_EQ(WKErrorJavaScriptExceptionOccurred, error.code); |
| 90 test::EvaluateJavaScript(incognito_web_view, @"document.cookie='n2=v;'", |
| 91 &error); |
| 92 ASSERT_NSEQ(nil, error); |
| 93 |
| 94 // Create incognito web view with the same configuration, otherwise browswing |
| 95 // data will not be shared. Make sure that new incognito web view has browsing |
| 96 // data from the previous incognito web view. |
| 97 CWVWebView* incognito_web_view2 = |
| 98 test::CreateWebView([incognito_web_view configuration]); |
| 99 ASSERT_TRUE(test::LoadUrl(incognito_web_view2, net::NSURLWithGURL(url))); |
| 100 id localStorageValue = test::EvaluateJavaScript( |
| 101 incognito_web_view2, @"localStorage.getItem('k2');", &error); |
| 102 ASSERT_NSEQ(nil, error); |
| 103 ASSERT_NSEQ([NSNull null], localStorageValue); |
| 104 id cookie = |
| 105 test::EvaluateJavaScript(incognito_web_view2, @"document.cookie", &error); |
| 106 ASSERT_NSEQ(nil, error); |
| 107 ASSERT_TRUE([cookie containsString:@"n2=v"]); |
| 108 |
| 109 // Verify that non-incognito web view does not have browsing data from |
| 110 // incognito web view. |
| 111 ASSERT_TRUE(web_view_); |
| 112 ASSERT_TRUE(test::LoadUrl(web_view_, net::NSURLWithGURL(url))); |
| 113 localStorageValue = test::EvaluateJavaScript( |
| 114 web_view_, @"localStorage.getItem('k2');", &error); |
| 115 EXPECT_NSEQ(nil, error); |
| 116 ASSERT_NSEQ([NSNull null], localStorageValue); |
| 117 cookie = test::EvaluateJavaScript(web_view_, @"document.cookie", &error); |
| 118 EXPECT_NSEQ(nil, error); |
| 119 ASSERT_FALSE([cookie containsString:@"n2=v"]); |
| 120 } |
| 121 |
| 122 } // namespace ios_web_view |
OLD | NEW |