michael@0: #!/usr/bin/env python michael@0: michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: # You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: import mozfile michael@0: import os michael@0: import shutil michael@0: import tempfile michael@0: import unittest michael@0: from mozprofile.permissions import ServerLocations, \ michael@0: MissingPrimaryLocationError, MultiplePrimaryLocationsError, \ michael@0: DuplicateLocationError, BadPortLocationError, LocationsSyntaxError michael@0: michael@0: class ServerLocationsTest(unittest.TestCase): michael@0: """test server locations""" michael@0: michael@0: locations = """# This is the primary location from which tests run. michael@0: # michael@0: http://mochi.test:8888 primary,privileged michael@0: michael@0: # a few test locations michael@0: http://127.0.0.1:80 privileged michael@0: http://127.0.0.1:8888 privileged michael@0: https://test:80 privileged michael@0: http://example.org:80 privileged michael@0: http://test1.example.org privileged michael@0: michael@0: """ michael@0: michael@0: locations_no_primary = """http://secondary.test:80 privileged michael@0: http://tertiary.test:8888 privileged michael@0: """ michael@0: michael@0: locations_bad_port = """http://mochi.test:8888 primary,privileged michael@0: http://127.0.0.1:80 privileged michael@0: http://127.0.0.1:8888 privileged michael@0: http://test:badport privileged michael@0: http://example.org:80 privileged michael@0: """ michael@0: michael@0: def compare_location(self, location, scheme, host, port, options): michael@0: self.assertEqual(location.scheme, scheme) michael@0: self.assertEqual(location.host, host) michael@0: self.assertEqual(location.port, port) michael@0: self.assertEqual(location.options, options) michael@0: michael@0: def create_temp_file(self, contents): michael@0: f = mozfile.NamedTemporaryFile() michael@0: f.write(contents) michael@0: f.flush() michael@0: return f michael@0: michael@0: def test_server_locations(self): michael@0: # write a permissions file michael@0: f = self.create_temp_file(self.locations) michael@0: michael@0: # read the locations michael@0: locations = ServerLocations(f.name) michael@0: michael@0: # ensure that they're what we expect michael@0: self.assertEqual(len(locations), 6) michael@0: i = iter(locations) michael@0: self.compare_location(i.next(), 'http', 'mochi.test', '8888', michael@0: ['primary', 'privileged']) michael@0: self.compare_location(i.next(), 'http', '127.0.0.1', '80', michael@0: ['privileged']) michael@0: self.compare_location(i.next(), 'http', '127.0.0.1', '8888', michael@0: ['privileged']) michael@0: self.compare_location(i.next(), 'https', 'test', '80', ['privileged']) michael@0: self.compare_location(i.next(), 'http', 'example.org', '80', michael@0: ['privileged']) michael@0: self.compare_location(i.next(), 'http', 'test1.example.org', '8888', michael@0: ['privileged']) michael@0: michael@0: locations.add_host('mozilla.org') michael@0: self.assertEqual(len(locations), 7) michael@0: self.compare_location(i.next(), 'http', 'mozilla.org', '80', michael@0: ['privileged']) michael@0: michael@0: # test some errors michael@0: self.assertRaises(MultiplePrimaryLocationsError, locations.add_host, michael@0: 'primary.test', options='primary') michael@0: michael@0: # We no longer throw these DuplicateLocation Error michael@0: try: michael@0: locations.add_host('127.0.0.1') michael@0: except DuplicateLocationError: michael@0: self.assertTrue(False, "Should no longer throw DuplicateLocationError") michael@0: michael@0: self.assertRaises(BadPortLocationError, locations.add_host, '127.0.0.1', michael@0: port='abc') michael@0: michael@0: # test some errors in locations file michael@0: f = self.create_temp_file(self.locations_no_primary) michael@0: michael@0: exc = None michael@0: try: michael@0: ServerLocations(f.name) michael@0: except LocationsSyntaxError, e: michael@0: exc = e michael@0: self.assertNotEqual(exc, None) michael@0: self.assertEqual(exc.err.__class__, MissingPrimaryLocationError) michael@0: self.assertEqual(exc.lineno, 3) michael@0: michael@0: # test bad port in a locations file to ensure lineno calculated michael@0: # properly. michael@0: f = self.create_temp_file(self.locations_bad_port) michael@0: michael@0: exc = None michael@0: try: michael@0: ServerLocations(f.name) michael@0: except LocationsSyntaxError, e: michael@0: exc = e michael@0: self.assertNotEqual(exc, None) michael@0: self.assertEqual(exc.err.__class__, BadPortLocationError) michael@0: self.assertEqual(exc.lineno, 4) michael@0: michael@0: def test_server_locations_callback(self): michael@0: class CallbackTest(object): michael@0: last_locations = None michael@0: michael@0: def callback(self, locations): michael@0: self.last_locations = locations michael@0: michael@0: c = CallbackTest() michael@0: f = self.create_temp_file(self.locations) michael@0: locations = ServerLocations(f.name, c.callback) michael@0: michael@0: # callback should be for all locations in file michael@0: self.assertEqual(len(c.last_locations), 6) michael@0: michael@0: # validate arbitrary one michael@0: self.compare_location(c.last_locations[2], 'http', '127.0.0.1', '8888', michael@0: ['privileged']) michael@0: michael@0: locations.add_host('a.b.c') michael@0: michael@0: # callback should be just for one location michael@0: self.assertEqual(len(c.last_locations), 1) michael@0: self.compare_location(c.last_locations[0], 'http', 'a.b.c', '80', michael@0: ['privileged']) michael@0: michael@0: # read a second file, which should generate a callback with both michael@0: # locations. michael@0: f = self.create_temp_file(self.locations_no_primary) michael@0: locations.read(f.name) michael@0: self.assertEqual(len(c.last_locations), 2) michael@0: michael@0: michael@0: if __name__ == '__main__': michael@0: unittest.main()