~ubuntu-branches/ubuntu/quantal/python-gevent/quantal

« back to all changes in this revision

Viewing changes to greentest/test__socket_dns.py

  • Committer: Bazaar Package Importer
  • Author(s): Örjan Persson
  • Date: 2011-05-17 16:43:20 UTC
  • mfrom: (6.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20110517164320-nwhld2xo6sz58p4m
Tags: 0.13.6-1
New upstream version (Closes: #601863).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
2
3
import sys
3
4
import re
4
5
import greentest
7
8
 
8
9
 
9
10
ACCEPTED_GAIERROR_MISMATCH = {
10
 
    "gaierror(-5, 'No address associated with hostname')": "DNSError(3, 'name does not exist')"
 
11
    "gaierror(-5, 'No address associated with hostname')":
 
12
    ("DNSError(3, 'name does not exist')", "DNSError(66, 'unknown')"),
11
13
}
12
14
 
13
15
assert gaierror is real_socket.gaierror
14
16
assert error is real_socket.error
15
17
 
16
18
VERBOSE = '-v' in sys.argv
 
19
IGNORE_ERRORS = '--ignore' in sys.argv
 
20
DEBUG = '-d' in sys.argv
 
21
for arg in ['--ignore', '-d']:
 
22
    try:
 
23
        sys.argv.remove(arg)
 
24
    except ValueError:
 
25
        pass
17
26
 
18
27
 
19
28
class TestCase(greentest.TestCase):
 
29
    __timeout__ = 30
 
30
 
 
31
    if IGNORE_ERRORS:
 
32
        def assertEqual(self, a, b):
 
33
            if a != b:
 
34
                print 'ERROR: %r != %r' % (a, b)
20
35
 
21
36
    def _test(self, hostname, check_ip=None):
22
37
        self._test_gethostbyname(hostname, check_ip=check_ip)
28
43
                print 'real_socket.gethostbyname(%r)' % (hostname, )
29
44
            real_ip = real_socket.gethostbyname(hostname)
30
45
        except Exception, ex:
 
46
            if DEBUG:
 
47
                raise
31
48
            real_ip = ex
32
49
        try:
33
50
            if VERBOSE:
34
51
                print 'gevent.socket.gethostbyname(%r)' % (hostname, )
35
52
            ip = gethostbyname(hostname)
36
53
        except Exception, ex:
 
54
            if DEBUG:
 
55
                raise
37
56
            ip = ex
38
57
        if self.equal(real_ip, ip):
39
58
            return ip
42
61
            self.assertEqual(check_ip, ip)
43
62
        return ip
44
63
 
45
 
    PORTS = [80, 0, 53]
 
64
    PORTS = [80, 0, 53, 'http']
46
65
    getaddrinfo_args = [(),
47
66
                        (AF_UNSPEC, ),
48
67
                        (AF_UNSPEC, SOCK_STREAM, 0, 0),
52
71
                        (AF_UNSPEC, SOCK_STREAM, 6),
53
72
                        (AF_INET, SOCK_DGRAM, 17)]
54
73
 
55
 
 
56
74
    def _test_getaddrinfo(self, hostname):
57
75
        for port in self.PORTS:
58
76
            for args in self.getaddrinfo_args:
59
77
                if VERBOSE:
 
78
                    print
60
79
                    print 'real_socket.getaddrinfo(%r, %r, %r)' % (hostname, port, args)
61
80
                try:
62
81
                    real_ip = real_socket.getaddrinfo(hostname, port, *args)
 
82
                    if VERBOSE:
 
83
                        print '    returned %r' % (real_ip, )
63
84
                except Exception, ex:
 
85
                    if DEBUG:
 
86
                        raise
64
87
                    real_ip = ex
65
88
                if VERBOSE:
66
89
                    print 'gevent.socket.getaddrinfo(%r, %r, %r)' % (hostname, port, args)
67
90
                try:
68
91
                    ip = getaddrinfo(hostname, port, *args)
 
92
                    if VERBOSE:
 
93
                        print '    returned %r' % (ip, )
69
94
                except Exception, ex:
 
95
                    if DEBUG:
 
96
                        raise
70
97
                    ip = ex
71
98
                if not self.equal(real_ip, ip):
72
99
                    args_str = ', '.join(repr(x) for x in (hostname, port) + args)
73
 
                    print 'WARNING: getaddrinfo(%s):\n    %r\n != %r' % (args_str, real_ip, ip)
 
100
                    print 'WARNING: getaddrinfo(%s):\n    %r    (stdlib)\n != %r    (gevent)' % (args_str, real_ip, ip)
74
101
        # QQQ switch_expected becomes useless when a bunch of unrelated tests are merged
75
102
        #     into a single one like above. Generate individual test cases instead?
76
103
 
80
107
        if isinstance(a, Exception) and isinstance(b, Exception):
81
108
            if repr(a) == repr(b):
82
109
                return True
83
 
            if ACCEPTED_GAIERROR_MISMATCH.get(repr(a), repr(b))==repr(b):
 
110
            if repr(b) in ACCEPTED_GAIERROR_MISMATCH.get(repr(a), (repr(b), )):
84
111
                return True
85
112
 
86
113
    def checkEqual(self, a, b):
152
179
        self._test('sdfsdfgu5e66098032453245wfdggd.com')
153
180
 
154
181
 
 
182
class TestInternational(TestCase):
 
183
 
 
184
    def test(self):
 
185
        self._test(u'президент.рф')
 
186
 
 
187
 
 
188
class TestIPv6(TestCase):
 
189
 
 
190
    def test(self):
 
191
        #self.PORTS = ['http']
 
192
        #self.getaddrinfo_args = [(), (AF_UNSPEC, ), (AF_INET, ), (AF_INET6, )]
 
193
        self._test('aaaa.test-ipv6.com')
 
194
 
 
195
 
 
196
class TestBadPort(TestCase):
 
197
 
 
198
    def test(self):
 
199
        self.PORTS = ['xxxxxx']
 
200
        self._test('www.google.com')
 
201
 
 
202
 
155
203
if __name__ == '__main__':
156
204
    greentest.main()
157