Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Note to self: that's a *lot* of test-setup-TorConfig code
scattered around; maybe needs an actual Fake finally??
  • Loading branch information
meejah committed Sep 30, 2017
1 parent 828f6b7 commit 6d9243d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions test/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ def _fake_queue(self, cmd):
return defer.succeed('version=0.1.2.3')
elif cmd == 'GETINFO events/names':
return defer.succeed('events/names=STATUS_CLIENT')
elif cmd == 'GETINFO config/defaults':
return defer.succeed('config/defaults=')
return defer.succeed(None)

def _fake_event_listener(self, what, cb):
Expand Down Expand Up @@ -609,6 +611,8 @@ def fake_queue(cmd):
return defer.succeed('version=0.1.2.3')
elif cmd == 'GETINFO events/names':
return defer.succeed('events/names=STATUS_CLIENT')
elif cmd == 'GETINFO config/defaults':
return defer.succeed('config/defaults=')
return defer.succeed(None)
tpp.queue_command = fake_queue
proto.makeConnection(Mock())
Expand Down
1 change: 1 addition & 0 deletions test/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def setUp(self):
self.protocol.answers.append(
'config/names=\nHiddenServiceOptions Virtual\nControlPort LineList'
)
self.protocol.answers.append('')
self.protocol.answers.append('HiddenServiceOptions')
# why do i have to pass a dict for this V but not this ^
self.protocol.answers.append({'ControlPort': '37337'})
Expand Down
36 changes: 36 additions & 0 deletions test/test_torconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def setUp(self):

def test_boolean_parse_error(self):
self.protocol.answers.append('config/names=\nfoo Boolean')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'foo': 'bar'})
cfg = TorConfig(self.protocol)
return self.assertFailure(cfg.post_bootstrap, ValueError)
Expand All @@ -164,6 +165,7 @@ def test_contains(self):

def test_boolean_parser(self):
self.protocol.answers.append('config/names=\nfoo Boolean\nbar Boolean')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'foo': '0'})
self.protocol.answers.append({'bar': '1'})
# FIXME does a Tor controller only ever send "0" and "1" for
Expand All @@ -175,6 +177,7 @@ def test_boolean_parser(self):

def test_save_boolean(self):
self.protocol.answers.append('config/names=\nfoo Boolean\nbar Boolean')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'foo': '0'})
self.protocol.answers.append({'bar': '1'})

Expand All @@ -189,6 +192,7 @@ def test_save_boolean(self):

def test_read_boolean_after_save(self):
self.protocol.answers.append('config/names=\nfoo Boolean\nbar Boolean')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'foo': '0'})
self.protocol.answers.append({'bar': '1'})

Expand All @@ -203,6 +207,7 @@ def test_read_boolean_after_save(self):

def test_save_boolean_with_strange_values(self):
self.protocol.answers.append('config/names=\nfoo Boolean\nbar Boolean')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'foo': '0'})
self.protocol.answers.append({'bar': '1'})

Expand All @@ -218,6 +223,7 @@ def test_boolean_auto_parser(self):
self.protocol.answers.append(
'config/names=\nfoo Boolean+Auto\nbar Boolean+Auto\nbaz Boolean+Auto'
)
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'foo': '0'})
self.protocol.answers.append({'bar': '1'})
self.protocol.answers.append({'baz': 'auto'})
Expand All @@ -231,6 +237,7 @@ def test_save_boolean_auto(self):
self.protocol.answers.append(
'config/names=\nfoo Boolean+Auto\nbar Boolean+Auto\nbaz Boolean+Auto\nqux Boolean+Auto'
)
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'foo': '1'})
self.protocol.answers.append({'bar': '1'})
self.protocol.answers.append({'baz': '1'})
Expand All @@ -256,6 +263,7 @@ def test_save_invalid_boolean_auto(self):
self.protocol.answers.append(
'config/names=\nfoo Boolean+Auto'
)
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'foo': '1'})

conf = TorConfig(self.protocol)
Expand All @@ -271,18 +279,21 @@ def test_save_invalid_boolean_auto(self):

