2  *  Copyright (c) 2015 Apple Inc. All rights reserved.
 
   4  *  A Swift test target to test SC APIs
 
   6  *  Created by Sushant Chavan on 4/21/15.
 
  10 import SystemConfiguration
 
  12 let target_host = "www.apple.com"
 
  13 var application = "SCTest-Swift" as CFString
 
  16 test_SCNetworkConfiguration ()
 
  18         //SCNetworkConfiguration APIs
 
  19         NSLog("\n\n*** SCNetworkConfiguration ***\n\n")
 
  20         let interfaceArray:[CFArray]
 
  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)
 
  36 test_SCNetworkReachability ()
 
  38         //SCNetworkReachability APIs
 
  39         NSLog("\n\n*** SCNetworkReachability ***\n\n")
 
  40         let target:SCNetworkReachability?
 
  41         var flags:SCNetworkReachabilityFlags = SCNetworkReachabilityFlags.allZeros
 
  43         target = SCNetworkReachabilityCreateWithName(nil, target_host)
 
  45                 NSLog("Error creating target: %s", SCErrorString(SCError()))
 
  49         SCNetworkReachabilityGetFlags(target!, &flags)
 
  50         NSLog("SCNetworkReachability flags for %@ is %#x", String(target_host), flags.rawValue)
 
  57         NSLog("\n\n*** SCPreferences ***\n\n")
 
  58         let prefs:SCPreferences?
 
  59         let networkServices:[CFArray]?
 
  63         prefs = SCPreferencesCreate(nil, application, nil)
 
  65                 NSLog("Error creating prefs: %s", SCErrorString(SCError()))
 
  69         if let model? = SCPreferencesGetValue(prefs!, "Model" as CFString) {
 
  70                 NSLog("Current system model is %@", model as! String)
 
  73         networkServices = SCNetworkServiceCopyAll(prefs!) as? [CFArray]
 
  74         if networkServices == nil {
 
  75                 NSLog("Error retrieving network services", SCErrorString(SCError()))
 
  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)
 
  91 test_SCDynamicStore ()
 
  94         NSLog("\n\n*** SCDynamicStore ***\n\n")
 
  96         let store:SCDynamicStore?
 
  97         let dict:[String:String]?
 
  98         let primaryIntf:String?
 
 100         store = SCDynamicStoreCreate(nil, application, nil, nil)
 
 102                 NSLog("Error creating session: %s", SCErrorString(SCError()))
 
 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!)
 
 112                 NSLog("Primary interface is unavailable")
 
 119         test_SCNetworkConfiguration()
 
 120         test_SCNetworkReachability()
 
 122         test_SCDynamicStore()