diff --git a/3rd-party/Sia-v1.1.0-windows-amd64/01 - Run Local.lnk b/3rd-party/Sia-v1.1.0-windows-amd64/01 - Run Local.lnk deleted file mode 100644 index 0221620..0000000 Binary files a/3rd-party/Sia-v1.1.0-windows-amd64/01 - Run Local.lnk and /dev/null differ diff --git a/3rd-party/Sia-v1.1.0-windows-amd64/01 - siad test.cmd b/3rd-party/Sia-v1.1.0-windows-amd64/01 - siad test.cmd new file mode 100644 index 0000000..3731db1 --- /dev/null +++ b/3rd-party/Sia-v1.1.0-windows-amd64/01 - siad test.cmd @@ -0,0 +1,5 @@ +@echo off +pushd "%~dp0%" + +del /s /q ..\..\UnitTests\data 1>NUL 2>&1 +siad -d ..\..\UnitTests\data \ No newline at end of file diff --git a/SiaDrive.Api/SiaApi.h b/SiaDrive.Api/SiaApi.h index c473607..ee386a2 100644 --- a/SiaDrive.Api/SiaApi.h +++ b/SiaDrive.Api/SiaApi.h @@ -17,7 +17,7 @@ public: WalletNotCreated }; - class _CSiaWallet + class AFX_EXT_CLASS _CSiaWallet { friend CSiaApi; private: diff --git a/SiaDrive.Api/SiaCurl.cpp b/SiaDrive.Api/SiaCurl.cpp index 08b7aa5..73ab744 100644 --- a/SiaDrive.Api/SiaCurl.cpp +++ b/SiaDrive.Api/SiaCurl.cpp @@ -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(); + 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; diff --git a/SiaDrive.Api/SiaCurl.h b/SiaDrive.Api/SiaCurl.h index a1d4fc4..1250467 100644 --- a/SiaDrive.Api/SiaCurl.h +++ b/SiaDrive.Api/SiaCurl.h @@ -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; diff --git a/SiaDrive.Api/SiaWallet.cpp b/SiaDrive.Api/SiaWallet.cpp index 78290e3..1f0be44 100644 --- a/SiaDrive.Api/SiaWallet.cpp +++ b/SiaDrive.Api/SiaWallet.cpp @@ -18,7 +18,7 @@ CSiaApi::_CSiaWallet::~_CSiaWallet() SiaApiError CSiaApi::_CSiaWallet::Create(String& seed) { - SiaApiError error = SiaApiError::Success; + SiaApiError error = SiaApiError::NotImplemented; return error; } diff --git a/UnitTests/SiaApiTests.cpp b/UnitTests/SiaApiTests.cpp new file mode 100644 index 0000000..531b613 --- /dev/null +++ b/UnitTests/SiaApiTests.cpp @@ -0,0 +1,22 @@ +#include "stdafx.h" +#include "CppUnitTest.h" +#include +#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); + } + }; +} \ No newline at end of file diff --git a/UnitTests/SiaCurlTests.cpp b/UnitTests/SiaCurlTests.cpp index dad5d26..5ee1d95 100644 --- a/UnitTests/SiaCurlTests.cpp +++ b/UnitTests/SiaCurlTests.cpp @@ -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); + } + }; } \ No newline at end of file diff --git a/UnitTests/SiaWalletApiTests.cpp b/UnitTests/SiaWalletApiTests.cpp new file mode 100644 index 0000000..5250f51 --- /dev/null +++ b/UnitTests/SiaWalletApiTests.cpp @@ -0,0 +1,31 @@ +#include "stdafx.h" +#include "CppUnitTest.h" +#include +#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))); + } + }; +} \ No newline at end of file diff --git a/UnitTests/UnitTestConfig.h b/UnitTests/UnitTestConfig.h new file mode 100644 index 0000000..fce131e --- /dev/null +++ b/UnitTests/UnitTestConfig.h @@ -0,0 +1,4 @@ +#pragma once + +#define TEST_SERVER_VERSION L"1.1.0" +using namespace Sia::Api; \ No newline at end of file diff --git a/UnitTests/UnitTests.vcxproj b/UnitTests/UnitTests.vcxproj index 22c0bc5..585566b 100644 --- a/UnitTests/UnitTests.vcxproj +++ b/UnitTests/UnitTests.vcxproj @@ -156,8 +156,17 @@ + + + + + + + + + Create Create diff --git a/UnitTests/UnitTests.vcxproj.filters b/UnitTests/UnitTests.vcxproj.filters index 4b53022..6ea879a 100644 --- a/UnitTests/UnitTests.vcxproj.filters +++ b/UnitTests/UnitTests.vcxproj.filters @@ -21,6 +21,9 @@ Header Files + + Header Files + @@ -29,5 +32,11 @@ Source Files + + Source Files + + + Source Files + \ No newline at end of file