Sha256: c0dba98935ee49fdf70ad3b73a6c1657ae67ca6481c73a31f6352dd114f0f3d3

Contents?: true

Size: 1.9 KB

Versions: 13

Compression:

Stored size: 1.9 KB

Contents

# 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?
    c.close
  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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
yahns-1.9.0 test/test_unix_socket.rb
yahns-1.8.0 test/test_unix_socket.rb
yahns-1.7.0 test/test_unix_socket.rb
yahns-1.6.0 test/test_unix_socket.rb
yahns-1.5.0 test/test_unix_socket.rb
yahns-1.4.0 test/test_unix_socket.rb
yahns-1.3.1 test/test_unix_socket.rb
yahns-1.3.0 test/test_unix_socket.rb
yahns-1.2.0 test/test_unix_socket.rb
yahns-1.1.0 test/test_unix_socket.rb
yahns-1.0.0 test/test_unix_socket.rb
yahns-0.0.3 test/test_unix_socket.rb
yahns-0.0.2 test/test_unix_socket.rb