2009-07-19 22:45:02 -04:00
|
|
|
# Copyright (C) 2003-2009 Robey Pointer <robeypointer@gmail.com>
|
2004-09-11 17:01:32 -04:00
|
|
|
#
|
|
|
|
# This file is part of paramiko.
|
|
|
|
#
|
|
|
|
# Paramiko is free software; you can redistribute it and/or modify it under the
|
|
|
|
# terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
# Software Foundation; either version 2.1 of the License, or (at your option)
|
|
|
|
# any later version.
|
|
|
|
#
|
2013-09-28 00:29:18 -04:00
|
|
|
# Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY
|
2004-09-11 17:01:32 -04:00
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with Paramiko; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Some unit tests for ssh protocol message blocks.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import unittest
|
2013-11-01 03:37:11 -04:00
|
|
|
from binascii import unhexlify
|
2004-09-11 17:01:32 -04:00
|
|
|
from paramiko.message import Message
|
2013-10-30 19:19:30 -04:00
|
|
|
from paramiko.common import *
|
2004-09-11 17:01:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
class MessageTest (unittest.TestCase):
|
|
|
|
|
2013-11-01 03:37:11 -04:00
|
|
|
__a = unhexlify('000000170760e09000000001710000000568656c6c6f000003e8') + (b('x') * 1000)
|
|
|
|
__b = unhexlify('0100f3003f00000010687565792c64657765792c6c6f756965')
|
|
|
|
__c = unhexlify('00000005ff0000000700f5e4d3c2b10900000001110000000700f5e4d3c2b109000000069a1b2c3d4ef7')
|
|
|
|
__d = unhexlify('00000005ff000000051122334455ff0000000a00f00000000000000000010000000363617400000003612c62')
|
2004-09-11 17:01:32 -04:00
|
|
|
|
|
|
|
def test_1_encode(self):
|
|
|
|
msg = Message()
|
|
|
|
msg.add_int(23)
|
|
|
|
msg.add_int(123789456)
|
|
|
|
msg.add_string('q')
|
|
|
|
msg.add_string('hello')
|
|
|
|
msg.add_string('x' * 1000)
|
2013-10-30 20:09:34 -04:00
|
|
|
self.assertEquals(msg.asbytes(), self.__a)
|
2004-09-11 17:01:32 -04:00
|
|
|
|
|
|
|
msg = Message()
|
|
|
|
msg.add_boolean(True)
|
|
|
|
msg.add_boolean(False)
|
2013-10-30 20:09:34 -04:00
|
|
|
msg.add_byte(byte_chr(0xf3))
|
|
|
|
|
|
|
|
msg.add_bytes(zero_byte + byte_chr(0x3f))
|
2004-09-11 17:01:32 -04:00
|
|
|
msg.add_list(['huey', 'dewey', 'louie'])
|
2013-10-30 20:09:34 -04:00
|
|
|
self.assertEquals(msg.asbytes(), self.__b)
|
2004-09-11 17:01:32 -04:00
|
|
|
|
|
|
|
msg = Message()
|
|
|
|
msg.add_int64(5)
|
2013-10-30 20:09:34 -04:00
|
|
|
msg.add_int64(0xf5e4d3c2b109)
|
2004-09-11 17:01:32 -04:00
|
|
|
msg.add_mpint(17)
|
2013-10-30 20:09:34 -04:00
|
|
|
msg.add_mpint(0xf5e4d3c2b109)
|
|
|
|
msg.add_mpint(-0x65e4d3c2b109)
|
|
|
|
self.assertEquals(msg.asbytes(), self.__c)
|
2004-09-11 17:01:32 -04:00
|
|
|
|
|
|
|
def test_2_decode(self):
|
|
|
|
msg = Message(self.__a)
|
|
|
|
self.assertEquals(msg.get_int(), 23)
|
|
|
|
self.assertEquals(msg.get_int(), 123789456)
|
2013-10-30 20:09:34 -04:00
|
|
|
self.assertEquals(msg.get_text(), 'q')
|
|
|
|
self.assertEquals(msg.get_text(), 'hello')
|
|
|
|
self.assertEquals(msg.get_text(), 'x' * 1000)
|
2004-09-11 17:01:32 -04:00
|
|
|
|
|
|
|
msg = Message(self.__b)
|
|
|
|
self.assertEquals(msg.get_boolean(), True)
|
|
|
|
self.assertEquals(msg.get_boolean(), False)
|
2013-10-30 20:09:34 -04:00
|
|
|
self.assertEquals(msg.get_byte(), byte_chr(0xf3))
|
|
|
|
self.assertEquals(msg.get_bytes(2), zero_byte + byte_chr(0x3f))
|
2004-09-11 17:01:32 -04:00
|
|
|
self.assertEquals(msg.get_list(), ['huey', 'dewey', 'louie'])
|
|
|
|
|
|
|
|
msg = Message(self.__c)
|
|
|
|
self.assertEquals(msg.get_int64(), 5)
|
2013-10-30 20:09:34 -04:00
|
|
|
self.assertEquals(msg.get_int64(), 0xf5e4d3c2b109)
|
2004-09-11 17:01:32 -04:00
|
|
|
self.assertEquals(msg.get_mpint(), 17)
|
2013-10-30 20:09:34 -04:00
|
|
|
self.assertEquals(msg.get_mpint(), 0xf5e4d3c2b109)
|
|
|
|
self.assertEquals(msg.get_mpint(), -0x65e4d3c2b109)
|
2004-09-11 17:01:32 -04:00
|
|
|
|
|
|
|
def test_3_add(self):
|
|
|
|
msg = Message()
|
|
|
|
msg.add(5)
|
2013-10-30 20:09:34 -04:00
|
|
|
msg.add(0x1122334455)
|
|
|
|
msg.add(0xf00000000000000000)
|
2004-09-11 17:01:32 -04:00
|
|
|
msg.add(True)
|
|
|
|
msg.add('cat')
|
|
|
|
msg.add(['a', 'b'])
|
2013-10-30 20:09:34 -04:00
|
|
|
self.assertEquals(msg.asbytes(), self.__d)
|
2004-09-11 17:01:32 -04:00
|
|
|
|
|
|
|
def test_4_misc(self):
|
|
|
|
msg = Message(self.__d)
|
|
|
|
self.assertEquals(msg.get_int(), 5)
|
2013-10-30 20:09:34 -04:00
|
|
|
self.assertEquals(msg.get_int(), 0x1122334455)
|
|
|
|
self.assertEquals(msg.get_int(), 0xf00000000000000000)
|
|
|
|
self.assertEquals(msg.get_so_far(), self.__d[:29])
|
|
|
|
self.assertEquals(msg.get_remainder(), self.__d[29:])
|
2004-09-25 17:32:53 -04:00
|
|
|
msg.rewind()
|
|
|
|
self.assertEquals(msg.get_int(), 5)
|
|
|
|
self.assertEquals(msg.get_so_far(), self.__d[:4])
|
|
|
|
self.assertEquals(msg.get_remainder(), self.__d[4:])
|