2 * Copyright (c) 2015, 2018 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * April 21, 2015 Sushant Chavan
32 * A Swift test target to test SC APIs
36 import SystemConfiguration
37 import SystemConfiguration_Private
39 let target_host = "www.apple.com"
40 var application = "SCTest-Swift" as CFString
42 #if !targetEnvironment(simulator)
44 test_SCDynamicStore ()
47 NSLog("\n\n*** SCDynamicStore ***\n\n")
49 let store:SCDynamicStore?
50 let dict:[String:String]?
51 let primaryIntf:String?
53 store = SCDynamicStoreCreate(nil, application, nil, nil)
55 NSLog("Error creating session: %s", SCErrorString(SCError()))
59 key = SCDynamicStoreKeyCreateNetworkGlobalEntity(nil, kSCDynamicStoreDomainState, kSCEntNetIPv4)
60 dict = SCDynamicStoreCopyValue(store, key) as? [String:String]
61 primaryIntf = dict?[kSCDynamicStorePropNetPrimaryInterface as String]
62 if (primaryIntf != nil) {
63 NSLog("Primary interface is %@", primaryIntf!)
65 NSLog("Primary interface is unavailable")
68 #endif // !targetEnvironment(simulator)
70 #if !targetEnvironment(simulator)
72 test_SCNetworkConfiguration ()
74 //SCNetworkConfiguration APIs
75 NSLog("\n\n*** SCNetworkConfiguration ***\n\n")
76 let interfaceArray:[CFArray]
78 NSLog("Network Interfaces:")
79 interfaceArray = SCNetworkInterfaceCopyAll() as! [CFArray]
80 for idx in 0...interfaceArray.count {
81 let interface = interfaceArray[idx]
82 if let bsdName = SCNetworkInterfaceGetBSDName(interface as! SCNetworkInterface) {
83 NSLog("- %@", bsdName as String)
87 #endif // !targetEnvironment(simulator)
90 test_SCNetworkReachability ()
92 //SCNetworkReachability APIs
93 NSLog("\n\n*** SCNetworkReachability ***\n\n")
94 let target:SCNetworkReachability?
95 var flags:SCNetworkReachabilityFlags = SCNetworkReachabilityFlags.init(rawValue: 0)
97 target = SCNetworkReachabilityCreateWithName(nil, target_host)
99 NSLog("Error creating target: %s", SCErrorString(SCError()))
103 SCNetworkReachabilityGetFlags(target!, &flags)
104 NSLog("SCNetworkReachability flags for %@ is %#x", String(target_host), flags.rawValue)
107 #if !targetEnvironment(simulator)
109 test_SCPreferences ()
112 NSLog("\n\n*** SCPreferences ***\n\n")
113 let prefs:SCPreferences?
114 let networkServices:[CFArray]?
116 prefs = SCPreferencesCreate(nil, application, nil)
118 NSLog("Error creating prefs: %s", SCErrorString(SCError()))
122 if let model = SCPreferencesGetValue(prefs!, "Model" as CFString) {
123 NSLog("Current system model is %@", model as! String)
126 networkServices = SCNetworkServiceCopyAll(prefs!) as? [CFArray]
127 if networkServices == nil {
128 NSLog("Error retrieving network services", SCErrorString(SCError()))
132 NSLog("Network Services:")
133 for idx in 0...(networkServices?.count)! {
134 let service = networkServices?[idx]
135 if let serviceName = SCNetworkServiceGetName(service as! SCNetworkService) {
136 NSLog("- %@", serviceName as String)
141 #endif // !targetEnvironment(simulator)
147 #if !targetEnvironment(simulator)
148 test_SCDynamicStore()
149 #endif // !targetEnvironment(simulator)
151 #if !targetEnvironment(simulator)
152 test_SCNetworkConfiguration()
153 #endif // !targetEnvironment(simulator)
155 test_SCNetworkReachability()
157 #if !targetEnvironment(simulator)
159 #endif // !targetEnvironment(simulator)