Fix destruction order of PromoResourceService and NTPResourceCache
[chromium-blink-merge.git] / net / dns / mock_mdns_socket_factory.cc
blob0bb8c5401dd8a8c7fb663fb2ec9bc298bac9bb73
1 // Copyright 2013 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.
5 #include <algorithm>
7 #include "net/base/net_errors.h"
8 #include "net/dns/mock_mdns_socket_factory.h"
10 using testing::_;
11 using testing::Invoke;
13 namespace net {
15 MockMDnsDatagramServerSocket::MockMDnsDatagramServerSocket(
16 AddressFamily address_family) {
17 local_address_ = GetMDnsIPEndPoint(address_family);
20 MockMDnsDatagramServerSocket::~MockMDnsDatagramServerSocket() {
23 int MockMDnsDatagramServerSocket::SendTo(IOBuffer* buf, int buf_len,
24 const IPEndPoint& address,
25 const CompletionCallback& callback) {
26 return SendToInternal(std::string(buf->data(), buf_len), address.ToString(),
27 callback);
30 int MockMDnsDatagramServerSocket::GetLocalAddress(IPEndPoint* address) const {
31 *address = local_address_;
32 return OK;
35 void MockMDnsDatagramServerSocket::SetResponsePacket(
36 std::string response_packet) {
37 response_packet_ = response_packet;
40 int MockMDnsDatagramServerSocket::HandleRecvNow(
41 IOBuffer* buffer, int size, IPEndPoint* address,
42 const CompletionCallback& callback) {
43 int size_returned =
44 std::min(response_packet_.size(), static_cast<size_t>(size));
45 memcpy(buffer->data(), response_packet_.data(), size_returned);
46 return size_returned;
49 int MockMDnsDatagramServerSocket::HandleRecvLater(
50 IOBuffer* buffer, int size, IPEndPoint* address,
51 const CompletionCallback& callback) {
52 int rv = HandleRecvNow(buffer, size, address, callback);
53 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, rv));
54 return ERR_IO_PENDING;
57 MockMDnsSocketFactory::MockMDnsSocketFactory() {
60 MockMDnsSocketFactory::~MockMDnsSocketFactory() {
63 void MockMDnsSocketFactory::CreateSockets(
64 ScopedVector<DatagramServerSocket>* sockets) {
65 CreateSocket(ADDRESS_FAMILY_IPV4, sockets);
66 CreateSocket(ADDRESS_FAMILY_IPV6, sockets);
69 void MockMDnsSocketFactory::CreateSocket(
70 AddressFamily address_family,
71 ScopedVector<DatagramServerSocket>* sockets) {
72 scoped_ptr<testing::NiceMock<MockMDnsDatagramServerSocket> > new_socket(
73 new testing::NiceMock<MockMDnsDatagramServerSocket>(address_family));
75 ON_CALL(*new_socket, SendToInternal(_, _, _))
76 .WillByDefault(Invoke(
77 this,
78 &MockMDnsSocketFactory::SendToInternal));
80 ON_CALL(*new_socket, RecvFrom(_, _, _, _))
81 .WillByDefault(Invoke(
82 this,
83 &MockMDnsSocketFactory::RecvFromInternal));
85 sockets->push_back(new_socket.release());
88 void MockMDnsSocketFactory::SimulateReceive(const uint8* packet, int size) {
89 DCHECK(recv_buffer_size_ >= size);
90 DCHECK(recv_buffer_.get());
91 DCHECK(!recv_callback_.is_null());
93 memcpy(recv_buffer_->data(), packet, size);
94 CompletionCallback recv_callback = recv_callback_;
95 recv_callback_.Reset();
96 recv_callback.Run(size);
99 int MockMDnsSocketFactory::RecvFromInternal(
100 IOBuffer* buffer, int size,
101 IPEndPoint* address,
102 const CompletionCallback& callback) {
103 recv_buffer_ = buffer;
104 recv_buffer_size_ = size;
105 recv_callback_ = callback;
106 return ERR_IO_PENDING;
109 int MockMDnsSocketFactory::SendToInternal(
110 const std::string& packet, const std::string& address,
111 const CompletionCallback& callback) {
112 OnSendTo(packet);
113 return packet.size();
116 } // namespace net