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. 4 5 #include <algorithm> 6 7 #include "net/base/net_errors.h" 8 #include "net/dns/mock_mdns_socket_factory.h" 9 10 using testing::_; 11 using testing::Invoke; 12 13 namespace net { 14 15 MockMDnsDatagramServerSocket::MockMDnsDatagramServerSocket() { 16 } 17 18 MockMDnsDatagramServerSocket::~MockMDnsDatagramServerSocket() { 19 } 20 21 int MockMDnsDatagramServerSocket::SendTo(IOBuffer* buf, int buf_len, 22 const IPEndPoint& address, 23 const CompletionCallback& callback) { 24 return SendToInternal(std::string(buf->data(), buf_len), address.ToString(), 25 callback); 26 } 27 28 int MockMDnsDatagramServerSocket::Listen(const IPEndPoint& address) { 29 return ListenInternal(address.ToString()); 30 } 31 32 int MockMDnsDatagramServerSocket::JoinGroup( 33 const IPAddressNumber& group_address) const { 34 return JoinGroupInternal(IPAddressToString(group_address)); 35 } 36 37 int MockMDnsDatagramServerSocket::LeaveGroup( 38 const IPAddressNumber& group_address) const { 39 return LeaveGroupInternal(IPAddressToString(group_address)); 40 } 41 42 void MockMDnsDatagramServerSocket::SetResponsePacket( 43 std::string response_packet) { 44 response_packet_ = response_packet; 45 } 46 47 int MockMDnsDatagramServerSocket::HandleRecvNow( 48 IOBuffer* buffer, int size, IPEndPoint* address, 49 const CompletionCallback& callback) { 50 int size_returned = 51 std::min(response_packet_.size(), static_cast<size_t>(size)); 52 memcpy(buffer->data(), response_packet_.data(), size_returned); 53 return size_returned; 54 } 55 56 int MockMDnsDatagramServerSocket::HandleRecvLater( 57 IOBuffer* buffer, int size, IPEndPoint* address, 58 const CompletionCallback& callback) { 59 int rv = HandleRecvNow(buffer, size, address, callback); 60 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, rv)); 61 return ERR_IO_PENDING; 62 } 63 64 MockMDnsSocketFactory::MockMDnsSocketFactory() { 65 } 66 67 MockMDnsSocketFactory::~MockMDnsSocketFactory() { 68 } 69 70 scoped_ptr<DatagramServerSocket> MockMDnsSocketFactory::CreateSocket() { 71 scoped_ptr<MockMDnsDatagramServerSocket> new_socket( 72 new testing:: NiceMock<MockMDnsDatagramServerSocket>); 73 74 ON_CALL(*new_socket, SendToInternal(_, _, _)) 75 .WillByDefault(Invoke( 76 this, 77 &MockMDnsSocketFactory::SendToInternal)); 78 79 ON_CALL(*new_socket, RecvFrom(_, _, _, _)) 80 .WillByDefault(Invoke( 81 this, 82 &MockMDnsSocketFactory::RecvFromInternal)); 83 84 return new_socket.PassAs<DatagramServerSocket>(); 85 } 86 87 void MockMDnsSocketFactory::SimulateReceive(const uint8* packet, int size) { 88 DCHECK(recv_buffer_size_ >= size); 89 DCHECK(recv_buffer_.get()); 90 DCHECK(!recv_callback_.is_null()); 91 92 memcpy(recv_buffer_->data(), packet, size); 93 CompletionCallback recv_callback = recv_callback_; 94 recv_callback_.Reset(); 95 recv_callback.Run(size); 96 } 97 98 int MockMDnsSocketFactory::RecvFromInternal( 99 IOBuffer* buffer, int size, 100 IPEndPoint* address, 101 const CompletionCallback& callback) { 102 recv_buffer_ = buffer; 103 recv_buffer_size_ = size; 104 recv_callback_ = callback; 105 return ERR_IO_PENDING; 106 } 107 108 int MockMDnsSocketFactory::SendToInternal( 109 const std::string& packet, const std::string& address, 110 const CompletionCallback& callback) { 111 OnSendTo(packet); 112 return packet.size(); 113 } 114 115 } // namespace net 116