Class: Gem::Package::TarTestCase

Inherits:
TestCase show all
Defined in:
lib/rubygems/package/tar_test_case.rb

Overview

A test case for Gem::Package::Tar* classes

Constant Summary

Constants inherited from TestCase

TestCase::PRIVATE_KEY_PASSPHRASE, TestCase::TEST_PATH

Instance Attribute Summary

Attributes inherited from TestCase

#fetcher, #gem_repo, #uri

Instance Method Summary collapse

Methods inherited from TestCase

#_synchronize, #add_to_fetcher, #all_spec_names, #assert_activate, #assert_contains_make_command, #assert_directory_exists, #bindir, #build_rake_in, #capture_subprocess_io, cert_path, #common_installer_setup, #common_installer_teardown, #credential_setup, #credential_teardown, #dep, #dependency_request, #enable_shared, #exeext, #git_gem, #have_git?, #in_path?, #install_default_gems, #install_gem, #install_gem_user, #install_specs, java_platform?, #java_platform?, key_path, load_cert, load_key, #load_yaml, #load_yaml_file, #loaded_spec_names, make_command, #make_command, #mu_pp, #new_default_spec, #nmake_found?, #parse_make_command_line, #process_based_port, process_based_port, #quick_gem, #read_binary, #read_cache, #req, #ruby_with_rubygems_in_load_path, rubybin, #save_gemspec, #save_loaded_features, #scan_make_command_lines, #setup, #spec, #spec_fetcher, #teardown, #uninstall_gem, #unresolved_names, #util_build_gem, #util_clear_RUBY_VERSION, #util_clear_gems, #util_gem, #util_gzip, #util_make_gems, #util_remove_gem, #util_restore_RUBY_VERSION, #util_set_RUBY_VERSION, #util_set_arch, #util_setup_spec_fetcher, #util_spec, #util_zip, #v, #vc_windows?, vc_windows?, #vendor_gem, #vendordir, #wait_for_child_process_to_exit, #win_platform?, win_platform?, #with_clean_path_to_ruby, #without_any_upwards_gemfiles, #write_file

Methods included from Deprecate

#deprecate, next_rubygems_major_version, rubygems_deprecate, rubygems_deprecate_command, skip, skip=, skip_during

Methods included from DefaultUserInteraction

ui, #ui, ui=, #ui=, use_ui, #use_ui

Methods included from Text

#clean_text, #format_text, #levenshtein_distance, #min3, #truncate_text

Instance Method Details

#ASCIIZ(str, length) ⇒ Object



9
10
11
# File 'lib/rubygems/package/tar_test_case.rb', line 9

def ASCIIZ(str, length)
  str + "\0" * (length - str.length)
end

#assert_headers_equal(expected, actual) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rubygems/package/tar_test_case.rb', line 25

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
    uname 32
    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"
      chksum_off = offset
      offset += length
      next
    end

    assert_equal expected[offset, length], actual[offset, length],
                 "Field #{name} of the tar header differs."

    offset += length
  end

  assert_equal expected[chksum_off, 8], actual[chksum_off, 8]
end

#calc_checksum(header) ⇒ Object



69
70
71
72
# File 'lib/rubygems/package/tar_test_case.rb', line 69

def calc_checksum(header)
  sum = header.unpack("C*").inject{|s,a| s + a }
  SP(Z(to_oct(sum, 6)))
end

#header(type, fname, dname, length, mode, mtime, checksum = nil, linkname = "") ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rubygems/package/tar_test_case.rb', line 74

def header(type, fname, dname, length, mode, mtime, checksum = nil, linkname = "")
  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
    Z(to_oct(mtime, 11)),  # char mtime[12];     0 padded, octal, null
    checksum,              # char checksum[8];   0 padded, octal, null, space
    type,                  # char typeflag[1];   file: "0"  dir: "5"
    ASCIIZ(linkname, 100), # char linkname[100]; ASCII + (Z unless filled)
    "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)
  ]

  h = arr.join
  ret = h + "\0" * (512 - h.size)
  assert_equal(512, ret.size)
  ret
end

#SP(s) ⇒ Object



13
14
15
# File 'lib/rubygems/package/tar_test_case.rb', line 13

def SP(s)
  s + " "
end

#SP_Z(s) ⇒ Object



17
18
19
# File 'lib/rubygems/package/tar_test_case.rb', line 17

def SP_Z(s)
  s + " \0"
end

#tar_dir_header(name, prefix, mode, mtime) ⇒ Object



102
103
104
105
106
# File 'lib/rubygems/package/tar_test_case.rb', line 102

def tar_dir_header(name, prefix, mode, mtime)
  h = header("5", name, prefix, 0, mode, mtime)
  checksum = calc_checksum(h)
  header("5", name, prefix, 0, mode, mtime, checksum)
end

#tar_file_header(fname, dname, mode, length, mtime) ⇒ Object



108
109
110
111
112
# File 'lib/rubygems/package/tar_test_case.rb', line 108

def tar_file_header(fname, dname, mode, length, mtime)
  h = header("0", fname, dname, length, mode, mtime)
  checksum = calc_checksum(h)
  header("0", fname, dname, length, mode, mtime, checksum)
end


114
115
116
117
118
# File 'lib/rubygems/package/tar_test_case.rb', line 114

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

#to_oct(n, pad_size) ⇒ Object



120
121
122
# File 'lib/rubygems/package/tar_test_case.rb', line 120

def to_oct(n, pad_size)
  "%0#{pad_size}o" % n
end

#util_dir_entryObject



132
133
134
# File 'lib/rubygems/package/tar_test_case.rb', line 132

def util_dir_entry
  util_entry tar_dir_header("foo", "bar", 0, Time.now)
end

#util_entry(tar) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/rubygems/package/tar_test_case.rb', line 124

def util_entry(tar)
  io = TempIO.new tar

  header = Gem::Package::TarHeader.from io

  Gem::Package::TarReader::Entry.new header, io
end

#util_symlink_entryObject



136
137
138
# File 'lib/rubygems/package/tar_test_case.rb', line 136

def util_symlink_entry
  util_entry tar_symlink_header("foo", "bar", 0, Time.now, "link")
end

#Z(s) ⇒ Object



21
22
23
# File 'lib/rubygems/package/tar_test_case.rb', line 21

def Z(s)
  s + "\0"
end