2008-06-03 09:26:38 -04:00
|
|
|
require "test/unit"
|
|
|
|
require "etc"
|
|
|
|
|
|
|
|
class TestEtc < Test::Unit::TestCase
|
|
|
|
def test_getlogin
|
|
|
|
s = Etc.getlogin
|
|
|
|
assert(s.is_a?(String) || s == nil, "getlogin must return a String or nil")
|
|
|
|
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
|
2008-07-29 00:21:32 -04:00
|
|
|
passwd = {}
|
2009-02-06 10:26:36 -05:00
|
|
|
Etc.passwd {|s| passwd[s.uid] ||= s }
|
|
|
|
passwd.each_value do |s|
|
2008-06-03 09:26:38 -04:00
|
|
|
assert_equal(s, Etc.getpwuid(s.uid))
|
2008-09-03 11:02:22 -04:00
|
|
|
assert_equal(s, Etc.getpwuid) if Process.euid == s.uid
|
2008-06-03 09:26:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_getpwnam
|
2009-02-03 10:39:44 -05:00
|
|
|
passwd = {}
|
2009-02-06 10:26:36 -05:00
|
|
|
Etc.passwd {|s| passwd[s.name] ||= s }
|
|
|
|
passwd.each_value do |s|
|
2008-06-03 09:26:38 -04:00
|
|
|
assert_equal(s, Etc.getpwnam(s.name))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
2009-02-03 10:39:44 -05:00
|
|
|
groups = {}
|
2008-06-03 09:26:38 -04:00
|
|
|
Etc.group do |s|
|
2009-02-06 10:26:36 -05:00
|
|
|
groups[s.gid] ||= s
|
2008-06-04 02:04:38 -04:00
|
|
|
end
|
2009-02-06 10:26:36 -05:00
|
|
|
groups.each_value do |s|
|
2008-06-03 09:26:38 -04:00
|
|
|
assert_equal(s, Etc.getgrgid(s.gid))
|
2008-09-03 11:02:22 -04:00
|
|
|
assert_equal(s, Etc.getgrgid) if Process.egid == s.gid
|
2008-06-03 09:26:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_getgrnam
|
2009-02-03 10:39:44 -05:00
|
|
|
groups = {}
|
2008-06-03 09:26:38 -04:00
|
|
|
Etc.group do |s|
|
2009-02-06 10:26:36 -05:00
|
|
|
groups[s.name] ||= s
|
2008-06-04 02:04:38 -04:00
|
|
|
end
|
2009-02-06 10:26:36 -05:00
|
|
|
groups.each_value do |s|
|
2008-06-03 09:26:38 -04:00
|
|
|
assert_equal(s, Etc.getgrnam(s.name))
|
|
|
|
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
|
|
|
|
end
|