Change asyncs to sync. Add cleanup to the tests

This commit is contained in:
Hinton 2022-03-11 14:52:39 +01:00
parent 0546762649
commit 7caab4ae56
4 changed files with 32 additions and 51 deletions

View File

@ -9,7 +9,6 @@ pub mod passwords {
#[napi]
pub async fn get_password(service: String, account: String) -> napi::Result<String> {
super::password::get_password(&service, &account)
.await
.map_err(|e| napi::Error::from_reason(e.to_string()))
}
@ -17,7 +16,6 @@ pub mod passwords {
#[napi]
pub async fn get_password_keytar(service: String, account: String) -> napi::Result<String> {
super::password::get_password_keytar(&service, &account)
.await
.map_err(|e| napi::Error::from_reason(e.to_string()))
}
@ -29,7 +27,6 @@ pub mod passwords {
password: String,
) -> napi::Result<()> {
super::password::set_password(&service, &account, &password)
.await
.map_err(|e| napi::Error::from_reason(e.to_string()))
}
@ -37,7 +34,6 @@ pub mod passwords {
#[napi]
pub async fn delete_password(service: String, account: String) -> napi::Result<()> {
super::password::delete_password(&service, &account)
.await
.map_err(|e| napi::Error::from_reason(e.to_string()))
}
}

View File

@ -3,21 +3,21 @@ use security_framework::passwords::{
delete_generic_password, get_generic_password, set_generic_password,
};
pub async fn get_password(service: &str, account: &str) -> Result<String> {
pub fn get_password(service: &str, account: &str) -> Result<String> {
let result = String::from_utf8(get_generic_password(&service, &account)?)?;
Ok(result)
}
pub async fn get_password_keytar(service: &str, account: &str) -> Result<String> {
get_password(service, account).await
pub fn get_password_keytar(service: &str, account: &str) -> Result<String> {
get_password(service, account)
}
pub async fn set_password(service: &str, account: &str, password: &str) -> Result<()> {
pub fn set_password(service: &str, account: &str, password: &str) -> Result<()> {
let result = set_generic_password(&service, &account, password.as_bytes())?;
Ok(result)
}
pub async fn delete_password(service: &str, account: &str) -> Result<()> {
pub fn delete_password(service: &str, account: &str) -> Result<()> {
let result = delete_generic_password(&service, &account)?;
Ok(result)
}
@ -26,19 +26,14 @@ pub async fn delete_password(service: &str, account: &str) -> Result<()> {
mod tests {
use super::*;
#[tokio::test]
async fn test() {
set_password("BitwardenTest", "BitwardenTest", "Random")
.await
.unwrap();
#[test]
fn test() {
scopeguard::defer!(delete_password("BitwardenTest", "BitwardenTest"););
set_password("BitwardenTest", "BitwardenTest", "Random").unwrap();
assert_eq!(
"Random",
get_password("BitwardenTest", "BitwardenTest")
.await
.unwrap()
get_password("BitwardenTest", "BitwardenTest").unwrap()
);
delete_password("BitwardenTest", "BitwardenTest")
.await
.unwrap();
delete_password("BitwardenTest", "BitwardenTest").unwrap();
}
}

View File

@ -2,7 +2,7 @@ use anyhow::{anyhow, Result};
use libsecret::{password_clear_sync, password_lookup_sync, password_store_sync, Schema};
use std::collections::HashMap;
pub async fn get_password(service: &str, account: &str) -> Result<String> {
pub fn get_password(service: &str, account: &str) -> Result<String> {
let res = password_lookup_sync(
Some(&get_schema()),
build_attributes(service, account),
@ -15,11 +15,11 @@ pub async fn get_password(service: &str, account: &str) -> Result<String> {
}
}
pub async fn get_password_keytar(service: &str, account: &str) -> Result<String> {
get_password(service, account).await
pub fn get_password_keytar(service: &str, account: &str) -> Result<String> {
get_password(service, account)
}
pub async fn set_password(service: &str, account: &str, password: &str) -> Result<()> {
pub fn set_password(service: &str, account: &str, password: &str) -> Result<()> {
let result = password_store_sync(
Some(&get_schema()),
build_attributes(service, account),
@ -31,7 +31,7 @@ pub async fn set_password(service: &str, account: &str, password: &str) -> Resul
Ok(result)
}
pub async fn delete_password(service: &str, account: &str) -> Result<()> {
pub fn delete_password(service: &str, account: &str) -> Result<()> {
let result = password_clear_sync(
Some(&get_schema()),
build_attributes(service, account),
@ -64,19 +64,14 @@ fn build_attributes<'a>(service: &'a str, account: &'a str) -> HashMap<&'a str,
mod tests {
use super::*;
#[tokio::test]
async fn test() {
set_password("BitwardenTest", "BitwardenTest", "Random")
.await
.unwrap();
#[test]
fn test() {
scopeguard::defer!(delete_password("BitwardenTest", "BitwardenTest"););
set_password("BitwardenTest", "BitwardenTest", "Random").unwrap();
assert_eq!(
"Random",
get_password("BitwardenTest", "BitwardenTest")
.await
.unwrap()
get_password("BitwardenTest", "BitwardenTest").unwrap()
);
delete_password("BitwardenTest", "BitwardenTest")
.await
.unwrap();
delete_password("BitwardenTest", "BitwardenTest").unwrap();
}
}

View File

@ -10,7 +10,7 @@ use windows::Win32::{
const CRED_FLAGS_NONE: u32 = 0;
pub async fn get_password<'a>(service: &str, account: &str) -> Result<String> {
pub fn get_password<'a>(service: &str, account: &str) -> Result<String> {
let target_name = U16CString::from_str(target_name(service, account))?;
let mut credential: *mut CREDENTIALW = std::ptr::null_mut();
@ -45,7 +45,7 @@ pub async fn get_password<'a>(service: &str, account: &str) -> Result<String> {
}
// Remove this after sufficient releases
pub async fn get_password_keytar<'a>(service: &str, account: &str) -> Result<String> {
pub fn get_password_keytar<'a>(service: &str, account: &str) -> Result<String> {
let target_name = U16CString::from_str(target_name(service, account))?;
let mut credential: *mut CREDENTIALW = std::ptr::null_mut();
@ -79,7 +79,7 @@ pub async fn get_password_keytar<'a>(service: &str, account: &str) -> Result<Str
Ok(String::from(password))
}
pub async fn set_password(service: &str, account: &str, password: &str) -> Result<()> {
pub fn set_password(service: &str, account: &str, password: &str) -> Result<()> {
let target_name = U16CString::from_str(target_name(service, account))?;
let user_name = U16CString::from_str(account)?;
let last_written = FILETIME {
@ -117,7 +117,7 @@ pub async fn set_password(service: &str, account: &str, password: &str) -> Resul
Ok(())
}
pub async fn delete_password(service: &str, account: &str) -> Result<()> {
pub fn delete_password(service: &str, account: &str) -> Result<()> {
let target_name = U16CString::from_str(target_name(service, account))?;
unsafe {
@ -140,19 +140,14 @@ fn target_name(service: &str, account: &str) -> String {
mod tests {
use super::*;
#[tokio::test]
async fn test() {
set_password("BitwardenTest", "BitwardenTest", "Random")
.await
.unwrap();
#[test]
fn test() {
scopeguard::defer!(delete_password("BitwardenTest", "BitwardenTest"););
set_password("BitwardenTest", "BitwardenTest", "Random").unwrap();
assert_eq!(
"Random",
get_password("BitwardenTest", "BitwardenTest")
.await
.unwrap()
get_password("BitwardenTest", "BitwardenTest").unwrap()
);
delete_password("BitwardenTest", "BitwardenTest")
.await
.unwrap();
delete_password("BitwardenTest", "BitwardenTest").unwrap();
}
}