summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog10
-rw-r--r--test/fileutils/fileasserts.rb9
-rw-r--r--test/fileutils/test_fileutils.rb76
3 files changed, 94 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 3e15bfd0c5..71ca216da7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+Tue Dec 24 23:20:38 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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]
+
Tue Dec 24 16:28:05 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* proc.c (rb_mod_define_method): consider visibility only if self
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