diff --git a/src/common/wifi/apmanager/include/microsoft/net/wifi/AccessPointManager.hxx b/src/common/wifi/apmanager/include/microsoft/net/wifi/AccessPointManager.hxx index 974b4748..8bffe621 100644 --- a/src/common/wifi/apmanager/include/microsoft/net/wifi/AccessPointManager.hxx +++ b/src/common/wifi/apmanager/include/microsoft/net/wifi/AccessPointManager.hxx @@ -74,7 +74,7 @@ public: std::vector> GetAllAccessPoints() const; - ~AccessPointManager() = default; + virtual ~AccessPointManager() = default; AccessPointManager(const AccessPointManager&) = delete; AccessPointManager(AccessPointManager&&) = delete; AccessPointManager& @@ -85,12 +85,9 @@ public: protected: /** * @brief Construct a new AccessPointManager object. - * - * @param accessPointFactory */ AccessPointManager() = default; -private: /** * @brief Callback function for all access point agent presence change events. * @@ -106,7 +103,7 @@ private: * * @param accessPoint The access point to add. */ - void + virtual void AddAccessPoint(std::shared_ptr accessPoint); /** @@ -114,7 +111,7 @@ private: * * @param accessPoint The access point to remove. */ - void + virtual void RemoveAccessPoint(std::shared_ptr accessPoint); private: diff --git a/src/common/wifi/core/AccessPoint.cxx b/src/common/wifi/core/AccessPoint.cxx index 3a97faf2..3b0d2591 100644 --- a/src/common/wifi/core/AccessPoint.cxx +++ b/src/common/wifi/core/AccessPoint.cxx @@ -11,7 +11,7 @@ AccessPoint::AccessPoint(std::string_view interfaceName, std::shared_ptr #include -#include #include TEST_CASE("Create AccessPointDiscoveryAgentOperationsNetlink", "[wifi][core][apmanager]") diff --git a/tests/unit/wifi/core/TestAccessPoint.cxx b/tests/unit/wifi/core/TestAccessPoint.cxx index f62b7f15..3628112f 100644 --- a/tests/unit/wifi/core/TestAccessPoint.cxx +++ b/tests/unit/wifi/core/TestAccessPoint.cxx @@ -6,7 +6,6 @@ #include #include #include -#include namespace Microsoft::Net::Wifi::Test { diff --git a/tests/unit/wifi/helpers/AccessPointControllerTest.cxx b/tests/unit/wifi/helpers/AccessPointControllerTest.cxx index 1442a12f..c5c2a5a1 100644 --- a/tests/unit/wifi/helpers/AccessPointControllerTest.cxx +++ b/tests/unit/wifi/helpers/AccessPointControllerTest.cxx @@ -1,33 +1,56 @@ +#include + #include +#include using namespace Microsoft::Net::Wifi; using namespace Microsoft::Net::Wifi::Test; -AccessPointControllerTest::AccessPointControllerTest(std::string_view interfaceName) : - InterfaceName(interfaceName) +AccessPointControllerTest::AccessPointControllerTest(AccessPointTest *accessPoint) : + AccessPoint(accessPoint) {} std::string_view -AccessPointControllerTest::GetInterfaceName() const noexcept +AccessPointControllerTest::GetInterfaceName() const { - return InterfaceName; + if (AccessPoint == nullptr) { + throw std::runtime_error("AccessPointControllerTest::GetInterfaceName called with null AccessPoint"); + } + + return AccessPoint->InterfaceName; } bool AccessPointControllerTest::GetIsEnabled() { - return IsEnabled; + if (AccessPoint == nullptr) { + throw std::runtime_error("AccessPointControllerTest::GetIsEnabled called with null AccessPoint"); + } + + return AccessPoint->IsEnabled; } Ieee80211AccessPointCapabilities AccessPointControllerTest::GetCapabilities() { - return {}; // TODO: return something + if (AccessPoint == nullptr) { + throw std::runtime_error("AccessPointControllerTest::GetCapabilities called with null AccessPoint"); + } + + return AccessPoint->Capabilities; } +AccessPointControllerFactoryTest::AccessPointControllerFactoryTest(AccessPointTest *accessPoint) : + AccessPoint(accessPoint) +{} + std::unique_ptr AccessPointControllerFactoryTest::Create(std::string_view interfaceName) { - return std::make_unique(interfaceName); + if (AccessPoint != nullptr && interfaceName != AccessPoint->InterfaceName) { + throw std::runtime_error("AccessPointControllerFactoryTest::Create called with unexpected interface name"); + } + + return std::make_unique(AccessPoint); } diff --git a/tests/unit/wifi/helpers/AccessPointManagerTest.cxx b/tests/unit/wifi/helpers/AccessPointManagerTest.cxx new file mode 100644 index 00000000..f4a28abc --- /dev/null +++ b/tests/unit/wifi/helpers/AccessPointManagerTest.cxx @@ -0,0 +1,16 @@ + +#include + +using namespace Microsoft::Net::Wifi::Test; + +void +AccessPointManagerTest::AddAccessPoint(std::shared_ptr accessPoint) +{ + return AccessPointManager::AddAccessPoint(std::move(accessPoint)); +} + +void +AccessPointManagerTest::RemoveAccessPoint(std::shared_ptr accessPoint) +{ + return AccessPointManager::RemoveAccessPoint(std::move(accessPoint)); +} diff --git a/tests/unit/wifi/helpers/AccessPointTest.cxx b/tests/unit/wifi/helpers/AccessPointTest.cxx index a7cf928e..0303ef46 100644 --- a/tests/unit/wifi/helpers/AccessPointTest.cxx +++ b/tests/unit/wifi/helpers/AccessPointTest.cxx @@ -1,16 +1,21 @@ -#include #include +#include using namespace Microsoft::Net::Wifi; using namespace Microsoft::Net::Wifi::Test; AccessPointTest::AccessPointTest(std::string_view interfaceName) : - InterfaceName(interfaceName) + AccessPointTest(interfaceName, Ieee80211AccessPointCapabilities{}) +{} + +AccessPointTest::AccessPointTest(std::string_view interfaceName, Microsoft::Net::Wifi::Ieee80211AccessPointCapabilities capabilities) : + InterfaceName(interfaceName), + Capabilities(capabilities) {} std::string_view -AccessPointTest::GetInterfaceName() const noexcept +AccessPointTest::GetInterfaceName() const { return InterfaceName; } @@ -18,7 +23,7 @@ AccessPointTest::GetInterfaceName() const noexcept std::unique_ptr AccessPointTest::CreateController() { - return std::make_unique(InterfaceName); + return std::make_unique(this); } std::shared_ptr diff --git a/tests/unit/wifi/helpers/CMakeLists.txt b/tests/unit/wifi/helpers/CMakeLists.txt index ab892758..d54b054b 100644 --- a/tests/unit/wifi/helpers/CMakeLists.txt +++ b/tests/unit/wifi/helpers/CMakeLists.txt @@ -8,12 +8,14 @@ set(WIFI_TEST_HELPERS_PUBLIC_INCLUDE_PREFIX ${WIFI_TEST_HELPERS_PUBLIC_INCLUDE}/ target_sources(wifi-test-helpers PRIVATE AccessPointControllerTest.cxx + AccessPointManagerTest.cxx AccessPointTest.cxx PUBLIC FILE_SET HEADERS BASE_DIRS ${WIFI_TEST_HELPERS_PUBLIC_INCLUDE} FILES ${WIFI_TEST_HELPERS_PUBLIC_INCLUDE_PREFIX}/AccessPointControllerTest.hxx + ${WIFI_TEST_HELPERS_PUBLIC_INCLUDE_PREFIX}/AccessPointManagerTest.hxx ${WIFI_TEST_HELPERS_PUBLIC_INCLUDE_PREFIX}/AccessPointTest.hxx ) @@ -26,5 +28,6 @@ target_include_directories(wifi-test-helpers target_link_libraries(wifi-test-helpers PUBLIC + wifi-apmanager wifi-core ) diff --git a/tests/unit/wifi/helpers/include/microsoft/net/wifi/test/AccessPointControllerTest.hxx b/tests/unit/wifi/helpers/include/microsoft/net/wifi/test/AccessPointControllerTest.hxx index 51e70233..25a57c89 100644 --- a/tests/unit/wifi/helpers/include/microsoft/net/wifi/test/AccessPointControllerTest.hxx +++ b/tests/unit/wifi/helpers/include/microsoft/net/wifi/test/AccessPointControllerTest.hxx @@ -9,27 +9,84 @@ namespace Microsoft::Net::Wifi::Test { +struct AccessPointTest; + +/** + * @brief IAccessPointController implementation for testing purposes. + * + * This implementation takes an AccessPointTest object which it uses to implement the IAccessPointController interface. + * The owner of this class must ensure that the passes AccessPointTest* remains valid for the lifetime of this object. + */ struct AccessPointControllerTest final : - public IAccessPointController + public Microsoft::Net::Wifi::IAccessPointController { - AccessPointControllerTest(std::string_view interfaceName); + AccessPointTest *AccessPoint{ nullptr }; + + /** + * @brief Construct a new AccessPointControllerTest object that uses the specified AccessPointTest to implement the + * IAccessPointController interface. + * + * @param accessPoint The access point to use. + */ + AccessPointControllerTest(AccessPointTest *accessPoint); + /** + * @brief Get the interface name associated with this controller. + * + * @return std::string_view + */ virtual std::string_view - GetInterfaceName() const noexcept override; + GetInterfaceName() const override; + /** + * @brief Get whether the access point is enabled. + * + * @return true + * @return false + */ virtual bool GetIsEnabled() override; + /** + * @brief Get the capabilities of the access point. + * + * @return Ieee80211AccessPointCapabilities + */ virtual Ieee80211AccessPointCapabilities GetCapabilities() override; - - std::string InterfaceName; - bool IsEnabled{ false }; }; +/** + * @brief IAccessPointControllerFactory implementation for testing purposes. + */ struct AccessPointControllerFactoryTest final : public Microsoft::Net::Wifi::IAccessPointControllerFactory { + AccessPointTest *AccessPoint{ nullptr }; + + /** + * @brief Construct a new AccessPointControllerFactoryTest object with no AccessPointTest parent/owner. This may + * only be used for cases where the constructed object won't actually be used (eg. unit tests for other components + * that require an IAccessPointControllerFactory, but the test doesn't trigger any functionality to use it). + * + * This should be contrained to testing creation/destruction of other objects that use this only. + */ + AccessPointControllerFactoryTest() = default; + + /** + * @brief Construct a new AccessPointControllerFactoryTest object. The owner of this object must ensure that the passed AccessPointTest remains valid for the lifetime of this object. + * + * @param accessPoint The access point to create controllers for. + */ + AccessPointControllerFactoryTest(AccessPointTest *accessPoint); + + /** + * @brief Create a new instance that can control the access point. + * + * @param interfaceName The name of the interface. If this factory was created with an AccessPointTest, this must + * match the interface name of the AccessPoint the object or else a std::runtime_error will be thrown. was created + * @return std::unique_ptr + */ std::unique_ptr Create(std::string_view interfaceName) override; }; diff --git a/tests/unit/wifi/helpers/include/microsoft/net/wifi/test/AccessPointManagerTest.hxx b/tests/unit/wifi/helpers/include/microsoft/net/wifi/test/AccessPointManagerTest.hxx new file mode 100644 index 00000000..b5183536 --- /dev/null +++ b/tests/unit/wifi/helpers/include/microsoft/net/wifi/test/AccessPointManagerTest.hxx @@ -0,0 +1,42 @@ + +#ifndef ACCESS_POINT_MANAGER_TEST +#define ACCESS_POINT_MANAGER_TEST + +#include + +#include +#include + +namespace Microsoft::Net::Wifi::Test +{ +/** + * @brief AccessPointManager to be used in tests, allowing access to protected methods to add/remove known access + * points. + */ +struct AccessPointManagerTest : + public Microsoft::Net::Wifi::AccessPointManager +{ + /** + * @brief Construct a new AccessPointManagerTest object. + */ + AccessPointManagerTest() = default; + + /** + * @brief Adds a new access point. + * + * @param accessPoint The access point to add. + */ + void + AddAccessPoint(std::shared_ptr accessPoint) override; + + /** + * @brief Removes an existing access point from use. + * + * @param accessPoint The access point to remove. + */ + void + RemoveAccessPoint(std::shared_ptr accessPoint) override; +}; +} // namespace Microsoft::Net::Wifi::Test + +#endif // ACCESS_POINT_MANAGER_TEST diff --git a/tests/unit/wifi/helpers/include/microsoft/net/wifi/test/AccessPointTest.hxx b/tests/unit/wifi/helpers/include/microsoft/net/wifi/test/AccessPointTest.hxx index a74ecdb3..c2c65745 100644 --- a/tests/unit/wifi/helpers/include/microsoft/net/wifi/test/AccessPointTest.hxx +++ b/tests/unit/wifi/helpers/include/microsoft/net/wifi/test/AccessPointTest.hxx @@ -6,16 +6,46 @@ #include #include +#include +#include namespace Microsoft::Net::Wifi::Test { +/** + * @brief IAccessPoint implementation for testing purposes. + * + * This implementation has public members for each configurable setting, allowing consumers to read and change them at + * will. This is intended to be used by test code only. + */ struct AccessPointTest final : public IAccessPoint { + std::string InterfaceName; + Microsoft::Net::Wifi::Ieee80211AccessPointCapabilities Capabilities; + bool IsEnabled{ false }; + + /** + * @brief Construct a new AccessPointTest object with the given interface name and default capabilities. + * + * @param interfaceName The interface name to use for the access point. + */ AccessPointTest(std::string_view interfaceName); + /** + * @brief Construct a new AccessPointTest object with the given interface name and capabilities. + * + * @param interfaceName The interface name to use for the access point. + * @param capabilities The capabilities to use for the access point. + */ + AccessPointTest(std::string_view interfaceName, Microsoft::Net::Wifi::Ieee80211AccessPointCapabilities capabilities); + + /** + * @brief Get the interface name of the access point. + * + * @return std::string_view + */ virtual std::string_view - GetInterfaceName() const noexcept override; + GetInterfaceName() const override; /** * @brief Create a new instance that can control the access point. @@ -24,8 +54,6 @@ struct AccessPointTest final : */ virtual std::unique_ptr CreateController() override; - - std::string InterfaceName; }; /** @@ -54,7 +82,7 @@ struct AccessPointFactoryTest : * * @param interface The interface to create the AccessPoint for. This can be * any string and does not have to correspond to a real device interface. - * @param createArgs + * @param createArgs Arguments to be passed to the access point during creation. * * @return std::shared_ptr */