minitar 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/minitar.rb CHANGED
@@ -4,45 +4,52 @@ require "fileutils"
4
4
  require "rbconfig"
5
5
  require "rbconfig/sizeof"
6
6
 
7
- # == Synopsis
7
+ # ## Synopsis
8
8
  #
9
9
  # Using minitar is easy. The simplest case is:
10
10
  #
11
- # require 'zlib'
12
- # require 'minitar'
11
+ # ```ruby
12
+ # require 'zlib'
13
+ # require 'minitar'
13
14
  #
14
- # # Packs everything that matches Find.find('tests'). test.tar will automatically be
15
- # # closed by Minitar.pack.
16
- # Minitar.pack('tests', File.open('test.tar', 'wb'))
15
+ # # Packs everything that matches Find.find('tests'). test.tar will automatically be
16
+ # # closed by Minitar.pack.
17
+ # Minitar.pack('tests', File.open('test.tar', 'wb'))
17
18
  #
18
- # # Unpacks 'test.tar' to 'x', creating 'x' if necessary.
19
- # Minitar.unpack('test.tar', 'x')
19
+ # # Unpacks 'test.tar' to 'x', creating 'x' if necessary.
20
+ # Minitar.unpack('test.tar', 'x')
21
+ # ```
20
22
  #
21
23
  # A gzipped tar can be written with:
22
24
  #
23
- # # test.tgz will be closed automatically.
24
- # Minitar.pack('tests', Zlib::GzipWriter.new(File.open('test.tgz', 'wb'))
25
25
  #
26
- # # test.tgz will be closed automatically.
27
- # Minitar.unpack(Zlib::GzipReader.new(File.open('test.tgz', 'rb')), 'x')
26
+ # ```ruby
27
+ # # test.tgz will be closed automatically.
28
+ # Minitar.pack('tests', Zlib::GzipWriter.new(File.open('test.tgz', 'wb'))
29
+ #
30
+ # # test.tgz will be closed automatically.
31
+ # Minitar.unpack(Zlib::GzipReader.new(File.open('test.tgz', 'rb')), 'x')
32
+ # ```
28
33
  #
29
34
  # As the case above shows, one need not write to a file. However, it will sometimes
30
35
  # require that one dive a little deeper into the API, as in the case of StringIO objects.
31
36
  # Note that I'm not providing a block with Minitar::Output, as Minitar::Output#close
32
37
  # automatically closes both the Output object and the wrapped data stream object.
33
38
  #
34
- # begin
35
- # sgz = Zlib::GzipWriter.new(StringIO.new(""))
36
- # tar = Minitar::Output.new(sgz)
37
- # Find.find('tests') do |entry|
38
- # Minitar.pack_file(entry, tar)
39
- # end
40
- # ensure
41
- # # Closes both tar and sgz.
42
- # tar.close
43
- # end
39
+ # ```ruby
40
+ # begin
41
+ # sgz = Zlib::GzipWriter.new(StringIO.new(""))
42
+ # tar = Minitar::Output.new(sgz)
43
+ # Find.find('tests') do |entry|
44
+ # Minitar.pack_file(entry, tar)
45
+ # end
46
+ # ensure
47
+ # # Closes both tar and sgz.
48
+ # tar.close
49
+ # end
50
+ # ```
44
51
  class Minitar
45
- # The base class for any minitar error.
52
+ # The base class for any Minitar error.
46
53
  Error = Class.new(::StandardError)
47
54
  # Raised when a wrapped data stream class is not seekable.
48
55
  NonSeekableStream = Class.new(Error)
@@ -54,24 +61,28 @@ class Minitar
54
61
  FileNameTooLong = Class.new(Error)
55
62
  # The exception raised when a data stream ends before the amount of data expected in the
56
63
  # archive's PosixHeader.
57
- UnexpectedEOF = Class.new(StandardError)
64
+ UnexpectedEOF = Class.new(Error)
58
65
  # The exception raised when a file contains a relative path in secure mode (the default
59
66
  # for this version).
60
67
  SecureRelativePathError = Class.new(Error)
68
+ # The exception raised when an entry name cannot be safely written on the current
69
+ # platform (for example, a name containing `:` on Windows, which may address an NTFS
70
+ # alternate data stream).
71
+ UnsafeFileNameError = Class.new(Error)
61
72
  # The exception raised when a file contains an invalid Posix header.
62
73
  InvalidTarStream = Class.new(Error)
