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

Side by Side Diff: chrome/browser/extensions/api/socket/tcp_socket_unittest.cc

Issue 10095020: Allow socket API to send binary data. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Patch Set 1 Created 8 years, 8 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
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 "chrome/browser/extensions/api/socket/tcp_socket.h" 5 #include "chrome/browser/extensions/api/socket/tcp_socket.h"
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" 8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h"
9 #include "net/base/address_list.h" 9 #include "net/base/address_list.h"
10 #include "net/base/completion_callback.h" 10 #include "net/base/completion_callback.h"
11 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h" 12 #include "net/base/net_errors.h"
12 #include "net/base/rand_callback.h" 13 #include "net/base/rand_callback.h"
13 #include "net/socket/tcp_client_socket.h" 14 #include "net/socket/tcp_client_socket.h"
14 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
15 16
16 using testing::_; 17 using testing::_;
17 using testing::DoAll; 18 using testing::DoAll;
18 using testing::Return; 19 using testing::Return;
19 using testing::SaveArg; 20 using testing::SaveArg;
20 21
(...skipping 28 matching lines...) Expand all
49 net::AddressList address_list; 50 net::AddressList address_list;
50 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list); 51 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list);
51 APIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier(); 52 APIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier();
52 53
53 scoped_ptr<TCPSocket> socket(TCPSocket::CreateSocketForTesting( 54 scoped_ptr<TCPSocket> socket(TCPSocket::CreateSocketForTesting(
54 tcp_client_socket, "1.2.3.4", 1, notifier)); 55 tcp_client_socket, "1.2.3.4", 1, notifier));
55 56
56 EXPECT_CALL(*tcp_client_socket, Read(_, _, _)) 57 EXPECT_CALL(*tcp_client_socket, Read(_, _, _))
57 .Times(1); 58 .Times(1);
58 59
59 std::string message = socket->Read(); 60 scoped_refptr<net::IOBufferWithSize> io_buffer(
61 new net::IOBufferWithSize(512));
62 socket->Read(io_buffer.get(), io_buffer->size());
60 } 63 }
61 64
62 TEST(SocketTest, TestTCPSocketWrite) { 65 TEST(SocketTest, TestTCPSocketWrite) {
63 net::AddressList address_list; 66 net::AddressList address_list;
64 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list); 67 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list);
65 APIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier(); 68 APIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier();
66 69
67 scoped_ptr<TCPSocket> socket(TCPSocket::CreateSocketForTesting( 70 scoped_ptr<TCPSocket> socket(TCPSocket::CreateSocketForTesting(
68 tcp_client_socket, "1.2.3.4", 1, notifier)); 71 tcp_client_socket, "1.2.3.4", 1, notifier));
69 72
70 EXPECT_CALL(*tcp_client_socket, Write(_, _, _)) 73 EXPECT_CALL(*tcp_client_socket, Write(_, _, _))
71 .Times(1); 74 .Times(1);
72 75
73 socket->Write("foo"); 76 scoped_refptr<net::IOBufferWithSize> io_buffer(
77 new net::IOBufferWithSize(256));
78 socket->Write(io_buffer.get(), io_buffer->size());
74 } 79 }
75 80
76 TEST(SocketTest, TestTCPSocketBlockedWrite) { 81 TEST(SocketTest, TestTCPSocketBlockedWrite) {
77 net::AddressList address_list; 82 net::AddressList address_list;
78 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list); 83 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list);
79 MockAPIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier(); 84 MockAPIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier();
80 85
81 scoped_ptr<TCPSocket> socket(TCPSocket::CreateSocketForTesting( 86 scoped_ptr<TCPSocket> socket(TCPSocket::CreateSocketForTesting(
82 tcp_client_socket, "1.2.3.4", 1, notifier)); 87 tcp_client_socket, "1.2.3.4", 1, notifier));
83 88
84 net::CompletionCallback callback; 89 net::CompletionCallback callback;
85 EXPECT_CALL(*tcp_client_socket, Write(_, _, _)) 90 EXPECT_CALL(*tcp_client_socket, Write(_, _, _))
86 .Times(1) 91 .Times(1)
87 .WillOnce(testing::DoAll(SaveArg<2>(&callback), 92 .WillOnce(testing::DoAll(SaveArg<2>(&callback),
88 Return(net::ERR_IO_PENDING))); 93 Return(net::ERR_IO_PENDING)));
89 94
90 ASSERT_EQ(net::ERR_IO_PENDING, socket->Write("foo")); 95 scoped_refptr<net::IOBufferWithSize> io_buffer(new net::IOBufferWithSize(
96 1));
97 ASSERT_EQ(net::ERR_IO_PENDING, socket->Write(io_buffer.get(),
98 io_buffer->size()));
91 99
92 // Good. Original call came back unable to complete. Now pretend the socket 100 // Good. Original call came back unable to complete. Now pretend the socket
93 // finished, and confirm that we passed the error back. 101 // finished, and confirm that we passed the error back.
94 EXPECT_CALL(*notifier, OnWriteComplete(42)) 102 EXPECT_CALL(*notifier, OnWriteComplete(42))
95 .Times(1); 103 .Times(1);
96 callback.Run(42); 104 callback.Run(42);
97 } 105 }
98 106
99 } // namespace extensions 107 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698