test_websocketproxy.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # vim: tabstop=4 shiftwidth=4 softtabstop=4
  2. # Copyright(c) 2015 Red Hat, Inc All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. """ Unit tests for websocketproxy """
  16. import sys
  17. import unittest
  18. import unittest
  19. import socket
  20. from io import StringIO
  21. from io import BytesIO
  22. from unittest.mock import patch, MagicMock
  23. from websockify import websocketproxy
  24. from websockify import token_plugins
  25. from websockify import auth_plugins
  26. class FakeSocket(object):
  27. def __init__(self, data=b''):
  28. self._data = data
  29. def recv(self, amt, flags=None):
  30. res = self._data[0:amt]
  31. if not (flags & socket.MSG_PEEK):
  32. self._data = self._data[amt:]
  33. return res
  34. def makefile(self, mode='r', buffsize=None):
  35. if 'b' in mode:
  36. return BytesIO(self._data)
  37. else:
  38. return StringIO(self._data.decode('latin_1'))
  39. class FakeServer(object):
  40. class EClose(Exception):
  41. pass
  42. def __init__(self):
  43. self.token_plugin = None
  44. self.auth_plugin = None
  45. self.wrap_cmd = None
  46. self.ssl_target = None
  47. self.unix_target = None
  48. class ProxyRequestHandlerTestCase(unittest.TestCase):
  49. def setUp(self):
  50. super(ProxyRequestHandlerTestCase, self).setUp()
  51. self.handler = websocketproxy.ProxyRequestHandler(
  52. FakeSocket(), "127.0.0.1", FakeServer())
  53. self.handler.path = "https://localhost:6080/websockify?token=blah"
  54. self.handler.headers = None
  55. patch('websockify.websockifyserver.WebSockifyServer.socket').start()
  56. def tearDown(self):
  57. patch.stopall()
  58. super(ProxyRequestHandlerTestCase, self).tearDown()
  59. def test_get_target(self):
  60. class TestPlugin(token_plugins.BasePlugin):
  61. def lookup(self, token):
  62. return ("some host", "some port")
  63. host, port = self.handler.get_target(
  64. TestPlugin(None))
  65. self.assertEqual(host, "some host")
  66. self.assertEqual(port, "some port")
  67. def test_get_target_unix_socket(self):
  68. class TestPlugin(token_plugins.BasePlugin):
  69. def lookup(self, token):
  70. return ("unix_socket", "/tmp/socket")
  71. _, socket = self.handler.get_target(
  72. TestPlugin(None))
  73. self.assertEqual(socket, "/tmp/socket")
  74. def test_get_target_raises_error_on_unknown_token(self):
  75. class TestPlugin(token_plugins.BasePlugin):
  76. def lookup(self, token):
  77. return None
  78. with self.assertRaises(FakeServer.EClose):
  79. self.handler.get_target(TestPlugin(None))
  80. @patch('websockify.websocketproxy.ProxyRequestHandler.send_auth_error', MagicMock())
  81. def test_token_plugin(self):
  82. class TestPlugin(token_plugins.BasePlugin):
  83. def lookup(self, token):
  84. return (self.source + token).split(',')
  85. self.handler.server.token_plugin = TestPlugin("somehost,")
  86. self.handler.validate_connection()
  87. self.assertEqual(self.handler.server.target_host, "somehost")
  88. self.assertEqual(self.handler.server.target_port, "blah")
  89. @patch('websockify.websocketproxy.ProxyRequestHandler.send_auth_error', MagicMock())
  90. def test_auth_plugin(self):
  91. class TestPlugin(auth_plugins.BasePlugin):
  92. def authenticate(self, headers, target_host, target_port):
  93. if target_host == self.source:
  94. raise auth_plugins.AuthenticationError(response_msg="some_error")
  95. self.handler.server.auth_plugin = TestPlugin("somehost")
  96. self.handler.server.target_host = "somehost"
  97. self.handler.server.target_port = "someport"
  98. with self.assertRaises(auth_plugins.AuthenticationError):
  99. self.handler.auth_connection()
  100. self.handler.server.target_host = "someotherhost"
  101. self.handler.auth_connection()