File: TestNSKeyedUnarchiver.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (125 lines) | stat: -rw-r--r-- 5,283 bytes parent folder | download
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
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//

class TestNSKeyedUnarchiver : XCTestCase {
    private enum SecureTest {
        case skip
        case performWithDefaultClass
        case performWithClasses([AnyClass])
    }
    
    private func test_unarchive_from_file(_ filename : String, _ expectedObject : NSObject, _ secureTest: SecureTest = .skip) throws {
        guard let testFilePath = testBundle().path(forResource: filename, ofType: "plist") else {
            XCTFail("Could not find \(filename)")
            return
        }
        do {
            // Use the path method:
            let object = NSKeyedUnarchiver.unarchiveObject(withFile: testFilePath) as? NSObject
            if let obj = object {
                if expectedObject != obj {
                    print("\(expectedObject) != \(obj)")
                }
            }
            
            XCTAssertEqual(expectedObject, object)
        }
        
        let classes: [AnyClass]
        
        switch secureTest {
        case .skip:
            classes = []
        case .performWithDefaultClass:
            classes = [ (expectedObject as NSObject).classForCoder ]
        case .performWithClasses(let specifiedClasses):
            classes = specifiedClasses
        }

        if !classes.isEmpty {
            // Use the secure method:
            let data = try Data(contentsOf: URL(fileURLWithPath: testFilePath))
            
            let object = try NSKeyedUnarchiver.unarchivedObject(ofClasses: classes, from: data) as? NSObject
            if let obj = object {
                if expectedObject != obj {
                    print("\(expectedObject) != \(obj)")
                }
            }
            
            XCTAssertEqual(expectedObject, object)
        }
    }

    func test_unarchive_array() throws {
        let array = NSArray(array: ["baa", "baa", "black", "sheep"])
        try test_unarchive_from_file("NSKeyedUnarchiver-ArrayTest", array)
    }
    
    func test_unarchive_complex() throws {
        let uuid = NSUUID(uuidString: "71DC068E-3420-45FF-919E-3A267D55EC22")!
        let url = URL(string: "index.xml", relativeTo: URL(string: "https://www.swift.org"))!
        let array = NSArray(array: [ NSNull(), NSString(string: "hello"), NSNumber(value: 34545), NSDictionary(dictionary: ["key" : "val"])])
        let dict : Dictionary<AnyHashable, Any> = [
            "uuid" : uuid,
            "url" : url,
            "string" : "hello",
            "array" : array
        ]
        try test_unarchive_from_file("NSKeyedUnarchiver-ComplexTest", NSDictionary(dictionary: dict), .performWithClasses([NSDictionary.self, NSArray.self, NSURL.self, NSUUID.self, NSNull.self]))
    }
    
    func test_unarchive_concrete_value() throws {
        let array: Array<Int32> = [1, 2, 3]
        let objctype = "[3i]"
        try array.withUnsafeBufferPointer { cArray in
            let concrete = NSValue(bytes: cArray.baseAddress!, objCType: objctype)
            try test_unarchive_from_file("NSKeyedUnarchiver-ConcreteValueTest", concrete, .skip)
        }
    }

// does not yet support isEqual()
//    func test_unarchive_notification() throws {
//        let notification = Notification(name: Notification.Name(rawValue:"notification-name"), object: "notification-object".bridge(),
//                                          userInfo: ["notification-key": "notification-val"])
//        try test_unarchive_from_file("NSKeyedUnarchiver-NotificationTest", notification)
//    }
    
    func test_unarchive_nsedgeinsets_value() throws {
        let edgeinsets = NSEdgeInsets(top: CGFloat(1.0), left: CGFloat(2.0), bottom: CGFloat(3.0), right: CGFloat(4.0))
        try test_unarchive_from_file("NSKeyedUnarchiver-EdgeInsetsTest", NSValue(edgeInsets: edgeinsets))
    }
    
    func test_unarchive_nsrange_value() throws {
        let range = NSRange(location: 97345, length: 98345)
        try test_unarchive_from_file("NSKeyedUnarchiver-RangeTest", NSValue(range: range))
    }
    
    func test_unarchive_nsrect_value() throws {
        let origin = NSPoint(x: CGFloat(400.0), y: CGFloat(300.0))
        let size = NSSize(width: CGFloat(200.0), height: CGFloat(300.0))
        let rect = NSRect(origin: origin, size: size)
        try test_unarchive_from_file("NSKeyedUnarchiver-RectTest", NSValue(rect: rect))
    }
    
    func test_unarchive_ordered_set() throws {
        let set = NSOrderedSet(array: ["valgeir", "nico", "puzzle"])
        try test_unarchive_from_file("NSKeyedUnarchiver-OrderedSetTest", set)
    }
    
    func test_unarchive_url() throws {
        let url = NSURL(string: "foo.xml", relativeTo: URL(string: "https://www.example.com"))
        try test_unarchive_from_file("NSKeyedUnarchiver-URLTest", url!)
    }
    
    func test_unarchive_uuid() throws {
        let uuid = NSUUID(uuidString: "0AD863BA-7584-40CF-8896-BD87B3280C34")
        try test_unarchive_from_file("NSKeyedUnarchiver-UUIDTest", uuid!)
    }
}