657
655
def test_unknown_ref(self):
658
656
c = self.get_config('')
659
self.assertRaises(errors.ExpandingUnknownOption,
657
self.assertRaises(config.ExpandingUnknownOption,
660
658
c.expand_options, '{foo}')
662
660
def test_indirect_ref(self):
676
674
def test_simple_loop(self):
677
675
c = self.get_config('foo={foo}')
678
self.assertRaises(errors.OptionExpansionLoop, c.expand_options, '{foo}')
676
self.assertRaises(config.OptionExpansionLoop, c.expand_options,
680
679
def test_indirect_loop(self):
681
680
c = self.get_config('''
685
e = self.assertRaises(errors.OptionExpansionLoop,
684
e = self.assertRaises(config.OptionExpansionLoop,
686
685
c.expand_options, '{foo}')
687
686
self.assertEqual('foo->bar->baz', e.refs)
688
687
self.assertEqual('{foo}', e.string)
741
740
[/another/branch/path]
744
self.assertRaises(errors.ExpandingUnknownOption,
743
self.assertRaises(config.ExpandingUnknownOption,
745
744
c.get_user_option, 'bar', expand=True)
747
746
def test_cross_related_sections(self):
956
955
self.assertEqual(True, suppress_warning('b'))
959
class TestGetConfig(tests.TestCase):
958
class TestGetConfig(tests.TestCaseInTempDir):
961
960
def test_constructs(self):
962
961
config.GlobalConfig()
1536
1535
def test_extract_email_address(self):
1537
1536
self.assertEqual('jane@test.com',
1538
1537
config.extract_email_address('Jane <jane@test.com>'))
1539
self.assertRaises(errors.NoEmailInUsername,
1538
self.assertRaises(config.NoEmailInUsername,
1540
1539
config.extract_email_address, 'Jane Tester')
1542
1541
def test_parse_username(self):
1597
1596
t = self.get_transport()
1598
1597
t.put_bytes('foo.conf', 'user=foo\n#\xff\n')
1599
1598
conf = config.TransportConfig(t, 'foo.conf')
1600
self.assertRaises(errors.ConfigContentError, conf._get_configobj)
1599
self.assertRaises(config.ConfigContentError, conf._get_configobj)
1602
1601
def test_load_erroneous_content(self):
1603
1602
"""Ensure we display a proper error on content that can't be parsed."""
1604
1603
t = self.get_transport()
1605
1604
t.put_bytes('foo.conf', '[open_section\n')
1606
1605
conf = config.TransportConfig(t, 'foo.conf')
1607
self.assertRaises(errors.ParseConfigError, conf._get_configobj)
1606
self.assertRaises(config.ParseConfigError, conf._get_configobj)
1609
1608
def test_load_permission_denied(self):
1610
1609
"""Ensure we get an empty config file if the file is inaccessible."""
2016
2015
def assertCallsError(self, opt, value):
2017
self.assertRaises(errors.ConfigOptionValueError,
2016
self.assertRaises(config.ConfigOptionValueError,
2018
2017
opt.convert_from_unicode, None, value)
2020
2019
def assertConvertInvalid(self, opt, invalid_value):
2181
2180
self.assertEqual('A simple option', self.registry.get_help('foo'))
2183
2182
def test_dont_register_illegal_name(self):
2184
self.assertRaises(errors.IllegalOptionName,
2183
self.assertRaises(config.IllegalOptionName,
2185
2184
self.registry.register, config.Option(' foo'))
2186
self.assertRaises(errors.IllegalOptionName,
2185
self.assertRaises(config.IllegalOptionName,
2187
2186
self.registry.register, config.Option('bar,'))
2189
2188
lazy_option = config.Option('lazy_foo', help='Lazy help')
2204
2203
# the option name which indirectly requires that the option name is a
2205
2204
# valid python identifier. We violate that rule here (using a key that
2206
2205
# doesn't match the option name) to test the option name checking.
2207
self.assertRaises(errors.IllegalOptionName,
2206
self.assertRaises(config.IllegalOptionName,
2208
2207
self.registry.register_lazy, ' foo', self.__module__,
2209
2208
'TestOptionRegistry.lazy_option')
2210
self.assertRaises(errors.IllegalOptionName,
2209
self.assertRaises(config.IllegalOptionName,
2211
2210
self.registry.register_lazy, '1,2', self.__module__,
2212
2211
'TestOptionRegistry.lazy_option')
2521
2520
t = self.get_transport()
2522
2521
t.put_bytes('foo.conf', 'user=foo\n#%s\n' % (self.invalid_utf8_char,))
2523
2522
store = config.TransportIniFileStore(t, 'foo.conf')
2524
self.assertRaises(errors.ConfigContentError, store.load)
2523
self.assertRaises(config.ConfigContentError, store.load)
2526
2525
def test_load_erroneous_content(self):
2527
2526
"""Ensure we display a proper error on content that can't be parsed."""
2528
2527
t = self.get_transport()
2529
2528
t.put_bytes('foo.conf', '[open_section\n')
2530
2529
store = config.TransportIniFileStore(t, 'foo.conf')
2531
self.assertRaises(errors.ParseConfigError, store.load)
2530
self.assertRaises(config.ParseConfigError, store.load)
2533
2532
def test_load_permission_denied(self):
2534
2533
"""Ensure we get warned when trying to load an inaccessible file."""
2578
2577
with open('foo.conf', 'wb') as f:
2579
2578
f.write('user=foo\n#%s\n' % (self.invalid_utf8_char,))
2580
2579
conf = config.IniBasedConfig(file_name='foo.conf')
2581
self.assertRaises(errors.ConfigContentError, conf._get_parser)
2580
self.assertRaises(config.ConfigContentError, conf._get_parser)
2583
2582
def test_load_erroneous_content(self):
2584
2583
"""Ensure we display a proper error on content that can't be parsed."""
2585
2584
with open('foo.conf', 'wb') as f:
2586
2585
f.write('[open_section\n')
2587
2586
conf = config.IniBasedConfig(file_name='foo.conf')
2588
self.assertRaises(errors.ParseConfigError, conf._get_parser)
2587
self.assertRaises(config.ParseConfigError, conf._get_parser)
2591
2590
class TestMutableStore(TestStore):
2870
2869
store = config.TransportIniFileStore(self.get_transport(), 'foo.conf')
2871
2870
self.assertEqual(False, store.is_loaded())
2872
2871
exc = self.assertRaises(
2873
errors.ParseConfigError, store._load_from_string,
2872
config.ParseConfigError, store._load_from_string,
2874
2873
'this is invalid !')
2875
2874
self.assertEndsWith(exc.filename, 'foo.conf')
2876
2875
# And the load failed
3670
3669
self.assertExpansion('xxx', '{foo}')
3672
3671
def test_unknown_ref(self):
3673
self.assertRaises(errors.ExpandingUnknownOption,
3672
self.assertRaises(config.ExpandingUnknownOption,
3674
3673
self.conf.expand_options, '{foo}')
3676
3675
def test_illegal_def_is_ignored(self):
3695
3694
def test_simple_loop(self):
3696
3695
self.conf.store._load_from_string('foo={foo}')
3697
self.assertRaises(errors.OptionExpansionLoop,
3696
self.assertRaises(config.OptionExpansionLoop,
3698
3697
self.conf.expand_options, '{foo}')
3700
3699
def test_indirect_loop(self):
3705
e = self.assertRaises(errors.OptionExpansionLoop,
3704
e = self.assertRaises(config.OptionExpansionLoop,
3706
3705
self.conf.expand_options, '{foo}')
3707
3706
self.assertEqual('foo->bar->baz', e.refs)
3708
3707
self.assertEqual('{foo}', e.string)
3773
3772
[/another/branch/path]
3776
self.assertRaises(errors.ExpandingUnknownOption,
3775
self.assertRaises(config.ExpandingUnknownOption,
3777
3776
c.get, 'bar', expand=True)
3779
3778
def test_cross_related_sections(self):
3863
3862
stack = config.LocationStack('/home/user/project/branch')
3864
self.assertRaises(errors.ExpandingUnknownOption,
3863
self.assertRaises(config.ExpandingUnknownOption,
3865
3864
stack.get, 'gfoo', expand=True)
3867
3866
def test_expand_local_option_locally(self):
4114
4113
self.assertIs(g1.store, g2.store)
4116
class TestAuthenticationConfigFilePermissions(tests.TestCaseInTempDir):
4117
"""Test warning for permissions of authentication.conf."""
4120
super(TestAuthenticationConfigFilePermissions, self).setUp()
4121
self.path = osutils.pathjoin(self.test_dir, 'authentication.conf')
4122
with open(self.path, 'w') as f:
4123
f.write(b"""[broken]
4126
port=port # Error: Not an int
4128
self.overrideAttr(config, 'authentication_config_filename',
4130
osutils.chmod_if_possible(self.path, 0o755)
4132
def test_check_warning(self):
4133
conf = config.AuthenticationConfig()
4134
self.assertEqual(conf._filename, self.path)
4135
self.assertContainsRe(self.get_log(),
4136
'Saved passwords may be accessible by other users.')
4138
def test_check_suppressed_warning(self):
4139
global_config = config.GlobalConfig()
4140
global_config.set_user_option('suppress_warnings',
4141
'insecure_permissions')
4142
conf = config.AuthenticationConfig()
4143
self.assertEqual(conf._filename, self.path)
4144
self.assertNotContainsRe(self.get_log(),
4145
'Saved passwords may be accessible by other users.')
4117
4148
class TestAuthenticationConfigFile(tests.TestCase):
4118
4149
"""Test the authentication.conf file matching"""
4137
4168
def test_non_utf8_config(self):
4138
4169
conf = config.AuthenticationConfig(_file=BytesIO(b'foo = bar\xff'))
4139
self.assertRaises(errors.ConfigContentError, conf._get_config)
4170
self.assertRaises(config.ConfigContentError, conf._get_config)
4141
4172
def test_missing_auth_section_header(self):
4142
4173
conf = config.AuthenticationConfig(_file=BytesIO(b'foo = bar'))
4145
4176
def test_auth_section_header_not_closed(self):
4146
4177
conf = config.AuthenticationConfig(_file=BytesIO(b'[DEF'))
4147
self.assertRaises(errors.ParseConfigError, conf._get_config)
4178
self.assertRaises(config.ParseConfigError, conf._get_config)
4149
4180
def test_auth_value_not_boolean(self):
4150
4181
conf = config.AuthenticationConfig(_file=BytesIO(b"""\
4350
4381
self.assertEqual(CREDENTIALS, credentials)
4353
class TestAuthenticationConfig(tests.TestCase):
4384
class TestAuthenticationConfig(tests.TestCaseInTempDir):
4354
4385
"""Test AuthenticationConfig behaviour"""
4356
4387
def _check_default_password_prompt(self, expected_prompt_format, scheme,
4736
4767
def test_unknown(self):
4737
4768
conf = config.MemoryStack('mail_client=firebird')
4738
self.assertRaises(errors.ConfigOptionValueError, conf.get,
4769
self.assertRaises(config.ConfigOptionValueError, conf.get,