aboutsummaryrefslogtreecommitdiff
path: root/pw_rpc_transport/egress_ingress_test.cc
blob: 5c3f8d317620bd442a3ea58f96d56523efbe1d2b (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright 2023 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_transport/egress_ingress.h"

#include <random>

#include "gtest/gtest.h"
#include "public/pw_rpc_transport/rpc_transport.h"
#include "pw_bytes/span.h"
#include "pw_metric/metric.h"
#include "pw_rpc/client_server.h"
#include "pw_rpc/packet_meta.h"
#include "pw_rpc_transport/hdlc_framing.h"
#include "pw_rpc_transport/internal/test.rpc.pwpb.h"
#include "pw_rpc_transport/rpc_transport.h"
#include "pw_rpc_transport/service_registry.h"
#include "pw_rpc_transport/simple_framing.h"
#include "pw_status/status.h"
#include "pw_string/string.h"
#include "pw_sync/thread_notification.h"

namespace pw::rpc {
namespace {

constexpr size_t kMaxPacketSize = 256;

class TestService final
    : public pw_rpc_transport::testing::pw_rpc::pwpb::TestService::Service<
          TestService> {
 public:
  Status Echo(
      const pw_rpc_transport::testing::pwpb::EchoMessage::Message& request,
      pw_rpc_transport::testing::pwpb::EchoMessage::Message& response) {
    response.msg = request.msg;
    return OkStatus();
  }
};

// A transport that stores all received frames so they can be manually retrieved
// by the ingress later.
class TestTransport : public RpcFrameSender {
 public:
  explicit TestTransport(size_t mtu, bool is_faulty = false)
      : mtu_(mtu), is_faulty_(is_faulty) {}

  size_t MaximumTransmissionUnit() const override { return mtu_; }

  Status Send(RpcFrame frame) override {
    if (is_faulty_) {
      return Status::Internal();
    }
    std::copy(
        frame.header.begin(), frame.header.end(), std::back_inserter(buffer_));
    std::copy(frame.payload.begin(),
              frame.payload.end(),
              std::back_inserter(buffer_));
    return OkStatus();
  }

  ByteSpan buffer() { return buffer_; }

 private:
  size_t mtu_;
  bool is_faulty_ = false;
  std::vector<std::byte> buffer_;
};

// An egress handler that passes the received RPC packet to the service
// registry.
class TestLocalEgress : public RpcEgressHandler {
 public:
  Status SendRpcPacket(ConstByteSpan packet) override {
    if (!registry_) {
      return Status::FailedPrecondition();
    }
    return registry_->ProcessRpcPacket(packet);
  }

  void set_registry(ServiceRegistry& registry) { registry_ = &registry; }

 private:
  ServiceRegistry* registry_ = nullptr;
};

TEST(RpcEgressIngressTest, SimpleFramingRoundtrip) {
  constexpr uint32_t kChannelAtoB = 1;
  constexpr size_t kMaxMessageLength = 200;
  constexpr size_t kAtoBMtu = 33;
  constexpr size_t kBtoAMtu = 72;

  TestTransport transport_a_to_b(kAtoBMtu);
  TestTransport transport_b_to_a(kBtoAMtu);

  SimpleRpcEgress<kMaxPacketSize> egress_a_to_b("a->b", transport_a_to_b);
  SimpleRpcEgress<kMaxPacketSize> egress_b_to_a("b->a", transport_b_to_a);

  std::array a_tx_channels = {
      rpc::Channel::Create<kChannelAtoB>(&egress_a_to_b)};
  std::array b_tx_channels = {
      rpc::Channel::Create<kChannelAtoB>(&egress_b_to_a)};

  ServiceRegistry registry_a(a_tx_channels);
  ServiceRegistry registry_b(b_tx_channels);

  TestService test_service;
  registry_b.RegisterService(test_service);

  TestLocalEgress local_egress_a;
  local_egress_a.set_registry(registry_a);

  TestLocalEgress local_egress_b;
  local_egress_b.set_registry(registry_b);

  std::array a_rx_channels = {
      ChannelEgress{kChannelAtoB, local_egress_a},
  };
  std::array b_rx_channels = {
      ChannelEgress{kChannelAtoB, local_egress_b},
  };

  SimpleRpcIngress<kMaxPacketSize> ingress_a(a_rx_channels);
  SimpleRpcIngress<kMaxPacketSize> ingress_b(b_rx_channels);

  auto client =
      registry_a
          .CreateClient<pw_rpc_transport::testing::pw_rpc::pwpb::TestService>(
              kChannelAtoB);

  sync::ThreadNotification receiver1_done;
  sync::ThreadNotification receiver2_done;

  struct ReceiverState {
    InlineString<kMaxMessageLength> message;
    sync::ThreadNotification done;
  };

  ReceiverState receiver1;
  ReceiverState receiver2;
  receiver1.message.append(2 * transport_a_to_b.MaximumTransmissionUnit(), '*');
  receiver2.message.append(2 * transport_b_to_a.MaximumTransmissionUnit(), '>');

  auto call1 = client.Echo(
      {.msg = receiver1.message},
      [&receiver1](
          const pw_rpc_transport::testing::pwpb::EchoMessage::Message& response,
          Status status) {
        EXPECT_EQ(status, OkStatus());
        EXPECT_EQ(response.msg, receiver1.message);
        receiver1.done.release();
      },
      [&receiver1](Status status) {
        EXPECT_EQ(status, OkStatus());
        receiver1.done.release();
      });

  auto call2 = client.Echo(
      {.msg = receiver2.message},
      [&receiver2](
          const pw_rpc_transport::testing::pwpb::EchoMessage::Message& response,
          Status status) {
        EXPECT_EQ(status, OkStatus());
        EXPECT_EQ(response.msg, receiver2.message);
        receiver2.done.release();
      },
      [&receiver2](Status status) {
        EXPECT_EQ(status, OkStatus());
        receiver2.done.release();
      });

  // Calling `ingress_b.ProcessIncomingData` reads all packets from the
  // transport and dispatches them according to the ingress configuration.
  // Dispatching a packet generates a reply message: we then read it back at the
  // sender by calling `ingress_a.ProcessIncomingData`.
  EXPECT_EQ(ingress_b.ProcessIncomingData(transport_a_to_b.buffer()),
            OkStatus());
  EXPECT_EQ(ingress_a.ProcessIncomingData(transport_b_to_a.buffer()),
            OkStatus());

  receiver1.done.acquire();
  receiver2.done.acquire();
}

TEST(RpcEgressIngressTest, HdlcFramingRoundtrip) {
  constexpr uint32_t kChannelAtoB = 1;
  constexpr size_t kMaxMessageLength = 200;
  constexpr size_t kAtoBMtu = 33;
  constexpr size_t kBtoAMtu = 72;

  TestTransport transport_a_to_b(kAtoBMtu);
  TestTransport transport_b_to_a(kBtoAMtu);

  HdlcRpcEgress<kMaxPacketSize> egress_a_to_b("a->b", transport_a_to_b);
  HdlcRpcEgress<kMaxPacketSize> egress_b_to_a("b->a", transport_b_to_a);

  std::array a_tx_channels = {
      rpc::Channel::Create<kChannelAtoB>(&egress_a_to_b)};
  std::array b_tx_channels = {
      rpc::Channel::Create<kChannelAtoB>(&egress_b_to_a)};

  ServiceRegistry registry_a(a_tx_channels);
  ServiceRegistry registry_b(b_tx_channels);

  TestService test_service;
  registry_b.RegisterService(test_service);

  TestLocalEgress local_egress_a;
  local_egress_a.set_registry(registry_a);

  TestLocalEgress local_egress_b;
  local_egress_b.set_registry(registry_b);

  std::array a_rx_channels = {
      ChannelEgress{kChannelAtoB, local_egress_a},
  };
  std::array b_rx_channels = {
      ChannelEgress{kChannelAtoB, local_egress_b},
  };

  HdlcRpcIngress<kMaxPacketSize> ingress_a(a_rx_channels);
  HdlcRpcIngress<kMaxPacketSize> ingress_b(b_rx_channels);

  auto client =
      registry_a
          .CreateClient<pw_rpc_transport::testing::pw_rpc::pwpb::TestService>(
              kChannelAtoB);

  sync::ThreadNotification receiver1_done;
  sync::ThreadNotification receiver2_done;

  struct ReceiverState {
    InlineString<kMaxMessageLength> message;
    sync::ThreadNotification done;
  };

  ReceiverState receiver1;
  ReceiverState receiver2;
  receiver1.message.append(2 * transport_a_to_b.MaximumTransmissionUnit(), '*');
  receiver2.message.append(2 * transport_b_to_a.MaximumTransmissionUnit(), '>');

  auto call1 = client.Echo(
      {.msg = receiver1.message},
      [&receiver1](
          const pw_rpc_transport::testing::pwpb::EchoMessage::Message& response,
          Status status) {
        EXPECT_EQ(status, OkStatus());
        EXPECT_EQ(response.msg, receiver1.message);
        receiver1.done.release();
      },
      [&receiver1](Status status) {
        EXPECT_EQ(status, OkStatus());
        receiver1.done.release();
      });

  auto call2 = client.Echo(
      {.msg = receiver2.message},
      [&receiver2](
          const pw_rpc_transport::testing::pwpb::EchoMessage::Message& response,
          Status status) {
        EXPECT_EQ(status, OkStatus());
        EXPECT_EQ(response.msg, receiver2.message);
        receiver2.done.release();
      },
      [&receiver2](Status status) {
        EXPECT_EQ(status, OkStatus());
        receiver2.done.release();
      });

  // Calling `ingress_b.ProcessIncomingData` reads all packets from the
  // transport and dispatches them according to the ingress configuration.
  // Dispatching a packet generates a reply message: we then read it back at the
  // sender by calling `ingress_a.ProcessIncomingData`.
  EXPECT_EQ(ingress_b.ProcessIncomingData(transport_a_to_b.buffer()),
            OkStatus());
  EXPECT_EQ(ingress_a.ProcessIncomingData(transport_b_to_a.buffer()),
            OkStatus());

  receiver1.done.acquire();
  receiver2.done.acquire();
}

TEST(RpcEgressIngressTest, MalformedRpcPacket) {
  constexpr uint32_t kTestChannel = 1;
  constexpr size_t kMtu = 33;
  std::vector<std::byte> kMalformedPacket = {std::byte{0x42}, std::byte{0x74}};

  TestTransport transport(kMtu);
  SimpleRpcEgress<kMaxPacketSize> egress("test", transport);

  TestLocalEgress local_egress;
  std::array rx_channels = {
      ChannelEgress{kTestChannel, local_egress},
  };

  SimpleRpcIngress<kMaxPacketSize> ingress(rx_channels);

  EXPECT_EQ(egress.Send(kMalformedPacket), OkStatus());
  EXPECT_EQ(ingress.ProcessIncomingData(transport.buffer()), OkStatus());

  EXPECT_EQ(ingress.num_bad_packets(), 1u);
  EXPECT_EQ(ingress.num_overflow_channel_ids(), 0u);
  EXPECT_EQ(ingress.num_missing_egresses(), 0u);
  EXPECT_EQ(ingress.num_egress_errors(), 0u);
}

TEST(RpcEgressIngressTest, ChannelIdOverflow) {
  constexpr uint32_t kInvalidChannelId = 65;
  constexpr size_t kMtu = 128;

  TestTransport transport(kMtu);
  SimpleRpcEgress<kMaxPacketSize> egress("test", transport);

  std::array sender_tx_channels = {
      rpc::Channel::Create<kInvalidChannelId>(&egress)};

  ServiceRegistry registry(sender_tx_channels);
  auto client =
      registry
          .CreateClient<pw_rpc_transport::testing::pw_rpc::pwpb::TestService>(
              kInvalidChannelId);

  SimpleRpcIngress<kMaxPacketSize> ingress;

  auto receiver = client.Echo({.msg = "test"});

  EXPECT_EQ(ingress.ProcessIncomingData(transport.buffer()), OkStatus());

  EXPECT_EQ(ingress.num_bad_packets(), 0u);
  EXPECT_EQ(ingress.num_overflow_channel_ids(), 1u);
  EXPECT_EQ(ingress.num_missing_egresses(), 0u);
  EXPECT_EQ(ingress.num_egress_errors(), 0u);
}

TEST(RpcEgressIngressTest, MissingEgressForIncomingPacket) {
  constexpr uint32_t kChannelA = 22;
  constexpr uint32_t kChannelB = 33;
  constexpr size_t kMtu = 128;

  TestTransport transport(kMtu);
  SimpleRpcEgress<kMaxPacketSize> egress("test", transport);

  std::array sender_tx_channels = {rpc::Channel::Create<kChannelA>(&egress)};

  ServiceRegistry registry(sender_tx_channels);
  auto client =
      registry
          .CreateClient<pw_rpc_transport::testing::pw_rpc::pwpb::TestService>(
              kChannelA);

  std::array ingress_channels = {ChannelEgress(kChannelB, egress)};
  SimpleRpcIngress<kMaxPacketSize> ingress(ingress_channels);

  auto receiver = client.Echo({.msg = "test"});

  EXPECT_EQ(ingress.ProcessIncomingData(transport.buffer()), OkStatus());

  EXPECT_EQ(ingress.num_bad_packets(), 0u);
  EXPECT_EQ(ingress.num_overflow_channel_ids(), 0u);
  EXPECT_EQ(ingress.num_missing_egresses(), 1u);
  EXPECT_EQ(ingress.num_egress_errors(), 0u);
}

TEST(RpcEgressIngressTest, EgressSendFailureForIncomingPacket) {
  constexpr uint32_t kChannelId = 22;
  constexpr size_t kMtu = 128;

  TestTransport good_transport(kMtu, /*is_faulty=*/false);
  TestTransport bad_transport(kMtu, /*is_faulty=*/true);
  SimpleRpcEgress<kMaxPacketSize> good_egress("test", good_transport);
  SimpleRpcEgress<kMaxPacketSize> bad_egress("test", bad_transport);

  std::array sender_tx_channels = {
      rpc::Channel::Create<kChannelId>(&good_egress)};

  ServiceRegistry registry(sender_tx_channels);
  auto client =
      registry
          .CreateClient<pw_rpc_transport::testing::pw_rpc::pwpb::TestService>(
              kChannelId);

  std::array ingress_channels = {ChannelEgress(kChannelId, bad_egress)};
  SimpleRpcIngress<kMaxPacketSize> ingress(ingress_channels);

  auto receiver = client.Echo({.msg = "test"});

  EXPECT_EQ(ingress.ProcessIncomingData(good_transport.buffer()), OkStatus());

  EXPECT_EQ(ingress.num_bad_packets(), 0u);
  EXPECT_EQ(ingress.num_overflow_channel_ids(), 0u);
  EXPECT_EQ(ingress.num_missing_egresses(), 0u);
  EXPECT_EQ(ingress.num_egress_errors(), 1u);
}

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