Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: python
dist: trusty
sudo: required

python:
- "2.7"
- "3.6"

install:
- "pip install nose-cov"
- "pip install pep8"
- "pip install robotframework"
- "pip install six"
- "pip install ."

script:
- python utest/run.py
2 changes: 1 addition & 1 deletion atest/Protocols.robot
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Server '${server}' should get '${msg}' from '${ip}':'${port}'
Verify server gets from
[Arguments] ${ip} ${port} ${expected} ${server} ${connection}=
${msg} ${from ip} ${from port} = Server receives binary from name=${server} connection=${connection}
Should be equal ${msg} ${expected}
Should be equal as Strings ${msg} ${expected}
Should be equal ${from ip} ${ip}
Should be equal as integers ${from port} ${port}

Expand Down
4 changes: 2 additions & 2 deletions atest/basic_network_operations.robot
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ Resource Protocols.robot
TCP Client sends binary to server
Client sends binary foo
${message}= Server receives binary
Should be equal ${message} foo
Should be equal as strings ${message} foo

TCP Server sends binary to client
Server sends binary foo
${message}= Client receives binary
Should be equal ${message} foo
Should be equal as strings ${message} foo

Multiple UDP clients
[Setup] Start two udp clients
Expand Down
4 changes: 2 additions & 2 deletions atest/ipv6.robot
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ IPv6 with protocols
Client and server send and receive binary
Client sends binary foo
${message}= Server receives binary
Should be equal ${message} foo
Should be equal as strings ${message} foo
Server sends binary bar
${message}= Client receives binary
Should be equal ${message} bar
Should be equal as strings ${message} bar
2 changes: 1 addition & 1 deletion atest/terminated_fields.robot
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Get message with chars and end byte long termination
Message with chars and end byte 0x0d0a
${msg} get message arbitrary_length:foo.bar.zig
Should be equal foo.bar.zig ${msg.arbitrary_length.ascii}
Should be equal foo.bar.zig\r\n ${msg.arbitrary_length._raw}
Should be equal as strings foo.bar.zig\r\n ${msg.arbitrary_length._raw}
should be equal as integers 13 ${msg.arbitrary_length.len}

Send and receive message with chars with end byte
Expand Down
2 changes: 1 addition & 1 deletion gen_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
from robot.libdoc import libdoc
from os.path import join, dirname

execfile(join(dirname(__file__), 'src', 'Rammbock', 'version.py'))
exec(compile(open(join(dirname(__file__), 'src', 'Rammbock', 'version.py')).read(), 'version.py', 'exec'))

libdoc(join(dirname(__file__),'src','Rammbock'), join(dirname(__file__),'Rammbock-%s.html' % VERSION))
8 changes: 4 additions & 4 deletions proto/generate_message_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def _get_message_text(messages):
return "Message content not found, run at DEBUG level to generate message contents"

def _print_element(elem, separator):
print '\n'.join(kw_stack)
print separator
print _get_message_text(elem.findall('msg'))
print '--------------------------------------'
print('\n'.join(kw_stack))
print(separator)
print(_get_message_text(elem.findall('msg')))
print('--------------------------------------')

for event, elem in ET.iterparse(source, events=('start', 'end')):
if elem.tag not in ("kw", "test", "suite", "statistics"):
Expand Down
21 changes: 11 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
from os.path import join, dirname, abspath

CURDIR = dirname(abspath(__file__))
execfile(join(CURDIR, 'src', 'Rammbock', 'version.py'))
exec(compile(open(join(CURDIR, 'src', 'Rammbock', 'version.py')).read(), 'version.py', 'exec'))

