1*2d9fd380Sjfb8856606#!/usr/bin/env python3
24418919fSjohnjiang# SPDX-License-Identifier: BSD-3-Clause
34418919fSjohnjiang# Copyright(c) 2010-2014 Intel Corporation
44418919fSjohnjiang
54418919fSjohnjiang# Script that runs cmdline_test app and feeds keystrokes into it.
64418919fSjohnjiangimport cmdline_test_data
74418919fSjohnjiangimport os
84418919fSjohnjiangimport pexpect
94418919fSjohnjiangimport sys
104418919fSjohnjiang
114418919fSjohnjiang
124418919fSjohnjiang#
134418919fSjohnjiang# function to run test
144418919fSjohnjiang#
154418919fSjohnjiangdef runTest(child, test):
164418919fSjohnjiang    child.send(test["Sequence"])
174418919fSjohnjiang    if test["Result"] is None:
184418919fSjohnjiang        return 0
194418919fSjohnjiang    child.expect(test["Result"], 1)
204418919fSjohnjiang
214418919fSjohnjiang#
224418919fSjohnjiang# history test is a special case
234418919fSjohnjiang#
244418919fSjohnjiang# This test does the following:
254418919fSjohnjiang# 1) fills the history with garbage up to its full capacity
264418919fSjohnjiang#    (just enough to remove last entry)
274418919fSjohnjiang# 2) scrolls back history to the very beginning
284418919fSjohnjiang# 3) checks if the output is as expected, that is, the first
294418919fSjohnjiang#    number in the sequence (not the last entry before it)
304418919fSjohnjiang#
314418919fSjohnjiang# This is a self-contained test, it needs only a pexpect child
324418919fSjohnjiang#
334418919fSjohnjiangdef runHistoryTest(child):
344418919fSjohnjiang    # find out history size
354418919fSjohnjiang    child.sendline(cmdline_test_data.CMD_GET_BUFSIZE)
364418919fSjohnjiang    child.expect("History buffer size: \\d+", timeout=1)
374418919fSjohnjiang    history_size = int(child.after[len(cmdline_test_data.BUFSIZE_TEMPLATE):])
384418919fSjohnjiang    i = 0
394418919fSjohnjiang
404418919fSjohnjiang    # fill the history with numbers
41*2d9fd380Sjfb8856606    while i < history_size // 10:
424418919fSjohnjiang        # add 1 to prevent from parsing as octals
434418919fSjohnjiang        child.send("1" + str(i).zfill(8) + cmdline_test_data.ENTER)
444418919fSjohnjiang        # the app will simply print out the number
454418919fSjohnjiang        child.expect(str(i + 100000000), timeout=1)
464418919fSjohnjiang        i += 1
474418919fSjohnjiang    # scroll back history
484418919fSjohnjiang    child.send(cmdline_test_data.UP * (i + 2) + cmdline_test_data.ENTER)
494418919fSjohnjiang    child.expect("100000000", timeout=1)
504418919fSjohnjiang
514418919fSjohnjiang# the path to cmdline_test executable is supplied via command-line.
524418919fSjohnjiangif len(sys.argv) < 2:
534418919fSjohnjiang    print("Error: please supply cmdline_test app path")
544418919fSjohnjiang    sys.exit(1)
554418919fSjohnjiang
564418919fSjohnjiangtest_app_path = sys.argv[1]
574418919fSjohnjiang
584418919fSjohnjiangif not os.path.exists(test_app_path):
594418919fSjohnjiang    print("Error: please supply cmdline_test app path")
604418919fSjohnjiang    sys.exit(1)
614418919fSjohnjiang
624418919fSjohnjiangchild = pexpect.spawn(test_app_path)
634418919fSjohnjiang
644418919fSjohnjiangprint("Running command-line tests...")
654418919fSjohnjiangfor test in cmdline_test_data.tests:
664418919fSjohnjiang    testname = (test["Name"] + ":").ljust(30)
674418919fSjohnjiang    try:
684418919fSjohnjiang        runTest(child, test)
694418919fSjohnjiang        print(testname, "PASS")
704418919fSjohnjiang    except:
714418919fSjohnjiang        print(testname, "FAIL")
724418919fSjohnjiang        print(child)
734418919fSjohnjiang        sys.exit(1)
744418919fSjohnjiang
754418919fSjohnjiang# since last test quits the app, run new instance
764418919fSjohnjiangchild = pexpect.spawn(test_app_path)
774418919fSjohnjiang
784418919fSjohnjiangtestname = ("History fill test:").ljust(30)
794418919fSjohnjiangtry:
804418919fSjohnjiang    runHistoryTest(child)
814418919fSjohnjiang    print(testname, "PASS")
824418919fSjohnjiangexcept:
834418919fSjohnjiang    print(testname, "FAIL")
844418919fSjohnjiang    print(child)
854418919fSjohnjiang    sys.exit(1)
864418919fSjohnjiangchild.close()
874418919fSjohnjiangsys.exit(0)
88