From 53488e573913ab0aa4debe8f318024cd7f7cb2be Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sat, 29 Jun 2019 18:25:45 +0200 Subject: [PATCH] pytest: Strengthen the htlc_accepted tests We were having a few issues with malformed data in the past, so this time we really check that stuff. Signed-off-by: Christian Decker --- tests/plugins/hold_htlcs.py | 12 +++++++++++- tests/test_plugin.py | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/plugins/hold_htlcs.py b/tests/plugins/hold_htlcs.py index cca513213..3e4a1008a 100755 --- a/tests/plugins/hold_htlcs.py +++ b/tests/plugins/hold_htlcs.py @@ -8,17 +8,27 @@ settled/forwarded/ from lightning import Plugin +import json +import os +import tempfile import time - plugin = Plugin() @plugin.hook("htlc_accepted") def on_htlc_accepted(htlc, onion, plugin): + # Stash the onion so the test can check it + fname = os.path.join(tempfile.mkdtemp(), "onion.json") + with open(fname, 'w') as f: + f.write(json.dumps(onion)) + plugin.log("Holding onto an incoming htlc for 10 seconds") + time.sleep(10) + print("Onion written to {}".format(fname)) + # Give the tester something to look for plugin.log("htlc_accepted hook called") return {'result': 'continue'} diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 7a68df658..ac3648956 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -4,8 +4,10 @@ from flaky import flaky # noqa: F401 from lightning import RpcError, Millisatoshi from utils import only_one, wait_for, TIMEOUT +import json import os import pytest +import re import sqlite3 import subprocess import time @@ -427,8 +429,21 @@ def test_htlc_accepted_hook_forward_restart(node_factory, executor): f1 = executor.submit(l1.rpc.pay, i1) l2.daemon.wait_for_log(r'Holding onto an incoming htlc for 10 seconds') + l2.restart() + # Grab the file where the plugin wrote the onion and read it in for some + # additional checks + logline = l2.daemon.wait_for_log(r'Onion written to') + fname = re.search(r'Onion written to (.*\.json)', logline).group(1) + onion = json.load(open(fname)) + assert re.match(r'^00006700000.000100000000000003e8000000..000000000000000000000000$', onion['payload']) + assert len(onion['payload']) == 64 + assert len(onion['shared_secret']) == 64 + assert onion['per_hop_v0']['realm'] == "00" + assert onion['per_hop_v0']['forward_amount'] == '1000msat' + assert len(onion['next_onion']) == 2 * (1300 + 32 + 33 + 1) + f1.result()