-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add :inet4 transport option to disable IPv4 fallback #425
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
defmodule Mint.Core.Transport.TCPTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias Mint.Core.Transport.TCP | ||
|
||
describe "connect/3" do | ||
test "can connect to IPv6 addresses" do | ||
tcp_opts = [ | ||
:inet6, | ||
mode: :binary, | ||
packet: :raw, | ||
active: false, | ||
reuseaddr: true, | ||
nodelay: true | ||
] | ||
|
||
{:ok, listen_socket} = :gen_tcp.listen(0, tcp_opts) | ||
{:ok, {_address, port}} = :inet.sockname(listen_socket) | ||
|
||
task = | ||
Task.async(fn -> | ||
{:ok, _socket} = :gen_tcp.accept(listen_socket) | ||
end) | ||
|
||
assert {:ok, _socket} = | ||
TCP.connect({127, 0, 0, 1}, port, | ||
active: false, | ||
inet6: true, | ||
timeout: 1000 | ||
) | ||
|
||
assert {:ok, _server_socket} = Task.await(task) | ||
end | ||
|
||
test "can fall back to IPv4 if IPv6 fails" do | ||
tcp_opts = [ | ||
:inet6, | ||
mode: :binary, | ||
packet: :raw, | ||
active: false, | ||
reuseaddr: true, | ||
nodelay: true | ||
] | ||
|
||
{:ok, listen_socket} = :gen_tcp.listen(0, tcp_opts) | ||
{:ok, {_address, port}} = :inet.sockname(listen_socket) | ||
|
||
task = | ||
Task.async(fn -> | ||
{:ok, _socket} = :gen_tcp.accept(listen_socket) | ||
end) | ||
|
||
assert {:ok, _socket} = | ||
TCP.connect("localhost", port, | ||
active: false, | ||
inet6: true, | ||
timeout: 1000 | ||
) | ||
|
||
assert {:ok, _server_socket} = Task.await(task) | ||
end | ||
|
||
test "does not fall back to IPv4 if IPv4 is disabled" do | ||
tcp_opts = [ | ||
:inet, | ||
mode: :binary, | ||
packet: :raw, | ||
active: false, | ||
reuseaddr: true, | ||
nodelay: true | ||
] | ||
|
||
{:ok, listen_socket} = :gen_tcp.listen(0, tcp_opts) | ||
{:ok, {_address, port}} = :inet.sockname(listen_socket) | ||
|
||
Task.async(fn -> | ||
{:ok, _socket} = :gen_tcp.accept(listen_socket) | ||
end) | ||
|
||
assert {:error, %Mint.TransportError{reason: :econnrefused}} = | ||
TCP.connect("localhost", port, | ||
active: false, | ||
inet6: true, | ||
inet4: false, | ||
timeout: 1000 | ||
) | ||
end | ||
end | ||
|
||
describe "controlling_process/2" do | ||
@describetag :capture_log | ||
|
||
setup do | ||
parent = self() | ||
ref = make_ref() | ||
|
||
ssl_opts = [ | ||
mode: :binary, | ||
packet: :raw, | ||
active: false, | ||
reuseaddr: true, | ||
nodelay: true | ||
] | ||
|
||
spawn_link(fn -> | ||
{:ok, listen_socket} = :gen_tcp.listen(0, ssl_opts) | ||
{:ok, {_address, port}} = :inet.sockname(listen_socket) | ||
send(parent, {ref, port}) | ||
|
||
{:ok, socket} = :gen_tcp.accept(listen_socket) | ||
|
||
send(parent, {ref, socket}) | ||
|
||
# Keep the server alive forever. | ||
:ok = Process.sleep(:infinity) | ||
end) | ||
|
||
assert_receive {^ref, port} when is_integer(port), 500 | ||
|
||
{:ok, socket} = TCP.connect("localhost", port, []) | ||
assert_receive {^ref, server_socket}, 200 | ||
|
||
{:ok, server_port: port, socket: socket, server_socket: server_socket} | ||
end | ||
|
||
test "changing the controlling process of a active: :once socket", | ||
%{socket: socket, server_socket: server_socket} do | ||
parent = self() | ||
ref = make_ref() | ||
|
||
# Send two SSL messages (that get translated to Erlang messages right | ||
# away because of "nodelay: true"), but wait after each one so that | ||
# it actually arrives and we can set the socket back to active: :once. | ||
:ok = TCP.setopts(socket, active: :once) | ||
:ok = :gen_tcp.send(server_socket, "some data 1") | ||
Process.sleep(100) | ||
|
||
:ok = TCP.setopts(socket, active: :once) | ||
:ok = :gen_tcp.send(server_socket, "some data 2") | ||
|
||
wait_until_passes(500, fn -> | ||
{:messages, messages} = Process.info(self(), :messages) | ||
assert {:tcp, socket, "some data 1"} in messages | ||
assert {:tcp, socket, "some data 2"} in messages | ||
end) | ||
|
||
other_process = spawn_link(fn -> process_mirror(parent, ref) end) | ||
|
||
assert :ok = TCP.controlling_process(socket, other_process) | ||
|
||
assert_receive {^ref, {:tcp, ^socket, "some data 1"}} | ||
assert_receive {^ref, {:tcp, ^socket, "some data 2"}} | ||
|
||
refute_received _message | ||
end | ||
|
||
test "changing the controlling process of a passive socket", | ||
%{socket: socket, server_socket: server_socket} do | ||
parent = self() | ||
ref = make_ref() | ||
|
||
:ok = :gen_tcp.send(server_socket, "some data") | ||
|
||
other_process = | ||
spawn_link(fn -> | ||
assert_receive message, 500 | ||
send(parent, {ref, message}) | ||
end) | ||
|
||
assert :ok = TCP.controlling_process(socket, other_process) | ||
assert {:ok, [active: false]} = TCP.getopts(socket, [:active]) | ||
:ok = TCP.setopts(socket, active: :once) | ||
|
||
assert_receive {^ref, {:tcp, ^socket, "some data"}}, 500 | ||
|
||
refute_received _message | ||
end | ||
|
||
test "changing the controlling process of a closed socket", | ||
%{socket: socket} do | ||
other_process = spawn_link(fn -> :ok = Process.sleep(:infinity) end) | ||
|
||
:ok = TCP.close(socket) | ||
|
||
assert {:error, _error} = TCP.controlling_process(socket, other_process) | ||
end | ||
end | ||
|
||
defp process_mirror(parent, ref) do | ||
receive do | ||
message -> | ||
send(parent, {ref, message}) | ||
process_mirror(parent, ref) | ||
end | ||
end | ||
|
||
defp wait_until_passes(time_left, fun) when time_left <= 0 do | ||
fun.() | ||
end | ||
|
||
defp wait_until_passes(time_left, fun) do | ||
fun.() | ||
rescue | ||
_exception -> | ||
Process.sleep(10) | ||
wait_until_passes(time_left - 10, fun) | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe stack mismatch shows up as :nxdomain so maybe there's no reason to try again on other error reasons? Or the idea is we get say :closed or :timeout and we might as well try on ipv4?
@dch you know more about this stuff, perhaps you have anything to add?