| 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 |
import os |
| 23 |
|
| 24 |
from proton import * |
| 25 |
|
| 26 |
from . import common |
| 27 |
|
| 28 |
|
| 29 |
def find_test_interop_dir(): |
| 30 |
"""Find the common tests directory relative to this script""" |
| 31 |
from os.path import dirname, join, abspath, isdir |
| 32 |
f = dirname(dirname(dirname(dirname(abspath(__file__))))) |
| 33 |
f = join(f, "tests", "interop") |
| 34 |
if not isdir(f): |
| 35 |
raise Exception("Cannot find tests/interop directory from " + __file__) |
| 36 |
return f |
| 37 |
|
| 38 |
|
| 39 |
test_interop_dir = find_test_interop_dir() |
| 40 |
|
| 41 |
|
| 42 |
class InteropTest(common.Test): |
| 43 |
|
| 44 |
def setUp(self): |
| 45 |
self.data = Data() |
| 46 |
self.message = Message() |
| 47 |
|
| 48 |
def tearDown(self): |
| 49 |
self.data = None |
| 50 |
|
| 51 |
def get_data(self, name): |
| 52 |
filename = os.path.join(test_interop_dir, name + ".amqp") |
| 53 |
f = open(filename, "rb") |
| 54 |
try: |
| 55 |
return f.read() |
| 56 |
finally: |
| 57 |
f.close() |
| 58 |
|
| 59 |
def decode_data(self, encoded): |
| 60 |
buffer = encoded |
| 61 |
while buffer: |
| 62 |
n = self.data.decode(buffer) |
| 63 |
buffer = buffer[n:] |
| 64 |
self.data.rewind() |
| 65 |
|
| 66 |
def decode_data_file(self, name): |
| 67 |
encoded = self.get_data(name) |
| 68 |
self.decode_data(encoded) |
| 69 |
encoded_size = self.data.encoded_size() |
| 70 |
# Re-encode and verify pre-computed and actual encoded size match. |
| 71 |
reencoded = self.data.encode() |
| 72 |
assert encoded_size == len(reencoded), "%d != %d" % (encoded_size, len(reencoded)) |
| 73 |
# verify round trip bytes |
| 74 |
assert reencoded == encoded, "Value mismatch: %s != %s" % (reencoded, encoded) |
| 75 |
|
| 76 |
def decode_message_file(self, name): |
| 77 |
self.message.decode(self.get_data(name)) |
| 78 |
body = self.message.body |
| 79 |
if str(type(body)) == "<type 'org.apache.qpid.proton.amqp.Binary'>": |
| 80 |
body = body.array.tostring() |
| 81 |
self.decode_data(body) |
| 82 |
|
| 83 |
def assert_next(self, type, value): |
| 84 |
next_type = self.data.next() |
| 85 |
assert next_type == type, "Type mismatch: %s != %s" % ( |
| 86 |
Data.type_names[next_type], Data.type_names[type]) |
| 87 |
next_value = self.data.get_object() |
| 88 |
assert next_value == value, "Value mismatch: %s != %s" % (next_value, value) |
| 89 |
|
| 90 |
def test_message(self): |
| 91 |
self.decode_message_file("message") |
| 92 |
self.assert_next(Data.STRING, "hello") |
| 93 |
assert self.data.next() is None |
| 94 |
|
| 95 |
def test_primitives(self): |
| 96 |
self.decode_data_file("primitives") |
| 97 |
self.assert_next(Data.BOOL, True) |
| 98 |
self.assert_next(Data.BOOL, False) |
| 99 |
self.assert_next(Data.UBYTE, 42) |
| 100 |
self.assert_next(Data.USHORT, 42) |
| 101 |
self.assert_next(Data.SHORT, -42) |
| 102 |
self.assert_next(Data.UINT, 12345) |
| 103 |
self.assert_next(Data.INT, -12345) |
| 104 |
self.assert_next(Data.ULONG, 12345) |
| 105 |
self.assert_next(Data.LONG, -12345) |
| 106 |
self.assert_next(Data.FLOAT, 0.125) |
| 107 |
self.assert_next(Data.DOUBLE, 0.125) |
| 108 |
assert self.data.next() is None |
| 109 |
|
| 110 |
def test_strings(self): |
| 111 |
self.decode_data_file("strings") |
| 112 |
self.assert_next(Data.BINARY, b"abc\0defg") |
| 113 |
self.assert_next(Data.STRING, "abcdefg") |
| 114 |
self.assert_next(Data.SYMBOL, "abcdefg") |
| 115 |
self.assert_next(Data.BINARY, b"") |
| 116 |
self.assert_next(Data.STRING, "") |
| 117 |
self.assert_next(Data.SYMBOL, "") |
| 118 |
assert self.data.next() is None |
| 119 |
|
| 120 |
def test_described(self): |
| 121 |
self.decode_data_file("described") |
| 122 |
self.assert_next(Data.DESCRIBED, Described("foo-descriptor", "foo-value")) |
| 123 |
self.data.exit() |
| 124 |
|
| 125 |
assert self.data.next() == Data.DESCRIBED |
| 126 |
self.data.enter() |
| 127 |
self.assert_next(Data.INT, 12) |
| 128 |
self.assert_next(Data.INT, 13) |
| 129 |
self.data.exit() |
| 130 |
|
| 131 |
assert self.data.next() is None |
| 132 |
|
| 133 |
def test_described_array(self): |
| 134 |
self.decode_data_file("described_array") |
| 135 |
self.assert_next(Data.ARRAY, Array("int-array", Data.INT, *range(0, 10))) |
| 136 |
|
| 137 |
def test_arrays(self): |
| 138 |
self.decode_data_file("arrays") |
| 139 |
self.assert_next(Data.ARRAY, Array(UNDESCRIBED, Data.INT, *range(0, 100))) |
| 140 |
self.assert_next(Data.ARRAY, Array(UNDESCRIBED, Data.STRING, *["a", "b", "c"])) |
| 141 |
self.assert_next(Data.ARRAY, Array(UNDESCRIBED, Data.INT)) |
| 142 |
assert self.data.next() is None |
| 143 |
|
| 144 |
def test_lists(self): |
| 145 |
self.decode_data_file("lists") |
| 146 |
self.assert_next(Data.LIST, [32, "foo", True]) |
| 147 |
self.assert_next(Data.LIST, []) |
| 148 |
assert self.data.next() is None |
| 149 |
|
| 150 |
def test_maps(self): |
| 151 |
self.decode_data_file("maps") |
| 152 |
self.assert_next(Data.MAP, {"one": 1, "two": 2, "three": 3}) |
| 153 |
self.assert_next(Data.MAP, {1: "one", 2: "two", 3: "three"}) |
| 154 |
self.assert_next(Data.MAP, {}) |
| 155 |
assert self.data.next() is None |