• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/packet_meta.h"
16 
17 #include "gtest/gtest.h"
18 #include "pw_rpc/internal/packet.h"
19 
20 namespace pw::rpc {
21 namespace {
22 
TEST(PacketMeta,FromBufferDecodesValidMinimalPacket)23 TEST(PacketMeta, FromBufferDecodesValidMinimalPacket) {
24   const uint32_t kChannelId = 12;
25   const ServiceId kServiceId = internal::WrapServiceId(0xdeadbeef);
26   const uint32_t kMethodId = 44;
27 
28   internal::Packet packet;
29   packet.set_channel_id(kChannelId);
30   packet.set_service_id(internal::UnwrapServiceId(kServiceId));
31   packet.set_type(internal::pwpb::PacketType::RESPONSE);
32   packet.set_method_id(kMethodId);
33 
34   std::byte buffer[128];
35   Result<ConstByteSpan> encode_result = packet.Encode(buffer);
36   ASSERT_EQ(encode_result.status(), OkStatus());
37 
38   Result<PacketMeta> decode_result = PacketMeta::FromBuffer(*encode_result);
39   ASSERT_EQ(decode_result.status(), OkStatus());
40   EXPECT_EQ(decode_result->channel_id(), kChannelId);
41   EXPECT_EQ(decode_result->service_id(), kServiceId);
42   EXPECT_TRUE(decode_result->destination_is_client());
43 }
44 
TEST(PacketMeta,FromBufferFailsOnIncompletePacket)45 TEST(PacketMeta, FromBufferFailsOnIncompletePacket) {
46   internal::Packet packet;
47 
48   std::byte buffer[128];
49   Result<ConstByteSpan> encode_result = packet.Encode(buffer);
50   ASSERT_EQ(encode_result.status(), OkStatus());
51 
52   Result<PacketMeta> decode_result = PacketMeta::FromBuffer(*encode_result);
53   ASSERT_EQ(decode_result.status(), Status::DataLoss());
54 }
55 
56 }  // namespace
57 }  // namespace pw::rpc
58