MCPcopy Create free account
hub / github.com/ElementsProject/lightning / test_websocket

Function test_websocket

tests/test_connection.py:3972–4041  ·  view source on GitHub ↗
(node_factory)

Source from the content-addressed store, hash-verified

3970
3971
3972def test_websocket(node_factory):
3973 ws_port = node_factory.get_unused_port()
3974 port = node_factory.get_unused_port()
3975 l1, l2 = node_factory.line_graph(2,
3976 opts=[{'addr': ':' + str(port),
3977 'bind-addr': 'ws:127.0.0.1: ' + str(ws_port),
3978 'dev-allow-localhost': None},
3979 {'dev-allow-localhost': None}],
3980 wait_for_announce=True)
3981 # Some depend on ipv4 vs ipv6 behaviour...
3982 for b in l1.rpc.getinfo()['binding']:
3983 if b['type'] == 'ipv4':
3984 assert b == {'type': 'ipv4', 'address': '0.0.0.0', 'port': port}
3985 elif b['type'] == 'ipv6':
3986 assert b == {'type': 'ipv6', 'address': '::', 'port': port}
3987 else:
3988 assert b == {'type': 'websocket',
3989 'address': '127.0.0.1',
3990 'subtype': 'ipv4',
3991 'port': ws_port}
3992
3993 # Adapter to turn websocket into a stream "connection"
3994 class BinWebSocket(object):
3995 def __init__(self, hostname, port):
3996 self.ws = websocket.WebSocket()
3997 self.ws.connect("ws://" + hostname + ":" + str(port))
3998 self.recvbuf = bytes()
3999
4000 def send(self, data):
4001 self.ws.send(data, websocket.ABNF.OPCODE_BINARY)
4002
4003 def recv(self, maxlen):
4004 while len(self.recvbuf) < maxlen:
4005 self.recvbuf += self.ws.recv()
4006
4007 ret = self.recvbuf[:maxlen]
4008 self.recvbuf = self.recvbuf[maxlen:]
4009 return ret
4010
4011 ws = BinWebSocket('localhost', ws_port)
4012 lconn = wire.LightningConnection(ws,
4013 wire.PublicKey(bytes.fromhex(l1.info['id'])),
4014 wire.PrivateKey(bytes([1] * 32)),
4015 is_initiator=True)
4016
4017 l1.daemon.wait_for_log('Websocket connection in from')
4018
4019 # Perform handshake.
4020 lconn.shake()
4021
4022 # Expect to receive init msg.
4023 msg = lconn.read_message()
4024 assert int.from_bytes(msg[0:2], 'big') == 16
4025
4026 # Echo same message back.
4027 lconn.send_message(msg)
4028
4029 # Now try sending a ping, ask for 50 bytes

Callers

nothing calls this directly

Calls 12

shakeMethod · 0.95
read_messageMethod · 0.95
send_messageMethod · 0.95
only_oneFunction · 0.90
BinWebSocketClass · 0.85
get_unused_portMethod · 0.80
line_graphMethod · 0.80
wait_for_logMethod · 0.80
anyClass · 0.50
getinfoMethod · 0.45
from_bytesMethod · 0.45
listnodesMethod · 0.45

Tested by

no test coverage detected