1from CFNetwork import *
2from PyObjCTools.TestSupport import *
3from Foundation import NSString
4
5class TestCFFTPStream (TestCase):
6
7    def testTypes(self):
8        self.assertIsCFType(CFHTTPAuthenticationRef)
9
10    def testConstants(self):
11        self.assertEqual(kCFStreamErrorHTTPAuthenticationTypeUnsupported, -1000)
12        self.assertEqual(kCFStreamErrorHTTPAuthenticationBadUserName, -1001)
13        self.assertEqual(kCFStreamErrorHTTPAuthenticationBadPassword, -1002)
14
15        self.assertIsInstance(kCFHTTPAuthenticationUsername, unicode)
16        self.assertIsInstance(kCFHTTPAuthenticationPassword, unicode)
17        self.assertIsInstance(kCFHTTPAuthenticationAccountDomain, unicode)
18
19    @min_os_level('10.5')
20    #These functions should work on 10.4 as well, but caue a crash in CFNetwork on that platform
21    def testFunctions(self):
22        self.assertIsInstance(CFHTTPAuthenticationGetTypeID(), (int, long))
23
24        msg = CFHTTPMessageCreateResponse(None, 401, "Authenticate", kCFHTTPVersion1_0)
25        self.assertIsInstance(msg, CFHTTPMessageRef)
26        CFHTTPMessageSetHeaderFieldValue(msg, NSString.stringWithString_('WWW-Authenticate'), NSString.stringWithString_('Basic realm="WallyWorld"'))
27
28        self.assertResultIsCFRetained(CFHTTPAuthenticationCreateFromResponse)
29        ref = CFHTTPAuthenticationCreateFromResponse(None, msg)
30        self.assertIsInstance(ref, CFHTTPAuthenticationRef)
31
32        self.assertResultIsBOOL(CFHTTPAuthenticationIsValid)
33        self.assertArgIsOut(CFHTTPAuthenticationIsValid, 1)
34        v, err = CFHTTPAuthenticationIsValid(ref, None)
35        self.assertIsInstance(v, bool)
36        if v:
37            self.assertTrue(err is None)
38        else:
39            self.assertIsInstance(err, CFStreamError)
40
41        self.assertResultIsBOOL(CFHTTPAuthenticationAppliesToRequest)
42        v = CFHTTPAuthenticationAppliesToRequest(ref, msg)
43        self.assertIsInstance(v, bool)
44
45        self.assertResultIsBOOL(CFHTTPAuthenticationRequiresOrderedRequests)
46        v = CFHTTPAuthenticationRequiresOrderedRequests(ref)
47        self.assertIsInstance(v, bool)
48
49        url = CFURLCreateWithString(None, "http://www.python.org/", None)
50        req = CFHTTPMessageCreateRequest(None, "GET", url, "1.0")
51
52        self.assertResultIsBOOL(CFHTTPMessageApplyCredentials)
53        self.assertArgIsOut(CFHTTPMessageApplyCredentials, 4)
54        v, err = CFHTTPMessageApplyCredentials(req, ref, "ronald", "secret", None)
55        if v is True:
56            self.assertEqual(err, None)
57
58        else:
59            self.assertIsInstance(err, CFStreamError)
60
61        self.assertResultIsBOOL(CFHTTPMessageApplyCredentialDictionary)
62        self.assertArgIsOut(CFHTTPMessageApplyCredentialDictionary, 3)
63        v, err = CFHTTPMessageApplyCredentialDictionary(req, ref, {
64                kCFHTTPAuthenticationUsername: 'ronald',
65                kCFHTTPAuthenticationPassword: 'secret',
66            }, None)
67        if v is True:
68            self.assertEqual(err, None)
69        else:
70            self.assertIsInstance(err, CFStreamError)
71
72        self.assertResultIsCFRetained(CFHTTPAuthenticationCopyRealm)
73        self.assertResultHasType(CFHTTPAuthenticationCopyRealm, b'^{__CFString=}')
74        v = CFHTTPAuthenticationCopyRealm(ref)
75        self.assertTrue(v is None or isinstance(v, unicode))
76
77        self.assertResultIsCFRetained(CFHTTPAuthenticationCopyDomains)
78        self.assertResultHasType(CFHTTPAuthenticationCopyDomains, b'^{__CFArray=}')
79        v = CFHTTPAuthenticationCopyDomains(ref)
80        self.assertTrue(v is None or isinstance(v, CFArrayRef))
81
82        self.assertResultIsCFRetained(CFHTTPAuthenticationCopyMethod)
83        self.assertResultHasType(CFHTTPAuthenticationCopyMethod, b'^{__CFString=}')
84        v = CFHTTPAuthenticationCopyMethod(ref)
85        self.assertTrue(v is None or isinstance(v, unicode))
86
87        self.assertResultIsBOOL(CFHTTPAuthenticationRequiresUserNameAndPassword)
88        v = CFHTTPAuthenticationRequiresUserNameAndPassword(ref)
89        self.assertIsInstance(v, bool)
90
91        self.assertResultIsBOOL(CFHTTPAuthenticationRequiresAccountDomain)
92        v = CFHTTPAuthenticationRequiresAccountDomain(ref)
93        self.assertIsInstance(v, bool)
94
95
96
97
98
99
100if __name__ == "__main__":
101    main()
102