def test_string_parser(self):
self.protocol.answers.append('config/names=\nfoo String')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'foo': 'bar'})
conf = TorConfig(self.protocol)
self.assertEqual(conf.foo, 'bar')

def test_int_parser(self):
self.protocol.answers.append('config/names=\nfoo Integer')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'foo': '123'})
conf = TorConfig(self.protocol)
self.assertEqual(conf.foo, 123)

def test_int_validator(self):
self.protocol.answers.append('config/names=\nfoo Integer')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'foo': '123'})
conf = TorConfig(self.protocol)

Expand Down Expand Up @@ -316,63 +327,73 @@ def test_int_validator(self):

def test_int_parser_error(self):
self.protocol.answers.append('config/names=\nfoo Integer')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'foo': '123foo'})
cfg = TorConfig(self.protocol)
self.assertFailure(cfg.post_bootstrap, ValueError)

def test_int_parser_error_2(self):
self.protocol.answers.append('config/names=\nfoo Integer')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'foo': '1.23'})
cfg = TorConfig(self.protocol)
return self.assertFailure(cfg.post_bootstrap, ValueError)

def test_linelist_parser(self):
self.protocol.answers.append('config/names=\nfoo LineList')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'foo': 'bar\nbaz'})
conf = TorConfig(self.protocol)
self.assertEqual(conf.foo, ['bar', 'baz'])

def test_listlist_parser_with_list(self):
self.protocol.answers.append('config/names=\nfoo LineList')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'foo': [1, 2, 3]})

conf = TorConfig(self.protocol)
self.assertEqual(conf.foo, ['1', '2', '3'])

def test_float_parser(self):
self.protocol.answers.append('config/names=\nfoo Float')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'foo': '1.23'})
conf = TorConfig(self.protocol)
self.assertEqual(conf.foo, 1.23)

def test_float_parser_error(self):
self.protocol.answers.append('config/names=\nfoo Float')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'foo': '1.23fff'})
cfg = TorConfig(self.protocol)
return self.assertFailure(cfg.post_bootstrap, ValueError)

def test_list(self):
self.protocol.answers.append('config/names=\nbing CommaList')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'bing': 'foo,bar,baz'})
conf = TorConfig(self.protocol)
self.assertEqual(conf.config['bing'], ['foo', 'bar', 'baz'])
# self.assertEqual(conf.bing, ['foo','bar','baz'])

def test_single_list(self):
self.protocol.answers.append('config/names=\nbing CommaList')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'bing': 'foo'})
conf = TorConfig(self.protocol)
self.assertTrue(conf.post_bootstrap.called)
self.assertEqual(conf.config['bing'], ['foo'])

def test_multi_list_space(self):
self.protocol.answers.append('config/names=\nbing CommaList')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'bing': 'foo, bar , baz'})
conf = TorConfig(self.protocol)
self.assertEqual(conf.bing, ['foo', 'bar', 'baz'])

def test_descriptor_access(self):
self.protocol.answers.append('config/names=\nbing CommaList')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'bing': 'foo,bar'})

conf = TorConfig(self.protocol)
Expand All @@ -394,6 +415,7 @@ def confirm(conf):

def test_unknown_descriptor(self):
self.protocol.answers.append('config/names=\nbing CommaList')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'bing': 'foo'})

conf = TorConfig(self.protocol)
Expand All @@ -407,6 +429,7 @@ def test_invalid_parser(self):
self.protocol.answers.append(
'config/names=\nSomethingExciting NonExistantParserType'
)
self.protocol.answers.append('') # defaults
cfg = TorConfig(self.protocol)
return self.assertFailure(cfg.post_bootstrap, RuntimeError)

Expand All @@ -422,6 +445,7 @@ def test_get_type(self):
self.protocol.answers.append(
'config/names=\nSomethingExciting CommaList\nHiddenServices Dependant'
)
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'SomethingExciting': 'a,b'})
conf = TorConfig(self.protocol)

