Skip to content

Commit

Permalink
Merge pull request #223 from soywod/tracing
Browse files Browse the repository at this point in the history
Logs contributed by @soywod - thanks so much!!
  • Loading branch information
brotskydotcom authored Oct 15, 2024
2 parents dc61b98 + 0c87c15 commit cad3c7b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ jobs:
run: cargo clippy -- -D warnings

- name: Build and Test
env:
RUST_LOG: debug
run: cargo test --features=apple-native,windows-native --verbose

- name: Build the CLI release
Expand Down Expand Up @@ -85,6 +87,8 @@ jobs:
run: gnome-keyring-daemon --components=secrets --daemonize --unlock <<< 'foobar'

- name: Run tests
env:
RUST_LOG: debug
# run tests single-threaded to avoid dbus race conditions
run: cargo test --features=${{ matrix.features }} -- --test-threads=1

Expand Down
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ async-io = ["zbus?/async-io"]
vendored = ["dbus-secret-service?/vendored", "openssl?/vendored"]

[dependencies]
log = "0.4.22"
openssl = { version = "0.10.55", optional = true }

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies] # see issue #190
Expand Down Expand Up @@ -63,10 +64,11 @@ path = "examples/cli.rs"
[dev-dependencies]
base64 = "0.22"
clap = { version = "4", features = ["derive", "wrap_help"] }
doc-comment = "0.3"
env_logger = "0.11.5"
rand = "0.8"
rpassword = "7"
rprompt = "2"
rand = "0.8"
doc-comment = "0.3"
whoami = "1"

[package.metadata.docs.rs]
Expand Down
29 changes: 25 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,11 @@ Service, accesses from multiple threads (and even the same thread very quickly)
are not recommended, as they may cause the RPC mechanism to fail.
*/

use log::debug;
use std::collections::HashMap;

pub use credential::{Credential, CredentialBuilder};
pub use error::{Error, Result};
use std::collections::HashMap;

pub mod mock;

