]> git.saurik.com Git - apple/configd.git/blob - SCTest-Swift/main.swift
configd-801.1.1.tar.gz
[apple/configd.git] / SCTest-Swift / main.swift
1 /*
2 * Copyright (c) 2015 Apple Inc. All rights reserved.
3 *
4 * A Swift test target to test SC APIs
5 *
6 * Created by Sushant Chavan on 4/21/15.
7 */
8
9 import Foundation
10 import SystemConfiguration
11
12 let target_host = "www.apple.com"
13 var application = "SCTest-Swift" as CFString
14
15 func
16 test_SCNetworkConfiguration ()
17 {
18 //SCNetworkConfiguration APIs
19 NSLog("\n\n*** SCNetworkConfiguration ***\n\n")
20 let interfaceArray:[CFArray]
21 let count:CFIndex
22 var idx:CFIndex
23
24 interfaceArray = SCNetworkInterfaceCopyAll() as! [CFArray]
25 count = CFArrayGetCount(interfaceArray)
26 NSLog("Network Interfaces:")
27 for idx = 0; idx < count ; idx++ {
28 let interface = interfaceArray[idx]
29 if let bsdName? = SCNetworkInterfaceGetBSDName(interface as! SCNetworkInterface) {
30 NSLog("- %@", bsdName as String)
31 }
32 }
33 }
34
35 func
36 test_SCNetworkReachability ()
37 {
38 //SCNetworkReachability APIs
39 NSLog("\n\n*** SCNetworkReachability ***\n\n")
40 let target:SCNetworkReachability?
41 var flags:SCNetworkReachabilityFlags = SCNetworkReachabilityFlags.allZeros
42
43 target = SCNetworkReachabilityCreateWithName(nil, target_host)
44 if target == nil {
45 NSLog("Error creating target: %s", SCErrorString(SCError()))
46 return
47 }
48
49 SCNetworkReachabilityGetFlags(target!, &flags)
50 NSLog("SCNetworkReachability flags for %@ is %#x", String(target_host), flags.rawValue)
51 }
52
53 func
54 test_SCPreferences ()
55 {
56 //SCPreferences APIs
57 NSLog("\n\n*** SCPreferences ***\n\n")
58 let prefs:SCPreferences?
59 let networkServices:[CFArray]?
60 let count:CFIndex
61 var idx:CFIndex
62
63 prefs = SCPreferencesCreate(nil, application, nil)
64 if prefs == nil {
65 NSLog("Error creating prefs: %s", SCErrorString(SCError()))
66 return
67 }
68
69 if let model? = SCPreferencesGetValue(prefs!, "Model" as CFString) {
70 NSLog("Current system model is %@", model as! String)
71 }
72
73 networkServices = SCNetworkServiceCopyAll(prefs!) as? [CFArray]
74 if networkServices == nil {
75 NSLog("Error retrieving network services", SCErrorString(SCError()))
76 return
77 }
78
79 count = CFArrayGetCount(networkServices)
80 NSLog("Network Services:")
81 for idx = 0; idx < count ; idx++ {
82 let service = networkServices?[idx]
83 if let serviceName? = SCNetworkServiceGetName(service as! SCNetworkService) {
84 NSLog("- %@", serviceName as String)
85 }
86
87 }
88 }
89
90 func
91 test_SCDynamicStore ()
92 {
93 //SCDynamicStore APIs
94 NSLog("\n\n*** SCDynamicStore ***\n\n")
95 let key:CFString
96 let store:SCDynamicStore?
97 let dict:[String:String]?
98 let primaryIntf:String?
99
100 store = SCDynamicStoreCreate(nil, application, nil, nil)
101 if store == nil {
102 NSLog("Error creating session: %s", SCErrorString(SCError()))
103 return
104 }
105
106 key = SCDynamicStoreKeyCreateNetworkGlobalEntity(nil, kSCDynamicStoreDomainState, kSCEntNetIPv4)
107 dict = SCDynamicStoreCopyValue(store, key) as? [String:String]
108 primaryIntf = dict?[kSCDynamicStorePropNetPrimaryInterface as String]
109 if (primaryIntf != nil) {
110 NSLog("Primary interface is %@", primaryIntf!)
111 } else {
112 NSLog("Primary interface is unavailable")
113 }
114 }
115
116 func
117 my_main ()
118 {
119 test_SCNetworkConfiguration()
120 test_SCNetworkReachability()
121 test_SCPreferences()
122 test_SCDynamicStore()
123 }
124
125 // Run the test
126 my_main()