Expand All @@ -443,6 +467,7 @@ def foo(self, *args):
def test_slutty_postbootstrap(self):
# test that doPostbootstrap still works in "slutty" mode
self.protocol.answers.append('config/names=\nORPort Port')
self.protocol.answers.append('') # defaults
# we can't answer right away, or we do all the _do_setup
# callbacks before _setup_ is set -- but we need to do an
# answer callback after that to trigger this bug
Expand All @@ -454,13 +479,15 @@ def test_slutty_postbootstrap(self):
def test_immediate_bootstrap(self):
self.protocol.post_bootstrap = None
self.protocol.answers.append('config/names=\nfoo Boolean')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'foo': '0'})
conf = TorConfig(self.protocol)
self.assertTrue('foo' in conf.config)

def test_multiple_orports(self):
self.protocol.post_bootstrap = None
self.protocol.answers.append('config/names=\nOrPort CommaList')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'OrPort': '1234'})
conf = TorConfig(self.protocol)
conf.OrPort = ['1234', '4321']
Expand All @@ -470,6 +497,7 @@ def test_multiple_orports(self):

def test_set_multiple(self):
self.protocol.answers.append('config/names=\nAwesomeKey String')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'AwesomeKey': 'foo'})

conf = TorConfig(self.protocol)
Expand All @@ -488,6 +516,7 @@ def test_log_double_save(self):
self.protocol.answers.append(
'config/names=\nLog LineList\nFoo String'''
)
self.protocol.answers.append('') # defaults
self.protocol.answers.append(
{'Log': 'notice file /var/log/tor/notices.log'}
)
Expand All @@ -507,6 +536,7 @@ def test_log_double_save(self):

def test_set_save_modify(self):
self.protocol.answers.append('config/names=\nLog LineList')
self.protocol.answers.append('') # defaults
self.protocol.answers.append(
{'Log': 'notice file /var/log/tor/notices.log'}
)
Expand All @@ -521,6 +551,7 @@ def test_set_save_modify(self):

def test_proper_sets(self):
self.protocol.answers.append('config/names=\nLog LineList')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'Log': 'foo'})

conf = TorConfig(self.protocol)
Expand All @@ -534,6 +565,7 @@ def test_proper_sets(self):
@defer.inlineCallbacks
def test_attach_protocol(self):
self.protocol.answers.append('config/names=\nLog LineList')
self.protocol.answers.append('') # defaults
self.protocol.answers.append({'Log': 'foo'})

conf = TorConfig()
Expand Down Expand Up @@ -570,6 +602,7 @@ class LogTests(unittest.TestCase):
def setUp(self):
self.protocol = FakeControlProtocol([])
self.protocol.answers.append('config/names=\nLog LineList''')
self.protocol.answers.append('') # defaults
self.protocol.answers.append(
{'Log': 'notice file /var/log/tor/notices.log'}
)
Expand Down Expand Up @@ -733,6 +766,7 @@ def test_conf_changed_parsed(self):
'''
protocol = FakeControlProtocol([])
protocol.answers.append('config/names=\nFoo Boolean\nBar Boolean')
protocol.answers.append('') # defaults
protocol.answers.append({'Foo': '0'})
protocol.answers.append({'Bar': '1'})

Expand All @@ -749,6 +783,7 @@ def test_conf_changed_parsed(self):
def test_conf_changed_invalid_values(self):
protocol = FakeControlProtocol([])
protocol.answers.append('config/names=\nFoo Integer\nBar Integer')
protocol.answers.append('') # defaults
protocol.answers.append({'Foo': '0'})
protocol.answers.append({'Bar': '1'})

Expand Down Expand Up @@ -919,6 +954,7 @@ def setUp(self):
HiddenServiceVersion Dependant
HiddenServiceDirGroupReadable Dependant
HiddenServiceAuthorizeClient Dependant''')
self.protocol.answers.append('') # defaults

@defer.inlineCallbacks
def test_options_hidden(self):
Expand Down
1 change: 1 addition & 0 deletions txtorcon/torconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ def __init__(self, control=None):
self._supports = dict(
HiddenServiceDirGroupReadable=False
)
self._defaults = dict()

self.post_bootstrap = defer.Deferred()
if self.protocol:
Expand Down

0 comments on commit 6d9243d

Please sign in to comment.