~ubuntu-branches/ubuntu/quantal/enigmail/quantal-security

« back to all changes in this revision

Viewing changes to mozilla/testing/mozbase/mozprofile/tests/server_locations.py

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2013-09-13 16:02:15 UTC
  • mfrom: (0.12.16)
  • Revision ID: package-import@ubuntu.com-20130913160215-u3g8nmwa0pdwagwc
Tags: 2:1.5.2-0ubuntu0.12.10.1
* New upstream release v1.5.2 for Thunderbird 24

* Build enigmail using a stripped down Thunderbird 17 build system, as it's
  now quite difficult to build the way we were doing previously, with the
  latest Firefox build system
* Add debian/patches/no_libxpcom.patch - Don't link against libxpcom, as it
  doesn't exist anymore (but exists in the build system)
* Add debian/patches/use_sdk.patch - Use the SDK version of xpt.py and
  friends
* Drop debian/patches/ipc-pipe_rename.diff (not needed anymore)
* Drop debian/patches/makefile_depth.diff (not needed anymore)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# This Source Code Form is subject to the terms of the Mozilla Public
 
4
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
 
5
# You can obtain one at http://mozilla.org/MPL/2.0/.
 
6
 
 
7
import os
 
8
import shutil
 
9
import tempfile
 
10
import unittest
 
11
from mozprofile.permissions import ServerLocations, \
 
12
    MissingPrimaryLocationError, MultiplePrimaryLocationsError, \
 
13
    DuplicateLocationError, BadPortLocationError, LocationsSyntaxError
 
14
 
 
15
class ServerLocationsTest(unittest.TestCase):
 
16
    """test server locations"""
 
17
 
 
18
    locations = """# This is the primary location from which tests run.
 
19
#
 
20
http://mochi.test:8888          primary,privileged
 
21
 
 
22
# a few test locations
 
23
http://127.0.0.1:80             privileged
 
24
http://127.0.0.1:8888           privileged
 
25
https://test:80                 privileged
 
26
http://example.org:80           privileged
 
27
http://test1.example.org        privileged
 
28
 
 
29
    """
 
30
 
 
31
    locations_no_primary = """http://secondary.test:80        privileged
 
32
http://tertiary.test:8888       privileged
 
33
"""
 
34
 
 
35
    locations_bad_port = """http://mochi.test:8888  primary,privileged
 
36
http://127.0.0.1:80             privileged
 
37
http://127.0.0.1:8888           privileged
 
38
http://test:badport             privileged
 
39
http://example.org:80           privileged
 
40
"""
 
41
 
 
42
    def compare_location(self, location, scheme, host, port, options):
 
43
        self.assertEqual(location.scheme, scheme)
 
44
        self.assertEqual(location.host, host)
 
45
        self.assertEqual(location.port, port)
 
46
        self.assertEqual(location.options, options)
 
47
 
 
48
    def create_temp_file(self, contents):
 
49
        f = tempfile.NamedTemporaryFile()
 
50
        f.write(contents)
 
51
        f.flush()
 
52
        return f
 
53
 
 
54
    def test_server_locations(self):
 
55
        # write a permissions file
 
56
        f = self.create_temp_file(self.locations)
 
57
 
 
58
        # read the locations
 
59
        locations = ServerLocations(f.name)
 
60
 
 
61
        # ensure that they're what we expect
 
62
        self.assertEqual(len(locations), 6)
 
63
        i = iter(locations)
 
64
        self.compare_location(i.next(), 'http', 'mochi.test', '8888',
 
65
                              ['primary', 'privileged'])
 
66
        self.compare_location(i.next(), 'http', '127.0.0.1', '80',
 
67
                              ['privileged'])
 
68
        self.compare_location(i.next(), 'http', '127.0.0.1', '8888',
 
69
                              ['privileged'])
 
70
        self.compare_location(i.next(), 'https', 'test', '80', ['privileged'])
 
71
        self.compare_location(i.next(), 'http', 'example.org', '80',
 
72
                              ['privileged'])
 
73
        self.compare_location(i.next(), 'http', 'test1.example.org', '80',
 
74
                              ['privileged'])
 
75
 
 
76
        locations.add_host('mozilla.org')
 
77
        self.assertEqual(len(locations), 7)
 
78
        self.compare_location(i.next(), 'http', 'mozilla.org', '80',
 
79
                              ['privileged'])
 
80
 
 
81
        # test some errors
 
82
        self.assertRaises(MultiplePrimaryLocationsError, locations.add_host,
 
83
                          'primary.test', options='primary')
 
84
 
 
85
        # We no longer throw these DuplicateLocation Error
 
86
        try:
 
87
            locations.add_host('127.0.0.1')
 
88
        except DuplicateLocationError:
 
89
            self.assertTrue(False, "Should no longer throw DuplicateLocationError")
 
90
 
 
91
        self.assertRaises(BadPortLocationError, locations.add_host, '127.0.0.1',
 
92
                          port='abc')
 
93
 
 
94
        # test some errors in locations file
 
95
        f = self.create_temp_file(self.locations_no_primary)
 
96
 
 
97
        exc = None
 
98
        try:
 
99
            ServerLocations(f.name)
 
100
        except LocationsSyntaxError, e:
 
101
            exc = e
 
102
        self.assertNotEqual(exc, None)
 
103
        self.assertEqual(exc.err.__class__, MissingPrimaryLocationError)
 
104
        self.assertEqual(exc.lineno, 3)
 
105
 
 
106
        # test bad port in a locations file to ensure lineno calculated
 
107
        # properly.
 
108
        f = self.create_temp_file(self.locations_bad_port)
 
109
 
 
110
        exc = None
 
111
        try:
 
112
            ServerLocations(f.name)
 
113
        except LocationsSyntaxError, e:
 
114
            exc = e
 
115
        self.assertNotEqual(exc, None)
 
116
        self.assertEqual(exc.err.__class__, BadPortLocationError)
 
117
        self.assertEqual(exc.lineno, 4)
 
118
 
 
119
    def test_server_locations_callback(self):
 
120
        class CallbackTest(object):
 
121
            last_locations = None
 
122
 
 
123
            def callback(self, locations):
 
124
                self.last_locations = locations
 
125
 
 
126
        c = CallbackTest()
 
127
        f = self.create_temp_file(self.locations)
 
128
        locations = ServerLocations(f.name, c.callback)
 
129
 
 
130
        # callback should be for all locations in file
 
131
        self.assertEqual(len(c.last_locations), 6)
 
132
 
 
133
        # validate arbitrary one
 
134
        self.compare_location(c.last_locations[2], 'http', '127.0.0.1', '8888',
 
135
                              ['privileged'])
 
136
 
 
137
        locations.add_host('a.b.c')
 
138
 
 
139
        # callback should be just for one location
 
140
        self.assertEqual(len(c.last_locations), 1)
 
141
        self.compare_location(c.last_locations[0], 'http', 'a.b.c', '80',
 
142
                              ['privileged'])
 
143
        
 
144
        # read a second file, which should generate a callback with both
 
145
        # locations.
 
146
        f = self.create_temp_file(self.locations_no_primary)
 
147
        locations.read(f.name)
 
148
        self.assertEqual(len(c.last_locations), 2)
 
149
 
 
150
 
 
151
if __name__ == '__main__':
 
152
    unittest.main()