This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 441
/
TabSessionTests.swift
767 lines (629 loc) · 25.2 KB
/
TabSessionTests.swift
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
import XCTest
import Shared
import BraveShared
import Storage
import Data
import WebKit
import ObjectiveC.runtime
@testable import Brave
private extension WKWebView {
class func swizzleMe() {
let originalSelector = #selector(WKWebView.init(frame:configuration:))
let swizzledSelector = #selector(WKWebView.reInit(frame:configuration:))
let originalMethod = class_getInstanceMethod(self, originalSelector)!
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)!
let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
if didAddMethod {
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
@objc
func reInit(frame: CGRect, configuration: WKWebViewConfiguration) -> WKWebView {
configuration.setValue(true, forKey: "alwaysRunsAtForegroundPriority")
return reInit(frame: frame, configuration: configuration)
}
}
private extension HTTPCookie {
class func filter(cookies: [HTTPCookie], for url: URL) -> [HTTPCookie]? {
guard let host = url.host?.lowercased() else { return nil }
return cookies.filter({ $0.validFor(host: host) })
}
private func validFor(host: String) -> Bool {
guard domain.hasPrefix(".") else { return host == domain }
return host == domain.dropFirst() || host.hasSuffix(domain)
}
}
private class WebViewNavigationAdapter: NSObject, WKNavigationDelegate {
private let didFailListener: ((Error) -> Void)?
private let didFinishListener: (() -> Void)?
init(didFailListener: ((Error) -> Void)? = nil, didFinishListener: (() -> Void)? = nil) {
self.didFailListener = didFailListener
self.didFinishListener = didFinishListener
super.init()
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(.allow)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
decisionHandler(.allow)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
didFinishListener?()
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
didFailListener?(error)
}
}
@MainActor class TabSessionTests: XCTestCase {
private var tabManager: TabManager!
private let maxTimeout = 60.0
override class func setUp() {
super.setUp()
// WKWebView.swizzleMe()
}
override func setUp() {
super.setUp()
DataController.shared.initializeOnce()
tabManager = { () -> TabManager in
let profile = BrowserProfile(localName: "profile")
return TabManager(prefs: profile.prefs, rewards: nil, tabGeneratorAPI: nil)
}()
}
override func tearDown() {
super.tearDown()
destroyData {}
tabManager = nil
}
private func destroyData(_ completion: @escaping () -> Void) {
tabManager.removeAll()
tabManager.reset()
HTTPCookieStorage.shared.removeCookies(since: .distantPast)
WKWebsiteDataStore.default().removeData(
ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(),
modifiedSince: .distantPast,
completionHandler: {
completion()
})
}
func testPrivateTabSessionSharing() {
let dataStoreExpectation = XCTestExpectation(description: "dataStorePersistence")
var webViewNavigationAdapter = WebViewNavigationAdapter()
destroyData {
let urls = [
"https://bing.com",
"https://google.com",
"https://yandex.ru",
"https://yahoo.com",
]
self.tabManager.addTabsForURLs(urls.compactMap({ URL(string: $0) }), zombie: false, isPrivate: true)
if self.tabManager.allTabs.count != 4 {
XCTFail("Error: Not all Tabs are created equally")
return dataStoreExpectation.fulfill()
}
let group = DispatchGroup()
for tab in self.tabManager.allTabs {
guard let webView = tab.webView else {
XCTFail("WebView is not created yet")
return dataStoreExpectation.fulfill()
}
if webView.configuration.websiteDataStore.isPersistent {
XCTFail("Private Tab is storing data persistently!")
return dataStoreExpectation.fulfill()
}
group.enter()
}
webViewNavigationAdapter = WebViewNavigationAdapter(
didFailListener: { _ in
group.leave()
},
didFinishListener: {
group.leave()
})
self.tabManager.addNavigationDelegate(webViewNavigationAdapter)
group.notify(queue: .main) {
if self.tabManager.allTabs.count != 4 {
XCTFail("Error: Not all Tabs are alive equally")
return dataStoreExpectation.fulfill()
}
// All requests finished loading.. time to check the cookies..
let group = DispatchGroup()
for tab in self.tabManager.allTabs {
guard let webView = tab.webView else {
XCTFail("WebView died unexpectedly")
return dataStoreExpectation.fulfill()
}
group.enter()
webView.configuration.websiteDataStore.fetchDataRecords(
ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(),
completionHandler: { records in
XCTAssertFalse(records.isEmpty, "Error: Data Store not shared amongst private tabs!")
let recordNames = Set<String>(records.compactMap({ URL(string: "http://\($0.displayName)")?.host }))
let urlNames = Set<String>(urls.compactMap({ URL(string: $0)?.host }))
XCTAssertTrue(urlNames.isSubset(of: recordNames), "Data Store records do not match!")
group.leave()
})
}
group.notify(queue: .main) {
dataStoreExpectation.fulfill()
}
}
}
wait(for: [dataStoreExpectation], timeout: maxTimeout)
}
func testNormalTabSessionSharing() {
let dataStoreExpectation = XCTestExpectation(description: "dataStorePersistence")
var webViewNavigationAdapter = WebViewNavigationAdapter()
destroyData {
let urls = [
"https://stackoverflow.com",
"https://discordapp.com",
"https://apple.com",
"https://slack.com",
]
self.tabManager.addTabsForURLs(urls.compactMap({ URL(string: $0) }), zombie: false, isPrivate: false)
self.tabManager.removeTabs(self.tabManager.allTabs.filter({ $0.url?.absoluteString.contains("localhost") ?? false }))
if self.tabManager.allTabs.count != 4 {
XCTFail("Error: Not all Tabs are created equally")
return dataStoreExpectation.fulfill()
}
let group = DispatchGroup()
for tab in self.tabManager.allTabs {
guard let webView = tab.webView else {
XCTFail("WebView is not created yet")
return dataStoreExpectation.fulfill()
}
if !webView.configuration.websiteDataStore.isPersistent {
XCTFail("Normal Tab is not storing data persistently!")
return dataStoreExpectation.fulfill()
}
group.enter()
}
webViewNavigationAdapter = WebViewNavigationAdapter(
didFailListener: { _ in
group.leave()
},
didFinishListener: {
group.leave()
})
self.tabManager.addNavigationDelegate(webViewNavigationAdapter)
group.notify(queue: .main) {
if self.tabManager.allTabs.count != 4 {
XCTFail("Error: Not all Tabs are alive equally")
return dataStoreExpectation.fulfill()
}
// All requests finished loading.. time to check the cookies..
let group = DispatchGroup()
if self.tabManager.allTabs.isEmpty {
XCTFail("Tabs somehow destroyed")
return dataStoreExpectation.fulfill()
}
for tab in self.tabManager.allTabs {
guard let webView = tab.webView else {
XCTFail("WebView died unexpectedly")
return dataStoreExpectation.fulfill()
}
group.enter()
webView.configuration.websiteDataStore.fetchDataRecords(
ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(),
completionHandler: { records in
XCTAssertFalse(records.isEmpty, "Error: Data Store not shared amongst normal tabs!")
let recordNames = Set<String>(records.compactMap({ URL(string: "http://\($0.displayName)")?.host }))
let urlNames = Set<String>(urls.compactMap({ URL(string: $0)?.host }))
XCTAssertTrue(urlNames.isSubset(of: recordNames), "Data Store records do not match!")
group.leave()
})
}
group.notify(queue: .main) {
dataStoreExpectation.fulfill()
}
}
}
wait(for: [dataStoreExpectation], timeout: maxTimeout)
}
func testPrivateTabNonPersistence() {
let dataStoreExpectation = XCTestExpectation(description: "dataStorePersistence")
var webViewNavigationAdapter = WebViewNavigationAdapter()
destroyData {
let urls = [
"https://github.com",
"https://bitbucket.com",
"https://youtube.com",
"https://msn.com",
]
self.tabManager.addTabsForURLs(urls.compactMap({ URL(string: $0) }), zombie: false, isPrivate: true)
if self.tabManager.allTabs.count != 4 {
XCTFail("Error: Not all Tabs are created equally")
dataStoreExpectation.fulfill()
}
let group = DispatchGroup()
for tab in self.tabManager.allTabs {
guard let webView = tab.webView else {
XCTFail("WebView is not created yet")
return dataStoreExpectation.fulfill()
}
if webView.configuration.websiteDataStore.isPersistent {
XCTFail("Private Tab is storing data persistently!")
return dataStoreExpectation.fulfill()
}
group.enter()
}
webViewNavigationAdapter = WebViewNavigationAdapter(
didFailListener: { _ in
group.leave()
},
didFinishListener: {
group.leave()
})
self.tabManager.addNavigationDelegate(webViewNavigationAdapter)
group.notify(queue: .main) {
// All requests finished loading.. kill all tabs.. check the cookies..
self.tabManager.removeAll()
// When the tab manager destroys tabs, if there are no tabs left, it adds a new zombie tab.
// Hence the count of 1.
if self.tabManager.allTabs.count != 1 {
XCTFail("Error: Not all Tabs are destroyed equally")
return dataStoreExpectation.fulfill()
}
// Now that all private tabs have been destroyed, we need to create a new one and check the storage.
// We'd verify if the new tab has the old data after all private tabs were destroyed.
let group = DispatchGroup()
webViewNavigationAdapter = WebViewNavigationAdapter(
didFailListener: { _ in
group.leave()
},
didFinishListener: {
group.leave()
})
self.tabManager.addNavigationDelegate(webViewNavigationAdapter)
self.tabManager.addTabsForURLs([URL(string: "https://brave.com")!], zombie: false, isPrivate: true)
if self.tabManager.tabs(withType: .private).isEmpty {
XCTFail("Error: Private tab not created")
return dataStoreExpectation.fulfill()
}
for tab in self.tabManager.allTabs {
guard let webView = tab.webView else {
XCTFail("WebView is not created yet")
return dataStoreExpectation.fulfill()
}
if webView.configuration.websiteDataStore.isPersistent {
XCTFail("Private Tab is storing data persistently!")
return dataStoreExpectation.fulfill()
}
group.enter()
}
group.notify(queue: .main) {
let group = DispatchGroup()
for tab in self.tabManager.allTabs {
guard let webView = tab.webView else {
XCTFail("WebView died unexpectedly")
return dataStoreExpectation.fulfill()
}
group.enter()
webView.configuration.websiteDataStore.fetchDataRecords(
ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(),
completionHandler: { records in
XCTAssertFalse(records.isEmpty, "Error: Data Store does not contain the latest webpage's data!")
let recordNames = records.compactMap({ URL(string: "http://\($0.displayName)")?.host })
for url in urls.compactMap({ URL(string: $0)?.host }) {
if recordNames.contains(url) {
XCTFail("Error: Data Store is NOT ephemeral!")
}
}
group.leave()
})
}
group.notify(queue: .main) {
dataStoreExpectation.fulfill()
}
}
}
}
wait(for: [dataStoreExpectation], timeout: maxTimeout)
}
func testTabsPrivateToNormal() {
let dataStoreExpectation = XCTestExpectation(description: "dataStorePersistence")
var webViewNavigationAdapter = WebViewNavigationAdapter()
destroyData {
let url = URL(string: "https://www.insanelymac.com")!
let otherURL = URL(string: "https://www.macrumors.com")!
self.tabManager.addTabsForURLs([url], zombie: false, isPrivate: true)
if self.tabManager.allTabs.count != 1 {
XCTFail("Error: Not all Tabs are created equally")
return dataStoreExpectation.fulfill()
}
let group = DispatchGroup()
for tab in self.tabManager.allTabs {
guard let webView = tab.webView else {
XCTFail("WebView is not created yet")
return dataStoreExpectation.fulfill()
}
if webView.configuration.websiteDataStore.isPersistent {
XCTFail("Private Tab is storing data persistently!")
return dataStoreExpectation.fulfill()
}
group.enter()
}
webViewNavigationAdapter = WebViewNavigationAdapter(
didFailListener: { _ in
group.leave()
},
didFinishListener: {
group.leave()
})
self.tabManager.addNavigationDelegate(webViewNavigationAdapter)
// All requests finished loading.. switch to normal mode.. check the cookies..
group.notify(queue: .main) {
let group = DispatchGroup()
webViewNavigationAdapter = WebViewNavigationAdapter(
didFailListener: { _ in
group.leave()
},
didFinishListener: {
group.leave()
})
self.tabManager.addNavigationDelegate(webViewNavigationAdapter)
self.tabManager.addTabsForURLs([otherURL], zombie: false, isPrivate: false)
// When the tab manager switches to normal mode from private
// It should kill all private tabs..
if !self.tabManager.tabs(withType: .private).isEmpty {
XCTFail("Error: Private tabs not destroyed when switching to normal mode!")
return dataStoreExpectation.fulfill()
}
if self.tabManager.tabs(withType: .regular).isEmpty {
XCTFail("Error: Normal tab not created")
return dataStoreExpectation.fulfill()
}
for tab in self.tabManager.allTabs {
guard let webView = tab.webView else {
XCTFail("WebView is not created yet")
return dataStoreExpectation.fulfill()
}
if webView.configuration.websiteDataStore.isPersistent == false {
XCTFail("Normal Tab is not storing data persistently!")
return dataStoreExpectation.fulfill()
}
group.enter()
}
group.notify(queue: .main) {
let group = DispatchGroup()
for tab in self.tabManager.tabs(withType: .regular) {
guard let webView = tab.webView else {
XCTFail("Webview died unexpectedly")
return dataStoreExpectation.fulfill()
}
group.enter()
webView.configuration.websiteDataStore.fetchDataRecords(
ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(),
completionHandler: { records in
XCTAssertFalse(records.isEmpty, "Error: Data Store not persistent in normal mode!")
let recordNames = Set<String>(records.compactMap({ URL(string: "http://\($0.displayName)")?.host }))
let urlNames = Set<String>([url.host ?? "FailedTestDomain"])
XCTAssertFalse(urlNames.isSubset(of: recordNames), "Data Store leaking from private tab to normal tab!")
group.leave()
})
}
group.notify(queue: .main) {
dataStoreExpectation.fulfill()
}
}
}
}
wait(for: [dataStoreExpectation], timeout: maxTimeout)
}
func testIsSecretTokenAvailable() {
let url = URL(string: "https://www.brave.com")!
let expectation = self.expectation(description: "No secret tokens found")
var webViewNavigationAdapter = WebViewNavigationAdapter()
self.tabManager.addTabsForURLs([url], zombie: false, isPrivate: false)
let javascript =
"""
(function() {
var found = ''
var parsed = []
// search all user known functions for tokens
var functions = []
if (typeof navigator.credentials !== 'undefined') {
if (typeof navigator.credentials.create !== 'undefined') {
functions.push(navigator.credentials.create)
}
if (typeof navigator.credentials.get !== 'undefined') {
functions.push(navigator.credentials.get)
}
}
// search all enumerable objects for tokens
var objects = [
document,
window
]
if (typeof registerResponse !== 'undefined') {
objects.push(registerResponse)
}
if (typeof signResponse !== 'undefined') {
objects.push(signResponse)
}
if (typeof navigator.credentials !== 'undefined') {
objects.push(navigator.credentials)
}
function isTokenPresent(value) {
if (typeof value == "undefined" || typeof value.indexOf == "undefined") {
return false;
}
if (value.indexOf("\(UserScriptManager.securityToken)") >= 0 || value.indexOf("\(UserScriptManager.securityToken)") >= 0) {
return true;
}
return false;
}
function secretTokensInObject(obj) {
if (obj == undefined) {
return ''
}
if (isTokenPresent(obj.name) || isTokenPresent(obj.toString())) {
return '' + obj
}
for (const [key, value] of Object.entries(obj)) {
if (isTokenPresent(key + '') || isTokenPresent(value + '')) {
return key + ': ' + value;
}
if (typeof value == 'function' && isTokenPresent(value.toString())) {
return 'function: ' + value
}
if (typeof value == 'object' && value != null) {
if (isTokenPresent(value.constructor.name)) {
return 'Object: ' + value.constructor.name
} else if (value != obj && parsed.indexOf(value) == -1 && objects.indexOf(value) == -1) {
objects.push(value)
}
}
}
parsed.push(obj)
return '';
}
function secretTokensInFunctions(func) {
if (func == undefined) {
return '';
}
if (isTokenPresent(func.toString())) {
return func.toString();
}
return ''
}
while (objects.length > 0 && found.length == 0) {
found = secretTokensInObject(objects.shift())
}
while (functions.length > 0 && found.length == 0) {
found = secretTokensInFunctions(functions.shift())
}
return found
})();
"""
let scripts = Set<UserScriptManager.ScriptType>(UserScriptManager.ScriptType.allCases)
let customScripts = Set<UserScriptType>([.farblingProtection(etld: "fake.com")])
let group = DispatchGroup()
for tab in self.tabManager.allTabs {
// include all scripts
UserScriptManager.shared.loadCustomScripts(into: tab, userScripts: scripts, customScripts: customScripts)
group.enter()
}
webViewNavigationAdapter = WebViewNavigationAdapter(
didFailListener: { _ in
group.leave()
},
didFinishListener: {
group.leave()
})
self.tabManager.addNavigationDelegate(webViewNavigationAdapter)
group.notify(queue: .main) {
let tab = self.tabManager.selectedTab
guard let webView = tab?.webView else {
XCTFail("WebView is not created yet")
return
}
webView.evaluateSafeJavaScript(functionName: javascript, contentWorld: .page, asFunction: false) { result, error in
guard let keys = result as? String else {
XCTFail("Javascript error while finding secret tokens")
expectation.fulfill()
return
}
if !keys.isEmpty {
XCTFail("Secret tokens found!" + keys)
}
expectation.fulfill()
}
}
waitForExpectations(timeout: 60, handler: nil)
}
func testTabsNormalToPrivate() {
let dataStoreExpectation = XCTestExpectation(description: "dataStorePersistence")
var webViewNavigationAdapter = WebViewNavigationAdapter()
destroyData {
let url = URL(string: "https://twitter.com")!
let otherURL = URL(string: "https://instagram.com")!
self.tabManager.addTabsForURLs([url], zombie: false, isPrivate: false)
if self.tabManager.allTabs.count != 1 {
XCTFail("Error: Not all Tabs are created equally")
return dataStoreExpectation.fulfill()
}
let group = DispatchGroup()
for tab in self.tabManager.allTabs {
guard let webView = tab.webView else {
XCTFail("WebView is not created yet")
return dataStoreExpectation.fulfill()
}
if !webView.configuration.websiteDataStore.isPersistent {
XCTFail("Normal Tab is not storing data persistently!")
return dataStoreExpectation.fulfill()
}
group.enter()
}
webViewNavigationAdapter = WebViewNavigationAdapter(
didFailListener: { _ in
group.leave()
},
didFinishListener: {
group.leave()
})
self.tabManager.addNavigationDelegate(webViewNavigationAdapter)
// All requests finished loading.. switch to private mode.. check the cookies..
group.notify(queue: .main) {
let group = DispatchGroup()
webViewNavigationAdapter = WebViewNavigationAdapter(
didFailListener: { _ in
group.leave()
},
didFinishListener: {
group.leave()
})
self.tabManager.addNavigationDelegate(webViewNavigationAdapter)
self.tabManager.addTabsForURLs([otherURL], zombie: false, isPrivate: true)
// When the tab manager switches to private mode from normal
// It should keep both private and normal tabs
if self.tabManager.tabs(withType: .private).isEmpty {
XCTFail("Error: Private tabs not created")
return dataStoreExpectation.fulfill()
}
if self.tabManager.tabs(withType: .regular).isEmpty {
XCTFail("Error: Normal tab not created")
return dataStoreExpectation.fulfill()
}
for tab in self.tabManager.tabs(withType: .private) {
guard let webView = tab.webView else {
XCTFail("WebView is not created yet")
return dataStoreExpectation.fulfill()
}
if webView.configuration.websiteDataStore.isPersistent {
XCTFail("Private Tab is storing data persistently!")
return dataStoreExpectation.fulfill()
}
group.enter()
}
group.notify(queue: .main) {
let group = DispatchGroup()
for tab in self.tabManager.tabs(withType: .private) {
guard let webView = tab.webView else {
XCTFail("WebView unexpectedly died")
return dataStoreExpectation.fulfill()
}
group.enter()
webView.configuration.websiteDataStore.fetchDataRecords(
ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(),
completionHandler: { records in
let recordNames = Set<String>(records.compactMap({ URL(string: "http://\($0.displayName)")?.host }))
let urlNames = Set<String>([url.host ?? "FailedTestDomain"])
XCTAssertFalse(urlNames.isSubset(of: recordNames), "Data Store leaking from normal tab to private tab!")
group.leave()
})
}
group.notify(queue: .main) {
dataStoreExpectation.fulfill()
}
}
}
}
wait(for: [dataStoreExpectation], timeout: maxTimeout)
}
}