[Unit Tests] Lock/unlock wallet
This commit is contained in:
@@ -11,7 +11,7 @@ CSiaApi::CSiaApi(const SiaHostConfig& hostConfig) :
|
||||
|
||||
CSiaApi::~CSiaApi()
|
||||
{
|
||||
_wallet->Unlock();
|
||||
_wallet->Lock();
|
||||
}
|
||||
|
||||
String CSiaApi::GetServerVersion() const
|
||||
|
@@ -47,7 +47,7 @@ public:
|
||||
bool Refresh();
|
||||
_SiaApiError Restore(const String& seed);
|
||||
_SiaApiError Lock();
|
||||
_SiaApiError Unlock();
|
||||
_SiaApiError Unlock(const String& password);
|
||||
};
|
||||
|
||||
public:
|
||||
|
@@ -65,7 +65,7 @@ SiaCurlError CSiaCurl::_Get(const String& path, json& response) const
|
||||
SiaCurlError ret;
|
||||
if (res == CURLE_OK)
|
||||
{
|
||||
ret = CheckApiError((response = json::parse(result.c_str())));
|
||||
ret = (result.length() ? CheckApiError((response = json::parse(result.c_str()))) : SiaCurlError::Success);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -153,7 +153,7 @@ SiaCurlError CSiaCurl::Post(const String& path, const PostParameters& parameters
|
||||
|
||||
if (res == CURLE_OK)
|
||||
{
|
||||
ret = CheckApiError((response = json::parse(result.c_str())));
|
||||
ret = (result.length() ? CheckApiError((response = json::parse(result.c_str()))) : SiaCurlError::Success);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@@ -41,7 +41,7 @@ bool CSiaApi::_CSiaWallet::Refresh()
|
||||
if (API_SUCCESS(SiaCurlError, error))
|
||||
{
|
||||
SetCreated(result["encrypted"].get<bool>());
|
||||
SetLocked(result["unlocked"].get<bool>());
|
||||
SetLocked(!result["unlocked"].get<bool>());
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -84,17 +84,35 @@ SiaApiError CSiaApi::_CSiaWallet::Lock()
|
||||
SiaApiError error = GetCreated() ? (GetLocked() ? SiaApiError::WalletLocked : SiaApiError::Success) : SiaApiError::WalletNotCreated;
|
||||
if (API_SUCCESS(SiaApiError, error))
|
||||
{
|
||||
error = SiaApiError::RequestError;
|
||||
|
||||
json result;
|
||||
SiaCurlError cerror = _siaCurl.Post(L"/wallet/lock", {}, result);
|
||||
if (API_SUCCESS(SiaCurlError, cerror))
|
||||
{
|
||||
Refresh();
|
||||
error = SiaApiError::Success;
|
||||
}
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
SiaApiError CSiaApi::_CSiaWallet::Unlock()
|
||||
SiaApiError CSiaApi::_CSiaWallet::Unlock(const String& password)
|
||||
{
|
||||
SiaApiError error = GetCreated() ? (GetLocked() ? SiaApiError::Success : SiaApiError::WalletUnlocked) : SiaApiError::WalletNotCreated;
|
||||
if (API_SUCCESS(SiaApiError, error))
|
||||
{
|
||||
error = SiaApiError::RequestError;
|
||||
|
||||
json result;
|
||||
SiaCurlError cerror = _siaCurl.Post(L"/wallet/unlock", { {L"encryptionpassword", password} }, result);
|
||||
if (API_SUCCESS(SiaCurlError, cerror))
|
||||
{
|
||||
Refresh();
|
||||
error = SiaApiError::Success;
|
||||
}
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
@@ -18,10 +18,10 @@ namespace UnitTests
|
||||
CSiaWalletPtr wallet = _api.GetWallet();
|
||||
Assert::IsNotNull(wallet.get());
|
||||
Assert::IsFalse(wallet->GetCreated());
|
||||
Assert::IsFalse(wallet->GetLocked());
|
||||
Assert::IsTrue(wallet->GetLocked());
|
||||
}
|
||||
|
||||
TEST_METHOD(CreateWalletWithEnglishSeed)
|
||||
TEST_METHOD(CreateWalletAndUnlockWithEnglishSeedAndLock)
|
||||
{
|
||||
CSiaWalletPtr wallet = _api.GetWallet();
|
||||
Assert::IsFalse(wallet->GetCreated());
|
||||
@@ -29,7 +29,15 @@ namespace UnitTests
|
||||
String seed;
|
||||
Assert::IsTrue(API_SUCCESS(SiaApiError, wallet->Create(SiaSeedLanguage::English, seed)));
|
||||
Assert::IsTrue(wallet->GetCreated());
|
||||
Assert::IsTrue(wallet->GetLocked());
|
||||
|
||||
Assert::IsTrue(API_SUCCESS(SiaApiError, wallet->Unlock(seed)));
|
||||
Assert::IsTrue(wallet->GetCreated());
|
||||
Assert::IsFalse(wallet->GetLocked());
|
||||
|
||||
Assert::IsTrue(API_SUCCESS(SiaApiError, wallet->Lock( )));
|
||||
Assert::IsTrue(wallet->GetCreated());
|
||||
Assert::IsTrue(wallet->GetLocked());
|
||||
}
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user