diff --git a/oslo_messaging/tests/drivers/zmq/__init__.py b/oslo_messaging/tests/drivers/zmq/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/oslo_messaging/tests/drivers/zmq/matchmaker/__init__.py b/oslo_messaging/tests/drivers/zmq/matchmaker/__init__.py new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/oslo_messaging/tests/drivers/zmq/matchmaker/__init__.py @@ -0,0 +1 @@ + diff --git a/oslo_messaging/tests/drivers/test_impl_matchmaker.py b/oslo_messaging/tests/drivers/zmq/matchmaker/test_impl_matchmaker.py similarity index 100% rename from oslo_messaging/tests/drivers/test_impl_matchmaker.py rename to oslo_messaging/tests/drivers/zmq/matchmaker/test_impl_matchmaker.py diff --git a/oslo_messaging/tests/drivers/test_matchmaker_redis.py b/oslo_messaging/tests/drivers/zmq/matchmaker/test_matchmaker_redis.py similarity index 100% rename from oslo_messaging/tests/drivers/test_matchmaker_redis.py rename to oslo_messaging/tests/drivers/zmq/matchmaker/test_matchmaker_redis.py diff --git a/oslo_messaging/tests/drivers/test_impl_zmq.py b/oslo_messaging/tests/drivers/zmq/test_impl_zmq.py similarity index 78% rename from oslo_messaging/tests/drivers/test_impl_zmq.py rename to oslo_messaging/tests/drivers/zmq/test_impl_zmq.py index dd6df4448..563483a25 100644 --- a/oslo_messaging/tests/drivers/test_impl_zmq.py +++ b/oslo_messaging/tests/drivers/zmq/test_impl_zmq.py @@ -13,8 +13,6 @@ # under the License. import logging -import os -import re import socket import threading @@ -22,11 +20,9 @@ import fixtures import testtools import oslo_messaging -from oslo_messaging._drivers.common import RPCException from oslo_messaging._drivers import impl_zmq from oslo_messaging._drivers.zmq_driver.broker.zmq_broker import ZmqBroker from oslo_messaging._drivers.zmq_driver import zmq_async -from oslo_messaging._drivers.zmq_driver import zmq_serializer from oslo_messaging._i18n import _ from oslo_messaging.tests import utils as test_utils @@ -248,52 +244,3 @@ class TestPoller(test_utils.BaseTestCase): incoming, socket = reply_poller.poll(1) self.assertIsNone(incoming) self.assertIsNone(socket) - - -class TestZmqSerializer(test_utils.BaseTestCase): - - def test_message_without_topic_raises_RPCException(self): - # The topic is the 4th element of the message. - msg_without_topic = ['only', 'three', 'parts'] - - expected = "Message did not contain a topic: %s" % msg_without_topic - with self.assertRaisesRegexp(RPCException, re.escape(expected)): - zmq_serializer.get_topic_from_call_message(msg_without_topic) - - def test_invalid_topic_format_raises_RPCException(self): - invalid_topic = "no dots to split on, so not index-able".encode('utf8') - bad_message = ['', '', '', invalid_topic] - - expected_msg = "Topic was not formatted correctly: %s" - expected_msg = expected_msg % invalid_topic.decode('utf8') - with self.assertRaisesRegexp(RPCException, expected_msg): - zmq_serializer.get_topic_from_call_message(bad_message) - - def test_py3_decodes_bytes_correctly(self): - message = ['', '', '', b'topic.ipaddress'] - - actual, _ = zmq_serializer.get_topic_from_call_message(message) - - self.assertEqual('topic', actual) - - def test_bad_characters_in_topic_raise_RPCException(self): - # handle unexpected os path separators: - unexpected_evil = '<' - os.path.sep = unexpected_evil - - unexpected_alt_evil = '>' - os.path.altsep = unexpected_alt_evil - - evil_chars = [unexpected_evil, unexpected_alt_evil, '\\', '/'] - - for evil_char in evil_chars: - evil_topic = '%s%s%s' % ('trust.me', evil_char, 'please') - evil_topic = evil_topic.encode('utf8') - evil_message = ['', '', '', evil_topic] - - expected_msg = "Topic contained dangerous characters: %s" - expected_msg = expected_msg % evil_topic.decode('utf8') - expected_msg = re.escape(expected_msg) - - with self.assertRaisesRegexp(RPCException, expected_msg): - zmq_serializer.get_topic_from_call_message(evil_message) diff --git a/oslo_messaging/tests/drivers/zmq/test_zmq_serializer.py b/oslo_messaging/tests/drivers/zmq/test_zmq_serializer.py new file mode 100644 index 000000000..48f52734e --- /dev/null +++ b/oslo_messaging/tests/drivers/zmq/test_zmq_serializer.py @@ -0,0 +1,67 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import os +import re + +from oslo_messaging._drivers.common import RPCException +from oslo_messaging._drivers.zmq_driver import zmq_serializer +from oslo_messaging.tests import utils as test_utils + + +class TestZmqSerializer(test_utils.BaseTestCase): + + def test_message_without_topic_raises_RPCException(self): + # The topic is the 4th element of the message. + msg_without_topic = ['only', 'three', 'parts'] + + expected = "Message did not contain a topic: %s" % msg_without_topic + with self.assertRaisesRegexp(RPCException, re.escape(expected)): + zmq_serializer.get_topic_from_call_message(msg_without_topic) + + def test_invalid_topic_format_raises_RPCException(self): + invalid_topic = "no dots to split on, so not index-able".encode('utf8') + bad_message = ['', '', '', invalid_topic] + + expected_msg = "Topic was not formatted correctly: %s" + expected_msg = expected_msg % invalid_topic.decode('utf8') + with self.assertRaisesRegexp(RPCException, expected_msg): + zmq_serializer.get_topic_from_call_message(bad_message) + + def test_py3_decodes_bytes_correctly(self): + message = ['', '', '', b'topic.ipaddress'] + + actual, _ = zmq_serializer.get_topic_from_call_message(message) + + self.assertEqual('topic', actual) + + def test_bad_characters_in_topic_raise_RPCException(self): + # handle unexpected os path separators: + unexpected_evil = '<' + os.path.sep = unexpected_evil + + unexpected_alt_evil = '>' + os.path.altsep = unexpected_alt_evil + + evil_chars = [unexpected_evil, unexpected_alt_evil, '\\', '/'] + + for evil_char in evil_chars: + evil_topic = '%s%s%s' % ('trust.me', evil_char, 'please') + evil_topic = evil_topic.encode('utf8') + evil_message = ['', '', '', evil_topic] + + expected_msg = "Topic contained dangerous characters: %s" + expected_msg = expected_msg % evil_topic.decode('utf8') + expected_msg = re.escape(expected_msg) + + with self.assertRaisesRegexp(RPCException, expected_msg): + zmq_serializer.get_topic_from_call_message(evil_message)