Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
V
Vereign Client Library
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Code
Vereign Client Library
Commits
61aa3d49
Verified
Commit
61aa3d49
authored
4 years ago
by
Daniel Lyubomirov
Browse files
Options
Downloads
Patches
Plain Diff
[17] crypto storage locking
parent
a4531d95
No related branches found
No related tags found
1 merge request
!97
Crypto Storage and LoginWithNewDevice and LoginWithPreviouslyAddedDevice APIs
Changes
22
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
cpp/tests/vereign/kvstore/lock_test.cc
+62
-0
62 additions, 0 deletions
cpp/tests/vereign/kvstore/lock_test.cc
cpp/tests/vereign/kvstore/sqlite_storage_test.cc
+97
-0
97 additions, 0 deletions
cpp/tests/vereign/kvstore/sqlite_storage_test.cc
with
159 additions
and
0 deletions
cpp/tests/vereign/kvstore/lock_test.cc
0 → 100644
+
62
−
0
View file @
61aa3d49
#include
<vereign/kvstore/sqlite_storage.hh>
#include
<vereign/kvstore/lock.hh>
#include
<vereign/kvstore/errors.hh>
#include
<vereign/bytes/view_dump.hh>
#include
<vereign/core/fs.hh>
#include
<vereign/core/lock_guard.hh>
#include
<vereign/core/scope_guard.hh>
#include
<vereign/core/temp.hh>
#include
<vereign/crypto/rand.hh>
#include
<vereign/sqlite/errors.hh>
#include
<util/error.hh>
#include
<sqlite3.h>
#include
<catch2/catch.hpp>
#include
<boost/filesystem.hpp>
#include
<thread>
#include
<chrono>
#include
<limits>
#include
<optional>
using
namespace
vereign
;
TEST_CASE
(
"kvstore::Lock"
,
"[vereign/kvstore]"
)
{
SECTION
(
"when the lock is released within the allowed retrials, the lock succeeds"
)
{
auto
storage_path
=
core
::
TempFilePath
(
"test_db_"
);
core
::
RemoveFileGuard
rm
{
storage_path
};
auto
foo_storage
=
kvstore
::
SqliteStorage
(
storage_path
.
string
());
auto
bar_storage
=
kvstore
::
SqliteStorage
(
storage_path
.
string
());
foo_storage
.
Lock
();
bool
err
=
false
;
auto
th
=
std
::
thread
{[
&
bar_storage
,
&
err
]()
{
try
{
kvstore
::
Lock
l
{
bar_storage
,
std
::
numeric_limits
<
int
>::
max
(),
std
::
chrono
::
milliseconds
{
10
}};
}
catch
(...)
{
err
=
true
;
}
}};
foo_storage
.
Unlock
();
th
.
join
();
}
SECTION
(
"when the lock is not released within the allowed retrials, the lock fails"
)
{
auto
storage_path
=
core
::
TempFilePath
(
"test_db_"
);
core
::
RemoveFileGuard
rm
{
storage_path
};
auto
foo_storage
=
kvstore
::
SqliteStorage
(
storage_path
.
string
());
auto
bar_storage
=
kvstore
::
SqliteStorage
(
storage_path
.
string
());
foo_storage
.
Lock
();
CHECK_THROWS_AS
(
kvstore
::
Lock
(
bar_storage
,
2
,
std
::
chrono
::
milliseconds
{
1
}),
kvstore
::
LockError
);
}
}
This diff is collapsed.
Click to expand it.
cpp/tests/vereign/kvstore/sqlite_storage_test.cc
0 → 100644
+
97
−
0
View file @
61aa3d49
#include
<vereign/kvstore/sqlite_storage.hh>
#include
<vereign/kvstore/lock.hh>
#include
<vereign/kvstore/errors.hh>
#include
<vereign/bytes/view_dump.hh>
#include
<vereign/core/fs.hh>
#include
<vereign/core/scope_guard.hh>
#include
<vereign/core/temp.hh>
#include
<vereign/crypto/rand.hh>
#include
<vereign/sqlite/errors.hh>
#include
<util/error.hh>
#include
<sqlite3.h>
#include
<catch2/catch.hpp>
#include
<boost/filesystem.hpp>
#include
<thread>
#include
<optional>
using
namespace
vereign
;
TEST_CASE
(
"kvstore::SqliteStorage::DeleteAll"
,
"[vereign/kvstore]"
)
{
auto
storage_path
=
core
::
TempFilePath
(
"test_db_"
);
core
::
RemoveFileGuard
rm
{
storage_path
};
auto
kvstorage
=
kvstore
::
SqliteStorage
(
storage_path
.
string
());
kvstorage
.
PutInt64
(
"foo"
,
42
);
kvstorage
.
PutInt64
(
"bar"
,
422
);
CHECK
(
kvstorage
.
GetInt64
(
"foo"
)
==
42
);
CHECK
(
kvstorage
.
GetInt64
(
"bar"
)
==
422
);
kvstorage
.
DeleteAll
();
CHECK
(
kvstorage
.
GetInt64
(
"foo"
)
==
0
);
CHECK
(
kvstorage
.
GetInt64
(
"bar"
)
==
0
);
}
TEST_CASE
(
"kvstore::SqliteStorage::Lock"
,
"[vereign/kvstore]"
)
{
SECTION
(
"when locked using lock guard, it must unlock on scope exit"
)
{
auto
storage_path
=
core
::
TempFilePath
(
"test_db_"
);
core
::
RemoveFileGuard
rm
{
storage_path
};
{
auto
kvstorage
=
kvstore
::
SqliteStorage
(
storage_path
.
string
());
kvstore
::
Lock
l
{
kvstorage
};
kvstorage
.
PutInt64
(
"foo"
,
42
);
kvstorage
.
PutInt64
(
"bar"
,
422
);
}
{
auto
kvstorage
=
kvstore
::
SqliteStorage
(
storage_path
.
string
());
kvstore
::
Lock
l
{
kvstorage
};
CHECK
(
kvstorage
.
GetInt64
(
"foo"
)
==
42
);
CHECK
(
kvstorage
.
GetInt64
(
"bar"
)
==
422
);
}
}
SECTION
(
"when locked, it must unlock on scope exit"
)
{
auto
storage_path
=
core
::
TempFilePath
(
"test_db_"
);
core
::
RemoveFileGuard
rm
{
storage_path
};
{
auto
kvstorage
=
kvstore
::
SqliteStorage
(
storage_path
.
string
());
kvstorage
.
Lock
();
kvstorage
.
PutInt64
(
"foo"
,
42
);
kvstorage
.
PutInt64
(
"bar"
,
422
);
}
{
auto
kvstorage
=
kvstore
::
SqliteStorage
(
storage_path
.
string
());
kvstorage
.
Lock
();
CHECK
(
kvstorage
.
GetInt64
(
"foo"
)
==
42
);
CHECK
(
kvstorage
.
GetInt64
(
"bar"
)
==
422
);
}
}
SECTION
(
"when the storage is already locked, it must fail with LockError"
)
{
auto
storage_path
=
core
::
TempFilePath
(
"test_db_"
);
core
::
RemoveFileGuard
rm
{
storage_path
};
auto
foo_storage
=
kvstore
::
SqliteStorage
(
storage_path
.
string
());;
auto
bar_storage
=
kvstore
::
SqliteStorage
(
storage_path
.
string
());
foo_storage
.
Lock
();
CHECK_THROWS_AS
(
bar_storage
.
Lock
(),
kvstore
::
LockError
);
}
}
This diff is collapsed.
Click to expand it.
Prev
1
2
Next
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment