def test_decode_ros_handshake_header(self): from rosgraph.network import decode_ros_handshake_header, ROSHandshakeException invalids = [b"field1",b"",] # prepend field length invalids = [(struct.pack('<I', len(s)) + s) for s in invalids] # prepend message length invalids = [(struct.pack('<I', len(s)) + s) for s in invalids] # invalid message length prefix valid = b"a=b" valid = struct.pack('<I', len(valid)) + valid invalids.append(struct.pack('<I', 123)+valid) # invalid field length prefix invalid = struct.pack('<I', 123) + b'a=b' invalids.append(struct.pack("<I", len(invalid)) + invalid) for i in invalids: try: decode_ros_handshake_header(i) assert False, "should have failed: %s"%i except ROSHandshakeException: pass assert {} == decode_ros_handshake_header(struct.pack('<I', 0)) # single-field tests tests = [ (b"a=b", {'a': 'b'}), # whitespace in keys is ignored (b" a =b", {'a': 'b'}), (b'newlines=\n\n\n\n', {'newlines': '\n\n\n\n'}), (b'equals=foo=bar=car', {'equals': 'foo=bar=car'}), (b"spaces=one two three four",{'spaces': 'one two three four'}), ] for s, d in tests: # add in length fields s = struct.pack('<I', len(s)+4) + struct.pack('<I', len(s)) + s assert d == decode_ros_handshake_header(s) # multi-field tests tests = [ {'a': 'b', 'c': 'd'}, {'spaces': ' ', 'tabs': '\t\t\t\t', 'equals': '====='}, ] for t in tests: s = b'' for k, v in t.items(): f = "%s=%s"%(k, v) f = f.encode() s += struct.pack('<I', len(f)) + f s = struct.pack('<I', len(s)) + s assert t == decode_ros_handshake_header(s) # make sure that decode ignores extra past header len assert t == decode_ros_handshake_header(s+s)
def test_decode_ros_handshake_header(self): from rosgraph.network import decode_ros_handshake_header, ROSHandshakeException invalids = [ "field1", "", ] # prepend field length invalids = [(struct.pack('<I', len(s)) + s) for s in invalids] # prepend message length invalids = [(struct.pack('<I', len(s)) + s) for s in invalids] # invalid message length prefix valid = "a=b" valid = struct.pack('<I', len(valid)) + valid invalids.append(struct.pack('<I', 123) + valid) # invalid field length prefix invalid = struct.pack('<I', 123) + 'a=b' invalids.append(struct.pack("<I", len(invalid)) + invalid) for i in invalids: try: decode_ros_handshake_header(i) assert False, "should have failed: %s" % i except ROSHandshakeException: pass assert {} == decode_ros_handshake_header(struct.pack('<I', 0)) # single-field tests tests = [ ("a=b", { 'a': 'b' }), # whitespace in keys is ignored (" a =b", { 'a': 'b' }), ('newlines=\n\n\n\n', { 'newlines': '\n\n\n\n' }), ('equals=foo=bar=car', { 'equals': 'foo=bar=car' }), ("spaces=one two three four", { 'spaces': 'one two three four' }), ] for s, d in tests: # add in length fields s = struct.pack('<I', len(s) + 4) + struct.pack('<I', len(s)) + s assert d == decode_ros_handshake_header(s) # multi-field tests tests = [ { 'a': 'b', 'c': 'd' }, { 'spaces': ' ', 'tabs': '\t\t\t\t', 'equals': '=====' }, ] for t in tests: s = '' for k, v in t.iteritems(): f = "%s=%s" % (k, v) s += struct.pack('<I', len(f)) + f s = struct.pack('<I', len(s)) + s assert t == decode_ros_handshake_header(s) # make sure that decode ignores extra past header len assert t == decode_ros_handshake_header(s + s)