1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mozbase/mozprofile/tests/server_locations.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,153 @@ 1.4 +#!/usr/bin/env python 1.5 + 1.6 +# This Source Code Form is subject to the terms of the Mozilla Public 1.7 +# License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 +# You can obtain one at http://mozilla.org/MPL/2.0/. 1.9 + 1.10 +import mozfile 1.11 +import os 1.12 +import shutil 1.13 +import tempfile 1.14 +import unittest 1.15 +from mozprofile.permissions import ServerLocations, \ 1.16 + MissingPrimaryLocationError, MultiplePrimaryLocationsError, \ 1.17 + DuplicateLocationError, BadPortLocationError, LocationsSyntaxError 1.18 + 1.19 +class ServerLocationsTest(unittest.TestCase): 1.20 + """test server locations""" 1.21 + 1.22 + locations = """# This is the primary location from which tests run. 1.23 +# 1.24 +http://mochi.test:8888 primary,privileged 1.25 + 1.26 +# a few test locations 1.27 +http://127.0.0.1:80 privileged 1.28 +http://127.0.0.1:8888 privileged 1.29 +https://test:80 privileged 1.30 +http://example.org:80 privileged 1.31 +http://test1.example.org privileged 1.32 + 1.33 + """ 1.34 + 1.35 + locations_no_primary = """http://secondary.test:80 privileged 1.36 +http://tertiary.test:8888 privileged 1.37 +""" 1.38 + 1.39 + locations_bad_port = """http://mochi.test:8888 primary,privileged 1.40 +http://127.0.0.1:80 privileged 1.41 +http://127.0.0.1:8888 privileged 1.42 +http://test:badport privileged 1.43 +http://example.org:80 privileged 1.44 +""" 1.45 + 1.46 + def compare_location(self, location, scheme, host, port, options): 1.47 + self.assertEqual(location.scheme, scheme) 1.48 + self.assertEqual(location.host, host) 1.49 + self.assertEqual(location.port, port) 1.50 + self.assertEqual(location.options, options) 1.51 + 1.52 + def create_temp_file(self, contents): 1.53 + f = mozfile.NamedTemporaryFile() 1.54 + f.write(contents) 1.55 + f.flush() 1.56 + return f 1.57 + 1.58 + def test_server_locations(self): 1.59 + # write a permissions file 1.60 + f = self.create_temp_file(self.locations) 1.61 + 1.62 + # read the locations 1.63 + locations = ServerLocations(f.name) 1.64 + 1.65 + # ensure that they're what we expect 1.66 + self.assertEqual(len(locations), 6) 1.67 + i = iter(locations) 1.68 + self.compare_location(i.next(), 'http', 'mochi.test', '8888', 1.69 + ['primary', 'privileged']) 1.70 + self.compare_location(i.next(), 'http', '127.0.0.1', '80', 1.71 + ['privileged']) 1.72 + self.compare_location(i.next(), 'http', '127.0.0.1', '8888', 1.73 + ['privileged']) 1.74 + self.compare_location(i.next(), 'https', 'test', '80', ['privileged']) 1.75 + self.compare_location(i.next(), 'http', 'example.org', '80', 1.76 + ['privileged']) 1.77 + self.compare_location(i.next(), 'http', 'test1.example.org', '8888', 1.78 + ['privileged']) 1.79 + 1.80 + locations.add_host('mozilla.org') 1.81 + self.assertEqual(len(locations), 7) 1.82 + self.compare_location(i.next(), 'http', 'mozilla.org', '80', 1.83 + ['privileged']) 1.84 + 1.85 + # test some errors 1.86 + self.assertRaises(MultiplePrimaryLocationsError, locations.add_host, 1.87 + 'primary.test', options='primary') 1.88 + 1.89 + # We no longer throw these DuplicateLocation Error 1.90 + try: 1.91 + locations.add_host('127.0.0.1') 1.92 + except DuplicateLocationError: 1.93 + self.assertTrue(False, "Should no longer throw DuplicateLocationError") 1.94 + 1.95 + self.assertRaises(BadPortLocationError, locations.add_host, '127.0.0.1', 1.96 + port='abc') 1.97 + 1.98 + # test some errors in locations file 1.99 + f = self.create_temp_file(self.locations_no_primary) 1.100 + 1.101 + exc = None 1.102 + try: 1.103 + ServerLocations(f.name) 1.104 + except LocationsSyntaxError, e: 1.105 + exc = e 1.106 + self.assertNotEqual(exc, None) 1.107 + self.assertEqual(exc.err.__class__, MissingPrimaryLocationError) 1.108 + self.assertEqual(exc.lineno, 3) 1.109 + 1.110 + # test bad port in a locations file to ensure lineno calculated 1.111 + # properly. 1.112 + f = self.create_temp_file(self.locations_bad_port) 1.113 + 1.114 + exc = None 1.115 + try: 1.116 + ServerLocations(f.name) 1.117 + except LocationsSyntaxError, e: 1.118 + exc = e 1.119 + self.assertNotEqual(exc, None) 1.120 + self.assertEqual(exc.err.__class__, BadPortLocationError) 1.121 + self.assertEqual(exc.lineno, 4) 1.122 + 1.123 + def test_server_locations_callback(self): 1.124 + class CallbackTest(object): 1.125 + last_locations = None 1.126 + 1.127 + def callback(self, locations): 1.128 + self.last_locations = locations 1.129 + 1.130 + c = CallbackTest() 1.131 + f = self.create_temp_file(self.locations) 1.132 + locations = ServerLocations(f.name, c.callback) 1.133 + 1.134 + # callback should be for all locations in file 1.135 + self.assertEqual(len(c.last_locations), 6) 1.136 + 1.137 + # validate arbitrary one 1.138 + self.compare_location(c.last_locations[2], 'http', '127.0.0.1', '8888', 1.139 + ['privileged']) 1.140 + 1.141 + locations.add_host('a.b.c') 1.142 + 1.143 + # callback should be just for one location 1.144 + self.assertEqual(len(c.last_locations), 1) 1.145 + self.compare_location(c.last_locations[0], 'http', 'a.b.c', '80', 1.146 + ['privileged']) 1.147 + 1.148 + # read a second file, which should generate a callback with both 1.149 + # locations. 1.150 + f = self.create_temp_file(self.locations_no_primary) 1.151 + locations.read(f.name) 1.152 + self.assertEqual(len(c.last_locations), 2) 1.153 + 1.154 + 1.155 +if __name__ == '__main__': 1.156 + unittest.main()