2020-03-09 15:44:29 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2022-05-10 06:17:12 +00:00
|
|
|
# SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
2022-01-02 11:27:05 +00:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2020-03-09 15:44:29 +00:00
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
from __future__ import division, print_function, unicode_literals
|
|
|
|
|
2022-05-10 06:17:12 +00:00
|
|
|
import logging
|
2021-01-26 02:49:01 +00:00
|
|
|
import os
|
|
|
|
|
2022-05-10 06:17:12 +00:00
|
|
|
import pytest
|
2022-07-06 16:34:06 +00:00
|
|
|
from common_test_methods import get_env_config_variable
|
2022-05-10 06:17:12 +00:00
|
|
|
from pytest_embedded import Dut
|
2020-03-09 15:44:29 +00:00
|
|
|
|
2022-01-02 11:27:05 +00:00
|
|
|
try:
|
|
|
|
import websocket
|
|
|
|
except ImportError:
|
|
|
|
print("Please install 'websocket' by running 'python -m pip install websocket-client'")
|
|
|
|
raise
|
|
|
|
|
2020-03-09 15:44:29 +00:00
|
|
|
OPCODE_TEXT = 0x1
|
|
|
|
OPCODE_BIN = 0x2
|
|
|
|
OPCODE_PING = 0x9
|
|
|
|
OPCODE_PONG = 0xa
|
|
|
|
|
|
|
|
|
|
|
|
class WsClient:
|
2022-05-10 06:17:12 +00:00
|
|
|
def __init__(self, ip: str, port: int) -> None:
|
2020-03-09 15:44:29 +00:00
|
|
|
self.port = port
|
|
|
|
self.ip = ip
|
2020-09-07 14:29:50 +00:00
|
|
|
self.ws = websocket.WebSocket()
|
2020-03-09 15:44:29 +00:00
|
|
|
|
2022-05-10 06:17:12 +00:00
|
|
|
def __enter__(self): # type: ignore
|
2021-01-26 02:49:01 +00:00
|
|
|
self.ws.connect('ws://{}:{}/ws'.format(self.ip, self.port))
|
2020-03-09 15:44:29 +00:00
|
|
|
return self
|
|
|
|
|
2022-05-10 06:17:12 +00:00
|
|
|
def __exit__(self, exc_type, exc_value, traceback): # type: ignore
|
2020-09-07 14:29:50 +00:00
|
|
|
self.ws.close()
|
2020-03-09 15:44:29 +00:00
|
|
|
|
2022-05-10 06:17:12 +00:00
|
|
|
def read(self): # type: ignore
|
2020-09-07 14:29:50 +00:00
|
|
|
return self.ws.recv_data(control_frame=True)
|
2020-03-09 15:44:29 +00:00
|
|
|
|
2022-05-10 06:17:12 +00:00
|
|
|
def write(self, data='', opcode=OPCODE_TEXT): # type: ignore
|
2020-09-07 14:29:50 +00:00
|
|
|
if opcode == OPCODE_BIN:
|
|
|
|
return self.ws.send_binary(data.encode())
|
|
|
|
if opcode == OPCODE_PING:
|
|
|
|
return self.ws.ping(data)
|
|
|
|
return self.ws.send(data)
|
2020-03-09 15:44:29 +00:00
|
|
|
|
|
|
|
|
2022-05-10 06:17:12 +00:00
|
|
|
@pytest.mark.esp32
|
2022-07-03 08:22:20 +00:00
|
|
|
@pytest.mark.wifi_router
|
2022-05-10 06:17:12 +00:00
|
|
|
def test_examples_protocol_http_ws_echo_server(dut: Dut) -> None:
|
2020-03-09 15:44:29 +00:00
|
|
|
# Get binary file
|
2022-05-10 06:17:12 +00:00
|
|
|
binary_file = os.path.join(dut.app.binary_path, 'ws_echo_server.bin')
|
2020-03-09 15:44:29 +00:00
|
|
|
bin_size = os.path.getsize(binary_file)
|
2022-05-10 06:17:12 +00:00
|
|
|
logging.info('http_ws_server_bin_size : {}KB'.format(bin_size // 1024))
|
2020-03-09 15:44:29 +00:00
|
|
|
|
2022-05-10 06:17:12 +00:00
|
|
|
logging.info('Starting ws-echo-server test app based on http_server')
|
2020-03-09 15:44:29 +00:00
|
|
|
|
|
|
|
# Parse IP address of STA
|
2022-05-10 06:17:12 +00:00
|
|
|
logging.info('Waiting to connect with AP')
|
2022-07-04 08:54:33 +00:00
|
|
|
if dut.app.sdkconfig.get('EXAMPLE_WIFI_SSID_PWD_FROM_STDIN') is True:
|
|
|
|
dut.expect('Please input ssid password:')
|
2022-07-06 16:34:06 +00:00
|
|
|
env_name = 'wifi_router'
|
|
|
|
ap_ssid = get_env_config_variable(env_name, 'ap_ssid')
|
|
|
|
ap_password = get_env_config_variable(env_name, 'ap_password')
|
|
|
|
dut.write(f'{ap_ssid} {ap_password}')
|
2022-07-14 14:55:50 +00:00
|
|
|
got_ip = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[1].decode()
|
2022-05-10 06:17:12 +00:00
|
|
|
got_port = dut.expect(r"Starting server on port: '(\d+)'", timeout=30)[1].decode()
|
2020-03-09 15:44:29 +00:00
|
|
|
|
2022-05-10 06:17:12 +00:00
|
|
|
logging.info('Got IP : {}'.format(got_ip))
|
|
|
|
logging.info('Got Port : {}'.format(got_port))
|
2020-03-09 15:44:29 +00:00
|
|
|
|
|
|
|
# Start ws server test
|
2020-03-11 09:03:12 +00:00
|
|
|
with WsClient(got_ip, int(got_port)) as ws:
|
2020-03-09 15:44:29 +00:00
|
|
|
DATA = 'Espressif'
|
|
|
|
for expected_opcode in [OPCODE_TEXT, OPCODE_BIN, OPCODE_PING]:
|
|
|
|
ws.write(data=DATA, opcode=expected_opcode)
|
|
|
|
opcode, data = ws.read()
|
2022-05-10 06:17:12 +00:00
|
|
|
logging.info('Testing opcode {}: Received opcode:{}, data:{}'.format(expected_opcode, opcode, data))
|
2020-11-11 03:31:17 +00:00
|
|
|
data = data.decode()
|
2020-03-09 15:44:29 +00:00
|
|
|
if expected_opcode == OPCODE_PING:
|
2022-05-10 06:17:12 +00:00
|
|
|
dut.expect('Got a WS PING frame, Replying PONG')
|
2020-03-09 15:44:29 +00:00
|
|
|
if opcode != OPCODE_PONG or data != DATA:
|
2021-01-26 02:49:01 +00:00
|
|
|
raise RuntimeError('Failed to receive correct opcode:{} or data:{}'.format(opcode, data))
|
2020-03-09 15:44:29 +00:00
|
|
|
continue
|
2022-05-10 06:17:12 +00:00
|
|
|
dut_data = dut.expect(r'Got packet with message: ([A-Za-z0-9_]*)')[1]
|
|
|
|
dut_opcode = dut.expect(r'Packet type: ([0-9]*)')[1].decode()
|
|
|
|
|
|
|
|
if opcode != expected_opcode or data != DATA or opcode != int(dut_opcode) or (data not in str(dut_data)):
|
2021-01-26 02:49:01 +00:00
|
|
|
raise RuntimeError('Failed to receive correct opcode:{} or data:{}'.format(opcode, data))
|
|
|
|
ws.write(data='Trigger async', opcode=OPCODE_TEXT)
|
2020-03-11 09:03:12 +00:00
|
|
|
opcode, data = ws.read()
|
2022-05-10 06:17:12 +00:00
|
|
|
logging.info('Testing async send: Received opcode:{}, data:{}'.format(opcode, data))
|
2020-11-11 03:31:17 +00:00
|
|
|
data = data.decode()
|
2021-01-26 02:49:01 +00:00
|
|
|
if opcode != OPCODE_TEXT or data != 'Async data':
|
|
|
|
raise RuntimeError('Failed to receive correct opcode:{} or data:{}'.format(opcode, data))
|