-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[cancelled, not using Crystal anymore] Add abstract unix socket handling #4056
Closed
Closed
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2fc5863
Add abstract UNIX socket handling
bew 66bd0ef
Allow abstract UNIX socket with '\0` path prefix'
bew a02c69e
Allow UNIX address on Linux only, raise on other plateform
bew 54d6dda
Run abstract UNIX specs on Linux only
bew a802ee9
Use `@` as address prefix for abstract sockets
bew e93d6d7
Various fixes, based on @straight-shoota comments
bew 277770b
fixup!
bew 5f2f223
Safer low-level manipulations
bew 7cddb29
Note -> NOTE
bew 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,15 +169,67 @@ describe Socket::IPAddress do | |
end | ||
|
||
describe Socket::UNIXAddress do | ||
{% if flag?(:linux) %} | ||
it "can be abstract on Linux" do | ||
path = "/abstract.sock" | ||
addr = Socket::UNIXAddress.new('@' + path) | ||
addr.abstract?.should be_true | ||
addr.path.should eq(path) | ||
end | ||
{% else %} | ||
it "raises for abstract UNIX address on non-Linux" do | ||
expect_raises(ArgumentError, "Unsupported") do | ||
Socket::UNIXAddress.new("@/abstract.sock") | ||
end | ||
end | ||
{% end %} | ||
|
||
it "is not abstract for a non-abstract address" do | ||
path = "/tmp/unix.sock" | ||
addr = Socket::UNIXAddress.new(path) | ||
addr.abstract?.should be_false | ||
addr.path.should eq(path) | ||
end | ||
|
||
it "creates a non-abstract UNIX address starting with @" do | ||
path = "./@non-abstract.sock" | ||
addr = Socket::UNIXAddress.new(path) | ||
addr.abstract?.should be_false | ||
addr.path.should eq(path) | ||
end | ||
|
||
it "transforms into a C struct and back" do | ||
addr1 = Socket::UNIXAddress.new("/tmp/service.sock") | ||
addr2 = Socket::UNIXAddress.from(addr1.to_unsafe, addr1.size) | ||
path = "/tmp/service.sock" | ||
|
||
addr1 = Socket::UNIXAddress.new(path) | ||
addr1.abstract?.should be_false | ||
addr1.path.should eq(path) | ||
|
||
addr2 = Socket::UNIXAddress.from(addr1.to_unsafe, addr1.size) | ||
addr2.family.should eq(addr1.family) | ||
addr2.path.should eq(addr1.path) | ||
addr2.to_s.should eq("/tmp/service.sock") | ||
addr2.abstract?.should eq(addr1.abstract?) | ||
addr2.to_s.should eq(path) | ||
end | ||
|
||
{% if flag?(:linux) %} | ||
it "transforms an abstract address into a C struct and back" do | ||
path = "/abstract-service.sock" | ||
|
||
addr1 = Socket::UNIXAddress.new('@' + path) | ||
addr1.path.should eq(path) | ||
addr1.abstract?.should be_true | ||
|
||
sockaddr_un = addr1.to_unsafe.as(LibC::SockaddrUn*).value | ||
sockaddr_un.sun_path[0].should eq(0_u8) | ||
String.new(sockaddr_un.sun_path.to_unsafe + 1).should eq(path) | ||
|
||
addr2 = Socket::UNIXAddress.new(pointerof(sockaddr_un), nil) | ||
addr2.path.should eq(addr1.path) | ||
addr2.abstract?.should eq(addr1.abstract?) | ||
end | ||
{% end %} | ||
|
||
it "raises when path is too long" do | ||
path = "/tmp/crystal-test-too-long-unix-socket-#{("a" * 2048)}.sock" | ||
expect_raises(ArgumentError, "Path size exceeds the maximum size") { Socket::UNIXAddress.new(path) } | ||
|
@@ -186,6 +238,12 @@ describe Socket::UNIXAddress do | |
it "to_s" do | ||
Socket::UNIXAddress.new("some_path").to_s.should eq("some_path") | ||
end | ||
|
||
{% if flag?(:linux) %} | ||
it "to_s for abstract UNIX address" do | ||
Socket::UNIXAddress.new("@some_path").to_s.should eq("@some_path") | ||
end | ||
{% end %} | ||
end | ||
|
||
describe UNIXServer do | ||
|
@@ -195,6 +253,26 @@ describe UNIXServer do | |
File.exists?(path).should be_false | ||
end | ||
|
||
{% if flag?(:linux) %} | ||
it "is not abstract when path is not an abstract address" do | ||
path = "/tmp/crystal-test-unix-sock" | ||
|
||
UNIXServer.open(path) do |server| | ||
server.abstract?.should be_false | ||
end | ||
end | ||
{% end %} | ||
|
||
{% if flag?(:linux) %} | ||
it "is abstract when path is an abstract address" do | ||
path = "/tmp/crystal-test-unix-abstract-sock" | ||
|
||
UNIXServer.open('@' + path) do |server| | ||
server.abstract?.should be_true | ||
end | ||
end | ||
{% end %} | ||
|
||
it "creates the socket file" do | ||
path = "/tmp/crystal-test-unix-sock" | ||
|
||
|
@@ -205,6 +283,34 @@ describe UNIXServer do | |
File.exists?(path).should be_false | ||
end | ||
|
||
it "creates the socket file with path starting with @" do | ||
path = "@crystal-test-unix-sock-starting-with-at-symbol" | ||
forced_non_abstract_path = "./#{path}" | ||
|
||
File.exists?(path).should be_false | ||
|
||
UNIXServer.open(forced_non_abstract_path) do | ||
File.exists?(path).should be_true | ||
end | ||
|
||
File.exists?(path).should be_false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may get rid of the outer |
||
end | ||
|
||
{% if flag?(:linux) %} | ||
it "does not create any file for abstract server" do | ||
path = "/tmp/crystal-test-unix-abstract-sock" | ||
abstract_path = '@' + path | ||
|
||
File.exists?(path).should be_false | ||
File.exists?(abstract_path).should be_false | ||
|
||
UNIXServer.open(abstract_path) do | ||
File.exists?(path).should be_false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe also doublecheck that |
||
File.exists?(abstract_path).should be_false | ||
end | ||
end | ||
{% end %} | ||
|
||
it "deletes socket file on close" do | ||
path = "/tmp/crystal-test-unix-sock" | ||
|
||
|
@@ -217,6 +323,23 @@ describe UNIXServer do | |
end | ||
end | ||
|
||
{% if flag?(:linux) %} | ||
it "does not delete any file on close for abstract server" do | ||
path = "/tmp/crystal-test-close-unix-abstract-sock" | ||
|
||
File.touch(path) | ||
File.exists?(path).should be_true | ||
|
||
begin | ||
server = UNIXServer.new('@' + path) | ||
server.close | ||
File.exists?(path).should be_true | ||
ensure | ||
File.delete(path) if File.exists?(path) | ||
end | ||
end | ||
{% end %} | ||
|
||
it "raises when socket file already exists" do | ||
path = "/tmp/crystal-test-unix-sock" | ||
server = UNIXServer.new(path) | ||
|
@@ -231,7 +354,7 @@ describe UNIXServer do | |
it "won't delete existing file on bind failure" do | ||
path = "/tmp/crystal-test-unix.sock" | ||
|
||
File.write(path, "") | ||
File.touch(path) | ||
File.exists?(path).should be_true | ||
|
||
begin | ||
|
@@ -247,15 +370,34 @@ describe UNIXServer do | |
|
||
describe "accept" do | ||
it "returns the client UNIXSocket" do | ||
UNIXServer.open("/tmp/crystal-test-unix-sock") do |server| | ||
UNIXSocket.open("/tmp/crystal-test-unix-sock") do |_| | ||
path = "/tmp/crystal-test-unix-sock" | ||
|
||
UNIXServer.open(path) do |server| | ||
UNIXSocket.open(path) do |_| | ||
client = server.accept | ||
client.should be_a(UNIXSocket) | ||
client.abstract?.should be_false | ||
client.close | ||
end | ||
end | ||
end | ||
|
||
{% if flag?(:linux) %} | ||
it "returns an abstract client UNIXSocket for abstract server" do | ||
path = "/tmp/crystal-test-abstract-unix-sock" | ||
abstract_path = '@' + path | ||
|
||
UNIXServer.open(abstract_path) do |server| | ||
UNIXSocket.open(abstract_path) do |_| | ||
client = server.accept | ||
client.should be_a(UNIXSocket) | ||
client.abstract?.should be_true | ||
client.close | ||
end | ||
end | ||
end | ||
{% end %} | ||
|
||
it "raises when server is closed" do | ||
server = UNIXServer.new("/tmp/crystal-test-unix-sock") | ||
exception = nil | ||
|
@@ -280,8 +422,10 @@ describe UNIXServer do | |
|
||
describe "accept?" do | ||
it "returns the client UNIXSocket" do | ||
UNIXServer.open("/tmp/crystal-test-unix-sock") do |server| | ||
UNIXSocket.open("/tmp/crystal-test-unix-sock") do |_| | ||
path = "/tmp/crystal-test-unix-sock" | ||
|
||
UNIXServer.open(path) do |server| | ||
UNIXSocket.open(path) do |_| | ||
client = server.accept?.not_nil! | ||
client.should be_a(UNIXSocket) | ||
client.close | ||
|
@@ -339,6 +483,36 @@ describe UNIXSocket do | |
end | ||
end | ||
|
||
{% if flag?(:linux) %} | ||
it "sends and receives messages over an abstract STREAM socket" do | ||
path = "/abstract-service.sock" | ||
abstract_path = '@' + path | ||
|
||
UNIXServer.open(abstract_path) do |server| | ||
server.local_address.abstract?.should be_true | ||
server.local_address.path.should eq(path) | ||
|
||
UNIXSocket.open(abstract_path) do |client| | ||
client.local_address.abstract?.should be_true | ||
client.local_address.path.should eq(path) | ||
|
||
server.accept do |sock| | ||
sock.local_address.path.should eq("") | ||
sock.local_address.abstract?.should be_true | ||
|
||
sock.remote_address.path.should eq("") | ||
sock.remote_address.abstract?.should be_true | ||
|
||
client << "ping" | ||
sock.gets(4).should eq("ping") | ||
sock << "pong" | ||
client.gets(4).should eq("pong") | ||
end | ||
end | ||
end | ||
end | ||
{% end %} | ||
|
||
it "sync flag after accept" do | ||
path = "/tmp/crystal-test-unix-sock" | ||
|
||
|
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
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.
These two macro branches can be combined.