(node_factory)
| 3921 | |
| 3922 | |
| 3923 | def test_websocket(node_factory): |
| 3924 | ws_port = node_factory.get_unused_port() |
| 3925 | port = node_factory.get_unused_port() |
| 3926 | l1, l2 = node_factory.line_graph(2, |
| 3927 | opts=[{'addr': ':' + str(port), |
| 3928 | 'bind-addr': 'ws:127.0.0.1: ' + str(ws_port), |
| 3929 | 'dev-allow-localhost': None}, |
| 3930 | {'dev-allow-localhost': None}], |
| 3931 | wait_for_announce=True) |
| 3932 | # Some depend on ipv4 vs ipv6 behaviour... |
| 3933 | for b in l1.rpc.getinfo()['binding']: |
| 3934 | if b['type'] == 'ipv4': |
| 3935 | assert b == {'type': 'ipv4', 'address': '0.0.0.0', 'port': port} |
| 3936 | elif b['type'] == 'ipv6': |
| 3937 | assert b == {'type': 'ipv6', 'address': '::', 'port': port} |
| 3938 | else: |
| 3939 | assert b == {'type': 'websocket', |
| 3940 | 'address': '127.0.0.1', |
| 3941 | 'subtype': 'ipv4', |
| 3942 | 'port': ws_port} |
| 3943 | |
| 3944 | # Adapter to turn websocket into a stream "connection" |
| 3945 | class BinWebSocket(object): |
| 3946 | def __init__(self, hostname, port): |
| 3947 | self.ws = websocket.WebSocket() |
| 3948 | self.ws.connect("ws://" + hostname + ":" + str(port)) |
| 3949 | self.recvbuf = bytes() |
| 3950 | |
| 3951 | def send(self, data): |
| 3952 | self.ws.send(data, websocket.ABNF.OPCODE_BINARY) |
| 3953 | |
| 3954 | def recv(self, maxlen): |
| 3955 | while len(self.recvbuf) < maxlen: |
| 3956 | self.recvbuf += self.ws.recv() |
| 3957 | |
| 3958 | ret = self.recvbuf[:maxlen] |
| 3959 | self.recvbuf = self.recvbuf[maxlen:] |
| 3960 | return ret |
| 3961 | |
| 3962 | ws = BinWebSocket('localhost', ws_port) |
| 3963 | lconn = wire.LightningConnection(ws, |
| 3964 | wire.PublicKey(bytes.fromhex(l1.info['id'])), |
| 3965 | wire.PrivateKey(bytes([1] * 32)), |
| 3966 | is_initiator=True) |
| 3967 | |
| 3968 | l1.daemon.wait_for_log('Websocket connection in from') |
| 3969 | |
| 3970 | # Perform handshake. |
| 3971 | lconn.shake() |
| 3972 | |
| 3973 | # Expect to receive init msg. |
| 3974 | msg = lconn.read_message() |
| 3975 | assert int.from_bytes(msg[0:2], 'big') == 16 |
| 3976 | |
| 3977 | # Echo same message back. |
| 3978 | lconn.send_message(msg) |
| 3979 | |
| 3980 | # Now try sending a ping, ask for 50 bytes |
nothing calls this directly
no test coverage detected