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 Objective-C test target to test SC APIs
33 */
34
35#import <TargetConditionals.h>
36@import Foundation;
37@import SystemConfiguration;
38@import SystemConfiguration_Private;
39
40#define MY_APP_NAME CFSTR("SCTestObjC")
41#define TARGET_HOST "www.apple.com"
42
43
44#if !TARGET_OS_SIMULATOR
45static void
46test_SCDynamicStore()
47{
48 NSLog(@"\n\n*** SCDynamicStore ***\n\n");
49 CFDictionaryRef dict;
50 CFStringRef intf;
51 CFStringRef key;
52 SCDynamicStoreRef store;
53
54 store = SCDynamicStoreCreate(NULL, MY_APP_NAME, NULL, NULL);
55 key = SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL, kSCDynamicStoreDomainState, kSCEntNetIPv4);
56 dict = SCDynamicStoreCopyValue(store, key);
57 intf = CFDictionaryGetValue(dict, kSCDynamicStorePropNetPrimaryInterface);
58 NSLog(@"- Primary Interface is %@\n", intf);
59
60 CFRelease(store);
61 CFRelease(dict);
62 CFRelease(key);
63}
64#endif // !TARGET_OS_SIMULATOR
65
66#if !TARGET_OS_SIMULATOR
67static void
68test_SCNetworkConfiguration()
69{
70 NSLog(@"\n\n*** SCNetworkConfiguration ***\n\n");
71 CFIndex count;
72 CFIndex idx;
73 CFArrayRef interfaces;
74
75 interfaces = SCNetworkInterfaceCopyAll();
76 count = CFArrayGetCount(interfaces);
77 NSLog(@"Network Interfaces:\n");
78 for (idx=0; idx < count; idx++) {
79 SCNetworkInterfaceRef intf;
80 CFStringRef bsdName;
81
82 intf = CFArrayGetValueAtIndex(interfaces, idx);
83 bsdName = SCNetworkInterfaceGetBSDName(intf);
84 NSLog(@"- %@", bsdName);
85 }
86
87 CFRelease(interfaces);
88}
89#endif // !TARGET_OS_SIMULATOR
90
91void
92test_SCNetworkReachability()
93{
94 NSLog(@"\n\n*** SCNetworkReachability ***\n\n");
95 SCNetworkReachabilityFlags flags;
96 SCNetworkReachabilityRef target;
97
98 target = SCNetworkReachabilityCreateWithName(NULL, TARGET_HOST);
99 (void)SCNetworkReachabilityGetFlags(target, &flags);
100 NSLog(@"- Reachability flags for "TARGET_HOST": %#x", flags);
101
102 CFRelease(target);
103}
104
105#if !TARGET_OS_SIMULATOR
106static void
107test_SCPreferences()
108{
109 NSLog(@"\n\n*** SCPreferences ***\n\n");
110 CFIndex count;
111 CFIndex idx;
112 CFStringRef model = NULL;
113 SCPreferencesRef prefs;
114 CFArrayRef services;
115
116 prefs = SCPreferencesCreate(NULL, MY_APP_NAME, NULL);
117 model = SCPreferencesGetValue(prefs, CFSTR("Model"));
118 if (model != NULL) {
119 NSLog(@"Current model is %@", model);
120 }
121
122 services = SCNetworkServiceCopyAll(prefs);
123 count = CFArrayGetCount(services);
124 NSLog(@"Network Services:\n");
125 for (idx = 0; idx < count; idx++) {
126 SCNetworkServiceRef serv;
127 CFStringRef servName;
128
129 serv = CFArrayGetValueAtIndex(services, idx);
130 servName = SCNetworkServiceGetName(serv);
131 NSLog(@"- %@\n", servName);
132 }
133
134 CFRelease(prefs);
135 CFRelease(services);
136}
137#endif // !TARGET_OS_SIMULATOR
138
139void
140SCTest()
141{
142
143#if !TARGET_OS_SIMULATOR
144 test_SCDynamicStore();
145#endif // !TARGET_OS_SIMULATOR
146
147#if !TARGET_OS_SIMULATOR
148 test_SCNetworkConfiguration();
149#endif // !TARGET_OS_SIMULATOR
150
151 test_SCNetworkReachability();
152
153#if !TARGET_OS_SIMULATOR
154 test_SCPreferences();
155#endif // !TARGET_OS_SIMULATOR
156
157}
158
159int main(int argc, const char * argv[]) {
160#pragma unused(argc, argv)
161 @autoreleasepool {
162 SCTest();
163 }
164 return 0;
165}