xref: /aosp_15_r20/external/pigweed/pw_rpc/integration_testing.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2021 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/integration_testing.h"
16 
17 #include <limits>
18 
19 #include "pw_log/log.h"
20 #include "pw_rpc/integration_test_socket_client.h"
21 #include "pw_unit_test/framework.h"
22 #include "pw_unit_test/logging_event_handler.h"
23 
24 namespace pw::rpc::integration_test {
25 namespace {
26 
27 // Hard-coded to 1055 bytes, which is enough to fit 512-byte payloads when using
28 // HDLC framing.
29 SocketClientContext<1055> context;
30 unit_test::LoggingEventHandler log_test_events;
31 
32 }  // namespace
33 
client()34 Client& client() { return context.client(); }
35 
SetClientSockOpt(int level,int optname,const void * optval,unsigned int optlen)36 int SetClientSockOpt(int level,
37                      int optname,
38                      const void* optval,
39                      unsigned int optlen) {
40   return context.SetSockOpt(level, optname, optval, optlen);
41 }
42 
SetEgressChannelManipulator(ChannelManipulator * new_channel_manipulator)43 void SetEgressChannelManipulator(ChannelManipulator* new_channel_manipulator) {
44   context.SetEgressChannelManipulator(new_channel_manipulator);
45 }
46 
SetIngressChannelManipulator(ChannelManipulator * new_channel_manipulator)47 void SetIngressChannelManipulator(ChannelManipulator* new_channel_manipulator) {
48   context.SetIngressChannelManipulator(new_channel_manipulator);
49 }
50 
InitializeClient(int port)51 Status InitializeClient(int port) {
52   unit_test::RegisterEventHandler(&log_test_events);
53   if (port <= 0 || port > std::numeric_limits<uint16_t>::max()) {
54     PW_LOG_CRITICAL("Port numbers must be between 1 and 65535; %d is invalid",
55                     port);
56     return Status::InvalidArgument();
57   }
58 
59   PW_LOG_INFO("Connecting to pw_rpc client at localhost:%d", port);
60   return context.Start(port);
61 }
62 
TerminateClient()63 void TerminateClient() { context.Terminate(); }
64 
InitializeClient(int argc,char * argv[],const char * usage_args)65 Status InitializeClient(int argc, char* argv[], const char* usage_args) {
66   if (argc < 2) {
67     PW_LOG_INFO("Usage: %s %s", argv[0], usage_args);
68     return Status::InvalidArgument();
69   }
70 
71   const int port = std::atoi(argv[1]);
72   return InitializeClient(port);
73 }
74 
75 }  // namespace pw::rpc::integration_test
76