2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2011-01-28 18:46:47 -05:00
|
|
|
require 'rubygems/test_case'
|
2008-03-31 18:40:06 -04:00
|
|
|
require 'rubygems/package'
|
|
|
|
|
2011-01-28 18:46:47 -05:00
|
|
|
##
|
|
|
|
# A test case for Gem::Package::Tar* classes
|
|
|
|
|
|
|
|
class Gem::Package::TarTestCase < Gem::TestCase
|
2008-03-31 18:40:06 -04:00
|
|
|
|
|
|
|
def ASCIIZ(str, length)
|
|
|
|
str + "\0" * (length - str.length)
|
|
|
|
end
|
|
|
|
|
|
|
|
def SP(s)
|
|
|
|
s + " "
|
|
|
|
end
|
|
|
|
|
|
|
|
def SP_Z(s)
|
|
|
|
s + " \0"
|
|
|
|
end
|
|
|
|
|
|
|
|
def Z(s)
|
|
|
|
s + "\0"
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_headers_equal(expected, actual)
|
|
|
|
expected = expected.to_s unless String === expected
|
|
|
|
actual = actual.to_s unless String === actual
|
|
|
|
|
|
|
|
fields = %w[
|
|
|
|
name 100
|
|
|
|
mode 8
|
|
|
|
uid 8
|
|
|
|
gid 8
|
|
|
|
size 12
|
|
|
|
mtime 12
|
|
|
|
checksum 8
|
|
|
|
typeflag 1
|
|
|
|
linkname 100
|
|
|
|
magic 6
|
|
|
|
version 2
|
2009-12-08 02:19:09 -05:00
|
|
|
uname 32
|
2008-03-31 18:40:06 -04:00
|
|
|
gname 32
|
|
|
|
devmajor 8
|
|
|
|
devminor 8
|
|
|
|
prefix 155
|
|
|
|
]
|
|
|
|
|
|
|
|
offset = 0
|
|
|
|
|
|
|
|
until fields.empty? do
|
|
|
|
name = fields.shift
|
|
|
|
length = fields.shift.to_i
|
|
|
|
|
|
|
|
if name == "checksum" then
|
|
|
|
chksum_off = offset
|
|
|
|
offset += length
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2009-12-08 02:19:09 -05:00
|
|
|
assert_equal expected[offset, length], actual[offset, length],
|
2008-03-31 18:40:06 -04:00
|
|
|
"Field #{name} of the tar header differs."
|
|
|
|
|
|
|
|
offset += length
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal expected[chksum_off, 8], actual[chksum_off, 8]
|
|
|
|
end
|
|
|
|
|
|
|
|
def calc_checksum(header)
|
|
|
|
sum = header.unpack("C*").inject{|s,a| s + a}
|
|
|
|
SP(Z(to_oct(sum, 6)))
|
|
|
|
end
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def header(type, fname, dname, length, mode, mtime, checksum = nil, linkname = "")
|
2008-03-31 18:40:06 -04:00
|
|
|
checksum ||= " " * 8
|
|
|
|
|
|
|
|
arr = [ # struct tarfile_entry_posix
|
|
|
|
ASCIIZ(fname, 100), # char name[100]; ASCII + (Z unless filled)
|
|
|
|
Z(to_oct(mode, 7)), # char mode[8]; 0 padded, octal null
|
|
|
|
Z(to_oct(0, 7)), # char uid[8]; ditto
|
|
|
|
Z(to_oct(0, 7)), # char gid[8]; ditto
|
|
|
|
Z(to_oct(length, 11)), # char size[12]; 0 padded, octal, null
|
2013-09-14 04:59:02 -04:00
|
|
|
Z(to_oct(mtime, 11)), # char mtime[12]; 0 padded, octal, null
|
2008-03-31 18:40:06 -04:00
|
|
|
checksum, # char checksum[8]; 0 padded, octal, null, space
|
|
|
|
type, # char typeflag[1]; file: "0" dir: "5"
|
2015-07-01 17:50:14 -04:00
|
|
|
ASCIIZ(linkname, 100), # char linkname[100]; ASCII + (Z unless filled)
|
2008-03-31 18:40:06 -04:00
|
|
|
"ustar\0", # char magic[6]; "ustar\0"
|
|
|
|
"00", # char version[2]; "00"
|
|
|
|
ASCIIZ("wheel", 32), # char uname[32]; ASCIIZ
|
|
|
|
ASCIIZ("wheel", 32), # char gname[32]; ASCIIZ
|
|
|
|
Z(to_oct(0, 7)), # char devmajor[8]; 0 padded, octal, null
|
|
|
|
Z(to_oct(0, 7)), # char devminor[8]; 0 padded, octal, null
|
|
|
|
ASCIIZ(dname, 155) # char prefix[155]; ASCII + (Z unless filled)
|
|
|
|
]
|
|
|
|
|
|
|
|
format = "C100C8C8C8C12C12C8CC100C6C2C32C32C8C8C155"
|
|
|
|
h = if RUBY_VERSION >= "1.9" then
|
|
|
|
arr.join
|
|
|
|
else
|
|
|
|
arr = arr.join("").split(//).map{|x| x[0]}
|
|
|
|
arr.pack format
|
|
|
|
end
|
|
|
|
ret = h + "\0" * (512 - h.size)
|
|
|
|
assert_equal(512, ret.size)
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def tar_dir_header(name, prefix, mode, mtime)
|
|
|
|
h = header("5", name, prefix, 0, mode, mtime)
|
2008-03-31 18:40:06 -04:00
|
|
|
checksum = calc_checksum(h)
|
2013-09-14 04:59:02 -04:00
|
|
|
header("5", name, prefix, 0, mode, mtime, checksum)
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def tar_file_header(fname, dname, mode, length, mtime)
|
|
|
|
h = header("0", fname, dname, length, mode, mtime)
|
2008-03-31 18:40:06 -04:00
|
|
|
checksum = calc_checksum(h)
|
2013-09-14 04:59:02 -04:00
|
|
|
header("0", fname, dname, length, mode, mtime, checksum)
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def tar_symlink_header(fname, prefix, mode, mtime, linkname)
|
|
|
|
h = header("2", fname, prefix, 0, mode, mtime, nil, linkname)
|
|
|
|
checksum = calc_checksum(h)
|
|
|
|
header("2", fname, prefix, 0, mode, mtime, checksum, linkname)
|
|
|
|
end
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def to_oct(n, pad_size)
|
|
|
|
"%0#{pad_size}o" % n
|
|
|
|
end
|
|
|
|
|
|
|
|
def util_entry(tar)
|
|
|
|
io = TempIO.new tar
|
2011-01-18 19:08:49 -05:00
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
header = Gem::Package::TarHeader.from io
|
2011-01-18 19:08:49 -05:00
|
|
|
|
|
|
|
Gem::Package::TarReader::Entry.new header, io
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def util_dir_entry
|
2013-09-14 04:59:02 -04:00
|
|
|
util_entry tar_dir_header("foo", "bar", 0, Time.now)
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def util_symlink_entry
|
|
|
|
util_entry tar_symlink_header("foo", "bar", 0, Time.now, "link")
|
|
|
|
end
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
end
|