File: udp_socket_unittest.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (156 lines) | stat: -rw-r--r-- 5,744 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>
#include <stdint.h>

#include <memory>
#include <string>

#include "base/location.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "extensions/browser/api/socket/udp_socket.h"
#include "net/base/io_buffer.h"
#include "net/base/ip_address.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace extensions {

// UDPSocketUnitTest exists solely to make it easier to pass a specific
// gtest_filter argument during development.
class UDPSocketUnitTest : public BrowserWithTestWindowTest {
};

static void OnConnected(int result) {
  EXPECT_EQ(0, result);
}

static void OnCompleted(int bytes_read,
                        scoped_refptr<net::IOBuffer> io_buffer,
                        bool socket_destroying,
                        const std::string& address,
                        uint16_t port) {
  // Do nothing; don't care.
}

static const char test_message[] = "$$TESTMESSAGETESTMESSAGETESTMESSAGETEST$$";
static const int test_message_length = arraysize(test_message);

net::AddressList CreateAddressList(const char* address_string, int port) {
  net::IPAddress ip;
  EXPECT_TRUE(ip.AssignFromIPLiteral(address_string));
  return net::AddressList::CreateFromIPAddress(ip, port);
}

static void OnSendCompleted(int result) {
  EXPECT_EQ(test_message_length, result);
}

TEST(UDPSocketUnitTest, TestUDPSocketRecvFrom) {
  base::MessageLoopForIO io_loop;  // For RecvFrom to do its threaded work.
  UDPSocket socket("abcdefghijklmnopqrst");

  // Confirm that we can call two RecvFroms in quick succession without
  // triggering crbug.com/146606.
  socket.Connect(CreateAddressList("127.0.0.1", 40000),
                 base::Bind(&OnConnected));
  socket.RecvFrom(4096, base::Bind(&OnCompleted));
  socket.RecvFrom(4096, base::Bind(&OnCompleted));
}

TEST(UDPSocketUnitTest, TestUDPMulticastJoinGroup) {
  const char kGroup[] = "237.132.100.17";
  UDPSocket src("abcdefghijklmnopqrst");
  UDPSocket dest("abcdefghijklmnopqrst");

  EXPECT_EQ(0, dest.Bind("0.0.0.0", 13333));
  EXPECT_EQ(0, dest.JoinGroup(kGroup));
  std::vector<std::string> groups = dest.GetJoinedGroups();
  EXPECT_EQ(static_cast<size_t>(1), groups.size());
  EXPECT_EQ(kGroup, *groups.begin());
  EXPECT_NE(0, dest.LeaveGroup("237.132.100.13"));
  EXPECT_EQ(0, dest.LeaveGroup(kGroup));
  groups = dest.GetJoinedGroups();
  EXPECT_EQ(static_cast<size_t>(0), groups.size());
}

TEST(UDPSocketUnitTest, TestUDPMulticastTimeToLive) {
  const char kGroup[] = "237.132.100.17";
  UDPSocket socket("abcdefghijklmnopqrst");
  EXPECT_NE(0, socket.SetMulticastTimeToLive(-1));  // Negative TTL shall fail.
  EXPECT_EQ(0, socket.SetMulticastTimeToLive(3));
  socket.Connect(CreateAddressList(kGroup, 13333), base::Bind(&OnConnected));
}

TEST(UDPSocketUnitTest, TestUDPMulticastLoopbackMode) {
  const char kGroup[] = "237.132.100.17";
  UDPSocket socket("abcdefghijklmnopqrst");
  EXPECT_EQ(0, socket.SetMulticastLoopbackMode(false));
  socket.Connect(CreateAddressList(kGroup, 13333), base::Bind(&OnConnected));
}

// Send a test multicast packet every second.
// Once the target socket received the packet, the message loop will exit.
static void SendMulticastPacket(const base::Closure& quit_run_loop,
                                UDPSocket* src,
                                int result) {
  if (result == 0) {
    scoped_refptr<net::IOBuffer> data = new net::WrappedIOBuffer(test_message);
    src->Write(data, test_message_length, base::Bind(&OnSendCompleted));
    base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
        FROM_HERE, base::Bind(&SendMulticastPacket, quit_run_loop, src, result),
        base::TimeDelta::FromSeconds(1));
  } else {
    quit_run_loop.Run();
    FAIL() << "Failed to connect to multicast address. Error code: " << result;
  }
}

static void OnMulticastReadCompleted(const base::Closure& quit_run_loop,
                                     bool* packet_received,
                                     int count,
                                     scoped_refptr<net::IOBuffer> io_buffer,
                                     bool socket_destroying) {
  EXPECT_EQ(test_message_length, count);
  EXPECT_EQ(0, strncmp(io_buffer->data(), test_message, test_message_length));
  *packet_received = true;
  quit_run_loop.Run();
}

TEST(UDPSocketUnitTest, TestUDPMulticastRecv) {
  const int kPort = 9999;
  const char kGroup[] = "237.132.100.17";
  bool packet_received = false;
  base::MessageLoopForIO io_loop;  // For Read to do its threaded work.
  UDPSocket dest("abcdefghijklmnopqrst");
  UDPSocket src("abcdefghijklmnopqrst");

  base::RunLoop run_loop;

  // Receiver
  EXPECT_EQ(0, dest.Bind("0.0.0.0", kPort));
  EXPECT_EQ(0, dest.JoinGroup(kGroup));
  dest.Read(1024, base::Bind(&OnMulticastReadCompleted, run_loop.QuitClosure(),
                             &packet_received));

  // Sender
  EXPECT_EQ(0, src.SetMulticastTimeToLive(0));
  src.Connect(CreateAddressList(kGroup, kPort),
              base::Bind(&SendMulticastPacket, run_loop.QuitClosure(), &src));

  // If not received within the test action timeout, quit the message loop.
  io_loop.task_runner()->PostDelayedTask(FROM_HERE, run_loop.QuitClosure(),
                                         TestTimeouts::action_timeout());

  run_loop.Run();

  EXPECT_TRUE(packet_received) << "Failed to receive from multicast address";
}

}  // namespace extensions