Expand Down Expand Up @@ -328,18 +330,25 @@ impl Entry {
/// will panic. If you encounter this, and especially if you can reproduce it, please report a bug with the
/// details (and preferably a backtrace) so the developers can investigate.
pub fn new(service: &str, user: &str) -> Result<Entry> {
build_default_credential(None, service, user)
debug!("creating entry with service {service}, user {user}, and no target");
let entry = build_default_credential(None, service, user)?;
debug!("created entry {:?}", entry.inner);
Ok(entry)
}

/// Create an entry for the given target, service, and user.
///
/// The default credential builder is used.
pub fn new_with_target(target: &str, service: &str, user: &str) -> Result<Entry> {
build_default_credential(Some(target), service, user)
debug!("creating entry with service {service}, user {user}, and target {target}");
let entry = build_default_credential(Some(target), service, user)?;
debug!("created entry {:?}", entry.inner);
Ok(entry)
}

/// Create an entry that uses the given platform credential for storage.
pub fn new_with_credential(credential: Box<Credential>) -> Entry {
debug!("create entry from {credential:?}");
Entry { inner: credential }
}

Expand All @@ -351,6 +360,7 @@ impl Entry {
/// on some platforms, and then only if a third-party
/// application wrote the ambiguous credential.
pub fn set_password(&self, password: &str) -> Result<()> {
debug!("set password for entry {:?}", self.inner);
self.inner.set_password(password)
}

Expand All @@ -362,6 +372,7 @@ impl Entry {
/// on some platforms, and then only if a third-party
/// application wrote the ambiguous credential.
pub fn set_secret(&self, secret: &[u8]) -> Result<()> {
debug!("set secret for entry {:?}", self.inner);
self.inner.set_secret(secret)
}

Expand All @@ -375,6 +386,7 @@ impl Entry {
/// on some platforms, and then only if a third-party
/// application wrote the ambiguous credential.
pub fn get_password(&self) -> Result<String> {
debug!("get password from entry {:?}", self.inner);
self.inner.get_password()
}

Expand All @@ -388,6 +400,7 @@ impl Entry {
/// on some platforms, and then only if a third-party
/// application wrote the ambiguous credential.
pub fn get_secret(&self) -> Result<Vec<u8>> {
debug!("get secret from entry {:?}", self.inner);
self.inner.get_secret()
}

Expand All @@ -405,6 +418,7 @@ impl Entry {
/// on some platforms, and then only if a third-party
/// application wrote the ambiguous credential.
pub fn get_attributes(&self) -> Result<HashMap<String, String>> {
debug!("get attributes from entry {:?}", self.inner);
self.inner.get_attributes()
}

Expand All @@ -424,6 +438,10 @@ impl Entry {
/// on some platforms, and then only if a third-party
/// application wrote the ambiguous credential.
pub fn update_attributes(&self, attributes: &HashMap<&str, &str>) -> Result<()> {
debug!(
"update attributes for entry {:?} from map {attributes:?}",
self.inner
);
self.inner.update_attributes(attributes)
}

Expand All @@ -441,6 +459,7 @@ impl Entry {
/// structure, which is controlled by Rust. It only
/// affects the underlying credential store.
pub fn delete_credential(&self) -> Result<()> {
debug!("delete entry {:?}", self.inner);
self.inner.delete_credential()
}

Expand Down Expand Up @@ -555,7 +574,9 @@ mod tests {
pub fn generate_random_string_of_len(len: usize) -> String {
// from the Rust Cookbook:
// https://rust-lang-nursery.github.io/rust-cookbook/algorithms/randomness.html
use rand::{distributions::Alphanumeric, thread_rng, Rng};
#[allow(unused_imports)]
use rand::Rng;
use rand::{distributions::Alphanumeric, thread_rng};
thread_rng()
.sample_iter(&Alphanumeric)
.take(len)
Expand Down
14 changes: 13 additions & 1 deletion tests/basic.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use common::generate_random_string;
use common::{generate_random_string, init_logger};
use keyring::{Entry, Error};

mod common;

#[test]
fn test_missing_entry() {
init_logger();

let name = generate_random_string();
let entry = Entry::new(&name, &name).expect("Can't create entry");
assert!(
Expand All @@ -16,6 +18,8 @@ fn test_missing_entry() {
#[test]
#[cfg(all(target_os = "linux", not(feature = "linux-native")))]
fn test_empty_password() {
init_logger();

let name = generate_random_string();
let entry = Entry::new(&name, &name).expect("Can't create entry");
let in_pass = "";
Expand All @@ -36,6 +40,8 @@ fn test_empty_password() {

#[test]
fn test_round_trip_ascii_password() {
init_logger();

let name = generate_random_string();
let entry = Entry::new(&name, &name).expect("Can't create entry");
let password = "test ascii password";
Expand All @@ -58,6 +64,8 @@ fn test_round_trip_ascii_password() {

#[test]
fn test_round_trip_non_ascii_password() {
init_logger();

let name = generate_random_string();
let entry = Entry::new(&name, &name).expect("Can't create entry");
let password = "このきれいな花は桜です";
Expand All @@ -80,6 +88,8 @@ fn test_round_trip_non_ascii_password() {

#[test]
fn test_round_trip_random_secret() {
init_logger();

use rand::{rngs::OsRng, Rng};
let name = generate_random_string();
let entry = Entry::new(&name, &name).expect("Can't create entry");
Expand All @@ -102,6 +112,8 @@ fn test_round_trip_random_secret() {

#[test]
fn test_update() {
init_logger();

let name = generate_random_string();
let entry = Entry::new(&name, &name).expect("Can't create entry");
let password = "test ascii password";
Expand Down
4 changes: 4 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ pub fn generate_random_string_of_len(len: usize) -> String {
pub fn generate_random_string() -> String {
generate_random_string_of_len(30)
}

pub fn init_logger() {
let _ = env_logger::builder().is_test(true).try_init();
}
16 changes: 15 additions & 1 deletion tests/threading.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use common::generate_random_string;
use common::{generate_random_string, init_logger};
use keyring::{Entry, Error};

mod common;

#[test]
fn test_create_then_move() {
init_logger();

let name = generate_random_string();
let entry = Entry::new(&name, &name).unwrap();
let test = move || {
Expand Down Expand Up @@ -40,6 +42,8 @@ fn test_create_then_move() {

#[test]
fn test_simultaneous_create_then_move() {
init_logger();

let mut handles = vec![];
for i in 0..10 {
let name = format!("{}-{}", generate_random_string(), i);
Expand Down Expand Up @@ -69,6 +73,8 @@ fn test_simultaneous_create_then_move() {
#[test]
#[cfg(not(target_os = "windows"))]
fn test_create_set_then_move() {
init_logger();

let name = generate_random_string();
let entry = Entry::new(&name, &name).expect("Can't create entry");
let password = "test ascii password";
Expand Down Expand Up @@ -96,6 +102,8 @@ fn test_create_set_then_move() {
#[test]
#[cfg(not(target_os = "windows"))]
fn test_simultaneous_create_set_then_move() {
init_logger();

let mut handles = vec![];
for i in 0..10 {
let name = format!("{}-{}", generate_random_string(), i);
Expand Down Expand Up @@ -124,6 +132,8 @@ fn test_simultaneous_create_set_then_move() {

#[test]
fn test_simultaneous_independent_create_set() {
init_logger();

let mut handles = vec![];
for i in 0..10 {
let name = format!("thread_entry{i}");
Expand Down Expand Up @@ -153,6 +163,8 @@ fn test_simultaneous_independent_create_set() {
#[test]
#[cfg(any(target_os = "macos", target_os = "windows", feature = "linux-native"))]
fn test_multiple_create_delete_single_thread() {
init_logger();

let name = generate_random_string();
let entry = Entry::new(&name, &name).expect("Can't create entry");
let repeats = 10;
Expand All @@ -176,6 +188,8 @@ fn test_multiple_create_delete_single_thread() {
#[test]
#[cfg(any(target_os = "macos", target_os = "windows", feature = "linux-native"))]
fn test_simultaneous_multiple_create_delete_single_thread() {
init_logger();

let mut handles = vec![];
for t in 0..10 {
let name = generate_random_string();
Expand Down

0 comments on commit cad3c7b

Please sign in to comment.