mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-21 00:41:57 +09:00
[WIP] (MG_Remote): implement P6.5 TCP and stream transport
This commit is contained in:
@@ -22,7 +22,7 @@ if (NOT WIN32)
|
||||
# P6 so: the SAME nine ITransport-contract cases InProcessTransportTest runs, over a real
|
||||
# socketpair with real threads, plus the three a stream has and a message queue does not.
|
||||
# POSIX for the same reason as FdPassingTest - and because CONTRACT-P6 §2.6 lands POSIX only.
|
||||
list(APPEND MOBILEGL_WIRE_TESTS SocketTransportTest)
|
||||
list(APPEND MOBILEGL_WIRE_TESTS SocketTransportTest StreamLinkTest)
|
||||
# P6 sm: two MobileGL processes. Needs the real server image beside it, so
|
||||
# the dependency is declared after the loop rather than assumed.
|
||||
list(APPEND MOBILEGL_WIRE_TESTS ServerSpawnTest)
|
||||
|
||||
@@ -118,6 +118,8 @@ TEST(ProtocolSmokeTest, UnionTagsAreFrozenWireValues) {
|
||||
EXPECT_EQ(static_cast<int>(CtrlMsg::AuxRequest), 8);
|
||||
EXPECT_EQ(static_cast<int>(CtrlMsg::Fatal), 9);
|
||||
EXPECT_EQ(static_cast<int>(CtrlMsg::LogLine), 10);
|
||||
EXPECT_EQ(static_cast<int>(CtrlMsg::Refuse), 11);
|
||||
EXPECT_EQ(static_cast<int>(CtrlMsg::LogFlush), 12);
|
||||
|
||||
EXPECT_EQ(static_cast<int>(SegmentKind::Cmd), 1);
|
||||
EXPECT_EQ(static_cast<int>(SegmentKind::Adopt), 6);
|
||||
@@ -156,3 +158,27 @@ TEST(ProtocolSmokeTest, TravelsAcrossTheTransportUnchanged) {
|
||||
ASSERT_NE(hello, nullptr);
|
||||
EXPECT_EQ(hello->pid(), 4242u);
|
||||
}
|
||||
|
||||
TEST(ProtocolSmokeTest, StreamHandshakeCarriesIndependentWireBuildAndServerWindows) {
|
||||
::flatbuffers::FlatBufferBuilder builder(512);
|
||||
auto terms = CreateLinkTerms(builder, DataPlane::Stream, WireForm::StructImage,
|
||||
1048560, 8388608, 33554432, 262144);
|
||||
auto hello = CreateHelloDirect(builder, MOBILEGL_PROTOCOL_ABI_MAJOR, MOBILEGL_PROTOCOL_ABI_MINOR,
|
||||
"commit-123", 1, 42, nullptr, 0, 0x12345678, terms, "pairing-token", DialMode::Connect);
|
||||
auto envelope = CreateCtrlEnvelope(builder, CtrlMsg::Hello, hello.Union());
|
||||
FinishCtrlEnvelopeBuffer(builder, envelope);
|
||||
::flatbuffers::Verifier verifier(builder.GetBufferPointer(), builder.GetSize());
|
||||
ASSERT_TRUE(VerifyCtrlEnvelopeBuffer(verifier));
|
||||
const auto* parsed = GetCtrlEnvelope(builder.GetBufferPointer())->msg_as_Hello();
|
||||
ASSERT_NE(parsed, nullptr);
|
||||
EXPECT_EQ(parsed->wireFingerprint(), 0x12345678u);
|
||||
EXPECT_EQ(parsed->buildFingerprint()->str(), "commit-123");
|
||||
EXPECT_EQ(parsed->token()->str(), "pairing-token");
|
||||
EXPECT_EQ(parsed->dialMode(), DialMode::Connect);
|
||||
ASSERT_NE(parsed->linkTerms(), nullptr);
|
||||
EXPECT_EQ(parsed->linkTerms()->dataPlane(), DataPlane::Stream);
|
||||
EXPECT_EQ(parsed->linkTerms()->maxReplyBytes(), 1048560u);
|
||||
EXPECT_EQ(parsed->linkTerms()->cmdWindowBytes(), 8388608u);
|
||||
EXPECT_EQ(parsed->linkTerms()->stageWindowBytes(), 33554432u);
|
||||
EXPECT_EQ(parsed->linkTerms()->eventWindowBytes(), 262144u);
|
||||
}
|
||||
|
||||
@@ -153,7 +153,8 @@ namespace {
|
||||
auto stamp = builder.CreateString(GIT_COMMIT_HASH_SHORT);
|
||||
auto hello = ::MobileGL::Wire::CreateHello(
|
||||
builder, MOBILEGL_PROTOCOL_ABI_MAJOR, MOBILEGL_PROTOCOL_ABI_MINOR, stamp,
|
||||
/*backendType=*/0u, /*pid=*/0u, /*configBlob=*/0, CapsAbiFingerprint());
|
||||
/*backendType=*/0u, /*pid=*/0u, /*configBlob=*/0, CapsAbiFingerprint(),
|
||||
CapsAbiFingerprint(), ::MobileGL::Wire::CreateLinkTerms(builder));
|
||||
auto root = ::MobileGL::Wire::CreateCtrlEnvelope(
|
||||
builder, ::MobileGL::Wire::CtrlMsg::Hello, hello.Union());
|
||||
::MobileGL::Wire::FinishCtrlEnvelopeBuffer(builder, root);
|
||||
|
||||
@@ -39,6 +39,8 @@
|
||||
#include "Includes.h"
|
||||
|
||||
#include <MG_Remote/CapsCodec.h>
|
||||
#include <MG_Remote/Handshake.h>
|
||||
#include <MG_Pipe/PipeWireLayout.h>
|
||||
#include <MG_Remote/Client/ClientSession.h>
|
||||
#include <MG_Remote/Protocol/generated/protocol_generated.h>
|
||||
#include <MG_Remote/Server/ServerSession.h>
|
||||
@@ -114,100 +116,148 @@ namespace {
|
||||
// below. Also red, separately, by deleting any one `mix(...)` line from MixAbiFingerprint: the
|
||||
// matching EXPECT_NE names the input that stopped being mixed. Both perturbations were run.
|
||||
TEST(SessionHandshakeTest, TheAbiFingerprintChangesWhenAnyOfItsInputsDoes) {
|
||||
const Uint64 production = CapsAbiFingerprint();
|
||||
EXPECT_NE(production, 0u) << "0 is reserved for \"not stated\"";
|
||||
EXPECT_EQ(production, CapsAbiFingerprint()) << "not stable within one build";
|
||||
|
||||
const Transport::AbiFingerprintInputs inputs = CapsAbiFingerprintInputs();
|
||||
EXPECT_EQ(production, Transport::MixAbiFingerprint(inputs))
|
||||
<< "CapsAbiFingerprint() is not MixAbiFingerprint over CapsAbiFingerprintInputs(): the "
|
||||
"handshake compares a value this case cannot reach, which is finding 6 again";
|
||||
|
||||
// The inputs are the real ones, so a CapsAbiFingerprintInputs() that hard-coded a size
|
||||
// would be caught here rather than agreed with by a peer built from a different tree.
|
||||
const Uint64 production = WireFingerprint();
|
||||
const auto inputs = CapsAbiFingerprintInputs();
|
||||
EXPECT_NE(production, 0u);
|
||||
EXPECT_EQ(production, CapsAbiFingerprint());
|
||||
EXPECT_EQ(production, Transport::MixAbiFingerprint(inputs));
|
||||
EXPECT_EQ(inputs.DynamicParamsSize, sizeof(MG_Backend::DynamicBackendParameters));
|
||||
EXPECT_EQ(inputs.CapsSize, sizeof(MG_Pipe::MGPCaps));
|
||||
EXPECT_EQ(inputs.FunctionTableSize, sizeof(MG_Backend::GLFunctionsTable));
|
||||
EXPECT_EQ(inputs.FormatCapabilityTargets,
|
||||
static_cast<Uint64>(MG_Backend::kFormatCapabilityTargetCount));
|
||||
EXPECT_EQ(inputs.FormatCapabilityFormats,
|
||||
static_cast<Uint64>(MG_Backend::kFormatCapabilityFormatCount));
|
||||
EXPECT_NE(inputs.FormatCapabilitiesCodecVersion, 0u);
|
||||
EXPECT_NE(inputs.RendererInfoCodecVersion, 0u);
|
||||
EXPECT_EQ(inputs.MemberLayout, MG_Pipe::kMGPipeWireMemberLayoutDigest);
|
||||
EXPECT_EQ(inputs.CatalogueLayout, MG_Pipe::kMGPipeWireCatalogueDigest);
|
||||
EXPECT_EQ(inputs.RenderStateLayout, MG_Pipe::WireRenderStateDigest());
|
||||
EXPECT_EQ(inputs.OpCount, static_cast<Uint64>(MG_Pipe::MGPWireOp::kOpCount));
|
||||
EXPECT_EQ(inputs.AbiVersion, static_cast<Uint32>(MOBILEGL_ABI_VERSION(
|
||||
MOBILEGL_PROTOCOL_ABI_MAJOR, MOBILEGL_PROTOCOL_ABI_MINOR)));
|
||||
// CONTRACT-P6 4.2. THE ASSERTION SPLITS WITH THE MACRO PAIR, and it has to: this used to
|
||||
// demand a non-empty stamp unconditionally and was RED in every build whose git could not be
|
||||
// read - a real signal, but one that told the reader nothing about WHICH of the two states
|
||||
// the build was in. Now the build system states it, and both states are asserted.
|
||||
ASSERT_NE(inputs.BuildStamp, nullptr);
|
||||
EXPECT_EQ(inputs.BuildStampPresent, static_cast<Uint32>(MOBILEGL_BUILD_STAMP_PRESENT))
|
||||
<< "the fingerprint disagrees with the build system about whether this build has a stamp";
|
||||
#if MGL_HANDSHAKE_TEST_HAS_GIT_HASH
|
||||
EXPECT_STREQ(inputs.BuildStamp, MOBILEGL_BUILD_STAMP_VALUE);
|
||||
#endif
|
||||
#if MOBILEGL_BUILD_STAMP_PRESENT
|
||||
EXPECT_NE(inputs.BuildStamp[0], '\0')
|
||||
<< "the build claims a stamp and the fingerprint mixed an empty one";
|
||||
#else
|
||||
// A stampless build is a KNOWN, DECLARED state - CMake warns at configure time - but it must
|
||||
// not be a silent one here either. What still has to hold is that the ABSENCE is mixed, which
|
||||
// the BuildStampPresent perturbation below proves.
|
||||
EXPECT_STREQ(inputs.BuildStamp, "")
|
||||
<< "PRESENT is 0 but a value came through; the two halves of the pair disagree";
|
||||
#endif
|
||||
|
||||
// Every input moves the answer. Each lambda changes exactly one field of a copy of the
|
||||
// REAL inputs, so what is proven is that the production value depends on that field.
|
||||
const auto perturbed = [&](auto&& mutate) {
|
||||
Transport::AbiFingerprintInputs copy = inputs;
|
||||
EXPECT_EQ(inputs.PointerBits, sizeof(void*) * 8);
|
||||
const auto perturbed = [&](auto mutate) {
|
||||
auto copy = inputs;
|
||||
mutate(copy);
|
||||
return Transport::MixAbiFingerprint(copy);
|
||||
};
|
||||
EXPECT_NE(production, perturbed([](Transport::AbiFingerprintInputs& i) { ++i.DynamicParamsSize; }))
|
||||
<< "sizeof(DynamicBackendParameters) is not mixed";
|
||||
EXPECT_NE(production, perturbed([](Transport::AbiFingerprintInputs& i) { ++i.CapsSize; }))
|
||||
<< "sizeof(MGPCaps) is not mixed";
|
||||
EXPECT_NE(production, perturbed([](Transport::AbiFingerprintInputs& i) { ++i.FunctionTableSize; }))
|
||||
<< "sizeof(GLFunctionsTable) is not mixed";
|
||||
EXPECT_NE(production,
|
||||
perturbed([](Transport::AbiFingerprintInputs& i) { ++i.FormatCapabilityTargets; }))
|
||||
<< "kFormatCapabilityTargetCount is not mixed";
|
||||
EXPECT_NE(production,
|
||||
perturbed([](Transport::AbiFingerprintInputs& i) { ++i.FormatCapabilityFormats; }))
|
||||
<< "kFormatCapabilityFormatCount is not mixed";
|
||||
EXPECT_NE(production, perturbed([](Transport::AbiFingerprintInputs& i) {
|
||||
++i.FormatCapabilitiesCodecVersion;
|
||||
}))
|
||||
<< "kFormatCapabilitiesCodecVersion is not mixed";
|
||||
EXPECT_NE(production,
|
||||
perturbed([](Transport::AbiFingerprintInputs& i) { ++i.RendererInfoCodecVersion; }))
|
||||
<< "kRendererInfoCodecVersion is not mixed";
|
||||
EXPECT_NE(production, perturbed([](Transport::AbiFingerprintInputs& i) { ++i.OpCount; }))
|
||||
<< "MGPWireOp::kOpCount is not mixed (ID-33)";
|
||||
EXPECT_NE(production, perturbed([](Transport::AbiFingerprintInputs& i) { ++i.AbiVersion; }))
|
||||
<< "the protocol ABI version is not mixed (ID-33)";
|
||||
EXPECT_NE(production,
|
||||
perturbed([](Transport::AbiFingerprintInputs& i) { i.BuildStamp = "not-this-build"; }))
|
||||
<< "the git stamp is not mixed";
|
||||
// A missing stamp is not the same as an empty one, and neither is the same as a real build.
|
||||
// THE PRESENCE FLAG IS ITSELF AN INPUT (4.2), and this is the assertion that makes a
|
||||
// stampless build safe to reason about: a peer that says "I could not name my commit" must
|
||||
// not produce the same fingerprint as one whose commit genuinely is "". It has to move the
|
||||
// answer in EITHER build state, which is why it sits outside the #if below.
|
||||
EXPECT_NE(production, perturbed([](Transport::AbiFingerprintInputs& i) { ++i.BuildStampPresent; }))
|
||||
<< "BuildStampPresent is not mixed; 'no stamp' and 'empty stamp' are one input again";
|
||||
EXPECT_NE(production, perturbed([](auto& i) { ++i.DynamicParamsSize; })) << "DynamicParamsSize";
|
||||
EXPECT_NE(production, perturbed([](auto& i) { ++i.CapsSize; })) << "CapsSize";
|
||||
EXPECT_NE(production, perturbed([](auto& i) { ++i.MemberLayout; })) << "MemberLayout";
|
||||
EXPECT_NE(production, perturbed([](auto& i) { ++i.CatalogueLayout; })) << "CatalogueLayout";
|
||||
EXPECT_NE(production, perturbed([](auto& i) { ++i.RenderStateLayout; })) << "RenderStateLayout";
|
||||
EXPECT_NE(production, perturbed([](auto& i) { ++i.FormatCapabilityTargets; })) << "FormatCapabilityTargets";
|
||||
EXPECT_NE(production, perturbed([](auto& i) { ++i.FormatCapabilityFormats; })) << "FormatCapabilityFormats";
|
||||
EXPECT_NE(production, perturbed([](auto& i) { ++i.FormatCapabilitiesCodecVersion; })) << "FormatCapabilitiesCodecVersion";
|
||||
EXPECT_NE(production, perturbed([](auto& i) { ++i.RendererInfoCodecVersion; })) << "RendererInfoCodecVersion";
|
||||
EXPECT_NE(production, perturbed([](auto& i) { ++i.OpCount; })) << "OpCount";
|
||||
EXPECT_NE(production, perturbed([](auto& i) { ++i.AbiVersion; })) << "AbiVersion";
|
||||
EXPECT_NE(production, perturbed([](auto& i) { ++i.PointerBits; })) << "PointerBits";
|
||||
EXPECT_NE(production, perturbed([](auto& i) { ++i.LittleEndian; })) << "LittleEndian";
|
||||
}
|
||||
|
||||
EXPECT_NE(production, perturbed([](Transport::AbiFingerprintInputs& i) { i.BuildStamp = nullptr; }));
|
||||
#if MOBILEGL_BUILD_STAMP_PRESENT
|
||||
// Vacuous without a stamp: production's value already IS "", so this perturbation would be
|
||||
// the identity and the assertion would pass while proving nothing.
|
||||
EXPECT_NE(production, perturbed([](Transport::AbiFingerprintInputs& i) { i.BuildStamp = ""; }));
|
||||
TEST(SessionHandshakeTest, SameWidthFieldReorderingChangesTheWireDigestWithoutABuildStamp) {
|
||||
using namespace MG_Pipe;
|
||||
std::vector<WireLayoutMember> fields(std::begin(kMGPipeWireLayoutMembers),
|
||||
std::end(kMGPipeWireLayoutMembers));
|
||||
// MGPRange.Offset and Size have the same width: sizeof-only checks cannot see this.
|
||||
SizeT offsetIndex = 0, sizeIndex = 0;
|
||||
for (SizeT i = 0; i < fields.size(); ++i) {
|
||||
if (std::strcmp(fields[i].Name, "MGPRange.Offset") == 0) offsetIndex = i;
|
||||
if (std::strcmp(fields[i].Name, "MGPRange.Size") == 0) sizeIndex = i;
|
||||
}
|
||||
ASSERT_NE(offsetIndex, sizeIndex);
|
||||
ASSERT_EQ(fields[offsetIndex].Size, fields[sizeIndex].Size);
|
||||
std::swap(fields[offsetIndex].Offset, fields[sizeIndex].Offset);
|
||||
EXPECT_NE(WireMemberLayoutDigest(fields.data(), fields.size()), kMGPipeWireMemberLayoutDigest);
|
||||
const auto before = WireFingerprint();
|
||||
EXPECT_STREQ(BuildFingerprint(), MOBILEGL_BUILD_STAMP_VALUE);
|
||||
EXPECT_EQ(BuildFingerprintPresent(), MOBILEGL_BUILD_STAMP_PRESENT != 0);
|
||||
EXPECT_EQ(before, WireFingerprint());
|
||||
}
|
||||
|
||||
namespace {
|
||||
class ScopedHandshakeEnvironment {
|
||||
public:
|
||||
ScopedHandshakeEnvironment(const char* name, const char* value) : m_name(name) {
|
||||
const char* previous = std::getenv(name);
|
||||
m_present = previous != nullptr;
|
||||
if (m_present) m_previous = previous;
|
||||
Set(value);
|
||||
}
|
||||
~ScopedHandshakeEnvironment() { Set(m_present ? m_previous.c_str() : nullptr); }
|
||||
private:
|
||||
void Set(const char* value) {
|
||||
#if defined(_WIN32)
|
||||
::_putenv_s(m_name, value == nullptr ? "" : value);
|
||||
#else
|
||||
if (value == nullptr) ::unsetenv(m_name); else ::setenv(m_name, value, 1);
|
||||
#endif
|
||||
EXPECT_NE(perturbed([](Transport::AbiFingerprintInputs& i) { i.BuildStamp = nullptr; }),
|
||||
perturbed([](Transport::AbiFingerprintInputs& i) { i.BuildStamp = ""; }))
|
||||
<< "\"no stamp\" and \"an empty stamp\" collapsed into one input";
|
||||
}
|
||||
const char* m_name;
|
||||
bool m_present = false;
|
||||
std::string m_previous;
|
||||
};
|
||||
|
||||
std::vector<Uint8> ReadHandshakeFrame(Transport::ITransport& transport) {
|
||||
Uint64 bytes = 0;
|
||||
if (transport.ReceiveFrame({nullptr, 0}, &bytes, 1000) != MOBILEGL_ERR_BUFFER_TOO_SMALL) return {};
|
||||
std::vector<Uint8> frame(bytes);
|
||||
if (transport.ReceiveFrame({frame.data(), frame.size()}, &bytes, 1000) != MOBILEGL_OK) return {};
|
||||
return frame;
|
||||
}
|
||||
|
||||
void CheckHandshake(Uint32 major, Uint64 wire, const char* build,
|
||||
::MobileGL::Wire::DialMode dial, ::MobileGL::Wire::RefuseCode expected) {
|
||||
using namespace ::MobileGL::Wire;
|
||||
std::unique_ptr<Transport::InProcessTransport> client, server;
|
||||
Transport::InProcessTransport::CreatePair(client, server);
|
||||
::flatbuffers::FlatBufferBuilder builder(512);
|
||||
auto terms = CreateLinkTerms(builder);
|
||||
auto hello = CreateHelloDirect(builder, major, MOBILEGL_PROTOCOL_ABI_MINOR, build,
|
||||
0, 1, nullptr, wire, wire, terms, nullptr, dial);
|
||||
auto root = CreateCtrlEnvelope(builder, CtrlMsg::Hello, hello.Union());
|
||||
FinishCtrlEnvelopeBuffer(builder, root);
|
||||
std::vector<Uint8> first(builder.GetBufferPointer(), builder.GetBufferPointer() + builder.GetSize());
|
||||
Server::ServerSession session;
|
||||
Transport::SessionSegmentSizes sizes;
|
||||
sizes.CmdRingBytes = 4096; sizes.StageBytes = 4096;
|
||||
sizes.ReplyBytes = 4096; sizes.EventRingBytes = 4096;
|
||||
session.SetSegmentSizes(sizes);
|
||||
const auto result = session.Accept(*server, &first);
|
||||
EXPECT_EQ(result, expected == RefuseCode::None ? MOBILEGL_OK : MOBILEGL_ERR_PROTOCOL_MISMATCH);
|
||||
const auto reply = ReadHandshakeFrame(*client);
|
||||
ASSERT_FALSE(reply.empty());
|
||||
::flatbuffers::Verifier verifier(reply.data(), reply.size());
|
||||
ASSERT_TRUE(VerifyCtrlEnvelopeBuffer(verifier));
|
||||
const auto* envelope = GetCtrlEnvelope(reply.data());
|
||||
if (expected == RefuseCode::None) {
|
||||
ASSERT_NE(envelope->msg_as_Welcome(), nullptr);
|
||||
EXPECT_EQ(envelope->msg_as_Welcome()->wireFingerprint(), WireFingerprint());
|
||||
ASSERT_NE(envelope->msg_as_Welcome()->linkTerms(), nullptr);
|
||||
EXPECT_EQ(envelope->msg_as_Welcome()->linkTerms()->cmdWindowBytes(), 4096u);
|
||||
} else {
|
||||
ASSERT_NE(envelope->msg_as_Refuse(), nullptr);
|
||||
EXPECT_EQ(envelope->msg_as_Refuse()->code(), expected);
|
||||
EXPECT_FALSE(session.Accepted());
|
||||
}
|
||||
session.Close();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(SessionHandshakeTest, WireMajorMismatchReturnsNamedRefuseWithoutAborting) {
|
||||
CheckHandshake(99, WireFingerprint(), BuildFingerprint(), ::MobileGL::Wire::DialMode::No,
|
||||
::MobileGL::Wire::RefuseCode::ProtocolVersion);
|
||||
}
|
||||
TEST(SessionHandshakeTest, WireLayoutMismatchReturnsNamedRefuseWithoutAborting) {
|
||||
CheckHandshake(MOBILEGL_PROTOCOL_ABI_MAJOR, WireFingerprint() ^ 1, BuildFingerprint(),
|
||||
::MobileGL::Wire::DialMode::No, ::MobileGL::Wire::RefuseCode::WireFingerprint);
|
||||
}
|
||||
TEST(SessionHandshakeTest, ConnectAcceptsDifferentBuildWithIdenticalWire) {
|
||||
ScopedHandshakeEnvironment required("MOBILEGL_IPC_REQUIRE_SAME_BUILD", "0");
|
||||
CheckHandshake(MOBILEGL_PROTOCOL_ABI_MAJOR, WireFingerprint(), "different-commit",
|
||||
::MobileGL::Wire::DialMode::Connect, ::MobileGL::Wire::RefuseCode::None);
|
||||
}
|
||||
TEST(SessionHandshakeTest, ForkRefusesDifferentBuildWithIdenticalWire) {
|
||||
CheckHandshake(MOBILEGL_PROTOCOL_ABI_MAJOR, WireFingerprint(), "different-commit",
|
||||
::MobileGL::Wire::DialMode::Fork, ::MobileGL::Wire::RefuseCode::BuildFingerprint);
|
||||
}
|
||||
TEST(SessionHandshakeTest, ConnectCanRequireTheSameBuild) {
|
||||
ScopedHandshakeEnvironment required("MOBILEGL_IPC_REQUIRE_SAME_BUILD", "1");
|
||||
CheckHandshake(MOBILEGL_PROTOCOL_ABI_MAJOR, WireFingerprint(), "different-commit",
|
||||
::MobileGL::Wire::DialMode::Connect, ::MobileGL::Wire::RefuseCode::BuildFingerprint);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -51,6 +51,8 @@
|
||||
|
||||
#if !defined(_WIN32)
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
@@ -58,6 +60,68 @@ using namespace MobileGL::MG_Remote::Transport;
|
||||
|
||||
#if !defined(_WIN32)
|
||||
|
||||
TEST(SocketTransportTest, TcpCarriesControlAndIndependentDataWithKeepalive) {
|
||||
// Find an available loopback port; Listen deliberately refuses ambiguous port 0 URIs.
|
||||
int reservation = ::socket(AF_INET, SOCK_STREAM, 0);
|
||||
ASSERT_GE(reservation, 0);
|
||||
sockaddr_in address{};
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
ASSERT_EQ(::bind(reservation, reinterpret_cast<sockaddr*>(&address), sizeof(address)), 0);
|
||||
socklen_t addressSize = sizeof(address);
|
||||
ASSERT_EQ(::getsockname(reservation, reinterpret_cast<sockaddr*>(&address), &addressSize), 0);
|
||||
const auto endpoint = std::string("tcp://127.0.0.1:") + std::to_string(ntohs(address.sin_port));
|
||||
::close(reservation);
|
||||
int listener = -1;
|
||||
ASSERT_EQ(SocketTransport::Listen(endpoint, &listener), MOBILEGL_OK);
|
||||
std::unique_ptr<SocketTransport> client, server;
|
||||
ASSERT_EQ(SocketTransport::ConnectTo(endpoint, 1000, client), MOBILEGL_OK);
|
||||
ASSERT_EQ(SocketTransport::AcceptPair(listener, 1000, server), MOBILEGL_OK);
|
||||
::close(listener);
|
||||
ASSERT_TRUE(client->IsTcp());
|
||||
ASSERT_TRUE(server->IsTcp());
|
||||
for (auto* transport : {client.get(), server.get()}) {
|
||||
int option = 0;
|
||||
socklen_t size = sizeof(option);
|
||||
ASSERT_EQ(::getsockopt(transport->StreamFd(), IPPROTO_TCP, TCP_NODELAY, &option, &size), 0);
|
||||
EXPECT_EQ(option, 1);
|
||||
ASSERT_EQ(::getsockopt(transport->StreamFd(), SOL_SOCKET, SO_KEEPALIVE, &option, &size), 0);
|
||||
EXPECT_EQ(option, 1);
|
||||
EXPECT_EQ(transport->ShareFd(0, {nullptr, 0}), MOBILEGL_ERR_UNSUPPORTED);
|
||||
}
|
||||
const int clientData = client->TakeDataFd(), serverData = server->TakeDataFd();
|
||||
ASSERT_GE(clientData, 0);
|
||||
ASSERT_GE(serverData, 0);
|
||||
EXPECT_EQ(client->TakeDataFd(), -1);
|
||||
const char message[] = "independent";
|
||||
ASSERT_EQ(::send(clientData, message, sizeof(message), MSG_NOSIGNAL), sizeof(message));
|
||||
char received[sizeof(message)]{};
|
||||
ASSERT_EQ(::recv(serverData, received, sizeof(received), MSG_WAITALL), sizeof(received));
|
||||
EXPECT_EQ(std::memcmp(message, received, sizeof(message)), 0);
|
||||
ASSERT_EQ(server->SendFrame({message, sizeof(message)}), MOBILEGL_OK);
|
||||
std::uint64_t size = 0;
|
||||
ASSERT_EQ(client->ReceiveFrame({received, sizeof(received)}, &size, 1000), MOBILEGL_OK);
|
||||
EXPECT_EQ(size, sizeof(message));
|
||||
::close(clientData);
|
||||
::close(serverData);
|
||||
}
|
||||
|
||||
TEST(SocketTransportTest, TcpRejectsMalformedNamesAndUnauthenticatedWildcard) {
|
||||
int listener = -1;
|
||||
for (const char* endpoint : {"tcp://", "tcp://localhost", "tcp://localhost:0", "tcp://localhost:65536",
|
||||
"tcp://localhost:-1", "tcp://[::1]broken", "tcp://::1:40000"}) {
|
||||
EXPECT_EQ(SocketTransport::Listen(endpoint, &listener), MOBILEGL_ERR_INVALID_ARGUMENT) << endpoint;
|
||||
EXPECT_EQ(listener, -1);
|
||||
}
|
||||
const char* prior = std::getenv("MOBILEGL_IPC_TOKEN");
|
||||
const std::string saved = prior ? prior : "";
|
||||
const bool wasSet = prior != nullptr;
|
||||
::unsetenv("MOBILEGL_IPC_TOKEN");
|
||||
EXPECT_EQ(SocketTransport::Listen("tcp://0.0.0.0:40613", &listener), MOBILEGL_ERR_PROTOCOL_MISMATCH);
|
||||
EXPECT_EQ(listener, -1);
|
||||
if (wasSet) ::setenv("MOBILEGL_IPC_TOKEN", saved.c_str(), 1);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
MobileGLByteSpan Span(const std::string& text) {
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
#include <MG_Remote/Transport/StreamLink.h>
|
||||
#include <MG_Remote/Transport/SessionRings.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <thread>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace MobileGL::MG_Remote::Transport;
|
||||
namespace {
|
||||
class StreamPair : public testing::Test {
|
||||
protected:
|
||||
SessionSegments clientMemory, serverMemory;
|
||||
StreamLink client, server;
|
||||
RingProducer commands;
|
||||
RingConsumer consumer;
|
||||
SessionProducer publish;
|
||||
SessionConsumer apply;
|
||||
void SetUp() override {
|
||||
SessionSegmentSizes sizes;
|
||||
sizes.CmdRingBytes = 1024;
|
||||
sizes.StageBytes = 4096;
|
||||
sizes.ReplyBytes = 4096;
|
||||
sizes.EventRingBytes = 1024;
|
||||
ASSERT_EQ(clientMemory.CreatePrivate(sizes, MemoryRole::Client), MOBILEGL_OK);
|
||||
ASSERT_EQ(serverMemory.CreatePrivate(sizes, MemoryRole::Server), MOBILEGL_OK);
|
||||
int sockets[2];
|
||||
ASSERT_EQ(::socketpair(AF_UNIX, SOCK_STREAM, 0, sockets), 0);
|
||||
ASSERT_EQ(client.Attach(sockets[0], clientMemory, TransportRoleTag::ClientProducer), MOBILEGL_OK);
|
||||
ASSERT_EQ(server.Attach(sockets[1], serverMemory, TransportRoleTag::ServerConsumer), MOBILEGL_OK);
|
||||
commands = RingProducer(clientMemory.CmdControl(), clientMemory.CmdRingBase(), 1024, RingCursorSet::Cmd);
|
||||
consumer = RingConsumer(serverMemory.CmdControl(), serverMemory.CmdRingBase(), 1024, RingCursorSet::Cmd);
|
||||
publish.Attach(clientMemory.CmdControl(), &commands, &client.ConsumerBell(), &client.ProducerBell(), 0);
|
||||
publish.SetLink(&client);
|
||||
apply.Attach(serverMemory.CmdControl(), &consumer, &server.ProducerBell(), &server.ConsumerBell(), 0);
|
||||
apply.SetLink(&server);
|
||||
}
|
||||
void TearDown() override {
|
||||
client.Detach();
|
||||
server.Detach();
|
||||
}
|
||||
void Queue(std::uint64_t seq, std::uint64_t value) {
|
||||
auto* p = commands.Reserve(1, 0, sizeof value);
|
||||
ASSERT_NE(p, nullptr);
|
||||
std::memcpy(p, &value, sizeof value);
|
||||
publish.PublishAndNotify(seq);
|
||||
}
|
||||
template <class F>
|
||||
bool Eventually(F ready) {
|
||||
const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(2);
|
||||
while (!ready() && std::chrono::steady_clock::now() < deadline)
|
||||
std::this_thread::yield();
|
||||
return ready();
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(StreamPair, StagingArrivesBeforeItsCommandAndLastRecordProgressFlushesOnIdle) {
|
||||
const std::array<unsigned char, 8> bytes{1, 2, 3, 4, 5, 6, 7, 8};
|
||||
std::memcpy(static_cast<unsigned char*>(clientMemory.StageBase()) + 24, bytes.data(), bytes.size());
|
||||
client.NoteStage({24, bytes.size()});
|
||||
Queue(1, 24);
|
||||
ASSERT_EQ(client.Flush(), MOBILEGL_OK);
|
||||
ASSERT_EQ(apply.WaitForWork(2000), SessionWait::Reached);
|
||||
ASSERT_TRUE(apply.ApplyOne([&](const RingRecordView& v) {
|
||||
std::uint64_t offset;
|
||||
std::memcpy(&offset, v.payload, sizeof offset);
|
||||
const void* p = nullptr;
|
||||
ASSERT_EQ(server.ResolveSpan(LinkSegment::Stage, {offset, bytes.size()}, &p), MOBILEGL_OK);
|
||||
EXPECT_EQ(std::memcmp(p, bytes.data(), bytes.size()), 0);
|
||||
}));
|
||||
apply.RetireThrough(1);
|
||||
// This is the production idle discipline, not a polling sender thread.
|
||||
EXPECT_EQ(apply.WaitForWork(1), SessionWait::TimedOut);
|
||||
EXPECT_EQ(publish.WaitForApplied(1, 2000), SessionWait::Reached);
|
||||
EXPECT_EQ(client.Progress()->retiredSeq.load(), 1u);
|
||||
}
|
||||
|
||||
TEST_F(StreamPair, RetiredByteCursorCrossesManyWrapsWithoutStageRingCursors) {
|
||||
for (std::uint64_t seq = 1; seq <= 160; ++seq) {
|
||||
Queue(seq, seq);
|
||||
ASSERT_EQ(client.Flush(), MOBILEGL_OK);
|
||||
ASSERT_EQ(apply.WaitForWork(2000), SessionWait::Reached);
|
||||
ASSERT_TRUE(apply.ApplyOne([&](const RingRecordView& v) {
|
||||
std::uint64_t value;
|
||||
std::memcpy(&value, v.payload, sizeof value);
|
||||
EXPECT_EQ(value, seq);
|
||||
}));
|
||||
apply.RetireThrough(seq);
|
||||
ASSERT_EQ(server.FlushProgress(), MOBILEGL_OK);
|
||||
ASSERT_EQ(publish.WaitForApplied(seq, 2000), SessionWait::Reached);
|
||||
}
|
||||
EXPECT_GT(commands.LocalHead(), 1024u);
|
||||
EXPECT_EQ(commands.FreeBytes(), 1024u);
|
||||
EXPECT_EQ(clientMemory.CmdControl()->stageHead.load(), 0u);
|
||||
EXPECT_EQ(clientMemory.CmdControl()->stageRetiredTail.load(), 0u);
|
||||
}
|
||||
|
||||
TEST_F(StreamPair, UnwantedReplyDoesNotBlockLaterReplyAndAppliedPublication) {
|
||||
Queue(1, 1);
|
||||
Queue(2, 2);
|
||||
ASSERT_EQ(client.Flush(), MOBILEGL_OK);
|
||||
ASSERT_EQ(apply.WaitForWork(2000), SessionWait::Reached);
|
||||
ASSERT_TRUE(apply.ApplyOne([&](const RingRecordView&) {
|
||||
const std::uint64_t value = 11;
|
||||
EXPECT_EQ(server.PostReply(1, 0, &value, sizeof value), MOBILEGL_OK);
|
||||
}));
|
||||
ASSERT_TRUE(apply.ApplyOne([&](const RingRecordView&) {
|
||||
const std::uint64_t value = 22;
|
||||
EXPECT_EQ(server.PostReply(2, 0, &value, sizeof value), MOBILEGL_OK);
|
||||
}));
|
||||
apply.RetireThrough(2);
|
||||
ASSERT_EQ(server.FlushProgress(), MOBILEGL_OK);
|
||||
ASSERT_EQ(publish.WaitForApplied(2, 2000), SessionWait::Reached);
|
||||
std::int32_t status;
|
||||
const void* p = nullptr;
|
||||
std::uint64_t size = 0;
|
||||
ASSERT_EQ(client.ReadReply(2, &status, &p, &size), MOBILEGL_OK);
|
||||
ASSERT_EQ(size, sizeof(std::uint64_t));
|
||||
std::uint64_t value;
|
||||
std::memcpy(&value, p, sizeof value);
|
||||
EXPECT_EQ(value, 22u);
|
||||
}
|
||||
|
||||
TEST_F(StreamPair, EventDrainReturnsCreditAndClearsRemoteFullLatch) {
|
||||
EventRingProducer events(serverMemory.EventControl(), serverMemory.CmdControl(), serverMemory.EventRingBase(),
|
||||
1024);
|
||||
EventRingConsumer read(clientMemory.EventControl(), clientMemory.CmdControl(), clientMemory.EventRingBase(),
|
||||
1024, clientMemory.EventSegmentBase());
|
||||
read.SetLink(&client);
|
||||
auto* p = events.Reserve(kEventGlError, 64);
|
||||
ASSERT_NE(p, nullptr);
|
||||
std::memset(p, 0, 64);
|
||||
events.Ring().Publish();
|
||||
serverMemory.CmdControl()->eventRingFull.store(1);
|
||||
ASSERT_EQ(server.FlushProgress(), MOBILEGL_OK);
|
||||
ASSERT_TRUE(Eventually([&] { return clientMemory.CmdControl()->eventRingFull.load() != 0; }));
|
||||
RingRecordView record;
|
||||
ASSERT_TRUE(read.Pop(record));
|
||||
read.Drained();
|
||||
ASSERT_TRUE(Eventually([&] { return serverMemory.CmdControl()->eventRingFull.load() == 0; }));
|
||||
EXPECT_EQ(events.Ring().FreeBytes(), 1024u);
|
||||
}
|
||||
|
||||
TEST_F(StreamPair, PeerEofKillsBothLocalBellsAndWakesAWaiter) {
|
||||
server.Detach();
|
||||
ASSERT_TRUE(Eventually([&] { return client.PeerHungUp(); }));
|
||||
EXPECT_TRUE(client.ProducerBell().Dead());
|
||||
EXPECT_TRUE(client.ConsumerBell().Dead());
|
||||
EXPECT_EQ(publish.WaitForApplied(10, 2000), SessionWait::ShutDown);
|
||||
}
|
||||
|
||||
void WriteFrame(int fd, unsigned kind, std::uint64_t a, const void* payload, std::uint32_t size) {
|
||||
unsigned char h[28]{};
|
||||
auto put = [&](unsigned at, std::uint64_t value, unsigned width) {
|
||||
for (unsigned i = 0; i < width; ++i)
|
||||
h[at + i] = value >> (8 * i);
|
||||
};
|
||||
put(0, 0x444c474d, 4);
|
||||
put(4, size, 4);
|
||||
h[8] = kind;
|
||||
put(12, a, 8);
|
||||
ASSERT_EQ(::send(fd, h, sizeof h, MSG_NOSIGNAL), sizeof h);
|
||||
if (size) ASSERT_EQ(::send(fd, payload, size, MSG_NOSIGNAL), size);
|
||||
}
|
||||
|
||||
TEST(StreamLinkDeath, StageOutsideNegotiatedWindowIsNamedProtocolCorruption) {
|
||||
EXPECT_DEATH(
|
||||
{
|
||||
SessionSegments memory;
|
||||
SessionSegmentSizes sizes;
|
||||
sizes.StageBytes = 64;
|
||||
memory.CreatePrivate(sizes, MemoryRole::Server);
|
||||
int sockets[2];
|
||||
socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
|
||||
StreamLink link;
|
||||
link.Attach(sockets[0], memory, TransportRoleTag::ServerConsumer);
|
||||
unsigned char byte = 1;
|
||||
WriteFrame(sockets[1], 2, 64, &byte, 1);
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
},
|
||||
"ProtocolCorruption.*window");
|
||||
}
|
||||
|
||||
TEST(StreamLinkDeath, RegressingProgressIsNamedProtocolCorruption) {
|
||||
EXPECT_DEATH(
|
||||
{
|
||||
SessionSegments memory;
|
||||
SessionSegmentSizes sizes;
|
||||
memory.CreatePrivate(sizes, MemoryRole::Client);
|
||||
memory.CmdControl()->submittedSeq.store(3);
|
||||
int sockets[2];
|
||||
socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
|
||||
StreamLink link;
|
||||
link.Attach(sockets[0], memory, TransportRoleTag::ClientProducer);
|
||||
unsigned char progress[64]{};
|
||||
progress[0] = 2;
|
||||
WriteFrame(sockets[1], 4, 0, progress, 64);
|
||||
progress[0] = 1;
|
||||
WriteFrame(sockets[1], 4, 0, progress, 64);
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
},
|
||||
"ProtocolCorruption.*backwards");
|
||||
}
|
||||
} // namespace
|
||||
@@ -279,6 +279,7 @@ TEST(SurfaceControlFrameTest, TheReplyHalfRoundTripsAsASurfaceReply) {
|
||||
frame.ok = true;
|
||||
frame.eglMajor = 1;
|
||||
frame.eglMinor = 5;
|
||||
frame.eventHead = 0x123456789ull;
|
||||
|
||||
flatbuffers::FlatBufferBuilder builder(256);
|
||||
EncodeSurfaceReplyFrame(frame, &builder);
|
||||
@@ -294,6 +295,7 @@ TEST(SurfaceControlFrameTest, TheReplyHalfRoundTripsAsASurfaceReply) {
|
||||
EXPECT_TRUE(back.ok);
|
||||
EXPECT_EQ(back.eglMajor, 1);
|
||||
EXPECT_EQ(back.eglMinor, 5);
|
||||
EXPECT_EQ(back.eventHead, frame.eventHead) << "control replies must fence data events";
|
||||
}
|
||||
|
||||
TEST(SurfaceControlFrameTest, MalformedAndInprocOnlyFramesRefuseToEncode) {
|
||||
|
||||
Reference in New Issue
Block a user