| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <algorithm> | 5 #include <algorithm> |
| 6 | 6 |
| 7 #include "net/base/net_errors.h" | 7 #include "net/base/net_errors.h" |
| 8 #include "net/dns/mock_mdns_socket_factory.h" | 8 #include "net/dns/mock_mdns_socket_factory.h" |
| 9 | 9 |
| 10 using testing::_; | 10 using testing::_; |
| 11 using testing::Invoke; | 11 using testing::Invoke; |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 | 14 |
| 15 MockMDnsDatagramServerSocket::MockMDnsDatagramServerSocket() { | 15 MockMDnsDatagramServerSocket::MockMDnsDatagramServerSocket( |
| 16 AddressFamily address_family) { |
| 17 local_address_ = GetMDnsIPEndPoint(address_family); |
| 16 } | 18 } |
| 17 | 19 |
| 18 MockMDnsDatagramServerSocket::~MockMDnsDatagramServerSocket() { | 20 MockMDnsDatagramServerSocket::~MockMDnsDatagramServerSocket() { |
| 19 } | 21 } |
| 20 | 22 |
| 21 int MockMDnsDatagramServerSocket::SendTo(IOBuffer* buf, int buf_len, | 23 int MockMDnsDatagramServerSocket::SendTo(IOBuffer* buf, int buf_len, |
| 22 const IPEndPoint& address, | 24 const IPEndPoint& address, |
| 23 const CompletionCallback& callback) { | 25 const CompletionCallback& callback) { |
| 24 return SendToInternal(std::string(buf->data(), buf_len), address.ToString(), | 26 return SendToInternal(std::string(buf->data(), buf_len), address.ToString(), |
| 25 callback); | 27 callback); |
| 26 } | 28 } |
| 27 | 29 |
| 28 int MockMDnsDatagramServerSocket::Listen(const IPEndPoint& address) { | 30 int MockMDnsDatagramServerSocket::GetLocalAddress(IPEndPoint* address) const { |
| 29 return ListenInternal(address.ToString()); | 31 *address = local_address_; |
| 30 } | 32 return OK; |
| 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 } | 33 } |
| 41 | 34 |
| 42 void MockMDnsDatagramServerSocket::SetResponsePacket( | 35 void MockMDnsDatagramServerSocket::SetResponsePacket( |
| 43 std::string response_packet) { | 36 std::string response_packet) { |
| 44 response_packet_ = response_packet; | 37 response_packet_ = response_packet; |
| 45 } | 38 } |
| 46 | 39 |
| 47 int MockMDnsDatagramServerSocket::HandleRecvNow( | 40 int MockMDnsDatagramServerSocket::HandleRecvNow( |
| 48 IOBuffer* buffer, int size, IPEndPoint* address, | 41 IOBuffer* buffer, int size, IPEndPoint* address, |
| 49 const CompletionCallback& callback) { | 42 const CompletionCallback& callback) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 60 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, rv)); | 53 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, rv)); |
| 61 return ERR_IO_PENDING; | 54 return ERR_IO_PENDING; |
| 62 } | 55 } |
| 63 | 56 |
| 64 MockMDnsSocketFactory::MockMDnsSocketFactory() { | 57 MockMDnsSocketFactory::MockMDnsSocketFactory() { |
| 65 } | 58 } |
| 66 | 59 |
| 67 MockMDnsSocketFactory::~MockMDnsSocketFactory() { | 60 MockMDnsSocketFactory::~MockMDnsSocketFactory() { |
| 68 } | 61 } |
| 69 | 62 |
| 70 scoped_ptr<DatagramServerSocket> MockMDnsSocketFactory::CreateSocket() { | 63 void MockMDnsSocketFactory::CreateSockets( |
| 71 scoped_ptr<MockMDnsDatagramServerSocket> new_socket( | 64 ScopedVector<DatagramServerSocket>* sockets) { |
| 72 new testing:: NiceMock<MockMDnsDatagramServerSocket>); | 65 CreateSocket(ADDRESS_FAMILY_IPV4, sockets); |
| 66 CreateSocket(ADDRESS_FAMILY_IPV6, sockets); |
| 67 } |
| 68 |
| 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)); |
| 73 | 74 |
| 74 ON_CALL(*new_socket, SendToInternal(_, _, _)) | 75 ON_CALL(*new_socket, SendToInternal(_, _, _)) |
| 75 .WillByDefault(Invoke( | 76 .WillByDefault(Invoke( |
| 76 this, | 77 this, |
| 77 &MockMDnsSocketFactory::SendToInternal)); | 78 &MockMDnsSocketFactory::SendToInternal)); |
| 78 | 79 |
| 79 ON_CALL(*new_socket, RecvFrom(_, _, _, _)) | 80 ON_CALL(*new_socket, RecvFrom(_, _, _, _)) |
| 80 .WillByDefault(Invoke( | 81 .WillByDefault(Invoke( |
| 81 this, | 82 this, |
| 82 &MockMDnsSocketFactory::RecvFromInternal)); | 83 &MockMDnsSocketFactory::RecvFromInternal)); |
| 83 | 84 |
| 84 return new_socket.PassAs<DatagramServerSocket>(); | 85 sockets->push_back(new_socket.release()); |
| 85 } | 86 } |
| 86 | 87 |
| 87 void MockMDnsSocketFactory::SimulateReceive(const uint8* packet, int size) { | 88 void MockMDnsSocketFactory::SimulateReceive(const uint8* packet, int size) { |
| 88 DCHECK(recv_buffer_size_ >= size); | 89 DCHECK(recv_buffer_size_ >= size); |
| 89 DCHECK(recv_buffer_.get()); | 90 DCHECK(recv_buffer_.get()); |
| 90 DCHECK(!recv_callback_.is_null()); | 91 DCHECK(!recv_callback_.is_null()); |
| 91 | 92 |
| 92 memcpy(recv_buffer_->data(), packet, size); | 93 memcpy(recv_buffer_->data(), packet, size); |
| 93 CompletionCallback recv_callback = recv_callback_; | 94 CompletionCallback recv_callback = recv_callback_; |
| 94 recv_callback_.Reset(); | 95 recv_callback_.Reset(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 106 } | 107 } |
| 107 | 108 |
| 108 int MockMDnsSocketFactory::SendToInternal( | 109 int MockMDnsSocketFactory::SendToInternal( |
| 109 const std::string& packet, const std::string& address, | 110 const std::string& packet, const std::string& address, |
| 110 const CompletionCallback& callback) { | 111 const CompletionCallback& callback) { |
| 111 OnSendTo(packet); | 112 OnSendTo(packet); |
| 112 return packet.size(); | 113 return packet.size(); |
| 113 } | 114 } |
| 114 | 115 |
| 115 } // namespace net | 116 } // namespace net |
| OLD | NEW |