aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWyatt Hepler <hepler@google.com>2023-01-31 23:17:01 +0000
committerCQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-01-31 23:17:01 +0000
commit00e693c6fb7b9423fa31475b6abe63bca2fd01d3 (patch)
tree87575a9d9251e2dd5d251ca09e96315af085117e
parentd12ae88c1199d8f94573114080d05e493d4ae171 (diff)
downloadpigweed-00e693c6fb7b9423fa31475b6abe63bca2fd01d3.tar.gz
pw_rpc: Prevent starting calls with channel ID 0
- Channel ID 0 (Channel::kUnassignedChannelID) cannot be used for calls, and any packets with channel ID 0 are already rejected by RPC endpoints. Add a PW_CHECK() that crashes if code attempts to start a call on channel 0. - Add a test for invoking a call on an unknown channel. No test is added for starting calls on channel 0, since pw_unit_test does not support testing that assertions failed. Change-Id: Idc4a21925694c6300daa507c160fbf6822a93244 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/127036 Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com> Reviewed-by: Erik Gilling <konkers@google.com> Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
-rw-r--r--pw_rpc/call.cc4
-rw-r--r--pw_rpc/docs.rst7
-rw-r--r--pw_rpc/raw/client_reader_writer_test.cc14
3 files changed, 22 insertions, 3 deletions
diff --git a/pw_rpc/call.cc b/pw_rpc/call.cc
index ed16c1f4c..e96fd793f 100644
--- a/pw_rpc/call.cc
+++ b/pw_rpc/call.cc
@@ -62,6 +62,10 @@ Call::Call(LockedEndpoint& endpoint_ref,
? kClientStreamActive
: kClientStreamInactive),
properties_(properties) {
+ PW_CHECK_UINT_NE(channel_id,
+ Channel::kUnassignedChannelId,
+ "Calls cannot be created with channel ID 0 "
+ "(Channel::kUnassignedChannelId)");
endpoint().RegisterCall(*this);
}
diff --git a/pw_rpc/docs.rst b/pw_rpc/docs.rst
index 3e8bf6b1f..133f41310 100644
--- a/pw_rpc/docs.rst
+++ b/pw_rpc/docs.rst
@@ -927,7 +927,8 @@ Example
// Generated clients are namespaced with their proto library.
using EchoClient = pw_rpc::nanopb::EchoService::Client;
- // RPC channel ID on which to make client calls.
+ // RPC channel ID on which to make client calls. RPC calls cannot be made on
+ // channel 0 (Channel::kUnassignedChannelId).
constexpr uint32_t kDefaultChannelId = 1;
pw::rpc::NanopbUnaryReceiver<pw_rpc_EchoMessage> echo_call;
@@ -1191,8 +1192,8 @@ instantiate as:
``MethodRequestType<RpcMethod>``/``MethodResponseType<RpcMethod>``. Raw has
them both set as ``void``. In reality, they are ``pw::ConstByteSpan``. Any
helper/trait that wants to use this types for raw methods should do a custom
- implemenation that copies the bytes under the span instead of copying just the
- span.
+ implementation that copies the bytes under the span instead of copying just
+ the span.
Client Synchronous Call wrappers
================================
diff --git a/pw_rpc/raw/client_reader_writer_test.cc b/pw_rpc/raw/client_reader_writer_test.cc
index 7b4184f0b..98b394679 100644
--- a/pw_rpc/raw/client_reader_writer_test.cc
+++ b/pw_rpc/raw/client_reader_writer_test.cc
@@ -14,6 +14,8 @@
#include "pw_rpc/raw/client_reader_writer.h"
+#include <optional>
+
#include "gtest/gtest.h"
#include "pw_rpc/raw/client_testing.h"
#include "pw_rpc/writer.h"
@@ -310,6 +312,18 @@ TEST(RawUnaryReceiver, Move_ActiveToActive) {
EXPECT_TRUE(active_call_2.active());
}
+TEST(RawUnaryReceiver, InvalidChannelId) {
+ RawClientTestContext ctx;
+ std::optional<Status> error;
+
+ RawUnaryReceiver call = TestService::TestUnaryRpc(
+ ctx.client(), 1290341, {}, {}, [&error](Status status) {
+ error = status;
+ });
+ EXPECT_FALSE(call.active());
+ EXPECT_EQ(error, Status::Unavailable());
+}
+
TEST(RawClientReader, NoClientStream_OutOfScope_SilentlyCloses) {
RawClientTestContext ctx;