From 42f5f956c3dbae0151176762a42ce564d603975c Mon Sep 17 00:00:00 2001
From: usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
Date: Wed, 28 Mar 2018 14:34:14 +0000
Subject: [PATCH] merge revision(s) 62990:
Ignore file separator from tmpfile/tmpdir name.
From: SHIBATA Hiroshi <hsbt@ruby-lang.org>
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@63017 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
---
lib/tmpdir.rb | 2 ++
test/test_tempfile.rb | 49 ++++++++++++++++++++++++++++++++++++++++++-
test/test_tmpdir.rb | 40 +++++++++++++++++++++++++++++++++++
3 files changed, 90 insertions(+), 1 deletion(-)
diff --git a/lib/tmpdir.rb b/lib/tmpdir.rb
index 18d4fb683d..e483f16602 100644
--- a/lib/tmpdir.rb
+++ b/lib/tmpdir.rb
@@ -116,6 +116,8 @@ class Dir
else
raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}"
end
+ prefix = prefix.delete("#{File::SEPARATOR}#{File::ALT_SEPARATOR}")
+ suffix &&= suffix.delete("#{File::SEPARATOR}#{File::ALT_SEPARATOR}")
t = Time.now.strftime("%Y%m%d")
path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
path << "-#{n}" if n
diff --git a/test/test_tempfile.rb b/test/test_tempfile.rb
index 087d9ad31f..b6790a06af 100644
--- a/test/test_tempfile.rb
+++ b/test/test_tempfile.rb
@@ -319,5 +319,52 @@ puts Tempfile.new('foo').path
assert_equal(0600, t.stat.mode & 0777)
end
end
-end
+ def test_create_with_block
+ path = nil
+ Tempfile.create("tempfile-create") {|f|
+ path = f.path
+ assert(File.exist?(path))
+ }
+ assert(!File.exist?(path))
+ end
+
+ def test_create_without_block
+ path = nil
+ f = Tempfile.create("tempfile-create")
+ path = f.path
+ assert(File.exist?(path))
+ f.close
+ assert(File.exist?(path))
+ ensure
+ f.close if f && !f.closed?
+ File.unlink path if path
+ end
+
+ TRAVERSAL_PATH = Array.new(Dir.pwd.split('/').count, '..').join('/') + Dir.pwd + '/'
+
+ def test_open_traversal_dir
+ expect = Dir.glob(TRAVERSAL_PATH + '*').count
+ t = Tempfile.open([TRAVERSAL_PATH, 'foo'])
+ actual = Dir.glob(TRAVERSAL_PATH + '*').count
+ assert_equal expect, actual
+ ensure
+ t.close!
+ end
+
+ def test_new_traversal_dir
+ expect = Dir.glob(TRAVERSAL_PATH + '*').count
+ t = Tempfile.new(TRAVERSAL_PATH + 'foo')
+ actual = Dir.glob(TRAVERSAL_PATH + '*').count
+ assert_equal expect, actual
+ ensure
+ t.close!
+ end
+
+ def test_create_traversal_dir
+ expect = Dir.glob(TRAVERSAL_PATH + '*').count
+ Tempfile.create(TRAVERSAL_PATH + 'foo')
+ actual = Dir.glob(TRAVERSAL_PATH + '*').count
+ assert_equal expect, actual
+ end
+end
diff --git a/test/test_tmpdir.rb b/test/test_tmpdir.rb
index 3bdce3542c..2585453183 100644
--- a/test/test_tmpdir.rb
+++ b/test/test_tmpdir.rb
@@ -30,4 +30,44 @@ class TestTmpdir < Test::Unit::TestCase
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')
+ actual = Dir.glob(TRAVERSAL_PATH + '*').count
+ assert_equal expect, actual
+ end
+
+ def test_mktmpdir_traversal_array
+ expect = Dir.glob(TRAVERSAL_PATH + '*').count
+ Dir.mktmpdir([TRAVERSAL_PATH, 'foo'])
+ actual = Dir.glob(TRAVERSAL_PATH + '*').count
+ assert_equal expect, actual
+ 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')
+ actual = Dir.glob(TRAVERSAL_PATH + '*').count
+ assert_equal expect, actual
+end
+
+ def test_mktmpdir_traversal_array
+ expect = Dir.glob(TRAVERSAL_PATH + '*').count
+ Dir.mktmpdir([TRAVERSAL_PATH, 'foo'])
+ actual = Dir.glob(TRAVERSAL_PATH + '*').count
+ assert_equal expect, actual
+ end
end
--
2.17.1