63
74
  end
64
75
 
65
76
  class << Minitar
66
- # Tests if +path+ refers to a directory. Fixes an apparently corrupted <tt>stat()</tt>
67
- # call on Windows.
68
- def dir?(path)
77
+ # Tests if `path` refers to a directory. Fixes an apparently corrupted `stat()` call on
78
+ # Windows.
79
+ def dir?(path) # :nodoc:
69
80
  path = "#{path}/" unless path.to_s.end_with?("/")
70
81
  File.directory?(path)
71
82
  end
72
83
 
73
- # A convenience method for wrapping Minitar::Input.open (mode +r+ or +rb) and
74
- # Minitar::Output.open (mode +w+ or +wb+). No other modes are currently supported.
84
+ # A convenience method for wrapping Minitar::Input.open (mode `r` or `rb`) and
85
+ # Minitar::Output.open (mode `w` or `wb`). No other modes are currently supported.
75
86
  def open(dest, mode = "r", &)
76
87
  case mode
77
88
  when "r", "rb"
@@ -79,28 +90,34 @@ class << Minitar
79
90
  when "w", "wb"
80
91
  Minitar::Output.open(dest, &)
81
92
  else
82
- raise ArgumentError, "Unknown open mode for Minitar.open."
93
+ raise ArgumentError, "Unknown open mode #{mode.inspect} for Minitar.open."
83
94
  end
84
95
  end
85
96
 
86
- def windows? = # :nodoc:
87
- RUBY_PLATFORM =~ /^(?:cyginw|mingw|mswin)/i ||
88
- RbConfig::CONFIG["host_os"].to_s =~ /^(cygwin|mingw|mswin|windows)/i ||
89
- File::ALT_SEPARATOR == "\\"
97
+ WINDOWS_CONFIG = RbConfig::CONFIG["host_os"].to_s =~ /^(cygwin|mingw|mswin|windows)/i
98
+ WINDOWS_PLATFORM = RUBY_PLATFORM =~ /^(?:cygwin|mingw|mswin)/i
99
+ WINDOWS_SEPARATOR = File::ALT_SEPARATOR == "\\"
100
+
101
+ private_constant :WINDOWS_CONFIG, :WINDOWS_PLATFORM, :WINDOWS_SEPARATOR
102
+
103
+ def windows? = windows_platform? || windows_config? || File::ALT_SEPARATOR == "\\" # :nodoc:
104
+ def windows_config? = WINDOWS_CONFIG # :nodoc:
105
+ def windows_platform? = WINDOWS_PLATFORM # :nodoc:
106
+ def windows_separator? = WINDOWS_SEPARATOR # :nodoc:
90
107
 
91
- # A convenience method to pack the provided +data+ as a file named +entry+. +entry+ may
92
- # either be a name or a Hash with the fields described below. When only a name is
108
+ # A convenience method to pack the provided `data` as a file named `entry`, which may
109
+ # either be a name String or a Hash with the fields described below. When only a name is
93
110
  # provided, or only some Hash fields are provided, the default values will apply.
94
111
  #
95
- # <tt>:name</tt>:: The filename to be packed into the archive. Required.
96
- # <tt>:mode</tt>:: The mode to be applied. Defaults to 0o644 for files and 0o755 for
97
- # directories.
98
- # <tt>:uid</tt>:: The user owner of the file. Default is +nil+.
99
- # <tt>:gid</tt>:: The group owner of the file. Default is +nil+.
100
- # <tt>:mtime</tt>:: The modification Time of the file. Default is +Time.now+.
112
+ # - `:name` (**required**): The filename to be packed into the archive.
113
+ # - `:mode`: The mode to be applied. Defaults to `0o644` for files and `0o755` for
114
+ # directories.
115
+ # - `:uid`: The user owner of the file. Default is `nil`.
116
+ # - `:gid`: The group owner of the file. Default is `nil`.
117
+ # - `:mtime`: The modification Time of the file. Default is `Time.now`.
101
118
  #
102
- # If +data+ is +nil+, a directory will be created. Use an empty String for a normal
103
- # empty file.
119
+ # If `data` is `nil`, a directory will be created. Use an empty String for an empty
120
+ # file.
104
121
  def pack_as_file(entry, data, outputter) # :yields: action, name, stats
105
122
  outputter = outputter.tar if outputter.is_a?(Minitar::Output)
106
123
 
@@ -143,35 +160,36 @@ class << Minitar
143
160
  end
