| 1 |
# |
| 2 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
# or more contributor license agreements. See the NOTICE file |
| 4 |
# distributed with this work for additional information |
| 5 |
# regarding copyright ownership. The ASF licenses this file |
| 6 |
# to you under the Apache License, Version 2.0 (the |
| 7 |
# "License"); you may not use this file except in compliance |
| 8 |
# with the License. You may obtain a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, |
| 13 |
# software distributed under the License is distributed on an |
| 14 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
# KIND, either express or implied. See the License for the |
| 16 |
# specific language governing permissions and limitations |
| 17 |
# under the License. |
| 18 |
# |
| 19 |
|
| 20 |
from __future__ import absolute_import |
| 21 |
|
| 22 |
from threading import Thread, Event |
| 23 |
from uuid import uuid4 |
| 24 |
|
| 25 |
from proton import Message, Url, Array, UNDESCRIBED, Data, symbol, ConnectionException |
| 26 |
from proton.handlers import MessagingHandler |
| 27 |
from proton.reactor import Container |
| 28 |
from proton.utils import SyncRequestResponse, BlockingConnection |
| 29 |
|
| 30 |
from .common import Test, free_tcp_port |
| 31 |
from .common import ensureCanTestExtendedSASL |
| 32 |
|
| 33 |
CONNECTION_PROPERTIES = {u'connection': u'properties'} |
| 34 |
OFFERED_CAPABILITIES = Array(UNDESCRIBED, Data.SYMBOL, symbol("O_one"), symbol("O_two"), symbol("O_three")) |
| 35 |
DESIRED_CAPABILITIES = Array(UNDESCRIBED, Data.SYMBOL, symbol("D_one"), symbol("D_two"), symbol("D_three")) |
| 36 |
ANONYMOUS = 'ANONYMOUS' |
| 37 |
EXTERNAL = 'EXTERNAL' |
| 38 |
|
| 39 |
|
| 40 |
class EchoServer(MessagingHandler, Thread): |
| 41 |
""" |
| 42 |
Simple echo server that echos messages to their reply-to. Runs in a thread. |
| 43 |
Will only accept a single connection and shut down when that connection closes. |
| 44 |
""" |
| 45 |
|
| 46 |
def __init__(self, url, timeout): |
| 47 |
MessagingHandler.__init__(self) |
| 48 |
Thread.__init__(self) |
| 49 |
self.daemon = True |
| 50 |
self.timeout = timeout |
| 51 |
self.url = url |
| 52 |
self.senders = {} |
| 53 |
self.container = None |
| 54 |
self.event = Event() |
| 55 |
|
| 56 |
def on_start(self, event): |
| 57 |
self.acceptor = event.container.listen(self.url) |
| 58 |
self.container = event.container |
| 59 |
self.event.set() |
| 60 |
|
| 61 |
def on_link_opening(self, event): |
| 62 |
if event.link.is_sender: |
| 63 |
if event.link.remote_source and event.link.remote_source.dynamic: |
| 64 |
event.link.source.address = str(uuid4()) |
| 65 |
self.senders[event.link.source.address] = event.link |
| 66 |
|
| 67 |
def on_message(self, event): |
| 68 |
m = event.message |
| 69 |
sender = self.senders.get(m.reply_to) |
| 70 |
if sender: |
| 71 |
reply = Message(address=m.reply_to, body=m.body, correlation_id=m.correlation_id) |
| 72 |
sender.send(reply) |
| 73 |
|
| 74 |
def on_connection_closing(self, event): |
| 75 |
self.acceptor.close() |
| 76 |
|
| 77 |
def on_transport_error(self, event): |
| 78 |
self.acceptor.close() |
| 79 |
|
| 80 |
def run(self): |
| 81 |
Container(self).run() |
| 82 |
|
| 83 |
def wait(self): |
| 84 |
self.event.wait(self.timeout) |
| 85 |
|
| 86 |
|
| 87 |
class ConnPropertiesServer(EchoServer): |
| 88 |
def __init__(self, url, timeout): |
| 89 |
EchoServer.__init__(self, url, timeout) |
| 90 |
self.properties_received = False |
| 91 |
self.offered_capabilities_received = False |
| 92 |
self.desired_capabilities_received = False |
| 93 |
|
| 94 |
def on_connection_opening(self, event): |
| 95 |
conn = event.connection |
| 96 |
|
| 97 |
if conn.remote_properties == CONNECTION_PROPERTIES: |
| 98 |
self.properties_received = True |
| 99 |
if conn.remote_offered_capabilities == OFFERED_CAPABILITIES: |
| 100 |
self.offered_capabilities_received = True |
| 101 |
if conn.remote_desired_capabilities == DESIRED_CAPABILITIES: |
| 102 |
self.desired_capabilities_received = True |
| 103 |
|
| 104 |
|
| 105 |
class SyncRequestResponseTest(Test): |
| 106 |
"""Test SyncRequestResponse""" |
| 107 |
|
| 108 |
def test_request_response(self): |
| 109 |
ensureCanTestExtendedSASL() |
| 110 |
|
| 111 |
def test(name, address="x"): |
| 112 |
for i in range(5): |
| 113 |
body = "%s%s" % (name, i) |
| 114 |
response = client.call(Message(address=address, body=body)) |
| 115 |
self.assertEquals(response.address, client.reply_to) |
| 116 |
self.assertEquals(response.body, body) |
| 117 |
|
| 118 |
server = EchoServer(Url(host="127.0.0.1", port=free_tcp_port()), self.timeout) |
| 119 |
server.start() |
| 120 |
server.wait() |
| 121 |
connection = BlockingConnection(server.url, timeout=self.timeout) |
| 122 |
client = SyncRequestResponse(connection) |
| 123 |
try: |
| 124 |
test("foo") # Simple request/resposne |
| 125 |
finally: |
| 126 |
client.connection.close() |
| 127 |
server.join(timeout=self.timeout) |
| 128 |
|
| 129 |
def test_connection_properties(self): |
| 130 |
ensureCanTestExtendedSASL() |
| 131 |
server = ConnPropertiesServer(Url(host="127.0.0.1", port=free_tcp_port()), timeout=self.timeout) |
| 132 |
server.start() |
| 133 |
server.wait() |
| 134 |
connection = BlockingConnection(server.url, timeout=self.timeout, properties=CONNECTION_PROPERTIES, |
| 135 |
offered_capabilities=OFFERED_CAPABILITIES, desired_capabilities=DESIRED_CAPABILITIES) |
| 136 |
client = SyncRequestResponse(connection) |
| 137 |
client.connection.close() |
| 138 |
server.join(timeout=self.timeout) |
| 139 |
self.assertEquals(server.properties_received, True) |
| 140 |
self.assertEquals(server.offered_capabilities_received, True) |
| 141 |
self.assertEquals(server.desired_capabilities_received, True) |
| 142 |
|
| 143 |
def test_allowed_mechs_external(self): |
| 144 |
# All this test does it make sure that if we pass allowed_mechs to BlockingConnection, it is actually used. |
| 145 |
port = free_tcp_port() |
| 146 |
server = ConnPropertiesServer(Url(host="127.0.0.1", port=port), timeout=self.timeout) |
| 147 |
server.start() |
| 148 |
server.wait() |
| 149 |
try: |
| 150 |
# This will cause an exception because we are specifying allowed_mechs as |
| 151 |
# EXTERNAL. The SASL handshake will fail because the server is not setup |
| 152 |
# to handle EXTERNAL |
| 153 |
connection = BlockingConnection(server.url, timeout=self.timeout, properties=CONNECTION_PROPERTIES, |
| 154 |
offered_capabilities=OFFERED_CAPABILITIES, desired_capabilities=DESIRED_CAPABILITIES, allowed_mechs=EXTERNAL) |
| 155 |
self.fail("Expected ConnectionException") |
| 156 |
except ConnectionException as e: |
| 157 |
self.assertTrue('amqp:unauthorized-access' in str(e), "expected unauthorized-access") |
| 158 |
server.join(timeout=self.timeout) |
| 159 |
|
| 160 |
def test_allowed_mechs_anonymous(self): |
| 161 |
# All this test does it make sure that if we pass allowed_mechs to BlockingConnection, it is actually used. |
| 162 |
server = ConnPropertiesServer(Url(host="127.0.0.1", port=free_tcp_port()), timeout=self.timeout) |
| 163 |
server.start() |
| 164 |
server.wait() |
| 165 |
# An ANONYMOUS allowed_mechs will work, anonymous connections are allowed by ConnPropertiesServer |
| 166 |
connection = BlockingConnection(server.url, timeout=self.timeout, properties=CONNECTION_PROPERTIES, |
| 167 |
offered_capabilities=OFFERED_CAPABILITIES, desired_capabilities=DESIRED_CAPABILITIES, allowed_mechs=ANONYMOUS) |
| 168 |
client = SyncRequestResponse(connection) |
| 169 |
client.connection.close() |
| 170 |
server.join(timeout=self.timeout) |
| 171 |
self.assertEquals(server.properties_received, True) |
| 172 |
self.assertEquals(server.offered_capabilities_received, True) |
| 173 |
self.assertEquals(server.desired_capabilities_received, True) |