aboutsummaryrefslogtreecommitdiff
path: root/pw_rpc/raw/public/pw_rpc/raw/client_testing.h
blob: 9402e52752745ec0b84469567dac09a715a708f2 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2021 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.
#pragma once

#include <cstddef>
#include <cstdint>
#include <optional>
#include <type_traits>

#include "pw_bytes/span.h"
#include "pw_rpc/client.h"
#include "pw_rpc/internal/method_info.h"
#include "pw_rpc/internal/packet.h"
#include "pw_rpc/method_type.h"
#include "pw_rpc/raw/fake_channel_output.h"

namespace pw::rpc {

// TODO: b/234878467 - Document the client testing APIs.

// Sends packets to an RPC client as if it were a pw_rpc server.
class FakeServer {
 public:
  constexpr FakeServer(internal::test::FakeChannelOutput& output,
                       Client& client,
                       uint32_t channel_id,
                       ByteSpan packet_buffer)
      : output_(output),
        client_(client),
        channel_id_(channel_id),
        packet_buffer_(packet_buffer) {}

  // Sends a response packet for a server or bidirectional streaming RPC to the
  // client.
  template <auto kMethod,
            typename = std::enable_if_t<
                HasServerStream(internal::MethodInfo<kMethod>::kType)>>
  void SendResponse(Status status,
                    std::optional<uint32_t> call_id = std::nullopt) const {
    SendPacket<kMethod>(
        internal::pwpb::PacketType::RESPONSE, {}, status, call_id);
  }

  // Sends a response packet for a unary or client streaming streaming RPC to
  // the client.
  template <auto kMethod,
            typename = std::enable_if_t<
                !HasServerStream(internal::MethodInfo<kMethod>::kType)>>
  void SendResponse(ConstByteSpan payload,
                    Status status,
                    std::optional<uint32_t> call_id = std::nullopt) const {
    SendPacket<kMethod>(
        internal::pwpb::PacketType::RESPONSE, payload, status, call_id);
  }

  // Sends a stream packet for a server or bidirectional streaming RPC to the
  // client.
  template <auto kMethod>
  void SendServerStream(ConstByteSpan payload,
                        std::optional<uint32_t> call_id = std::nullopt) const {
    static_assert(HasServerStream(internal::MethodInfo<kMethod>::kType),
                  "Only server and bidirectional streaming methods can receive "
                  "server stream packets");
    SendPacket<kMethod>(internal::pwpb::PacketType::SERVER_STREAM,
                        payload,
                        OkStatus(),
                        call_id);
  }

  // Sends a server error packet to the client.
  template <auto kMethod>
  void SendServerError(Status error,
                       std::optional<uint32_t> call_id = std::nullopt) const {
    SendPacket<kMethod>(
        internal::pwpb::PacketType::SERVER_ERROR, {}, error, call_id);
  }

 private:
  template <auto kMethod>
  void SendPacket(internal::pwpb::PacketType type,
                  ConstByteSpan payload,
                  Status status,
                  std::optional<uint32_t> call_id) const {
    using Info = internal::MethodInfo<kMethod>;
    CheckProcessPacket(
        type, Info::kServiceId, Info::kMethodId, call_id, payload, status);
  }

  void CheckProcessPacket(internal::pwpb::PacketType type,
                          uint32_t service_id,
                          uint32_t method_id,
                          std::optional<uint32_t> call_id,
                          ConstByteSpan payload,
                          Status status) const;

  Status ProcessPacket(internal::pwpb::PacketType type,
                       uint32_t service_id,
                       uint32_t method_id,
                       std::optional<uint32_t> call_id,
                       ConstByteSpan payload,
                       Status status) const;

  internal::test::FakeChannelOutput& output_;
  Client& client_;
  const uint32_t channel_id_;
  ByteSpan packet_buffer_;  // For encoding packets sent by the server
};

// Instantiates a FakeServer, Client, Channel, and RawFakeChannelOutput for
// testing RPC client calls. These components may be used individually, but are
// instantiated together for convenience.
template <size_t kMaxPackets = 10,
          size_t kPacketEncodeBufferSizeBytes = 128,
          size_t kPayloadsBufferSizeBytes = 256>
class RawClientTestContext {
 public:
  static constexpr uint32_t kDefaultChannelId = 1;

  constexpr RawClientTestContext()
      : channel_(Channel::Create<kDefaultChannelId>(&channel_output_)),
        client_(span(&channel_, 1)),
        packet_buffer_{},
        fake_server_(
            channel_output_, client_, kDefaultChannelId, packet_buffer_) {}

  const Channel& channel() const { return channel_; }
  Channel& channel() { return channel_; }

  const FakeServer& server() const { return fake_server_; }
  FakeServer& server() { return fake_server_; }

  const Client& client() const { return client_; }
  Client& client() { return client_; }

  const auto& output() const { return channel_output_; }
  auto& output() { return channel_output_; }

 private:
  RawFakeChannelOutput<kMaxPackets, kPayloadsBufferSizeBytes> channel_output_;
  Channel channel_;
  Client client_;
  std::byte packet_buffer_[kPacketEncodeBufferSizeBytes];
  FakeServer fake_server_;
};

}  // namespace pw::rpc