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.
7 #include "net/base/net_errors.h"
8 #include "net/dns/mock_mdns_socket_factory.h"
11 using testing::Invoke
;
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(),
30 int MockMDnsDatagramServerSocket::GetLocalAddress(IPEndPoint
* address
) const {
31 *address
= local_address_
;
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
) {
44 std::min(response_packet_
.size(), static_cast<size_t>(size
));
45 memcpy(buffer
->data(), response_packet_
.data(), 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(
78 &MockMDnsSocketFactory::SendToInternal
));
80 ON_CALL(*new_socket
, RecvFrom(_
, _
, _
, _
))
81 .WillByDefault(Invoke(
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
,
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
) {
113 return packet
.size();