2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2011-01-28 18:46:47 -05:00
|
|
|
require 'rubygems/package/tar_test_case'
|
2008-03-31 18:40:06 -04:00
|
|
|
require 'rubygems/package/tar_writer'
|
2013-09-14 04:59:02 -04:00
|
|
|
require 'minitest/mock'
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
class TestGemPackageTarWriter < Gem::Package::TarTestCase
|
2008-03-31 18:40:06 -04:00
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
|
|
|
|
@data = 'abcde12345'
|
|
|
|
@io = TempIO.new
|
|
|
|
@tar_writer = Gem::Package::TarWriter.new @io
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
@tar_writer.close unless @tar_writer.closed?
|
2014-05-26 10:36:22 -04:00
|
|
|
@io.close!
|
2008-03-31 18:40:06 -04:00
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_file
|
2013-09-14 04:59:02 -04:00
|
|
|
Time.stub :now, Time.at(1458518157) do
|
|
|
|
@tar_writer.add_file 'x', 0644 do |f| f.write 'a' * 10 end
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
assert_headers_equal(tar_file_header('x', '', 0644, 10, Time.now),
|
2008-03-31 18:40:06 -04:00
|
|
|
@io.string[0, 512])
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
2008-03-31 18:40:06 -04:00
|
|
|
assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
|
|
|
|
assert_equal 1024, @io.pos
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def test_add_symlink
|
|
|
|
Time.stub :now, Time.at(1458518157) do
|
|
|
|
@tar_writer.add_symlink 'x', 'y', 0644
|
|
|
|
|
|
|
|
assert_headers_equal(tar_symlink_header('x', '', 0644, Time.now, 'y'),
|
|
|
|
@io.string[0, 512])
|
|
|
|
end
|
|
|
|
assert_equal 512, @io.pos
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_add_file_digest
|
2013-09-14 04:59:02 -04:00
|
|
|
digest_algorithms = Digest::SHA1, Digest::SHA512
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
Time.stub :now, Time.at(1458518157) do
|
|
|
|
digests = @tar_writer.add_file_digest 'x', 0644, digest_algorithms do |io|
|
|
|
|
io.write 'a' * 10
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
assert_equal '3495ff69d34671d1e15b33a63c1379fdedd3a32a',
|
|
|
|
digests['SHA1'].hexdigest
|
|
|
|
assert_equal '4714870aff6c97ca09d135834fdb58a6389a50c1' \
|
|
|
|
'1fef8ec4afef466fb60a23ac6b7a9c92658f14df' \
|
|
|
|
'4993d6b40a4e4d8424196afc347e97640d68de61' \
|
|
|
|
'e1cf14b0',
|
|
|
|
digests['SHA512'].hexdigest
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
assert_headers_equal(tar_file_header('x', '', 0644, 10, Time.now),
|
2012-11-29 01:52:18 -05:00
|
|
|
@io.string[0, 512])
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
|
|
|
|
assert_equal 1024, @io.pos
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_file_digest_multiple
|
2013-09-14 04:59:02 -04:00
|
|
|
digest_algorithms = [Digest::SHA1, Digest::SHA512]
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
Time.stub :now, Time.at(1458518157) do
|
|
|
|
digests = @tar_writer.add_file_digest 'x', 0644, digest_algorithms do |io|
|
|
|
|
io.write 'a' * 10
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
assert_equal '3495ff69d34671d1e15b33a63c1379fdedd3a32a',
|
|
|
|
digests['SHA1'].hexdigest
|
|
|
|
assert_equal '4714870aff6c97ca09d135834fdb58a6389a50c1' \
|
|
|
|
'1fef8ec4afef466fb60a23ac6b7a9c92658f14df' \
|
|
|
|
'4993d6b40a4e4d8424196afc347e97640d68de61' \
|
|
|
|
'e1cf14b0',
|
|
|
|
digests['SHA512'].hexdigest
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
assert_headers_equal(tar_file_header('x', '', 0644, 10, Time.now),
|
|
|
|
@io.string[0, 512])
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
|
|
|
|
assert_equal 1024, @io.pos
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_file_signer
|
2013-09-14 04:59:02 -04:00
|
|
|
skip 'openssl is missing' unless defined?(OpenSSL::SSL)
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
signer = Gem::Security::Signer.new PRIVATE_KEY, [PUBLIC_CERT]
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
Time.stub :now, Time.at(1458518157) do
|
|
|
|
@tar_writer.add_file_signed 'x', 0644, signer do |io|
|
|
|
|
io.write 'a' * 10
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_headers_equal(tar_file_header('x', '', 0644, 10, Time.now),
|
|
|
|
@io.string[0, 512])
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
digest = signer.digest_algorithm.new
|
|
|
|
digest.update 'a' * 10
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
signature = signer.sign digest.digest
|
|
|
|
|
|
|
|
assert_headers_equal(tar_file_header('x.sig', '', 0444, signature.length,
|
|
|
|
Time.now),
|
|
|
|
@io.string[1024, 512])
|
|
|
|
assert_equal "#{signature}#{"\0" * (512 - signature.length)}",
|
|
|
|
@io.string[1536, 512]
|
|
|
|
|
|
|
|
assert_equal 2048, @io.pos
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_file_signer_empty
|
|
|
|
signer = Gem::Security::Signer.new nil, nil
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
Time.stub :now, Time.at(1458518157) do
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
@tar_writer.add_file_signed 'x', 0644, signer do |io|
|
|
|
|
io.write 'a' * 10
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_headers_equal(tar_file_header('x', '', 0644, 10, Time.now),
|
2012-11-29 01:52:18 -05:00
|
|
|
@io.string[0, 512])
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
|
|
|
|
|
|
|
|
assert_equal 1024, @io.pos
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_file_simple
|
2013-09-14 04:59:02 -04:00
|
|
|
Time.stub :now, Time.at(1458518157) do
|
|
|
|
@tar_writer.add_file_simple 'x', 0644, 10 do |io| io.write "a" * 10 end
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
assert_headers_equal(tar_file_header('x', '', 0644, 10, Time.now),
|
2008-03-31 18:40:06 -04:00
|
|
|
@io.string[0, 512])
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
2008-03-31 18:40:06 -04:00
|
|
|
|
|
|
|
assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
|
|
|
|
assert_equal 1024, @io.pos
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_file_simple_padding
|
2013-09-14 04:59:02 -04:00
|
|
|
Time.stub :now, Time.at(1458518157) do
|
|
|
|
@tar_writer.add_file_simple 'x', 0, 100
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
assert_headers_equal tar_file_header('x', '', 0, 100, Time.now),
|
2008-03-31 18:40:06 -04:00
|
|
|
@io.string[0, 512]
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
2008-03-31 18:40:06 -04:00
|
|
|
|
|
|
|
assert_equal "\0" * 512, @io.string[512, 512]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_file_simple_data
|
|
|
|
@tar_writer.add_file_simple("lib/foo/bar", 0, 10) { |f| f.write @data }
|
|
|
|
@tar_writer.flush
|
|
|
|
|
|
|
|
assert_equal @data + ("\0" * (512-@data.size)),
|
|
|
|
@io.string[512, 512]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_file_simple_size
|
2009-12-08 02:19:09 -05:00
|
|
|
assert_raises Gem::Package::TarWriter::FileOverflow do
|
2008-03-31 18:40:06 -04:00
|
|
|
@tar_writer.add_file_simple("lib/foo/bar", 0, 10) do |io|
|
|
|
|
io.write "1" * 11
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_file_unseekable
|
2009-12-08 02:19:09 -05:00
|
|
|
assert_raises Gem::Package::NonSeekableIO do
|
2008-03-31 18:40:06 -04:00
|
|
|
Gem::Package::TarWriter.new(Object.new).add_file 'x', 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_close
|
|
|
|
@tar_writer.close
|
|
|
|
|
|
|
|
assert_equal "\0" * 1024, @io.string
|
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
e = assert_raises IOError do
|
2008-03-31 18:40:06 -04:00
|
|
|
@tar_writer.close
|
|
|
|
end
|
|
|
|
assert_equal 'closed Gem::Package::TarWriter', e.message
|
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
e = assert_raises IOError do
|
2008-03-31 18:40:06 -04:00
|
|
|
@tar_writer.flush
|
|
|
|
end
|
|
|
|
assert_equal 'closed Gem::Package::TarWriter', e.message
|
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
e = assert_raises IOError do
|
2008-03-31 18:40:06 -04:00
|
|
|
@tar_writer.add_file 'x', 0
|
|
|
|
end
|
|
|
|
assert_equal 'closed Gem::Package::TarWriter', e.message
|
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
e = assert_raises IOError do
|
2008-03-31 18:40:06 -04:00
|
|
|
@tar_writer.add_file_simple 'x', 0, 0
|
|
|
|
end
|
|
|
|
assert_equal 'closed Gem::Package::TarWriter', e.message
|
|
|
|
|
2008-10-25 18:58:43 -04:00
|
|
|
e = assert_raises IOError do
|
2008-03-31 18:40:06 -04:00
|
|
|
@tar_writer.mkdir 'x', 0
|
|
|
|
end
|
|
|
|
assert_equal 'closed Gem::Package::TarWriter', e.message
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mkdir
|
2013-09-14 04:59:02 -04:00
|
|
|
Time.stub :now, Time.at(1458518157) do
|
|
|
|
@tar_writer.mkdir 'foo', 0644
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
assert_headers_equal tar_dir_header('foo', '', 0644, Time.now),
|
|
|
|
@io.string[0, 512]
|
|
|
|
|
|
|
|
assert_equal 512, @io.pos
|
|
|
|
end
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_split_name
|
|
|
|
assert_equal ['b' * 100, 'a' * 155],
|
|
|
|
@tar_writer.split_name("#{'a' * 155}/#{'b' * 100}")
|
|
|
|
|
|
|
|
assert_equal ["#{'qwer/' * 19}bla", 'a' * 151],
|
|
|
|
@tar_writer.split_name("#{'a' * 151}/#{'qwer/' * 19}bla")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_split_name_too_long_name
|
|
|
|
name = File.join 'a', 'b' * 100
|
|
|
|
assert_equal ['b' * 100, 'a'], @tar_writer.split_name(name)
|
|
|
|
|
2014-12-06 19:53:01 -05:00
|
|
|
name = File.join 'a', 'b' * 101
|
|
|
|
exception = assert_raises Gem::Package::TooLongFileName do
|
2008-03-31 18:40:06 -04:00
|
|
|
@tar_writer.split_name name
|
|
|
|
end
|
2014-12-06 19:53:01 -05:00
|
|
|
assert_includes exception.message, name
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_split_name_too_long_prefix
|
|
|
|
name = File.join 'a' * 155, 'b'
|
|
|
|
assert_equal ['b', 'a' * 155], @tar_writer.split_name(name)
|
|
|
|
|
2014-12-06 19:53:01 -05:00
|
|
|
name = File.join 'a' * 156, 'b'
|
|
|
|
exception = assert_raises Gem::Package::TooLongFileName do
|
2008-03-31 18:40:06 -04:00
|
|
|
@tar_writer.split_name name
|
|
|
|
end
|
2014-12-06 19:53:01 -05:00
|
|
|
assert_includes exception.message, name
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_split_name_too_long_total
|
2014-12-06 19:53:01 -05:00
|
|
|
name = 'a' * 257
|
|
|
|
exception = assert_raises Gem::Package::TooLongFileName do
|
|
|
|
@tar_writer.split_name name
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
2014-12-06 19:53:01 -05:00
|
|
|
assert_includes exception.message, name
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|