mirror of
https://github.com/LizardByte/Sunshine.git
synced 2026-11-02 18:26:58 +09:00
101 lines
3.4 KiB
C++
101 lines
3.4 KiB
C++
/**
|
|
* @file tests/unit/test_stream.cpp
|
|
* @brief Test src/stream.*
|
|
*/
|
|
|
|
// test includes
|
|
#include "../tests_common.h"
|
|
|
|
// standard includes
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
// local includes
|
|
#include <src/network.h>
|
|
|
|
namespace stream {
|
|
std::vector<uint8_t> concat_and_insert(uint64_t insert_size, uint64_t slice_size, const std::string_view &data1, const std::string_view &data2);
|
|
std::optional<std::pair<std::uint16_t, std::string_view>> parse_control_packet(const ENetPacket &packet);
|
|
bool test_receive_ping_endpoints(bool session_id_pings);
|
|
} // namespace stream
|
|
|
|
TEST(StreamPingTests, KeepsLegacyAudioAndVideoSenderEndpointsSeparate) {
|
|
for (int attempt = 0; attempt < 20; ++attempt) {
|
|
ASSERT_TRUE(stream::test_receive_ping_endpoints(false)) << "Attempt " << attempt;
|
|
}
|
|
}
|
|
|
|
TEST(StreamPingTests, KeepsSessionIdAudioAndVideoSenderEndpointsSeparate) {
|
|
for (int attempt = 0; attempt < 20; ++attempt) {
|
|
ASSERT_TRUE(stream::test_receive_ping_endpoints(true)) << "Attempt " << attempt;
|
|
}
|
|
}
|
|
|
|
TEST(ConcatAndInsertTests, ConcatNoInsertionTest) {
|
|
std::array<char, 2> b1 = {'a', 'b'};
|
|
std::array<char, 3> b2 = {'c', 'd', 'e'};
|
|
auto res = stream::concat_and_insert(0, 2, std::string_view {b1.data(), b1.size()}, std::string_view {b2.data(), b2.size()});
|
|
auto expected = std::vector<uint8_t> {'a', 'b', 'c', 'd', 'e'};
|
|
ASSERT_EQ(res, expected);
|
|
}
|
|
|
|
TEST(ConcatAndInsertTests, ConcatLargeStrideTest) {
|
|
std::array<char, 2> b1 = {'a', 'b'};
|
|
std::array<char, 3> b2 = {'c', 'd', 'e'};
|
|
auto res = stream::concat_and_insert(1, b1.size() + b2.size() + 1, std::string_view {b1.data(), b1.size()}, std::string_view {b2.data(), b2.size()});
|
|
auto expected = std::vector<uint8_t> {0, 'a', 'b', 'c', 'd', 'e'};
|
|
ASSERT_EQ(res, expected);
|
|
}
|
|
|
|
TEST(ConcatAndInsertTests, ConcatSmallStrideTest) {
|
|
std::array<char, 2> b1 = {'a', 'b'};
|
|
std::array<char, 3> b2 = {'c', 'd', 'e'};
|
|
auto res = stream::concat_and_insert(1, 1, std::string_view {b1.data(), b1.size()}, std::string_view {b2.data(), b2.size()});
|
|
auto expected = std::vector<uint8_t> {0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e'};
|
|
ASSERT_EQ(res, expected);
|
|
}
|
|
|
|
TEST(ControlPacketTests, RejectsZeroLengthPacket) {
|
|
net::packet_t packet {enet_packet_create(nullptr, 0, 0)};
|
|
|
|
ASSERT_NE(packet, nullptr);
|
|
EXPECT_EQ(packet->data, nullptr);
|
|
EXPECT_EQ(stream::parse_control_packet(*packet), std::nullopt);
|
|
}
|
|
|
|
TEST(ControlPacketTests, RejectsOneBytePacket) {
|
|
const std::uint8_t data {0x06};
|
|
net::packet_t packet {enet_packet_create(&data, sizeof(data), 0)};
|
|
|
|
ASSERT_NE(packet, nullptr);
|
|
EXPECT_EQ(stream::parse_control_packet(*packet), std::nullopt);
|
|
}
|
|
|
|
TEST(ControlPacketTests, AcceptsTypeWithoutPayload) {
|
|
const std::array<std::uint8_t, 2> data {0x06, 0x02};
|
|
net::packet_t packet {enet_packet_create(data.data(), data.size(), 0)};
|
|
|
|
ASSERT_NE(packet, nullptr);
|
|
auto message = stream::parse_control_packet(*packet);
|
|
ASSERT_TRUE(message);
|
|
EXPECT_EQ(message->first, 0x0206);
|
|
EXPECT_TRUE(message->second.empty());
|
|
}
|
|
|
|
TEST(ControlPacketTests, AcceptsTypeAndPayload) {
|
|
const std::array<std::uint8_t, 5> data {0x06, 0x02, 'a', 'b', 'c'};
|
|
net::packet_t packet {enet_packet_create(data.data(), data.size(), 0)};
|
|
|
|
ASSERT_NE(packet, nullptr);
|
|
auto message = stream::parse_control_packet(*packet);
|
|
ASSERT_TRUE(message);
|
|
EXPECT_EQ(message->first, 0x0206);
|
|
EXPECT_EQ(message->second, "abc");
|
|
}
|