1 require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
2 require 'rubygems/package'
6 # straight from setup.rb
8 # for corrupted windows stat()
9 File.directory?((path[-1,1] == '/') ? path : path + '/')
13 File.open(name, "rb") { |f| f.read }
18 class TarTestCase < RubyGemTestCase
20 def ASCIIZ(str, length)
21 str + "\0" * (length - str.length)
36 def assert_headers_equal(expected, actual)
37 expected = expected.to_s unless String === expected
38 actual = actual.to_s unless String === actual
61 until fields.empty? do
63 length = fields.shift.to_i
65 if name == "checksum" then
71 assert_equal expected[offset, length], actual[offset, length],
72 "Field #{name} of the tar header differs."
77 assert_equal expected[chksum_off, 8], actual[chksum_off, 8]
80 def calc_checksum(header)
81 sum = header.unpack("C*").inject{|s,a| s + a}
85 def header(type, fname, dname, length, mode, checksum = nil)
88 arr = [ # struct tarfile_entry_posix
89 ASCIIZ(fname, 100), # char name[100]; ASCII + (Z unless filled)
90 Z(to_oct(mode, 7)), # char mode[8]; 0 padded, octal null
91 Z(to_oct(0, 7)), # char uid[8]; ditto
92 Z(to_oct(0, 7)), # char gid[8]; ditto
93 Z(to_oct(length, 11)), # char size[12]; 0 padded, octal, null
94 Z(to_oct(0, 11)), # char mtime[12]; 0 padded, octal, null
95 checksum, # char checksum[8]; 0 padded, octal, null, space
96 type, # char typeflag[1]; file: "0" dir: "5"
97 "\0" * 100, # char linkname[100]; ASCII + (Z unless filled)
98 "ustar\0", # char magic[6]; "ustar\0"
99 "00", # char version[2]; "00"
100 ASCIIZ("wheel", 32), # char uname[32]; ASCIIZ
101 ASCIIZ("wheel", 32), # char gname[32]; ASCIIZ
102 Z(to_oct(0, 7)), # char devmajor[8]; 0 padded, octal, null
103 Z(to_oct(0, 7)), # char devminor[8]; 0 padded, octal, null
104 ASCIIZ(dname, 155) # char prefix[155]; ASCII + (Z unless filled)
107 format = "C100C8C8C8C12C12C8CC100C6C2C32C32C8C8C155"
108 h = if RUBY_VERSION >= "1.9" then
111 arr = arr.join("").split(//).map{|x| x[0]}
114 ret = h + "\0" * (512 - h.size)
115 assert_equal(512, ret.size)
119 def tar_dir_header(name, prefix, mode)
120 h = header("5", name, prefix, 0, mode)
121 checksum = calc_checksum(h)
122 header("5", name, prefix, 0, mode, checksum)
125 def tar_file_header(fname, dname, mode, length)
126 h = header("0", fname, dname, length, mode)
127 checksum = calc_checksum(h)
128 header("0", fname, dname, length, mode, checksum)
131 def to_oct(n, pad_size)
137 header = Gem::Package::TarHeader.from io
138 entry = Gem::Package::TarReader::Entry.new header, io
142 util_entry tar_dir_header("foo", "bar", 0)