1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 68 69 70 71 72 73 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
# frozen_string_literal: true
require "minitest_helper"
class TestTarReader < Minitest::Test
def test_open_no_block
str = build_tar_file_header("lib/foo", "", 0o10644, 10) + "\0" * 512
str += build_tar_file_header("bar", "baz", 0o644, 0)
str += build_tar_dir_header("foo", "bar", 0o12345)
str += "\0" * 1024
reader = Minitar::Reader.open(StringIO.new(str))
refute reader.closed?
ensure
reader.close
refute reader.closed? # Reader doesn't actually close anything
end
def test_multiple_entries
str = build_tar_file_header("lib/foo", "", 0o10644, 10) + "\0" * 512
str += build_tar_file_header("bar", "baz", 0o644, 0)
str += build_tar_dir_header("foo", "bar", 0o12345)
str += build_tar_file_header("src/", "", 0o755, 0) # "file" with a trailing slash
str += "\0" * 1024
names = %w[lib/foo bar foo src/]
prefixes = ["", "baz", "bar", ""]
modes = [0o10644, 0o644, 0o12345, 0o755]
sizes = [10, 0, 0, 0]
isdir = [false, false, true, true]
isfile = [true, true, false, false]
Minitar::Reader.new(StringIO.new(str)) do |is|
i = 0
is.each_entry do |entry|
assert_kind_of Minitar::Reader::EntryStream, entry
assert_equal names[i], entry.name
assert_equal prefixes[i], entry.prefix
assert_equal sizes[i], entry.size
assert_equal modes[i], entry.mode
assert_equal isdir[i], entry.directory?
assert_equal isfile[i], entry.file?
if prefixes[i] != ""
assert_equal File.join(prefixes[i], names[i]), entry.full_name
else
assert_equal names[i], entry.name
end
i += 1
end
assert_equal names.size, i
end
end
def test_rewind_entry_works
content = ("a".."z").to_a.join(" ")
str = build_tar_file_header("lib/foo", "", 0o10644, content.bytesize) +
content + "\0" * (512 - content.bytesize)
str << "\0" * 1024
Minitar::Reader.new(StringIO.new(str)) do |is|
is.each_entry do |entry|
3.times do
entry.rewind
assert_equal content, entry.read
assert_equal content.bytesize, entry.pos
end
end
end
end
def test_rewind_works
content = ("a".."z").to_a.join(" ")
str = build_tar_file_header("lib/foo", "", 0o10644, content.bytesize) +
content + "\0" * (512 - content.bytesize)
str << "\0" * 1024
Minitar::Reader.new(StringIO.new(str)) do |is|
3.times do
is.rewind
i = 0
is.each_entry do |entry|
assert_equal content, entry.read
i += 1
end
assert_equal 1, i
end
end
end
def test_read_works
contents = ("a".."z").inject(+"") { |a, e| a << e * 100 }
str = build_tar_file_header("lib/foo", "", 0o10644, contents.bytesize) + contents
str += "\0" * (512 - (str.bytesize % 512))
Minitar::Reader.new(StringIO.new(str)) do |is|
is.each_entry do |entry|
assert_kind_of Minitar::Reader::EntryStream, entry
data = entry.read(3000) # bigger than contents.bytesize
assert_equal contents, data
assert entry.eof?
end
end
Minitar::Reader.new(StringIO.new(str)) do |is|
is.each_entry do |entry|
assert_kind_of Minitar::Reader::EntryStream, entry
data = entry.read(100)
(entry.size - data.bytesize).times { data << entry.getc.chr }
assert_equal contents, data
assert_nil entry.read(10)
assert entry.eof?
end
end
Minitar::Reader.new(StringIO.new(str)) do |is|
is.each_entry do |entry|
assert_kind_of Minitar::Reader::EntryStream, entry
data = entry.read
assert_equal contents, data
assert_nil entry.read(10)
assert_nil entry.read
assert_nil entry.getc
assert entry.eof?
end
end
end
def test_eof_works
str = build_tar_file_header("bar", "baz", 0o644, 0)
Minitar::Reader.new(StringIO.new(str)) do |is|
is.each_entry do |entry|
assert_kind_of Minitar::Reader::EntryStream, entry
data = entry.read
assert_nil data
assert_nil entry.read(10)
assert_nil entry.read
assert_nil entry.getc
assert entry.eof?
end
end
str = build_tar_dir_header("foo", "bar", 0o12345)
Minitar::Reader.new(StringIO.new(str)) do |is|
is.each_entry do |entry|
assert_kind_of Minitar::Reader::EntryStream, entry
data = entry.read
assert_nil data
assert_nil entry.read(10)
assert_nil entry.read
assert_nil entry.getc
assert entry.eof?
end
end
str = build_tar_dir_header("foo", "bar", 0o12345)
str += build_tar_file_header("bar", "baz", 0o644, 0)
str += build_tar_file_header("bar", "baz", 0o644, 0)
Minitar::Reader.new(StringIO.new(str)) do |is|
is.each_entry do |entry|
assert_kind_of Minitar::Reader::EntryStream, entry
data = entry.read
assert_nil data
assert_nil entry.read(10)
assert_nil entry.read
assert_nil entry.getc
assert entry.eof?
end
end
end
def test_read_invalid_tar_file
assert_raises Minitar::InvalidTarStream do
Minitar::Reader.open(StringIO.new("testing")) do |r|
r.each_entry do |entry|
fail "invalid tar file should not read files"
end
end
end
end
end
|