| // Copyright lowRISC contributors. |
| // Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| // SPDX-License-Identifier: Apache-2.0 |
| |
| use anyhow::{anyhow, Result}; |
| use regex::Regex; |
| use std::time::Duration; |
| use structopt::StructOpt; |
| |
| use opentitanlib::app::TransportWrapper; |
| use opentitanlib::execute_test; |
| use opentitanlib::test_utils::init::InitializeTest; |
| use opentitanlib::uart::console::{ExitStatus, UartConsole}; |
| |
| #[derive(Debug, StructOpt)] |
| struct Opts { |
| #[structopt(flatten)] |
| init: InitializeTest, |
| |
| #[structopt( |
| long, parse(try_from_str=humantime::parse_duration), |
| default_value = "600s", |
| help = "Console receive timeout", |
| )] |
| timeout: Duration, |
| } |
| |
| fn power_virus_systemtest(opts: &Opts, transport: &TransportWrapper) -> Result<()> { |
| // Below is temporary until this test implements the command / response |
| // ujson framework. |
| let uart = transport.uart("console")?; |
| let mut console = UartConsole { |
| timeout: Some(opts.timeout), |
| exit_success: Some(Regex::new(r"PASS.*\n")?), |
| exit_failure: Some(Regex::new(r"(FAIL|FAULT).*\n")?), |
| newline: true, |
| ..Default::default() |
| }; |
| let mut stdout = std::io::stdout(); |
| let result = console.interact(&*uart, None, Some(&mut stdout))?; |
| match result { |
| ExitStatus::None | ExitStatus::CtrlC => Ok(()), |
| ExitStatus::Timeout => { |
| if console.exit_success.is_some() { |
| Err(anyhow!("Console timeout exceeded")) |
| } else { |
| Ok(()) |
| } |
| } |
| ExitStatus::ExitSuccess => { |
| log::info!( |
| "ExitSuccess({:?})", |
| console.captures(result).unwrap().get(0).unwrap().as_str() |
| ); |
| Ok(()) |
| } |
| ExitStatus::ExitFailure => { |
| log::info!( |
| "ExitFailure({:?})", |
| console.captures(result).unwrap().get(0).unwrap().as_str() |
| ); |
| Err(anyhow!("Matched exit_failure expression")) |
| } |
| } |
| } |
| |
| fn main() -> Result<()> { |
| let opts = Opts::from_args(); |
| opts.init.init_logging(); |
| let transport = opts.init.init_target()?; |
| let uart = transport.uart("console")?; |
| uart.set_flow_control(true)?; |
| let _ = UartConsole::wait_for(&*uart, r"Running [^\r\n]*", opts.timeout)?; |
| |
| execute_test!(power_virus_systemtest, &opts, &transport); |
| Ok(()) |
| } |