]> git.saurik.com Git - apple/configd.git/blob - SCTest-ObjC/test-objC.m
configd-1109.60.2.tar.gz
[apple/configd.git] / SCTest-ObjC / test-objC.m
1 /*
2 * Copyright (c) 2015, 2018, 2020 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
37 #if !defined(USING_PUBLIC_SDK)
38 @import Foundation;
39 @import SystemConfiguration;
40 @import SystemConfiguration_Private;
41 #else // !defined(USING_PUBLIC_SDK)
42 #include <Foundation/Foundation.h>
43 #include <SystemConfiguration/SystemConfiguration.h>
44 #endif // !defined(USING_PUBLIC_SDK)
45
46 #if TARGET_OS_MACCATALYST
47 #pragma message "Building for IOS_MAC"
48 #endif
49
50 #define MY_APP_NAME CFSTR("SCTestObjC")
51 #define TARGET_HOST "www.apple.com"
52
53
54 #if !TARGET_OS_SIMULATOR && !defined(USING_PUBLIC_SDK)
55 static void
56 test_SCDynamicStore()
57 {
58 NSLog(@"\n\n*** SCDynamicStore ***\n\n");
59 CFDictionaryRef dict;
60 CFStringRef intf;
61 CFStringRef key;
62 SCDynamicStoreRef store;
63
64 store = SCDynamicStoreCreate(NULL, MY_APP_NAME, NULL, NULL);
65 key = SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL, kSCDynamicStoreDomainState, kSCEntNetIPv4);
66 dict = SCDynamicStoreCopyValue(store, key);
67 intf = CFDictionaryGetValue(dict, kSCDynamicStorePropNetPrimaryInterface);
68 NSLog(@"- Primary Interface is %@\n", intf);
69
70 CFRelease(store);
71 CFRelease(dict);
72 CFRelease(key);
73 }
74 #endif // !TARGET_OS_SIMULATOR && !defined(USING_PUBLIC_SDK)
75
76 #if !TARGET_OS_SIMULATOR && !defined(USING_PUBLIC_SDK)
77 static void
78 test_SCNetworkConfiguration()
79 {
80 NSLog(@"\n\n*** SCNetworkConfiguration ***\n\n");
81 CFIndex count;
82 CFIndex idx;
83 CFArrayRef interfaces;
84
85 interfaces = SCNetworkInterfaceCopyAll();
86 count = CFArrayGetCount(interfaces);
87 NSLog(@"Network Interfaces:\n");
88 for (idx=0; idx < count; idx++) {
89 SCNetworkInterfaceRef intf;
90 CFStringRef bsdName;
91
92 intf = CFArrayGetValueAtIndex(interfaces, idx);
93 bsdName = SCNetworkInterfaceGetBSDName(intf);
94 NSLog(@"- %@", bsdName);
95 }
96
97 CFRelease(interfaces);
98 }
99 #endif // !TARGET_OS_SIMULATOR && !defined(USING_PUBLIC_SDK)
100
101 void
102 test_SCNetworkReachability()
103 {
104 NSLog(@"\n\n*** SCNetworkReachability ***\n\n");
105 SCNetworkReachabilityFlags flags;
106 SCNetworkReachabilityRef target;
107
108 target = SCNetworkReachabilityCreateWithName(NULL, TARGET_HOST);
109 (void)SCNetworkReachabilityGetFlags(target, &flags);
110 NSLog(@"- Reachability flags for "TARGET_HOST": %#x", flags);
111
112 CFRelease(target);
113 }
114
115 #if !TARGET_OS_SIMULATOR && !defined(USING_PUBLIC_SDK)
116 static void
117 test_SCPreferences()
118 {
119 NSLog(@"\n\n*** SCPreferences ***\n\n");
120 CFIndex count;
121 CFIndex idx;
122 CFStringRef model = NULL;
123 SCPreferencesRef prefs;
124 CFArrayRef services;
125
126 prefs = SCPreferencesCreate(NULL, MY_APP_NAME, NULL);
127 model = SCPreferencesGetValue(prefs, CFSTR("Model"));
128 if (model != NULL) {
129 NSLog(@"Current model is %@", model);
130 }
131
132 services = SCNetworkServiceCopyAll(prefs);
133 count = CFArrayGetCount(services);
134 NSLog(@"Network Services:\n");
135 for (idx = 0; idx < count; idx++) {
136 SCNetworkServiceRef serv;
137 CFStringRef servName;
138
139 serv = CFArrayGetValueAtIndex(services, idx);
140 servName = SCNetworkServiceGetName(serv);
141 NSLog(@"- %@\n", servName);
142 }
143
144 CFRelease(prefs);
145 CFRelease(services);
146 }
147 #endif // !TARGET_OS_SIMULATOR && !defined(USING_PUBLIC_SDK)
148
149 void
150 SCTest()
151 {
152
153 #if !TARGET_OS_SIMULATOR && !defined(USING_PUBLIC_SDK)
154 test_SCDynamicStore();
155 #endif // !TARGET_OS_SIMULATOR && !defined(USING_PUBLIC_SDK)
156
157 #if !TARGET_OS_SIMULATOR && !defined(USING_PUBLIC_SDK)
158 test_SCNetworkConfiguration();
159 #endif // !TARGET_OS_SIMULATOR && !defined(USING_PUBLIC_SDK)
160
161 test_SCNetworkReachability();
162
163 #if !TARGET_OS_SIMULATOR && !defined(USING_PUBLIC_SDK)
164 test_SCPreferences();
165 #endif // !TARGET_OS_SIMULATOR && !defined(USING_PUBLIC_SDK)
166
167 }
168
169 int
170 main(int argc, const char * argv[]) {
171 #pragma unused(argc, argv)
172
173 #if TARGET_OS_MACCATALYST
174 #if !defined(USING_PUBLIC_SDK)
175 #include <CoreFoundation/CFPriv.h>
176 #else // !defined(USING_PUBLIC_SDK)
177 extern Boolean _CFMZEnabled(void);
178 #endif // !defined(USING_PUBLIC_SDK)
179 if (_CFMZEnabled()) {
180 NSLog(@"*** IOS_MAC ***\n");
181 }
182 #endif
183
184 @autoreleasepool {
185 SCTest();
186 }
187 return 0;
188 }