-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeychainTests.swift
More file actions
294 lines (220 loc) · 7.65 KB
/
Copy pathKeychainTests.swift
File metadata and controls
294 lines (220 loc) · 7.65 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
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
import Foundation
import Testing
@testable import Keychain
@Suite class KeychainTests {
var keychain: MockKeychain = MockKeychain()
@Test
func testCrossStorage() async throws {
// Write it as String
try await keychain.set(string: "abc", for: "key")
// Read it as data
let data = try #require(try await keychain.data(for: "key"))
let result = String(decoding: data, as: UTF8.self)
#expect(result == "abc")
}
@Test
func testStringStorageAndRetrieval() async throws {
let key = "testString"
let value = "Hello, Keychain!"
// Store the value
try await keychain.set(string: value, for: key)
// Retrieve the value
let retrieved = try await keychain.string(for: key)
// Verify
#expect(retrieved == value)
}
@Test
func testStringMethodStorageAndRetrieval() async throws {
let key = "testStringMethod"
let value = "Hello, Keychain Method!"
// Store the value using method
try await keychain.set(string: value, for: key)
// Retrieve the value using method
let retrieved = try await keychain.string(for: key)
// Verify
#expect(retrieved == value)
}
@Test
func testDataStorageAndRetrieval() async throws {
let key = "testData"
let value = Data("Binary Data".utf8)
// Store the value
try await keychain.set(data: value, for: key)
// Retrieve the value
let retrieved = try await keychain.data(for: key)
// Verify
#expect(retrieved == value)
}
@Test
func testDataMethodStorageAndRetrieval() async throws {
let key = "testDataMethod"
let value = Data("Binary Data Method".utf8)
// Store the value using method
try await keychain.set(data: value, for: key)
// Retrieve the value using method
let retrieved = try await keychain.data(for: key)
// Verify
#expect(retrieved == value)
}
@Test
func testBoolStorageAndRetrieval() async throws {
let key = "testBool"
let value = true
// Store the value
try await keychain.set(bool: value, for: key)
// Retrieve the value
let retrieved = try await keychain.bool(for: key)
// Verify
#expect(retrieved == value)
}
@Test
func testBoolMethodStorageAndRetrieval() async throws {
let key = "testBoolMethod"
let value = true
// Store the value using method
try await keychain.set(bool: value, for: key)
// Retrieve the value using method
let retrieved = try await keychain.bool(for: key)
// Verify
#expect(retrieved == value)
}
@Test
func testIntStorageAndRetrieval() async throws {
let key = "testInt"
let value = 42
// Store the value
try await keychain.set(int: value, for: key)
// Retrieve the value
let retrieved = try await keychain.int(for: key)
// Verify
#expect(retrieved == value)
}
@Test
func testIntMethodStorageAndRetrieval() async throws {
let key = "testIntMethod"
let value = 12345
// Store the value using method
try await keychain.set(int: value, for: key)
// Retrieve the value using method
let retrieved = try await keychain.int(for: key)
// Verify
#expect(retrieved == value)
}
@Test
func testGenericValueStorageAndRetrieval() async throws {
let key = "testGenericValue"
let name = "Test User"
let age = 30
let isAdmin = true
// Create dictionary data using individual components
let nameData = try JSONEncoder().encode(name)
let ageData = try JSONEncoder().encode(age)
let isAdminData = try JSONEncoder().encode(isAdmin)
// Store individual components
try await keychain.set(data: nameData, for: "\(key).name")
try await keychain.set(data: ageData, for: "\(key).age")
try await keychain.set(data: isAdminData, for: "\(key).isAdmin")
// Retrieve individual values
let retrievedName = try await JSONDecoder().decode(
String.self, from: keychain.data(for: "\(key).name") ?? Data())
let retrievedAge = try await JSONDecoder().decode(
Int.self, from: keychain.data(for: "\(key).age") ?? Data())
let retrievedIsAdmin = try await JSONDecoder().decode(
Bool.self, from: keychain.data(for: "\(key).isAdmin") ?? Data())
// Verify each component
#expect(retrievedName == name)
#expect(retrievedAge == age)
#expect(retrievedIsAdmin == isAdmin)
}
@Test
func testArrayStorageAndRetrieval() async throws {
let key = "testArrayValue"
let fruits = ["Apple", "Banana", "Orange"]
// Encode array to data
let fruitData = try JSONEncoder().encode(fruits)
// Store data
try await keychain.set(data: fruitData, for: key)
// Retrieve and decode
let retrievedData = try await keychain.data(for: key)
let retrievedFruits = try JSONDecoder().decode([String].self, from: retrievedData ?? Data())
// Verify array contents
#expect(retrievedFruits.count == 3)
#expect(retrievedFruits[0] == "Apple")
#expect(retrievedFruits[1] == "Banana")
#expect(retrievedFruits[2] == "Orange")
}
@Test
func testOverwritingValues() async throws {
let key = "testOverwrite"
// Store initial value
try await keychain.set(string: "Initial Value", for: key)
// Overwrite with new value
try await keychain.set(string: "New Value", for: key)
// Retrieve the value
let retrieved = try await keychain.string(for: key)
// Verify
#expect(retrieved == "New Value")
}
@Test
func testDeletingValues() async throws {
let key = "testDelete"
// Store a value
try await keychain.set(string: "Value to Delete", for: key)
// Verify it was stored
#expect(try await keychain.string(for: key) != nil)
// Delete
try await keychain.removeData(for: key)
// Verify it was deleted
#expect(try await keychain.string(for: key) == nil)
}
@Test
func testDeletingDataValues() async throws {
let key = "testDeleteData"
// Store a value
try await keychain.set(data: Data("Data to Delete".utf8), for: key)
// Verify it was stored
#expect(try await keychain.data(for: key) != nil)
// Delete by setting nil
try await keychain.removeData(for: key)
// Verify it was deleted
#expect(try await keychain.data(for: key) == nil)
}
// MARK: - Reset Functionality Tests
@Test
func testReset() async throws {
let key = "testKey"
// Store a value
try await keychain.set(string: "Test Value", for: key)
// Verify it was stored
#expect(try await keychain.string(for: key) == "Test Value")
// Reset the keychain
try await keychain.reset()
// Verify the key was deleted
#expect(try await keychain.string(for: key) == nil)
}
@Test
func testMultipleDataTypes() async throws {
// Store different data types
try await keychain.set(string: "String Value", for: "stringKey")
try await keychain.set(bool: true, for: "boolKey")
try await keychain.set(data: Data("Data Value".utf8), for: "dataKey")
try await keychain.set(int: 123, for: "intKey")
// Verify all values were stored correctly
#expect(try await keychain.string(for: "stringKey") == "String Value")
#expect(try await keychain.bool(for: "boolKey") == true)
#expect(try await keychain.data(for: "dataKey") == Data("Data Value".utf8))
#expect(try await keychain.int(for: "intKey") == 123)
}
@Test
func testCacheConsistency() async throws {
let key = "cacheTest"
// Store a value
try await keychain.set(string: "Cached Value", for: key)
// Read it multiple times to ensure cache consistency
let firstRead = try await keychain.string(for: key)
let secondRead = try await keychain.string(for: key)
// Verify both reads return the same value
#expect(firstRead == secondRead)
#expect(firstRead == "Cached Value")
}
}