diff --git a/.vscode/tasks.json b/.vscode/tasks.json index bf6c999..9f00615 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -26,10 +26,7 @@ "USE_GPSD=1", "DStarGateway/dstargateway" ], - "group": { - "kind": "build", - "isDefault": true - }, + "group": "build", "problemMatcher": [] }, { @@ -94,7 +91,10 @@ "ENABLE_DEBUG=1", "USE_GPSD=1" ], - "group": "build", + "group": { + "kind": "build", + "isDefault": true + }, "problemMatcher": [] } ] diff --git a/Tests/HostsFilesManager/UpdateHosts.cpp b/Tests/HostsFilesManager/UpdateHosts.cpp new file mode 100644 index 0000000..91d399f --- /dev/null +++ b/Tests/HostsFilesManager/UpdateHosts.cpp @@ -0,0 +1,207 @@ +/* + * Copyright (C) 2021-2024 by Geoffrey Merck F4FXL / KC3FRA + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include +#include + +#include "HostsFilesManager.h" +#include "CacheManager.h" +#include "DStarDefines.h" + +namespace HostsFilesManagerTests +{ + class HostsFilesManager_UpdateHosts : public ::testing::Test + { + protected: + std::string m_internetPath; + std::string m_customPath; + CCacheManager * m_cache; + + HostsFilesManager_UpdateHosts() : + m_internetPath(), + m_customPath(), + m_cache(nullptr) + { + + } + + + void SetUp() override + { + char buf[2048]; + auto size = ::readlink("/proc/self/exe", buf, 2048); + if(size > 0) { + std::string path(buf, size); + auto lastSlashPos = path.find_last_of('/'); + path.resize(lastSlashPos); + + m_internetPath = path + "/HostsFilesManager/internet/"; + m_customPath = path + "/HostsFilesManager/custom/"; + } + + CHostsFilesManager::setDCS(false, ""); + CHostsFilesManager::setDextra(false, ""); + CHostsFilesManager::setXLX(false, ""); + CHostsFilesManager::setDPlus(false, ""); + + if(m_cache != nullptr) delete m_cache; + m_cache = new CCacheManager(); + } + + static bool dummyDownload(const std::string & a, const std::string & b) + { + std::cout << a << std::endl << b; + return true; + } + }; + + TEST_F(HostsFilesManager_UpdateHosts, DExtraFromInternet) + { + CHostsFilesManager::setDextra(true, "files are actually not downloaded, we want to to check that DEXtra Hosts are stored as DExtra"); + CHostsFilesManager::setHostFilesDirectories(m_internetPath, "specify invalid custom path as we do not want to override hosts from the internet"); + CHostsFilesManager::setDownloadCallback(HostsFilesManager_UpdateHosts::dummyDownload); + CHostsFilesManager::setCache(m_cache); + + CHostsFilesManager::UpdateHosts(); + auto gw = m_cache->findGateway("XRF123 G"); + + EXPECT_NE(gw, nullptr) << "DExtra host not found"; + EXPECT_STREQ(gw->getGateway().c_str(), "XRF123 G"); + EXPECT_EQ(gw->getAddress().s_addr, ::inet_addr("1.1.1.1")) << "Address missmatch"; + EXPECT_EQ(gw->getProtocol(), DP_DEXTRA) << "Protocol mismatch"; + } + + + TEST_F(HostsFilesManager_UpdateHosts, DExtraFromInternetCustomOverride) + { + CHostsFilesManager::setDextra(true, "files are actually not downloaded, we want to to check that DEXtra Hosts are stored as DExtra"); + CHostsFilesManager::setHostFilesDirectories(m_internetPath, m_customPath); + CHostsFilesManager::setDownloadCallback(HostsFilesManager_UpdateHosts::dummyDownload); + CHostsFilesManager::setCache(m_cache); + + CHostsFilesManager::UpdateHosts(); + auto gw = m_cache->findGateway("XRF123 G"); + + EXPECT_NE(gw, nullptr) << "DExtra host not found"; + EXPECT_STREQ(gw->getGateway().c_str(), "XRF123 G"); + EXPECT_EQ(gw->getAddress().s_addr, ::inet_addr("2.2.2.2")) << "Address missmatch"; + EXPECT_EQ(gw->getProtocol(), DP_DEXTRA) << "Protocol mismatch"; + } + + TEST_F(HostsFilesManager_UpdateHosts, DCSFromInternet) + { + CHostsFilesManager::setDCS(true, "files are actually not downloaded, we want to to check that DEXtra Hosts are stored as DExtra"); + CHostsFilesManager::setHostFilesDirectories(m_internetPath, "specify invalid custom path as we do not want to override hosts from the internet"); + CHostsFilesManager::setDownloadCallback(HostsFilesManager_UpdateHosts::dummyDownload); + CHostsFilesManager::setCache(m_cache); + + CHostsFilesManager::UpdateHosts(); + auto gw = m_cache->findGateway("DCS123 G"); + + EXPECT_NE(gw, nullptr) << "DCS host not found"; + EXPECT_STREQ(gw->getGateway().c_str(), "DCS123 G"); + EXPECT_EQ(gw->getAddress().s_addr, ::inet_addr("1.1.1.1")) << "Address missmatch"; + EXPECT_EQ(gw->getProtocol(), DP_DCS) << "Protocol mismatch"; + } + + + TEST_F(HostsFilesManager_UpdateHosts, DCSFromInternetCustomOverride) + { + CHostsFilesManager::setDCS(true, "files are actually not downloaded, we want to to check that DEXtra Hosts are stored as DExtra"); + CHostsFilesManager::setHostFilesDirectories(m_internetPath, m_customPath); + CHostsFilesManager::setDownloadCallback(HostsFilesManager_UpdateHosts::dummyDownload); + CHostsFilesManager::setCache(m_cache); + + CHostsFilesManager::UpdateHosts(); + auto gw = m_cache->findGateway("DCS123 G"); + + EXPECT_NE(gw, nullptr) << "DCS host not found"; + EXPECT_STREQ(gw->getGateway().c_str(), "DCS123 G"); + EXPECT_EQ(gw->getAddress().s_addr, ::inet_addr("2.2.2.2")) << "Address missmatch"; + EXPECT_EQ(gw->getProtocol(), DP_DCS) << "Protocol mismatch"; + } + + TEST_F(HostsFilesManager_UpdateHosts, DPlusFromInternet) + { + CHostsFilesManager::setDPlus(true, "files are actually not downloaded, we want to to check that DEXtra Hosts are stored as DExtra"); + CHostsFilesManager::setHostFilesDirectories(m_internetPath, "specify invalid custom path as we do not want to override hosts from the internet"); + CHostsFilesManager::setDownloadCallback(HostsFilesManager_UpdateHosts::dummyDownload); + CHostsFilesManager::setCache(m_cache); + + CHostsFilesManager::UpdateHosts(); + auto gw = m_cache->findGateway("REF123 G"); + + EXPECT_NE(gw, nullptr) << "DPlus host not found"; + EXPECT_STREQ(gw->getGateway().c_str(), "REF123 G"); + EXPECT_EQ(gw->getAddress().s_addr, ::inet_addr("1.1.1.1")) << "Address missmatch"; + EXPECT_EQ(gw->getProtocol(), DP_DPLUS) << "Protocol mismatch"; + } + + + TEST_F(HostsFilesManager_UpdateHosts, DPlusFromInternetCustomOverride) + { + CHostsFilesManager::setDPlus(true, "files are actually not downloaded, we want to to check that DEXtra Hosts are stored as DExtra"); + CHostsFilesManager::setHostFilesDirectories(m_internetPath, m_customPath); + CHostsFilesManager::setDownloadCallback(HostsFilesManager_UpdateHosts::dummyDownload); + CHostsFilesManager::setCache(m_cache); + + CHostsFilesManager::UpdateHosts(); + auto gw = m_cache->findGateway("REF123 G"); + + EXPECT_NE(gw, nullptr) << "DPlus host not found"; + EXPECT_STREQ(gw->getGateway().c_str(), "REF123 G"); + EXPECT_EQ(gw->getAddress().s_addr, ::inet_addr("2.2.2.2")) << "Address missmatch"; + EXPECT_EQ(gw->getProtocol(), DP_DPLUS) << "Protocol mismatch"; + } + + TEST_F(HostsFilesManager_UpdateHosts, XLXFromInternet) + { + CHostsFilesManager::setXLX(true, "files are actually not downloaded, we want to to check that DEXtra Hosts are stored as DExtra"); + CHostsFilesManager::setHostFilesDirectories(m_internetPath, "specify invalid custom path as we do not want to override hosts from the internet"); + CHostsFilesManager::setDownloadCallback(HostsFilesManager_UpdateHosts::dummyDownload); + CHostsFilesManager::setCache(m_cache); + + CHostsFilesManager::UpdateHosts(); + auto gw = m_cache->findGateway("XLX123 G"); + + EXPECT_NE(gw, nullptr) << "XLX host not found"; + EXPECT_STREQ(gw->getGateway().c_str(), "XLX123 G"); + EXPECT_EQ(gw->getAddress().s_addr, ::inet_addr("1.1.1.1")) << "Address missmatch"; + EXPECT_EQ(gw->getProtocol(), DP_DCS) << "Protocol mismatch"; + } + + + TEST_F(HostsFilesManager_UpdateHosts, XLXFromInternetCustomOverride) + { + CHostsFilesManager::setXLX(true, "files are actually not downloaded, we want to to check that DEXtra Hosts are stored as DExtra"); + CHostsFilesManager::setHostFilesDirectories(m_internetPath, m_customPath); + CHostsFilesManager::setDownloadCallback(HostsFilesManager_UpdateHosts::dummyDownload); + CHostsFilesManager::setCache(m_cache); + + CHostsFilesManager::UpdateHosts(); + auto gw = m_cache->findGateway("XLX123 G"); + + EXPECT_NE(gw, nullptr) << "XLX host not found"; + EXPECT_STREQ(gw->getGateway().c_str(), "XLX123 G"); + EXPECT_EQ(gw->getAddress().s_addr, ::inet_addr("2.2.2.2")) << "Address missmatch"; + EXPECT_EQ(gw->getProtocol(), DP_DCS) << "Protocol mismatch"; + } +} \ No newline at end of file diff --git a/Tests/HostsFilesManager/custom/DCS_Hosts.txt b/Tests/HostsFilesManager/custom/DCS_Hosts.txt new file mode 100644 index 0000000..5612ca5 --- /dev/null +++ b/Tests/HostsFilesManager/custom/DCS_Hosts.txt @@ -0,0 +1 @@ +DCS123 2.2.2.2 \ No newline at end of file diff --git a/Tests/HostsFilesManager/custom/DExtra_Hosts.txt b/Tests/HostsFilesManager/custom/DExtra_Hosts.txt new file mode 100644 index 0000000..9e68e88 --- /dev/null +++ b/Tests/HostsFilesManager/custom/DExtra_Hosts.txt @@ -0,0 +1 @@ +XRF123 2.2.2.2 \ No newline at end of file diff --git a/Tests/HostsFilesManager/custom/DPlus_Hosts.txt b/Tests/HostsFilesManager/custom/DPlus_Hosts.txt new file mode 100644 index 0000000..b8a56a7 --- /dev/null +++ b/Tests/HostsFilesManager/custom/DPlus_Hosts.txt @@ -0,0 +1 @@ +REF123 2.2.2.2 \ No newline at end of file diff --git a/Tests/HostsFilesManager/custom/XLX_Hosts.txt b/Tests/HostsFilesManager/custom/XLX_Hosts.txt new file mode 100644 index 0000000..d65056c --- /dev/null +++ b/Tests/HostsFilesManager/custom/XLX_Hosts.txt @@ -0,0 +1 @@ +XLX123 2.2.2.2 \ No newline at end of file diff --git a/Tests/HostsFilesManager/internet/DCS_Hosts.txt b/Tests/HostsFilesManager/internet/DCS_Hosts.txt new file mode 100644 index 0000000..3bae50b --- /dev/null +++ b/Tests/HostsFilesManager/internet/DCS_Hosts.txt @@ -0,0 +1 @@ +DCS123 1.1.1.1 \ No newline at end of file diff --git a/Tests/HostsFilesManager/internet/DExtra_Hosts.txt b/Tests/HostsFilesManager/internet/DExtra_Hosts.txt new file mode 100644 index 0000000..7ec1fbf --- /dev/null +++ b/Tests/HostsFilesManager/internet/DExtra_Hosts.txt @@ -0,0 +1 @@ +XRF123 1.1.1.1 \ No newline at end of file diff --git a/Tests/HostsFilesManager/internet/DPlus_Hosts.txt b/Tests/HostsFilesManager/internet/DPlus_Hosts.txt new file mode 100644 index 0000000..3a1a484 --- /dev/null +++ b/Tests/HostsFilesManager/internet/DPlus_Hosts.txt @@ -0,0 +1 @@ +REF123 1.1.1.1 \ No newline at end of file diff --git a/Tests/HostsFilesManager/internet/XLX_Hosts.txt b/Tests/HostsFilesManager/internet/XLX_Hosts.txt new file mode 100644 index 0000000..1d2d8e5 --- /dev/null +++ b/Tests/HostsFilesManager/internet/XLX_Hosts.txt @@ -0,0 +1 @@ +XLX123 1.1.1.1 \ No newline at end of file