1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/etc/test_etc.rb
shirosaki a5849245c6 test_etc.rb: fix for non unique GID
* test/etc/test_etc.rb (TestEtc#test_getgrgid): fix for non unique GID.
  No unixen systems guarantee that GID is unique. Etc.getgrgid would
  not return the first entry in the order of Etc.group for shared GID.
  [ruby-core:47312] [Bug #6935]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36833 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-08-27 11:57:43 +00:00

115 lines
3 KiB
Ruby

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)
assert(s.age.is_a?(Integer) || s.age.is_a?(String)) if s.respond_to?(:age)
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
Etc.passwd { assert_raise(RuntimeError) { Etc.passwd { } }; break }
end
def test_getpwuid
# 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]
unless s.empty?
assert_include(s, Etc.getpwuid)
end
end
def test_getpwnam
passwd = {}
Etc.passwd do |s|
passwd[s.name] ||= s unless /\A\+/ =~ s.name
end
passwd.each_value do |s|
assert_equal(s, Etc.getpwnam(s.name))
end
end
def test_passwd_with_low_level_api
a = []
Etc.passwd {|s| a << s }
b = []
Etc.setpwent
while s = Etc.getpwent
b << s
end
Etc.endpwent
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
Etc.group { assert_raise(RuntimeError) { Etc.group { } }; break }
end
def test_getgrgid
# 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.
Etc.group {|s| groups[s.gid] |= [s]}
groups.each_pair do |gid, s|
assert_include(s, Etc.getgrgid(gid))
end
s = groups[Process.egid]
unless s.empty?
assert_include(s, Etc.getgrgid)
end
end
def test_getgrnam
groups = {}
Etc.group do |s|
groups[s.name] ||= s unless /\A\+/ =~ s.name
end
groups.each_value do |s|
assert_equal(s, Etc.getgrnam(s.name))
end
end
def test_group_with_low_level_api
a = []
Etc.group {|s| a << s }
b = []
Etc.setgrent
while s = Etc.getgrent
b << s
end
Etc.endgrent
assert_equal(a, b)
end
end