diff options
author | Eric Wong <e@80x24.org> | 2013-10-30 01:50:16 +0000 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2013-10-30 07:01:00 +0000 |
commit | 666d1936c72ae73b9f02d73000015135cdc4a716 (patch) | |
tree | bdc0a1f4ad57f0f10bf3fb364b2643007acacce1 /test/test_unix_socket.rb | |
parent | 6b5b073d80d5790c24321a9d15484426341f8c13 (diff) | |
download | yahns-666d1936c72ae73b9f02d73000015135cdc4a716.tar.gz |
This should prevent us from introducing bugs into some otherwise rarely-tested code.
Diffstat (limited to 'test/test_unix_socket.rb')
-rw-r--r-- | test/test_unix_socket.rb | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/test/test_unix_socket.rb b/test/test_unix_socket.rb new file mode 100644 index 0000000..374781c --- /dev/null +++ b/test/test_unix_socket.rb @@ -0,0 +1,71 @@ +# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors +# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt) +require_relative 'server_helper' + +class TestUnixSocket < Testcase + ENV["N"].to_i > 1 and parallelize_me! + include ServerHelper + alias setup server_helper_setup + alias teardown server_helper_teardown + + def unix_socket(path) + Timeout.timeout(30) do + begin + c = UNIXSocket.new(path) + c.close_on_exec = true + return c + rescue Errno::ENOENT + sleep 0.01 + retry + end + end + end + + def test_socket + tmpdir = Dir.mktmpdir + err, cfg = @err, Yahns::Config.new + sock = "#{tmpdir}/sock" + cfg.instance_eval do + ru = lambda { |_| [ 200, {'Content-Length'=>'2'}, ['HI'] ] } + GTL.synchronize { app(:rack, ru) { listen sock } } + stderr_path err.path + end + pid = mkserver(cfg) + c = unix_socket(sock) + c.write "GET / HTTP/1.0\r\n\r\n" + assert_equal c, c.wait(30) + buf = c.read + assert_match %r{\AHTTP/1\.1 200 OK\r\n}, buf + assert_match %r{\r\n\r\nHI\z}, buf + st = File.stat(sock) + assert st.world_readable? + assert st.world_writable? + ensure + quit_wait(pid) + FileUtils.rm_rf(tmpdir) + end + + def test_socket_perms + tmpdir = Dir.mktmpdir + err, cfg = @err, Yahns::Config.new + sock = "#{tmpdir}/sock" + cfg.instance_eval do + ru = lambda { |_| [ 200, {'Content-Length'=>'2'}, ['HI'] ] } + GTL.synchronize { app(:rack, ru) { listen sock, umask: 0077 } } + stderr_path err.path + end + pid = mkserver(cfg) + c = unix_socket(sock) + c.write "GET / HTTP/1.0\r\n\r\n" + assert_equal c, c.wait(30) + buf = c.read + assert_match %r{\AHTTP/1\.1 200 OK\r\n}, buf + assert_match %r{\r\n\r\nHI\z}, buf + st = File.stat(sock) + refute st.world_readable? + refute st.world_writable? + ensure + quit_wait(pid) + FileUtils.rm_rf(tmpdir) + end +end |