• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2022 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef P2P_BASE_MOCK_DNS_RESOLVING_PACKET_SOCKET_FACTORY_H_
12 #define P2P_BASE_MOCK_DNS_RESOLVING_PACKET_SOCKET_FACTORY_H_
13 
14 #include <functional>
15 #include <memory>
16 
17 #include "api/test/mock_async_dns_resolver.h"
18 #include "p2p/base/basic_packet_socket_factory.h"
19 
20 namespace rtc {
21 
22 // A PacketSocketFactory implementation for tests that uses a mock DnsResolver
23 // and allows setting expectations on the resolver and results.
24 class MockDnsResolvingPacketSocketFactory : public BasicPacketSocketFactory {
25  public:
26   using Expectations = std::function<void(webrtc::MockAsyncDnsResolver*,
27                                           webrtc::MockAsyncDnsResolverResult*)>;
28 
MockDnsResolvingPacketSocketFactory(SocketFactory * socket_factory)29   explicit MockDnsResolvingPacketSocketFactory(SocketFactory* socket_factory)
30       : BasicPacketSocketFactory(socket_factory) {}
31 
CreateAsyncDnsResolver()32   std::unique_ptr<webrtc::AsyncDnsResolverInterface> CreateAsyncDnsResolver()
33       override {
34     std::unique_ptr<webrtc::MockAsyncDnsResolver> resolver =
35         std::make_unique<webrtc::MockAsyncDnsResolver>();
36     if (expectations_) {
37       expectations_(resolver.get(), &resolver_result_);
38     }
39     return resolver;
40   }
41 
SetExpectations(Expectations expectations)42   void SetExpectations(Expectations expectations) {
43     expectations_ = expectations;
44   }
45 
46  private:
47   webrtc::MockAsyncDnsResolverResult resolver_result_;
48   Expectations expectations_;
49 };
50 
51 }  // namespace rtc
52 
53 #endif  // P2P_BASE_MOCK_DNS_RESOLVING_PACKET_SOCKET_FACTORY_H_
54