1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

fix env leaks

* lib/rubygems/test_case.rb (setup, teardown): fix
  environment variable change leaks.

* test/cgi/update_env.rb: ditto.

* test/rake/test_rake_application_options.rb (setup, teardown):
  ditto.

* test/rake/test_rake_file_utils.rb (setup, teardown): ditto.

* test/rubygems/test_gem_request.rb (setup): add https_proxy.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49950 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-03-12 14:57:33 +00:00
parent 31eb193883
commit 05c29680e5
13 changed files with 119 additions and 93 deletions

View file

@ -221,6 +221,9 @@ class Gem::TestCase < MiniTest::Unit::TestCase
@orig_gem_home = ENV['GEM_HOME']
@orig_gem_path = ENV['GEM_PATH']
@orig_gem_vendor = ENV['GEM_VENDOR']
@orig_gem_spec_cache = ENV['GEM_SPEC_CACHE']
@orig_rubygems_gemdeps = ENV['RUBYGEMS_GEMDEPS']
@orig_rubygems_host = ENV['RUBYGEMS_HOST']
ENV['GEM_VENDOR'] = nil
@ -353,6 +356,9 @@ class Gem::TestCase < MiniTest::Unit::TestCase
ENV['GEM_HOME'] = @orig_gem_home
ENV['GEM_PATH'] = @orig_gem_path
ENV['GEM_VENDOR'] = @orig_gem_vendor
ENV['GEM_SPEC_CACHE'] = @orig_gem_spec_cache
ENV['RUBYGEMS_GEMDEPS'] = @orig_rubygems_gemdeps
ENV['RUBYGEMS_HOST'] = @orig_rubygems_host
Gem.ruby = @orig_ruby if @orig_ruby

View file

@ -1,21 +1,25 @@
require 'test/unit'
require 'cgi'
require 'stringio'
require_relative 'update_env'
class CGICookieTest < Test::Unit::TestCase
include UpdateEnv
def setup
ENV['REQUEST_METHOD'] = 'GET'
@environ = {}
update_env(
'REQUEST_METHOD' => 'GET',
'SCRIPT_NAME' => nil,
)
@str1="\xE3\x82\x86\xE3\x82\x93\xE3\x82\x86\xE3\x82\x93"
@str1.force_encoding("UTF-8") if defined?(::Encoding)
end
def teardown
%W[REQUEST_METHOD SCRIPT_NAME].each do |name|
ENV.delete(name)
end
ENV.update(@environ)
end

View file

