| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
# or more contributor license agreements. See the NOTICE file |
| 5 |
# distributed with this work for additional information |
| 6 |
# regarding copyright ownership. The ASF licenses this file |
| 7 |
# to you under the Apache License, Version 2.0 (the |
| 8 |
# "License"); you may not use this file except in compliance |
| 9 |
# with the License. You may obtain a copy of the License at |
| 10 |
# |
| 11 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
# |
| 13 |
# Unless required by applicable law or agreed to in writing, |
| 14 |
# software distributed under the License is distributed on an |
| 15 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
# KIND, either express or implied. See the License for the |
| 17 |
# specific language governing permissions and limitations |
| 18 |
# under the License |
| 19 |
# |
| 20 |
|
| 21 |
# Run the C++ examples and verify that they behave as expected. |
| 22 |
# Example executables must be in PATH |
| 23 |
|
| 24 |
import os, shutil, sys, subprocess, unittest |
| 25 |
from test_subprocess import Popen, Server as TestServer, check_output, in_path |
| 26 |
from os.path import dirname |
| 27 |
|
| 28 |
|
| 29 |
class Server(TestServer): |
| 30 |
@property |
| 31 |
def addr(self): |
| 32 |
return ":%s/example" % self.port |
| 33 |
|
| 34 |
|
| 35 |
class Broker(Server): |
| 36 |
def __init__(self): |
| 37 |
super(Broker, self).__init__(["broker", "-a", "//:0"], kill_me=True) |
| 38 |
|
| 39 |
|
| 40 |
def _cyrusSetup(conf_dir): |
| 41 |
"""Write out simple SASL config.tests |
| 42 |
""" |
| 43 |
saslpasswd = os.getenv('SASLPASSWD') |
| 44 |
if saslpasswd: |
| 45 |
abs_conf_dir = os.path.abspath(conf_dir) |
| 46 |
shutil.rmtree(abs_conf_dir, True) |
| 47 |
os.mkdir(abs_conf_dir) |
| 48 |
db = os.path.join(abs_conf_dir,'proton.sasldb') |
| 49 |
conf = os.path.join(abs_conf_dir,'proton-server.conf') |
| 50 |
with open(conf, 'w') as f: |
| 51 |
f.write(f"""sasldb_path: {db} |
| 52 |
mech_list: EXTERNAL DIGEST-MD5 SCRAM-SHA-1 CRAM-MD5 PLAIN ANONYMOUS |
| 53 |
""") |
| 54 |
cmd = f"echo password | {saslpasswd} -c -p -f {db} -u proton user" |
| 55 |
subprocess.check_output(cmd, shell=True) |
| 56 |
os.environ['PN_SASL_CONFIG_PATH'] = abs_conf_dir |
| 57 |
|
| 58 |
|
| 59 |
# Globally initialize Cyrus SASL configuration |
| 60 |
_cyrusSetup('sasl-conf') |
| 61 |
|
| 62 |
CLIENT_EXPECT="""Twas brillig, and the slithy toves => TWAS BRILLIG, AND THE SLITHY TOVES |
| 63 |
Did gire and gymble in the wabe. => DID GIRE AND GYMBLE IN THE WABE. |
| 64 |
All mimsy were the borogroves, => ALL MIMSY WERE THE BOROGROVES, |
| 65 |
And the mome raths outgrabe. => AND THE MOME RATHS OUTGRABE. |
| 66 |
""" |
| 67 |
|
| 68 |
|
| 69 |
def recv_expect(): |
| 70 |
return "".join(['{"sequence"=%s}\n' % (i+1) for i in range(100)]) |
| 71 |
|
| 72 |
|
| 73 |
class ContainerExampleTest(unittest.TestCase): |
| 74 |
"""Run the container examples, verify they behave as expected.""" |
| 75 |
|
| 76 |
def test_helloworld(self): |
| 77 |
self.assertMultiLineEqual('Hello World!\n', check_output(["helloworld", Broker.addr])) |
| 78 |
|
| 79 |
def test_simple_send_recv(self): |
| 80 |
self.assertMultiLineEqual("all messages confirmed\n", check_output(["simple_send", "-a", Broker.addr])) |
| 81 |
self.assertMultiLineEqual(recv_expect(), check_output(["simple_recv", "-a", Broker.addr])) |
| 82 |
|
| 83 |
def test_simple_recv_send(self): |
| 84 |
recv = Popen(["simple_recv", "-a", Broker.addr]) |
| 85 |
self.assertMultiLineEqual("all messages confirmed\n", check_output(["simple_send", "-a", Broker.addr])) |
| 86 |
self.assertMultiLineEqual(recv_expect(), recv.communicate()[0]) |
| 87 |
|
| 88 |
def test_simple_send_direct_recv(self): |
| 89 |
recv = Server(["direct_recv", "-a", "//:0"]) |
| 90 |
self.assertMultiLineEqual("all messages confirmed\n", check_output(["simple_send", "-a", recv.addr])) |
| 91 |
self.assertMultiLineEqual(recv_expect(), recv.communicate()[0]) |
| 92 |
|
| 93 |
def test_simple_recv_direct_send(self): |
| 94 |
send = Server(["direct_send", "-a", "//:0"]) |
| 95 |
self.assertMultiLineEqual(recv_expect(), check_output(["simple_recv", "-a", send.addr])) |
| 96 |
self.assertMultiLineEqual("all messages confirmed\n", send.communicate()[0]) |
| 97 |
|
| 98 |
def test_request_response(self): |
| 99 |
with Popen(["server", Broker.addr, "example"], kill_me=True) as server: |
| 100 |
server.expect("connected to %s" % Broker.addr) |
| 101 |
self.assertMultiLineEqual(CLIENT_EXPECT, check_output(["client", "-a", Broker.addr])) |
| 102 |
|
| 103 |
def test_request_response_direct(self): |
| 104 |
with Server(["server_direct", "-a", "//:0"], kill_me=True) as server: |
| 105 |
self.assertMultiLineEqual(CLIENT_EXPECT, check_output(["client", "-a", server.addr])) |
| 106 |
|
| 107 |
def test_flow_control(self): |
| 108 |
want="""success: Example 1: simple credit |
| 109 |
success: Example 2: basic drain |
| 110 |
success: Example 3: drain without credit |
| 111 |
success: Example 4: high/low watermark |
| 112 |
""" |
| 113 |
self.assertMultiLineEqual(want, check_output(["flow_control", "--quiet"])) |
| 114 |
|
| 115 |
def test_encode_decode(self): |
| 116 |
want=""" |
| 117 |
== Array, list and map of uniform type. |
| 118 |
array<int>[int(1), int(2), int(3)] |
| 119 |
[ 1 2 3 ] |
| 120 |
list[int(1), int(2), int(3)] |
| 121 |
[ 1 2 3 ] |
| 122 |
map{string(one):int(1), string(two):int(2)} |
| 123 |
{ one:1 two:2 } |
| 124 |
map{string(z):int(3), string(a):int(4)} |
| 125 |
[ z:3 a:4 ] |
| 126 |
list[string(a), string(b), string(c)] |
| 127 |
|
| 128 |
== List and map of mixed type values. |
| 129 |
list[int(42), string(foo)] |
| 130 |
[ 42 foo ] |
| 131 |
map{int(4):string(four), string(five):int(5)} |
| 132 |
{ 4:four five:5 } |
| 133 |
|
| 134 |
== Insert with stream operators. |
| 135 |
array<int>[int(1), int(2), int(3)] |
| 136 |
list[int(42), boolean(0), symbol(x)] |
| 137 |
map{string(k1):int(42), symbol(k2):boolean(0)} |
| 138 |
""" |
| 139 |
self.maxDiff = None |
| 140 |
self.assertMultiLineEqual(want, check_output(["encode_decode"])) |
| 141 |
|
| 142 |
def test_scheduled_send_03(self): |
| 143 |
# Output should be a bunch of "send" lines but can't guarantee exactly how many. |
| 144 |
out = check_output(["scheduled_send_03", "-a", Broker.addr+"scheduled_send", "-t", "0.1", "-i", "0.001"]).split() |
| 145 |
self.assertTrue(len(out) > 0); |
| 146 |
self.assertEqual(["send"]*len(out), out) |
| 147 |
|
| 148 |
def test_scheduled_send(self): |
| 149 |
if not in_path("scheduled_send"): |
| 150 |
return |
| 151 |
out = check_output(["scheduled_send", "-a", Broker.addr+"scheduled_send", "-t", "0.1", "-i", "0.001"]).split() |
| 152 |
self.assertTrue(len(out) > 0); |
| 153 |
self.assertEqual(["send"]*len(out), out) |
| 154 |
|
| 155 |
def test_message_properties(self): |
| 156 |
expect="""using put/get: short=123 string=foo symbol=sym |
| 157 |
using coerce: short(as long)=123 |
| 158 |
props[short]=123 |
| 159 |
props[string]=foo |
| 160 |
props[symbol]=sym |
| 161 |
short=42 string=bar |
| 162 |
expected conversion_error: "unexpected type, want: uint got: int" |
| 163 |
expected conversion_error: "unexpected type, want: uint got: string" |
| 164 |
""" |
| 165 |
self.assertMultiLineEqual(expect, check_output(["message_properties"])) |
| 166 |
|
| 167 |
def test_multithreaded_client(self): |
| 168 |
if not in_path("multithreaded_client"): |
| 169 |
return |
| 170 |
got = check_output(["multithreaded_client", Broker.addr, "examples", "10"]) |
| 171 |
self.maxDiff = None |
| 172 |
self.assertIn("10 messages sent and received", got); |
| 173 |
|
| 174 |
|
| 175 |
# Test is unstable, enable when fixed |
| 176 |
def skip_test_multithreaded_client_flow_control(self): |
| 177 |
if not in_path("multithreaded_client_flow_control"): |
| 178 |
return |
| 179 |
got = check_output(["multithreaded_client_flow_control", Broker.addr, "examples", "10", "2"]) |
| 180 |
self.maxDiff = None |
| 181 |
self.assertIn("20 messages sent and received", got); |
| 182 |
|
| 183 |
class ContainerExampleSSLTest(unittest.TestCase): |
| 184 |
"""Run the SSL container examples, verify they behave as expected.""" |
| 185 |
|
| 186 |
def ssl_certs_dir(self): |
| 187 |
"""Absolute path to the test SSL certificates""" |
| 188 |
return os.path.join(dirname(sys.argv[0]), "ssl-certs") |
| 189 |
|
| 190 |
def test_ssl(self): |
| 191 |
# SSL without SASL, VERIFY_PEER_NAME |
| 192 |
out = check_output(["ssl", "-c", self.ssl_certs_dir()]) |
| 193 |
expect = "Server certificate identity CN=test_server\nHello World!" |
| 194 |
self.assertIn(expect, out) |
| 195 |
|
| 196 |
def test_ssl_no_name(self): |
| 197 |
out = check_output(["ssl", "-c", self.ssl_certs_dir(), "-v", "noname"]) |
| 198 |
expect = "Outgoing client connection connected via SSL. Server certificate identity CN=test_server\nHello World!" |
| 199 |
self.assertIn(expect, out) |
| 200 |
|
| 201 |
def test_ssl_bad_name(self): |
| 202 |
# VERIFY_PEER |
| 203 |
out = check_output(["ssl", "-c", self.ssl_certs_dir(), "-v", "fail"]) |
| 204 |
expect = "Expected failure of connection with wrong peer name" |
| 205 |
self.assertIn(expect, out) |
| 206 |
|
| 207 |
def test_ssl_client_cert(self): |
| 208 |
# SSL with SASL EXTERNAL |
| 209 |
expect="""Inbound client certificate identity CN=test_client |
| 210 |
Outgoing client connection connected via SSL. Server certificate identity CN=test_server |
| 211 |
Hello World! |
| 212 |
""" |
| 213 |
out = check_output(["ssl_client_cert", self.ssl_certs_dir()]) |
| 214 |
self.assertIn(expect, out) |
| 215 |
|
| 216 |
if __name__ == "__main__": |
| 217 |
with Broker() as b: |
| 218 |
Broker.addr = b.addr |
| 219 |
unittest.main() |