1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
|
# Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import os
import platform
import socket
import struct
import sys
from io import BytesIO, TextIOWrapper
from queue import Queue
from threading import Event
from unittest import mock
from unittest.mock import patch
from urllib.parse import urlparse
import pytest
from awscrt import websocket
from awscrt.http import (
HttpHeaders,
HttpProxyAuthenticationType,
HttpProxyOptions,
HttpRequest,
)
from awscrt.io import ClientTlsContext, TlsConnectionOptions, TlsContextOptions
from awscrt.websocket import (
IncomingFrame,
OnConnectionSetupData,
OnConnectionShutdownData,
OnIncomingFrameCompleteData,
OnIncomingFramePayloadData,
OnSendFrameCompleteData,
Opcode,
WebSocket,
)
from awscli.customizations.ec2instanceconnect.websocket import (
BaseWebsocketIO,
InputClosedError,
StdinStdoutIO,
TCPSocketIO,
Websocket,
WebsocketException,
WebsocketManager,
WindowsStdinStdoutIO,
)
from tests.markers import if_windows, skip_if_windows
class TestWebsocketIO:
def test_stdin_stdout_io_read(self, monkeypatch):
io = StdinStdoutIO()
expected_data = b"Test"
monkeypatch.setattr('sys.stdin', TextIOWrapper(BytesIO(expected_data)))
returned_data = io.read(100)
assert returned_data == expected_data
def test_stdin_stdout_io_write(self, monkeypatch):
io = StdinStdoutIO()
mock_text_io_wrapper = mock.Mock(spec=TextIOWrapper)
monkeypatch.setattr('sys.stdout', mock_text_io_wrapper)
expected_data = b"Test"
io.write(expected_data)
mock_text_io_wrapper.buffer.write.assert_called_with(expected_data)
assert mock_text_io_wrapper.flush.called
@skip_if_windows
@mock.patch("select.select")
def test_stdin_stdout_io_has_data_to_read_returns_false_non_windows(
self, mock_select
):
io = StdinStdoutIO()
expected_socket_list = [sys.stdin]
mock_select.return_value = None, None, None
return_val = io.has_data_to_read()
mock_select.assert_called_with(expected_socket_list, [], [], 0.05)
assert not return_val
@skip_if_windows
@mock.patch("select.select")
def test_stdin_stdout_io_has_data_to_read_returns_true_non_windows(
self, mock_select
):
io = StdinStdoutIO()
expected_socket_list = [sys.stdin]
mock_select.return_value = True, None, None
return_val = io.has_data_to_read()
mock_select.assert_called_with(expected_socket_list, [], [], 0.05)
assert return_val
@if_windows
def test_windows_stdin_stdout_io_has_data_to_read_always_returns_true(
self,
):
io = WindowsStdinStdoutIO()
assert io.has_data_to_read()
def test_tcp_socket_io_read(self):
mock_conn = mock.Mock(spec=socket.SocketType)
io = TCPSocketIO(mock_conn)
expected_data = b"Test"
mock_conn.recv.return_value = expected_data
bytes_to_read = 100
returned_data = io.read(bytes_to_read)
assert returned_data == expected_data
mock_conn.recv.assert_called_with(bytes_to_read)
def test_tcp_socket_io_write(self):
mock_conn = mock.Mock(spec=socket.SocketType)
io = TCPSocketIO(mock_conn)
expected_data = b"Test"
io.write(expected_data)
mock_conn.sendall.assert_called_with(expected_data)
def test_tcp_socket_io_read_raise_error_when_empty_data(self):
mock_conn = mock.Mock(spec=socket.SocketType)
io = TCPSocketIO(mock_conn)
expected_data = b""
mock_conn.recv.return_value = expected_data
bytes_to_read = 100
with pytest.raises(InputClosedError):
io.read(bytes_to_read)
mock_conn.recv.assert_called_with(bytes_to_read)
@pytest.fixture
def mock_websocketio():
return mock.Mock(spec=BaseWebsocketIO)
@pytest.fixture
def mock_crt_websocket():
return mock.Mock(spec=WebSocket)
@pytest.fixture
def mock_on_connection_event():
return mock.Mock(spec=Event)
@pytest.fixture
def mock_send_frame_results_queue():
return mock.Mock(spec=Queue)
@pytest.fixture
def mock_shutdown_event():
return mock.Mock(spec=Event)
@pytest.fixture
def mock_tls_connection_options():
return mock.Mock(spec=TlsConnectionOptions)
@pytest.fixture
def mock_handshake_request():
return mock.Mock(spec=HttpRequest)
@pytest.fixture
def web_socket(
mock_websocketio,
mock_crt_websocket,
mock_on_connection_event,
mock_send_frame_results_queue,
mock_shutdown_event,
mock_tls_connection_options,
):
web_socket = Websocket(
mock_websocketio,
"websocket-id",
mock_tls_connection_options,
mock_on_connection_event,
mock_shutdown_event,
mock_send_frame_results_queue,
)
web_socket._websocket = mock_crt_websocket
return web_socket
def assert_websocket_closed(
mock_shutdown_event, mock_crt_websocket, mock_websocketio
):
assert mock_shutdown_event.set.called
assert mock_crt_websocket.close.called
assert mock_websocketio.close.called
class CaptureCRTWebsocket:
def __init__(self):
self.on_connection_setup_handler = None
self.on_connection_shutdown_handler = None
self.on_incoming_frame_payload_handler = None
self.on_incoming_frame_complete_handler = None
def connect(self, **kwargs):
self.on_connection_setup_handler = kwargs['on_connection_setup']
self.on_connection_shutdown_handler = kwargs['on_connection_shutdown']
self.on_incoming_frame_payload_handler = kwargs[
'on_incoming_frame_payload'
]
self.on_incoming_frame_complete_handler = kwargs[
'on_incoming_frame_complete'
]
@pytest.fixture
def capture_crt_websocket():
return CaptureCRTWebsocket()
@pytest.fixture
def websocket_url():
return 'wss://amazon.com/openTunnel?params'
class TestWebsocket:
@pytest.fixture(autouse=True)
def patch_crt_websocket(self, monkeypatch, capture_crt_websocket):
monkeypatch.setattr(
"awscrt.websocket.connect", capture_crt_websocket.connect
)
@patch.object(websocket, "connect")
@patch.object(websocket, "create_handshake_request")
def test_connect(
self,
mock_handshake,
mock_connect,
mock_handshake_request,
mock_on_connection_event,
mock_tls_connection_options,
web_socket,
websocket_url,
):
parsed_url = urlparse(websocket_url)
host = parsed_url.hostname
path = parsed_url.path + "?" + parsed_url.query
mock_handshake.return_value = mock_handshake_request
http_headers = HttpHeaders()
mock_handshake_request.headers = http_headers
web_socket.connect(websocket_url, 'defined-user-agent')
mock_handshake.assert_called_with(host=host, path=path)
mock_tls_connection_options.set_server_name.assert_called_with(host)
assert 'defined-user-agent' == http_headers.get('User-Agent')
mock_connect.assert_called_with(
host=host,
handshake_request=mock_handshake_request,
port=443,
tls_connection_options=mock_tls_connection_options,
proxy_options=None,
on_connection_setup=mock.ANY,
on_connection_shutdown=mock.ANY,
on_incoming_frame_payload=mock.ANY,
on_incoming_frame_complete=mock.ANY,
)
assert mock_on_connection_event.wait.called
@patch.object(websocket, "connect")
@patch.object(websocket, "create_handshake_request")
@mock.patch.dict(
os.environ,
{
"HTTPS_PROXY": "http://user1:pass1@localhost:8989",
"NO_PROXY": "different-domain",
},
)
def test_connect_with_proxy(
self,
mock_handshake,
mock_connect,
mock_on_connection_event,
mock_tls_connection_options,
web_socket,
websocket_url,
):
parsed_url = urlparse(websocket_url)
host = parsed_url.hostname
path = parsed_url.path + "?" + parsed_url.query
mock_handshake.return_value = mock_handshake_request
web_socket.connect(websocket_url)
mock_handshake.assert_called_with(host=host, path=path)
mock_tls_connection_options.set_server_name.assert_called_with(host)
mock_connect.assert_called_with(
host=host,
handshake_request=mock_handshake_request,
port=443,
tls_connection_options=mock_tls_connection_options,
proxy_options=mock.ANY,
on_connection_setup=mock.ANY,
on_connection_shutdown=mock.ANY,
on_incoming_frame_payload=mock.ANY,
on_incoming_frame_complete=mock.ANY,
)
assert (
"localhost"
== mock_connect.call_args.kwargs['proxy_options'].host_name
)
assert 8989 == mock_connect.call_args.kwargs['proxy_options'].port
assert (
HttpProxyAuthenticationType.Basic
== mock_connect.call_args.kwargs['proxy_options'].auth_type
)
assert (
"user1"
== mock_connect.call_args.kwargs['proxy_options'].auth_username
)
assert (
"pass1"
== mock_connect.call_args.kwargs['proxy_options'].auth_password
)
assert mock_on_connection_event.wait.called
@patch.object(websocket, "connect")
@patch.object(websocket, "create_handshake_request")
@mock.patch.dict(
os.environ, {"HTTPS_PROXY": "http://user1:pass1@localhost:8989"}
)
def test_connect_with_proxy_but_no_proxy_env_empty(
self,
mock_handshake,
mock_connect,
mock_on_connection_event,
mock_tls_connection_options,
web_socket,
websocket_url,
):
parsed_url = urlparse(websocket_url)
host = parsed_url.hostname
path = parsed_url.path + "?" + parsed_url.query
mock_handshake.return_value = mock_handshake_request
web_socket.connect(websocket_url)
mock_handshake.assert_called_with(host=host, path=path)
mock_tls_connection_options.set_server_name.assert_called_with(host)
mock_connect.assert_called_with(
host=host,
handshake_request=mock_handshake_request,
port=443,
tls_connection_options=mock_tls_connection_options,
proxy_options=mock.ANY,
on_connection_setup=mock.ANY,
on_connection_shutdown=mock.ANY,
on_incoming_frame_payload=mock.ANY,
on_incoming_frame_complete=mock.ANY,
)
assert (
"localhost"
== mock_connect.call_args.kwargs['proxy_options'].host_name
)
assert 8989 == mock_connect.call_args.kwargs['proxy_options'].port
assert (
HttpProxyAuthenticationType.Basic
== mock_connect.call_args.kwargs['proxy_options'].auth_type
)
assert (
"user1"
== mock_connect.call_args.kwargs['proxy_options'].auth_username
)
assert (
"pass1"
== mock_connect.call_args.kwargs['proxy_options'].auth_password
)
assert mock_on_connection_event.wait.called
@patch.object(websocket, "connect")
@patch.object(websocket, "create_handshake_request")
@mock.patch.dict(
os.environ,
{
"HTTPS_PROXY": "http://user1:pass1@localhost:8989",
"NO_PROXY": "1eice-123.eice.amazon.com",
},
)
def test_connect_with_proxy_define_and_no_proxy_defined(
self,
mock_handshake,
mock_connect,
mock_handshake_request,
mock_on_connection_event,
mock_tls_connection_options,
web_socket,
):
url = "wss://eice-123.eice.amazon.com/openTunnel?params"
parsed_url = urlparse(url)
host = parsed_url.hostname
path = parsed_url.path + "?" + parsed_url.query
mock_handshake.return_value = mock_handshake_request
web_socket.connect(url)
mock_handshake.assert_called_with(host=host, path=path)
mock_tls_connection_options.set_server_name.assert_called_with(host)
mock_connect.assert_called_with(
host=host,
handshake_request=mock_handshake_request,
port=443,
tls_connection_options=mock_tls_connection_options,
proxy_options=None,
on_connection_setup=mock.ANY,
on_connection_shutdown=mock.ANY,
on_incoming_frame_payload=mock.ANY,
on_incoming_frame_complete=mock.ANY,
)
assert mock_on_connection_event.wait.called
def test_write_data_from_input_when_there_is_data_to_read(
self,
mock_websocketio,
mock_crt_websocket,
mock_send_frame_results_queue,
mock_shutdown_event,
web_socket,
capture_crt_websocket,
):
data_as_bytes = b"Test"
mock_websocketio.read.return_value = data_as_bytes
mock_websocketio.has_data_to_read.return_value = True
mock_shutdown_event.is_set.side_effect = [False, True]
web_socket.write_data_from_input()
mock_websocketio.read.assert_called_with(65_000)
mock_crt_websocket.send_frame.assert_called_with(
opcode=Opcode.BINARY,
payload=data_as_bytes,
on_complete=mock.ANY,
)
assert mock_send_frame_results_queue.get.called
assert_websocket_closed(
mock_shutdown_event, mock_crt_websocket, mock_websocketio
)
@mock.patch("time.sleep")
def test_write_data_from_input_when_there_is_no_data_to_read(
self,
mock_sleep,
mock_websocketio,
mock_crt_websocket,
mock_shutdown_event,
web_socket,
):
mock_websocketio.has_data_to_read.return_value = False
mock_shutdown_event.is_set.side_effect = [False, True]
web_socket.write_data_from_input()
mock_sleep.assert_called_with(0.05)
assert_websocket_closed(
mock_shutdown_event, mock_crt_websocket, mock_websocketio
)
def test_write_data_from_input_shuts_down_when_sends_frame_throws_close_frame_sent(
self,
mock_websocketio,
mock_crt_websocket,
mock_shutdown_event,
web_socket,
):
mock_crt_websocket.send_frame.side_effect = RuntimeError(
"AWS_ERROR_HTTP_WEBSOCKET_CLOSE_FRAME_SENT"
)
data_as_bytes = b"Test"
mock_websocketio.read.return_value = data_as_bytes
mock_websocketio.has_data_to_read.return_value = True
mock_shutdown_event.is_set.side_effect = [False, True]
web_socket.write_data_from_input()
assert_websocket_closed(
mock_shutdown_event, mock_crt_websocket, mock_websocketio
)
def test_write_data_northbound_shuts_down_when_sends_frame_throws_http_connection_closed(
self,
mock_websocketio,
mock_crt_websocket,
mock_shutdown_event,
web_socket,
):
mock_crt_websocket.send_frame.side_effect = RuntimeError(
"AWS_ERROR_HTTP_CONNECTION_CLOSED"
)
data_as_bytes = b"Test"
mock_websocketio.read.return_value = data_as_bytes
mock_websocketio.has_data_to_read.return_value = True
mock_shutdown_event.is_set.side_effect = [False, True]
web_socket.write_data_from_input()
assert_websocket_closed(
mock_shutdown_event, mock_crt_websocket, mock_websocketio
)
def test_on_connection_sets_websocket(
self,
capture_crt_websocket,
websocket_url,
mock_on_connection_event,
web_socket,
):
data = OnConnectionSetupData()
data.websocket = mock.Mock(spec=Websocket)
request_id = "request_id"
data.handshake_response_headers = [("X-Amzn-RequestId", request_id)]
web_socket.connect(websocket_url)
capture_crt_websocket.on_connection_setup_handler(data)
assert data.websocket == web_socket._websocket
assert mock_on_connection_event.set.called
def test_on_connection_with_exception(
self,
mock_on_connection_event,
web_socket,
websocket_url,
capture_crt_websocket,
):
data = OnConnectionSetupData()
request_id = "request_id"
data.handshake_response_headers = [("X-Amzn-RequestId", request_id)]
data.exception = Exception()
data.handshake_response_status = 401
data.handshake_response_body = b"Failed to connect"
web_socket.connect(websocket_url)
capture_crt_websocket.on_connection_setup_handler(data)
assert mock_on_connection_event.set.called
def test_on_incoming_frame_payload_data_with_binary_frame(
self,
mock_websocketio,
web_socket,
websocket_url,
capture_crt_websocket,
):
data_in_bytes = b"Test"
frame = IncomingFrame(
opcode=Opcode.BINARY, payload_length=len(data_in_bytes), fin=True
)
web_socket.connect(websocket_url)
capture_crt_websocket.on_incoming_frame_payload_handler(
OnIncomingFramePayloadData(data=data_in_bytes, frame=frame)
)
mock_websocketio.write.assert_called_with(data_in_bytes)
def test_on_incoming_frame_payload_data_with_close_frame(
self, web_socket, websocket_url, capture_crt_websocket
):
data_in_bytes = b"Test"
frame = IncomingFrame(
opcode=Opcode.CLOSE, payload_length=len(data_in_bytes), fin=True
)
web_socket.connect(websocket_url)
capture_crt_websocket.on_incoming_frame_payload_handler(
OnIncomingFramePayloadData(data=data_in_bytes, frame=frame)
)
assert bytearray(data_in_bytes) == web_socket._close_frame_bytes
def test_on_incoming_frame_payload_data_with_text_frame_sets_shutdown_event(
self,
mock_websocketio,
mock_crt_websocket,
mock_shutdown_event,
web_socket,
websocket_url,
capture_crt_websocket,
):
data_in_bytes = b"Test"
frame = IncomingFrame(
opcode=Opcode.TEXT, payload_length=len(data_in_bytes), fin=True
)
web_socket.connect(websocket_url)
capture_crt_websocket.on_incoming_frame_payload_handler(
OnIncomingFramePayloadData(data=data_in_bytes, frame=frame)
)
assert isinstance(web_socket.exception, WebsocketException)
assert_websocket_closed(
mock_shutdown_event, mock_crt_websocket, mock_websocketio
)
def test_on_incoming_frame_complete_with_close_frame(
self,
mock_websocketio,
mock_crt_websocket,
mock_shutdown_event,
web_socket,
websocket_url,
capture_crt_websocket,
):
shutdown_reason = b"Test"
data_in_bytes = struct.pack(">H", 1) + shutdown_reason
frame = IncomingFrame(
opcode=Opcode.CLOSE, payload_length=len(shutdown_reason), fin=True
)
web_socket.connect(websocket_url)
capture_crt_websocket.on_incoming_frame_payload_handler(
OnIncomingFramePayloadData(
frame=frame,
data=data_in_bytes,
)
)
capture_crt_websocket.on_incoming_frame_complete_handler(
OnIncomingFrameCompleteData(frame=frame, exception=None)
)
expected_exc = WebsocketException(
f"Websocket Closure Reason: {shutdown_reason.decode()}"
)
assert isinstance(web_socket.exception, WebsocketException)
# Ensure shutdown reason is decoded properly. (validate properties on WebsocketException)
assert web_socket.exception.args == expected_exc.args
assert_websocket_closed(
mock_shutdown_event, mock_crt_websocket, mock_websocketio
)
def test_on_incoming_frame_complete_with_close_frame_ignores_shutdown_code_1000(
self,
mock_websocketio,
mock_crt_websocket,
mock_shutdown_event,
web_socket,
websocket_url,
capture_crt_websocket,
):
frame = IncomingFrame(opcode=Opcode.CLOSE, payload_length=0, fin=True)
incoming_frame_data = OnIncomingFrameCompleteData(frame=frame)
web_socket.connect(websocket_url)
capture_crt_websocket.on_incoming_frame_complete_handler(
incoming_frame_data
)
assert not web_socket.exception
assert_websocket_closed(
mock_shutdown_event, mock_crt_websocket, mock_websocketio
)
def test_on_connection_shutdown(
self,
mock_websocketio,
mock_crt_websocket,
mock_shutdown_event,
web_socket,
websocket_url,
capture_crt_websocket,
):
data = OnConnectionShutdownData()
web_socket.connect(websocket_url)
capture_crt_websocket.on_connection_shutdown_handler(data)
assert_websocket_closed(
mock_shutdown_event, mock_crt_websocket, mock_websocketio
)
|