diff --git a/tests/plugins/reject.py b/tests/plugins/reject.py new file mode 100755 index 000000000..685057856 --- /dev/null +++ b/tests/plugins/reject.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +"""Simple plugin to test the connected_hook. + +It can mark some node_ids as rejects and it'll check for each +connection if it should be disconnected immediately or if it can +continue. + +""" + +from lightning import Plugin + +plugin = Plugin() + + +@plugin.hook('peer_connected') +def on_connected(peer, plugin): + if peer['id'] in plugin.reject_ids: + print("{} is in reject list, disconnecting".format(peer['id'])) + return {'result': 'disconnect'} + + print("{} is allowed".format(peer['id'])) + return {'result': 'continue'} + + +@plugin.init() +def init(configuration, options, plugin): + plugin.reject_ids = [] + + +@plugin.method('reject') +def reject(node_id, plugin): + """Mark a given node_id as reject for future connections. + """ + print("Rejecting connections from {}".format(node_id)) + plugin.reject_ids.append(node_id) + + +plugin.run() diff --git a/tests/test_plugin.py b/tests/test_plugin.py index f1e1ad757..a21adb0f7 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -126,3 +126,23 @@ def test_pay_plugin(node_factory): # Make sure usage messages are present. assert only_one(l1.rpc.help('pay')['help'])['command'] == 'pay bolt11 [msatoshi] [description] [riskfactor] [maxfeepercent] [retry_for] [maxdelay] [exemptfee]' assert only_one(l1.rpc.help('paystatus')['help'])['command'] == 'paystatus [bolt11]' + + +def test_plugin_connected_hook(node_factory): + """ l1 uses the reject plugin to reject connections. + + l1 is configured to accept connections from l2, but not from l3. + """ + opts = [{'plugin': 'tests/plugins/reject.py'}, {}, {}] + l1, l2, l3 = node_factory.get_nodes(3, opts=opts) + l1.rpc.reject(l3.info['id']) + + l2.connect(l1) + l1.daemon.wait_for_log(r"{} is allowed".format(l2.info['id'])) + assert len(l1.rpc.listpeers(l2.info['id'])['peers']) == 1 + + l3.connect(l1) + l1.daemon.wait_for_log(r"{} is in reject list".format(l3.info['id'])) + + peer = l1.rpc.listpeers(l3.info['id'])['peers'] + assert(peer == [] or not peer[0]['connected'])