1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_rpc/pwpb/fake_channel_output.h"
16
17 #include <array>
18 #include <cstddef>
19 #include <memory>
20
21 #include "gtest/gtest.h"
22 #include "pw_rpc/internal/channel.h"
23 #include "pw_rpc/internal/packet.h"
24 #include "pw_rpc_test_protos/test.rpc.pwpb.h"
25
26 PW_MODIFY_DIAGNOSTICS_PUSH();
27 PW_MODIFY_DIAGNOSTIC(ignored, "-Wmissing-field-initializers");
28
29 namespace pw::rpc::internal::test {
30 namespace {
31
32 using ::pw::rpc::internal::pwpb::PacketType;
33 using ::pw::rpc::test::pw_rpc::pwpb::TestService;
34 using Info = ::pw::rpc::internal::MethodInfo<TestService::TestUnaryRpc>;
35
TEST(PwpbFakeChannelOutput,Requests)36 TEST(PwpbFakeChannelOutput, Requests) {
37 PwpbFakeChannelOutput<1> output;
38
39 std::byte payload_buffer[32] = {};
40 constexpr Info::Request request{.integer = -100, .status_code = 5};
41 const StatusWithSize payload =
42 Info::serde().request().Encode(request, payload_buffer);
43 ASSERT_TRUE(payload.ok());
44
45 std::array<std::byte, 128> buffer;
46
47 auto packet = Packet(PacketType::REQUEST,
48 1,
49 Info::kServiceId,
50 Info::kMethodId,
51 999,
52 span(payload_buffer, payload.size()))
53 .Encode(buffer);
54 ASSERT_TRUE(packet.ok());
55
56 ASSERT_EQ(OkStatus(), output.Send(span(buffer).first(packet->size())));
57
58 ASSERT_TRUE(output.responses<TestService::TestUnaryRpc>().empty());
59 ASSERT_EQ(output.requests<TestService::TestUnaryRpc>().size(), 1u);
60
61 Info::Request sent = output.requests<TestService::TestUnaryRpc>().front();
62 EXPECT_EQ(sent.integer, -100);
63 EXPECT_EQ(sent.status_code, 5u);
64 }
65
TEST(PwpbFakeChannelOutput,Responses)66 TEST(PwpbFakeChannelOutput, Responses) {
67 PwpbFakeChannelOutput<1> output;
68
69 std::byte payload_buffer[32] = {};
70 const Info::Response response{.value = -9876};
71 const StatusWithSize payload =
72 Info::serde().response().Encode(response, payload_buffer);
73 ASSERT_TRUE(payload.ok());
74
75 std::array<std::byte, 128> buffer;
76
77 auto packet = Packet(PacketType::RESPONSE,
78 1,
79 Info::kServiceId,
80 Info::kMethodId,
81 999,
82 span(payload_buffer, payload.size()))
83 .Encode(buffer);
84 ASSERT_TRUE(packet.ok());
85
86 ASSERT_EQ(OkStatus(), output.Send(span(buffer).first(packet->size())));
87
88 ASSERT_EQ(output.responses<TestService::TestUnaryRpc>().size(), 1u);
89 ASSERT_TRUE(output.requests<TestService::TestUnaryRpc>().empty());
90
91 Info::Response sent = output.responses<TestService::TestUnaryRpc>().front();
92 EXPECT_EQ(sent.value, -9876);
93 }
94
95 } // namespace
96 } // namespace pw::rpc::internal::test
97
98 PW_MODIFY_DIAGNOSTICS_POP();
99