mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-01-18 12:52:44 +01:00
tests/shell_lock: add test
This commit is contained in:
parent
ccc795133f
commit
2f91557098
32
tests/shell_lock/Makefile
Normal file
32
tests/shell_lock/Makefile
Normal file
@ -0,0 +1,32 @@
|
||||
DEVELHELP=0
|
||||
include ../Makefile.tests_common
|
||||
|
||||
USEMODULE += shell
|
||||
USEMODULE += shell_commands
|
||||
|
||||
USEMODULE += shell_lock
|
||||
USEMODULE += shell_lock_auto_locking
|
||||
|
||||
CFLAGS += -DCONFIG_SHELL_LOCK_PASSWORD=\"password\"
|
||||
CFLAGS += -DCONFIG_SHELL_LOCK_AUTO_LOCK_TIMEOUT_MS=7000
|
||||
|
||||
# This config defaults to 1 on native, such that pm_off() would be called as soon as
|
||||
# shell_run_once is terminated in shell_run_forever. We do not want this behavior for this test.
|
||||
CFLAGS += -DCONFIG_SHELL_SHUTDOWN_ON_EXIT=0
|
||||
|
||||
# test_utils_interactive_sync_shell assumes that the prompt is always '> ' which breaks
|
||||
# with the password prompt of the shell_lock module which is different from the shell's prompt
|
||||
DISABLE_MODULE += test_utils_interactive_sync_shell
|
||||
|
||||
# for z1, socat doesn't work (unknown reason)
|
||||
ifeq (z1, $(BOARD))
|
||||
RIOT_TERMINAL ?= pyterm
|
||||
endif
|
||||
|
||||
# Use a terminal that does not introduce extra characters into the stream.
|
||||
RIOT_TERMINAL ?= socat
|
||||
|
||||
include $(RIOTBASE)/Makefile.include
|
||||
|
||||
# the test script skips tests if socat is not used
|
||||
$(call target-export-variables,$(RIOT_TERMINAL),RIOT_TERMINAL)
|
8
tests/shell_lock/Makefile.ci
Normal file
8
tests/shell_lock/Makefile.ci
Normal file
@ -0,0 +1,8 @@
|
||||
BOARD_INSUFFICIENT_MEMORY := \
|
||||
arduino-duemilanove \
|
||||
arduino-leonardo \
|
||||
arduino-nano \
|
||||
arduino-uno \
|
||||
atmega328p \
|
||||
nucleo-l011k4 \
|
||||
#
|
36
tests/shell_lock/main.c
Normal file
36
tests/shell_lock/main.c
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Freie Universität Berlin
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Test for the lock behaviour of the shell
|
||||
*
|
||||
* @author Hendrik van Essen <hendrik.ve@fu-berlin.de>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "shell.h"
|
||||
|
||||
#include "test_utils/interactive_sync.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_utils_interactive_sync();
|
||||
|
||||
puts("test_shell_lock");
|
||||
|
||||
/* define buffer to be used by the shell */
|
||||
char line_buf[SHELL_DEFAULT_BUFSIZE];
|
||||
|
||||
/* start shell */
|
||||
shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
|
||||
|
||||
return 0;
|
||||
}
|
99
tests/shell_lock/tests/01-run.py
Executable file
99
tests/shell_lock/tests/01-run.py
Executable file
@ -0,0 +1,99 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# Copyright (C) 2021 Freie Universität Berlin
|
||||
#
|
||||
# This file is subject to the terms and conditions of the GNU Lesser
|
||||
# General Public License v2.1. See the file LICENSE in the top level
|
||||
# directory for more details.
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
from testrunner import run
|
||||
|
||||
PASSWORD_CORRECT = "password"
|
||||
PASSWORDS_INCORRECT = [
|
||||
"pass",
|
||||
"word",
|
||||
"asswor",
|
||||
"passw0rd",
|
||||
"password_",
|
||||
"_password"
|
||||
]
|
||||
|
||||
EXPECTED_HELP = (
|
||||
'Command Description',
|
||||
'---------------------------------------',
|
||||
'lock Lock the shell',
|
||||
'pm interact with layered PM subsystem',
|
||||
'reboot Reboot the node',
|
||||
'version Prints current RIOT_VERSION',
|
||||
)
|
||||
|
||||
AUTO_LOCK_TIMEOUT_MS = 7000
|
||||
SHELL_PROMPT = '> '
|
||||
PASSWORD_PROMPT = 'Password: '
|
||||
BOARD = os.environ['BOARD']
|
||||
|
||||
|
||||
def testfunc(child):
|
||||
|
||||
# avoid sending an extra empty line on native.
|
||||
if BOARD == 'native':
|
||||
child.crlf = '\n'
|
||||
|
||||
# unlock
|
||||
child.sendline(PASSWORD_CORRECT)
|
||||
child.expect_exact('Shell was unlocked.')
|
||||
child.expect_exact(SHELL_PROMPT)
|
||||
|
||||
# check we have access
|
||||
child.sendline('help')
|
||||
for line in EXPECTED_HELP:
|
||||
child.expect_exact(line)
|
||||
|
||||
# lock
|
||||
child.sendline('lock')
|
||||
child.expect(SHELL_PROMPT)
|
||||
|
||||
# trigger password prompt
|
||||
child.sendline('help')
|
||||
child.expect('The shell is locked. Enter a valid password to unlock.')
|
||||
|
||||
# test different incorrect passwords
|
||||
for i, pwd in enumerate(PASSWORDS_INCORRECT):
|
||||
|
||||
# every third incorrect attempt leads to 7 second of sleep, otherwise
|
||||
# just 1 second
|
||||
if i > 0 and i % 3 == 0:
|
||||
timeout = 7
|
||||
else:
|
||||
timeout = 1
|
||||
|
||||
# some boards react quite slow, give them 2 extra seconds
|
||||
child.expect_exact(PASSWORD_PROMPT, timeout=(timeout + 2))
|
||||
child.sendline(pwd)
|
||||
child.expect_exact('Wrong password')
|
||||
|
||||
# unlock
|
||||
child.sendline(PASSWORD_CORRECT)
|
||||
child.expect_exact('Shell was unlocked.')
|
||||
child.expect_exact(SHELL_PROMPT)
|
||||
|
||||
# check we have access
|
||||
child.sendline('help')
|
||||
for line in EXPECTED_HELP:
|
||||
child.expect_exact(line)
|
||||
|
||||
# wait until auto_lock locks the shell after
|
||||
# CONFIG_SHELL_LOCK_AUTO_LOCK_TIMEOUT_MS (+ 1 second buffer time)
|
||||
time.sleep((AUTO_LOCK_TIMEOUT_MS / 1000.0) + 1)
|
||||
|
||||
# trigger password prompt
|
||||
child.sendline('help')
|
||||
child.expect('The shell is locked. Enter a valid password to unlock.')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(run(testfunc))
|
Loading…
Reference in New Issue
Block a user