OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/api/socket/udp_socket.h" | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" | |
9 #include "net/base/completion_callback.h" | |
10 #include "net/base/io_buffer.h" | |
11 #include "net/base/net_errors.h" | |
12 #include "net/base/rand_callback.h" | |
13 #include "net/udp/udp_client_socket.h" | |
14 #include "testing/gmock/include/gmock/gmock.h" | |
15 | |
16 using testing::_; | |
17 using testing::DoAll; | |
18 using testing::Return; | |
19 using testing::SaveArg; | |
20 | |
21 namespace extensions { | |
22 | |
23 class MockUDPSocket : public net::UDPClientSocket { | |
24 public: | |
25 MockUDPSocket() | |
26 : net::UDPClientSocket(net::DatagramSocket::DEFAULT_BIND, | |
27 net::RandIntCallback(), | |
28 NULL, | |
29 net::NetLog::Source()) {} | |
30 | |
31 MOCK_METHOD3(Read, int(net::IOBuffer* buf, int buf_len, | |
32 const net::CompletionCallback& callback)); | |
33 MOCK_METHOD3(Write, int(net::IOBuffer* buf, int buf_len, | |
34 const net::CompletionCallback& callback)); | |
35 private: | |
36 DISALLOW_COPY_AND_ASSIGN(MockUDPSocket); | |
37 }; | |
38 | |
39 class MockAPIResourceEventNotifier : public APIResourceEventNotifier { | |
40 public: | |
41 MockAPIResourceEventNotifier() : APIResourceEventNotifier(NULL, NULL, | |
42 std::string(), | |
43 0, GURL()) {} | |
44 | |
45 MOCK_METHOD2(OnReadComplete, void(int result_code, | |
46 const std::string& message)); | |
47 MOCK_METHOD1(OnWriteComplete, void(int result_code)); | |
48 }; | |
49 | |
50 TEST(SocketTest, TestUDPSocketRead) { | |
51 MockUDPSocket* udp_client_socket = new MockUDPSocket(); | |
52 APIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier(); | |
53 | |
54 scoped_ptr<UDPSocket> socket(UDPSocket::CreateSocketForTesting( | |
55 udp_client_socket, "1.2.3.4", 1, notifier)); | |
56 | |
57 EXPECT_CALL(*udp_client_socket, Read(_, _, _)) | |
58 .Times(1); | |
59 | |
60 scoped_refptr<net::IOBufferWithSize> io_buffer( | |
61 new net::IOBufferWithSize(512)); | |
62 socket->Read(io_buffer.get(), io_buffer->size()); | |
63 } | |
64 | |
65 TEST(SocketTest, TestUDPSocketWrite) { | |
66 MockUDPSocket* udp_client_socket = new MockUDPSocket(); | |
67 APIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier(); | |
68 | |
69 scoped_ptr<UDPSocket> socket(UDPSocket::CreateSocketForTesting( | |
70 udp_client_socket, "1.2.3.4", 1, notifier)); | |
71 | |
72 EXPECT_CALL(*udp_client_socket, Write(_, _, _)) | |
73 .Times(1); | |
74 | |
75 scoped_refptr<net::IOBufferWithSize> io_buffer( | |
76 new net::IOBufferWithSize(512)); | |
77 socket->Write(io_buffer.get(), io_buffer->size()); | |
78 } | |
79 | |
80 TEST(SocketTest, TestUDPSocketBlockedWrite) { | |
81 MockUDPSocket* udp_client_socket = new MockUDPSocket(); | |
82 MockAPIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier(); | |
83 | |
84 scoped_ptr<UDPSocket> socket(UDPSocket::CreateSocketForTesting( | |
85 udp_client_socket, "1.2.3.4", 1, notifier)); | |
86 | |
87 net::CompletionCallback callback; | |
88 EXPECT_CALL(*udp_client_socket, Write(_, _, _)) | |
89 .Times(1) | |
90 .WillOnce(testing::DoAll(SaveArg<2>(&callback), | |
91 Return(net::ERR_IO_PENDING))); | |
92 | |
93 scoped_refptr<net::IOBufferWithSize> io_buffer(new net::IOBufferWithSize(1)); | |
94 ASSERT_EQ(net::ERR_IO_PENDING, socket->Write(io_buffer.get(), | |
95 io_buffer->size())); | |
96 | |
97 // Good. Original call came back unable to complete. Now pretend the socket | |
98 // finished, and confirm that we passed the error back. | |
99 EXPECT_CALL(*notifier, OnWriteComplete(42)) | |
100 .Times(1); | |
101 callback.Run(42); | |
102 } | |
103 | |
104 } // namespace extensions | |
OLD | NEW |