2021-01-25 21:49:01 -05:00
|
|
|
from __future__ import print_function, unicode_literals
|
|
|
|
|
2020-02-25 15:48:06 -05:00
|
|
|
import re
|
|
|
|
import socket
|
|
|
|
import subprocess
|
|
|
|
import time
|
2021-01-25 21:49:01 -05:00
|
|
|
from threading import Event, Thread
|
|
|
|
|
2020-04-16 01:50:32 -04:00
|
|
|
import netifaces
|
2021-01-25 21:49:01 -05:00
|
|
|
import ttfw_idf
|
2020-02-25 15:48:06 -05:00
|
|
|
|
|
|
|
|
|
|
|
def run_server(server_stop, port, server_ip, client_ip):
|
2021-01-25 21:49:01 -05:00
|
|
|
print('Starting PPP server on port: {}'.format(port))
|
2020-02-25 15:48:06 -05:00
|
|
|
try:
|
|
|
|
arg_list = ['pppd', port, '115200', '{}:{}'.format(server_ip, client_ip), 'modem', 'local', 'noauth', 'debug', 'nocrtscts', 'nodetach', '+ipv6']
|
|
|
|
p = subprocess.Popen(arg_list, stdout=subprocess.PIPE, bufsize=1)
|
|
|
|
while not server_stop.is_set():
|
|
|
|
if p.poll() is not None:
|
|
|
|
raise ValueError('ENV_TEST_FAILURE: PPP terminated unexpectedly with {}'.format(p.poll()))
|
|
|
|
line = p.stdout.readline()
|
|
|
|
if line:
|
2021-01-25 21:49:01 -05:00
|
|
|
print('[PPPD:]{}'.format(line.rstrip()))
|
2020-02-25 15:48:06 -05:00
|
|
|
time.sleep(0.1)
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
raise ValueError('ENV_TEST_FAILURE: Error running PPP server')
|
|
|
|
finally:
|
|
|
|
p.terminate()
|
2021-01-25 21:49:01 -05:00
|
|
|
print('PPP server stopped')
|
2020-02-25 15:48:06 -05:00
|
|
|
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
@ttfw_idf.idf_custom_test(env_tag='Example_PPP', group='test-apps')
|
2020-02-25 15:48:06 -05:00
|
|
|
def test_examples_protocol_pppos_connect(env, extra_data):
|
|
|
|
"""
|
|
|
|
steps:
|
|
|
|
1. starts PPP server
|
|
|
|
2. get DUT as PPP client to connect to the server
|
|
|
|
3. check TCP client-server connection between client-server
|
|
|
|
"""
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1 = env.get_dut('pppos_connect_test', 'tools/test_apps/protocols/pppos', dut_class=ttfw_idf.ESP32DUT)
|
2020-02-25 15:48:06 -05:00
|
|
|
# Look for test case symbolic names
|
|
|
|
try:
|
2021-01-25 21:49:01 -05:00
|
|
|
server_ip = dut1.app.get_sdkconfig()['CONFIG_TEST_APP_PPP_SERVER_IP'].replace('"','')
|
|
|
|
client_ip = dut1.app.get_sdkconfig()['CONFIG_TEST_APP_PPP_CLIENT_IP'].replace('"','')
|
|
|
|
port_nr = dut1.app.get_sdkconfig()['CONFIG_TEST_APP_TCP_PORT']
|
2020-02-25 15:48:06 -05:00
|
|
|
except Exception:
|
|
|
|
print('ENV_TEST_FAILURE: Some mandatory configuration not found in sdkconfig')
|
|
|
|
raise
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
print('Starting the test on {}'.format(dut1))
|
2020-02-25 15:48:06 -05:00
|
|
|
dut1.start_app()
|
|
|
|
|
|
|
|
# the PPP test env uses two ttyUSB's: one for ESP32 board, another one for ppp server
|
|
|
|
# use the other port for PPP server than the DUT/ESP
|
|
|
|
port = '/dev/ttyUSB0' if dut1.port == '/dev/ttyUSB1' else '/dev/ttyUSB1'
|
|
|
|
# Start the PPP server
|
|
|
|
server_stop = Event()
|
|
|
|
t = Thread(target=run_server, args=(server_stop, port, server_ip, client_ip))
|
|
|
|
t.start()
|
|
|
|
try:
|
2020-04-16 01:50:32 -04:00
|
|
|
ppp_server_timeout = time.time() + 30
|
2021-01-25 21:49:01 -05:00
|
|
|
while 'ppp0' not in netifaces.interfaces():
|
2020-04-16 01:50:32 -04:00
|
|
|
print("PPP server haven't yet setup its netif, list of active netifs:{}".format(netifaces.interfaces()))
|
|
|
|
time.sleep(0.5)
|
|
|
|
if time.time() > ppp_server_timeout:
|
2021-01-25 21:49:01 -05:00
|
|
|
raise ValueError('ENV_TEST_FAILURE: PPP server failed to setup ppp0 interface within timeout')
|
|
|
|
ip6_addr = dut1.expect(re.compile(r'Got IPv6 address (\w{4}\:\w{4}\:\w{4}\:\w{4}\:\w{4}\:\w{4}\:\w{4}\:\w{4})'), timeout=30)[0]
|
|
|
|
print('IPv6 address of ESP: {}'.format(ip6_addr))
|
2020-02-25 15:48:06 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1.expect(re.compile(r'Socket listening'))
|
|
|
|
print('Starting the IPv6 test...')
|
2020-02-25 15:48:06 -05:00
|
|
|
# Connect to TCP server on ESP using IPv6 address
|
2021-01-25 21:49:01 -05:00
|
|
|
for res in socket.getaddrinfo(ip6_addr + '%ppp0', int(port_nr), socket.AF_INET6,
|
2020-02-25 15:48:06 -05:00
|
|
|
socket.SOCK_STREAM, socket.SOL_TCP):
|
|
|
|
af, socktype, proto, canonname, addr = res
|
|
|
|
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
|
|
|
|
sock.connect(addr)
|
2021-01-25 21:49:01 -05:00
|
|
|
sock.sendall(b'Espressif')
|
2020-02-25 15:48:06 -05:00
|
|
|
sock.close()
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1.expect(re.compile(r'IPv6 test passed'))
|
|
|
|
print('IPv6 test passed!')
|
2020-02-25 15:48:06 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
print('Starting the IPv4 test...')
|
2020-02-25 15:48:06 -05:00
|
|
|
# Start the TCP server and wait for the ESP to connect with IPv4 address
|
|
|
|
try:
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
sock.bind(('', int(port_nr)))
|
|
|
|
sock.listen(1)
|
|
|
|
conn, addr = sock.accept()
|
|
|
|
except socket.error as msg:
|
|
|
|
print('Socket error: ' + str(msg[0]) + ': ' + msg[1])
|
|
|
|
raise
|
|
|
|
timeout = time.time() + 60
|
|
|
|
while time.time() < timeout:
|
|
|
|
data = conn.recv(128)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
data = data.decode()
|
|
|
|
print('Received data: ' + data)
|
|
|
|
if data.startswith('Espressif'):
|
|
|
|
conn.send(data.encode())
|
|
|
|
break
|
|
|
|
conn.close()
|
2021-01-25 21:49:01 -05:00
|
|
|
dut1.expect(re.compile(r'IPv4 test passed'))
|
|
|
|
print('IPv4 test passed!')
|
2020-02-25 15:48:06 -05:00
|
|
|
finally:
|
|
|
|
server_stop.set()
|
|
|
|
t.join()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_examples_protocol_pppos_connect()
|