| 1 |
|
| 2 |
|
| 3 |
from mosq_test_helper import * |
| 4 |
|
| 5 |
if sys.version < '2.7': |
| 6 |
print("WARNING: SSL not supported on Python 2.6") |
| 7 |
exit(0) |
| 8 |
|
| 9 |
def write_config1(filename, port1, port2): |
| 10 |
with open(filename, 'w') as f: |
| 11 |
f.write("allow_anonymous true\n") |
| 12 |
f.write("\n") |
| 13 |
f.write(f"psk_file {str(source_dir/'08-tls-psk-bridge.psk')}\n") |
| 14 |
f.write("\n") |
| 15 |
f.write("listener %d\n" % (port1)) |
| 16 |
f.write("\n") |
| 17 |
f.write("listener %d\n" % (port2)) |
| 18 |
f.write("psk_hint hint\n") |
| 19 |
|
| 20 |
def write_config2(filename, port2, port3): |
| 21 |
with open(filename, 'w') as f: |
| 22 |
f.write("listener %d\n" % (port3)) |
| 23 |
f.write("allow_anonymous true\n") |
| 24 |
f.write("\n") |
| 25 |
f.write("connection bridge-psk\n") |
| 26 |
f.write("address localhost:%d\n" % (port2)) |
| 27 |
f.write("topic psk/test out\n") |
| 28 |
f.write("bridge_identity psk-test\n") |
| 29 |
f.write("bridge_psk deadbeef\n") |
| 30 |
|
| 31 |
(port1, port2, port3) = mosq_test.get_port(3) |
| •32 |
conf_file1 = "/tmp/08-tls-psk-bridge.conf" |
| 33 |
conf_file2 = "/tmp/08-tls-psk-bridge.conf2" |
| 34 |
write_config1(conf_file1, port1, port2) |
| 35 |
write_config2(conf_file2, port2, port3) |
| 36 |
|
| 37 |
env = mosq_test.env_add_ld_library_path() |
| 38 |
|
| 39 |
rc = 1 |
| 40 |
connect_packet = mosq_test.gen_connect("no-psk-test-client") |
| 41 |
connack_packet = mosq_test.gen_connack(rc=0) |
| 42 |
|
| 43 |
mid = 1 |
| 44 |
subscribe_packet = mosq_test.gen_subscribe(mid, "psk/test", 0) |
| 45 |
suback_packet = mosq_test.gen_suback(mid, 0) |
| 46 |
|
| 47 |
publish_packet = mosq_test.gen_publish(topic="psk/test", payload="message", qos=0) |
| 48 |
|
| •49 |
bridge_cmd = ['/usr/sbin/mosquitto', '-c', conf_file2] |
| 50 |
broker = mosq_test.start_broker(filename=conf_file1, use_conf=True, port=port1) |
| 51 |
bridge = mosq_test.start_broker(filename=conf_file2+'_bridge', cmd=bridge_cmd, port=port3) |
| 52 |
|
| 53 |
pub = None |
| 54 |
try: |
| 55 |
sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=30, port=port1) |
| 56 |
|
| 57 |
mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback") |
| 58 |
|
| 59 |
pub = subprocess.run(['./c/08-tls-psk-bridge.test', str(port3)], env=env, capture_output=True, encoding='utf-8') |
| 60 |
if pub.returncode != 0: |
| 61 |
print("d") |
| 62 |
print(pub.returncode) |
| 63 |
raise ValueError |
| 64 |
|
| 65 |
mosq_test.expect_packet(sock, "publish", publish_packet) |
| 66 |
rc = pub.returncode |
| 67 |
|
| 68 |
sock.close() |
| 69 |
except mosq_test.TestError: |
| 70 |
pass |
| 71 |
finally: |
| 72 |
os.remove(conf_file1) |
| 73 |
os.remove(conf_file2) |
| 74 |
broker.terminate() |
| 75 |
if mosq_test.wait_for_subprocess(broker): |
| 76 |
print("broker not terminated") |
| 77 |
if rc == 0: rc=1 |
| 78 |
bridge.terminate() |
| 79 |
if mosq_test.wait_for_subprocess(bridge): |
| 80 |
print("bridge not terminated") |
| 81 |
if rc == 0: rc=1 |
| 82 |
(stdo, stde) = broker.communicate() |
| 83 |
if rc: |
| 84 |
print(stde.decode('utf-8')) |
| 85 |
(stdo, stde) = bridge.communicate() |
| 86 |
print(stde.decode('utf-8')) |
| 87 |
if pub: |
| 88 |
print(pub.stdout) |
| 89 |
print(pub.stderr) |
| 90 |
|
| 91 |
exit(rc) |
| 92 |
|