| 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 } | 16 } |
| 17 | 17 |
| 18 MockMDnsDatagramServerSocket::~MockMDnsDatagramServerSocket() { | 18 MockMDnsDatagramServerSocket::~MockMDnsDatagramServerSocket() { |
| 19 } | 19 } |
| 20 | 20 |
| 21 int MockMDnsDatagramServerSocket::SendTo(IOBuffer* buf, int buf_len, | 21 int MockMDnsDatagramServerSocket::SendTo(IOBuffer* buf, int buf_len, |
| 22 const IPEndPoint& address, | 22 const CompletionCallback& callback) { |
| 23 const CompletionCallback& callback) { | 23 return SendToInternal(std::string(buf->data(), buf_len), 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 } | 24 } |
| 41 | 25 |
| 42 void MockMDnsDatagramServerSocket::SetResponsePacket( | 26 void MockMDnsDatagramServerSocket::SetResponsePacket( |
| 43 std::string response_packet) { | 27 std::string response_packet) { |
| 44 response_packet_ = response_packet; | 28 response_packet_ = response_packet; |
| 45 } | 29 } |
| 46 | 30 |
| 47 int MockMDnsDatagramServerSocket::HandleRecvNow( | 31 int MockMDnsDatagramServerSocket::HandleRecvNow( |
| 48 IOBuffer* buffer, int size, IPEndPoint* address, | 32 IOBuffer* buffer, int size, IPEndPoint* address, |
| 49 const CompletionCallback& callback) { | 33 const CompletionCallback& callback) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 60 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, rv)); | 44 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, rv)); |
| 61 return ERR_IO_PENDING; | 45 return ERR_IO_PENDING; |
| 62 } | 46 } |
| 63 | 47 |
| 64 MockMDnsSocketFactory::MockMDnsSocketFactory() { | 48 MockMDnsSocketFactory::MockMDnsSocketFactory() { |
| 65 } | 49 } |
| 66 | 50 |
| 67 MockMDnsSocketFactory::~MockMDnsSocketFactory() { | 51 MockMDnsSocketFactory::~MockMDnsSocketFactory() { |
| 68 } | 52 } |
| 69 | 53 |
| 70 scoped_ptr<DatagramServerSocket> MockMDnsSocketFactory::CreateSocket() { | 54 void MockMDnsSocketFactory::CreateSockets( |
| 71 scoped_ptr<MockMDnsDatagramServerSocket> new_socket( | 55 ScopedVector<MDnsConnection::Socket>* sockets) { |
| 72 new testing:: NiceMock<MockMDnsDatagramServerSocket>); | 56 for (size_t i = 0; i < 2; ++i) { |
| 57 scoped_ptr<MockMDnsDatagramServerSocket> new_socket( |
| 58 new testing::NiceMock<MockMDnsDatagramServerSocket>()); |
| 73 | 59 |
| 74 ON_CALL(*new_socket, SendToInternal(_, _, _)) | 60 ON_CALL(*new_socket, SendToInternal(_, _)) |
| 75 .WillByDefault(Invoke( | 61 .WillByDefault(Invoke( |
| 76 this, | 62 this, |
| 77 &MockMDnsSocketFactory::SendToInternal)); | 63 &MockMDnsSocketFactory::SendToInternal)); |
| 78 | 64 |
| 79 ON_CALL(*new_socket, RecvFrom(_, _, _, _)) | 65 ON_CALL(*new_socket, RecvFrom(_, _, _, _)) |
| 80 .WillByDefault(Invoke( | 66 .WillByDefault(Invoke( |
| 81 this, | 67 this, |
| 82 &MockMDnsSocketFactory::RecvFromInternal)); | 68 &MockMDnsSocketFactory::RecvFromInternal)); |
| 83 | 69 |
| 84 return new_socket.PassAs<DatagramServerSocket>(); | 70 sockets->push_back(new_socket.release()); |
| 71 } |
| 85 } | 72 } |
| 86 | 73 |
| 87 void MockMDnsSocketFactory::SimulateReceive(const uint8* packet, int size) { | 74 void MockMDnsSocketFactory::SimulateReceive(const uint8* packet, int size) { |
| 88 DCHECK(recv_buffer_size_ >= size); | 75 DCHECK(recv_buffer_size_ >= size); |
| 89 DCHECK(recv_buffer_.get()); | 76 DCHECK(recv_buffer_.get()); |
| 90 DCHECK(!recv_callback_.is_null()); | 77 DCHECK(!recv_callback_.is_null()); |
| 91 | 78 |
| 92 memcpy(recv_buffer_->data(), packet, size); | 79 memcpy(recv_buffer_->data(), packet, size); |
| 93 CompletionCallback recv_callback = recv_callback_; | 80 CompletionCallback recv_callback = recv_callback_; |
| 94 recv_callback_.Reset(); | 81 recv_callback_.Reset(); |
| 95 recv_callback.Run(size); | 82 recv_callback.Run(size); |
| 96 } | 83 } |
| 97 | 84 |
| 98 int MockMDnsSocketFactory::RecvFromInternal( | 85 int MockMDnsSocketFactory::RecvFromInternal( |
| 99 IOBuffer* buffer, int size, | 86 IOBuffer* buffer, int size, |
| 100 IPEndPoint* address, | 87 IPEndPoint* address, |
| 101 const CompletionCallback& callback) { | 88 const CompletionCallback& callback) { |
| 102 recv_buffer_ = buffer; | 89 recv_buffer_ = buffer; |
| 103 recv_buffer_size_ = size; | 90 recv_buffer_size_ = size; |
| 104 recv_callback_ = callback; | 91 recv_callback_ = callback; |
| 105 return ERR_IO_PENDING; | 92 return ERR_IO_PENDING; |
| 106 } | 93 } |
| 107 | 94 |
| 108 int MockMDnsSocketFactory::SendToInternal( | 95 int MockMDnsSocketFactory::SendToInternal(const std::string& packet, |
| 109 const std::string& packet, const std::string& address, | 96 const CompletionCallback& callback) { |
| 110 const CompletionCallback& callback) { | |
| 111 OnSendTo(packet); | 97 OnSendTo(packet); |
| 112 return packet.size(); | 98 return packet.size(); |
| 113 } | 99 } |
| 114 | 100 |
| 115 } // namespace net | 101 } // namespace net |
| OLD | NEW |