Unit test support
This commit is contained in:
Binary file not shown.
5
3rd-party/Sia-v1.1.0-windows-amd64/01 - siad test.cmd
vendored
Normal file
5
3rd-party/Sia-v1.1.0-windows-amd64/01 - siad test.cmd
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
@echo off
|
||||||
|
pushd "%~dp0%"
|
||||||
|
|
||||||
|
del /s /q ..\..\UnitTests\data 1>NUL 2>&1
|
||||||
|
siad -d ..\..\UnitTests\data
|
@@ -17,7 +17,7 @@ public:
|
|||||||
WalletNotCreated
|
WalletNotCreated
|
||||||
};
|
};
|
||||||
|
|
||||||
class _CSiaWallet
|
class AFX_EXT_CLASS _CSiaWallet
|
||||||
{
|
{
|
||||||
friend CSiaApi;
|
friend CSiaApi;
|
||||||
private:
|
private:
|
||||||
|
@@ -15,6 +15,32 @@ CSiaCurl::~CSiaCurl()
|
|||||||
curl_easy_cleanup(_curlHandle);
|
curl_easy_cleanup(_curlHandle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SiaCurlError CSiaCurl::CheckApiError(const json& result)
|
||||||
|
{
|
||||||
|
SiaCurlError ret = SiaCurlError::Success;
|
||||||
|
if (result.find("message") != result.end())
|
||||||
|
{
|
||||||
|
ret = SiaCurlError::UnknownFailure;
|
||||||
|
|
||||||
|
const std::string msg = result["message"].get<std::string>();
|
||||||
|
if ((msg.length() >= 3))
|
||||||
|
{
|
||||||
|
if ((msg.substr(0, 3) == "404"))
|
||||||
|
{
|
||||||
|
ret = SiaCurlError::InvalidRequestPath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string CSiaCurl::ConstructPath(const String& relativePath) const
|
||||||
|
{
|
||||||
|
const std::string ret = "http://" + std::string(CW2A(GetHostConfig().HostName.c_str())) + ":" + std::to_string(GetHostConfig().HostPort) + std::string(CW2A(relativePath.c_str()));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
SiaCurlError CSiaCurl::_Get(const String& path, json& response) const
|
SiaCurlError CSiaCurl::_Get(const String& path, json& response) const
|
||||||
{
|
{
|
||||||
std::string result;
|
std::string result;
|
||||||
@@ -31,10 +57,10 @@ SiaCurlError CSiaCurl::_Get(const String& path, json& response) const
|
|||||||
const CURLcode res = curl_easy_perform(_curlHandle);
|
const CURLcode res = curl_easy_perform(_curlHandle);
|
||||||
if (res != CURLE_OK)
|
if (res != CURLE_OK)
|
||||||
{
|
{
|
||||||
ret = SiaCurlError::Failed;
|
ret = SiaCurlError::UnknownFailure;
|
||||||
}
|
}
|
||||||
|
|
||||||
response = json::parse(result.c_str());
|
ret = CheckApiError((response = json::parse(result.c_str())));
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@@ -56,12 +82,6 @@ bool CSiaCurl::CheckVersion(SiaCurlError& error) const
|
|||||||
return API_SUCCESS(SiaCurlError, error);
|
return API_SUCCESS(SiaCurlError, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string CSiaCurl::ConstructPath(const String& relativePath) const
|
|
||||||
{
|
|
||||||
const std::string ret = "http://" + std::string(CW2A(GetHostConfig().HostName.c_str()))+ ":" + std::to_string(GetHostConfig().HostPort) + std::string(CW2A(relativePath.c_str()));
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
String CSiaCurl::GetServerVersion() const
|
String CSiaCurl::GetServerVersion() const
|
||||||
{
|
{
|
||||||
json response;
|
json response;
|
||||||
|
@@ -15,7 +15,8 @@ public:
|
|||||||
ServerVersionMismatch,
|
ServerVersionMismatch,
|
||||||
InvalidRequiredVersion,
|
InvalidRequiredVersion,
|
||||||
NoResponse,
|
NoResponse,
|
||||||
Failed
|
InvalidRequestPath,
|
||||||
|
UnknownFailure
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -29,6 +30,9 @@ private:
|
|||||||
|
|
||||||
Property(SiaHostConfig, HostConfig, public, public)
|
Property(SiaHostConfig, HostConfig, public, public)
|
||||||
|
|
||||||
|
private:
|
||||||
|
static _SiaCurlError CheckApiError(const json& result);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string ConstructPath(const String& relativePath) const;
|
std::string ConstructPath(const String& relativePath) const;
|
||||||
_SiaCurlError _Get(const String& path, json& response) const;
|
_SiaCurlError _Get(const String& path, json& response) const;
|
||||||
|
@@ -18,7 +18,7 @@ CSiaApi::_CSiaWallet::~_CSiaWallet()
|
|||||||
|
|
||||||
SiaApiError CSiaApi::_CSiaWallet::Create(String& seed)
|
SiaApiError CSiaApi::_CSiaWallet::Create(String& seed)
|
||||||
{
|
{
|
||||||
SiaApiError error = SiaApiError::Success;
|
SiaApiError error = SiaApiError::NotImplemented;
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
22
UnitTests/SiaApiTests.cpp
Normal file
22
UnitTests/SiaApiTests.cpp
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#include "stdafx.h"
|
||||||
|
#include "CppUnitTest.h"
|
||||||
|
#include <SiaApi.h>
|
||||||
|
#include "UnitTestConfig.h"
|
||||||
|
|
||||||
|
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||||
|
|
||||||
|
namespace UnitTests
|
||||||
|
{
|
||||||
|
TEST_CLASS(SiaApi)
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
const SiaHostConfig TEST_HOST_CONFIG = { L"localhost", 9980, TEST_SERVER_VERSION };
|
||||||
|
|
||||||
|
public:
|
||||||
|
TEST_METHOD(GetServerVersion)
|
||||||
|
{
|
||||||
|
CSiaApi api(TEST_HOST_CONFIG);
|
||||||
|
Assert::IsTrue(api.GetServerVersion() == TEST_SERVER_VERSION);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@@ -1,9 +1,9 @@
|
|||||||
#include "stdafx.h"
|
#include "stdafx.h"
|
||||||
#include "CppUnitTest.h"
|
#include "CppUnitTest.h"
|
||||||
#include "SiaCurl.h"
|
#include "SiaCurl.h"
|
||||||
|
#include "UnitTestConfig.h"
|
||||||
|
|
||||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||||
using namespace Sia::Api;
|
|
||||||
|
|
||||||
namespace UnitTests
|
namespace UnitTests
|
||||||
{
|
{
|
||||||
@@ -13,7 +13,7 @@ namespace UnitTests
|
|||||||
TEST_METHOD(GetBasicTest)
|
TEST_METHOD(GetBasicTest)
|
||||||
{
|
{
|
||||||
CSiaCurl s;
|
CSiaCurl s;
|
||||||
s.SetHostConfig({ L"localhost", 9980, L"1.1.0" });
|
s.SetHostConfig({ L"localhost", 9980, TEST_SERVER_VERSION });
|
||||||
|
|
||||||
json result;
|
json result;
|
||||||
SiaCurlError err = s.Get(L"/daemon/version", result);
|
SiaCurlError err = s.Get(L"/daemon/version", result);
|
||||||
@@ -40,5 +40,25 @@ namespace UnitTests
|
|||||||
Assert::IsTrue(err == SiaCurlError::ServerVersionMismatch);
|
Assert::IsTrue(err == SiaCurlError::ServerVersionMismatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(MissingBeginningForwardSlash)
|
||||||
|
{
|
||||||
|
CSiaCurl s;
|
||||||
|
s.SetHostConfig({ L"localhost", 9980, TEST_SERVER_VERSION });
|
||||||
|
|
||||||
|
json result;
|
||||||
|
SiaCurlError err = s.Get(L"daemon/version", result);
|
||||||
|
Assert::IsTrue(err == SiaCurlError::InvalidRequestPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(InvalidCharactersInPath)
|
||||||
|
{
|
||||||
|
CSiaCurl s;
|
||||||
|
s.SetHostConfig({ L"localhost", 9980, TEST_SERVER_VERSION });
|
||||||
|
|
||||||
|
json result;
|
||||||
|
SiaCurlError err = s.Get(L"~~~^**()Z&%$#daemon/version", result);
|
||||||
|
Assert::IsTrue(err == SiaCurlError::InvalidRequestPath);
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
31
UnitTests/SiaWalletApiTests.cpp
Normal file
31
UnitTests/SiaWalletApiTests.cpp
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#include "stdafx.h"
|
||||||
|
#include "CppUnitTest.h"
|
||||||
|
#include <SiaApi.h>
|
||||||
|
#include "UnitTestConfig.h"
|
||||||
|
|
||||||
|
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||||
|
|
||||||
|
namespace UnitTests
|
||||||
|
{
|
||||||
|
TEST_CLASS(SiaWalletApi)
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
CSiaApi _api = CSiaApi({ L"localhost", 9980, TEST_SERVER_VERSION });
|
||||||
|
|
||||||
|
public:
|
||||||
|
TEST_METHOD(GetWallet)
|
||||||
|
{
|
||||||
|
CSiaWalletPtr wallet = _api.GetWallet();
|
||||||
|
Assert::IsNotNull(wallet.get());
|
||||||
|
Assert::IsFalse(wallet->GetCreated());
|
||||||
|
Assert::IsFalse(wallet->GetLocked());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(CreateWallet)
|
||||||
|
{
|
||||||
|
CSiaWalletPtr wallet = _api.GetWallet();
|
||||||
|
String seed;
|
||||||
|
Assert::IsTrue(API_SUCCESS(SiaApiError, wallet->Create(seed)));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
4
UnitTests/UnitTestConfig.h
Normal file
4
UnitTests/UnitTestConfig.h
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define TEST_SERVER_VERSION L"1.1.0"
|
||||||
|
using namespace Sia::Api;
|
@@ -156,8 +156,17 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="stdafx.h" />
|
<ClInclude Include="stdafx.h" />
|
||||||
<ClInclude Include="targetver.h" />
|
<ClInclude Include="targetver.h" />
|
||||||
|
<ClInclude Include="UnitTestConfig.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="SiaApiTests.cpp">
|
||||||
|
<SubType>
|
||||||
|
</SubType>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="SiaWalletApiTests.cpp">
|
||||||
|
<SubType>
|
||||||
|
</SubType>
|
||||||
|
</ClCompile>
|
||||||
<ClCompile Include="stdafx.cpp">
|
<ClCompile Include="stdafx.cpp">
|
||||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
|
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
|
||||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
|
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
|
||||||
|
@@ -21,6 +21,9 @@
|
|||||||
<ClInclude Include="targetver.h">
|
<ClInclude Include="targetver.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="UnitTestConfig.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="stdafx.cpp">
|
<ClCompile Include="stdafx.cpp">
|
||||||
@@ -29,5 +32,11 @@
|
|||||||
<ClCompile Include="SiaCurlTests.cpp">
|
<ClCompile Include="SiaCurlTests.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="SiaApiTests.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="SiaWalletApiTests.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
Reference in New Issue
Block a user