|
| 1 | +""" |
| 2 | +Copyright 2025 Google LLC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://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, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | +import asyncio |
| 18 | +import socket |
| 19 | +import ssl |
| 20 | +from typing import Any |
| 21 | + |
| 22 | +from mock import Mock |
| 23 | +import pytest |
| 24 | + |
| 25 | +from google.cloud.sql.connector import proxy |
| 26 | + |
| 27 | +LOCAL_PROXY_MAX_MESSAGE_SIZE = 10485760 |
| 28 | + |
| 29 | +@pytest.mark.usefixtures("proxy_server") |
| 30 | +@pytest.mark.asyncio |
| 31 | +async def test_proxy_creates_folder(context: ssl.SSLContext, kwargs: Any) -> None: |
| 32 | + """Test to verify that the proxy server is getting back the task.""" |
| 33 | + ip_addr = "127.0.0.1" |
| 34 | + path = "/tmp/connector-socket/socket" |
| 35 | + sock = context.wrap_socket( |
| 36 | + socket.create_connection((ip_addr, 3307)), |
| 37 | + server_hostname=ip_addr, |
| 38 | + ) |
| 39 | + loop = asyncio.get_running_loop() |
| 40 | + |
| 41 | + task = proxy.start_local_proxy(sock, path, loop) |
| 42 | + assert (task is not None) |
| 43 | + |
| 44 | + proxy_task = asyncio.gather(task) |
| 45 | + try: |
| 46 | + await asyncio.wait_for(proxy_task, timeout=0.1) |
| 47 | + except TimeoutError: |
| 48 | + pass # This task runs forever so it is expected to throw this exception |
| 49 | + |
| 50 | +@pytest.mark.usefixtures("proxy_server") |
| 51 | +@pytest.mark.asyncio |
| 52 | +async def test_local_proxy_communication(context: ssl.SSLContext, kwargs: Any) -> None: |
| 53 | + """Test to verify that the communication is getting through.""" |
| 54 | + socket_path = "/tmp/connector-socket/socket" |
| 55 | + ssl_sock = Mock(spec=ssl.SSLSocket) |
| 56 | + loop = asyncio.get_running_loop() |
| 57 | + |
| 58 | + with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client: |
| 59 | + ssl_sock.recv.return_value = b"Received" |
| 60 | + |
| 61 | + task = proxy.start_local_proxy(ssl_sock, socket_path, loop) |
| 62 | + |
| 63 | + client.connect(socket_path) |
| 64 | + client.sendall(b"Test") |
| 65 | + await asyncio.sleep(1) |
| 66 | + |
| 67 | + ssl_sock.sendall.assert_called_with(b"Test") |
| 68 | + response = client.recv(LOCAL_PROXY_MAX_MESSAGE_SIZE) |
| 69 | + assert (response == b"Received") |
| 70 | + |
| 71 | + client.close() |
| 72 | + await asyncio.sleep(1) |
| 73 | + |
| 74 | + proxy_task = asyncio.gather(task) |
| 75 | + await asyncio.wait_for(proxy_task, timeout=2) |
0 commit comments