diff --git a/tests/unit/linux/wpa-controller/detail/WpaDaemonManager.cxx b/tests/unit/linux/wpa-controller/detail/WpaDaemonManager.cxx index 6088137f..fc57b8b5 100644 --- a/tests/unit/linux/wpa-controller/detail/WpaDaemonManager.cxx +++ b/tests/unit/linux/wpa-controller/detail/WpaDaemonManager.cxx @@ -10,16 +10,49 @@ #include #include -#include "WpaDaemonManager.hxx" +#include #include #include #include -#include "hostapd.conf.format.hxx" #include "HostapdBinaryInfo.hxx" +#include "WpaDaemonManager.hxx" namespace detail { +/** + * @brief Format string for the default wpa_supplicant configuration file contents. + * + * There are 1 arguments expected to be substituted into the format string: + * + * 1. The control interface path. + */ +static constexpr auto WpaDaemonWpaSupplicantConfigurationFileContentsFormat = R"CONFIG( +ctrl_interface={} +)CONFIG"; + +/** + * @brief Format string for the default hostapd configuration file contents. + * + * There are 2 argumentd expected to be substituted into the format string: + * + * 1. The wlan interface name. + * 2. The control interface path. + */ +static constexpr auto WpaDaemonHostapdConfigurationFileContentsFormat = R"CONFIG( +interface={} +driver=nl80211 +ctrl_interface={} +ssid=wificontrollertest +hw_mode=g +channel=1 +auth_algs=3 +wpa=2 +wpa_passphrase=password +wpa_key_mgmt=WPA-PSK WPA-PSK-SHA256 SAE +wpa_pairwise=TKIP CCMP +rsn_pairwise=CCMP +)CONFIG"; /** * @brief Write the default configuration file contents for the specified wpa @@ -27,20 +60,24 @@ namespace detail * * @param wpaType The type of wpa daemon to write the configuration file for. * @param interfaceName The wlan interface the daemon will be managing. + * @param controlSocketPath The path to the control socket for the daemon. * @param configurationFile The file stream to write the configuration file to. */ void -WriteDefaultConfigurationFileContents(Wpa::WpaType wpaType, std::string_view interfaceName, std::ofstream& configurationFile) +WriteDefaultConfigurationFileContents(Wpa::WpaType wpaType, std::string_view interfaceName, std::string_view controlSocketPath, std::ofstream& configurationFile) { switch (wpaType) { - case Wpa::WpaType::Hostapd: { - configurationFile << std::format(WpaDaemonHostapdConfigurationFileContentsFormat, interfaceName); + case Wpa::WpaType::Hostapd: + configurationFile << std::format(WpaDaemonHostapdConfigurationFileContentsFormat, interfaceName, controlSocketPath); break; - } - default: { + case Wpa::WpaType::WpaSupplicant: + configurationFile << std::format(WpaDaemonWpaSupplicantConfigurationFileContentsFormat, controlSocketPath); + break; + case Wpa::WpaType::Unknown: + [[fallthrough]]; + default: throw std::runtime_error(std::format("Unsupported wpa daemon type '{}'", magic_enum::enum_name(wpaType))); } - } } } // namespace detail @@ -51,10 +88,11 @@ WpaDaemonManager::CreateAndWriteDefaultConfigurationFile(Wpa::WpaType wpaType, s // Determine which daemon to create the configuration file for. const auto daemon = Wpa::GetWpaTypeDaemonBinaryName(wpaType); const auto daemonConfigurationFilePath = std::filesystem::temp_directory_path() / std::format("{}.conf", daemon); + const auto daemonControlSocketPath{ Wpa::ProtocolWpaConfig::GetControlSocketPath(wpaType) }; // Create and write default configuration file contents. std::ofstream daemonConfigurationFile{ daemonConfigurationFilePath, std::ios::out | std::ios::trunc }; - detail::WriteDefaultConfigurationFileContents(wpaType, interfaceName, daemonConfigurationFile); + detail::WriteDefaultConfigurationFileContents(wpaType, interfaceName, daemonControlSocketPath, daemonConfigurationFile); daemonConfigurationFile.flush(); daemonConfigurationFile.close(); @@ -71,6 +109,15 @@ WpaDaemonManager::Start(Wpa::WpaType wpaType, std::string_view interfaceName, co interfaceName = WpaDaemonManager::InterfaceNameDefault; } + // Create the control socket path if it doesn't exist. + const std::filesystem::path controlSocketPath{ Wpa::ProtocolWpaConfig::GetControlSocketPath(wpaType) }; + if (!std::filesystem::exists(controlSocketPath)) { + if (!std::filesystem::create_directories(controlSocketPath)) { + LOGE << std::format("Failed to create control socket path '{}'\n", controlSocketPath.c_str()); + return std::nullopt; + } + } + // Determine which daemon to start and formulate daemon binary arguments. const auto daemon = Wpa::GetWpaTypeDaemonBinaryName(wpaType); const auto* configurationFileArgumentPrefix = (wpaType == Wpa::WpaType::WpaSupplicant) ? "-c" : "";