aboutsummaryrefslogtreecommitdiff
path: root/pw_rpc/packet_meta_test.cc
blob: d21f3a48dfd5db7e598bc3d6d4bebf9bbc2e7a82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2022 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

#include "pw_rpc/packet_meta.h"

#include "gtest/gtest.h"
#include "pw_rpc/internal/packet.h"

namespace pw::rpc {
namespace {

TEST(PacketMeta, FromBufferDecodesValidMinimalPacket) {
  const uint32_t kChannelId = 12;
  const ServiceId kServiceId = internal::WrapServiceId(0xdeadbeef);
  const uint32_t kMethodId = 44;

  internal::Packet packet;
  packet.set_channel_id(kChannelId);
  packet.set_service_id(internal::UnwrapServiceId(kServiceId));
  packet.set_type(internal::pwpb::PacketType::RESPONSE);
  packet.set_method_id(kMethodId);

  std::byte buffer[128];
  Result<ConstByteSpan> encode_result = packet.Encode(buffer);
  ASSERT_EQ(encode_result.status(), OkStatus());

  Result<PacketMeta> decode_result = PacketMeta::FromBuffer(*encode_result);
  ASSERT_EQ(decode_result.status(), OkStatus());
  EXPECT_EQ(decode_result->channel_id(), kChannelId);
  EXPECT_EQ(decode_result->service_id(), kServiceId);
  EXPECT_TRUE(decode_result->destination_is_client());
}

TEST(PacketMeta, FromBufferFailsOnIncompletePacket) {
  internal::Packet packet;

  std::byte buffer[128];
  Result<ConstByteSpan> encode_result = packet.Encode(buffer);
  ASSERT_EQ(encode_result.status(), OkStatus());

  Result<PacketMeta> decode_result = PacketMeta::FromBuffer(*encode_result);
  ASSERT_EQ(decode_result.status(), Status::DataLoss());
}

}  // namespace
}  // namespace pw::rpc