cmogstored.git  about / heads / tags
alternative mogstored implementation for MogileFS
blob 34e2cdb3dc620dd934126ad0b67469e3f8460c5c 10371 bytes (raw)
$ git show HEAD:test/http_put.rb	# shows this blob on the CLI

  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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
 
#!/usr/bin/env ruby
# -*- encoding: binary -*-
# Copyright (C) 2012-2020 all contributors <cmogstored-public@yhbt.net>
# License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
require 'test/test_helper'
require 'digest/md5'
require 'net/http'
require 'stringio'

class TestHTTPPut < Test::Unit::TestCase
  def setup
    @tmpdir = Dir.mktmpdir('cmogstored-httpput-test')
    Dir.mkdir("#@tmpdir/dev666")
    @to_close = []
    @host = TEST_HOST
    srv = TCPServer.new(@host, 0)
    @port = srv.addr[1]
    srv.close
    @err = Tempfile.new("stderr")
    cmd = [ "cmogstored", "--docroot=#@tmpdir", "--httplisten=#@host:#@port",
            "--maxconns=500" ]
    vg = ENV["VALGRIND"] and cmd = vg.split(/\s+/).concat(cmd)
    @pid = fork {
      $stderr.reopen(@err)
      @err.close
      exec(*cmd)
    }
    @client = get_client
  end

  def teardown
    Process.kill(:QUIT, @pid) rescue nil
    _, status = Process.waitpid2(@pid)
    @to_close.each { |io| io.close unless io.closed? }
    FileUtils.rm_rf(@tmpdir)
    @err.rewind
    #$stderr.write(@err.read)
    assert status.success?, status.inspect
  end

  def bad_put(path)
    Net::HTTP.start(@host, @port) do |http|
      put = Net::HTTP::Put.new(path)
      put.content_type = "application/octet-stream"
      body = StringIO.new("BODY!")
      put.body_stream = body
      put.content_length = body.size
      return http.request(put)
    end
  end

  def test_put_does_not_create_shallow_dev_dirs
    assert ! File.exist?("#@tmpdir/dev777")
    assert_equal 404, bad_put("/dev777/foo").code.to_i
    assert_equal 400, bad_put("/dev777/foo/").code.to_i
    assert_equal 400, bad_put("/foo").code.to_i
    assert ! File.exist?("#@tmpdir/foo")
    assert ! File.exist?("#@tmpdir/foo/")
    assert ! File.exist?("#@tmpdir/dev777")
  end

  def test_put
    Net::HTTP.start(@host, @port) do |http|
      4.times do |i|
        put = Net::HTTP::Put.new("/dev666/#{i}/foo#{i}")
        body = StringIO.new("BODY!")
        put.content_type = "application/octet-stream"
        put.body_stream = body
        put.content_length = body.size
        resp = http.request(put)
        assert_equal 201, resp.code.to_i
        assert_equal "BODY!", IO.read("#@tmpdir/dev666/#{i}/foo#{i}")
      end
    end
  end

  def test_single_write
    req = "PUT /dev666/zz HTTP/1.1\r\n" \
          "Host: #@host:#@port\r\n" \
          "Content-Length: 5\r\n" \
          "\r\n" \
          "abcde"
    @client.write(req)
    expect = "HTTP/1.1 201 Created\r\n" \
      "Date: #{EPOCH}\r\n" \
      "Content-Length: 0\r\n" \
      "Content-Type: text/plain\r\n" \
      "Connection: keep-alive\r\n\r\n"
    resp = @client.readpartial(666)
    replace_dates!(resp)
    assert_equal expect, resp
    assert_equal "abcde", IO.read("#@tmpdir/dev666/zz")
  end

  def test_pipelined
    req = 2.times.map do |i|
      "PUT /dev666/#{i} HTTP/1.1\r\n" \
      "Host: #@host:#@port\r\n" \
      "Content-Length: 5\r\n" \
      "\r\n" \
      "abcd#{i}"
    end.join
    @client.write(req)
    expect = "HTTP/1.1 201 Created\r\n" \
      "Date: #{EPOCH}\r\n" \
      "Content-Length: 0\r\n" \
      "Content-Type: text/plain\r\n" \
      "Connection: keep-alive\r\n\r\n"
    resp = @client.read(expect.size * 2)
    replace_dates!(resp)
    assert_equal(expect * 2, resp)
    assert_equal "abcd0", IO.read("#@tmpdir/dev666/0")
    assert_equal "abcd1", IO.read("#@tmpdir/dev666/1")
    assert_nil IO.select([@client], nil, nil, 0.1)
  end

  def test_range_put
    req = "PUT /dev666/foo HTTP/1.0\r\n" \
          "Content-Length: 1\r\n" \
          "Content-Range: bytes 5-5/*\r\n" \
          "\r\na"
    @client.write(req)
    resp = @client.readpartial(666)
    assert_match(%r{\AHTTP/1\.1 201 Created\r\n}, resp)
    File.open("#@tmpdir/dev666/foo") do |fp|
      assert_equal("\0\0\0\0\0a", fp.read(fp.stat.size))
    end
  end

  def test_range_put_bad_content_length
    req = "PUT /dev666/foo HTTP/1.0\r\n" \
          "Content-Length: 2\r\n" \
          "Content-Range: bytes 5-5/*\r\n" \
          "\r\na"
    @client.write(req)
    resp = @client.read
    assert_match(%r{\AHTTP/1\.1 400 Bad Request\r\n}, resp)
    assert( ! File.exist?("#@tmpdir/dev666/foo") )
  end

  def test_range_put_bogus_range
    req = "PUT /dev666/foo HTTP/1.0\r\n" \
          "Content-Length: 2\r\n" \
          "Content-Range: bytes 5-1/*\r\n" \
          "\r\na"
    @client.write(req)
    resp = @client.read
    assert_match(%r{\AHTTP/1\.1 400 Bad Request\r\n}, resp)
    assert( ! File.exist?("#@tmpdir/dev666/foo") )
  end

  def test_put_content_len_overflow
    max = 0xffffffff << 64
    req = "PUT /dev666/foo HTTP/1.0\r\n" \
          "Content-Length: #{max}\r\n" \
          "\r\n"
    @client.write(req)
    resp = @client.read
    assert_match(%r{\AHTTP/1\.1 507 Insufficient Storage\r\n}, resp)
    assert( ! File.exist?("#@tmpdir/dev666/foo") )
  end

  def test_put_bogus
    max = 0xffffffff << 64
    req = "PUT /dev666/foo HTTP/1.1\r\n" \
          "Transfer-Encoding: bogus\r\n" \
          "\r\n"
    @client.write(req)
    resp = @client.read
    assert_match(%r{\AHTTP/1\.1 400 Bad Request\r\n}, resp)
    assert( ! File.exist?("#@tmpdir/dev666/foo") )
  end

  def test_put_range_beg_overflow
    max = 0xffffffff << 64
    req = "PUT /dev666/foo HTTP/1.1\r\n" \
          "Transfer-Encoding: chunked\r\n" \
          "Content-Range: bytes #{max}-#{max + 1}/*\r\n" \
          "\r\n"
    @client.write(req)
    resp = @client.read
    assert_match(%r{\AHTTP/1\.1 507 Insufficient Storage\r\n}, resp)
    assert( ! File.exist?("#@tmpdir/dev666/foo") )
  end

  def test_put_range_end_overflow
    max = 0xffffffff << 64
    req = "PUT /dev666/foo HTTP/1.1\r\n" \
          "Transfer-Encoding: chunked\r\n" \
          "Content-Range: bytes 0-#{max}/*\r\n" \
          "\r\n"
    @client.write(req)
    resp = @client.read
    assert_match(%r{\AHTTP/1\.1 507 Insufficient Storage\r\n}, resp)
    assert( ! File.exist?("#@tmpdir/dev666/foo") )
  end

  def test_put_range_real
    expect = ""
    Net::HTTP.start(@host, @port) do |http|
      beg = 0
      4.times do |i|
        put = Net::HTTP::Put.new("/dev666/foo")
        body = StringIO.new("BODY#{i}\n")
        put.content_type = "application/octet-stream"
        put.body_stream = body
        put["Content-Range"] = "bytes #{beg}-#{beg+body.size-1}/*"
        put.content_length = body.size
        resp = http.request(put)
        assert_equal 201, resp.code.to_i
        expect << body.string
        beg += body.size
      end
    end

    assert_equal expect, IO.read("#@tmpdir/dev666/foo")
  end

  def test_put_bad_content_length
    req = "PUT /dev666/foo HTTP/1.1\r\n" \
          "Content-Length: 123a\r\n" \
          "\r\n"
    @client.write(req)
    resp = @client.read
    assert_match(%r{\AHTTP/1\.1 400 Bad Request\r\n}, resp)
    assert( ! File.exist?("#@tmpdir/dev666/foo") )
  end

  def test_put_favors_chunked
    order = [ "Transfer-Encoding: chunked", "Content-Length: 123" ]
    %w[a b].each do
      path = '/dev666/a'
      req = "PUT #{path} HTTP/1.1\r\n" \
            "#{order.join("\r\n")}\r\n\r\n" \
            "a\r\nhelloworld\r\n0\r\n\r\n"
      order.reverse!
      @client.write(req)
      resp = @client.readpartial(4096)
      assert_match(%r{\AHTTP/1\.1 201 Created\r\n}, resp, "#{path} created")
      assert(File.exist?("#@tmpdir#{path}"))
      assert_equal('helloworld', File.read("#@tmpdir#{path}"))
    end
  end

  def test_content_md5_good
    req = "PUT /dev666/foo HTTP/1.1\r\n" \
          "Host: #@host:#@port\r\n" \
          "Content-Length: 5\r\n" \
          "Content-MD5: q1a02StAcTrMWviZhdS3hg==\r\n" \
          "\r\n" \
          "abcde"
    @client.write(req)
    line = @client.gets
    assert_match(%r{\AHTTP/1\.1 201 Created}, line)
  end

  def test_content_md5_bad
    req = "PUT /dev666/foo HTTP/1.1\r\n" \
          "Host: #@host:#@port\r\n" \
          "Content-Length: 5\r\n" \
          "Content-MD5: q1a02StAcTrMWviZhdS3hg==\r\n" \
          "\r\n" \
          "abcd!"
    @client.write(req)
    line = @client.gets
    assert_match(%r{\AHTTP/1\.1 400 Bad Request}, line)
    lines = []
    lines << @client.gets until lines[-1] == "\r\n"
    assert_equal 1, lines.grep(/\AConnection:\s*keep-alive/).size
  end

  def test_put_get_absolute_url
    url = "http://#@host:#@port/dev666/foo"
    req = "PUT #{url} HTTP/1.1\r\n" \
          "Host: #@host:#@port\r\n" \
          "Content-Length: 5\r\n" \
          "\r\n" \
          "abcde"
    @client.write(req)
    line = @client.gets
    assert_match(%r{\AHTTP/1\.1 201}, line)

    assert_equal 'abcde', File.read("#@tmpdir/dev666/foo")

    begin
      @client.read_nonblock(1000)
    rescue Errno::EAGAIN
      break
    end while true

    req = "GET #{url} HTTP/1.1\r\n" \
          "Host: #@host:#@port\r\n" \
          "Content-Length: 5\r\n" \
          "\r\n" \
          "abcde"
    @client.write(req)
    line = @client.gets
    assert_match(%r{\AHTTP/1\.1 200}, line)
  end

  def test_put_premature_eof
    path = "/dev666/foo"
    url = "http://#@host:#@port#{path}"
    req = "PUT #{url} HTTP/1.1\r\n" \
          "Host: #@host:#@port\r\n" \
          "Content-Length: 666\r\n" \
          "\r\n" \
          "abcde"
    @client.write(req)
    @client.shutdown(Socket::SHUT_WR)
    addr = "#{@client.addr[3]}:#{@client.addr[1]}"
    assert_nil @client.read(1)
    assert ! File.exist?("#@tmpdir#{path}")
    buf = File.read(@err.path)
    assert_match(%r{PUT #{path} failed from #{addr} after 5 bytes:}, buf)
    if RUBY_PLATFORM =~ /linux/
      assert_match(%r{last_data_recv=\d+ms from #{addr} for PUT #{path}}, buf)
    end
  end

  def test_put_premature_eof_chunked
    path = "/dev666/foo"
    url = "http://#@host:#@port#{path}"
    req = "PUT #{url} HTTP/1.1\r\n" \
          "Host: #@host:#@port\r\n" \
          "Transfer-Encoding: chunked\r\n" \
          "\r\n" \
          "666\r\nf"
    @client.write(req)
    @client.shutdown(Socket::SHUT_WR)
    addr = "#{@client.addr[3]}:#{@client.addr[1]}"
    assert_nil @client.read(1)
    assert ! File.exist?("#@tmpdir#{path}")
    buf = File.read(@err.path)
    assert_match(%r{PUT #{path} failed from #{addr} after 1 bytes:}, buf)
    if RUBY_PLATFORM =~ /linux/
      assert_match(%r{last_data_recv=\d+ms from #{addr} for PUT #{path}}, buf)
    end
  end
end

git clone https://yhbt.net/cmogstored.git