| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/run_loop.h" |
| 13 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 14 #include "base/test/test_timeouts.h" | 15 #include "base/test/test_timeouts.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "chrome/test/base/browser_with_test_window_test.h" | 17 #include "chrome/test/base/browser_with_test_window_test.h" |
| 17 #include "extensions/browser/api/socket/udp_socket.h" | 18 #include "extensions/browser/api/socket/udp_socket.h" |
| 18 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
| 19 #include "net/base/ip_address.h" | 20 #include "net/base/ip_address.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 22 |
| 22 namespace extensions { | 23 namespace extensions { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 socket.Connect(CreateAddressList(kGroup, 13333), base::Bind(&OnConnected)); | 87 socket.Connect(CreateAddressList(kGroup, 13333), base::Bind(&OnConnected)); |
| 87 } | 88 } |
| 88 | 89 |
| 89 TEST(UDPSocketUnitTest, TestUDPMulticastLoopbackMode) { | 90 TEST(UDPSocketUnitTest, TestUDPMulticastLoopbackMode) { |
| 90 const char kGroup[] = "237.132.100.17"; | 91 const char kGroup[] = "237.132.100.17"; |
| 91 UDPSocket socket("abcdefghijklmnopqrst"); | 92 UDPSocket socket("abcdefghijklmnopqrst"); |
| 92 EXPECT_EQ(0, socket.SetMulticastLoopbackMode(false)); | 93 EXPECT_EQ(0, socket.SetMulticastLoopbackMode(false)); |
| 93 socket.Connect(CreateAddressList(kGroup, 13333), base::Bind(&OnConnected)); | 94 socket.Connect(CreateAddressList(kGroup, 13333), base::Bind(&OnConnected)); |
| 94 } | 95 } |
| 95 | 96 |
| 96 static void QuitMessageLoop() { | |
| 97 base::MessageLoopForIO::current()->QuitNow(); | |
| 98 } | |
| 99 | |
| 100 // Send a test multicast packet every second. | 97 // Send a test multicast packet every second. |
| 101 // Once the target socket received the packet, the message loop will exit. | 98 // Once the target socket received the packet, the message loop will exit. |
| 102 static void SendMulticastPacket(UDPSocket* src, int result) { | 99 static void SendMulticastPacket(const base::Closure& quit_run_loop, |
| 100 UDPSocket* src, |
| 101 int result) { |
| 103 if (result == 0) { | 102 if (result == 0) { |
| 104 scoped_refptr<net::IOBuffer> data = new net::WrappedIOBuffer(test_message); | 103 scoped_refptr<net::IOBuffer> data = new net::WrappedIOBuffer(test_message); |
| 105 src->Write(data, test_message_length, base::Bind(&OnSendCompleted)); | 104 src->Write(data, test_message_length, base::Bind(&OnSendCompleted)); |
| 106 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 105 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 107 FROM_HERE, base::Bind(&SendMulticastPacket, src, result), | 106 FROM_HERE, base::Bind(&SendMulticastPacket, quit_run_loop, src, result), |
| 108 base::TimeDelta::FromSeconds(1)); | 107 base::TimeDelta::FromSeconds(1)); |
| 109 } else { | 108 } else { |
| 110 QuitMessageLoop(); | 109 quit_run_loop.Run(); |
| 111 FAIL() << "Failed to connect to multicast address. Error code: " << result; | 110 FAIL() << "Failed to connect to multicast address. Error code: " << result; |
| 112 } | 111 } |
| 113 } | 112 } |
| 114 | 113 |
| 115 static void OnMulticastReadCompleted(bool *packet_received, | 114 static void OnMulticastReadCompleted(const base::Closure& quit_run_loop, |
| 115 bool* packet_received, |
| 116 int count, | 116 int count, |
| 117 scoped_refptr<net::IOBuffer> io_buffer) { | 117 scoped_refptr<net::IOBuffer> io_buffer) { |
| 118 EXPECT_EQ(test_message_length, count); | 118 EXPECT_EQ(test_message_length, count); |
| 119 EXPECT_EQ(0, strncmp(io_buffer->data(), test_message, test_message_length)); | 119 EXPECT_EQ(0, strncmp(io_buffer->data(), test_message, test_message_length)); |
| 120 *packet_received = true; | 120 *packet_received = true; |
| 121 QuitMessageLoop(); | 121 quit_run_loop.Run(); |
| 122 } | 122 } |
| 123 | 123 |
| 124 TEST(UDPSocketUnitTest, TestUDPMulticastRecv) { | 124 TEST(UDPSocketUnitTest, TestUDPMulticastRecv) { |
| 125 const int kPort = 9999; | 125 const int kPort = 9999; |
| 126 const char kGroup[] = "237.132.100.17"; | 126 const char kGroup[] = "237.132.100.17"; |
| 127 bool packet_received = false; | 127 bool packet_received = false; |
| 128 base::MessageLoopForIO io_loop; // For Read to do its threaded work. | 128 base::MessageLoopForIO io_loop; // For Read to do its threaded work. |
| 129 UDPSocket dest("abcdefghijklmnopqrst"); | 129 UDPSocket dest("abcdefghijklmnopqrst"); |
| 130 UDPSocket src("abcdefghijklmnopqrst"); | 130 UDPSocket src("abcdefghijklmnopqrst"); |
| 131 | 131 |
| 132 base::RunLoop run_loop; |
| 133 |
| 132 // Receiver | 134 // Receiver |
| 133 EXPECT_EQ(0, dest.Bind("0.0.0.0", kPort)); | 135 EXPECT_EQ(0, dest.Bind("0.0.0.0", kPort)); |
| 134 EXPECT_EQ(0, dest.JoinGroup(kGroup)); | 136 EXPECT_EQ(0, dest.JoinGroup(kGroup)); |
| 135 dest.Read(1024, base::Bind(&OnMulticastReadCompleted, &packet_received)); | 137 dest.Read(1024, base::Bind(&OnMulticastReadCompleted, run_loop.QuitClosure(), |
| 138 &packet_received)); |
| 136 | 139 |
| 137 // Sender | 140 // Sender |
| 138 EXPECT_EQ(0, src.SetMulticastTimeToLive(0)); | 141 EXPECT_EQ(0, src.SetMulticastTimeToLive(0)); |
| 139 src.Connect(CreateAddressList(kGroup, kPort), | 142 src.Connect(CreateAddressList(kGroup, kPort), |
| 140 base::Bind(&SendMulticastPacket, &src)); | 143 base::Bind(&SendMulticastPacket, run_loop.QuitClosure(), &src)); |
| 141 | 144 |
| 142 // If not received within the test action timeout, quit the message loop. | 145 // If not received within the test action timeout, quit the message loop. |
| 143 io_loop.task_runner()->PostDelayedTask( | 146 io_loop.task_runner()->PostDelayedTask(FROM_HERE, run_loop.QuitClosure(), |
| 144 FROM_HERE, base::Bind(&QuitMessageLoop), TestTimeouts::action_timeout()); | 147 TestTimeouts::action_timeout()); |
| 145 | 148 |
| 146 io_loop.Run(); | 149 run_loop.Run(); |
| 147 | 150 |
| 148 EXPECT_TRUE(packet_received) << "Failed to receive from multicast address"; | 151 EXPECT_TRUE(packet_received) << "Failed to receive from multicast address"; |
| 149 } | 152 } |
| 150 | 153 |
| 151 } // namespace extensions | 154 } // namespace extensions |
| OLD | NEW |