Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: net/udp/udp_socket_unittest.cc

Issue 15829004: Update net/ to use scoped_refptr<T>::get() rather than implicit "operator T*" (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: license twerk Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/udp/udp_socket_libevent.cc ('k') | net/url_request/test_url_fetcher_factory.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "net/udp/udp_client_socket.h" 5 #include "net/udp/udp_client_socket.h"
6 #include "net/udp/udp_server_socket.h" 6 #include "net/udp/udp_server_socket.h"
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
(...skipping 15 matching lines...) Expand all
26 class UDPSocketTest : public PlatformTest { 26 class UDPSocketTest : public PlatformTest {
27 public: 27 public:
28 UDPSocketTest() 28 UDPSocketTest()
29 : buffer_(new IOBufferWithSize(kMaxRead)) { 29 : buffer_(new IOBufferWithSize(kMaxRead)) {
30 } 30 }
31 31
32 // Blocks until data is read from the socket. 32 // Blocks until data is read from the socket.
33 std::string RecvFromSocket(UDPServerSocket* socket) { 33 std::string RecvFromSocket(UDPServerSocket* socket) {
34 TestCompletionCallback callback; 34 TestCompletionCallback callback;
35 35
36 int rv = socket->RecvFrom(buffer_, kMaxRead, &recv_from_address_, 36 int rv = socket->RecvFrom(
37 callback.callback()); 37 buffer_.get(), kMaxRead, &recv_from_address_, callback.callback());
38 if (rv == ERR_IO_PENDING) 38 if (rv == ERR_IO_PENDING)
39 rv = callback.WaitForResult(); 39 rv = callback.WaitForResult();
40 if (rv < 0) 40 if (rv < 0)
41 return std::string(); // error! 41 return std::string(); // error!
42 return std::string(buffer_->data(), rv); 42 return std::string(buffer_->data(), rv);
43 } 43 }
44 44
45 // Loop until |msg| has been written to the socket or until an 45 // Loop until |msg| has been written to the socket or until an
46 // error occurs. 46 // error occurs.
47 // If |address| is specified, then it is used for the destination 47 // If |address| is specified, then it is used for the destination
48 // to send to. Otherwise, will send to the last socket this server 48 // to send to. Otherwise, will send to the last socket this server
49 // received from. 49 // received from.
50 int SendToSocket(UDPServerSocket* socket, std::string msg) { 50 int SendToSocket(UDPServerSocket* socket, std::string msg) {
51 return SendToSocket(socket, msg, recv_from_address_); 51 return SendToSocket(socket, msg, recv_from_address_);
52 } 52 }
53 53
54 int SendToSocket(UDPServerSocket* socket, 54 int SendToSocket(UDPServerSocket* socket,
55 std::string msg, 55 std::string msg,
56 const IPEndPoint& address) { 56 const IPEndPoint& address) {
57 TestCompletionCallback callback; 57 TestCompletionCallback callback;
58 58
59 int length = msg.length(); 59 int length = msg.length();
60 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); 60 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg));
61 scoped_refptr<DrainableIOBuffer> buffer( 61 scoped_refptr<DrainableIOBuffer> buffer(
62 new DrainableIOBuffer(io_buffer, length)); 62 new DrainableIOBuffer(io_buffer.get(), length));
63 63
64 int bytes_sent = 0; 64 int bytes_sent = 0;
65 while (buffer->BytesRemaining()) { 65 while (buffer->BytesRemaining()) {
66 int rv = socket->SendTo(buffer, buffer->BytesRemaining(), 66 int rv = socket->SendTo(
67 address, callback.callback()); 67 buffer.get(), buffer->BytesRemaining(), address, callback.callback());
68 if (rv == ERR_IO_PENDING) 68 if (rv == ERR_IO_PENDING)
69 rv = callback.WaitForResult(); 69 rv = callback.WaitForResult();
70 if (rv <= 0) 70 if (rv <= 0)
71 return bytes_sent > 0 ? bytes_sent : rv; 71 return bytes_sent > 0 ? bytes_sent : rv;
72 bytes_sent += rv; 72 bytes_sent += rv;
73 buffer->DidConsume(rv); 73 buffer->DidConsume(rv);
74 } 74 }
75 return bytes_sent; 75 return bytes_sent;
76 } 76 }
77 77
78 std::string ReadSocket(UDPClientSocket* socket) { 78 std::string ReadSocket(UDPClientSocket* socket) {
79 TestCompletionCallback callback; 79 TestCompletionCallback callback;
80 80
81 int rv = socket->Read(buffer_, kMaxRead, callback.callback()); 81 int rv = socket->Read(buffer_.get(), kMaxRead, callback.callback());
82 if (rv == ERR_IO_PENDING) 82 if (rv == ERR_IO_PENDING)
83 rv = callback.WaitForResult(); 83 rv = callback.WaitForResult();
84 if (rv < 0) 84 if (rv < 0)
85 return std::string(); // error! 85 return std::string(); // error!
86 return std::string(buffer_->data(), rv); 86 return std::string(buffer_->data(), rv);
87 } 87 }
88 88
89 // Loop until |msg| has been written to the socket or until an 89 // Loop until |msg| has been written to the socket or until an
90 // error occurs. 90 // error occurs.
91 int WriteSocket(UDPClientSocket* socket, std::string msg) { 91 int WriteSocket(UDPClientSocket* socket, std::string msg) {
92 TestCompletionCallback callback; 92 TestCompletionCallback callback;
93 93
94 int length = msg.length(); 94 int length = msg.length();
95 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); 95 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg));
96 scoped_refptr<DrainableIOBuffer> buffer( 96 scoped_refptr<DrainableIOBuffer> buffer(
97 new DrainableIOBuffer(io_buffer, length)); 97 new DrainableIOBuffer(io_buffer.get(), length));
98 98
99 int bytes_sent = 0; 99 int bytes_sent = 0;
100 while (buffer->BytesRemaining()) { 100 while (buffer->BytesRemaining()) {
101 int rv = socket->Write(buffer, buffer->BytesRemaining(), 101 int rv = socket->Write(
102 callback.callback()); 102 buffer.get(), buffer->BytesRemaining(), callback.callback());
103 if (rv == ERR_IO_PENDING) 103 if (rv == ERR_IO_PENDING)
104 rv = callback.WaitForResult(); 104 rv = callback.WaitForResult();
105 if (rv <= 0) 105 if (rv <= 0)
106 return bytes_sent > 0 ? bytes_sent : rv; 106 return bytes_sent > 0 ? bytes_sent : rv;
107 bytes_sent += rv; 107 bytes_sent += rv;
108 buffer->DidConsume(rv); 108 buffer->DidConsume(rv);
109 } 109 }
110 return bytes_sent; 110 return bytes_sent;
111 } 111 }
112 112
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after
511 // Close the socket while read is pending. 511 // Close the socket while read is pending.
512 TEST_F(UDPSocketTest, CloseWithPendingRead) { 512 TEST_F(UDPSocketTest, CloseWithPendingRead) {
513 IPEndPoint bind_address; 513 IPEndPoint bind_address;
514 CreateUDPAddress("127.0.0.1", 0, &bind_address); 514 CreateUDPAddress("127.0.0.1", 0, &bind_address);
515 UDPServerSocket server(NULL, NetLog::Source()); 515 UDPServerSocket server(NULL, NetLog::Source());
516 int rv = server.Listen(bind_address); 516 int rv = server.Listen(bind_address);
517 EXPECT_EQ(OK, rv); 517 EXPECT_EQ(OK, rv);
518 518
519 TestCompletionCallback callback; 519 TestCompletionCallback callback;
520 IPEndPoint from; 520 IPEndPoint from;
521 rv = server.RecvFrom(buffer_, kMaxRead, &from, callback.callback()); 521 rv = server.RecvFrom(buffer_.get(), kMaxRead, &from, callback.callback());
522 EXPECT_EQ(rv, ERR_IO_PENDING); 522 EXPECT_EQ(rv, ERR_IO_PENDING);
523 523
524 server.Close(); 524 server.Close();
525 525
526 EXPECT_FALSE(callback.have_result()); 526 EXPECT_FALSE(callback.have_result());
527 } 527 }
528 528
529 #if defined(OS_ANDROID) 529 #if defined(OS_ANDROID)
530 // Some Android devices do not support multicast socket. 530 // Some Android devices do not support multicast socket.
531 // The ones supporting multicast need WifiManager.MulitcastLock to enable it. 531 // The ones supporting multicast need WifiManager.MulitcastLock to enable it.
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 EXPECT_NE(OK, socket.SetMulticastTimeToLive(-1)); 576 EXPECT_NE(OK, socket.SetMulticastTimeToLive(-1));
577 577
578 EXPECT_EQ(OK, socket.Bind(bind_address)); 578 EXPECT_EQ(OK, socket.Bind(bind_address));
579 579
580 socket.Close(); 580 socket.Close();
581 } 581 }
582 582
583 } // namespace 583 } // namespace
584 584
585 } // namespace net 585 } // namespace net
OLDNEW
« no previous file with comments | « net/udp/udp_socket_libevent.cc ('k') | net/url_request/test_url_fetcher_factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698