testing/mozbase/mozprofile/tests/server_locations.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial