Skip to content

Commit d06db54

Browse files
author
Vyacheslav
authored
Merge pull request hyperledger-indy#929 from vimmerru/bugfixes/is-802
Bugfixes/is 802
2 parents d519150 + 85dd292 commit d06db54

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

libindy/src/services/wallet/storage/default/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,21 @@ mod tests {
857857
assert_match!(Err(WalletStorageError::NotFound), res);
858858
}
859859

860+
#[test]
861+
fn sqlite_storage_add_works_for_is_802() {
862+
_cleanup();
863+
864+
let storage = _storage();
865+
866+
storage.add(&_type1(), &_id1(), &_value1(), &_tags()).unwrap();
867+
868+
let res = storage.add(&_type1(), &_id1(), &_value1(), &_tags());
869+
assert_match!(Err(WalletStorageError::ItemAlreadyExists), res);
870+
871+
let res = storage.add(&_type1(), &_id1(), &_value1(), &_tags());
872+
assert_match!(Err(WalletStorageError::ItemAlreadyExists), res);
873+
}
874+
860875
#[test]
861876
fn sqlite_storage_set_get_works() {
862877
_cleanup();

libindy/src/services/wallet/storage/default/transaction.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ impl<'conn> Transaction<'conn> {
2525
})
2626
}
2727

28+
/// Get the current setting for what happens to the transaction when it is dropped.
29+
pub fn drop_behavior(&self) -> DropBehavior {
30+
self.drop_behavior
31+
}
32+
33+
/// Configure the transaction to perform the specified action when it is dropped.
34+
pub fn set_drop_behavior(&mut self, drop_behavior: DropBehavior) {
35+
self.drop_behavior = drop_behavior
36+
}
37+
2838
/// A convenience method which consumes and commits a transaction.
2939
pub fn commit(mut self) -> Result<()> {
3040
self.commit_()
@@ -34,6 +44,36 @@ impl<'conn> Transaction<'conn> {
3444
self.committed = true;
3545
self.conn.execute_batch("COMMIT")
3646
}
47+
48+
/// A convenience method which consumes and rolls back a transaction.
49+
pub fn rollback(mut self) -> Result<()> {
50+
self.rollback_()
51+
}
52+
53+
fn rollback_(&mut self) -> Result<()> {
54+
self.committed = true;
55+
self.conn.execute_batch("ROLLBACK")
56+
}
57+
58+
/// Consumes the transaction, committing or rolling back according to the current setting
59+
/// (see `drop_behavior`).
60+
///
61+
/// Functionally equivalent to the `Drop` implementation, but allows callers to see any
62+
/// errors that occur.
63+
pub fn finish(mut self) -> Result<()> {
64+
self.finish_()
65+
}
66+
67+
fn finish_(&mut self) -> Result<()> {
68+
if self.committed {
69+
return Ok(());
70+
}
71+
match self.drop_behavior() {
72+
DropBehavior::Commit => self.commit_(),
73+
DropBehavior::Rollback => self.rollback_(),
74+
DropBehavior::Ignore => Ok(()),
75+
}
76+
}
3777
}
3878

3979
impl<'conn> Deref for Transaction<'conn> {
@@ -42,4 +82,11 @@ impl<'conn> Deref for Transaction<'conn> {
4282
fn deref(&self) -> &Connection {
4383
self.conn
4484
}
45-
}
85+
}
86+
87+
#[allow(unused_must_use)]
88+
impl<'conn> Drop for Transaction<'conn> {
89+
fn drop(&mut self) {
90+
self.finish_();
91+
}
92+
}

libindy/tests/did.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,36 @@ mod high_cases {
10691069

10701070
TestUtils::cleanup_storage();
10711071
}
1072+
1073+
#[test]
1074+
fn indy_store_their_did_works_for_is_802() {
1075+
TestUtils::cleanup_storage();
1076+
1077+
let wallet_handle = WalletUtils::create_and_open_default_wallet().unwrap();
1078+
1079+
let identity_json = json!({
1080+
"did": DID,
1081+
"verkey": VERKEY,
1082+
}).to_string();
1083+
1084+
// 1. Try 'storeTheirDid' operation with say did1 and verkey1
1085+
DidUtils::store_their_did(wallet_handle, &identity_json).unwrap();
1086+
1087+
// 2. Repeat above operation (with same did and ver key used in #1)
1088+
// but this time catch and swallow the exception (it will throw the exception WalletItemAlreadyExistsException)
1089+
let res = DidUtils::store_their_did(wallet_handle, &identity_json);
1090+
assert_eq!(ErrorCode::WalletItemAlreadyExists, res.unwrap_err());
1091+
1092+
// 3. Then, now if you try 'storeTheirDid' operation
1093+
// (either with same did and verkey or you can choose different did and verkey),
1094+
// in IS-802 it fails with error 'Storage error occurred during wallet operation.'
1095+
let res = DidUtils::store_their_did(wallet_handle, &identity_json);
1096+
assert_eq!(ErrorCode::WalletItemAlreadyExists, res.unwrap_err());
1097+
1098+
WalletUtils::close_wallet(wallet_handle).unwrap();
1099+
1100+
TestUtils::cleanup_storage();
1101+
}
10721102
}
10731103

10741104
mod replace_keys {

0 commit comments

Comments
 (0)