1from CFNetwork import * 2from PyObjCTools.TestSupport import * 3import sys 4import socket 5 6if sys.version_info[0] != 2: 7 def buffer(value): 8 return value.encode('latin1') 9 10class TestCFHost (TestCase): 11 def testTypes(self): 12 self.assertIsCFType(CFHostRef) 13 14 def testConstants(self): 15 self.assertIsInstance(kCFStreamErrorDomainNetDB, (int, long)) 16 self.assertIsInstance(kCFStreamErrorDomainSystemConfiguration, (int, long)) 17 self.assertEqual(kCFHostAddresses, 0) 18 self.assertEqual(kCFHostNames, 1) 19 self.assertEqual(kCFHostReachability, 2) 20 21 def testFunctions(self): 22 self.assertIsInstance(CFHostGetTypeID(), (int, long)) 23 24 self.assertResultIsCFRetained(CFHostCreateWithName) 25 v = CFHostCreateWithName(None, u"www.python.org") 26 self.assertIsInstance(v, CFHostRef) 27 28 try: 29 value = socket.gethostbyname('www.python.org') 30 expected_resolution = True 31 except socket.error: 32 expected_resolution = False 33 34 35 36 addr = ' ' * 24; 37 self.assertResultIsCFRetained(CFHostCreateWithAddress) 38 t = CFHostCreateWithAddress(None, buffer(addr)) 39 self.assertIsInstance(t, CFHostRef) 40 41 self.assertResultIsBOOL(CFHostStartInfoResolution) 42 self.assertArgIsOut(CFHostStartInfoResolution, 2) 43 ok, error = CFHostStartInfoResolution(v, kCFHostAddresses, None) 44 self.assertIsObject(ok, expected_resolution) 45 self.assertIsInstance(error, CFStreamError) 46 47 self.assertResultIsCFRetained(CFHostCreateCopy) 48 w = CFHostCreateCopy(None, v) 49 self.assertIsInstance(w, (type(None), CFHostRef)) 50 51 52 self.assertArgHasType(CFHostGetReachability, 1, b'o^' + objc._C_NSBOOL) 53 lst, ok = CFHostGetReachability(v, None) 54 self.assertIsInstance(lst, (CFDataRef, type(None))) 55 self.assertIsInstance(ok, bool) 56 57 CFHostCancelInfoResolution(v, kCFHostAddresses) 58 59 rl = CFRunLoopGetCurrent() 60 CFHostScheduleWithRunLoop(v, rl, kCFRunLoopDefaultMode) 61 62 CFHostUnscheduleFromRunLoop(v, rl, kCFRunLoopDefaultMode) 63 64 65 self.assertArgHasType(CFHostGetNames, 1, b'o^' + objc._C_NSBOOL) 66 lst, ok = CFHostGetNames(v, None) 67 self.assertIsInstance(lst, CFArrayRef) 68 self.assertIsInstance(ok, bool) 69 70 71 def testCallbacks(self): 72 lst = [] 73 ctx = object() 74 def callback(host, typeinfo, error, ctx): 75 lst.append([host, typeinfo, error, ctx]) 76 77 host = CFHostCreateWithName(None, u"localhost") 78 CFHostSetClient(host, callback, ctx) 79 80 rl = CFRunLoopGetCurrent() 81 CFHostScheduleWithRunLoop(host, rl, kCFRunLoopDefaultMode) 82 83 ok, err = CFHostStartInfoResolution(host, kCFHostAddresses, None) 84 self.assertTrue(ok) 85 self.assertIsInstance(ok, bool) 86 self.assertIsInstance(err, CFStreamError) 87 88 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 4.0, False) 89 90 CFHostUnscheduleFromRunLoop(host, rl, kCFRunLoopDefaultMode) 91 self.assertEqual(len(lst), 1) 92 self.assertIsInstance(lst[0][0], CFHostRef) 93 self.assertIsInstance(lst[0][1], (int, long)) 94 self.assertIsInstance(lst[0][2], CFStreamError) 95 self.assertIsObject(lst[0][3], ctx) 96 97 self.failIfResultIsCFRetained(CFHostGetAddressing) 98 self.assertArgHasType(CFHostGetAddressing, 1, b'o^Z') 99 lst, ok = CFHostGetAddressing(host, None) 100 self.assertIsInstance(lst, CFArrayRef) 101 self.assertIsInstance(lst[0], CFDataRef) 102 self.assertIsInstance(ok, bool) 103 104 105if __name__ == "__main__": 106 main() 107