Skip to content

Commit

Permalink
PayloadChannel: enhance and test IsRequest(), IsNotification()
Browse files Browse the repository at this point in the history
  • Loading branch information
jmillan committed Jul 20, 2022
1 parent 1e75fe1 commit 6358399
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 8 deletions.
2 changes: 1 addition & 1 deletion worker/include/PayloadChannel/Notification.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace PayloadChannel
};

public:
static bool IsNotification(const char* jsonNotification);
static bool IsNotification(const char* msg, size_t msgLen);

private:
static absl::flat_hash_map<std::string, EventId> string2EventId;
Expand Down
2 changes: 1 addition & 1 deletion worker/include/PayloadChannel/PayloadChannelRequest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace PayloadChannel
};

public:
static bool IsRequest(const char* msg);
static bool IsRequest(const char* msg, size_t msgLen);

private:
static absl::flat_hash_map<std::string, MethodId> string2MethodId;
Expand Down
2 changes: 2 additions & 0 deletions worker/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ mediasoup_worker_test = executable(
],
sources: common_sources + [
'test/src/tests.cpp',
'test/src/PayloadChannel/TestPayloadChannelRequest.cpp',
'test/src/PayloadChannel/TestNotification.cpp',
'test/src/RTC/TestKeyFrameRequestManager.cpp',
'test/src/RTC/TestNackGenerator.cpp',
'test/src/RTC/TestRateCalculator.cpp',
Expand Down
4 changes: 2 additions & 2 deletions worker/src/PayloadChannel/Notification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ namespace PayloadChannel
/*
* msg notification starts with "n:"
*/
bool Notification::IsNotification(const char* msg)
bool Notification::IsNotification(const char* msg, size_t msgLen)
{
MS_TRACE();

return (msg[0] == 'n' && msg[1] == ':');
return (msgLen > 2 && msg[0] == 'n' && msg[1] == ':');
}

/* Instance methods. */
Expand Down
4 changes: 2 additions & 2 deletions worker/src/PayloadChannel/PayloadChannelRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ namespace PayloadChannel
/*
* msg request starts with "r:"
*/
bool PayloadChannelRequest::IsRequest(const char* msg)
bool PayloadChannelRequest::IsRequest(const char* msg, size_t msgLen)
{
MS_TRACE();

return (msg[0] == 'r' && msg[1] == ':');
return (msgLen > 2 && msg[0] == 'r' && msg[1] == ':');
}

/* Instance methods. */
Expand Down
4 changes: 2 additions & 2 deletions worker/src/PayloadChannel/PayloadChannelSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ namespace PayloadChannel

if (!this->ongoingNotification && !this->ongoingRequest)
{
if (PayloadChannelRequest::IsRequest(msg))
if (PayloadChannelRequest::IsRequest(msg, msgLen))
{
try
{
Expand All @@ -381,7 +381,7 @@ namespace PayloadChannel
MS_ERROR("discarding wrong Payload Channel request: %s", error.what());
}
}
else if (Notification::IsNotification(msg))
else if (Notification::IsNotification(msg, msgLen))
{
try
{
Expand Down
34 changes: 34 additions & 0 deletions worker/test/src/PayloadChannel/TestNotification.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "common.hpp"
#include "PayloadChannel/Notification.hpp"
#include <catch2/catch.hpp>

SCENARIO("Notification", "[channel][notification]")
{
SECTION("notification")
{
char foo[]{"n:abcd"};

REQUIRE(PayloadChannel::Notification::IsNotification(foo, sizeof(foo)) == true);
}

SECTION("request")
{
char foo[]{"r:abcd"};

REQUIRE(PayloadChannel::Notification::IsNotification(foo, sizeof(foo)) == false);
}

SECTION("non notification")
{
char foo[]{"abcd"};

REQUIRE(PayloadChannel::Notification::IsNotification(foo, sizeof(foo)) == false);
}

SECTION("empty string")
{
char foo[]{};

REQUIRE(PayloadChannel::Notification::IsNotification(foo, sizeof(foo)) == false);
}
}
34 changes: 34 additions & 0 deletions worker/test/src/PayloadChannel/TestPayloadChannelRequest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "common.hpp"
#include "PayloadChannel/PayloadChannelRequest.hpp"
#include <catch2/catch.hpp>

SCENARIO("ChannelRequest", "[channel][request]")
{
SECTION("notification")
{
char foo[]{"n:abcd"};

REQUIRE(PayloadChannel::PayloadChannelRequest::IsRequest(foo, sizeof(foo)) == false);
}

SECTION("request")
{
char foo[]{"r:abcd"};

REQUIRE(PayloadChannel::PayloadChannelRequest::IsRequest(foo, sizeof(foo)) == true);
}

SECTION("non request")
{
char foo[]{"abcd"};

REQUIRE(PayloadChannel::PayloadChannelRequest::IsRequest(foo, sizeof(foo)) == false);
}

SECTION("empty string")
{
char foo[]{};

REQUIRE(PayloadChannel::PayloadChannelRequest::IsRequest(foo, sizeof(foo)) == false);
}
}

0 comments on commit 6358399

Please sign in to comment.