forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSKeyedArchival.swift
230 lines (200 loc) · 6.72 KB
/
NSKeyedArchival.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
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
// REQUIRES: objc_interop
import Foundation
import StdlibUnittest
// Since we test various apis with and without errors define these conveniences
func expectError(_ verb: () throws -> ()) {
do {
try verb()
expectUnreachable("Expected to throw error!")
} catch {
// expected
}
}
func expectNoError(_ verb: () throws -> ()) {
do {
try verb()
} catch let error {
expectUnreachable("Did not expect to throw error! \(error)")
}
}
// Utilities for minimizing boiler plate in the tests
@available(OSX 10.11, iOS 9.0, *)
func archive(_ archival: (_: NSKeyedArchiver) throws -> Void) rethrows -> Data {
let data = NSMutableData()
let ka = NSKeyedArchiver(forWritingWith: data)
do {
defer { ka.finishEncoding() }
try archival(ka)
}
expectNotEqual(data.length, 0)
return data as Data
}
@available(OSX 10.11, iOS 9.0, *)
func unarchive(_ data: Data, _ unarchival: (_: NSKeyedUnarchiver) throws -> Void) rethrows -> Void {
let ku = NSKeyedUnarchiver(forReadingWith: data as Data)
ku.requiresSecureCoding = true
ku.decodingFailurePolicy = .setErrorAndReturn
try unarchival(ku)
ku.finishDecoding()
}
// MARK: - Data
// simple struct comprised of entirely plist types
struct Simple : Codable, Equatable {
var number: Int = 5
var string: String = "hello"
var data: Data = Data(bytes: [0, 1, 2, 3])
var array: [String] = ["stay", "a while", "and listen"]
var dictionary: [String:String] = ["Deckard": "Cain"]
static func ==(lhs: Simple, rhs: Simple) -> Bool {
return lhs.number == rhs.number && lhs.string == rhs.string && lhs.data == rhs.data && lhs.array == rhs.array && lhs.dictionary == rhs.dictionary
}
}
// simple struct comprised of entirely plist types
struct ThrowingCodable : Codable {
public func encode(to encoder: Encoder) throws {
throw NSError(domain: "sad", code: 3)
}
}
// MARK - Tests
@available(OSX 10.11, iOS 9.0, *)
func test_simpleCodableSupport() {
let s = Simple()
var data: Data? = nil
expectNoError {
data = try archive { archiver in
try archiver.encodeEncodable(s, forKey: "valid")
try archiver.encodeEncodable(s, forKey: "wrong-type")
}
}
unarchive(data!) { unarchiver in
// confirm we can roundtrip our data
let roundtrip = unarchiver.decodeDecodable(Simple.self, forKey: "valid")
expectNotNil(roundtrip)
if let rt = roundtrip {
expectEqual(rt, s)
}
// also ask for something that is not there
let notThere = unarchiver.decodeDecodable(Simple.self, forKey: "not-there")
expectNil(notThere)
expectNil(unarchiver.error)
// String != Simple so this should fail at the type level
let wrongType = unarchiver.decodeDecodable(String.self, forKey: "wrong-type")
expectNil(wrongType)
expectNotNil(unarchiver.error)
}
}
@available(OSX 10.11, iOS 9.0, *)
func test_encodableErrorHandling() {
expectError {
_ = try archive { archiver in
try archiver.encodeEncodable(ThrowingCodable(), forKey: "non-codable")
}
}
}
@available(OSX 10.11, iOS 9.0, *)
func test_readingNonCodableFromDecodeDecodable() {
var data: Data? = nil
expectNoError {
data = archive { archiver in
archiver.encode(NSDate(), forKey: "non-codable")
}
}
unarchive(data!) { unarchiver in
let nonCodable = unarchiver.decodeDecodable(Simple.self, forKey: "non-codable")
expectNil(nonCodable)
expectNotNil(unarchiver.error)
}
}
@available(OSX 10.11, iOS 9.0, *)
func test_toplevelAPIVariants() {
let s = Simple()
var data: Data? = nil
expectNoError {
data = try archive { archiver in
try archiver.encodeEncodable(s, forKey: "valid")
try archiver.encodeEncodable(Date(), forKey: "non-codable")
}
}
unarchive(data!) { unarchiver in
var caught = false
do {
_ = try unarchiver.decodeTopLevelDecodable(Simple.self, forKey: "non-codable")
} catch {
caught = true
}
expectTrue(caught)
}
expectNoError {
_ = try unarchive(data!) { unarchiver in
let roundtrip = try unarchiver.decodeTopLevelDecodable(Simple.self, forKey: "valid")
expectNotNil(roundtrip)
if let rt = roundtrip {
expectEqual(rt, s)
}
}
}
}
@available(OSX 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *)
func test_unarchiveObjectOfClass() {
let topLevel = NSArray()
let data = NSKeyedArchiver.archivedData(withRootObject: topLevel)
// Should be able to decode an NSArray back out.
expectNoError {
guard let result = try NSKeyedUnarchiver.unarchivedObject(ofClass: NSArray.self, from: data) else {
expectUnreachable("Unable to decode top-level array.")
return
}
expectEqual(result, topLevel)
}
// Shouldn't be able to decode an NSString out of an NSArray.
expectError {
let _ = try NSKeyedUnarchiver.unarchivedObject(ofClass: NSString.self, from: data)
}
}
@available(OSX 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *)
func test_unarchiveObjectOfClasses() {
let topLevel = NSArray()
let data = NSKeyedArchiver.archivedData(withRootObject: topLevel)
// Should be able to unarchive an array back out.
expectNoError {
guard let result = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSArray.self], from: data) as? NSArray else {
expectUnreachable("Unable to decode top-level array.")
return
}
expectEqual(result, topLevel)
}
// Shouldn't be able to decode an NSString out of an NSArray.
expectError {
let _ = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSString.self], from: data)
}
}
// MARK: - Run Tests
#if !FOUNDATION_XCTEST
if #available(OSX 10.11, iOS 9.0, *) {
let NSKeyedArchiverTest = TestSuite("TestNSKeyedArchiver")
var tests = [
"NSKeyedArchival.simpleCodableSupportInNSKeyedArchival": test_simpleCodableSupport,
"NSKeyedArchival.encodableErrorHandling": test_encodableErrorHandling,
"NSKeyedArchival.readingNonCodableFromDecodeDecodable": test_readingNonCodableFromDecodeDecodable,
"NSKeyedArchival.toplevelAPIVariants": test_toplevelAPIVariants
]
if #available(OSX 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *) {
tests["NSKeyedArchival.unarchiveObjectOfClass"] = test_unarchiveObjectOfClass
tests["NSKeyedArchival.unarchiveObjectOfClasses"] = test_unarchiveObjectOfClasses
}
for (name, test) in tests {
NSKeyedArchiverTest.test(name) { test() }
}
runAllTests()
}
#endif