Project

General

Profile

Feature #9286 » add_unit_test_chown_normal_user.patch

vajrasky (Vajrasky Kok), 12/23/2013 06:32 PM

View differences:

test/fileutils/test_fileutils.rb (working copy)
# $Id$
require 'fileutils'
require 'etc'
require_relative 'fileasserts'
require 'pathname'
require 'tmpdir'
......
def setup
@prevdir = Dir.pwd
@groups = Process.groups
tmproot = TMPROOT
mymkdir tmproot unless File.directory?(tmproot)
Dir.chdir tmproot
......
}
end if have_file_perm?
# FIXME: How can I test this method?
def test_chown
check_singleton :chown
assert_output_lines([]) {
touch 'tmp/a'
# integer input for group, nil for user
chown nil, @groups[0], 'tmp/a'
assert_ownership_group @groups[0], 'tmp/a'
}
if @groups[1]
assert_output_lines([]) {
touch 'tmp/b'
# string input for group, -1 for user
chown -1, Etc.getgrgid(@groups[1]).name, 'tmp/b'
assert_ownership_group @groups[1], 'tmp/b'
}
end
end if have_file_perm?
def test_chown_verbose
assert_output_lines(["chown :#{@groups[0]} tmp/a1 tmp/a2"]) {
touch 'tmp/a1'
touch 'tmp/a2'
chown nil, @groups[0], ['tmp/a1', 'tmp/a2'], :verbose => true
assert_ownership_group @groups[0], 'tmp/a1'
assert_ownership_group @groups[0], 'tmp/a2'
}
end if have_file_perm?
def test_chown_noop
if @groups[1]
touch 'tmp/a'
chown nil, @groups[0], 'tmp/a', :noop => false
assert_ownership_group @groups[0], 'tmp/a'
chown nil, @groups[1], 'tmp/a', :noop => true
assert_ownership_group @groups[0], 'tmp/a'
chown nil, @groups[1], 'tmp/a'
assert_ownership_group @groups[1], 'tmp/a'
end
end if have_file_perm?
def test_chown_error
touch 'tmp/a'
exception = assert_raise(Errno::EPERM) {
chown 'root', @groups[0], 'tmp/a'
}
assert_equal exception.message,
"Operation not permitted @ chown_internal - tmp/a"
exception = assert_raise(ArgumentError) {
chown '', @groups[0], 'tmp/a'
}
assert_equal exception.message, "can't find user for "
exception = assert_raise(Errno::ENOENT) {
chown nil, @groups[0], ''
}
assert_equal exception.message,
"No such file or directory @ chown_internal - "
end if have_file_perm?
# FIXME: Need to add test for chown with root account
# FIXME: How can I test this method?
def test_chown_R
check_singleton :chown_R
test/fileutils/fileasserts.rb (working copy)
<#{'%0*o' % [width, mode2]}>: "#{file2}"
EOT
end
def assert_ownership_group(expected, file)
actual = File.stat(file).gid
assert expected == actual, <<EOT
File group ownership of "#{file}" unexpected:
Expected: <#{'%s' % expected}>
Actual: <#{'%s' % actual}>
EOT
end
end
end
end
(2-2/2)