|
| 1 | +defmodule Mint.Core.Transport.TCPTest do |
| 2 | + use ExUnit.Case, async: true |
| 3 | + |
| 4 | + alias Mint.Core.Transport.TCP |
| 5 | + |
| 6 | + describe "connect/3" do |
| 7 | + test "can connect to IPv6 addresses" do |
| 8 | + tcp_opts = [ |
| 9 | + :inet6, |
| 10 | + mode: :binary, |
| 11 | + packet: :raw, |
| 12 | + active: false, |
| 13 | + reuseaddr: true, |
| 14 | + nodelay: true |
| 15 | + ] |
| 16 | + |
| 17 | + {:ok, listen_socket} = :gen_tcp.listen(0, tcp_opts) |
| 18 | + {:ok, {_address, port}} = :inet.sockname(listen_socket) |
| 19 | + |
| 20 | + task = |
| 21 | + Task.async(fn -> |
| 22 | + {:ok, _socket} = :gen_tcp.accept(listen_socket) |
| 23 | + end) |
| 24 | + |
| 25 | + assert {:ok, _socket} = |
| 26 | + TCP.connect({127, 0, 0, 1}, port, |
| 27 | + active: false, |
| 28 | + inet6: true, |
| 29 | + timeout: 1000 |
| 30 | + ) |
| 31 | + |
| 32 | + assert {:ok, _server_socket} = Task.await(task) |
| 33 | + end |
| 34 | + |
| 35 | + test "can fall back to IPv4 if IPv6 fails" do |
| 36 | + tcp_opts = [ |
| 37 | + :inet6, |
| 38 | + mode: :binary, |
| 39 | + packet: :raw, |
| 40 | + active: false, |
| 41 | + reuseaddr: true, |
| 42 | + nodelay: true |
| 43 | + ] |
| 44 | + |
| 45 | + {:ok, listen_socket} = :gen_tcp.listen(0, tcp_opts) |
| 46 | + {:ok, {_address, port}} = :inet.sockname(listen_socket) |
| 47 | + |
| 48 | + task = |
| 49 | + Task.async(fn -> |
| 50 | + {:ok, _socket} = :gen_tcp.accept(listen_socket) |
| 51 | + end) |
| 52 | + |
| 53 | + assert {:ok, _socket} = |
| 54 | + TCP.connect("localhost", port, |
| 55 | + active: false, |
| 56 | + inet6: true, |
| 57 | + timeout: 1000 |
| 58 | + ) |
| 59 | + |
| 60 | + assert {:ok, _server_socket} = Task.await(task) |
| 61 | + end |
| 62 | + |
| 63 | + test "does not fall back to IPv4 if IPv4 is disabled" do |
| 64 | + tcp_opts = [ |
| 65 | + :inet, |
| 66 | + mode: :binary, |
| 67 | + packet: :raw, |
| 68 | + active: false, |
| 69 | + reuseaddr: true, |
| 70 | + nodelay: true |
| 71 | + ] |
| 72 | + |
| 73 | + {:ok, listen_socket} = :gen_tcp.listen(0, tcp_opts) |
| 74 | + {:ok, {_address, port}} = :inet.sockname(listen_socket) |
| 75 | + |
| 76 | + Task.async(fn -> |
| 77 | + {:ok, _socket} = :gen_tcp.accept(listen_socket) |
| 78 | + end) |
| 79 | + |
| 80 | + assert {:error, %Mint.TransportError{reason: :econnrefused}} = |
| 81 | + TCP.connect("localhost", port, |
| 82 | + active: false, |
| 83 | + inet6: true, |
| 84 | + inet4: false, |
| 85 | + timeout: 1000 |
| 86 | + ) |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + describe "controlling_process/2" do |
| 91 | + @describetag :capture_log |
| 92 | + |
| 93 | + setup do |
| 94 | + parent = self() |
| 95 | + ref = make_ref() |
| 96 | + |
| 97 | + ssl_opts = [ |
| 98 | + mode: :binary, |
| 99 | + packet: :raw, |
| 100 | + active: false, |
| 101 | + reuseaddr: true, |
| 102 | + nodelay: true |
| 103 | + ] |
| 104 | + |
| 105 | + spawn_link(fn -> |
| 106 | + {:ok, listen_socket} = :gen_tcp.listen(0, ssl_opts) |
| 107 | + {:ok, {_address, port}} = :inet.sockname(listen_socket) |
| 108 | + send(parent, {ref, port}) |
| 109 | + |
| 110 | + {:ok, socket} = :gen_tcp.accept(listen_socket) |
| 111 | + |
| 112 | + send(parent, {ref, socket}) |
| 113 | + |
| 114 | + # Keep the server alive forever. |
| 115 | + :ok = Process.sleep(:infinity) |
| 116 | + end) |
| 117 | + |
| 118 | + assert_receive {^ref, port} when is_integer(port), 500 |
| 119 | + |
| 120 | + {:ok, socket} = TCP.connect("localhost", port, []) |
| 121 | + assert_receive {^ref, server_socket}, 200 |
| 122 | + |
| 123 | + {:ok, server_port: port, socket: socket, server_socket: server_socket} |
| 124 | + end |
| 125 | + |
| 126 | + test "changing the controlling process of a active: :once socket", |
| 127 | + %{socket: socket, server_socket: server_socket} do |
| 128 | + parent = self() |
| 129 | + ref = make_ref() |
| 130 | + |
| 131 | + # Send two SSL messages (that get translated to Erlang messages right |
| 132 | + # away because of "nodelay: true"), but wait after each one so that |
| 133 | + # it actually arrives and we can set the socket back to active: :once. |
| 134 | + :ok = TCP.setopts(socket, active: :once) |
| 135 | + :ok = :gen_tcp.send(server_socket, "some data 1") |
| 136 | + Process.sleep(100) |
| 137 | + |
| 138 | + :ok = TCP.setopts(socket, active: :once) |
| 139 | + :ok = :gen_tcp.send(server_socket, "some data 2") |
| 140 | + |
| 141 | + wait_until_passes(500, fn -> |
| 142 | + {:messages, messages} = Process.info(self(), :messages) |
| 143 | + assert {:tcp, socket, "some data 1"} in messages |
| 144 | + assert {:tcp, socket, "some data 2"} in messages |
| 145 | + end) |
| 146 | + |
| 147 | + other_process = spawn_link(fn -> process_mirror(parent, ref) end) |
| 148 | + |
| 149 | + assert :ok = TCP.controlling_process(socket, other_process) |
| 150 | + |
| 151 | + assert_receive {^ref, {:tcp, ^socket, "some data 1"}} |
| 152 | + assert_receive {^ref, {:tcp, ^socket, "some data 2"}} |
| 153 | + |
| 154 | + refute_received _message |
| 155 | + end |
| 156 | + |
| 157 | + test "changing the controlling process of a passive socket", |
| 158 | + %{socket: socket, server_socket: server_socket} do |
| 159 | + parent = self() |
| 160 | + ref = make_ref() |
| 161 | + |
| 162 | + :ok = :gen_tcp.send(server_socket, "some data") |
| 163 | + |
| 164 | + other_process = |
| 165 | + spawn_link(fn -> |
| 166 | + assert_receive message, 500 |
| 167 | + send(parent, {ref, message}) |
| 168 | + end) |
| 169 | + |
| 170 | + assert :ok = TCP.controlling_process(socket, other_process) |
| 171 | + assert {:ok, [active: false]} = TCP.getopts(socket, [:active]) |
| 172 | + :ok = TCP.setopts(socket, active: :once) |
| 173 | + |
| 174 | + assert_receive {^ref, {:tcp, ^socket, "some data"}}, 500 |
| 175 | + |
| 176 | + refute_received _message |
| 177 | + end |
| 178 | + |
| 179 | + test "changing the controlling process of a closed socket", |
| 180 | + %{socket: socket} do |
| 181 | + other_process = spawn_link(fn -> :ok = Process.sleep(:infinity) end) |
| 182 | + |
| 183 | + :ok = TCP.close(socket) |
| 184 | + |
| 185 | + assert {:error, _error} = TCP.controlling_process(socket, other_process) |
| 186 | + end |
| 187 | + end |
| 188 | + |
| 189 | + defp process_mirror(parent, ref) do |
| 190 | + receive do |
| 191 | + message -> |
| 192 | + send(parent, {ref, message}) |
| 193 | + process_mirror(parent, ref) |
| 194 | + end |
| 195 | + end |
| 196 | + |
| 197 | + defp wait_until_passes(time_left, fun) when time_left <= 0 do |
| 198 | + fun.() |
| 199 | + end |
| 200 | + |
| 201 | + defp wait_until_passes(time_left, fun) do |
| 202 | + fun.() |
| 203 | + rescue |
| 204 | + _exception -> |
| 205 | + Process.sleep(10) |
| 206 | + wait_until_passes(time_left - 10, fun) |
| 207 | + end |
| 208 | +end |
0 commit comments