aboutsummaryrefslogtreecommitdiff
path: root/pw_rpc/nanopb/public/pw_rpc/nanopb/client_testing.h
blob: a06d5379831a0e7350bad5956374ff098e2745ee (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
// 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 "pw_bytes/span.h"
#include "pw_rpc/client.h"
#include "pw_rpc/internal/method_info.h"
#include "pw_rpc/nanopb/fake_channel_output.h"
#include "pw_rpc/raw/client_testing.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. Accepts
// payloads as Nanopb structs.
class NanopbFakeServer : public FakeServer {
 private:
  template <auto kMethod>
  using Response = typename internal::MethodInfo<kMethod>::Response;

 public:
  using FakeServer::FakeServer;

  // Sends a response packet for a server or bidirectional streaming RPC to the
  // client.
  template <auto kMethod>
  void SendResponse(Status status) const {
    FakeServer::SendResponse<kMethod>(status);
  }

  // Sends a response packet for a unary or client streaming streaming RPC to
  // the client.
  template <auto kMethod,
            size_t kEncodeBufferSizeBytes = 2 * sizeof(Response<kMethod>)>
  void SendResponse(const Response<kMethod>& payload, Status status) const {
    std::byte buffer[kEncodeBufferSizeBytes] = {};
    FakeServer::SendResponse<kMethod>(EncodeResponse<kMethod>(&payload, buffer),
                                      status);
  }

  // Sends a stream packet for a server or bidirectional streaming RPC to the
  // client.
  template <auto kMethod,
            size_t kEncodeBufferSizeBytes = 2 * sizeof(Response<kMethod>)>
  void SendServerStream(const Response<kMethod>& payload) const {
    std::byte buffer[kEncodeBufferSizeBytes] = {};
    FakeServer::SendServerStream<kMethod>(
        EncodeResponse<kMethod>(&payload, buffer));
  }

 private:
  template <auto kMethod>
  static ConstByteSpan EncodeResponse(const void* payload, ByteSpan buffer) {
    const StatusWithSize result =
        internal::MethodInfo<kMethod>::serde().response().Encode(payload,
                                                                 buffer);
    PW_ASSERT(result.ok());
    return span(buffer).first(result.size());
  }
};

// Instantiates a NanopbFakeServer, Client, Channel, and NanopbFakeChannelOutput
// 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 NanopbClientTestContext {
 public:
  constexpr NanopbClientTestContext()
      : 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 NanopbFakeServer& server() const { return fake_server_; }
  NanopbFakeServer& 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:
  static constexpr uint32_t kDefaultChannelId = 1;

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

}  // namespace pw::rpc