1
0

Unit test support

This commit is contained in:
Scott E. Graves
2017-02-03 14:01:00 -06:00
parent 0b625185aa
commit 25eae58a5c
12 changed files with 137 additions and 13 deletions

View File

@@ -0,0 +1,5 @@
@echo off
pushd "%~dp0%"
del /s /q ..\..\UnitTests\data 1>NUL 2>&1
siad -d ..\..\UnitTests\data

View File

@@ -17,7 +17,7 @@ public:
WalletNotCreated
};
class _CSiaWallet
class AFX_EXT_CLASS _CSiaWallet
{
friend CSiaApi;
private:

View File

@@ -15,6 +15,32 @@ CSiaCurl::~CSiaCurl()
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
{
std::string result;
@@ -31,10 +57,10 @@ SiaCurlError CSiaCurl::_Get(const String& path, json& response) const
const CURLcode res = curl_easy_perform(_curlHandle);
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;
@@ -56,12 +82,6 @@ bool CSiaCurl::CheckVersion(SiaCurlError& error) const
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
{
json response;

View File

@@ -15,7 +15,8 @@ public:
ServerVersionMismatch,
InvalidRequiredVersion,
NoResponse,
Failed
InvalidRequestPath,
UnknownFailure
};
public:
@@ -29,6 +30,9 @@ private:
Property(SiaHostConfig, HostConfig, public, public)
private:
static _SiaCurlError CheckApiError(const json& result);
private:
std::string ConstructPath(const String& relativePath) const;
_SiaCurlError _Get(const String& path, json& response) const;

View File

@@ -18,7 +18,7 @@ CSiaApi::_CSiaWallet::~_CSiaWallet()
SiaApiError CSiaApi::_CSiaWallet::Create(String& seed)
{
SiaApiError error = SiaApiError::Success;
SiaApiError error = SiaApiError::NotImplemented;
return error;
}

22
UnitTests/SiaApiTests.cpp Normal file
View 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);
}
};
}

View File

@@ -1,9 +1,9 @@
#include "stdafx.h"
#include "CppUnitTest.h"
#include "SiaCurl.h"
#include "UnitTestConfig.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace Sia::Api;
namespace UnitTests
{
@@ -13,7 +13,7 @@ namespace UnitTests
TEST_METHOD(GetBasicTest)
{
CSiaCurl s;
s.SetHostConfig({ L"localhost", 9980, L"1.1.0" });
s.SetHostConfig({ L"localhost", 9980, TEST_SERVER_VERSION });
json result;
SiaCurlError err = s.Get(L"/daemon/version", result);
@@ -40,5 +40,25 @@ namespace UnitTests
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);
}
};
}

View 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)));
}
};
}

View File

@@ -0,0 +1,4 @@
#pragma once
#define TEST_SERVER_VERSION L"1.1.0"
using namespace Sia::Api;

View File

@@ -156,8 +156,17 @@
<ItemGroup>
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
<ClInclude Include="UnitTestConfig.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="SiaApiTests.cpp">
<SubType>
</SubType>
</ClCompile>
<ClCompile Include="SiaWalletApiTests.cpp">
<SubType>
</SubType>
</ClCompile>
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>

View File

@@ -21,6 +21,9 @@
<ClInclude Include="targetver.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="UnitTestConfig.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
@@ -29,5 +32,11 @@
<ClCompile Include="SiaCurlTests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="SiaApiTests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="SiaWalletApiTests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>