Skip to content

Commit

Permalink
Update clipboard test specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpfs committed Sep 21, 2024
1 parent b7d2096 commit 1644a16
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 83 deletions.
78 changes: 0 additions & 78 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ tempfile = "3.5"
prost = "0.13"
clap = { version = "4.3.19", features = ["derive", "wrap_help", "env"] }
colored = "2"
arboard = "3"
arboard = { version = "3", default-features = false }
zeroize = "1"

axum-server = { version = "0.7", features = ["tls-rustls-no-provider"] }
Expand Down
5 changes: 3 additions & 2 deletions crates/account_extras/src/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ use zeroize::Zeroize;
/// Wraps [arboard::Clipboard](https://docs.rs/arboard/latest/arboard/struct.Clipboard.html) and provides functions for
/// setting clipboard text with a timeout to clear the clipboard
/// content.
///
pub struct NativeClipboard {
clipboard: Clipboard,
timeout_seconds: u16,
}

impl NativeClipboard {
/// Create a new native clipboard using the default
/// Create a native clipboard using the default
/// timeout of 90 seconds.
pub fn new() -> Result<Self> {
Self::new_timeout(90)
}

/// Create a new native clipboard with a timeout.
/// Create a native clipboard with a timeout.
pub fn new_timeout(timeout_seconds: u16) -> Result<Self> {
Ok(Self {
clipboard: Clipboard::new()?,
Expand Down
41 changes: 39 additions & 2 deletions crates/account_extras/tests/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,59 @@ use anyhow::Result;

#[cfg(feature = "clipboard")]
#[tokio::test]
async fn clipboard() -> Result<()> {
// NOTE: we must run these tests in serial
// NOTE: as the underlying Clipboard cannot
// NOTE: be concurrently created on MacOS
// NOTE: (and possibly other platforms)
clipboard_timeout().await?;
clipboard_timeout_preserve().await?;
Ok(())
}

#[cfg(feature = "clipboard")]
async fn clipboard_timeout() -> Result<()> {
use sos_account_extras::clipboard::NativeClipboard;
use std::time::Duration;

let mut clipboard = NativeClipboard::new_timeout(2)?;
let mut clipboard = NativeClipboard::new_timeout(1)?;
let text = "mock-secret";

clipboard.set_text_timeout(text).await?;

let value = clipboard.get_text()?;
assert_eq!(text, value);

tokio::time::sleep(Duration::from_secs(4)).await;
tokio::time::sleep(Duration::from_secs(2)).await;

// Should error when the clipboard is empty
assert!(clipboard.get_text().is_err());

Ok(())
}

#[cfg(feature = "clipboard")]
async fn clipboard_timeout_preserve() -> Result<()> {
use sos_account_extras::clipboard::NativeClipboard;
use std::time::Duration;

let mut clipboard = NativeClipboard::new_timeout(1)?;
let text = "mock-secret";
let other_value = "mock-value";

clipboard.set_text_timeout(text).await?;

let value = clipboard.get_text()?;
assert_eq!(text, value);

// Set to another value so the clipboard will not
// be cleared on timeout
clipboard.set_text(other_value)?;
tokio::time::sleep(Duration::from_secs(2)).await;

// Verify the clipboard was not cleared on timeout
let value = clipboard.get_text()?;
assert_eq!(other_value, value);

Ok(())
}

0 comments on commit 1644a16

Please sign in to comment.