144
161
  end
145
162
 
146
- # A convenience method to pack the file provided. +entry+ may either be a filename (in
147
- # which case various values for the file (see below) will be obtained from
148
- # <tt>File#stat(entry)</tt> or a Hash with the fields:
163
+ # A convenience method to pack the file provided. `entry` may either be a filename
164
+ # string (which will in which case various values for the file will be obtained from
165
+ # `File#stat(entry)` or a Hash with the fields:
149
166
  #
150
- # <tt>:name</tt>:: The filename to be packed into the archive. Required.
151
- # <tt>:mode</tt>:: The mode to be applied.
152
- # <tt>:uid</tt>:: The user owner of the file. (Ignored on Windows.)
153
- # <tt>:gid</tt>:: The group owner of the file. (Ignored on Windows.)
154
- # <tt>:mtime</tt>:: The modification Time of the file.
167
+ # - `:name` (**required**): The filename to be packed into the archive.
168
+ # - `:mode`: The mode to be applied.
169
+ # - `:uid`: The user owner of the file, ignored on Windows.
170
+ # - `:gid`: The group owner of the file, ignored on Windows.
171
+ # - `:mtime`: The modification Time of the file.
155
172
  #
156
- # During packing, if a block is provided, #pack_file yields an +action+ Symbol, the full
173
+ # During packing, if a block is provided, #pack_file yields an `action` Symbol, the full
157
174
  # name of the file being packed, and a Hash of statistical information, just as with
158
175
  # Minitar::Input#extract_entry.
159
176
  #
160
- # The +action+ will be one of:
161
- # <tt>:dir</tt>:: The +entry+ is a directory.
162
- # <tt>:file_start</tt>:: The +entry+ is a file; the extract of the file is just
163
- # beginning.
164
- # <tt>:file_progress</tt>:: Yielded every 4096 bytes during the extract of the +entry+.
165
- # <tt>:file_done</tt>:: Yielded when the +entry+ is completed.
177
+ # The `action` will be one of:
166
178
  #
167
- # The +stats+ hash contains the following keys:
168
- # <tt>:current</tt>:: The current total number of bytes read in the +entry+.
169
- # <tt>:currinc</tt>:: The current number of bytes read in this read cycle.
170
- # <tt>:name</tt>:: The filename to be packed into the tarchive. *REQUIRED*.
171
- # <tt>:mode</tt>:: The mode to be applied.
172
- # <tt>:uid</tt>:: The user owner of the file. (+nil+ on Windows.)
173
- # <tt>:gid</tt>:: The group owner of the file. (+nil+ on Windows.)
174
- # <tt>:mtime</tt>:: The modification Time of the file.
179
+ # - `:dir`: The `entry` is a directory.
180
+ # - `:file_start`: The `entry` is a file; the extract of the file is just beginning.
181
+ # - `:file_progress`: Yielded every 4096 bytes during the extract of the file `entry`.
182
+ # - `file_done`: Yielded when the file `entry` is completed.
183
+ #
184
+ # The +stats+ hash may contain the following keys:
185
+ #
186
+ # - `:current`: The current total number of bytes read in the `entry`.
187
+ # - `:currinc`: The current number of bytes read in this read cycle.
188
+ # - `:name`: The filename packed into the tarchive.
189
+ # - `:mode`: The mode to be applied.
190
+ # - `:uid`: The user owner of the file, `nil` on Windows.
191
+ # - `:gid`: The group owner of the file, `nil` on Windows.
192
+ # - `:mtime`: The modification Time of the file.
175
193
  def pack_file(entry, outputter) # :yields: action, name, stats
176
194
  outputter = outputter.tar if outputter.is_a?(Minitar::Output)
177
195
 
@@ -219,11 +237,12 @@ class << Minitar
219
237
  end
220
238
  end
221
239
 
222
- # A convenience method to pack files specified by +src+ into +dest+. If +src+ is an
240
+ # A convenience method to pack files specified by `src` into `dest`. If `src` is an
223
241
  # Array, then each file detailed therein will be packed into the resulting
224
- # Minitar::Output stream; if +recurse_dirs+ is true, then directories will be recursed.
242
+ # Minitar::Output stream. If `recurse_dirs` is `true` (the default), then directories
243
+ # will be recursed.
225
244
  #
226
- # If +src+ is not an Array, it will be treated as the result of Find.find; all files
245
+ # If `src` is not an Array, it will be treated as the source of Find.find; all files
227
246
  # matching will be packed.
