Skip to content

Commit

Permalink
Merge pull request #136 from microsoft/apmanagertest
Browse files Browse the repository at this point in the history
Add helpers for managing access points for unit tests
  • Loading branch information
abeltrano authored Jan 26, 2024
2 parents f44b304 + d2d47aa commit 70b8526
Show file tree
Hide file tree
Showing 16 changed files with 204 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public:
std::vector<std::weak_ptr<IAccessPoint>>
GetAllAccessPoints() const;

~AccessPointManager() = default;
virtual ~AccessPointManager() = default;
AccessPointManager(const AccessPointManager&) = delete;
AccessPointManager(AccessPointManager&&) = delete;
AccessPointManager&
Expand All @@ -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.
*
Expand All @@ -106,15 +103,15 @@ private:
*
* @param accessPoint The access point to add.
*/
void
virtual void
AddAccessPoint(std::shared_ptr<IAccessPoint> accessPoint);

/**
* @brief Removes an existing access point from use.
*
* @param accessPoint The access point to remove.
*/
void
virtual void
RemoveAccessPoint(std::shared_ptr<IAccessPoint> accessPoint);

private:
Expand Down
2 changes: 1 addition & 1 deletion src/common/wifi/core/AccessPoint.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ AccessPoint::AccessPoint(std::string_view interfaceName, std::shared_ptr<IAccess
{}

std::string_view
AccessPoint::GetInterfaceName() const noexcept
AccessPoint::GetInterfaceName() const
{
return m_interfaceName;
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/wifi/core/AccessPointController.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ AccessPointController::AccessPointController(std::string_view interface) :
}

std::string_view
AccessPointController::GetInterfaceName() const noexcept
AccessPointController::GetInterfaceName() const
{
return m_interfaceName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct AccessPoint :
* @return std::string_view
*/
std::string_view
GetInterfaceName() const noexcept override;
GetInterfaceName() const override;

/**
* @brief Create a controller object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct AccessPointController :
* @return std::string_view
*/
std::string_view
GetInterfaceName() const noexcept override;
GetInterfaceName() const override;

private:
std::string m_interfaceName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct IAccessPoint
* @return std::string_view
*/
virtual std::string_view
GetInterfaceName() const noexcept = 0;
GetInterfaceName() const = 0;

/**
* @brief Create a new instance that can control the access point.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct IAccessPointController
* @return std::string_view
*/
virtual std::string_view
GetInterfaceName() const noexcept = 0;
GetInterfaceName() const = 0;

/**
* @brief Get whether the access point is enabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <catch2/catch_test_macros.hpp>

#include <microsoft/net/wifi/AccessPointDiscoveryAgentOperationsNetlink.hxx>
#include <microsoft/net/wifi/test/AccessPointTest.hxx>
#include <microsoft/net/wifi/test/AccessPointFactoryLinuxTest.hxx>

TEST_CASE("Create AccessPointDiscoveryAgentOperationsNetlink", "[wifi][core][apmanager]")
Expand Down
1 change: 0 additions & 1 deletion tests/unit/wifi/core/TestAccessPoint.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <microsoft/net/wifi/AccessPoint.hxx>
#include <microsoft/net/wifi/IAccessPointController.hxx>
#include <microsoft/net/wifi/test/AccessPointControllerTest.hxx>
#include <microsoft/net/wifi/test/AccessPointTest.hxx>

namespace Microsoft::Net::Wifi::Test
{
Expand Down
37 changes: 30 additions & 7 deletions tests/unit/wifi/helpers/AccessPointControllerTest.cxx
Original file line number Diff line number Diff line change
@@ -1,33 +1,56 @@

#include <stdexcept>

#include <microsoft/net/wifi/test/AccessPointControllerTest.hxx>
#include <microsoft/net/wifi/test/AccessPointTest.hxx>

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<IAccessPointController>
AccessPointControllerFactoryTest::Create(std::string_view interfaceName)
{
return std::make_unique<AccessPointControllerTest>(interfaceName);
if (AccessPoint != nullptr && interfaceName != AccessPoint->InterfaceName) {
throw std::runtime_error("AccessPointControllerFactoryTest::Create called with unexpected interface name");
}

return std::make_unique<AccessPointControllerTest>(AccessPoint);
}
16 changes: 16 additions & 0 deletions tests/unit/wifi/helpers/AccessPointManagerTest.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#include <microsoft/net/wifi/test/AccessPointManagerTest.hxx>

using namespace Microsoft::Net::Wifi::Test;

void
AccessPointManagerTest::AddAccessPoint(std::shared_ptr<IAccessPoint> accessPoint)
{
return AccessPointManager::AddAccessPoint(std::move(accessPoint));
}

void
AccessPointManagerTest::RemoveAccessPoint(std::shared_ptr<IAccessPoint> accessPoint)
{
return AccessPointManager::RemoveAccessPoint(std::move(accessPoint));
}
13 changes: 9 additions & 4 deletions tests/unit/wifi/helpers/AccessPointTest.cxx
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@

#include <microsoft/net/wifi/test/AccessPointTest.hxx>
#include <microsoft/net/wifi/test/AccessPointControllerTest.hxx>
#include <microsoft/net/wifi/test/AccessPointTest.hxx>

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;
}

std::unique_ptr<IAccessPointController>
AccessPointTest::CreateController()
{
return std::make_unique<AccessPointControllerTest>(InterfaceName);
return std::make_unique<AccessPointControllerTest>(this);
}

std::shared_ptr<IAccessPoint>
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/wifi/helpers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -26,5 +28,6 @@ target_include_directories(wifi-test-helpers

target_link_libraries(wifi-test-helpers
PUBLIC
wifi-apmanager
wifi-core
)
Original file line number Diff line number Diff line change
Expand Up @@ -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<Microsoft::Net::Wifi::IAccessPointController>
*/
std::unique_ptr<Microsoft::Net::Wifi::IAccessPointController>
Create(std::string_view interfaceName) override;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

#ifndef ACCESS_POINT_MANAGER_TEST
#define ACCESS_POINT_MANAGER_TEST

#include <memory>

#include <microsoft/net/wifi/AccessPointManager.hxx>
#include <microsoft/net/wifi/IAccessPoint.hxx>

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<IAccessPoint> accessPoint) override;

/**
* @brief Removes an existing access point from use.
*
* @param accessPoint The access point to remove.
*/
void
RemoveAccessPoint(std::shared_ptr<IAccessPoint> accessPoint) override;
};
} // namespace Microsoft::Net::Wifi::Test

#endif // ACCESS_POINT_MANAGER_TEST
Loading

0 comments on commit 70b8526

Please sign in to comment.