1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef SERVICES_NETWORK_UDP_SOCKET_TEST_UTIL_H_
6 #define SERVICES_NETWORK_UDP_SOCKET_TEST_UTIL_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "base/containers/span.h"
14 #include "base/macros.h"
15 #include "base/optional.h"
16 #include "base/run_loop.h"
17 #include "mojo/public/cpp/bindings/remote.h"
18 #include "net/base/ip_endpoint.h"
19 #include "net/base/net_errors.h"
20 #include "services/network/public/mojom/udp_socket.mojom.h"
21 
22 namespace network {
23 
24 namespace test {
25 
26 // Helper functions to invoke the corresponding mojo APIs and wait for
27 // completion.
28 class UDPSocketTestHelper {
29  public:
30   explicit UDPSocketTestHelper(mojo::Remote<mojom::UDPSocket>* socket);
31   ~UDPSocketTestHelper();
32   int ConnectSync(const net::IPEndPoint& remote_addr,
33                   mojom::UDPSocketOptionsPtr options,
34                   net::IPEndPoint* local_addr_out);
35   int BindSync(const net::IPEndPoint& local_addr,
36                mojom::UDPSocketOptionsPtr options,
37                net::IPEndPoint* local_addr_out);
38   int SendToSync(const net::IPEndPoint& remote_addr,
39                  const std::vector<uint8_t>& input);
40   int SendSync(const std::vector<uint8_t>& input);
41   int SetBroadcastSync(bool broadcast);
42   int SetSendBufferSizeSync(int send_buffer_size);
43   int SetReceiveBufferSizeSync(int receive_buffer_size);
44   int JoinGroupSync(const net::IPAddress& group_address);
45   int LeaveGroupSync(const net::IPAddress& group_address);
46 
47  private:
48   mojo::Remote<mojom::UDPSocket>* socket_;
49 };
50 
51 // An implementation of mojom::UDPSocketListener that records received results.
52 class UDPSocketListenerImpl : public mojom::UDPSocketListener {
53  public:
54   struct ReceivedResult {
55     ReceivedResult(int net_error_arg,
56                    const base::Optional<net::IPEndPoint>& src_addr_arg,
57                    base::Optional<std::vector<uint8_t>> data_arg);
58     ReceivedResult(const ReceivedResult& other);
59     ~ReceivedResult();
60 
61     int net_error;
62     base::Optional<net::IPEndPoint> src_addr;
63     base::Optional<std::vector<uint8_t>> data;
64   };
65 
66   UDPSocketListenerImpl();
67   ~UDPSocketListenerImpl() override;
68 
results()69   const std::vector<ReceivedResult>& results() const { return results_; }
70 
71   void WaitForReceivedResults(size_t count);
72 
73  private:
74   void OnReceived(int32_t result,
75                   const base::Optional<net::IPEndPoint>& src_addr,
76                   base::Optional<base::span<const uint8_t>> data) override;
77   std::unique_ptr<base::RunLoop> run_loop_;
78   std::vector<ReceivedResult> results_;
79   size_t expected_receive_count_;
80 
81   DISALLOW_COPY_AND_ASSIGN(UDPSocketListenerImpl);
82 };
83 
84 }  // namespace test
85 
86 }  // namespace network
87 
88 #endif  // SERVICES_NETWORK_UDP_SOCKET_TEST_UTIL_H_
89