setup(name = 'robotframework-rammbock',
version = VERSION,
description = 'Protocol testing library for Robot Framework',
author = 'Robot Framework Developers',
author_email = 'robotframework-users@googlegroups.com',
url = 'http://github.com/robotframework/Rammbock/',
package_dir = {'' : 'src'},
packages = ['Rammbock', 'Rammbock.templates'],
long_description = open(join(CURDIR, 'README.rst')).read()
setup(name='robotframework-rammbock',
version=VERSION,
description='Protocol testing library for Robot Framework',
author='Robot Framework Developers',
author_email='robotframework-users@googlegroups.com',
url='http://github.com/robotframework/Rammbock/',
package_dir={'': 'src'},
packages=['Rammbock', 'Rammbock.templates'],
long_description=open(join(CURDIR, 'README.rst')).read(),
install_requires=['robotframework', 'six']
)
2 changes: 1 addition & 1 deletion src/Rammbock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from rammbock import Rammbock
from .rammbock import Rammbock
19 changes: 11 additions & 8 deletions src/Rammbock/binary_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
import binascii
import struct

from robot.libraries.BuiltIn import BuiltIn

try:
if bin(0):
pass
except NameError, name_error:
except NameError as name_error:
def bin(value):
"""
Support for Python 2.5
Expand All @@ -44,8 +46,8 @@ def bin(value):

def to_bin(string_value):
if string_value in (None, ''):
return ''
string_value = str(string_value)
return b''
string_value = BuiltIn().convert_to_string(string_value)
if string_value.startswith('0x'):
return _hex_to_bin(string_value)
elif string_value.startswith('0b'):
Expand All @@ -55,9 +57,9 @@ def to_bin(string_value):


def _int_to_bin(integer):
if integer >= 18446744073709551616L:
if integer >= 18446744073709551616:
return to_bin(hex(integer))
return LONGLONG.pack(integer).lstrip('\x00') or '\x00'
return LONGLONG.pack(integer).lstrip(b'\x00') or b'\x00'


def _hex_to_bin(string_value):
Expand All @@ -72,15 +74,15 @@ def to_bin_of_length(length, string_value):
if len(bin) > length:
raise AssertionError('Too long binary value %s (max length %d)'
% (string_value, length))
return bin.rjust(length, '\x00')
return bin.rjust(length, b'\x00')


def to_hex(binary):
return binascii.hexlify(binary)


def to_0xhex(binary):
return '0x' + to_hex(binary)
return b'0x' + to_hex(binary)


def to_binary_string_of_length(length, bytes):
Expand Down Expand Up @@ -136,8 +138,9 @@ def _invert(value):


def to_int(string_value):
if string_value in (None, ''):
if string_value in (None, '', b''):
raise Exception("No value or empty value given")
string_value = BuiltIn().convert_to_string(string_value)
if string_value.startswith('0x') or string_value[:3] == '-0x':
return int(string_value, 16)
elif string_value.startswith('0b') or string_value[:3] == '-0b':
Expand Down
13 changes: 11 additions & 2 deletions src/Rammbock/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
TBCDContainerTemplate)
from .binary_tools import to_0xhex, to_bin

from robot.libraries.BuiltIn import BuiltIn
from robot.utils import is_string, PY3


class RammbockCore(object):

Expand Down Expand Up @@ -351,6 +354,8 @@ def client_sends_binary(self, message, name=None, label=None):
| Client sends binary | Hello! |
| Client sends binary | ${some binary} | Client1 | label=DebugMessage |
"""
if PY3 and isinstance(message, str):
message = BuiltIn().convert_to_bytes(message)
client, name = self._clients.get_with_name(name)
client.send(message)
self._register_send(client, label, name)
Expand All @@ -367,6 +372,8 @@ def server_sends_binary(self, message, name=None, connection=None, label=None):
| Server sends binary | ${some binary} | Server1 | label=DebugMessage |
| Server sends binary | ${some binary} | connection=my_connection |
"""
if PY3 and isinstance(message, str):
message = message.encode()
server, name = self._servers.get_with_name(name)
server.send(message, alias=connection)
self._register_send(server, label, name, connection=connection)
Expand Down Expand Up @@ -451,7 +458,7 @@ def save_template(self, name, unlocked=False):
| Save Template | MyMessage |
| Save Template | MyOtherMessage | unlocked=True |
"""
if isinstance(unlocked, basestring):
if is_string(unlocked):
unlocked = unlocked.lower() != 'false'
template = self._get_message_template()
if not unlocked:
Expand Down Expand Up @@ -661,7 +668,7 @@ def _receive(self, nodes, *parameters):
yield msg, message_fields, header_fields
self._register_receive(node, self._current_container.name, name)
logger.debug("Received %s" % repr(msg))
except AssertionError, e:
except AssertionError as e:
self._register_receive(node, self._current_container.name, name, error=e.args[0])
raise e

Expand Down Expand Up @@ -992,6 +999,8 @@ def _name_and_value(self, separator, parameter):
index = parameter.find(separator)
try:
key = str(parameter[:index].strip())
if PY3:
key.encode('ascii')
except UnicodeError:
raise Exception("Only ascii characters are supported in parameters.")
return key, parameter[index + 1:].strip()
Expand Down
35 changes: 20 additions & 15 deletions src/Rammbock/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
from .binary_tools import to_0xhex, to_binary_string_of_length, \
to_bin_of_length, to_tbcd_value, to_tbcd_binary, from_twos_comp
from .ordered_dict import OrderedDict
from robot.libraries.BuiltIn import BuiltIn
from robot.utils import py2to3, unic


@py2to3
class _StructuredElement(object):

_type = None
Expand All @@ -28,17 +31,17 @@ def __init__(self, name):
self._parent = None

def __setitem__(self, name, child):
self._fields[unicode(name)] = child
self._fields[unic(name)] = child
child._parent = self

def __getitem__(self, name):
return self._fields[unicode(name)]
return self._fields[unic(name)]

def __getattr__(self, name):
return self[name]

def __delitem__(self, name):
name = unicode(name)
name = unic(name)
item = self._fields[name]
del self._fields[name]
item._parent = None
Expand All @@ -53,7 +56,7 @@ def __repr__(self):
return result

def __contains__(self, key):
return unicode(key) in self._fields
return unic(key) in self._fields

def _format_indented(self, text):
return ''.join([' %s\n' % line for line in text.splitlines()])
Expand All @@ -66,7 +69,7 @@ def _get_name(self):
return '%s %s' % (self._type, self._name)

def _get_raw_bytes(self):
return ''.join((field._raw for field in self._fields.values()))
return b''.join((field._raw for field in self._fields.values()))

def __len__(self):
return sum(len(field) for field in self._fields.values())
Expand Down Expand Up @@ -134,8 +137,8 @@ def _get_aligned(self, length):
return length + (self._align - length % self._align) % self._align

def _get_raw_bytes(self):
result = ''.join((field._raw for field in self._fields.values()))
return result.ljust(self._get_aligned(len(result)), '\x00')
result = b''.join((field._raw for field in self._fields.values()))
return result.ljust(self._get_aligned(len(result)), b'\x00')


class Union(_StructuredElement):
Expand All @@ -151,7 +154,7 @@ def _get_raw_bytes(self):
for field in self._fields.values():
if len(field._raw) > len(max_raw):
max_raw = field._raw
return max_raw.ljust(self._length, '\x00')
return max_raw.ljust(self._length, b'\x00')

def __len__(self):
return self._length
Expand All @@ -169,7 +172,7 @@ def _binlength(self):
return sum(field.binlength for field in self._fields.values())

def __len__(self):
return self._binlength() / 8
return self._binlength() // 8

def _get_raw_bytes(self):
# TODO: faster implementation...
Expand Down Expand Up @@ -219,6 +222,7 @@ class Header(_StructuredElement):
_type = 'Header'


@py2to3
class Field(object):

def __init__(self, type, name, value, aligned_len=None, little_endian=False):
Expand Down Expand Up @@ -258,15 +262,12 @@ def sint(self):

@property
def hex(self):
return hex(self)
return BuiltIn().convert_to_string(to_0xhex(self._value))

@property
def tbcd(self):
return to_tbcd_value(self._original_value)

def __hex__(self):
return to_0xhex(self._value)

def __nonzero__(self):
return True

Expand All @@ -287,11 +288,15 @@ def _bin(self):

@property
def ascii(self):
return ''.join(i for i in self._value if 128 > ord(i) >= 32)
try:
result = ''.join(i for i in self._value if 128 > ord(i) >= 32)
except TypeError:
result = ''.join(chr(i) for i in self._value if 128 > i >= 32)
return result

@property
def _raw(self):
return self._original_value.ljust(self._length, '\x00')
return self._original_value.ljust(self._length, b'\x00')

def __str__(self):
return str(self.__getattribute__(self._type))
Expand Down
Loading