summaryrefslogtreecommitdiff
path: root/spec/ruby/library/socket/ancillarydata/unix_rights_spec.rb
blob: 0bbef4c08da6add5a5dcc3a0de01e1f13341622e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require_relative '../spec_helper'

with_feature :ancillary_data do
  describe 'Socket::AncillaryData.unix_rights' do
    describe 'using a list of IO objects' do
      before do
        @data = Socket::AncillaryData.unix_rights(STDOUT, STDERR)
      end

      it 'sets the family to AF_UNIX' do
        @data.family.should == Socket::AF_UNIX
      end

      it 'sets the level to SOL_SOCKET' do
        @data.level.should == Socket::SOL_SOCKET
      end

      it 'sets the type to SCM_RIGHTS' do
        @data.type.should == Socket::SCM_RIGHTS
      end

      it 'sets the data to a String containing the file descriptors' do
        @data.data.unpack('I*').should == [STDOUT.fileno, STDERR.fileno]
      end
    end

    describe 'using non IO objects' do
      it 'raises TypeError' do
        lambda { Socket::AncillaryData.unix_rights(10) }.should raise_error(TypeError)
      end
    end
  end

  describe 'Socket::AncillaryData#unix_rights' do
    it 'returns the data as an Array of IO objects' do
      data = Socket::AncillaryData.unix_rights(STDOUT, STDERR)

      data.unix_rights.should == [STDOUT, STDERR]
    end

    it 'returns nil when the data is not a list of file descriptors' do
      data = Socket::AncillaryData.new(:UNIX, :SOCKET, :RIGHTS, '')

      data.unix_rights.should be_nil
    end

    it 'raises TypeError when the level is not SOL_SOCKET' do
      data = Socket::AncillaryData.new(:INET, :IP, :RECVTTL, '')

      lambda { data.unix_rights }.should raise_error(TypeError)
    end

    it 'raises TypeError when the type is not SCM_RIGHTS' do
      data = Socket::AncillaryData.new(:INET, :SOCKET, :TIMESTAMP, '')

      lambda { data.unix_rights }.should raise_error(TypeError)
    end
  end
end