Skip to content

Commit

Permalink
Add test application
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteOtter committed Jul 25, 2024
1 parent 93d8859 commit 0fb36aa
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
10 changes: 10 additions & 0 deletions vnc-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "vnc-test"
version = "0.1.0"
edition = "2021"

[dependencies]
isototest = { package = "isototest", path = "../isototest" }
nix = { version = "0.29.0", features = ["socket", "net"] }
tokio = { version = "1.38.1", features = ["full"] }
vnc-rs = "0.5.1"
102 changes: 102 additions & 0 deletions vnc-test/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use isototest::action::write_to_console;
use isototest::connection::create_vnc_client;
use nix::sys::socket::{self, sockaddr_in, AddressFamily, SockType};
use std::process::exit;
use std::process::{Command, Stdio};
use std::ptr::null_mut;
use tokio::{
self,
net::{TcpListener, TcpStream},
};
use vnc::VncError;

// pub struct VncServer {
// srv: Option<TcpListener>,
// }

// impl VncServer {
// pub async fn start(&mut self) -> Result<(), Box<dyn std::error::Error>> {
// let addr = Self::get_dynamic_port_address()?;
// self.srv = Some(TcpListener::bind(addr).await?);
// Ok(())
// }

// pub async fn stop(&self) -> Result<(), Box<dyn std::error::Error>> {
// if let Some(srv) = &self.srv {
// drop(srv);
// }
// Ok(())
// }

// pub async fn spawn_vnc_server(&self) {
// if let Some(srv) = &self.srv {
// let mut socket = srv.accept().await.unwrap();
// tokio::spawn(async move {
// let output = Command::new("vncserver")
// .stdin(Stdio::piped())
// .stdout(Stdio::piped())
// .stderr(Stdio::piped())
// .spawn()
// .unwrap();

// let _ = output.wait_with_output().unwrap();
// });
// }
// }

// fn get_dynamic_port_address() -> Result<String, Box<dyn std::error::Error>> {
// let sock = unsafe { socket::socket(AddressFamily::AF_INET, SockType::SOCK_STREAM, 0)? };
// let mut sin = sockaddr_in {
// sin_family: AddressFamily::AF_INET as u16,
// sin_port: 0,
// sin_addr: std::net::Ipv4Addr::LOCALHOST.into(),
// sin_zero: [0; 8],
// };

// let res =
// unsafe { socket::getaddrinfo(null_mut(), b"vnc\0".as_ptr(), null_mut(), null_mut()) };
// let addr = unsafe { res.get(0)?.addr };
// let port = ((addr as *const sockaddr_in).cast::<u16>() as usize) % 65536;

// Ok(format!("127.0.0.1:{}", port))
// }
// }

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// let mut vnc_server = VncServer { srv: None };

// println!("Starting...");

// vnc_server.start().await?;
// println!("Spawning connection...");
// vnc_server.spawn_vnc_server().await;

// println!("Creating client...");

// println!("Getting port...");
// let addr = vnc_server.srv.as_ref().unwrap().local_addr()?;
let addr = "";
let client = match create_vnc_client(addr.to_string(), None).await {
Ok(client) => {
println!("Client created. Handshake successful.");
client
}
Err(e) => {
eprintln!("[Error] {:?}.", e);
exit(1);
}
};

match write_to_console(&client, include_str!("lore.txt").to_string(), None).await {
Ok(_) => {
println!("Test text sent!");
}
Err(e) => {
eprintln!("[error] {:?}.", e);
exit(1);
}
}

Ok(())
}

0 comments on commit 0fb36aa

Please sign in to comment.