1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/test_tmpdir.rb
nobu 1fae154c07 tmpdir.rb: permission of user given directory
* lib/tmpdir.rb (Dir.mktmpdir): check if the permission of the
  parent directory only when using the default temporary
  directory, and no check against user given directory.  the
  security is the user's responsibility in that case.
  [ruby-core:91216] [Bug #15555]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66909 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-01-23 06:06:47 +00:00

86 lines
2.2 KiB
Ruby

# frozen_string_literal: true
require 'test/unit'
require 'tmpdir'
class TestTmpdir < Test::Unit::TestCase
def test_tmpdir_modifiable
tmpdir = Dir.tmpdir
assert_equal(false, tmpdir.frozen?)
tmpdir_org = tmpdir.dup
tmpdir << "foo"
assert_equal(tmpdir_org, Dir.tmpdir)
end
def test_tmpdir_modifiable_safe
Thread.new {
$SAFE = 1
tmpdir = Dir.tmpdir
assert_equal(false, tmpdir.frozen?)
tmpdir_org = tmpdir.dup
tmpdir << "foo"
assert_equal(tmpdir_org, Dir.tmpdir)
}.join
ensure
$SAFE = 0
end
def test_world_writable
skip "no meaning on this platform" if /mswin|mingw/ =~ RUBY_PLATFORM
Dir.mktmpdir do |tmpdir|
# ToDo: fix for parallel test
olddir, ENV["TMPDIR"] = ENV["TMPDIR"], tmpdir
begin
assert_equal(tmpdir, Dir.tmpdir)
File.chmod(0777, tmpdir)
assert_not_equal(tmpdir, Dir.tmpdir)
newdir = Dir.mktmpdir("d", tmpdir) do |dir|
assert_file.directory? dir
assert_equal(tmpdir, File.dirname(dir))
dir
end
assert_file.not_exist?(newdir)
File.chmod(01777, tmpdir)
assert_equal(tmpdir, Dir.tmpdir)
ensure
ENV["TMPDIR"] = olddir
end
end
end
def test_no_homedir
bug7547 = '[ruby-core:50793]'
home, ENV["HOME"] = ENV["HOME"], nil
dir = assert_nothing_raised(bug7547) do
break Dir.mktmpdir("~")
end
assert_match(/\A~/, File.basename(dir), bug7547)
ensure
ENV["HOME"] = home
Dir.rmdir(dir) if dir
end
def test_mktmpdir_nil
Dir.mktmpdir(nil) {|d|
assert_kind_of(String, d)
}
end
TRAVERSAL_PATH = Array.new(Dir.pwd.split('/').count, '..').join('/') + Dir.pwd + '/'
TRAVERSAL_PATH.delete!(':') if /mswin|mingw/ =~ RUBY_PLATFORM
def test_mktmpdir_traversal
expect = Dir.glob(TRAVERSAL_PATH + '*').count
Dir.mktmpdir(TRAVERSAL_PATH + 'foo') do
actual = Dir.glob(TRAVERSAL_PATH + '*').count
assert_equal expect, actual
end
end
def test_mktmpdir_traversal_array
expect = Dir.glob(TRAVERSAL_PATH + '*').count
Dir.mktmpdir([TRAVERSAL_PATH, 'foo']) do
actual = Dir.glob(TRAVERSAL_PATH + '*').count
assert_equal expect, actual
end
end
end