From b61fefd11fa9594404feeac9faf4d10dca78d82d Mon Sep 17 00:00:00 2001 From: JDierkse Date: Wed, 10 Jun 2020 11:50:22 +0200 Subject: [PATCH] Add Tests --- .gitignore | 2 ++ Libraries/Utilities | 1 + Makefile.conf | 10 +++++-- Makefile.target | 7 ++++- Test/Makefile | 1 + Test/Test.cc | 24 +++++++++++++++ Test/Test.h | 7 +++++ Test/mDNSTest.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 120 insertions(+), 3 deletions(-) create mode 120000 Libraries/Utilities create mode 120000 Test/Makefile create mode 100644 Test/Test.cc create mode 100644 Test/Test.h create mode 100644 Test/mDNSTest.cpp diff --git a/.gitignore b/.gitignore index 851ef09..0b9ff8b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ .*.swp .AppleDouble lib +test* +!test.cc diff --git a/Libraries/Utilities b/Libraries/Utilities new file mode 120000 index 0000000..4a6b454 --- /dev/null +++ b/Libraries/Utilities @@ -0,0 +1 @@ +../../Utilities \ No newline at end of file diff --git a/Makefile.conf b/Makefile.conf index 81ee11a..6acc881 100644 --- a/Makefile.conf +++ b/Makefile.conf @@ -3,8 +3,12 @@ # LFLAGS += -lLogging -LFLAGS += -L$(ROOTPATH)/lib/$(ARCH) -CFLAGS += -I$(ROOTPATH) -I$(ROOTPATH)/include +LFLAGS += -L$(ROOTPATH)/Libraries/Logging/lib/$(ARCH) +CFLAGS += -I$(ROOTPATH)/Libraries/Logging/include + +LFLAGS += -lUtilities +LFLAGS += -L$(ROOTPATH)/Libraries/Utilities/lib/$(ARCH) +CFLAGS += -I$(ROOTPATH)/Libraries/Utilities/include CFLAGS += -I$(ROOTPATH)/Libraries/asio/include @@ -12,4 +16,6 @@ LFLAGS += -lNetwork LFLAGS += -L$(ROOTPATH)/lib/$(ARCH) CFLAGS += -I$(ROOTPATH) -I$(ROOTPATH)/include +LFLAGS += -pthread + DEBUGDIR := .debug diff --git a/Makefile.target b/Makefile.target index 82c334b..1fa3df2 100644 --- a/Makefile.target +++ b/Makefile.target @@ -8,6 +8,11 @@ Network.a.$(ARCH) : $(OBJECTS) Network.a: $(call build_target,$@) +test.$(ARCH): $(OBJECTS) Test/Test.o.$(ARCH) | Network.a.$(ARCH) + $(call build_target_arch,$@,$^) +test: + $(call build_target,$@) + .DEFAULT_GOAL := Network.a -TARGETS += Network.a +TARGETS += Network.a test diff --git a/Test/Makefile b/Test/Makefile new file mode 120000 index 0000000..d0b0e8e --- /dev/null +++ b/Test/Makefile @@ -0,0 +1 @@ +../Makefile \ No newline at end of file diff --git a/Test/Test.cc b/Test/Test.cc new file mode 100644 index 0000000..9bf3d44 --- /dev/null +++ b/Test/Test.cc @@ -0,0 +1,24 @@ +#include "Test.h" +#include + + +int main(int /*argc*/, char** /*argv*/) +{ + Logging::OpenLog(); + Logging::SetLogMask(Logging::Severity::Info); + + if ( + Network::Test::TestMulticastDNS() + ) + { + Logging::Log(Logging::Severity::Info, "Pass"); + Logging::CloseLog(); + + return 0; + } + + Logging::Log(Logging::Severity::Info, "Error"); + Logging::CloseLog(); + + return -1; +} diff --git a/Test/Test.h b/Test/Test.h new file mode 100644 index 0000000..049b143 --- /dev/null +++ b/Test/Test.h @@ -0,0 +1,7 @@ +namespace Network { +namespace Test { + +bool TestMulticastDNS(); + +} // namespace Test +} // namespace Network diff --git a/Test/mDNSTest.cpp b/Test/mDNSTest.cpp new file mode 100644 index 0000000..db543dd --- /dev/null +++ b/Test/mDNSTest.cpp @@ -0,0 +1,71 @@ +#include "Dns/MulticastDnsClient.h" +#include +#include +#include +#include + + +namespace Network { +namespace Test { + +class Test +{ +public: + Test() : + m_client(std::bind(&Test::Callback, this, std::placeholders::_1)) + { + Network::Dns::Query query; + query.SetType(Network::Dns::QueryType::A); + query.SetClass(Network::Dns::QueryClass::IN); + query.SetName(Network::Dns::Name("_http", "_tcp", "local")); + + m_client.Query(query); + } + + void Callback(const std::vector records) + { + Logging::Log(Logging::Severity::Info, "Callback"); + for(auto& record : records) + { + if(record.GetType() == Network::Dns::Type::A) + { + std::vector data = record.GetResourceData(); + + std::stringstream ss; + ss << static_cast(data.at(0)) << "." << static_cast(data.at(1)) << "." << static_cast(data.at(2)) << "." << static_cast(data.at(3)); + asio::ip::address_v4 address = asio::ip::address_v4::from_string(ss.str()); + + if (std::find(m_addresses.begin(), m_addresses.end(), address) == m_addresses.end()) + m_addresses.push_back(address); + } + } + } + + void Print() + { + std::stringstream ss; + ss << m_addresses.size() << " IPs" << std::endl; + for (auto& address : m_addresses) + ss << " " << address.to_string() << std::endl; + Logging::Log(Logging::Severity::Info, ss.str()); + } + +private: + Network::Dns::MulticastDnsClient m_client; + std::vector m_addresses; +}; + +bool TestMulticastDNS() +{ + Test test; + test.Print(); + + sleep(4); + + test.Print(); + + return true; +} + +} // namespace Test +} // namespace Network