forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientDeviceServiceTests.swift
More file actions
114 lines (98 loc) · 3.8 KB
/
Copy pathClientDeviceServiceTests.swift
File metadata and controls
114 lines (98 loc) · 3.8 KB
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
import CryptoKit
import XCTest
@testable import Omi_Computer
final class ClientDeviceServiceTests: XCTestCase {
func testDeviceIdHashIsStableEightHexChars() {
let hash = ClientDeviceService.shared.deviceIdHash
XCTAssertEqual(hash.count, 8)
XCTAssertTrue(hash.allSatisfy { $0.isHexDigit })
XCTAssertEqual(hash, ClientDeviceService.shared.deviceIdHash)
}
func testClientDeviceIdUsesMacosPrefix() {
let id = ClientDeviceService.shared.clientDeviceId
XCTAssertTrue(id.hasPrefix("macos_"))
XCTAssertEqual(id, "macos_\(ClientDeviceService.shared.deviceIdHash)")
}
func testNamedDevelopmentBundleUsesBundleScopedInstallId() {
let suiteName = "ClientDeviceServiceTests.named.\(UUID().uuidString)"
let defaults = UserDefaults(suiteName: suiteName)!
defer {
defaults.removePersistentDomain(forName: suiteName)
}
let service = ClientDeviceService(
bundleIdentifier: "com.omi.omi-menubar-logo",
userDefaults: defaults
)
let hash = service.deviceIdHash
XCTAssertEqual(hash.count, 8)
XCTAssertEqual(
hash,
ClientDeviceService(
bundleIdentifier: "com.omi.omi-menubar-logo",
userDefaults: defaults
).deviceIdHash
)
}
func testDesktopDevBundleAlsoUsesUserDefaultsInstallId() {
// Omi Dev must not touch the shared login-keychain device-id item either.
let suiteName = "ClientDeviceServiceTests.dev.\(UUID().uuidString)"
let defaults = UserDefaults(suiteName: suiteName)!
defer {
defaults.removePersistentDomain(forName: suiteName)
}
let service = ClientDeviceService(
bundleIdentifier: AppBuild.desktopDevBundleIdentifier,
userDefaults: defaults
)
_ = service.deviceIdHash
XCTAssertNotNil(defaults.string(forKey: "dev-client-device-install-uuid"))
}
func testNamedDevelopmentBundlesDoNotShareInstallIds() {
let firstSuiteName = "ClientDeviceServiceTests.first.\(UUID().uuidString)"
let secondSuiteName = "ClientDeviceServiceTests.second.\(UUID().uuidString)"
let firstDefaults = UserDefaults(suiteName: firstSuiteName)!
let secondDefaults = UserDefaults(suiteName: secondSuiteName)!
defer {
firstDefaults.removePersistentDomain(forName: firstSuiteName)
secondDefaults.removePersistentDomain(forName: secondSuiteName)
}
let first = ClientDeviceService(
bundleIdentifier: "com.omi.omi-first-feature",
userDefaults: firstDefaults
)
let second = ClientDeviceService(
bundleIdentifier: "com.omi.omi-second-feature",
userDefaults: secondDefaults
)
_ = first.deviceIdHash
_ = second.deviceIdHash
XCTAssertNotEqual(
firstDefaults.string(forKey: "dev-client-device-install-uuid"),
secondDefaults.string(forKey: "dev-client-device-install-uuid")
)
}
func testProductionMigratesMirroredInstallIdWhenScopedKeychainIsMissing() {
let suiteName = "ClientDeviceServiceTests.production-mirror.\(UUID().uuidString)"
let defaults = UserDefaults(suiteName: suiteName)!
let mirror = "stable-install-id-before-scoped-keychain"
defaults.set(mirror, forKey: "client-device-install-uuid-mirror")
defer {
defaults.removePersistentDomain(forName: suiteName)
}
var persistedInstallIds: [String] = []
let service = ClientDeviceService(
bundleIdentifier: AppBuild.productionBundleIdentifier,
userDefaults: defaults,
keychainReader: { .missing },
keychainWriter: { persistedInstallIds.append($0) }
)
let expectedHash = SHA256.hash(data: Data(mirror.utf8))
.map { String(format: "%02x", $0) }
.joined()
.prefix(8)
.description
XCTAssertEqual(service.deviceIdHash, expectedHash)
XCTAssertEqual(persistedInstallIds, [mirror])
XCTAssertEqual(defaults.string(forKey: "client-device-install-uuid-mirror"), mirror)
}
}