NetTunnelWindows/TestNetTunnelSDK/TestNetTunnelSDK.cpp

312 lines
12 KiB
C++

#include "pch.h"
#include "CppUnitTest.h"
#include "sccsdk.h"
#include <iostream>
#include <strsafe.h>
#include <cppcodec/base64_url_unpadded.hpp>
#include <cppcodec/base64_rfc4648.hpp>
#include <cppcodec/base64_url.hpp>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace TestNetTunnelSDK {
TEST_MODULE_INITIALIZE(ModuleInitialize) {
const TCHAR *path = TEXT(
"C:\\Users\\HuangXin\\Documents\\development\\visual_studio\\tunnel_windows\\NetTunnelApp\\bin\\Debug");
//const TCHAR* pSvrUsr = TEXT("https://112.17.28.201:1443");
const TCHAR *pSvrUsr = TEXT("https://112.17.28.200:1443");
//const TCHAR* pSvrUsr = TEXT("http://xajhuang.com:9276");
Logger::WriteMessage("In Module Initialize:\n");
Assert::AreEqual(0, TunnelSDKInitEnv(path, pSvrUsr, TEXT("./test/utest.log"), LOG_TRACE, false));
Assert::AreEqual(0, EnableVerifySignature(TEXT("sc-winvdisdk-efa9v12xwtz5eppr"), TEXT("lh5r8sw6m9m416nm")));
//Assert::AreEqual(0, EnableSCGProxy(true, TEXT("efc.xajhuang.com"), 10001));
}
TEST_MODULE_CLEANUP(ModuleCleanup) {
Logger::WriteMessage("In Module Cleanup\n");
TunnelSDKUnInit();
}
TEST_CLASS(TestNetTunnelSDK) {
public:
const int RET_OK = ERR_SUCCESS;
TEST_METHOD(TestRunPipeCmd) {
#if 0
TCHAR buf[1024];
int ret = ERR_SUCCESS;
DWORD retCode;
Assert::AreEqual(ret, RunCommand(TEXT("cmd.exe /C dir E:\\"), buf, 1024, &retCode));
Logger::WriteMessage("Return:");
Logger::WriteMessage(buf);
#endif
}
TEST_METHOD(TestGetAllNICInfo) {
PNIC_CONTENT pInfo = nullptr;
int size = 0;
Assert::AreEqual(RET_OK, GetAllNICInfo(&pInfo, &size));
Assert::IsNotNull(pInfo);
Assert::AreNotEqual(0, size);
//free(pInfo);
}
TEST_METHOD(TestSetInterfaceIpAddressWMI) {
const int ret = SetInterfaceIpAddressWMI(TEXT("huangxin"), TEXT("192.168.100.100"), TEXT("255.255.0.0"));
Assert::AreEqual(RET_OK, ret);
}
#if 0
TEST_METHOD(TestRemoveAllConnectSHaring) {
const int ret = RemoveAllConnectSHaring();
Assert::AreEqual(RET_OK, ret);
}
TEST_METHOD(TestVerifyInterfaceIpAddr) {
const int ret = VerifyInterfaceIpAddr(11, TEXT("10.10.10.251"), TEXT("255.255.255.0"), 2);
Assert::AreEqual(RET_OK, ret);
}
TEST_METHOD(TestSetInterfaceIpAddressFromCIDR) {
Assert::AreEqual(RET_OK, SetInterfaceIpAddressFromCIDR(TEXT("wg_server"), TEXT("192.168.100.250/24")));
}
TEST_METHOD(TestSetInterfaceIpAddress) {
Assert::AreEqual(RET_OK,
SetInterfaceIpAddress(TEXT("wg_server"), TEXT("192.168.101.250"), TEXT("255.255.0.0")));
}
TEST_METHOD(TestGetInterfaceIndexByName) {
int index;
Assert::AreEqual(RET_OK, GetInterfaceIndexByName(TEXT("wg_server"), &index));
}
TEST_METHOD(TestRemoveInterfaceIpAddress) {
Assert::AreEqual(RET_OK, RemoveInterfaceIpAddress(TEXT("wg_server")));
}
TEST_METHOD(TestGetWindowsHyperVStatus) {
int enable;
Assert::AreEqual(RET_OK, GetWindowsHyperVStatus(&enable));
Assert::AreEqual(TRUE, enable);
}
TEST_METHOD(TestInstallWindowsNATCommand) {
Assert::AreEqual(RET_OK, InstallWindowsNATCommand());
}
TEST_METHOD(TestIsInterfacePrivate) {
bool bRet;
Assert::AreNotEqual(RET_OK, IsInterfacePrivate(TEXT("wg_cli_123"), &bRet));
Assert::AreEqual(RET_OK, SetInterfacePrivate(TEXT("wg_cli"), true));
Assert::AreEqual(RET_OK, IsInterfacePrivate(TEXT("wg_cli"), &bRet));
Assert::AreEqual(true, bRet);
Assert::AreEqual(RET_OK, SetInterfacePrivate(TEXT("wg_cli"), false));
Assert::AreEqual(RET_OK, IsInterfacePrivate(TEXT("wg_cli"), &bRet));
Assert::AreEqual(false, bRet);
}
TEST_METHOD(TestSetInterfacePrivate) {
Assert::AreEqual(RET_OK, SetInterfacePrivate(TEXT("wg_cli_435"), true));
}
TEST_METHOD(TestWireGuardNetConnectionSharingEnable) {
Assert::AreEqual(RET_OK, WireGuardNetConnectionSharingEnable());
}
TEST_METHOD(TestCalcFileHash) {
TCHAR outHash[MAX_PATH] = {0};
Assert::AreEqual(RET_OK, CalcFileHash(HASH_SHA256, TEXT("E:\\wireguard.psm1"), outHash));
Assert::AreEqual(TEXT("3ef17a2eb23a148a3fbe14cb4ef38747e0a1e5037b6afa2350a9b18f617bb5db"), outHash);
}
TEST_METHOD(TestWireGuardInstallServerService) {
Assert::AreEqual(RET_OK, WireGuardInstallDefaultServerService(true));
}
TEST_METHOD(TestGetInternetConnectAdaptersIndex) {
bool bRet;
Assert::AreEqual(RET_OK, IsInternetConnectAdapter(9, &bRet));
Assert::AreEqual(true, bRet);
Assert::AreEqual(RET_OK, IsInternetConnectAdapter(44, &bRet));
Assert::AreEqual(false, bRet);
}
TEST_METHOD(TestGetNetConnectionNetworkCategory) {
bool chkStatus = false;
Assert::AreEqual(RET_OK, GetNetConnectionNetworkCategory(TEXT("admin"), &chkStatus));
}
TEST_METHOD(TestWaitNetAdapterConnected) {
Assert::AreEqual(RET_OK, WaitNetAdapterConnected(TEXT("admin"), 10 * 1000));
}
TEST_METHOD(TestProtoGetUserConfigure) {
//Assert::AreEqual(RET_OK, ProtoGetUserConfigure("admin", "asfdsafdladf083asldf+="));
}
TEST_METHOD(TestAddRouteTable) {
Assert::AreEqual(RET_OK, AddRouteTable(TEXT("1.1.1.1"), TEXT("255.255.255.0"), TEXT("10.10.10.1")));
}
TEST_METHOD(TestGetInterfaceGUIDByName) {
GUID guid;
Assert::AreEqual(RET_OK, GetInterfaceGUIDByName(TEXT("admin"), &guid));
Assert::AreNotEqual(0, static_cast<int>(guid.Data1));
//Assert::AreEqual(RET_OK, ProtoGetUserConfigure("admin", "asfdsafdladf083asldf+="));
}
TEST_METHOD(TestSetNATRule) {
Assert::AreEqual(RET_OK, SetNATRule(TEXT("admin"), TEXT("10.10.10.250/24")));
}
TEST_METHOD(TestRemoveNATRule) {
Assert::AreEqual(RET_OK, RemoveNATRule(TEXT("admin")));
}
TEST_METHOD(TestGetInterfaceIfIndexByIpAddr) {
ULONG index = -1;
Assert::AreEqual(RET_OK, GetInterfaceIfIndexByIpAddr(TEXT("10.10.10.1"), &index));
Assert::AreNotEqual(-1, static_cast<int>(index));
}
TEST_METHOD(TestGetWireGuradTunnelInfo) {
Assert::AreEqual(RET_OK, GetWireGuradTunnelInfo(TEXT("admin")));
//Assert::AreEqual(RET_OK, RemoteWireGuardControl(false));
//Assert::AreEqual(RET_OK, LocalWireGuardControl(false, false));
}
#endif
TEST_METHOD(TestIsWireGuardServerRunning) {
bool bRet;
Assert::AreEqual(RET_OK, IsWireGuardServerRunning(TEXT("tunnel_svr"), &bRet));
Assert::AreEqual(false, bRet);
}
TEST_METHOD(TestGetNetConnectionSharing) {
//bool chkStatus;
//Assert::AreEqual(RET_OK, GetNetIntelnetConnectionSharing(TEXT("admin"), &chkStatus));
//Assert::IsTrue(chkStatus);
}
TEST_METHOD(TestSetNetConnectionNetworkCategory) {
//Assert::AreEqual(RET_OK, SetNetConnectionNetworkCategory());
}
TEST_METHOD(TestBase64UrlEncode) {
const std::string src = "iTeBvsuhkSOIoLB/t8qC/dBOu1lhLWwIZ675nF37MwM=";
using base64urlunPad = cppcodec::base64_url_unpadded;
using base64url = cppcodec::base64_url;
using base64 = cppcodec::base64_rfc4648;
std::string base = base64::encode(src);
std::string baseurl = base64url::encode(src);
std::string baseunpad = base64urlunPad::encode(src);
//std::string dbase = base64::decode(base);
//std::cout << base64::encode(pSrc, lstrlen(pSrc)) << std::endl;
//std::string encode = ::encode(pSrc);
Logger::WriteMessage("[");
Logger::WriteMessage(base64::encode(src).c_str());
Logger::WriteMessage("]\n");
}
TEST_METHOD(TestUserLogin) {
PUSER_CLIENT_CONFIG pCfg;
Assert::AreEqual(
RET_OK,
GetUserClientConfigure(
TEXT("admin"),
TEXT("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9."
"eyJ1c2VyX25hbWUiOiIxNjk2NDA1MDY5MzQ1OTY4MTI4Iiwic2NvcGUiOlsiYWxsIl0sInVzZXJUeXBlIjoidXNlciIsImV4c"
"CI6MTY5NTg4OTk4MywiYXV0aG9yaXRpZXMiOlsiUk9MRV9SRVRBSU58MCJdLCJqdGkiOiJmTGlSaDMzTFI1b2stU01wcEFNdX"
"E2VV9nQjAiLCJhY2NvdW50IjoiaHVhbmd4aW4iLCJjbGllbnRfaWQiOiJzYy13aW52ZGlzZGstZWZhOXYxMnh3dHo1ZXBwciJ"
"9.nDJ1hbukk-mGj-gD64WGAWogR4UWgvuw7B-2GKDHCVL4uylA-NRp2aqgJAriluwPGYub4-RWpKyLTRGi-GtsduuHp_"
"hYKcjbzsxMx040-qkoG6trCbtV_UWr6tIKFTePRLBcsSSiH7D9nHxSgmbJiVay2_"
"Q85ddUurF3jfdZ3AnkfWCC2Y1tfqEInkwqWRFqb17hDjpKNr__ZPmB3aOpR3jjOGbz4HI4s0nevQgzYW9MEscW-"
"jtMws4i00jsHpS-dwfiybNB91JUsZT_g1BWQW0YPByS1Gt0SAsX4apB_k5xTDUNC-"
"GwHfaGpICv8CAAIlyzXtQSVwC7QSFmLtuctg"),
&pCfg));
//Assert::AreEqual(RET_OK, LocalWireGuardControl(true, true));
//Assert::AreEqual(RET_OK, LocalWireGuardControl(false, false));
}
TEST_METHOD(TestClientProgress) {
PUSER_CLIENT_CONFIG pCfg = nullptr;
Assert::AreEqual(RET_OK, GetUserClientConfigure(TEXT("admin"), TEXT("1323245235"), &pCfg));
Assert::IsNotNull(pCfg);
Assert::AreEqual(RET_OK, RemoteCtrlSvrCfgUserTunnel(347, TEXT("172.18.2.0/24")));
Assert::AreEqual(RET_OK, RemoteWireGuardControl(true));
Assert::AreEqual(RET_OK, LocalWireGuardControl(true, true));
//EnableSCGProxy(true, 10000);
//Sleep(30 * 1000);
Assert::AreEqual(RET_OK, LocalWireGuardControl(false, false));
Assert::AreEqual(RET_OK, RemoteWireGuardControl(false));
}
TEST_METHOD(TestClientProgressFree) {
PUSER_CLIENT_CONFIG pCfg = nullptr;
#if 0
Assert::AreEqual(
RET_OK,
GetUserClientConfigure(
TEXT("huangxin"),
TEXT("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9."
"eyJ1c2VyX25hbWUiOiIxNjk2NDA1MDY5MzQ1OTY4MTI4Iiwic2NvcGUiOlsiYWxsIl0sInVzZXJUeXBlIjoidXNlciIsImV4c"
"CI6MTY5NTg4OTk4MywiYXV0aG9yaXRpZXMiOlsiUk9MRV9SRVRBSU58MCJdLCJqdGkiOiJmTGlSaDMzTFI1b2stU01wcEFNdX"
"E2VV9nQjAiLCJhY2NvdW50IjoiaHVhbmd4aW4iLCJjbGllbnRfaWQiOiJzYy13aW52ZGlzZGstZWZhOXYxMnh3dHo1ZXBwciJ"
"9.nDJ1hbukk-mGj-gD64WGAWogR4UWgvuw7B-2GKDHCVL4uylA-NRp2aqgJAriluwPGYub4-RWpKyLTRGi-GtsduuHp_"
"hYKcjbzsxMx040-qkoG6trCbtV_UWr6tIKFTePRLBcsSSiH7D9nHxSgmbJiVay2_"
"Q85ddUurF3jfdZ3AnkfWCC2Y1tfqEInkwqWRFqb17hDjpKNr__ZPmB3aOpR3jjOGbz4HI4s0nevQgzYW9MEscW-"
"jtMws4i00jsHpS-dwfiybNB91JUsZT_g1BWQW0YPByS1Gt0SAsX4apB_k5xTDUNC-"
"GwHfaGpICv8CAAIlyzXtQSVwC7QSFmLtuctg"),
&pCfg));
#else
Assert::AreEqual(
RET_OK,
GetUserClientConfigure(
TEXT("huangxin"),
TEXT("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9."
"eyJ1c2VyX25hbWUiOiIxNjk2NDA1MDY5MzQ1OTY4MTI4Iiwic2NvcGUiOlsiYWxsIl0sInVzZXJUeXBlIjoidXNlciIsImV4c"
"CI6MTY5NjEyNzIwNiwiYXV0aG9yaXRpZXMiOlsiUk9MRV9SRVRBSU58MCJdLCJqdGkiOiJxX1J0Y2lzWVVwMTJUalhCTW5DUH"
"E0NjBwMVkiLCJhY2NvdW50IjoiaHVhbmd4aW4iLCJjbGllbnRfaWQiOiJzYy13aW52ZGlzZGstZWZhOXYxMnh3dHo1ZXBwciJ"
"9.CC3oTE4XFKXN_URENXBPD4bWGDfsvb3yvnT7adaiP0kpjOc6Q7XfOQQO68fRg5RwaPdtBbLWywby0ZhiUyh4LDh-"
"oDMyKaovyrX3ht7yHlLXpkgg8mdWWt_tTV9_DqPD_"
"dSWJLzjWLkCxvW9nWyfXtJflXN3TljuUBaXaUB8FR5dDqcxPJytIDM0pq0RsmYpK8O-kkdyaOGblTmb_"
"usj9yU8ZdMUQuMvywhl72_WaONiYeMwNtGvLvCdi0oRxGFwaOMDn4X29lZjvbsmEU12DwyuXPvX_"
"JHkj9Sg3O7k9DYMU0W1ucfw9Jlnxt-9oZ6inOM9shVlAPG1LbgP0WDGaA"),
&pCfg));
#endif
Assert::IsNotNull(pCfg);
Assert::AreEqual(RET_OK, LocalWireGuardControl(false, false));
//Assert::AreEqual(RET_OK, RemoteWireGuardControl(false));
}
TEST_METHOD(TestGetSDKErrorMessage) {
const CHAR *pErrStr = GetSDKErrorMessage(ERR_INPUT_PARAMS);
Assert::AreEqual(pErrStr, "ERR_INPUT_PARAMS");
}
};
}