use crate::common::proxies::control_proxy::ControlProxy;
use crate::universal::setup;
use googletest::prelude::*;
use rstest::rstest;
use tokio;
use zbus::{Connection, Result};
#[gtest]
#[tokio::test]
#[rstest]
#[case::out_of_tree(
"/lib/firmware/new_file",
"",
err(displays_as(contains_substring("Cannot access property")))
)]
#[case::path_traversal(
"/sys/class/fpga_manager/../../../usr/bin/evil_file.sh",
"",
err(displays_as(contains_substring("path traversal")))
)]
#[case::missing_file(
"/sys/class/fpga_manager/fpga0/write_not_exist.txt",
"",
err(displays_as(contains_substring("FpgadError::IOWrite: ")))
)]
#[case::reset_key("/sys/class/fpga_manager/fpga0/key", "", ok(anything()))]
pub async fn cases<M: for<'a> Matcher<&'a Result<String>>>(
#[case] path: &str,
#[case] data: &str,
#[case] condition: M,
_setup: (),
) {
let connection = Connection::system()
.await
.expect("failed to create connection");
let proxy = ControlProxy::new(&connection)
.await
.expect("failed to create control proxy");
let res = proxy.write_property(path, data).await;
expect_that!(&res, condition);
}