summaryrefslogtreecommitdiffstats
path: root/ios/web/browser_state_web_view_partition_inttest.mm
blob: 9e39ea623b46a2205ad164322031ba531110f0b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>
#import <WebKit/WebKit.h>

#import "base/mac/scoped_nsobject.h"
#include "base/memory/scoped_ptr.h"
#include "base/test/ios/wait_util.h"
#include "ios/web/public/browser_state.h"
#import "ios/web/public/test/http_server.h"
#include "ios/web/public/test/response_providers/string_response_provider.h"
#import "ios/web/public/web_view_creation_util.h"
#import "ios/web/test/web_int_test.h"
#import "ios/web/web_state/ui/web_view_js_utils.h"
#import "net/base/mac/url_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"

// A WKNavigationDelegate that is used to check if a WKWebView has finished
// a navigation. Used for testing purposes.
@interface TestNavigationDelegate : NSObject <WKNavigationDelegate>
// YES if a navigation has finished.
@property (nonatomic, assign) BOOL didFinishNavigation;
@end

@implementation TestNavigationDelegate

@synthesize didFinishNavigation = _didFinishNavigation;

- (void)webView:(WKWebView*)webView
    didFinishNavigation:(WKNavigation*)navigation {
  self.didFinishNavigation = YES;
}

@end

// A test fixture for testing that browsing data is partitioned between
// web views created with a non-OTR BrowserState and web views created with an
// OTR BrowserState.
class BrowserStateWebViewPartitionTest : public web::WebIntTest {
 protected:
  void SetUp() override {
    web::WebIntTest::SetUp();

    otr_browser_state_.SetOffTheRecord(true);

    web::test::HttpServer& server = web::test::HttpServer::GetSharedInstance();
    ASSERT_TRUE(server.IsRunning());

    provider_.reset(new web::StringResponseProvider("Hello World"));
    server.AddResponseProvider(provider_.get());
  }

  void TearDown() override {
    web::test::HttpServer& server = web::test::HttpServer::GetSharedInstance();
    server.RemoveResponseProvider(provider_.release());

    web::WebIntTest::TearDown();
  }

  // Runs the given JavaScript on |web_view| and returns the result as a string.
  NSString* EvaluateJavaScript(WKWebView* web_view,
                               NSString* js) {
    __block base::scoped_nsobject<NSString> result;
    __block bool block_was_called = false;
    web::EvaluateJavaScript(web_view, js, ^(NSString* js_result, NSError*) {
      result.reset([js_result copy]);
      block_was_called = true;
    });
    base::test::ios::WaitUntilCondition(^bool {
      return block_was_called;
    });
    return [[result copy] autorelease];
  }

  // Sets a persistent cookie with key, value on |web_view|.
  void SetCookie(NSString* key, NSString* value, WKWebView* web_view) {
    NSString* set_cookie = [NSString
        stringWithFormat:@"document.cookie='%@=%@;"
                         @"Expires=Tue, 05-May-9999 02:18:23 GMT; Path=/'",
                         key, value];
    EvaluateJavaScript(web_view, set_cookie);
  }

  // Returns a csv list of all cookies from |web_view|.
  NSString* GetCookies(WKWebView* web_view) {
    return EvaluateJavaScript(web_view, @"document.cookie");
  }

  // Sets a localstorage key, value pair on |web_view|.
  void SetLocalStorageItem(NSString* key,
                           NSString* value,
                           WKWebView* web_view) {
    NSString* set_local_storage_item = [NSString
        stringWithFormat:@"localStorage.setItem('%@', '%@')", key, value];
    EvaluateJavaScript(web_view, set_local_storage_item);
  }

  // Returns the localstorage value associated with |key| from |web_view|.
  NSString* GetLocalStorageItem(NSString* key, WKWebView* web_view) {
    NSString* get_local_storage_value =
        [NSString stringWithFormat:@"localStorage.getItem('%@');", key];
    return EvaluateJavaScript(web_view, get_local_storage_value);
  }

