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