2017-01-28 20:35:23 -05:00
|
|
|
# frozen_string_literal: true
|
2008-06-03 09:26:38 -04:00
|
|
|
require "test/unit"
|
|
|
|
require "etc"
|
|
|
|
|
|
|
|
class TestEtc < Test::Unit::TestCase
|
|
|
|
def test_getlogin
|
|
|
|
s = Etc.getlogin
|
2014-11-10 03:24:36 -05:00
|
|
|
return if s == nil
|
|
|
|
assert(s.is_a?(String), "getlogin must return a String or nil")
|
|
|
|
assert_predicate(s, :valid_encoding?, "login name should be a valid string")
|
2008-06-03 09:26:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_passwd
|
|
|
|
Etc.passwd do |s|
|
|
|
|
assert_instance_of(String, s.name)
|
|
|
|
assert_instance_of(String, s.passwd) if s.respond_to?(:passwd)
|
|
|
|
assert_kind_of(Integer, s.uid)
|
|
|
|
assert_kind_of(Integer, s.gid)
|
|
|
|
assert_instance_of(String, s.gecos) if s.respond_to?(:gecos)
|
|
|
|
assert_instance_of(String, s.dir)
|
|
|
|
assert_instance_of(String, s.shell)
|
|
|
|
assert_kind_of(Integer, s.change) if s.respond_to?(:change)
|
|
|
|
assert_kind_of(Integer, s.quota) if s.respond_to?(:quota)
|
2008-07-29 08:36:28 -04:00
|
|
|
assert(s.age.is_a?(Integer) || s.age.is_a?(String)) if s.respond_to?(:age)
|
2008-06-03 09:26:38 -04:00
|
|
|
assert_instance_of(String, s.uclass) if s.respond_to?(:uclass)
|
|
|
|
assert_instance_of(String, s.comment) if s.respond_to?(:comment)
|
|
|
|
assert_kind_of(Integer, s.expire) if s.respond_to?(:expire)
|
|
|
|
end
|
|
|
|
|
2010-05-05 02:11:04 -04:00
|
|
|
Etc.passwd { assert_raise(RuntimeError) { Etc.passwd { } }; break }
|
2008-06-03 09:26:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_getpwuid
|
2012-08-05 01:14:35 -04:00
|
|
|
# password database is not unique on UID, and which entry will be
|
|
|
|
# returned by getpwuid() is not specified.
|
|
|
|
passwd = Hash.new {[]}
|
|
|
|
# on MacOSX, same entries are returned from /etc/passwd and Open
|
|
|
|
# Directory.
|
|
|
|
Etc.passwd {|s| passwd[s.uid] |= [s]}
|
|
|
|
passwd.each_pair do |uid, s|
|
|
|
|
assert_include(s, Etc.getpwuid(uid))
|
|
|
|
end
|
|
|
|
s = passwd[Process.euid]
|
2012-08-05 22:20:47 -04:00
|
|
|
unless s.empty?
|
2012-08-05 01:14:35 -04:00
|
|
|
assert_include(s, Etc.getpwuid)
|
2008-06-03 09:26:38 -04:00
|
|
|
end
|
2020-02-12 03:28:29 -05:00
|
|
|
end unless RUBY_PLATFORM.include?("android")
|
2008-06-03 09:26:38 -04:00
|
|
|
|
|
|
|
def test_getpwnam
|
2009-02-03 10:39:44 -05:00
|
|
|
passwd = {}
|
2011-06-26 12:34:20 -04:00
|
|
|
Etc.passwd do |s|
|
|
|
|
passwd[s.name] ||= s unless /\A\+/ =~ s.name
|
|
|
|
end
|
2009-02-06 10:26:36 -05:00
|
|
|
passwd.each_value do |s|
|
2008-06-03 09:26:38 -04:00
|
|
|
assert_equal(s, Etc.getpwnam(s.name))
|
|
|
|
end
|
2020-02-12 03:28:29 -05:00
|
|
|
end unless RUBY_PLATFORM.include?("android")
|
2008-06-03 09:26:38 -04:00
|
|
|
|
2008-09-03 10:54:14 -04:00
|
|
|
def test_passwd_with_low_level_api
|
2008-06-03 09:26:38 -04:00
|
|
|
a = []
|
|
|
|
Etc.passwd {|s| a << s }
|
|
|
|
b = []
|
2008-09-03 10:54:14 -04:00
|
|
|
Etc.setpwent
|
|
|
|
while s = Etc.getpwent
|
2008-06-03 09:26:38 -04:00
|
|
|
b << s
|
|
|
|
end
|
2008-09-03 10:54:14 -04:00
|
|
|
Etc.endpwent
|
2008-06-03 09:26:38 -04:00
|
|
|
assert_equal(a, b)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_group
|
|
|
|
Etc.group do |s|
|
|
|
|
assert_instance_of(String, s.name)
|
|
|
|
assert_instance_of(String, s.passwd) if s.respond_to?(:passwd)
|
|
|
|
assert_kind_of(Integer, s.gid)
|
|
|
|
end
|
|
|
|
|
2010-05-05 02:11:04 -04:00
|
|
|
Etc.group { assert_raise(RuntimeError) { Etc.group { } }; break }
|
2008-06-03 09:26:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_getgrgid
|
2012-08-27 07:57:43 -04:00
|
|
|
# group database is not unique on GID, and which entry will be
|
|
|
|
# returned by getgrgid() is not specified.
|
|
|
|
groups = Hash.new {[]}
|
|
|
|
# on MacOSX, same entries are returned from /etc/group and Open
|
|
|
|
# Directory.
|
2015-01-10 02:15:12 -05:00
|
|
|
Etc.group {|s| groups[s.gid] |= [[s.name, s.gid]]}
|
2012-08-27 07:57:43 -04:00
|
|
|
groups.each_pair do |gid, s|
|
2015-01-10 02:15:12 -05:00
|
|
|
g = Etc.getgrgid(gid)
|
|
|
|
assert_include(s, [g.name, g.gid])
|
2008-06-04 02:04:38 -04:00
|
|
|
end
|
2012-08-27 07:57:43 -04:00
|
|
|
s = groups[Process.egid]
|
|
|
|
unless s.empty?
|
2015-01-10 02:15:12 -05:00
|
|
|
g = Etc.getgrgid
|
|
|
|
assert_include(s, [g.name, g.gid])
|
2008-06-03 09:26:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_getgrnam
|
2017-09-29 13:19:37 -04:00
|
|
|
groups = Hash.new {[]}
|
2008-06-03 09:26:38 -04:00
|
|
|
Etc.group do |s|
|
2017-09-29 13:19:37 -04:00
|
|
|
groups[s.name] |= [s.gid] unless /\A\+/ =~ s.name
|
2008-06-04 02:04:38 -04:00
|
|
|
end
|
2015-01-10 02:15:12 -05:00
|
|
|
groups.each_pair do |n, s|
|
2017-09-29 13:19:37 -04:00
|
|
|
assert_include(s, Etc.getgrnam(n).gid)
|
2008-06-03 09:26:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-09-03 10:54:14 -04:00
|
|
|
def test_group_with_low_level_api
|
2008-06-03 09:26:38 -04:00
|
|
|
a = []
|
|
|
|
Etc.group {|s| a << s }
|
|
|
|
b = []
|
2008-09-03 10:54:14 -04:00
|
|
|
Etc.setgrent
|
|
|
|
while s = Etc.getgrent
|
2008-06-03 09:26:38 -04:00
|
|
|
b << s
|
|
|
|
end
|
2008-09-03 10:54:14 -04:00
|
|
|
Etc.endgrent
|
2008-06-03 09:26:38 -04:00
|
|
|
assert_equal(a, b)
|
|
|
|
end
|
2014-05-17 20:58:34 -04:00
|
|
|
|
|
|
|
def test_uname
|
|
|
|
begin
|
|
|
|
uname = Etc.uname
|
|
|
|
rescue NotImplementedError
|
|
|
|
return
|
|
|
|
end
|
|
|
|
assert_kind_of(Hash, uname)
|
|
|
|
[:sysname, :nodename, :release, :version, :machine].each {|sym|
|
|
|
|
assert_operator(uname, :has_key?, sym)
|
|
|
|
assert_kind_of(String, uname[sym])
|
|
|
|
}
|
|
|
|
end
|
2014-05-17 21:48:47 -04:00
|
|
|
|
|
|
|
def test_sysconf
|
|
|
|
begin
|
|
|
|
Etc.sysconf
|
|
|
|
rescue NotImplementedError
|
|
|
|
return
|
|
|
|
rescue ArgumentError
|
|
|
|
end
|
|
|
|
assert_kind_of(Integer, Etc.sysconf(Etc::SC_CLK_TCK))
|
2014-05-18 19:35:04 -04:00
|
|
|
end if defined?(Etc::SC_CLK_TCK)
|
2014-05-17 21:48:47 -04:00
|
|
|
|
|
|
|
def test_confstr
|
|
|
|
begin
|
|
|
|
Etc.confstr
|
|
|
|
rescue NotImplementedError
|
|
|
|
return
|
|
|
|
rescue ArgumentError
|
|
|
|
end
|
|
|
|
assert_kind_of(String, Etc.confstr(Etc::CS_PATH))
|
2014-05-18 19:35:04 -04:00
|
|
|
end if defined?(Etc::CS_PATH)
|
2014-05-17 21:48:47 -04:00
|
|
|
|
|
|
|
def test_pathconf
|
|
|
|
begin
|
|
|
|
Etc.confstr
|
|
|
|
rescue NotImplementedError
|
|
|
|
return
|
|
|
|
rescue ArgumentError
|
|
|
|
end
|
|
|
|
IO.pipe {|r, w|
|
2014-05-17 22:06:15 -04:00
|
|
|
val = w.pathconf(Etc::PC_PIPE_BUF)
|
2014-05-17 21:48:47 -04:00
|
|
|
assert(val.nil? || val.kind_of?(Integer))
|
|
|
|
}
|
2014-05-18 19:35:04 -04:00
|
|
|
end if defined?(Etc::PC_PIPE_BUF)
|
2014-05-17 21:48:47 -04:00
|
|
|
|
2014-10-01 23:23:26 -04:00
|
|
|
def test_nprocessors
|
|
|
|
n = Etc.nprocessors
|
|
|
|
assert_operator(1, :<=, n)
|
|
|
|
end
|
|
|
|
|
2008-06-03 09:26:38 -04:00
|
|
|
end
|