228
247
  def pack(src, dest, recurse_dirs = true, &block)
229
248
  require "find"
@@ -246,8 +265,10 @@ class << Minitar
246
265
  end
247
266
  end
248
267
 
249
- # A convenience method to unpack files from +src+ into the directory specified by
250
- # +dest+. Only those files named explicitly in +files+ will be extracted.
268
+ # A convenience method to unpack files from `src` into the directory specified by
269
+ # `dest`.
270
+ #
271
+ # If `files` is not empty, it will act as a full name filter to restrict extraction.
251
272
  def unpack(src, dest, files = [], options = {}, &block)
252
273
  Minitar::Input.open(src) do |inp|
253
274
  if File.exist?(dest) && !dir?(dest)
@@ -264,8 +285,8 @@ class << Minitar
264
285
  end
265
286
  end
266
287
 
267
- # Check whether +io+ can seek without errors.
268
- def seekable?(io, methods = nil)
288
+ # Check whether `io` can seek without errors.
289
+ def seekable?(io, methods = nil) # :nodoc:
269
290
  # The IO class throws an exception at runtime if we try to change position on
270
291
  # a non-regular file.
271
292
  if io.respond_to?(:stat)
@@ -283,4 +304,6 @@ require "minitar/posix_header"
283
304
  require "minitar/pax_header"
284
305
  require "minitar/input"
285
306
  require "minitar/output"
307
+ require "minitar/reader"
308
+ require "minitar/writer"
286
309
  require "minitar/version"