  // Loads a test web page (that contains a small string) in |web_view| and
  // waits until the web view has finished the navigation.
  void LoadTestWebPage(WKWebView* web_view) {
    DCHECK(web_view);

    base::scoped_nsobject<TestNavigationDelegate> navigation_delegate(
        [[TestNavigationDelegate alloc] init]);

    id old_navigation_delegate = web_view.navigationDelegate;
    web_view.navigationDelegate = navigation_delegate;

    web::test::HttpServer& server = web::test::HttpServer::GetSharedInstance();
    ASSERT_TRUE(server.IsRunning());

    NSURL* url = net::NSURLWithGURL(server.MakeUrl("http://whatever/"));
    [web_view loadRequest:[NSURLRequest requestWithURL:url]];
    base::test::ios::WaitUntilCondition(^bool {
      return [navigation_delegate didFinishNavigation];
    });

    web_view.navigationDelegate = old_navigation_delegate;
  }

  // Returns the BrowserState that is used for testing.
  web::BrowserState* GetOtrBrowserState() { return &otr_browser_state_; }

 private:
  // The ResponseProvider used to load a simple web page.
  scoped_ptr<web::ResponseProvider> provider_;
  // The OTR browser state used in tests.
  web::TestBrowserState otr_browser_state_;
};

// Tests that cookies are partitioned between web views created with a
// non-OTR BrowserState and an OTR BrowserState.
TEST_F(BrowserStateWebViewPartitionTest, Cookies) {
  base::scoped_nsobject<WKWebView> web_view_1(
      web::CreateWKWebView(CGRectZero, GetBrowserState()));
  LoadTestWebPage(web_view_1);
  SetCookie(@"someCookieName1", @"someCookieValue1", web_view_1);
  EXPECT_NSEQ(@"someCookieName1=someCookieValue1", GetCookies(web_view_1));

  base::scoped_nsobject<WKWebView> web_view_2(
      web::CreateWKWebView(CGRectZero, GetOtrBrowserState()));
  LoadTestWebPage(web_view_2);

  // Test that the cookie has not leaked over to |web_view_2|.
  EXPECT_NSEQ(@"", GetCookies(web_view_2));

  SetCookie(@"someCookieName2", @"someCookieValue2", web_view_2);
  EXPECT_NSEQ(@"someCookieName2=someCookieValue2", GetCookies(web_view_2));

  // Test that the cookie has not leaked over to |web_view_1|.
  NSString* cookies = GetCookies(web_view_1);
  EXPECT_FALSE([cookies containsString:@"someCookieName2"]);
}

// Tests that localStorage is partitioned between web views created with a
// non-OTR BrowserState and an OTR BrowserState.
TEST_F(BrowserStateWebViewPartitionTest, LocalStorage) {
  base::scoped_nsobject<WKWebView> web_view_1(
      web::CreateWKWebView(CGRectZero, GetBrowserState()));
  LoadTestWebPage(web_view_1);
  SetLocalStorageItem(@"someKey1", @"someValue1", web_view_1);
  EXPECT_NSEQ(@"someValue1", GetLocalStorageItem(@"someKey1", web_view_1));

  base::scoped_nsobject<WKWebView> web_view_2(
      web::CreateWKWebView(CGRectZero, GetOtrBrowserState()));
  LoadTestWebPage(web_view_2);

  // Test that LocalStorage has not leaked over to |web_view_2|.
  EXPECT_NSEQ(@"", GetLocalStorageItem(@"someKey1", web_view_2));

  SetLocalStorageItem(@"someKey2", @"someValue2", web_view_2);
  // Due to platform limitation, it's not possible to actually set localStorage
  // item on an OTR BrowserState. Therefore, it's not possible to verify that a
  // localStorage item has been correctly set.
  // Look at
  // http://stackoverflow.com/questions/14555347/html5-localstorage-error-with-safari-quota-exceeded-err-dom-exception-22-an
  // for more details.
  // Test that LocalStorage has not leaked over to |web_view_1|.
  EXPECT_NSEQ(@"", GetLocalStorageItem(@"someKey2", web_view_1));
}