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 "base/location.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "net/base/net_errors.h"
11 #include "net/dns/mock_mdns_socket_factory.h"
14 using testing::Invoke
;
18 MockMDnsDatagramServerSocket::MockMDnsDatagramServerSocket(
19 AddressFamily address_family
) {
20 local_address_
= GetMDnsIPEndPoint(address_family
);
23 MockMDnsDatagramServerSocket::~MockMDnsDatagramServerSocket() {
26 int MockMDnsDatagramServerSocket::SendTo(IOBuffer
* buf
, int buf_len
,
27 const IPEndPoint
& address
,
28 const CompletionCallback
& callback
) {
29 return SendToInternal(std::string(buf
->data(), buf_len
), address
.ToString(),
33 int MockMDnsDatagramServerSocket::GetLocalAddress(IPEndPoint
* address
) const {
34 *address
= local_address_
;
38 void MockMDnsDatagramServerSocket::SetResponsePacket(
39 std::string response_packet
) {
40 response_packet_
= response_packet
;
43 int MockMDnsDatagramServerSocket::HandleRecvNow(
44 IOBuffer
* buffer
, int size
, IPEndPoint
* address
,
45 const CompletionCallback
& callback
) {
47 std::min(response_packet_
.size(), static_cast<size_t>(size
));
48 memcpy(buffer
->data(), response_packet_
.data(), size_returned
);
52 int MockMDnsDatagramServerSocket::HandleRecvLater(
53 IOBuffer
* buffer
, int size
, IPEndPoint
* address
,
54 const CompletionCallback
& callback
) {
55 int rv
= HandleRecvNow(buffer
, size
, address
, callback
);
56 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
,
57 base::Bind(callback
, rv
));
58 return ERR_IO_PENDING
;
61 MockMDnsSocketFactory::MockMDnsSocketFactory() {
64 MockMDnsSocketFactory::~MockMDnsSocketFactory() {
67 void MockMDnsSocketFactory::CreateSockets(
68 ScopedVector
<DatagramServerSocket
>* sockets
) {
69 CreateSocket(ADDRESS_FAMILY_IPV4
, sockets
);
70 CreateSocket(ADDRESS_FAMILY_IPV6
, sockets
);
73 void MockMDnsSocketFactory::CreateSocket(
74 AddressFamily address_family
,
75 ScopedVector
<DatagramServerSocket
>* sockets
) {
76 scoped_ptr
<testing::NiceMock
<MockMDnsDatagramServerSocket
> > new_socket(
77 new testing::NiceMock
<MockMDnsDatagramServerSocket
>(address_family
));
79 ON_CALL(*new_socket
, SendToInternal(_
, _
, _
))
80 .WillByDefault(Invoke(
82 &MockMDnsSocketFactory::SendToInternal
));
84 ON_CALL(*new_socket
, RecvFrom(_
, _
, _
, _
))
85 .WillByDefault(Invoke(
87 &MockMDnsSocketFactory::RecvFromInternal
));
89 sockets
->push_back(new_socket
.Pass());
92 void MockMDnsSocketFactory::SimulateReceive(const uint8
* packet
, int size
) {
93 DCHECK(recv_buffer_size_
>= size
);
94 DCHECK(recv_buffer_
.get());
95 DCHECK(!recv_callback_
.is_null());
97 memcpy(recv_buffer_
->data(), packet
, size
);
98 CompletionCallback recv_callback
= recv_callback_
;
99 recv_callback_
.Reset();
100 recv_callback
.Run(size
);
103 int MockMDnsSocketFactory::RecvFromInternal(
104 IOBuffer
* buffer
, int size
,
106 const CompletionCallback
& callback
) {
107 recv_buffer_
= buffer
;
108 recv_buffer_size_
= size
;
109 recv_callback_
= callback
;
110 return ERR_IO_PENDING
;
113 int MockMDnsSocketFactory::SendToInternal(
114 const std::string
& packet
, const std::string
& address
,
115 const CompletionCallback
& callback
) {
117 return packet
.size();