this repo has no description
1/*
2* Copyright (c) 2015, 2018 Apple Inc. All rights reserved.
3*
4* @APPLE_LICENSE_HEADER_START@
5*
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
11* file.
12*
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.
20*
21* @APPLE_LICENSE_HEADER_END@
22*/
23
24/*
25* Modification History
26*
27* April 21, 2015 Sushant Chavan
28* - initial revision
29*/
30
31/*
32* A Swift test target to test SC APIs
33*/
34
35import Foundation
36import SystemConfiguration
37import SystemConfiguration_Private
38
39let target_host = "www.apple.com"
40var application = "SCTest-Swift" as CFString
41
42#if !targetEnvironment(simulator)
43func
44 test_SCDynamicStore ()
45{
46 //SCDynamicStore APIs
47 NSLog("\n\n*** SCDynamicStore ***\n\n")
48 let key:CFString
49 let store:SCDynamicStore?
50 let dict:[String:String]?
51 let primaryIntf:String?
52
53 store = SCDynamicStoreCreate(nil, application, nil, nil)
54 if store == nil {
55 NSLog("Error creating session: %s", SCErrorString(SCError()))
56 return
57 }
58
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!)
64 } else {
65 NSLog("Primary interface is unavailable")
66 }
67}
68#endif // !targetEnvironment(simulator)
69
70#if !targetEnvironment(simulator)
71func
72test_SCNetworkConfiguration ()
73{
74 //SCNetworkConfiguration APIs
75 NSLog("\n\n*** SCNetworkConfiguration ***\n\n")
76 let interfaceArray:[CFArray]
77
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)
84 }
85 }
86}
87#endif // !targetEnvironment(simulator)
88
89func
90test_SCNetworkReachability ()
91{
92 //SCNetworkReachability APIs
93 NSLog("\n\n*** SCNetworkReachability ***\n\n")
94 let target:SCNetworkReachability?
95 var flags:SCNetworkReachabilityFlags = SCNetworkReachabilityFlags.init(rawValue: 0)
96
97 target = SCNetworkReachabilityCreateWithName(nil, target_host)
98 if target == nil {
99 NSLog("Error creating target: %s", SCErrorString(SCError()))
100 return
101 }
102
103 SCNetworkReachabilityGetFlags(target!, &flags)
104 NSLog("SCNetworkReachability flags for %@ is %#x", String(target_host), flags.rawValue)
105}
106
107#if !targetEnvironment(simulator)
108func
109test_SCPreferences ()
110{
111 //SCPreferences APIs
112 NSLog("\n\n*** SCPreferences ***\n\n")
113 let prefs:SCPreferences?
114 let networkServices:[CFArray]?
115
116 prefs = SCPreferencesCreate(nil, application, nil)
117 if prefs == nil {
118 NSLog("Error creating prefs: %s", SCErrorString(SCError()))
119 return
120 }
121
122 if let model = SCPreferencesGetValue(prefs!, "Model" as CFString) {
123 NSLog("Current system model is %@", model as! String)
124 }
125
126 networkServices = SCNetworkServiceCopyAll(prefs!) as? [CFArray]
127 if networkServices == nil {
128 NSLog("Error retrieving network services", SCErrorString(SCError()))
129 return
130 }
131
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)
137 }
138
139 }
140}
141#endif // !targetEnvironment(simulator)
142
143func
144my_main ()
145{
146
147#if !targetEnvironment(simulator)
148 test_SCDynamicStore()
149#endif // !targetEnvironment(simulator)
150
151#if !targetEnvironment(simulator)
152 test_SCNetworkConfiguration()
153#endif // !targetEnvironment(simulator)
154
155 test_SCNetworkReachability()
156
157#if !targetEnvironment(simulator)
158 test_SCPreferences()
159#endif // !targetEnvironment(simulator)
160
161}
162
163// Run the test
164my_main()