@ -1,12 +1,14 @@
require 'test/unit'
require 'cgi'
require 'stringio'
require_relative 'update_env'
class CGICoreTest < Test::Unit::TestCase
include UpdateEnv
def setup
@environ = {}
#@environ = {
# 'SERVER_PROTOCOL' => 'HTTP/1.1',
# 'REQUEST_METHOD' => 'GET',
@ -15,35 +17,32 @@ class CGICoreTest < Test::Unit::TestCase
#ENV.update(@environ)
end
def teardown
@environ.each do |key, val| ENV.delete(key) end
ENV.update(@environ)
$stdout = STDOUT
end
def test_cgi_parse_illegal_query
@environ = {
update_env(
'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => 'a=111&&b=222&c&d=',
'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
'SERVER_SOFTWARE' => 'Apache 2.2.0',
'SERVER_PROTOCOL' => 'HTTP/1.1',
}
ENV.update(@environ)
)
cgi = CGI.new
assert_equal(["a","b","c","d"],cgi.keys.sort)
assert_equal("",cgi["d"])
end
def test_cgi_core_params_GET
@environ = {
update_env(
'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => 'id=123&id=456&id=&id&str=%40h+%3D%7E+%2F%5E%24%2F',
'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
'SERVER_SOFTWARE' => 'Apache 2.2.0',
'SERVER_PROTOCOL' => 'HTTP/1.1',
}
ENV.update(@environ)
)
cgi = CGI.new
## cgi[]
assert_equal('123', cgi['id'])
@ -68,14 +67,13 @@ class CGICoreTest < Test::Unit::TestCase
def test_cgi_core_params_POST
query_str = 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F'
@environ = {
update_env(
'REQUEST_METHOD' => 'POST',
'CONTENT_LENGTH' => query_str.length.to_s,
'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
'SERVER_SOFTWARE' => 'Apache 2.2.0',
'SERVER_PROTOCOL' => 'HTTP/1.1',
}
ENV.update(@environ)
)
$stdin = StringIO.new
$stdin << query_str
$stdin.rewind
@ -93,13 +91,12 @@ class CGICoreTest < Test::Unit::TestCase
def test_cgi_core_params_encoding_check
query_str = 'str=%BE%BE%B9%BE'
@environ = {
update_env(
'REQUEST_METHOD' => 'POST',
'CONTENT_LENGTH' => query_str.length.to_s,
'SERVER_SOFTWARE' => 'Apache 2.2.0',
'SERVER_PROTOCOL' => 'HTTP/1.1',
}
ENV.update(@environ)
)
$stdin = StringIO.new
$stdin << query_str
$stdin.rewind
@ -131,14 +128,13 @@ class CGICoreTest < Test::Unit::TestCase
def test_cgi_core_cookie
@environ = {
update_env(
'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F',
'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
'SERVER_SOFTWARE' => 'Apache 2.2.0',
'SERVER_PROTOCOL' => 'HTTP/1.1',
}
ENV.update(@environ)
)
cgi = CGI.new
assert_not_equal(nil,cgi.cookies)
[ ['_session_id', ['12345'], ],
@ -156,11 +152,10 @@ class CGICoreTest < Test::Unit::TestCase
def test_cgi_core_maxcontentlength
@environ = {
update_env(
'REQUEST_METHOD' => 'POST',
'CONTENT_LENGTH' => (64 * 1024 * 1024).to_s
}
ENV.update(@environ)
)
ex = assert_raise(StandardError) do
CGI.new
end
@ -169,14 +164,13 @@ class CGICoreTest < Test::Unit::TestCase
def test_cgi_core_out
@environ = {
update_env(
'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F',
'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
'SERVER_SOFTWARE' => 'Apache 2.2.0',
'SERVER_PROTOCOL' => 'HTTP/1.1',
}
ENV.update(@environ)
)
cgi = CGI.new
## euc string
euc_str = "\270\253\244\355\241\242\277\315\244\254\245\264\245\337\244\316\244\350\244\246\244\300"
@ -201,7 +195,7 @@ class CGICoreTest < Test::Unit::TestCase
cgi.out(options) { euc_str }
assert_equal('en', options['language'])
## HEAD method
ENV['REQUEST_METHOD'] = 'HEAD'
update_env('REQUEST_METHOD' => 'HEAD')
options = { 'charset'=>'utf8' }
$stdout = StringIO.new
cgi.out(options) { euc_str }
@ -214,10 +208,9 @@ class CGICoreTest < Test::Unit::TestCase
def test_cgi_core_print
@environ = {
update_env(
'REQUEST_METHOD' => 'GET',
}
ENV.update(@environ)
)
cgi = CGI.new
$stdout = StringIO.new
str = "foobar"
@ -229,10 +222,9 @@ class CGICoreTest < Test::Unit::TestCase
def test_cgi_core_environs
@environ = {
update_env(
'REQUEST_METHOD' => 'GET',
}
ENV.update(@environ)
)
cgi = CGI.new
##
list1 = %w[ AUTH_TYPE CONTENT_TYPE GATEWAY_INTERFACE PATH_INFO
@ -246,9 +238,8 @@ class CGICoreTest < Test::Unit::TestCase
# list2 = %w[ CONTENT_LENGTH SERVER_PORT ]
## string expected
list1.each do |name|
@environ[name] = "**#{name}**"
update_env(name => "**#{name}**")
end
ENV.update(@environ)
list1.each do |name|
method = name.sub(/\AHTTP_/, '').downcase
actual = cgi.__send__ method
@ -256,23 +247,22 @@ class CGICoreTest < Test::Unit::TestCase
assert_equal(expected, actual)
end
## integer expected
ENV['CONTENT_LENGTH'] = '123'
ENV['SERVER_PORT'] = '8080'
update_env('CONTENT_LENGTH' => '123')
update_env('SERVER_PORT' => '8080')
assert_equal(123, cgi.content_length)
assert_equal(8080, cgi.server_port)
## raw cookie
ENV['HTTP_COOKIE'] = 'name1=val1'
ENV['HTTP_COOKIE2'] = 'name2=val2'
update_env('HTTP_COOKIE' => 'name1=val1')
update_env('HTTP_COOKIE2' => 'name2=val2')
assert_equal('name1=val1', cgi.raw_cookie)
assert_equal('name2=val2', cgi.raw_cookie2)
end
def test_cgi_core_htmltype_header
@environ = {
update_env(
'REQUEST_METHOD' => 'GET',
}
ENV.update(@environ)
)
## no htmltype
cgi = CGI.new
assert_raise(NoMethodError) do cgi.doctype end

View file

@ -1,23 +1,25 @@
require 'test/unit'
require 'cgi'
require 'time'
require_relative 'update_env'
class CGIHeaderTest < Test::Unit::TestCase
include UpdateEnv
def setup
@environ = {
@environ = {}
update_env(
'SERVER_PROTOCOL' => 'HTTP/1.1',
'REQUEST_METHOD' => 'GET',
'SERVER_SOFTWARE' => 'Apache 2.2.0',
}
ENV.update(@environ)
)
end
def teardown
@environ.each do |key, val| ENV.delete(key) end
ENV.update(@environ)
end

View file

@ -1,17 +1,19 @@
require 'test/unit'
require 'cgi'
require_relative 'update_env'
class CGIModrubyTest < Test::Unit::TestCase
include UpdateEnv
def setup
@environ = {
@environ = {}
update_env(
'SERVER_PROTOCOL' => 'HTTP/1.1',
'REQUEST_METHOD' => 'GET',
#'QUERY_STRING' => 'a=foo&b=bar',
}
ENV.update(@environ)
)
CGI.class_eval { const_set(:MOD_RUBY, true) }
Apache._reset()
#@cgi = CGI.new
@ -20,7 +22,7 @@ class CGIModrubyTest < Test::Unit::TestCase
def teardown
@environ.each do |key, val| ENV.delete(key) end
ENV.update(@environ)
CGI.class_eval { remove_const(:MOD_RUBY) }
end

View file

@ -2,6 +2,7 @@ require 'test/unit'
require 'cgi'
require 'tempfile'
require 'stringio'
require_relative 'update_env'
##
@ -104,16 +105,21 @@ end
class CGIMultipartTest < Test::Unit::TestCase
include UpdateEnv
def setup
ENV['REQUEST_METHOD'] = 'POST'
@environ = {}
update_env(
'REQUEST_METHOD' => 'POST',
'CONTENT_TYPE' => nil,
'CONTENT_LENGTH' => nil,
)
@tempfiles = []
end
def teardown
%w[ REQUEST_METHOD CONTENT_TYPE CONTENT_LENGTH REQUEST_METHOD ].each do |name|
ENV.delete(name)
end
ENV.update(@environ)
$stdin.close() if $stdin.is_a?(Tempfile)
$stdin = STDIN
@tempfiles.each {|t|

View file

@ -4,30 +4,33 @@ require 'cgi/session'
require 'cgi/session/pstore'
require 'stringio'
require 'tmpdir'
require_relative 'update_env'
class CGISessionTest < Test::Unit::TestCase
include UpdateEnv
def setup
@environ = {}
@session_dir = Dir.mktmpdir(%w'session dir')
end
def teardown
@environ.each do |key, val| ENV.delete(key) end
ENV.update(@environ)
$stdout = STDOUT
FileUtils.rm_rf(@session_dir)
end
def test_cgi_session_filestore
@environ = {
update_env(
'REQUEST_METHOD' => 'GET',
# 'QUERY_STRING' => 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F',
# 'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
'SERVER_SOFTWARE' => 'Apache 2.2.0',
'SERVER_PROTOCOL' => 'HTTP/1.1',
}
)
value1="value1"
value2="\x8F\xBC\x8D]"
value2.force_encoding("SJIS") if defined?(::Encoding)
ENV.update(@environ)
cgi = CGI.new
session = CGI::Session.new(cgi,"tmpdir"=>@session_dir)
session["key1"]=value1
@ -38,14 +41,13 @@ class CGISessionTest < Test::Unit::TestCase
$stdout = StringIO.new
cgi.out{""}
@environ = {
update_env(
'REQUEST_METHOD' => 'GET',
# 'HTTP_COOKIE' => "_session_id=#{session_id}",
'QUERY_STRING' => "_session_id=#{session.session_id}",
'SERVER_SOFTWARE' => 'Apache 2.2.0',
'SERVER_PROTOCOL' => 'HTTP/1.1',
}
ENV.update(@environ)
)
cgi = CGI.new
session = CGI::Session.new(cgi,"tmpdir"=>@session_dir)
$stdout = StringIO.new
@ -55,17 +57,16 @@ class CGISessionTest < Test::Unit::TestCase
end
def test_cgi_session_pstore
@environ = {
update_env(
'REQUEST_METHOD' => 'GET',
# 'QUERY_STRING' => 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F',
# 'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
'SERVER_SOFTWARE' => 'Apache 2.2.0',
'SERVER_PROTOCOL' => 'HTTP/1.1',
}
)
value1="value1"
value2="\x8F\xBC\x8D]"
value2.force_encoding("SJIS") if defined?(::Encoding)
ENV.update(@environ)
cgi = CGI.new
session = CGI::Session.new(cgi,"tmpdir"=>@session_dir,"database_manager"=>CGI::Session::PStore)
session["key1"]=value1
@ -76,14 +77,13 @@ class CGISessionTest < Test::Unit::TestCase
$stdout = StringIO.new
cgi.out{""}
@environ = {
update_env(
'REQUEST_METHOD' => 'GET',
# 'HTTP_COOKIE' => "_session_id=#{session_id}",
'QUERY_STRING' => "_session_id=#{session.session_id}",
'SERVER_SOFTWARE' => 'Apache 2.2.0',
'SERVER_PROTOCOL' => 'HTTP/1.1',
}
ENV.update(@environ)
)
cgi = CGI.new
session = CGI::Session.new(cgi,"tmpdir"=>@session_dir,"database_manager"=>CGI::Session::PStore)
$stdout = StringIO.new
@ -92,17 +92,16 @@ class CGISessionTest < Test::Unit::TestCase
session.close
end
def test_cgi_session_specify_session_id
@environ = {
update_env(
'REQUEST_METHOD' => 'GET',
# 'QUERY_STRING' => 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F',
# 'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
'SERVER_SOFTWARE' => 'Apache 2.2.0',
'SERVER_PROTOCOL' => 'HTTP/1.1',
}
)
value1="value1"
value2="\x8F\xBC\x8D]"
value2.force_encoding("SJIS") if defined?(::Encoding)
ENV.update(@environ)
cgi = CGI.new
session = CGI::Session.new(cgi,"tmpdir"=>@session_dir,"session_id"=>"foo")
session["key1"]=value1
@ -115,14 +114,13 @@ class CGISessionTest < Test::Unit::TestCase
$stdout = StringIO.new
cgi.out{""}
@environ = {
update_env(
'REQUEST_METHOD' => 'GET',
# 'HTTP_COOKIE' => "_session_id=#{session_id}",
'QUERY_STRING' => "_session_id=#{session.session_id}",
'SERVER_SOFTWARE' => 'Apache 2.2.0',
'SERVER_PROTOCOL' => 'HTTP/1.1',
}
ENV.update(@environ)
)
cgi = CGI.new
session = CGI::Session.new(cgi,"tmpdir"=>@session_dir)
$stdout = StringIO.new
@ -132,17 +130,16 @@ class CGISessionTest < Test::Unit::TestCase
session.close
end
def test_cgi_session_specify_session_key
@environ = {
update_env(
'REQUEST_METHOD' => 'GET',
# 'QUERY_STRING' => 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F',
# 'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
'SERVER_SOFTWARE' => 'Apache 2.2.0',
'SERVER_PROTOCOL' => 'HTTP/1.1',
}
)
value1="value1"
value2="\x8F\xBC\x8D]"
value2.force_encoding("SJIS") if defined?(::Encoding)
ENV.update(@environ)
cgi = CGI.new
session = CGI::Session.new(cgi,"tmpdir"=>@session_dir,"session_key"=>"bar")
session["key1"]=value1
@ -154,14 +151,13 @@ class CGISessionTest < Test::Unit::TestCase
$stdout = StringIO.new
cgi.out{""}
@environ = {
update_env(
'REQUEST_METHOD' => 'GET',
'HTTP_COOKIE' => "bar=#{session_id}",
# 'QUERY_STRING' => "bar=#{session.session_id}",
'SERVER_SOFTWARE' => 'Apache 2.2.0',
'SERVER_PROTOCOL' => 'HTTP/1.1',
}
ENV.update(@environ)
)
cgi = CGI.new
session = CGI::Session.new(cgi,"tmpdir"=>@session_dir,"session_key"=>"bar")
$stdout = StringIO.new

View file

@ -1,12 +1,15 @@
require 'test/unit'
require 'cgi'
require 'stringio'
require_relative 'update_env'
class CGITagHelperTest < Test::Unit::TestCase
include UpdateEnv
def setup
@environ = {}
#@environ = {
# 'SERVER_PROTOCOL' => 'HTTP/1.1',
# 'REQUEST_METHOD' => 'GET',
@ -17,16 +20,15 @@ class CGITagHelperTest < Test::Unit::TestCase
def teardown
@environ.each do |key, val| ENV.delete(key) end
ENV.update(@environ)
$stdout = STDOUT
end
def test_cgi_tag_helper_html3
@environ = {
update_env(
'REQUEST_METHOD' => 'GET',
}
ENV.update(@environ)
)
## html3
cgi = CGI.new('html3')
assert_equal('<A HREF=""></A>',cgi.a)
@ -337,10 +339,9 @@ class CGITagHelperTest < Test::Unit::TestCase
=end
def test_cgi_tag_helper_html5
@environ = {
update_env(
'REQUEST_METHOD' => 'GET',
}
ENV.update(@environ)
)
## html5
cgi = CGI.new('html5')
assert_equal('<HEADER></HEADER>',cgi.header)

View file

@ -1,21 +1,25 @@
require 'test/unit'
require 'cgi'
require 'stringio'
require_relative 'update_env'
class CGIUtilTest < Test::Unit::TestCase
include CGI::Util
include UpdateEnv
def setup
ENV['REQUEST_METHOD'] = 'GET'
@environ = {}
update_env(
'REQUEST_METHOD' => 'GET',
'SCRIPT_NAME' => nil,
)
@str1="&<>\" \xE3\x82\x86\xE3\x82\x93\xE3\x82\x86\xE3\x82\x93"
@str1.force_encoding("UTF-8") if defined?(::Encoding)
end
def teardown
%W[REQUEST_METHOD SCRIPT_NAME].each do |name|
ENV.delete(name)
end
ENV.update(@environ)
end

8
test/cgi/update_env.rb Normal file
View file

@ -0,0 +1,8 @@
module UpdateEnv
def update_env(environ)
environ.each do |key, val|
@environ[key] = ENV[key] unless @environ.key?(key)
ENV[key] = val
end
end
end

View file

@ -7,6 +7,7 @@ class TestRakeApplicationOptions < Rake::TestCase
def setup
super
@testkey = ENV['TESTKEY']
clear_argv
Rake::FileUtilsExt.verbose_flag = false
Rake::FileUtilsExt.nowrite_flag = false
@ -14,6 +15,7 @@ class TestRakeApplicationOptions < Rake::TestCase
end
def teardown
ENV['TESTKEY'] = @testkey
clear_argv
Rake::FileUtilsExt.verbose_flag = false
Rake::FileUtilsExt.nowrite_flag = false

View file

@ -3,10 +3,15 @@ require 'fileutils'
require 'stringio'
class TestRakeFileUtils < Rake::TestCase
def setup
super
@rake_test_sh = ENV['RAKE_TEST_SH']
end
def teardown
FileUtils::LN_SUPPORTED[0] = true
RakeFileUtils.verbose_flag = Rake::FileUtilsExt::DEFAULT
ENV['RAKE_TEST_SH'] = @rake_test_sh
super
end

View file

@ -16,7 +16,7 @@ class TestGemRequest < Gem::TestCase
end
def setup
@proxies = %w[http_proxy HTTP_PROXY http_proxy_user HTTP_PROXY_USER http_proxy_pass HTTP_PROXY_PASS no_proxy NO_PROXY]
@proxies = %w[http_proxy https_proxy HTTP_PROXY http_proxy_user HTTP_PROXY_USER http_proxy_pass HTTP_PROXY_PASS no_proxy NO_PROXY]
@old_proxies = @proxies.map {|k| ENV[k] }
@proxies.each {|k| ENV[k] = nil }