• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /macosx-10.10.1/pyobjc-45/pyobjc/pyobjc-framework-SystemConfiguration-2.5.1/PyObjCTest/
1from PyObjCTools.TestSupport import *
2from SystemConfiguration import *
3import socket
4
5from PyObjCTest.test_scnetwork import resolver_available
6import contextlib
7
8try:
9    long
10except NameError:
11    long = int
12
13class TestSCNetworkReachability (TestCase):
14    def testTypes(self):
15        self.assertIsInstance(SCNetworkReachabilityRef, objc.objc_class)
16
17    @onlyIf(resolver_available(), "No DNS resolver available")
18    def testFunctions(self):
19        self.assertResultIsCFRetained(SCNetworkReachabilityCreateWithAddressPair)
20        v = SCNetworkReachabilityCreateWithAddressPair(None,
21                ('0.0.0.0', 20990),
22                ('www.python.org', 80))
23
24
25        with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sd:
26            sd.listen(5)
27
28            self.assertResultIsCFRetained(SCNetworkReachabilityCreateWithAddress)
29            ref = v = SCNetworkReachabilityCreateWithAddress(None, sd.getsockname())
30            self.assertIsInstance(v, SCNetworkReachabilityRef)
31
32            self.assertResultIsCFRetained(SCNetworkReachabilityCreateWithName)
33            v = SCNetworkReachabilityCreateWithName(None, b'www.python.org')
34            self.assertIsInstance(v, SCNetworkReachabilityRef)
35
36            v = SCNetworkReachabilityGetTypeID()
37            self.assertIsInstance(v, (int, long))
38
39            self.assertResultIsBOOL(SCNetworkReachabilityGetFlags)
40            v, fl = SCNetworkReachabilityGetFlags(ref, None)
41            self.assertTrue(v)
42            self.assertIsInstance(fl, (int, long))
43
44
45            l = []
46            def callout(ref, flags, ctx):
47                l.append([ref, flags, ctx])
48            ctx = object()
49            v = SCNetworkReachabilitySetCallback(ref, callout, ctx)
50            self.assertTrue(v is True)
51
52
53            rl = CFRunLoopGetCurrent()
54            self.assertResultIsBOOL(SCNetworkReachabilityScheduleWithRunLoop)
55            r = SCNetworkReachabilityScheduleWithRunLoop(ref, rl, kCFRunLoopCommonModes)
56
57            CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0, False)
58
59            self.assertResultIsBOOL(SCNetworkReachabilityUnscheduleFromRunLoop)
60            r = SCNetworkReachabilityUnscheduleFromRunLoop(ref, rl, kCFRunLoopCommonModes)
61
62
63if __name__ == "__main__":
64    main()
65