summaryrefslogtreecommitdiff
path: root/jni/ruby/test/rubygems/test_gem_package_tar_writer.rb
blob: f087df3fcf54645df888e4d3e19ffd39bc0a1d0e (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
require 'rubygems/package/tar_test_case'
require 'rubygems/package/tar_writer'
require 'minitest/mock'

class TestGemPackageTarWriter < Gem::Package::TarTestCase

  def setup
    super

    @data = 'abcde12345'
    @io = TempIO.new
    @tar_writer = Gem::Package::TarWriter.new @io
  end

  def teardown
    @tar_writer.close unless @tar_writer.closed?
    @io.close!

    super
  end

  def test_add_file
    Time.stub :now, Time.at(1458518157) do
      @tar_writer.add_file 'x', 0644 do |f| f.write 'a' * 10 end

      assert_headers_equal(tar_file_header('x', '', 0644, 10, Time.now),
                         @io.string[0, 512])
    end
    assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
    assert_equal 1024, @io.pos
  end

  def test_add_file_digest
    digest_algorithms = Digest::SHA1, Digest::SHA512

    Time.stub :now, Time.at(1458518157) do
      digests = @tar_writer.add_file_digest 'x', 0644, digest_algorithms do |io|
        io.write 'a' * 10
      end

      assert_equal '3495ff69d34671d1e15b33a63c1379fdedd3a32a',
                   digests['SHA1'].hexdigest
      assert_equal '4714870aff6c97ca09d135834fdb58a6389a50c1' \
                   '1fef8ec4afef466fb60a23ac6b7a9c92658f14df' \
                   '4993d6b40a4e4d8424196afc347e97640d68de61' \
                   'e1cf14b0',
                   digests['SHA512'].hexdigest

      assert_headers_equal(tar_file_header('x', '', 0644, 10, Time.now),
                         @io.string[0, 512])
    end
    assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
    assert_equal 1024, @io.pos
  end

  def test_add_file_digest_multiple
    digest_algorithms = [Digest::SHA1, Digest::SHA512]

    Time.stub :now, Time.at(1458518157) do
      digests = @tar_writer.add_file_digest 'x', 0644, digest_algorithms do |io|
        io.write 'a' * 10
      end

      assert_equal '3495ff69d34671d1e15b33a63c1379fdedd3a32a',
                   digests['SHA1'].hexdigest
      assert_equal '4714870aff6c97ca09d135834fdb58a6389a50c1' \
                   '1fef8ec4afef466fb60a23ac6b7a9c92658f14df' \
                   '4993d6b40a4e4d8424196afc347e97640d68de61' \
                   'e1cf14b0',
                   digests['SHA512'].hexdigest

      assert_headers_equal(tar_file_header('x', '', 0644, 10, Time.now),
                           @io.string[0, 512])
    end
    assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
    assert_equal 1024, @io.pos
  end

  def test_add_file_signer
    skip 'openssl is missing' unless defined?(OpenSSL::SSL)

    signer = Gem::Security::Signer.new PRIVATE_KEY, [PUBLIC_CERT]

    Time.stub :now, Time.at(1458518157) do
      @tar_writer.add_file_signed 'x', 0644, signer do |io|
        io.write 'a' * 10
      end

      assert_headers_equal(tar_file_header('x', '', 0644, 10, Time.now),
                           @io.string[0, 512])


      assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]

      digest = signer.digest_algorithm.new
      digest.update 'a' * 10

      signature = signer.sign digest.digest

      assert_headers_equal(tar_file_header('x.sig', '', 0444, signature.length,
                                           Time.now),
                           @io.string[1024, 512])
      assert_equal "#{signature}#{"\0" * (512 - signature.length)}",
                   @io.string[1536, 512]

      assert_equal 2048, @io.pos
    end

  end

  def test_add_file_signer_empty
    signer = Gem::Security::Signer.new nil, nil

    Time.stub :now, Time.at(1458518157) do

      @tar_writer.add_file_signed 'x', 0644, signer do |io|
        io.write 'a' * 10
      end

      assert_headers_equal(tar_file_header('x', '', 0644, 10, Time.now),
                         @io.string[0, 512])
    end
    assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]

    assert_equal 1024, @io.pos
  end

  def test_add_file_simple
    Time.stub :now, Time.at(1458518157) do
      @tar_writer.add_file_simple 'x', 0644, 10 do |io| io.write "a" * 10 end

      assert_headers_equal(tar_file_header('x', '', 0644, 10, Time.now),
                         @io.string[0, 512])
    end

    assert_equal "aaaaaaaaaa#{"\0" * 502}", @io.string[512, 512]
    assert_equal 1024, @io.pos
  end

  def test_add_file_simple_padding
    Time.stub :now, Time.at(1458518157) do
      @tar_writer.add_file_simple 'x', 0, 100

      assert_headers_equal tar_file_header('x', '', 0, 100, Time.now),
                         @io.string[0, 512]
    end

    assert_equal "\0" * 512, @io.string[512, 512]
  end

  def test_add_file_simple_data
    @tar_writer.add_file_simple("lib/foo/bar", 0, 10) { |f| f.write @data }
    @tar_writer.flush

    assert_equal @data + ("\0" * (512-@data.size)),
                 @io.string[512, 512]
  end

  def test_add_file_simple_size
    assert_raises Gem::Package::TarWriter::FileOverflow do
      @tar_writer.add_file_simple("lib/foo/bar", 0, 10) do |io|
        io.write "1" * 11
      end
    end
  end

  def test_add_file_unseekable
    assert_raises Gem::Package::NonSeekableIO do
      Gem::Package::TarWriter.new(Object.new).add_file 'x', 0
    end
  end

  def test_close
    @tar_writer.close

    assert_equal "\0" * 1024, @io.string

    e = assert_raises IOError do
      @tar_writer.close
    end
    assert_equal 'closed Gem::Package::TarWriter', e.message

    e = assert_raises IOError do
      @tar_writer.flush
    end
    assert_equal 'closed Gem::Package::TarWriter', e.message

    e = assert_raises IOError do
      @tar_writer.add_file 'x', 0
    end
    assert_equal 'closed Gem::Package::TarWriter', e.message

    e = assert_raises IOError do
      @tar_writer.add_file_simple 'x', 0, 0
    end
    assert_equal 'closed Gem::Package::TarWriter', e.message

    e = assert_raises IOError do
      @tar_writer.mkdir 'x', 0
    end
    assert_equal 'closed Gem::Package::TarWriter', e.message
  end

  def test_mkdir
    Time.stub :now, Time.at(1458518157) do
      @tar_writer.mkdir 'foo', 0644

      assert_headers_equal tar_dir_header('foo', '', 0644, Time.now),
                           @io.string[0, 512]

      assert_equal 512, @io.pos
    end
  end

  def test_split_name
    assert_equal ['b' * 100, 'a' * 155],
                 @tar_writer.split_name("#{'a' * 155}/#{'b' * 100}")

    assert_equal ["#{'qwer/' * 19}bla", 'a' * 151],
                 @tar_writer.split_name("#{'a' * 151}/#{'qwer/' * 19}bla")
  end

  def test_split_name_too_long_name
    name = File.join 'a', 'b' * 100
    assert_equal ['b' * 100, 'a'], @tar_writer.split_name(name)

    name = File.join 'a', 'b' * 101
    exception = assert_raises Gem::Package::TooLongFileName do
      @tar_writer.split_name name
    end
    assert_includes exception.message, name
  end

  def test_split_name_too_long_prefix
    name = File.join 'a' * 155, 'b'
    assert_equal ['b', 'a' * 155], @tar_writer.split_name(name)

    name = File.join 'a' * 156, 'b'
    exception = assert_raises Gem::Package::TooLongFileName do
      @tar_writer.split_name name
    end
    assert_includes exception.message, name
  end

  def test_split_name_too_long_total
    name = 'a' * 257
    exception = assert_raises Gem::Package::TooLongFileName do
      @tar_writer.split_name name
    end
    assert_includes exception.message, name
  end

end