@@ -0,0 +1,170 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "minitest_helper"
4
+
5
+ # Tests the private name normalization and relative path checks. These run on every
6
+ # platform because the alternate separator is passed as a parameter.
7
+ class TestPathNormalization < Minitest::Test
8
+ def input
9
+ @input ||= Minitar::Input.allocate
10
+ end
11
+
12
+ def normalize(name, alt_separator)
13
+ input.send(:normalize_entry_name, name, alt_separator)
14
+ end
15
+
16
+ def check(name)
17
+ input.send(:check_relative_path!, name)
18
+ end
19
+
20
+ # --- check_relative_path! (all platforms) ---
21
+
22
+ def test_check_rejects_parent_components
23
+ [
24
+ "..", "../x", "a/..", "a/../b", "a/../../x", "a/b/.."
25
+ ].each do |name|
26
+ assert_raises(Minitar::SecureRelativePathError, name) { check(name) }
27
+ end
28
+ end
29
+
30
+ def test_check_accepts_dots_within_a_component
31
+ [
32
+ "dirname../x", "..dirname/x", "a/dirname..", "a/..b/c", "...", "a/.../b", "a..b/c",
33
+ ".hidden/x", "a/./b"
34
+ ].each do |name|
35
+ check(name)
36
+ end
37
+
38
+ pass
39
+ end
40
+
41
+ # --- nil alt_separator (POSIX): nothing but squeeze ---
42
+
43
+ def test_nil_separator_keeps_backslashes_literal
44
+ [
45
+ "..\\x", "a\\..\\..\\x", "a\\..\\b", "..\\/x", "C:\\x", "C:x", "C:/x",
46
+ "\\\\host\\share\\x", "a\\b", "a.txt:stream", "...\\x", "dirname..\\x", "a. \\x"
47
+ ].each do |name|
48
+ assert_equal name, normalize(name, nil)
49
+ check(name)
50
+ end
51
+ end
52
+
53
+ def test_nil_separator_squeezes_slashes
54
+ assert_equal "a/b/c", normalize("a//b///c", nil)
55
+ end
56
+
57
+ # --- "\\" alt_separator (Windows semantics) ---
58
+
59
+ def test_backslash_separator_rejects_traversal
60
+ [
61
+ "..\\x", "a\\..\\..\\x", "a\\..\\b", "..\\/x", "../x", "a/..\\x", "a\\..",
62
+ "\\..\\x", "\\dir\\..\\x", "dir\\..\\..\\x"
63
+ ].each do |name|
64
+ assert_raises(Minitar::SecureRelativePathError, name) {
65
+ check(normalize(name, "\\"))
66
+ }
67
+ end
68
+ end
69
+
70
+ def test_backslash_separator_rejects_dot_only_components
71
+ [
72
+ "...\\x", "....\\x", "a\\...\\b", "a\\...", ".. \\x", ".. .\\x", "a\\.. "
73
+ ].each do |name|
74
+ assert_raises(Minitar::UnsafeFileNameError, name) { normalize(name, "\\") }
75
+ end
76
+ end
77
+
78
+ def test_backslash_separator_leaves_dot_and_dotdot_components_to_the_path_check
79
+ assert_equal "a/./b", normalize("a\\.\\b", "\\")
80
+ assert_equal "a/../b", normalize("a\\..\\b", "\\")
81
+ end
82
+
83
+ def test_backslash_separator_preserves_interior_dots
84
+ [
85
+ "a..b..c.txt", "a..b\\c.txt", "..a\\c.txt"
86
+ ].each do |name|
87
+ assert_equal name.tr("\\", "/"), normalize(name, "\\")
88
+ end
89
+ end
90
+
91
+ def test_backslash_separator_rejects_trailing_dots_and_spaces
92
+ [
93
+ "dirname..\\x", "dir.\\x", "dir \\x", "a\\file.", "a\\file ", "foo...\\x"
94
+ ].each do |name|
95
+ assert_raises(Minitar::UnsafeFileNameError, name) { normalize(name, "\\") }
96
+ end
97
+ end
98
+
99
+ def test_backslash_separator_accepts_ordinary_dots
100
+ assert_equal "a/./b", normalize("a\\.\\b", "\\")
101
+ assert_equal "dir..name/.hidden/a.b.txt/", normalize("dir..name\\.hidden\\a.b.txt\\", "\\")
102
+ end
103
+
104
+ def test_backslash_separator_converts_and_squeezes
105
+ assert_equal "a/b", normalize("a\\b", "\\")
106
+ assert_equal "a/b/c/d", normalize("a\\\\b//c\\/d", "\\")
107
+ end
108
+
109
+ def test_backslash_separator_rejects_drive_names
110
+ [
111
+ "C:\\x", "C:x", "C:/x", "c:\\x", "C:", "a\\C:\\x"
112
+ ].each do |name|
113
+ assert_raises(Minitar::UnsafeFileNameError, name) { normalize(name, "\\") }
114
+ end
115
+ end
116
+
117
+ def test_backslash_separator_turns_unc_names_into_relative_paths
118
+ {
119
+ "\\\\host\\share\\x" => "/host/share/x",
120
+ "//host/share/x" => "/host/share/x",
121
+ "\\/host/share" => "/host/share",
122
+ "\\\\host" => "/host"
123
+ }.each do |name, expected|
124
+ assert_equal expected, normalize(name, "\\")
125
+ check(expected)
126
+ end
127
+ end
128
+
129
+ def test_backslash_separator_rejects_alternate_data_streams
130
+ [
131
+ "a.txt:stream", "dir/a.txt:stream:$DATA", "dir:x/a", "a\\b:s"
132
+ ].each do |name|
133
+ assert_raises(Minitar::UnsafeFileNameError, name) { normalize(name, "\\") }
134
+ end
135
+ end
136
+
137
+ def test_unsafe_file_name_error_is_a_minitar_error
138
+ assert_operator Minitar::UnsafeFileNameError, :<, Minitar::Error
139
+ end
140
+
141
+ # --- end-to-end on POSIX: guards against over-normalizing ---
142
+
143
+ def test_posix_extraction_keeps_backslash_names_literal
144
+ skip "POSIX only" if Minitar.windows_separator?
145
+
146
+ names = ["..\\x", "a\\..\\..\\x", "a\\b", "dirname../x"]
147
+ Dir.mktmpdir do |root|
148
+ dest = File.join(root, "dest")
149
+ Minitar.unpack(tar_with(names), dest)
150
+
151
+ assert_equal names.map { _1.sub(%r{/.*}, "") }.uniq.sort, Dir.children(dest).sort
152
+ assert_path_exists File.join(dest, "..\\x")
153
+ assert_path_exists File.join(dest, "dirname..", "x")
154
+ assert_equal ["dest"], Dir.children(root)
155
+ end
156
+ end
157
+
158
+ private
159
+
160
+ def tar_with(names)
161
+ io = StringIO.new(+"".b)
162
+ Minitar::Writer.open(io) do |w|
163
+ names.each do |name|
164
+ w.add_file_simple(name, mode: 0o644, size: 1) { _1.write("x") }
165
+ end
166
+ end
167
+ io.rewind
168
+ io
169
+ end
170
+ end
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "minitest_helper"
4
+
5
+ # End-to-end extraction tests that depend on real Windows path semantics. These are
6
+ # skipped everywhere else.
7
+ class TestWindowsExtraction < Minitest::Test
8
+ def setup
9
+ skip "Windows only" unless Minitar.windows_separator?
10
+ end
11
+
12
+ TRAVERSAL_NAMES = [
13
+ "..\\escaped.txt", "a\\..\\..\\escaped.txt", "..\\/escaped.txt", "../escaped.txt"
14
+ ].freeze
15
+
16
+ def test_traversal_names_are_rejected_and_nothing_escapes
17
+ TRAVERSAL_NAMES.each do |name|
18
+ with_dest do |root, dest|
19
+ assert_raises(Minitar::SecureRelativePathError, name) {
20
+ Minitar.unpack(tar_with(name), dest)
21
+ }
22
+ assert_equal [
23
+ "dest"
24
+ ], Dir.children(root), name
25
+ end
26
+ end
27
+ end
28
+
29
+ def test_drive_names_are_rejected
30
+ [
31
+ "C:\\escaped.txt", "C:escaped.txt", "C:/escaped.txt"
32
+ ].each do |name|
33
+ with_dest do |root, dest|
34
+ assert_raises(Minitar::UnsafeFileNameError, name) {
35
+ Minitar.unpack(tar_with(name), dest)
36
+ }
37
+ assert_equal ["dest"], Dir.children(root), name
38
+ assert_empty Dir.children(dest)
39
+ end
40
+ end
41
+ end
42
+
43
+ def test_unc_names_extract_as_relative_paths_inside_destdir
44
+ [
45
+ "\\\\host\\share\\c.txt", "//host/share/c.txt"
46
+ ].each do |name|
47
+ with_dest do |root, dest|
48
+ Minitar.unpack(tar_with(name), dest)
49
+
50
+ assert_equal ["dest"], Dir.children(root), name
51
+ assert_path_exists File.join(dest, "host", "share", "c.txt")
52
+ end
53
+ end
54
+ end
55
+
56
+ def test_alternate_data_streams_are_rejected
57
+ with_dest do |root, dest|
58
+ assert_raises(Minitar::UnsafeFileNameError) {
59
+ Minitar.unpack(tar_with("a.txt:stream"), dest)
60
+ }
61
+ assert_empty Dir.children(dest)
62
+ end
63
+ end
64
+
65
+ def test_backslash_name_extracts_as_nested_path
66
+ with_dest do |_, dest|
67
+ Minitar.unpack(tar_with("a\\b\\c.txt"), dest)
68
+
69
+ assert_equal "x", File.read(File.join(dest, "a", "b", "c.txt"))
70
+ end
71
+ end
72
+
73
+ def test_dots_within_a_component_are_accepted
74
+ with_dest do |_, dest|
75
+ Minitar.unpack(tar_with("dir..name\\c.txt"), dest)
76
+
77
+ assert_path_exists File.join(dest, "dir..name", "c.txt")
78
+ end
79
+ end
80
+
81
+ def test_dot_only_and_trailing_dot_components_are_rejected
82
+ [
83
+ "...\\escaped.txt", "a\\...\\escaped.txt", ".. \\escaped.txt"
84
+ ].each do |name|
85
+ with_dest do |root, dest|
86
+ assert_raises(Minitar::UnsafeFileNameError, name) {
87
+ Minitar.unpack(tar_with(name), dest)
88
+ }
89
+ assert_equal [
90
+ "dest"
91
+ ], Dir.children(root), name
92
+ end
93
+ end
94
+
95
+ with_dest do |_, dest|
96
+ assert_raises(Minitar::UnsafeFileNameError) {
97
+ Minitar.unpack(tar_with("dirname..\\c.txt"), dest)
98
+ }
99
+ assert_empty Dir.children(dest)
100
+ end
101
+ end
102
+
103
+ private
104
+
105
+ def with_dest
106
+ Dir.mktmpdir do |root|
107
+ dest = File.join(root, "dest")
108
+ Dir.mkdir(dest)
109
+ yield root, dest
110
+ end
111
+ end
112
+
113
+ def tar_with(name)
114
+ io = StringIO.new(+"".b)
115
+ Minitar::Writer.open(io) do |w|
116
+ w.add_file_simple(name, mode: 0o644, size: 1) { _1.write("x") }
117
+ end
118
+ io.rewind
119
+ io
120
+ end
121
+ end