testing/mozbase/mozprofile/tests/server_locations.py

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial