1*3f6f8362SLouise Kilheeney#!/usr/bin/env python3 20c36081dSBruce Richardson# SPDX-License-Identifier: BSD-3-Clause 30c36081dSBruce Richardson# Copyright(c) 2010-2014 Intel Corporation 40c36081dSBruce Richardson 50c36081dSBruce Richardson# Script that runs cmdline_test app and feeds keystrokes into it. 60c36081dSBruce Richardsonimport cmdline_test_data 70c36081dSBruce Richardsonimport os 80c36081dSBruce Richardsonimport pexpect 90c36081dSBruce Richardsonimport sys 100c36081dSBruce Richardson 110c36081dSBruce Richardson 120c36081dSBruce Richardson# 130c36081dSBruce Richardson# function to run test 140c36081dSBruce Richardson# 150c36081dSBruce Richardsondef runTest(child, test): 160c36081dSBruce Richardson child.send(test["Sequence"]) 170c36081dSBruce Richardson if test["Result"] is None: 180c36081dSBruce Richardson return 0 190c36081dSBruce Richardson child.expect(test["Result"], 1) 200c36081dSBruce Richardson 210c36081dSBruce Richardson# 220c36081dSBruce Richardson# history test is a special case 230c36081dSBruce Richardson# 240c36081dSBruce Richardson# This test does the following: 250c36081dSBruce Richardson# 1) fills the history with garbage up to its full capacity 260c36081dSBruce Richardson# (just enough to remove last entry) 270c36081dSBruce Richardson# 2) scrolls back history to the very beginning 280c36081dSBruce Richardson# 3) checks if the output is as expected, that is, the first 290c36081dSBruce Richardson# number in the sequence (not the last entry before it) 300c36081dSBruce Richardson# 310c36081dSBruce Richardson# This is a self-contained test, it needs only a pexpect child 320c36081dSBruce Richardson# 330c36081dSBruce Richardsondef runHistoryTest(child): 340c36081dSBruce Richardson # find out history size 350c36081dSBruce Richardson child.sendline(cmdline_test_data.CMD_GET_BUFSIZE) 360c36081dSBruce Richardson child.expect("History buffer size: \\d+", timeout=1) 370c36081dSBruce Richardson history_size = int(child.after[len(cmdline_test_data.BUFSIZE_TEMPLATE):]) 380c36081dSBruce Richardson i = 0 390c36081dSBruce Richardson 400c36081dSBruce Richardson # fill the history with numbers 41*3f6f8362SLouise Kilheeney while i < history_size // 10: 420c36081dSBruce Richardson # add 1 to prevent from parsing as octals 430c36081dSBruce Richardson child.send("1" + str(i).zfill(8) + cmdline_test_data.ENTER) 440c36081dSBruce Richardson # the app will simply print out the number 450c36081dSBruce Richardson child.expect(str(i + 100000000), timeout=1) 460c36081dSBruce Richardson i += 1 470c36081dSBruce Richardson # scroll back history 480c36081dSBruce Richardson child.send(cmdline_test_data.UP * (i + 2) + cmdline_test_data.ENTER) 490c36081dSBruce Richardson child.expect("100000000", timeout=1) 500c36081dSBruce Richardson 510c36081dSBruce Richardson# the path to cmdline_test executable is supplied via command-line. 520c36081dSBruce Richardsonif len(sys.argv) < 2: 530c36081dSBruce Richardson print("Error: please supply cmdline_test app path") 540c36081dSBruce Richardson sys.exit(1) 550c36081dSBruce Richardson 560c36081dSBruce Richardsontest_app_path = sys.argv[1] 570c36081dSBruce Richardson 580c36081dSBruce Richardsonif not os.path.exists(test_app_path): 590c36081dSBruce Richardson print("Error: please supply cmdline_test app path") 600c36081dSBruce Richardson sys.exit(1) 610c36081dSBruce Richardson 620c36081dSBruce Richardsonchild = pexpect.spawn(test_app_path) 630c36081dSBruce Richardson 640c36081dSBruce Richardsonprint("Running command-line tests...") 650c36081dSBruce Richardsonfor test in cmdline_test_data.tests: 660c36081dSBruce Richardson testname = (test["Name"] + ":").ljust(30) 670c36081dSBruce Richardson try: 680c36081dSBruce Richardson runTest(child, test) 690c36081dSBruce Richardson print(testname, "PASS") 700c36081dSBruce Richardson except: 710c36081dSBruce Richardson print(testname, "FAIL") 720c36081dSBruce Richardson print(child) 730c36081dSBruce Richardson sys.exit(1) 740c36081dSBruce Richardson 750c36081dSBruce Richardson# since last test quits the app, run new instance 760c36081dSBruce Richardsonchild = pexpect.spawn(test_app_path) 770c36081dSBruce Richardson 780c36081dSBruce Richardsontestname = ("History fill test:").ljust(30) 790c36081dSBruce Richardsontry: 800c36081dSBruce Richardson runHistoryTest(child) 810c36081dSBruce Richardson print(testname, "PASS") 820c36081dSBruce Richardsonexcept: 830c36081dSBruce Richardson print(testname, "FAIL") 840c36081dSBruce Richardson print(child) 850c36081dSBruce Richardson sys.exit(1) 860c36081dSBruce Richardsonchild.close() 870c36081dSBruce Richardsonsys.exit(0) 88