1 # frozen_string_literal: true
5 class TestEtc < Test::Unit::TestCase
9 assert(s.is_a?(String), "getlogin must return a String or nil")
10 assert_predicate(s, :valid_encoding?, "login name should be a valid string")
15 assert_instance_of(String, s.name)
16 assert_instance_of(String, s.passwd) if s.respond_to?(:passwd)
17 assert_kind_of(Integer, s.uid)
18 assert_kind_of(Integer, s.gid)
19 assert_instance_of(String, s.gecos) if s.respond_to?(:gecos)
20 assert_instance_of(String, s.dir)
21 assert_instance_of(String, s.shell)
22 assert_kind_of(Integer, s.change) if s.respond_to?(:change)
23 assert_kind_of(Integer, s.quota) if s.respond_to?(:quota)
24 assert(s.age.is_a?(Integer) || s.age.is_a?(String)) if s.respond_to?(:age)
25 assert_instance_of(String, s.uclass) if s.respond_to?(:uclass)
26 assert_instance_of(String, s.comment) if s.respond_to?(:comment)
27 assert_kind_of(Integer, s.expire) if s.respond_to?(:expire)
30 Etc.passwd { assert_raise(RuntimeError) { Etc.passwd { } }; break }
34 # password database is not unique on UID, and which entry will be
35 # returned by getpwuid() is not specified.
36 passwd = Hash.new {[]}
37 # on MacOSX, same entries are returned from /etc/passwd and Open
39 Etc.passwd {|s| passwd[s.uid] |= [s]}
40 passwd.each_pair do |uid, s|
41 assert_include(s, Etc.getpwuid(uid))
43 s = passwd[Process.euid]
45 assert_include(s, Etc.getpwuid)
47 end unless RUBY_PLATFORM.include?("android")
52 passwd[s.name] ||= s unless /\A\+/ =~ s.name
54 passwd.each_value do |s|
55 assert_equal(s, Etc.getpwnam(s.name))
57 end unless RUBY_PLATFORM.include?("android")
59 def test_passwd_with_low_level_api
61 Etc.passwd {|s| a << s }
64 while s = Etc.getpwent
73 assert_instance_of(String, s.name)
74 assert_instance_of(String, s.passwd) if s.respond_to?(:passwd)
75 assert_kind_of(Integer, s.gid)
78 Etc.group { assert_raise(RuntimeError) { Etc.group { } }; break }
82 # group database is not unique on GID, and which entry will be
83 # returned by getgrgid() is not specified.
84 groups = Hash.new {[]}
85 # on MacOSX, same entries are returned from /etc/group and Open
87 Etc.group {|s| groups[s.gid] |= [[s.name, s.gid]]}
88 groups.each_pair do |gid, s|
90 assert_include(s, [g.name, g.gid])
92 s = groups[Process.egid]
95 assert_include(s, [g.name, g.gid])
100 groups = Hash.new {[]}
102 groups[s.name] |= [s.gid] unless /\A\+/ =~ s.name
104 groups.each_pair do |n, s|
105 assert_include(s, Etc.getgrnam(n).gid)
109 def test_group_with_low_level_api
111 Etc.group {|s| a << s }
114 while s = Etc.getgrent
124 rescue NotImplementedError
127 assert_kind_of(Hash, uname)
128 [:sysname, :nodename, :release, :version, :machine].each {|sym|
129 assert_operator(uname, :has_key?, sym)
130 assert_kind_of(String, uname[sym])
137 rescue NotImplementedError
141 assert_kind_of(Integer, Etc.sysconf(Etc::SC_CLK_TCK))
142 end if defined?(Etc::SC_CLK_TCK)
147 rescue NotImplementedError
151 assert_kind_of(String, Etc.confstr(Etc::CS_PATH))
152 end if defined?(Etc::CS_PATH)
157 rescue NotImplementedError
162 val = w.pathconf(Etc::PC_PIPE_BUF)
163 assert(val.nil? || val.kind_of?(Integer))
165 end if defined?(Etc::PC_PIPE_BUF)
169 assert_operator(1, :<=, n)
173 return unless Etc.passwd # => skip test if no platform support
176 assert_ractor(<<~RUBY, require: 'etc')
177 ractor = Ractor.new do
184 ractor.take # => :sync
185 assert_raise RuntimeError, /parallel/ do
188 name = ractor.take # => first name
189 ractor.take # => :done
190 name2 = Etc.passwd do |s|
193 assert_equal(name2, name)