2022-08-03 11:15:56 -07:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
2022-08-18 14:03:55 -07:00
|
|
|
#include <array>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <string_view>
|
2022-08-03 11:15:56 -07:00
|
|
|
#include <thread>
|
2026-03-29 20:41:32 -07:00
|
|
|
#include <vector>
|
2022-08-03 11:15:56 -07:00
|
|
|
|
2023-08-28 15:13:34 -07:00
|
|
|
#include <gtest/gtest.h>
|
2025-11-07 19:57:55 -05:00
|
|
|
|
|
|
|
|
#include "wpi/net/MulticastServiceAnnouncer.h"
|
|
|
|
|
#include "wpi/net/MulticastServiceResolver.h"
|
2022-08-03 11:15:56 -07:00
|
|
|
|
|
|
|
|
TEST(MulticastServiceAnnouncerTest, EmptyText) {
|
|
|
|
|
const std::string_view serviceName = "TestServiceNoText";
|
2026-03-29 20:41:32 -07:00
|
|
|
const std::string_view serviceType = "_wpinotxt._tcp";
|
2022-08-03 11:15:56 -07:00
|
|
|
const int port = std::rand();
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::net::MulticastServiceAnnouncer announcer(serviceName, serviceType, port);
|
|
|
|
|
wpi::net::MulticastServiceResolver resolver(serviceType);
|
2022-08-03 11:15:56 -07:00
|
|
|
|
|
|
|
|
if (announcer.HasImplementation() && resolver.HasImplementation()) {
|
|
|
|
|
announcer.Start();
|
|
|
|
|
resolver.Start();
|
|
|
|
|
|
2026-03-29 20:41:32 -07:00
|
|
|
std::vector<wpi::net::MulticastServiceResolver::ServiceData> allData;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 15; i++) {
|
|
|
|
|
// GetData gives me new data since last time. This smoketest is just
|
|
|
|
|
// looking for -any- response
|
|
|
|
|
allData = resolver.GetData();
|
|
|
|
|
if (!allData.empty()) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ASSERT_GT(allData.size(), 0ul);
|
2022-08-03 11:15:56 -07:00
|
|
|
|
|
|
|
|
resolver.Stop();
|
|
|
|
|
announcer.Stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(MulticastServiceAnnouncerTest, SingleText) {
|
|
|
|
|
const std::string_view serviceName = "TestServiceSingle";
|
|
|
|
|
const std::string_view serviceType = "_wpitxt";
|
|
|
|
|
const int port = std::rand();
|
|
|
|
|
std::array<std::pair<std::string, std::string>, 1> txt = {
|
2024-11-17 20:29:23 -08:00
|
|
|
std::pair{"hello", "world"}};
|
2025-11-07 20:01:58 -05:00
|
|
|
wpi::net::MulticastServiceAnnouncer announcer(serviceName, serviceType, port,
|
|
|
|
|
txt);
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::net::MulticastServiceResolver resolver(serviceType);
|
2022-08-03 11:15:56 -07:00
|
|
|
|
|
|
|
|
if (announcer.HasImplementation() && resolver.HasImplementation()) {
|
|
|
|
|
announcer.Start();
|
|
|
|
|
resolver.Start();
|
|
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
|
|
|
}
|
|
|
|
|
}
|