/
AutofillVaultUserScriptTests.swift
487 lines (367 loc) · 19.2 KB
/
AutofillVaultUserScriptTests.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
//
// AutofillVaultUserScriptTests.swift
// DuckDuckGo
//
// Copyright © 2021 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import XCTest
import WebKit
@testable import BrowserServicesKit
// swiftlint:disable type_body_length
// swiftlint:disable file_length
class AutofillVaultUserScriptTests: XCTestCase {
let userScript: AutofillUserScript = {
let embeddedConfig =
"""
{
"features": {
"autofill": {
"status": "enabled",
"exceptions": []
}
},
"unprotectedTemporary": []
}
""".data(using: .utf8)!
let privacyConfig = AutofillTestHelper.preparePrivacyConfig(embeddedConfig: embeddedConfig)
let properties = ContentScopeProperties(gpcEnabled: false, sessionKey: "1234", featureToggles: ContentScopeFeatureToggles.allTogglesOn)
let sourceProvider = DefaultAutofillSourceProvider(privacyConfigurationManager: privacyConfig,
properties: properties)
return AutofillUserScript(scriptSourceProvider: sourceProvider)
}()
let userContentController = WKUserContentController()
var encryptedMessagingParams: [String: Any] {
return [
"messageHandling": [
"iv": Array(repeating: UInt8(1), count: 32),
"key": Array(repeating: UInt8(1), count: 32),
"secret": userScript.generatedSecret,
"methodName": "test-methodName"
]
]
}
@available(macOS 11, iOS 14, *)
func testWhenAccountsForDomainRequested_ThenDelegateCalled() {
class GetAccountsDelegate: MockSecureVaultDelegate {
override func autofillUserScript(_: AutofillUserScript,
didRequestAccountsForDomain domain: String,
completionHandler: @escaping ([SecureVaultModels.WebsiteAccount]) -> Void) {
completionHandler([
SecureVaultModels.WebsiteAccount(id: 1, username: "1@example.com", domain: "domain", created: Date(), lastUpdated: Date()),
SecureVaultModels.WebsiteAccount(id: 2, username: "2@example.com", domain: "domain", created: Date(), lastUpdated: Date())
])
}
}
let delegate = GetAccountsDelegate()
userScript.vaultDelegate = delegate
let mockWebView = MockWebView()
let message = MockWKScriptMessage(name: "pmHandlerGetAccounts", body: encryptedMessagingParams, webView: mockWebView)
let expect = expectation(description: #function)
userScript.userContentController(userContentController, didReceive: message) {
XCTAssertNotNil($0)
XCTAssertNil($1)
let data = ($0 as? String)?.data(using: .utf8)
let response = try? JSONDecoder().decode(AutofillUserScript.RequestVaultAccountsResponse.self, from: data!)
XCTAssertEqual(response?.success.count, 2)
expect.fulfill()
}
waitForExpectations(timeout: 1.0)
}
@available(macOS 11, iOS 14, *)
func testWhenCredentialForAccountRequested_ThenDelegateCalled() {
class GetCredentialsDelegate: MockSecureVaultDelegate {
override func autofillUserScript(_: AutofillUserScript,
didRequestCredentialsForAccount accountId: Int64,
completionHandler: @escaping (SecureVaultModels.WebsiteCredentials?) -> Void) {
completionHandler(.init(account: .init(id: accountId,
username: "1@example.com",
domain: "example.com",
created: Date(),
lastUpdated: Date()),
password: "password".data(using: .utf8)!))
}
}
let randomAccountId = Int.random(in: 0 ..< Int.max) // JS will come through as a Int rather than Int64
let delegate = GetCredentialsDelegate()
userScript.vaultDelegate = delegate
var body = encryptedMessagingParams
body["id"] = "\(randomAccountId)"
let mockWebView = MockWebView()
let message = MockWKScriptMessage(name: "pmHandlerGetAutofillCredentials", body: body, webView: mockWebView)
let expect = expectation(description: #function)
userScript.userContentController(userContentController, didReceive: message) {
XCTAssertNotNil($0)
XCTAssertNil($1)
let data = ($0 as? String)?.data(using: .utf8)
let response = try? JSONDecoder().decode(AutofillUserScript.RequestVaultCredentialsForAccountResponse.self, from: data!)
XCTAssertEqual(Int64(response!.success.id), Int64(randomAccountId))
expect.fulfill()
}
waitForExpectations(timeout: 1.0)
}
@available(macOS 11, iOS 14, *)
func testWhenStoreDataCalled_ThenDelegateIsCalled() {
let delegate = MockSecureVaultDelegate()
userScript.vaultDelegate = delegate
var body = encryptedMessagingParams
body["credentials"] = ["username": "username@example.com", "password": "password"]
let mockWebView = MockWebView()
let message = MockAutofillMessage(name: "pmHandlerStoreData", body: body,
host: "example.com", webView: mockWebView)
userScript.processMessage(userContentController, didReceive: message)
XCTAssertEqual(delegate.lastDomain, "example.com")
XCTAssertEqual(delegate.lastUsername, "username@example.com")
XCTAssertEqual(delegate.lastPassword, "password")
}
@available(macOS 11, iOS 14, *)
func testWhenCreditCardForIdIsRequested_ThenDelegateIsCalled() {
class GetCreditCardDelegate: MockSecureVaultDelegate {
override func autofillUserScript(_: AutofillUserScript,
didRequestCreditCardWithId creditCardId: Int64,
completionHandler: @escaping (SecureVaultModels.CreditCard?) -> Void) {
completionHandler(.init(id: creditCardId,
title: "Mock Card",
cardNumber: "1234123412341234",
cardholderName: "Dax",
cardSecurityCode: "123",
expirationMonth: 11,
expirationYear: 2021))
}
}
let randomCardId = Int.random(in: 0 ..< Int.max)
let delegate = GetCreditCardDelegate()
userScript.vaultDelegate = delegate
var body = encryptedMessagingParams
body["id"] = "\(randomCardId)"
let mockWebView = MockWebView()
let message = MockWKScriptMessage(name: "pmHandlerGetCreditCard", body: body, webView: mockWebView)
let expect = expectation(description: #function)
userScript.userContentController(userContentController, didReceive: message) {
XCTAssertNotNil($0)
XCTAssertNil($1)
let data = ($0 as? String)?.data(using: .utf8)
let response = try? JSONDecoder().decode(AutofillUserScript.RequestAutoFillCreditCardResponse.self, from: data!)
XCTAssertEqual(response?.success.id, Int64(randomCardId))
expect.fulfill()
}
waitForExpectations(timeout: 1.0)
}
@available(macOS 11, iOS 14, *)
func testWhenIdentityForIdIsRequested_ThenDelegateIsCalled() {
class GetCreditCardDelegate: MockSecureVaultDelegate {
override func autofillUserScript(_: AutofillUserScript,
didRequestIdentityWithId identityId: Int64,
completionHandler: @escaping (SecureVaultModels.Identity?) -> Void) {
completionHandler(.init(id: identityId,
title: "Identity",
created: Date(),
lastUpdated: Date(),
firstName: "Dax",
middleName: nil,
lastName: nil,
birthdayDay: 1,
birthdayMonth: 2,
birthdayYear: 3,
addressStreet: nil,
addressCity: nil,
addressProvince: nil,
addressPostalCode: nil,
addressCountryCode: nil,
homePhone: nil,
mobilePhone: nil,
emailAddress: nil))
}
}
let randomIdentityId = Int.random(in: 0 ..< Int.max)
let delegate = GetCreditCardDelegate()
userScript.vaultDelegate = delegate
var body = encryptedMessagingParams
body["id"] = "\(randomIdentityId)"
let mockWebView = MockWebView()
let message = MockWKScriptMessage(name: "pmHandlerGetIdentity", body: body, webView: mockWebView)
let expect = expectation(description: #function)
userScript.userContentController(userContentController, didReceive: message) {
XCTAssertNotNil($0)
XCTAssertNil($1)
let data = ($0 as? String)?.data(using: .utf8)
let response = try? JSONDecoder().decode(AutofillUserScript.RequestAutoFillIdentityResponse.self, from: data!)
XCTAssertEqual(response?.success.id, Int64(randomIdentityId))
expect.fulfill()
}
waitForExpectations(timeout: 1.0)
}
func testWhenShowPasswordManagementUIIsCalled_ThenDelegateIsCalled() {
let delegate = MockSecureVaultDelegate()
userScript.vaultDelegate = delegate
let mockWebView = MockWebView()
let message = MockAutofillMessage(name: "pmHandlerOpenManagePasswords", body: encryptedMessagingParams,
host: "example.com", webView: mockWebView)
userScript.processMessage(userContentController, didReceive: message)
XCTAssertEqual(delegate.lastDomain, "example.com")
}
func testWhenShowCardManagementUIIsCalled_ThenDelegateIsCalled() {
let delegate = MockSecureVaultDelegate()
userScript.vaultDelegate = delegate
let mockWebView = MockWebView()
let message = MockAutofillMessage(name: "pmHandlerOpenManageCreditCards", body: encryptedMessagingParams,
host: "example.com", webView: mockWebView)
userScript.processMessage(userContentController, didReceive: message)
XCTAssertEqual(delegate.lastDomain, "example.com")
}
func testWhenShowIdentityManagementUIIsCalled_ThenDelegateIsCalled() {
let delegate = MockSecureVaultDelegate()
userScript.vaultDelegate = delegate
let mockWebView = MockWebView()
let message = MockAutofillMessage(name: "pmHandlerOpenManageIdentities", body: encryptedMessagingParams,
host: "example.com", webView: mockWebView)
userScript.processMessage(userContentController, didReceive: message)
XCTAssertEqual(delegate.lastDomain, "example.com")
}
func testWhenInitializingAutofillData_WhenCredentialsAreProvidedWithoutAUsername_ThenAutofillDataIsStillInitialized() {
let password = "password"
let detectedAutofillData = [
"credentials": [
"password": password
]
]
let autofillData = AutofillUserScript.DetectedAutofillData(dictionary: detectedAutofillData)
XCTAssertNil(autofillData.creditCard)
XCTAssertNil(autofillData.identity)
XCTAssertNotNil(autofillData.credentials)
XCTAssertEqual(autofillData.credentials?.username, nil)
XCTAssertEqual(autofillData.credentials?.password, password)
}
func testWhenInitializingAutofillData_WhenCredentialsAreProvidedWithAUsername_ThenAutofillDataIsStillInitialized() {
let username = "username"
let password = "password"
let detectedAutofillData = [
"credentials": [
"username": username,
"password": password
]
]
let autofillData = AutofillUserScript.DetectedAutofillData(dictionary: detectedAutofillData)
XCTAssertNil(autofillData.creditCard)
XCTAssertNil(autofillData.identity)
XCTAssertNotNil(autofillData.credentials)
XCTAssertEqual(autofillData.credentials?.username, username)
XCTAssertEqual(autofillData.credentials?.password, password)
}
func testWhenGetAutofilldataIsCall_ThenMainAndSubtypesAreUsed() {
let delegate = MockSecureVaultDelegate()
userScript.vaultDelegate = delegate
var body = encryptedMessagingParams
body["mainType"] = "credentials"
body["subType"] = "username"
body["trigger"] = "userInitiated"
let mockWebView = MockWebView()
let message = MockAutofillMessage(name: "getAutofillData", body: body, host: "example.com", webView: mockWebView)
userScript.processMessage(userContentController, didReceive: message)
let predicate = NSPredicate(block: { _, _ -> Bool in
return !delegate.receivedCallbacks.isEmpty
})
let expectation = XCTNSPredicateExpectation(predicate: predicate, object: delegate.receivedCallbacks)
wait(for: [expectation], timeout: 5)
XCTAssertEqual(delegate.lastSubtype, AutofillUserScript.GetAutofillDataSubType.username)
}
func testWhenGetAutofilldataIsCalledWithUnknownMainType_ThenTheMessageIsIgnored() {
let delegate = MockSecureVaultDelegate()
userScript.vaultDelegate = delegate
var body = encryptedMessagingParams
body["mainType"] = "creditCards" // <- unsupported main type
body["subType"] = "username"
body["trigger"] = "userInitiated"
let mockWebView = MockWebView()
let message = MockAutofillMessage(name: "getAutofillData", body: body, host: "example.com", webView: mockWebView)
userScript.processMessage(userContentController, didReceive: message)
XCTAssertNil(delegate.lastSubtype)
}
func testWhenGetAutofilldataIsCalledWithUnknownSubType_ThenTheMessageIsIgnored() {
let delegate = MockSecureVaultDelegate()
userScript.vaultDelegate = delegate
var body = encryptedMessagingParams
body["mainType"] = "credentials"
body["subType"] = "anything_else"
body["trigger"] = "userInitiated"
let mockWebView = MockWebView()
let message = MockAutofillMessage(name: "getAutofillData", body: body, host: "example.com", webView: mockWebView)
userScript.processMessage(userContentController, didReceive: message)
XCTAssertNil(delegate.lastSubtype)
}
}
class MockSecureVaultDelegate: AutofillSecureVaultDelegate {
enum CallbackType {
case didRequestPasswordManagerForDomain
case didRequestStoreDataForDomain
case didRequestAccountsForDomain
case didRequestCredentialsForDomain
}
var receivedCallbacks: [CallbackType] = []
var lastDomain: String?
var lastUsername: String?
var lastPassword: String?
var lastSubtype: AutofillUserScript.GetAutofillDataSubType?
func autofillUserScript(_: AutofillUserScript, didRequestPasswordManagerForDomain domain: String) {
lastDomain = domain
receivedCallbacks.append(.didRequestPasswordManagerForDomain)
}
func autofillUserScript(_: AutofillUserScript, didRequestStoreDataForDomain domain: String, data: AutofillUserScript.DetectedAutofillData) {
lastDomain = domain
lastUsername = data.credentials?.username
lastPassword = data.credentials?.password
receivedCallbacks.append(.didRequestStoreDataForDomain)
}
func autofillUserScript(_: AutofillUserScript,
didRequestAccountsForDomain domain: String,
completionHandler: @escaping ([SecureVaultModels.WebsiteAccount]) -> Void) {
lastDomain = domain
receivedCallbacks.append(.didRequestAccountsForDomain)
}
func autofillUserScript(_: AutofillUserScript,
didRequestCredentialsForAccount accountId: Int64,
completionHandler: @escaping (SecureVaultModels.WebsiteCredentials?) -> Void) {
}
func autofillUserScript(_: AutofillUserScript,
didRequestAutoFillInitDataForDomain domain: String,
completionHandler: @escaping ([SecureVaultModels.WebsiteAccount],
[SecureVaultModels.Identity],
[SecureVaultModels.CreditCard]) -> Void) {
}
func autofillUserScript(_: AutofillUserScript,
didRequestCreditCardWithId creditCardId: Int64,
completionHandler: @escaping (SecureVaultModels.CreditCard?) -> Void) {
}
func autofillUserScript(_: AutofillUserScript,
didRequestIdentityWithId identityId: Int64,
completionHandler: @escaping (SecureVaultModels.Identity?) -> Void) {
}
func autofillUserScript(_ script: AutofillUserScript,
didRequestCredentialsForDomain: String,
subType: AutofillUserScript.GetAutofillDataSubType,
trigger: AutofillUserScript.GetTriggerType,
completionHandler: @escaping (SecureVaultModels.WebsiteCredentials?, RequestVaultCredentialsAction) -> Void) {
lastSubtype = subType
receivedCallbacks.append(.didRequestCredentialsForDomain)
completionHandler(nil, .none)
}
}
struct NoneEncryptingEncrypter: AutofillEncrypter {
func encryptReply(_ reply: String, key: [UInt8], iv: [UInt8]) throws -> (ciphertext: Data, tag: Data) {
return (reply.data(using: .utf8)!, Data())
}
}
// swiftlint:enable type_body_length
// swiftlint:enable file_length