webssh/tests/test_handler.py

101 lines
3.8 KiB
Python
Raw Normal View History

2018-04-24 01:59:51 +00:00
import unittest
2018-04-25 13:05:01 +00:00
import paramiko
2018-04-24 01:59:51 +00:00
2018-04-24 02:57:25 +00:00
from tornado.httputil import HTTPServerRequest
from tests.utils import read_file, make_tests_data_path
2018-05-30 13:29:44 +00:00
from webssh.handler import MixinHandler, IndexHandler, parse_encoding
class TestHandler(unittest.TestCase):
def test_parse_encoding(self):
data = ''
self.assertIsNone(parse_encoding(data))
data = 'UTF-8'
self.assertEqual(parse_encoding(data), 'UTF-8')
data = 'en_US.UTF-8'
self.assertEqual(parse_encoding(data), 'UTF-8')
data = 'LANG=en_US.UTF-8\nLANGUAGE=\nLC_CTYPE="en_US.UTF-8"\n'
self.assertEqual(parse_encoding(data), 'UTF-8')
data = 'LANGUAGE=\nLC_CTYPE="en_US.UTF-8"\n'
self.assertEqual(parse_encoding(data), 'UTF-8')
2018-04-24 01:59:51 +00:00
class TestMixinHandler(unittest.TestCase):
2018-04-25 11:01:54 +00:00
def test_get_real_client_addr(self):
2018-04-24 01:59:51 +00:00
handler = MixinHandler()
2018-04-24 02:57:25 +00:00
handler.request = HTTPServerRequest(uri='/')
2018-04-24 01:59:51 +00:00
self.assertIsNone(handler.get_real_client_addr())
2018-04-24 02:23:45 +00:00
ip = '127.0.0.1'
2018-04-24 02:57:25 +00:00
handler.request.headers.add('X-Real-Ip', ip)
2018-04-25 11:01:54 +00:00
self.assertEqual(handler.get_real_client_addr(), False)
2018-04-24 02:23:45 +00:00
2018-04-25 11:01:54 +00:00
handler.request.headers.add('X-Real-Port', '12345x')
self.assertEqual(handler.get_real_client_addr(), False)
2018-04-24 02:23:45 +00:00
2018-04-25 11:01:54 +00:00
handler.request.headers.update({'X-Real-Port': '12345'})
self.assertEqual(handler.get_real_client_addr(), (ip, 12345))
2018-04-24 01:59:51 +00:00
2018-04-25 11:01:54 +00:00
handler.request.headers.update({'X-Real-ip': None})
self.assertEqual(handler.get_real_client_addr(), False)
handler.request.headers.update({'X-Real-Port': '12345x'})
self.assertEqual(handler.get_real_client_addr(), False)
2018-04-25 13:05:01 +00:00
class TestIndexHandler(unittest.TestCase):
def test_get_specific_pkey_with_plain_key(self):
fname = 'test_rsa.key'
cls = paramiko.RSAKey
key = read_file(make_tests_data_path(fname))
2018-04-25 13:05:01 +00:00
pkey = IndexHandler.get_specific_pkey(cls, key, None)
self.assertIsInstance(pkey, cls)
2018-08-20 10:35:50 +00:00
pkey = IndexHandler.get_specific_pkey(cls, key, 'iginored')
2018-04-25 13:05:01 +00:00
self.assertIsInstance(pkey, cls)
pkey = IndexHandler.get_specific_pkey(cls, 'x'+key, None)
self.assertIsNone(pkey)
def test_get_specific_pkey_with_encrypted_key(self):
fname = 'test_rsa_password.key'
cls = paramiko.RSAKey
2018-08-20 10:35:50 +00:00
password = 'television'
2018-04-25 13:05:01 +00:00
key = read_file(make_tests_data_path(fname))
2018-04-25 13:05:01 +00:00
pkey = IndexHandler.get_specific_pkey(cls, key, password)
self.assertIsInstance(pkey, cls)
pkey = IndexHandler.get_specific_pkey(cls, 'x'+key, None)
self.assertIsNone(pkey)
with self.assertRaises(ValueError):
pkey = IndexHandler.get_specific_pkey(cls, key, None)
def test_get_pkey_obj_with_plain_key(self):
fname = 'test_ed25519.key'
cls = paramiko.Ed25519Key
key = read_file(make_tests_data_path(fname))
2018-08-22 09:37:23 +00:00
pkey = IndexHandler.get_pkey_obj(key, None, fname)
2018-04-25 13:05:01 +00:00
self.assertIsInstance(pkey, cls)
2018-08-22 09:37:23 +00:00
pkey = IndexHandler.get_pkey_obj(key, 'iginored', fname)
2018-04-25 13:05:01 +00:00
self.assertIsInstance(pkey, cls)
2018-08-22 09:37:23 +00:00
with self.assertRaises(ValueError) as exc:
pkey = IndexHandler.get_pkey_obj('x'+key, None, fname)
self.assertIn('Invalid private key', str(exc))
2018-04-25 13:05:01 +00:00
def test_get_pkey_obj_with_encrypted_key(self):
fname = 'test_ed25519_password.key'
password = 'abc123'
cls = paramiko.Ed25519Key
key = read_file(make_tests_data_path(fname))
2018-08-22 09:37:23 +00:00
pkey = IndexHandler.get_pkey_obj(key, password, fname)
2018-04-25 13:05:01 +00:00
self.assertIsInstance(pkey, cls)
2018-08-22 09:37:23 +00:00
with self.assertRaises(ValueError) as exc:
pkey = IndexHandler.get_pkey_obj(key, 'wrongpass', fname)
self.assertIn('Wrong password', str(exc))
with self.assertRaises(ValueError) as exc:
pkey = IndexHandler.get_pkey_obj('x'+key, password, fname)
self.assertIn('Invalid private key', str(exc))