mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 06:38:20 +00:00
cluster/websocket: Implement WebSocket server
This commit is contained in:
parent
1e757b2b59
commit
6032741868
75 changed files with 2860 additions and 4 deletions
126
testing/btest/cluster/websocket/bad-event-args.zeek
Normal file
126
testing/btest/cluster/websocket/bad-event-args.zeek
Normal file
|
@ -0,0 +1,126 @@
|
|||
# @TEST-DOC: A WebSocket client sending invalid data for an event.
|
||||
#
|
||||
# @TEST-REQUIRES: have-zeromq
|
||||
# @TEST-REQUIRES: python3 -c 'import websockets.sync'
|
||||
#
|
||||
# @TEST-GROUP: cluster-zeromq
|
||||
#
|
||||
# @TEST-PORT: XPUB_PORT
|
||||
# @TEST-PORT: XSUB_PORT
|
||||
# @TEST-PORT: LOG_PULL_PORT
|
||||
# @TEST-PORT: WEBSOCKET_PORT
|
||||
#
|
||||
# @TEST-EXEC: cp $FILES/zeromq/cluster-layout-simple.zeek cluster-layout.zeek
|
||||
# @TEST-EXEC: cp $FILES/zeromq/test-bootstrap.zeek zeromq-test-bootstrap.zeek
|
||||
#
|
||||
# @TEST-EXEC: zeek -b --parse-only manager.zeek
|
||||
# @TEST-EXEC: python3 -m py_compile client.py
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-run manager "ZEEKPATH=$ZEEKPATH:.. && CLUSTER_NODE=manager zeek -b ../manager.zeek >out"
|
||||
# @TEST-EXEC: btest-bg-run client "python3 ../client.py >out"
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-wait 30
|
||||
# @TEST-EXEC: btest-diff ./manager/out
|
||||
# @TEST-EXEC: btest-diff ./manager/.stderr
|
||||
# @TEST-EXEC: btest-diff ./client/out
|
||||
# @TEST-EXEC: btest-diff ./client/.stderr
|
||||
|
||||
# @TEST-START-FILE manager.zeek
|
||||
@load ./zeromq-test-bootstrap
|
||||
redef exit_only_after_terminate = T;
|
||||
|
||||
global ping: event(msg: string, c: count) &is_used;
|
||||
global pong: event(msg: string, c: count) &is_used;
|
||||
|
||||
event zeek_init()
|
||||
{
|
||||
Cluster::subscribe("/zeek/event/my_topic");
|
||||
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
|
||||
}
|
||||
|
||||
event ping(msg: string, n: count) &is_used
|
||||
{
|
||||
print fmt("got ping: %s, %s", msg, n);
|
||||
local e = Cluster::make_event(pong, msg, n);
|
||||
Cluster::publish("/zeek/event/my_topic", e);
|
||||
}
|
||||
|
||||
event Cluster::websocket_client_added(info: Cluster::EndpointInfo, subscriptions: string_vec)
|
||||
{
|
||||
print "Cluster::websocket_client_added", subscriptions;
|
||||
}
|
||||
|
||||
event Cluster::websocket_client_lost(info: Cluster::EndpointInfo)
|
||||
{
|
||||
print "Cluster::websocket_client_lost";
|
||||
terminate();
|
||||
}
|
||||
# @TEST-END-FILE
|
||||
|
||||
|
||||
@TEST-START-FILE client.py
|
||||
import json, os, time
|
||||
from websockets.sync.client import connect
|
||||
|
||||
ws_port = os.environ['WEBSOCKET_PORT'].split('/')[0]
|
||||
ws_url = f'ws://127.0.0.1:{ws_port}/v1/messages/json'
|
||||
topic = '/zeek/event/my_topic'
|
||||
|
||||
def make_ping(event_args):
|
||||
return {
|
||||
"type": "data-message",
|
||||
"topic": topic,
|
||||
"@data-type": "vector",
|
||||
"data": [
|
||||
{"@data-type": "count", "data": 1}, # Format
|
||||
{"@data-type": "count", "data": 1}, # Type
|
||||
{"@data-type": "vector", "data": [
|
||||
{ "@data-type": "string", "data": "ping"}, # Event name
|
||||
{ "@data-type": "vector", "data": event_args },
|
||||
], },
|
||||
],
|
||||
}
|
||||
|
||||
def run(ws_url):
|
||||
with connect(ws_url) as ws:
|
||||
print("Connected!")
|
||||
# Send subscriptions
|
||||
ws.send(json.dumps([topic]))
|
||||
ack = json.loads(ws.recv())
|
||||
assert "type" in ack
|
||||
assert ack["type"] == "ack"
|
||||
assert "endpoint" in ack
|
||||
assert "version" in ack
|
||||
|
||||
ws.send(json.dumps(make_ping(42)))
|
||||
err1 = json.loads(ws.recv())
|
||||
print("err1", err1)
|
||||
ws.send(json.dumps(make_ping([{"@data-type": "string", "data": "Hello"}])))
|
||||
err2 = json.loads(ws.recv())
|
||||
print("err2", err2)
|
||||
ws.send(json.dumps(make_ping([{"@data-type": "count", "data": 42}, {"@data-type": "string", "data": "Hello"}])))
|
||||
err3 = json.loads(ws.recv())
|
||||
print("err3", err3)
|
||||
|
||||
# This should be good ping(string, count)
|
||||
ws.send(json.dumps(make_ping([{"@data-type": "string", "data": "Hello"}, {"@data-type": "count", "data": 42}])))
|
||||
pong = json.loads(ws.recv())
|
||||
name, args, _ = pong["data"][2]["data"]
|
||||
print("pong", name, args)
|
||||
|
||||
# This one fails again
|
||||
ws.send(json.dumps(make_ping([{"@data-type": "money", "data": 0}])))
|
||||
err4 = json.loads(ws.recv())
|
||||
print("err4", err4)
|
||||
|
||||
def main():
|
||||
for _ in range(100):
|
||||
try:
|
||||
run(ws_url)
|
||||
break
|
||||
except ConnectionRefusedError:
|
||||
time.sleep(0.1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@TEST-END-FILE
|
102
testing/btest/cluster/websocket/bad-subscriptions.zeek
Normal file
102
testing/btest/cluster/websocket/bad-subscriptions.zeek
Normal file
|
@ -0,0 +1,102 @@
|
|||
# @TEST-DOC: Clients sends broken subscription arrays
|
||||
#
|
||||
# @TEST-REQUIRES: have-zeromq
|
||||
# @TEST-REQUIRES: python3 -c 'import websockets.sync'
|
||||
#
|
||||
# @TEST-GROUP: cluster-zeromq
|
||||
#
|
||||
# @TEST-PORT: XPUB_PORT
|
||||
# @TEST-PORT: XSUB_PORT
|
||||
# @TEST-PORT: LOG_PULL_PORT
|
||||
# @TEST-PORT: WEBSOCKET_PORT
|
||||
#
|
||||
# @TEST-EXEC: cp $FILES/zeromq/cluster-layout-simple.zeek cluster-layout.zeek
|
||||
# @TEST-EXEC: cp $FILES/zeromq/test-bootstrap.zeek zeromq-test-bootstrap.zeek
|
||||
#
|
||||
# @TEST-EXEC: zeek -b --parse-only manager.zeek
|
||||
# @TEST-EXEC: python3 -m py_compile client.py
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-run manager "ZEEKPATH=$ZEEKPATH:.. && CLUSTER_NODE=manager zeek -b ../manager.zeek >out"
|
||||
# @TEST-EXEC: btest-bg-run client "python3 ../client.py >out"
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-wait 30
|
||||
# @TEST-EXEC: btest-diff ./manager/out
|
||||
# @TEST-EXEC: btest-diff ./manager/.stderr
|
||||
# @TEST-EXEC: btest-diff ./client/out
|
||||
# @TEST-EXEC: btest-diff ./client/.stderr
|
||||
|
||||
# @TEST-START-FILE manager.zeek
|
||||
@load ./zeromq-test-bootstrap
|
||||
redef exit_only_after_terminate = T;
|
||||
|
||||
global event_count = 0;
|
||||
|
||||
global ping: event(msg: string, c: count) &is_used;
|
||||
global pong: event(msg: string, c: count) &is_used;
|
||||
|
||||
event zeek_init()
|
||||
{
|
||||
Cluster::subscribe("/zeek/event/my_topic");
|
||||
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
|
||||
}
|
||||
|
||||
global added = 0;
|
||||
global lost = 0;
|
||||
|
||||
event Cluster::websocket_client_added(info: Cluster::EndpointInfo, subscriptions: string_vec)
|
||||
{
|
||||
++added;
|
||||
print "Cluster::websocket_client_added", added, subscriptions;
|
||||
}
|
||||
|
||||
event Cluster::websocket_client_lost(info: Cluster::EndpointInfo)
|
||||
{
|
||||
++lost;
|
||||
print "Cluster::websocket_client_lost", lost;
|
||||
if ( lost == 4 )
|
||||
terminate();
|
||||
}
|
||||
# @TEST-END-FILE
|
||||
|
||||
|
||||
@TEST-START-FILE client.py
|
||||
import json, os, time
|
||||
from websockets.sync.client import connect
|
||||
|
||||
ws_port = os.environ['WEBSOCKET_PORT'].split('/')[0]
|
||||
ws_url = f'ws://127.0.0.1:{ws_port}/v1/messages/json'
|
||||
topic = '/zeek/event/my_topic'
|
||||
|
||||
def run(ws_url):
|
||||
with connect(ws_url) as ws:
|
||||
ws.send('["broken", "brrr')
|
||||
err = json.loads(ws.recv())
|
||||
print("broken array response", err)
|
||||
|
||||
with connect(ws_url) as ws:
|
||||
ws.send('[1, 2]')
|
||||
err = json.loads(ws.recv())
|
||||
print("non string error", err)
|
||||
|
||||
with connect(ws_url) as ws:
|
||||
ws.send('[1, "/my_topic"]')
|
||||
err = json.loads(ws.recv())
|
||||
print("mix error", err)
|
||||
|
||||
# This should work - maybe duplicate isn't great, but good for testing.
|
||||
with connect(ws_url) as ws:
|
||||
ws.send('["/topic/good", "/duplicate", "/duplicate", "/is/okay"]')
|
||||
ack = json.loads(ws.recv())
|
||||
print("ack", ack["type"] == "ack")
|
||||
|
||||
def main():
|
||||
for _ in range(100):
|
||||
try:
|
||||
run(ws_url)
|
||||
break
|
||||
except ConnectionRefusedError:
|
||||
time.sleep(0.1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@TEST-END-FILE
|
101
testing/btest/cluster/websocket/bad-url.zeek
Normal file
101
testing/btest/cluster/websocket/bad-url.zeek
Normal file
|
@ -0,0 +1,101 @@
|
|||
# @TEST-DOC: Run a single node cluster (manager) with a websocket server and have a single client connect.
|
||||
#
|
||||
# @TEST-REQUIRES: have-zeromq
|
||||
# @TEST-REQUIRES: python3 -c 'import websockets.sync'
|
||||
#
|
||||
# @TEST-GROUP: cluster-zeromq
|
||||
#
|
||||
# @TEST-PORT: XPUB_PORT
|
||||
# @TEST-PORT: XSUB_PORT
|
||||
# @TEST-PORT: LOG_PULL_PORT
|
||||
# @TEST-PORT: WEBSOCKET_PORT
|
||||
#
|
||||
# @TEST-EXEC: cp $FILES/zeromq/cluster-layout-simple.zeek cluster-layout.zeek
|
||||
# @TEST-EXEC: cp $FILES/zeromq/test-bootstrap.zeek zeromq-test-bootstrap.zeek
|
||||
#
|
||||
# @TEST-EXEC: zeek -b --parse-only manager.zeek
|
||||
# @TEST-EXEC: python3 -m py_compile client.py
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-run manager "ZEEKPATH=$ZEEKPATH:.. && CLUSTER_NODE=manager zeek -b ../manager.zeek >out"
|
||||
# @TEST-EXEC: btest-bg-run client "python3 ../client.py >out"
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-wait 5
|
||||
# @TEST-EXEC: btest-diff ./manager/out
|
||||
# @TEST-EXEC: btest-diff ./manager/.stderr
|
||||
# @TEST-EXEC: btest-diff ./client/out
|
||||
# @TEST-EXEC: btest-diff ./client/.stderr
|
||||
|
||||
# @TEST-START-FILE manager.zeek
|
||||
@load ./zeromq-test-bootstrap
|
||||
redef exit_only_after_terminate = T;
|
||||
|
||||
global ping_count = 0;
|
||||
|
||||
global ping: event(msg: string, c: count) &is_used;
|
||||
global pong: event(msg: string, c: count) &is_used;
|
||||
|
||||
event zeek_init()
|
||||
{
|
||||
Cluster::subscribe("/zeek/event/my_topic");
|
||||
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
|
||||
}
|
||||
|
||||
event ping(msg: string, n: count) &is_used
|
||||
{
|
||||
++ping_count;
|
||||
print fmt("got ping: %s, %s", msg, n);
|
||||
local e = Cluster::make_event(pong, "my-message", ping_count);
|
||||
Cluster::publish("/zeek/event/my_topic", e);
|
||||
}
|
||||
|
||||
event Cluster::websocket_client_added(info: Cluster::EndpointInfo, subscriptions: string_vec)
|
||||
{
|
||||
print "Cluster::websocket_client_added", subscriptions;
|
||||
}
|
||||
|
||||
event Cluster::websocket_client_lost(info: Cluster::EndpointInfo)
|
||||
{
|
||||
print "Cluster::websocket_client_lost";
|
||||
terminate();
|
||||
}
|
||||
# @TEST-END-FILE
|
||||
|
||||
|
||||
@TEST-START-FILE client.py
|
||||
import json, os, time
|
||||
import websockets.exceptions
|
||||
from websockets.sync.client import connect
|
||||
|
||||
ws_port = os.environ['WEBSOCKET_PORT'].split('/')[0]
|
||||
ws_prefix = f'ws://127.0.0.1:{ws_port}'
|
||||
topic = '/zeek/event/my_topic'
|
||||
|
||||
|
||||
def run(ws_prefix):
|
||||
with connect(ws_prefix + '/v1/messages/json') as ws_good:
|
||||
print('Connected ws_good!')
|
||||
with connect(ws_prefix + '/v0/messages/json') as ws_bad:
|
||||
print('Connected ws_bad!')
|
||||
try:
|
||||
err = json.loads(ws_bad.recv())
|
||||
except websockets.exceptions.ConnectionClosedError as e:
|
||||
pass
|
||||
|
||||
print('Error for ws_bad', err)
|
||||
|
||||
ws_good.send(json.dumps(['hello-good']))
|
||||
ack = json.loads(ws_good.recv())
|
||||
assert 'type' in ack
|
||||
assert ack['type'] == 'ack'
|
||||
|
||||
def main():
|
||||
for _ in range(100):
|
||||
try:
|
||||
run(ws_prefix)
|
||||
break
|
||||
except ConnectionRefusedError:
|
||||
time.sleep(0.1)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@TEST-END-FILE
|
111
testing/btest/cluster/websocket/cluster-log.zeek
Normal file
111
testing/btest/cluster/websocket/cluster-log.zeek
Normal file
|
@ -0,0 +1,111 @@
|
|||
# @TEST-DOC: Test websocket clients appearing in cluster.log
|
||||
#
|
||||
# @TEST-REQUIRES: have-zeromq
|
||||
# @TEST-REQUIRES: python3 -c 'import websockets.sync'
|
||||
#
|
||||
# @TEST-GROUP: cluster-zeromq
|
||||
#
|
||||
# @TEST-PORT: XPUB_PORT
|
||||
# @TEST-PORT: XSUB_PORT
|
||||
# @TEST-PORT: LOG_PULL_PORT
|
||||
# @TEST-PORT: WEBSOCKET_PORT
|
||||
#
|
||||
# @TEST-EXEC: cp $FILES/zeromq/cluster-layout-simple.zeek cluster-layout.zeek
|
||||
# @TEST-EXEC: cp $FILES/zeromq/test-bootstrap.zeek zeromq-test-bootstrap.zeek
|
||||
#
|
||||
# @TEST-EXEC: zeek -b --parse-only manager.zeek
|
||||
# @TEST-EXEC: python3 -m py_compile client.py
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-run manager "ZEEKPATH=$ZEEKPATH:.. && CLUSTER_NODE=manager zeek -b ../manager.zeek >out"
|
||||
# @TEST-EXEC: btest-bg-run client "python3 ../client.py >out"
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-wait 30
|
||||
# @TEST-EXEC: btest-diff ./manager/out
|
||||
# @TEST-EXEC: btest-diff ./manager/.stderr
|
||||
# @TEST-EXEC: zeek-cut node message < ./manager/cluster.log | sed -r "s/client '.+' /client <nodeid> /g" | sed -r "s/:[0-9]+/:<port>/g" > ./manager/cluster.log.cannonified
|
||||
# @TEST-EXEC: btest-diff ./manager/cluster.log.cannonified
|
||||
# @TEST-EXEC: btest-diff ./client/out
|
||||
# @TEST-EXEC: btest-diff ./client/.stderr
|
||||
|
||||
# @TEST-START-FILE manager.zeek
|
||||
@load ./zeromq-test-bootstrap
|
||||
redef exit_only_after_terminate = T;
|
||||
|
||||
# Have the manager create cluster.log
|
||||
redef Log::enable_local_logging = T;
|
||||
redef Log::default_rotation_interval = 0sec;
|
||||
|
||||
global ping_count = 0;
|
||||
|
||||
global ping: event(msg: string, c: count) &is_used;
|
||||
global pong: event(msg: string, c: count) &is_used;
|
||||
|
||||
event zeek_init()
|
||||
{
|
||||
Cluster::subscribe("/test/manager");
|
||||
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
|
||||
}
|
||||
|
||||
event ping(msg: string, n: count) &is_used
|
||||
{
|
||||
++ping_count;
|
||||
print fmt("got ping: %s, %s", msg, n);
|
||||
local e = Cluster::make_event(pong, fmt("orig_msg=%s", msg), ping_count);
|
||||
Cluster::publish("/test/clients", e);
|
||||
}
|
||||
|
||||
global added = 0;
|
||||
global lost = 0;
|
||||
|
||||
event Cluster::websocket_client_added(info: Cluster::EndpointInfo, subscriptions: string_vec)
|
||||
{
|
||||
++added;
|
||||
print "Cluster::websocket_client_added", added, subscriptions;
|
||||
}
|
||||
|
||||
event Cluster::websocket_client_lost(info: Cluster::EndpointInfo)
|
||||
{
|
||||
++lost;
|
||||
print "Cluster::websocket_client_lost", lost;
|
||||
if ( lost == 3 )
|
||||
terminate();
|
||||
}
|
||||
# @TEST-END-FILE
|
||||
|
||||
|
||||
@TEST-START-FILE client.py
|
||||
import json, os, time
|
||||
from websockets.sync.client import connect
|
||||
|
||||
ws_port = os.environ['WEBSOCKET_PORT'].split('/')[0]
|
||||
ws_url = f'ws://127.0.0.1:{ws_port}/v1/messages/json'
|
||||
|
||||
def run(ws_url):
|
||||
with connect(ws_url) as ws1:
|
||||
with connect(ws_url) as ws2:
|
||||
with connect(ws_url) as ws3:
|
||||
clients = [ws1, ws2, ws3]
|
||||
print("Connected!")
|
||||
ids = set()
|
||||
for i, c in enumerate(clients, 1):
|
||||
c.send(json.dumps([f"/topic/ws/{i}", "/topic/ws/all"]))
|
||||
ack = json.loads(c.recv())
|
||||
assert "type" in ack, repr(ack)
|
||||
assert ack["type"] == "ack"
|
||||
assert "endpoint" in ack, repr(ack)
|
||||
assert "version" in ack
|
||||
ids.add(ack["endpoint"])
|
||||
|
||||
print("unique ids", len(ids))
|
||||
|
||||
def main():
|
||||
for _ in range(100):
|
||||
try:
|
||||
run(ws_url)
|
||||
break
|
||||
except ConnectionRefusedError:
|
||||
time.sleep(0.1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@TEST-END-FILE
|
50
testing/btest/cluster/websocket/listen-idempotent.zeek
Normal file
50
testing/btest/cluster/websocket/listen-idempotent.zeek
Normal file
|
@ -0,0 +1,50 @@
|
|||
# @TEST-DOC: Allow listening with the same tls_options on the same port, but fail for disagreeing tls_options.
|
||||
#
|
||||
# @TEST-EXEC: zeek -b %INPUT
|
||||
# @TEST-EXEC: TEST_DIFF_CANONIFIER='sed -E "s/127.0.0.1:[0-9]+/127.0.0.1:<port>/g" | $SCRIPTS/diff-remove-abspath' btest-diff .stderr
|
||||
#
|
||||
# @TEST-PORT: WEBSOCKET_PORT
|
||||
# @TEST-PORT: WEBSOCKET_SECURE_PORT
|
||||
|
||||
event zeek_init()
|
||||
{
|
||||
local tls_options = Cluster::WebSocketTLSOptions(
|
||||
$cert_file="../localhost.crt",
|
||||
$key_file="../localhost.key",
|
||||
);
|
||||
|
||||
local tls_options_2 = Cluster::WebSocketTLSOptions(
|
||||
$cert_file="../localhost.crt",
|
||||
$key_file="../localhost.key",
|
||||
);
|
||||
local ws_port = to_port(getenv("WEBSOCKET_PORT"));
|
||||
local wss_port = to_port(getenv("WEBSOCKET_SECURE_PORT"));
|
||||
|
||||
local ws_opts = Cluster::WebSocketServerOptions($listen_host="127.0.0.1", $listen_port=ws_port);
|
||||
local ws_opts_x = copy(ws_opts);
|
||||
ws_opts_x$tls_options = tls_options;
|
||||
|
||||
local ws_opts_wss_port = Cluster::WebSocketServerOptions($listen_host="127.0.0.1", $listen_port=wss_port);
|
||||
|
||||
local ws_tls_opts = Cluster::WebSocketServerOptions(
|
||||
$listen_host="127.0.0.1",
|
||||
$listen_port=wss_port,
|
||||
$tls_options=tls_options,
|
||||
);
|
||||
# Same as ws_tls_opts
|
||||
local ws_tls_opts_copy = Cluster::WebSocketServerOptions(
|
||||
$listen_host="127.0.0.1",
|
||||
$listen_port=wss_port,
|
||||
$tls_options=tls_options_2,
|
||||
);
|
||||
|
||||
assert Cluster::listen_websocket(ws_opts);
|
||||
assert Cluster::listen_websocket(ws_opts);
|
||||
assert ! Cluster::listen_websocket(ws_opts_x);
|
||||
assert Cluster::listen_websocket(ws_tls_opts);
|
||||
assert Cluster::listen_websocket(ws_tls_opts);
|
||||
assert Cluster::listen_websocket(ws_tls_opts_copy);
|
||||
assert ! Cluster::listen_websocket(ws_opts_wss_port);
|
||||
|
||||
terminate();
|
||||
}
|
123
testing/btest/cluster/websocket/one-pipelining.zeek
Normal file
123
testing/btest/cluster/websocket/one-pipelining.zeek
Normal file
|
@ -0,0 +1,123 @@
|
|||
# @TEST-DOC: Send subscriptions and events without waiting for pong, should be okay, the websocket server will queue this a bit.
|
||||
#
|
||||
# @TEST-REQUIRES: have-zeromq
|
||||
# @TEST-REQUIRES: python3 -c 'import websockets.sync'
|
||||
#
|
||||
# @TEST-GROUP: cluster-zeromq
|
||||
#
|
||||
# @TEST-PORT: XPUB_PORT
|
||||
# @TEST-PORT: XSUB_PORT
|
||||
# @TEST-PORT: LOG_PULL_PORT
|
||||
# @TEST-PORT: WEBSOCKET_PORT
|
||||
#
|
||||
# @TEST-EXEC: cp $FILES/zeromq/cluster-layout-simple.zeek cluster-layout.zeek
|
||||
# @TEST-EXEC: cp $FILES/zeromq/test-bootstrap.zeek zeromq-test-bootstrap.zeek
|
||||
#
|
||||
# @TEST-EXEC: zeek -b --parse-only manager.zeek
|
||||
# @TEST-EXEC: python3 -m py_compile client.py
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-run manager "ZEEKPATH=$ZEEKPATH:.. && CLUSTER_NODE=manager zeek -b ../manager.zeek >out"
|
||||
# @TEST-EXEC: btest-bg-run client "python3 ../client.py >out"
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-wait 30
|
||||
# @TEST-EXEC: btest-diff ./manager/out
|
||||
# @TEST-EXEC: btest-diff ./manager/.stderr
|
||||
# @TEST-EXEC: btest-diff ./client/out
|
||||
# @TEST-EXEC: btest-diff ./client/.stderr
|
||||
|
||||
# @TEST-START-FILE manager.zeek
|
||||
@load ./zeromq-test-bootstrap
|
||||
redef exit_only_after_terminate = T;
|
||||
|
||||
global ping_count = 0;
|
||||
|
||||
global ping: event(msg: string, c: count) &is_used;
|
||||
global pong: event(msg: string, c: count) &is_used;
|
||||
|
||||
event zeek_init()
|
||||
{
|
||||
Cluster::subscribe("/zeek/event/my_topic");
|
||||
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
|
||||
}
|
||||
|
||||
event ping(msg: string, n: count) &is_used
|
||||
{
|
||||
++ping_count;
|
||||
print fmt("got ping: %s, %s", msg, n);
|
||||
local e = Cluster::make_event(pong, "my-message", ping_count);
|
||||
Cluster::publish("/zeek/event/my_topic", e);
|
||||
}
|
||||
|
||||
event Cluster::websocket_client_added(info: Cluster::EndpointInfo, subscriptions: string_vec)
|
||||
{
|
||||
print "Cluster::websocket_client_added", subscriptions;
|
||||
}
|
||||
|
||||
event Cluster::websocket_client_lost(info: Cluster::EndpointInfo)
|
||||
{
|
||||
print "Cluster::websocket_client_lost";
|
||||
terminate();
|
||||
}
|
||||
# @TEST-END-FILE
|
||||
|
||||
|
||||
@TEST-START-FILE client.py
|
||||
import json, os, time
|
||||
from websockets.sync.client import connect
|
||||
|
||||
ws_port = os.environ['WEBSOCKET_PORT'].split('/')[0]
|
||||
ws_url = f'ws://127.0.0.1:{ws_port}/v1/messages/json'
|
||||
topic = '/zeek/event/my_topic'
|
||||
|
||||
def make_ping(c):
|
||||
return {
|
||||
"type": "data-message",
|
||||
"topic": topic,
|
||||
"@data-type": "vector",
|
||||
"data": [
|
||||
{"@data-type": "count", "data": 1}, # Format
|
||||
{"@data-type": "count", "data": 1}, # Type
|
||||
{"@data-type": "vector", "data": [
|
||||
{ "@data-type": "string", "data": "ping"}, # Event name
|
||||
{ "@data-type": "vector", "data": [ # event args
|
||||
{"@data-type": "string", "data": f"python-websocket-client"},
|
||||
{"@data-type": "count", "data": c},
|
||||
], },
|
||||
], },
|
||||
],
|
||||
}
|
||||
|
||||
def run(ws_url):
|
||||
with connect(ws_url) as ws:
|
||||
print("Connected!")
|
||||
# Send subscriptions
|
||||
ws.send(json.dumps([topic]))
|
||||
|
||||
for i in range(5):
|
||||
print("Sending ping", i)
|
||||
ws.send(json.dumps(make_ping(i)))
|
||||
|
||||
ack = json.loads(ws.recv())
|
||||
assert "type" in ack
|
||||
assert ack["type"] == "ack"
|
||||
assert "endpoint" in ack
|
||||
assert "version" in ack
|
||||
|
||||
for i in range(5):
|
||||
print("Receiving pong", i)
|
||||
pong = json.loads(ws.recv())
|
||||
assert pong["@data-type"] == "vector"
|
||||
ev = pong["data"][2]["data"]
|
||||
print("topic", pong["topic"], "event name", ev[0]["data"], "args", ev[1]["data"])
|
||||
|
||||
def main():
|
||||
for _ in range(100):
|
||||
try:
|
||||
run(ws_url)
|
||||
break
|
||||
except ConnectionRefusedError:
|
||||
time.sleep(0.1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@TEST-END-FILE
|
120
testing/btest/cluster/websocket/one.zeek
Normal file
120
testing/btest/cluster/websocket/one.zeek
Normal file
|
@ -0,0 +1,120 @@
|
|||
# @TEST-DOC: Run a single node cluster (manager) with a websocket server and have a single client connect.
|
||||
#
|
||||
# @TEST-REQUIRES: have-zeromq
|
||||
# @TEST-REQUIRES: python3 -c 'import websockets.sync'
|
||||
#
|
||||
# @TEST-GROUP: cluster-zeromq
|
||||
#
|
||||
# @TEST-PORT: XPUB_PORT
|
||||
# @TEST-PORT: XSUB_PORT
|
||||
# @TEST-PORT: LOG_PULL_PORT
|
||||
# @TEST-PORT: WEBSOCKET_PORT
|
||||
#
|
||||
# @TEST-EXEC: cp $FILES/zeromq/cluster-layout-simple.zeek cluster-layout.zeek
|
||||
# @TEST-EXEC: cp $FILES/zeromq/test-bootstrap.zeek zeromq-test-bootstrap.zeek
|
||||
#
|
||||
# @TEST-EXEC: zeek -b --parse-only manager.zeek
|
||||
# @TEST-EXEC: python3 -m py_compile client.py
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-run manager "ZEEKPATH=$ZEEKPATH:.. && CLUSTER_NODE=manager zeek -b ../manager.zeek >out"
|
||||
# @TEST-EXEC: btest-bg-run client "python3 ../client.py >out"
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-wait 30
|
||||
# @TEST-EXEC: btest-diff ./manager/out
|
||||
# @TEST-EXEC: btest-diff ./manager/.stderr
|
||||
# @TEST-EXEC: btest-diff ./client/out
|
||||
# @TEST-EXEC: btest-diff ./client/.stderr
|
||||
|
||||
# @TEST-START-FILE manager.zeek
|
||||
@load ./zeromq-test-bootstrap
|
||||
redef exit_only_after_terminate = T;
|
||||
|
||||
global ping_count = 0;
|
||||
|
||||
global ping: event(msg: string, c: count) &is_used;
|
||||
global pong: event(msg: string, c: count) &is_used;
|
||||
|
||||
event zeek_init()
|
||||
{
|
||||
Cluster::subscribe("/zeek/event/my_topic");
|
||||
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
|
||||
}
|
||||
|
||||
event ping(msg: string, n: count) &is_used
|
||||
{
|
||||
++ping_count;
|
||||
print fmt("got ping: %s, %s", msg, n);
|
||||
local e = Cluster::make_event(pong, "my-message", ping_count);
|
||||
Cluster::publish("/zeek/event/my_topic", e);
|
||||
}
|
||||
|
||||
event Cluster::websocket_client_added(info: Cluster::EndpointInfo, subscriptions: string_vec)
|
||||
{
|
||||
print "Cluster::websocket_client_added", subscriptions;
|
||||
}
|
||||
|
||||
event Cluster::websocket_client_lost(info: Cluster::EndpointInfo)
|
||||
{
|
||||
print "Cluster::websocket_client_lost";
|
||||
terminate();
|
||||
}
|
||||
# @TEST-END-FILE
|
||||
|
||||
|
||||
@TEST-START-FILE client.py
|
||||
import json, os, time
|
||||
from websockets.sync.client import connect
|
||||
|
||||
ws_port = os.environ['WEBSOCKET_PORT'].split('/')[0]
|
||||
ws_url = f'ws://127.0.0.1:{ws_port}/v1/messages/json'
|
||||
topic = '/zeek/event/my_topic'
|
||||
|
||||
def make_ping(c):
|
||||
return {
|
||||
"type": "data-message",
|
||||
"topic": topic,
|
||||
"@data-type": "vector",
|
||||
"data": [
|
||||
{"@data-type": "count", "data": 1}, # Format
|
||||
{"@data-type": "count", "data": 1}, # Type
|
||||
{"@data-type": "vector", "data": [
|
||||
{ "@data-type": "string", "data": "ping"}, # Event name
|
||||
{ "@data-type": "vector", "data": [ # event args
|
||||
{"@data-type": "string", "data": f"python-websocket-client"},
|
||||
{"@data-type": "count", "data": c},
|
||||
], },
|
||||
], },
|
||||
],
|
||||
}
|
||||
|
||||
def run(ws_url):
|
||||
with connect(ws_url) as ws:
|
||||
print("Connected!")
|
||||
# Send subscriptions
|
||||
ws.send(json.dumps([topic]))
|
||||
ack = json.loads(ws.recv())
|
||||
assert "type" in ack
|
||||
assert ack["type"] == "ack"
|
||||
assert "endpoint" in ack
|
||||
assert "version" in ack
|
||||
|
||||
for i in range(5):
|
||||
print("Sending ping", i)
|
||||
ws.send(json.dumps(make_ping(i)))
|
||||
print("Receiving pong", i)
|
||||
pong = json.loads(ws.recv())
|
||||
assert pong["@data-type"] == "vector"
|
||||
ev = pong["data"][2]["data"]
|
||||
print("topic", pong["topic"], "event name", ev[0]["data"], "args", ev[1]["data"])
|
||||
|
||||
def main():
|
||||
for _ in range(100):
|
||||
try:
|
||||
run(ws_url)
|
||||
break
|
||||
except ConnectionRefusedError:
|
||||
time.sleep(0.1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@TEST-END-FILE
|
137
testing/btest/cluster/websocket/three.zeek
Normal file
137
testing/btest/cluster/websocket/three.zeek
Normal file
|
@ -0,0 +1,137 @@
|
|||
# @TEST-DOC: Run a single node cluster (manager) with a websocket server, have three clients connect.
|
||||
#
|
||||
# @TEST-REQUIRES: have-zeromq
|
||||
# @TEST-REQUIRES: python3 -c 'import websockets.sync'
|
||||
#
|
||||
# @TEST-GROUP: cluster-zeromq
|
||||
#
|
||||
# @TEST-PORT: XPUB_PORT
|
||||
# @TEST-PORT: XSUB_PORT
|
||||
# @TEST-PORT: LOG_PULL_PORT
|
||||
# @TEST-PORT: WEBSOCKET_PORT
|
||||
#
|
||||
# @TEST-EXEC: cp $FILES/zeromq/cluster-layout-simple.zeek cluster-layout.zeek
|
||||
# @TEST-EXEC: cp $FILES/zeromq/test-bootstrap.zeek zeromq-test-bootstrap.zeek
|
||||
#
|
||||
# @TEST-EXEC: zeek -b --parse-only manager.zeek
|
||||
# @TEST-EXEC: python3 -m py_compile client.py
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-run manager "ZEEKPATH=$ZEEKPATH:.. && CLUSTER_NODE=manager zeek -b ../manager.zeek >out"
|
||||
# @TEST-EXEC: btest-bg-run client "python3 ../client.py >out"
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-wait 30
|
||||
# @TEST-EXEC: btest-diff ./manager/out
|
||||
# @TEST-EXEC: btest-diff ./manager/.stderr
|
||||
# @TEST-EXEC: btest-diff ./client/out
|
||||
# @TEST-EXEC: btest-diff ./client/.stderr
|
||||
|
||||
# @TEST-START-FILE manager.zeek
|
||||
@load ./zeromq-test-bootstrap
|
||||
redef exit_only_after_terminate = T;
|
||||
|
||||
global ping_count = 0;
|
||||
|
||||
global ping: event(msg: string, c: count) &is_used;
|
||||
global pong: event(msg: string, c: count) &is_used;
|
||||
|
||||
event zeek_init()
|
||||
{
|
||||
Cluster::subscribe("/test/manager");
|
||||
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
|
||||
}
|
||||
|
||||
event ping(msg: string, n: count) &is_used
|
||||
{
|
||||
++ping_count;
|
||||
print fmt("got ping: %s, %s", msg, n);
|
||||
local e = Cluster::make_event(pong, fmt("orig_msg=%s", msg), ping_count);
|
||||
Cluster::publish("/test/clients", e);
|
||||
}
|
||||
|
||||
global added = 0;
|
||||
global lost = 0;
|
||||
|
||||
event Cluster::websocket_client_added(info: Cluster::EndpointInfo, subscriptions: string_vec)
|
||||
{
|
||||
++added;
|
||||
print "Cluster::websocket_client_added", added, subscriptions;
|
||||
}
|
||||
|
||||
event Cluster::websocket_client_lost(info: Cluster::EndpointInfo)
|
||||
{
|
||||
++lost;
|
||||
print "Cluster::websocket_client_lost", lost;
|
||||
if ( lost == 3 )
|
||||
terminate();
|
||||
}
|
||||
# @TEST-END-FILE
|
||||
|
||||
|
||||
@TEST-START-FILE client.py
|
||||
import json, os, time
|
||||
from websockets.sync.client import connect
|
||||
|
||||
ws_port = os.environ['WEBSOCKET_PORT'].split('/')[0]
|
||||
ws_url = f'ws://127.0.0.1:{ws_port}/v1/messages/json'
|
||||
topic = '/test/clients'
|
||||
|
||||
def make_ping(c, who):
|
||||
return {
|
||||
"type": "data-message",
|
||||
"topic": "/test/manager",
|
||||
"@data-type": "vector",
|
||||
"data": [
|
||||
{"@data-type": "count", "data": 1}, # Format
|
||||
{"@data-type": "count", "data": 1}, # Type
|
||||
{"@data-type": "vector", "data": [
|
||||
{ "@data-type": "string", "data": "ping"}, # Event name
|
||||
{ "@data-type": "vector", "data": [ # event args
|
||||
{"@data-type": "string", "data": who},
|
||||
{"@data-type": "count", "data": c},
|
||||
], },
|
||||
], },
|
||||
],
|
||||
}
|
||||
|
||||
def run(ws_url):
|
||||
with connect(ws_url) as ws1:
|
||||
with connect(ws_url) as ws2:
|
||||
with connect(ws_url) as ws3:
|
||||
clients = [ws1, ws2, ws3]
|
||||
print("Connected!")
|
||||
ids = set()
|
||||
for c in clients:
|
||||
c.send(json.dumps([topic]))
|
||||
for c in clients:
|
||||
ack = json.loads(c.recv())
|
||||
assert "type" in ack, repr(ack)
|
||||
assert ack["type"] == "ack"
|
||||
assert "endpoint" in ack, repr(ack)
|
||||
assert "version" in ack
|
||||
ids.add(ack["endpoint"])
|
||||
|
||||
print("unique ids", len(ids))
|
||||
|
||||
for i in range(16):
|
||||
c = clients[i % len(clients)]
|
||||
name = f"ws{(i % len(clients)) + 1}"
|
||||
print(name, "sending ping", i)
|
||||
c.send(json.dumps(make_ping(i, name)))
|
||||
|
||||
print("receiving pong", i)
|
||||
for c in clients:
|
||||
pong = json.loads(c.recv())
|
||||
ev = pong["data"][2]["data"]
|
||||
print("ev: topic", pong["topic"], "event name", ev[0]["data"], "args", ev[1]["data"])
|
||||
|
||||
def main():
|
||||
for _ in range(100):
|
||||
try:
|
||||
run(ws_url)
|
||||
break
|
||||
except ConnectionRefusedError:
|
||||
time.sleep(0.1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@TEST-END-FILE
|
19
testing/btest/cluster/websocket/tls-usage-error.zeek
Normal file
19
testing/btest/cluster/websocket/tls-usage-error.zeek
Normal file
|
@ -0,0 +1,19 @@
|
|||
# @TEST-DOC: Calling listen_websocket() with badly configured WebSocketTLSOptions.
|
||||
#
|
||||
# @TEST-EXEC: zeek -b %INPUT
|
||||
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff .stderr
|
||||
|
||||
|
||||
event zeek_init()
|
||||
{
|
||||
local tls_options_no_key = Cluster::WebSocketTLSOptions(
|
||||
$cert_file="../localhost.crt",
|
||||
);
|
||||
|
||||
local tls_options_no_cert = Cluster::WebSocketTLSOptions(
|
||||
$key_file="../localhost.key",
|
||||
);
|
||||
|
||||
assert ! Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=1234/tcp, $tls_options=tls_options_no_key]);
|
||||
assert ! Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=1234/tcp, $tls_options=tls_options_no_cert]);
|
||||
}
|
151
testing/btest/cluster/websocket/tls.zeek
Normal file
151
testing/btest/cluster/websocket/tls.zeek
Normal file
|
@ -0,0 +1,151 @@
|
|||
# @TEST-DOC: Run a single node cluster (manager) with a websocket server that has TLS enabled.
|
||||
#
|
||||
# @TEST-REQUIRES: have-zeromq
|
||||
# @TEST-REQUIRES: python3 -c 'import websockets.asyncio'
|
||||
#
|
||||
# @TEST-GROUP: cluster-zeromq
|
||||
#
|
||||
# @TEST-PORT: XPUB_PORT
|
||||
# @TEST-PORT: XSUB_PORT
|
||||
# @TEST-PORT: LOG_PULL_PORT
|
||||
# @TEST-PORT: WEBSOCKET_PORT
|
||||
#
|
||||
# @TEST-EXEC: cp $FILES/zeromq/cluster-layout-simple.zeek cluster-layout.zeek
|
||||
# @TEST-EXEC: cp $FILES/zeromq/test-bootstrap.zeek zeromq-test-bootstrap.zeek
|
||||
#
|
||||
# @TEST-EXEC: zeek -b --parse-only manager.zeek
|
||||
# @TEST-EXEC: python3 -m py_compile client.py
|
||||
#
|
||||
# @TEST-EXEC: chmod +x gen-localhost-certs.sh
|
||||
# @TEST-EXEC: ./gen-localhost-certs.sh
|
||||
# @TEST-EXEC: btest-bg-run manager "ZEEKPATH=$ZEEKPATH:.. && CLUSTER_NODE=manager zeek -b ../manager.zeek >out"
|
||||
# @TEST-EXEC: btest-bg-run client "python3 ../client.py >out"
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-wait 30
|
||||
# @TEST-EXEC: btest-diff ./manager/out
|
||||
# @TEST-EXEC: btest-diff ./manager/.stderr
|
||||
# @TEST-EXEC: btest-diff ./client/out
|
||||
# @TEST-EXEC: btest-diff ./client/.stderr
|
||||
|
||||
# @TEST-START-FILE manager.zeek
|
||||
@load ./zeromq-test-bootstrap
|
||||
redef exit_only_after_terminate = T;
|
||||
|
||||
global ping_count = 0;
|
||||
|
||||
global ping: event(msg: string, c: count) &is_used;
|
||||
global pong: event(msg: string, c: count) &is_used;
|
||||
|
||||
event zeek_init()
|
||||
{
|
||||
Cluster::subscribe("/zeek/event/my_topic");
|
||||
|
||||
local tls_options = Cluster::WebSocketTLSOptions(
|
||||
$cert_file="../localhost.crt",
|
||||
$key_file="../localhost.key",
|
||||
);
|
||||
|
||||
local ws_server_options = Cluster::WebSocketServerOptions(
|
||||
$listen_host="127.0.0.1",
|
||||
$listen_port=to_port(getenv("WEBSOCKET_PORT")),
|
||||
$tls_options=tls_options,
|
||||
);
|
||||
|
||||
Cluster::listen_websocket(ws_server_options);
|
||||
}
|
||||
|
||||
event ping(msg: string, n: count) &is_used
|
||||
{
|
||||
++ping_count;
|
||||
print fmt("got ping: %s, %s", msg, n);
|
||||
local e = Cluster::make_event(pong, "my-message", ping_count);
|
||||
Cluster::publish("/zeek/event/my_topic", e);
|
||||
}
|
||||
|
||||
event Cluster::websocket_client_added(info: Cluster::EndpointInfo, subscriptions: string_vec)
|
||||
{
|
||||
print "Cluster::websocket_client_added", subscriptions;
|
||||
}
|
||||
|
||||
event Cluster::websocket_client_lost(info: Cluster::EndpointInfo)
|
||||
{
|
||||
print "Cluster::websocket_client_lost";
|
||||
terminate();
|
||||
}
|
||||
# @TEST-END-FILE
|
||||
|
||||
|
||||
@TEST-START-FILE client.py
|
||||
import asyncio, json, os, socket, time
|
||||
from websockets.asyncio.client import connect
|
||||
|
||||
ws_port = os.environ['WEBSOCKET_PORT'].split('/')[0]
|
||||
ws_url = f'wss://localhost:{ws_port}/v1/messages/json'
|
||||
topic = '/zeek/event/my_topic'
|
||||
|
||||
# Make the websockets library use the custom server cert.
|
||||
# https://stackoverflow.com/a/55856969
|
||||
os.environ["SSL_CERT_FILE"] = "../localhost.crt"
|
||||
|
||||
def make_ping(c):
|
||||
return {
|
||||
"type": "data-message",
|
||||
"topic": topic,
|
||||
"@data-type": "vector",
|
||||
"data": [
|
||||
{"@data-type": "count", "data": 1}, # Format
|
||||
{"@data-type": "count", "data": 1}, # Type
|
||||
{"@data-type": "vector", "data": [
|
||||
{ "@data-type": "string", "data": "ping"}, # Event name
|
||||
{ "@data-type": "vector", "data": [ # event args
|
||||
{"@data-type": "string", "data": f"python-websocket-client"},
|
||||
{"@data-type": "count", "data": c},
|
||||
], },
|
||||
], },
|
||||
],
|
||||
}
|
||||
|
||||
async def run():
|
||||
async with connect(ws_url, family=socket.AF_INET) as ws:
|
||||
print("Connected!")
|
||||
# Send subscriptions
|
||||
await ws.send(json.dumps([topic]))
|
||||
ack = json.loads(await ws.recv())
|
||||
assert "type" in ack
|
||||
assert ack["type"] == "ack"
|
||||
assert "endpoint" in ack
|
||||
assert "version" in ack
|
||||
|
||||
for i in range(5):
|
||||
print("Sending ping", i)
|
||||
await ws.send(json.dumps(make_ping(i)))
|
||||
print("Receiving pong", i)
|
||||
pong = json.loads(await ws.recv())
|
||||
assert pong["@data-type"] == "vector"
|
||||
ev = pong["data"][2]["data"]
|
||||
print("topic", pong["topic"], "event name", ev[0]["data"], "args", ev[1]["data"])
|
||||
|
||||
def main():
|
||||
for _ in range(100):
|
||||
try:
|
||||
asyncio.run(run())
|
||||
break
|
||||
except ConnectionRefusedError:
|
||||
time.sleep(0.1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@TEST-END-FILE
|
||||
|
||||
# The cert and key were generated with OpenSSL using the following command,
|
||||
# taken from https://letsencrypt.org/docs/certificates-for-localhost/
|
||||
#
|
||||
# The test will generate the script, but the certificate is valid
|
||||
# for 10 years.
|
||||
@TEST-START-FILE gen-localhost-certs.sh
|
||||
#!/usr/bin/env bash
|
||||
openssl req -x509 -out localhost.crt -keyout localhost.key \
|
||||
-newkey rsa:2048 -nodes -sha256 -days 3650 \
|
||||
-subj '/CN=localhost' -extensions EXT -config <( \
|
||||
printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nbasicConstraints=CA:TRUE")
|
||||
@TEST-END-FILE
|
158
testing/btest/cluster/websocket/two-pipelining.zeek
Normal file
158
testing/btest/cluster/websocket/two-pipelining.zeek
Normal file
|
@ -0,0 +1,158 @@
|
|||
# @TEST-DOC: Send subscriptions and events without waiting for pong, should be okay, the websocket server will queue this a bit.
|
||||
#
|
||||
# @TEST-REQUIRES: have-zeromq
|
||||
# @TEST-REQUIRES: python3 -c 'import websockets.sync'
|
||||
#
|
||||
# @TEST-GROUP: cluster-zeromq
|
||||
#
|
||||
# @TEST-PORT: XPUB_PORT
|
||||
# @TEST-PORT: XSUB_PORT
|
||||
# @TEST-PORT: LOG_PULL_PORT
|
||||
# @TEST-PORT: WEBSOCKET_PORT
|
||||
#
|
||||
# @TEST-EXEC: cp $FILES/zeromq/cluster-layout-simple.zeek cluster-layout.zeek
|
||||
# @TEST-EXEC: cp $FILES/zeromq/test-bootstrap.zeek zeromq-test-bootstrap.zeek
|
||||
#
|
||||
# @TEST-EXEC: zeek -b --parse-only manager.zeek
|
||||
# @TEST-EXEC: python3 -m py_compile client.py
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-run manager "ZEEKPATH=$ZEEKPATH:.. && CLUSTER_NODE=manager zeek -b ../manager.zeek >out"
|
||||
# @TEST-EXEC: btest-bg-run client "python3 ../client.py >out"
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-wait 30
|
||||
# @TEST-EXEC: sort ./manager/out > ./manager/out.sorted
|
||||
# @TEST-EXEC: btest-diff ./manager/out.sorted
|
||||
# @TEST-EXEC: btest-diff ./manager/.stderr
|
||||
# @TEST-EXEC: btest-diff ./client/out
|
||||
# @TEST-EXEC: btest-diff ./client/.stderr
|
||||
|
||||
# @TEST-START-FILE manager.zeek
|
||||
@load ./zeromq-test-bootstrap
|
||||
redef exit_only_after_terminate = T;
|
||||
|
||||
global ping_count = 0;
|
||||
|
||||
global ping: event(msg: string, c: count) &is_used;
|
||||
global pong: event(msg: string, c: count) &is_used;
|
||||
|
||||
event zeek_init()
|
||||
{
|
||||
Cluster::subscribe("/zeek/event/to_manager");
|
||||
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
|
||||
}
|
||||
|
||||
global added = 0;
|
||||
global lost = 0;
|
||||
|
||||
type Item: record {
|
||||
msg: string;
|
||||
n: count;
|
||||
};
|
||||
|
||||
global queue: vector of Item;
|
||||
|
||||
event ping(msg: string, n: count) &is_used
|
||||
{
|
||||
# Queue the pings if we haven't seen both clients yet.
|
||||
if ( added < 2 )
|
||||
{
|
||||
queue += Item($msg=msg, $n=n);
|
||||
return;
|
||||
}
|
||||
|
||||
++ping_count;
|
||||
print fmt("B got ping: %s, %s", msg, n);
|
||||
local e = Cluster::make_event(pong, "my-message", ping_count);
|
||||
Cluster::publish("/zeek/event/to_client", e);
|
||||
}
|
||||
|
||||
event Cluster::websocket_client_added(info: Cluster::EndpointInfo, subscriptions: string_vec)
|
||||
{
|
||||
++added;
|
||||
print "A Cluster::websocket_client_added", added, subscriptions;
|
||||
|
||||
if ( added == 2 )
|
||||
{
|
||||
# Anything in the queue?
|
||||
for ( _, item in queue )
|
||||
event ping(item$msg, item$n);
|
||||
}
|
||||
}
|
||||
|
||||
event Cluster::websocket_client_lost(info: Cluster::EndpointInfo)
|
||||
{
|
||||
++lost;
|
||||
print "C Cluster::websocket_client_lost";
|
||||
if ( lost == 2 )
|
||||
terminate();
|
||||
}
|
||||
# @TEST-END-FILE
|
||||
|
||||
|
||||
@TEST-START-FILE client.py
|
||||
import json, os, time
|
||||
from websockets.sync.client import connect
|
||||
|
||||
ws_port = os.environ['WEBSOCKET_PORT'].split('/')[0]
|
||||
ws_url = f'ws://127.0.0.1:{ws_port}/v1/messages/json'
|
||||
topic = '/zeek/event/to_client'
|
||||
|
||||
def make_ping(c, who):
|
||||
return {
|
||||
"type": "data-message",
|
||||
"topic": "/zeek/event/to_manager",
|
||||
"@data-type": "vector",
|
||||
"data": [
|
||||
{"@data-type": "count", "data": 1}, # Format
|
||||
{"@data-type": "count", "data": 1}, # Type
|
||||
{"@data-type": "vector", "data": [
|
||||
{ "@data-type": "string", "data": "ping"}, # Event name
|
||||
{ "@data-type": "vector", "data": [ # event args
|
||||
{"@data-type": "string", "data": who},
|
||||
{"@data-type": "count", "data": c},
|
||||
], },
|
||||
], },
|
||||
],
|
||||
}
|
||||
|
||||
def run(ws_url):
|
||||
with connect(ws_url) as ws1:
|
||||
with connect(ws_url) as ws2:
|
||||
clients = [ws1, ws2]
|
||||
print("Connected!")
|
||||
# Send subscriptions
|
||||
for ws in clients:
|
||||
ws.send(json.dumps([topic]))
|
||||
|
||||
for i in range(5):
|
||||
for c, ws in enumerate(clients, 1):
|
||||
print(f"Sending ping {i} - ws{c}")
|
||||
ws.send(json.dumps(make_ping(i, f"ws{c}")))
|
||||
|
||||
for c, ws in enumerate(clients, 1):
|
||||
print(f"Receiving ack - ws{c}")
|
||||
ack = json.loads(ws.recv())
|
||||
assert "type" in ack
|
||||
assert ack["type"] == "ack"
|
||||
assert "endpoint" in ack
|
||||
assert "version" in ack
|
||||
|
||||
for i in range(10):
|
||||
for c, ws in enumerate(clients):
|
||||
print(f"Receiving pong {i} - ws{c}")
|
||||
pong = json.loads(ws.recv())
|
||||
assert pong["@data-type"] == "vector"
|
||||
ev = pong["data"][2]["data"]
|
||||
print("topic", pong["topic"], "event name", ev[0]["data"], "args", ev[1]["data"])
|
||||
|
||||
def main():
|
||||
for _ in range(100):
|
||||
try:
|
||||
run(ws_url)
|
||||
break
|
||||
except ConnectionRefusedError:
|
||||
time.sleep(0.1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@TEST-END-FILE
|
Loading…
Add table
Add a link
Reference in a new issue