Skip to content

Instantly share code, notes, and snippets.

@pokgak
Last active October 18, 2019 12:36
Show Gist options
  • Save pokgak/618a0904f95eacd1647decef9c534634 to your computer and use it in GitHub Desktop.
Save pokgak/618a0904f95eacd1647decef9c534634 to your computer and use it in GitHub Desktop.
Python pexpect script to test handling handshake_failure alert between two native RIOT instances
#!/usr/bin/env python3
import sys
import os
import pexpect
import time
import signal
serverIP = "<SERVER_IP>"
# server setup
# pexpect.run('make clean')
serverEnv = {
# "CFLAGS": "-DDTLS_PSK -DTINYDTLS_DEBUG", # use this line to test handshake failure
"CFLAGS": "-DTINYDTLS_DEBUG", # use this line to test normal send should be succesful
"PORT": "tap0",
}
serverEnv.update(os.environ)
server_logfile = open("server.log", "w")
server = pexpect.spawn('make all term',
env=serverEnv,
timeout=5, encoding='utf-8',
logfile=server_logfile,
echo=False)
server.sendline("help")
server.expect("Command .* Description", timeout=None)
# start dtls server
server.sendline("dtlss start")
time.sleep(1)
# client setup
# pexpect.run('make clean')
clientEnv = {
# "CFLAGS": "-DDTLS_ECC -DTINYDTLS_DEBUG", # use this line to test handshake failure
"CFLAGS": "-DTINYDTLS_DEBUG", # use this line to test normal send should be succesful
"PORT": "tap1",
}
clientEnv.update(os.environ)
client_logfile = open("client.log", "w")
client = pexpect.spawn('make all term',
env=clientEnv,
timeout=5, encoding='utf-8',
logfile=client_logfile,
echo=False)
client.sendline("help")
client.expect("Command .* Description", timeout=None)
# send dtls packet from client
client.sendline("ifconfig")
time.sleep(1)
client.sendline("dtlsc " + serverIP + " TEST")
res = server.expect(["Server: got DTLS Data App", "WARN error while handling handshake packet"])
if res != 0:
print("server: sent handshake_failure alert")
else:
print("server: received application data")
res = client.expect(["Client: got DTLS Data App", "Alert: level 2, description 40"])
if res != 0:
print("client: received handshake_failure alert")
else:
print("client: received application data")
# make sure client does not hangs
client.sendline("help")
client.expect("Command .* Description")
try:
os.killpg(os.getpgid(server.pid), signal.SIGKILL)
os.killpg(os.getpgid(client.pid), signal.SIGKILL)
except ProcessLookupError:
print("Process already stopped")
server.close()
client.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment