diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-12-24 14:20:47 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-12-24 14:20:47 +0000 |
commit | 7e2269a9ce38e3fe9c3b8fd2c469593b60d87263 (patch) | |
tree | 30d9cda9f0e7d52c4dd140368ed4ef9e4dcbd82a /test/fileutils | |
parent | 9ef195f8d649819d56266a0078ebd7fb90ed0762 (diff) |
test_fileutils.rb: tests for chown
* test/fileutils/fileasserts.rb (assert_ownership_user): new
assertion for user ownership.
* test/fileutils/test_fileutils.rb (test_chown_error),
(test_chown_without_permission, test_chown_with_root):
based on the patch by vajrasky (Vajrasky Kok) at
[ruby-core:59298]. [Feature #9292]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44387 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/fileutils')
-rw-r--r-- | test/fileutils/fileasserts.rb | 9 | ||||
-rw-r--r-- | test/fileutils/test_fileutils.rb | 76 |
2 files changed, 84 insertions, 1 deletions
diff --git a/test/fileutils/fileasserts.rb b/test/fileutils/fileasserts.rb index 2cc7e2316b..b6067f6d84 100644 --- a/test/fileutils/fileasserts.rb +++ b/test/fileutils/fileasserts.rb @@ -97,6 +97,15 @@ File group ownership of "#{file}" unexpected: Actual: <#{actual}> EOT end + + def assert_ownership_user(expected, file) + actual = File.stat(file).uid + assert expected == actual, <<EOT +File user ownership of "#{file}" unexpected: + Expected: <#{expected}> + Actual: <#{actual}> +EOT + end end end end diff --git a/test/fileutils/test_fileutils.rb b/test/fileutils/test_fileutils.rb index 89dea1c639..eff863d340 100644 --- a/test/fileutils/test_fileutils.rb +++ b/test/fileutils/test_fileutils.rb @@ -69,6 +69,29 @@ class TestFileUtils < Test::Unit::TestCase return true end + def root_in_posix? + if Process.respond_to?('uid') + return Process.uid == 0 + else + return false + end + end + + def distinct_uids(n = 2) + return unless user = Etc.getpwent + uids = [user.uid] + while user = Etc.getpwent + uid = user.uid + unless uids.include?(uid) + uids << uid + break if uids.size >= n + end + end + uids + ensure + Etc.endpwent + end + begin tmproot = TMPROOT Dir.mkdir tmproot unless File.directory?(tmproot) @@ -1098,7 +1121,58 @@ class TestFileUtils < Test::Unit::TestCase } end if have_file_perm? - # FIXME: Need to add test for chown with root account + if have_file_perm? + def test_chown_error + uid = distinct_uids(1) + return unless uid + + touch 'tmp/a' + + assert_raise_with_message(ArgumentError, "can't find user for ") { + chown '', @groups[0], 'tmp/a' + } + + assert_raise_with_message(ArgumentError, "can't find group for ") { + chown uid, '', 'tmp/a' + } + + assert_raise_with_message(Errno::ENOENT, /No such file or directory/) { + chown nil, @groups[0], '' + } + end + + if root_in_posix? + def test_chown_with_root + uid_1, uid_2 = distinct_uids(2) + return unless uid_1 and uid_2 + + gid = @groups[0] # Most of the time, root only has one group + + files = ['tmp/a1', 'tmp/a2'] + files.each {|file| touch file} + [uid_1, uid_2].each {|uid| + assert_output_lines(["chown #{uid}:#{gid} tmp/a1 tmp/a2"]) { + chown uid, gid, files, verbose: true + files.each {|file| + assert_ownership_group gid, file + assert_ownership_user uid, file + } + } + } + end + else + def test_chown_without_permission + uid_1, uid_2 = distinct_uids(2) + return unless uid_1 and uid_2 + + touch 'tmp/a' + exception = assert_raise(Errno::EPERM) { + chown uid_1, nil, 'tmp/a' + chown uid_2, nil, 'tmp/a' + } + end + end + end # FIXME: How can I test this method? def test_chown_R |