Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/wrong cf on write to vault #253

Merged
merged 4 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/write_to_vault.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"iota-stronghold": patch
---

- corrects wrong control flow. `write_to_vault` always returned an error even if the operation was successful.
18 changes: 15 additions & 3 deletions client/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,21 @@ impl Stronghold {
match result {
Ok(_) => {
// exists
let _result = self
return match self
.target
.send(WriteToVault {
location,
payload,
hint,
})
.await;
.await
{
Ok(result) => match result {
Ok(_) => StatusMessage::OK,
Err(e) => StatusMessage::Error(e.to_string()),
},
Err(e) => StatusMessage::Error(e.to_string()),
};
}
Err(_) => {
// does not exist
Expand All @@ -177,7 +184,7 @@ impl Stronghold {
{
Ok(_) => {
// write to vault
if let Ok(_result) = self
if let Ok(result) = self
.target
.send(WriteToVault {
location,
Expand All @@ -186,6 +193,11 @@ impl Stronghold {
})
.await
{
if result.is_ok() {
return StatusMessage::OK;
} else {
return StatusMessage::Error(result.err().unwrap().to_string());
}
Comment on lines +196 to +200
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if result.is_ok() {
return StatusMessage::OK;
} else {
return StatusMessage::Error(result.err().unwrap().to_string());
}
return match result {
Ok(_) => StatusMessage::OK,
Err(e) => StatusMessage::Error(e.to_string()),
};

} else {
return StatusMessage::Error("Error Writing data".into());
}
Expand Down
45 changes: 27 additions & 18 deletions client/src/tests/interface_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,46 @@ async fn test_stronghold() {
.unwrap();

// Write at the first record of the vault using Some(0). Also creates the new vault.
stronghold
assert!(stronghold
.write_to_vault(
loc0.clone(),
b"test".to_vec(),
RecordHint::new(b"first hint").expect(line_error!()),
vec![],
)
.await;
.await
.is_ok());

// read head.
let (p, _) = stronghold.read_secret(client_path.clone(), loc0.clone()).await;

assert_eq!(std::str::from_utf8(&p.unwrap()), Ok("test"));

// Write on the next record of the vault using None. This calls InitRecord and creates a new one at index 1.
stronghold
assert!(stronghold
.write_to_vault(
loc1.clone(),
b"another test".to_vec(),
RecordHint::new(b"another hint").expect(line_error!()),
vec![],
)
.await;
.await
.is_ok());

// read head.
let (p, _) = stronghold.read_secret(client_path.clone(), loc1.clone()).await;

assert_eq!(std::str::from_utf8(&p.unwrap()), Ok("another test"));

stronghold
assert!(stronghold
.write_to_vault(
loc2.clone(),
b"yet another test".to_vec(),
RecordHint::new(b"yet another hint").expect(line_error!()),
vec![],
)
.await;
.await
.is_ok());

// read head.
let (p, _) = stronghold.read_secret(client_path.clone(), loc2.clone()).await;
Expand Down Expand Up @@ -174,14 +177,15 @@ async fn run_stronghold_multi_actors() {

stronghold.switch_actor_target(client_path0.clone()).await;

stronghold
assert!(stronghold
.write_to_vault(
loc0.clone(),
b"test".to_vec(),
RecordHint::new(b"0").expect(line_error!()),
vec![],
)
.await;
.await
.is_ok());

// read head.
let (p, _) = stronghold.read_secret(client_path0.clone(), loc0.clone()).await;
Expand All @@ -191,14 +195,15 @@ async fn run_stronghold_multi_actors() {
stronghold.switch_actor_target(client_path1.clone()).await;

// Write on the next record of the vault using None. This calls InitRecord and creates a new one at index 1.
stronghold
assert!(stronghold
.write_to_vault(
loc0.clone(),
b"another test".to_vec(),
RecordHint::new(b"1").expect(line_error!()),
vec![],
)
.await;
.await
.is_ok());

// read head.
let (p, _) = stronghold.read_secret(client_path1.clone(), loc0.clone()).await;
Expand All @@ -207,14 +212,15 @@ async fn run_stronghold_multi_actors() {

stronghold.switch_actor_target(client_path0.clone()).await;

stronghold
assert!(stronghold
.write_to_vault(
loc0.clone(),
b"yet another test".to_vec(),
RecordHint::new(b"2").expect(line_error!()),
vec![],
)
.await;
.await
.is_ok());

let (p, _) = stronghold.read_secret(client_path0.clone(), loc0.clone()).await;

Expand Down Expand Up @@ -249,27 +255,29 @@ async fn run_stronghold_multi_actors() {

assert_eq!(std::str::from_utf8(&p.unwrap()), Ok("another test"));

stronghold
assert!(stronghold
.write_to_vault(
loc3.clone(),
b"a new actor test".to_vec(),
RecordHint::new(b"2").expect(line_error!()),
vec![],
)
.await;
.await
.is_ok());

let (p, _) = stronghold.read_secret(client_path2.clone(), loc3).await;

assert_eq!(std::str::from_utf8(&p.unwrap()), Ok("a new actor test"));

stronghold
assert!(stronghold
.write_to_vault(
loc4.clone(),
b"a new actor test again".to_vec(),
RecordHint::new(b"3").expect(line_error!()),
vec![],
)
.await;
.await
.is_ok());

let (p, _) = stronghold.read_secret(client_path2, loc4.clone()).await;

Expand Down Expand Up @@ -329,14 +337,15 @@ async fn test_stronghold_generics() {
.await
.unwrap();

stronghold
assert!(stronghold
.write_to_vault(
slip10_seed.clone(),
b"AAAAAA".to_vec(),
RecordHint::new(b"first hint").expect(line_error!()),
vec![],
)
.await;
.await
.is_ok());
let (p, _) = stronghold.read_secret(client_path, slip10_seed).await;
assert_eq!(std::str::from_utf8(&p.unwrap()), Ok("AAAAAA"));

Expand Down