diff --git a/.travis.yml b/.travis.yml index 63f94a6..c1c66cb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,10 @@ language: ruby cache: bundler rvm: -- 1.9.3 -- 2.0 +# - 1.9.3 +# - 2.0 - 2.1 -- 2.2 +# - 2.2 env: global: - secure: cC3aeKKrNLGuK60zEPKNPmRlE3m/VlzQLAXjD8nYjhbPVL+zjaylY87vq657gSFmDDGY3ISF0aR3rjFpSHeoVr6CiVZDnQIgoaC2FD1oDxkzy6/8tEQBJ0YORLzfCO63NJTKltVMvSsPWck4xE+qIsQLIpDjia9fHc2InLtkn9pTnuhMpHxjBIat6TgFPSiIC9YXXjCveudTy2o+9Is2nCmkdsr7lqEkifLIyXmGzyUp99hmQy0OZ2lF06OMH/RwWSDRBZryqMT+ju1qsv+/LkDRFwaqKfxqDdewhxj6eZdA7Q1YyB7ChT+uFRNaHkrWwPCC+JcZnHq8kt8qykUfN+Saq/txZ65qpQFp0rDUG929iaUUnfreearniqe9+u+7LnrFeseVLa6dJD+fcsTcCQRgfUvuaMARLq3EwqUCvAXPWHFTnY6ynqerTEwlJbxpCXtYktXaO0KugOI+iF4APs/r2iroAM/zkf3AjH78UcSAIBn1AzxQy7+Ah6G+nS69/nvbtPGiiSAmJYgKfBcD1ctDTJXzen78MGz7ImTC64k+2J4kuu8CSi/j8wbdIRHrIIszBUEPKQrGp9RDx08MRffinhnInq1uGEBFvip2Q1qk3tuGXJT+2m/OgbK7KlVmlzRz9M+NGav6ixQpW4qPvu2mf2bdmMeAd/0sg4IIIRc= diff --git a/Rakefile b/Rakefile index 6307b04..73bcdbb 100755 --- a/Rakefile +++ b/Rakefile @@ -52,17 +52,21 @@ task :smart_test do if ENV.keys.include?('RUBY_SDK_OSS_KEY') begin - env_crc_enable = ENV['RUBY_SDK_OSS_CRC_ENABLE'] + env_upload_crc_enable = ENV['RUBY_SDK_OSS_UPLOAD_CRC_ENABLE'] + env_download_crc_enable = ENV['RUBY_SDK_OSS_DOWNLOAD_CRC_ENABLE'] - # run test without crc - ENV['RUBY_SDK_OSS_CRC_ENABLE'] = nil if ENV['RUBY_SDK_OSS_CRC_ENABLE'] + # run test with crc + ENV['RUBY_SDK_OSS_UPLOAD_CRC_ENABLE'] = 'true' + ENV['RUBY_SDK_OSS_DOWNLOAD_CRC_ENABLE'] = 'true' Rake::Task[:test].invoke - # run test with crc - ENV['RUBY_SDK_OSS_CRC_ENABLE'] = 'true' + # run test without crc + ENV['RUBY_SDK_OSS_UPLOAD_CRC_ENABLE'] = 'false' + ENV['RUBY_SDK_OSS_DOWNLOAD_CRC_ENABLE'] = 'false' Rake::Task[:test].invoke ensure - ENV['RUBY_SDK_OSS_CRC_ENABLE'] = env_crc_enable + ENV['RUBY_SDK_OSS_UPLOAD_CRC_ENABLE'] = env_upload_crc_enable + ENV['RUBY_SDK_OSS_DOWNLOAD_CRC_ENABLE'] = env_download_crc_enable end end end diff --git a/lib/aliyun/oss/bucket.rb b/lib/aliyun/oss/bucket.rb index 6d64797..2b775e1 100644 --- a/lib/aliyun/oss/bucket.rb +++ b/lib/aliyun/oss/bucket.rb @@ -639,10 +639,16 @@ def sign(string_to_sign) @protocol.sign(string_to_sign) end - # Get the crc status - # @return true(crc enable) or false(crc disable) - def crc_enable - @protocol.crc_enable + # Get the download crc status + # @return true(download crc enable) or false(download crc disable) + def download_crc_enable + @protocol.download_crc_enable + end + + # Get the upload crc status + # @return true(upload crc enable) or false(upload crc disable) + def upload_crc_enable + @protocol.upload_crc_enable end private diff --git a/lib/aliyun/oss/client.rb b/lib/aliyun/oss/client.rb index 813c114..59aaf26 100644 --- a/lib/aliyun/oss/client.rb +++ b/lib/aliyun/oss/client.rb @@ -28,6 +28,10 @@ class Client # KEY SECRET,如果不填则会尝试匿名访问 # @option opts [Boolean] :cname [可选] 指定endpoint是否是用户绑 # 定的域名 + # @option opts [Boolean] :upload_crc_enable [可选]指定上传处理 + # 是否开启CRC校验,默认为开启(true) + # @option opts [Boolean] :download_crc_enable [可选]指定下载处理 + # 是否开启CRC校验,默认为不开启(false) # @option opts [String] :sts_token [可选] 指定STS的 # SecurityToken,如果指定,则使用STS授权访问 # @option opts [Fixnum] :open_timeout [可选] 指定建立连接的超时 diff --git a/lib/aliyun/oss/config.rb b/lib/aliyun/oss/config.rb index 590ff4e..eb4cb54 100644 --- a/lib/aliyun/oss/config.rb +++ b/lib/aliyun/oss/config.rb @@ -12,7 +12,7 @@ class Config < Common::Struct::Base attrs :endpoint, :cname, :sts_token, :access_key_id, :access_key_secret, :open_timeout, :read_timeout, - :crc_enable + :download_crc_enable, :upload_crc_enable def initialize(opts = {}) super(opts) @@ -20,7 +20,8 @@ def initialize(opts = {}) @access_key_id = @access_key_id.strip if @access_key_id @access_key_secret = @access_key_secret.strip if @access_key_secret normalize_endpoint if endpoint - @crc_enable ||= false + @upload_crc_enable = (@upload_crc_enable == 'false' || @upload_crc_enable == false) ? false : true + @download_crc_enable = (@download_crc_enable == 'true' || @download_crc_enable == true) ? true : false end private diff --git a/lib/aliyun/oss/http.rb b/lib/aliyun/oss/http.rb index d33f2f8..c845945 100644 --- a/lib/aliyun/oss/http.rb +++ b/lib/aliyun/oss/http.rb @@ -169,6 +169,7 @@ def handle_response(r, &block) r.read_body else # streaming read body on success + p "debug: handle_response x_oss_request_id=#{r['x_oss_request_id']} content-encoding=#{r['content-encoding']}" encoding = r['content-encoding'] if encoding == 'gzip' stream = StreamWriter.new { |s| r.read_body { |chunk| s << chunk } } diff --git a/lib/aliyun/oss/protocol.rb b/lib/aliyun/oss/protocol.rb index d47a857..7b95d34 100644 --- a/lib/aliyun/oss/protocol.rb +++ b/lib/aliyun/oss/protocol.rb @@ -535,7 +535,7 @@ def put_object(bucket_name, object_name, opts = {}, &block) headers[CALLBACK_HEADER] = opts[:callback].serialize end - payload = HTTP::StreamPayload.new(@config.crc_enable, opts[:init_crc], &block) + payload = HTTP::StreamPayload.new(@config.upload_crc_enable, opts[:init_crc], &block) r = @http.put( {:bucket => bucket_name, :object => object_name}, {:headers => headers, :body => payload}) @@ -546,7 +546,7 @@ def put_object(bucket_name, object_name, opts = {}, &block) raise e end - if @config.crc_enable + if @config.upload_crc_enable && !r.headers[:x_oss_hash_crc64ecma].nil? data_crc = payload.read.data_crc Aliyun::OSS::Util.crc_check(data_crc, r.headers[:x_oss_hash_crc64ecma], 'put') end @@ -593,13 +593,15 @@ def append_object(bucket_name, object_name, position, opts = {}, &block) headers.merge!(to_lower_case(opts[:headers])) if opts.key?(:headers) - payload = HTTP::StreamPayload.new(@config.crc_enable && !opts[:init_crc].nil?, opts[:init_crc], &block) + payload = HTTP::StreamPayload.new(@config.upload_crc_enable && !opts[:init_crc].nil?, opts[:init_crc], &block) r = @http.post( {:bucket => bucket_name, :object => object_name, :sub_res => sub_res}, {:headers => headers, :body => payload}) - if @config.crc_enable && !opts[:init_crc].nil? + if @config.upload_crc_enable && + !r.headers[:x_oss_hash_crc64ecma].nil? && + !opts[:init_crc].nil? data_crc = payload.read.data_crc Aliyun::OSS::Util.crc_check(data_crc, r.headers[:x_oss_hash_crc64ecma], 'append') end @@ -773,11 +775,22 @@ def get_object(bucket_name, object_name, opts = {}, &block) rewrites[:expires].httpdate if rewrites.key?(:expires) end + data_crc = opts[:init_crc].nil? ? 0 : opts[:init_crc] r = @http.get( {:bucket => bucket_name, :object => object_name, :sub_res => sub_res}, {:headers => headers} - ) { |chunk| yield chunk if block_given? } + ) do |chunk| + if block_given? + # crc enable and no range and oss server support crc + data_crc = Aliyun::OSS::Util.crc(chunk, data_crc) if @config.download_crc_enable && range.nil? + yield chunk + end + end + + if @config.download_crc_enable && range.nil? && !r.headers[:x_oss_hash_crc64ecma].nil? + Aliyun::OSS::Util.crc_check(data_crc, r.headers[:x_oss_hash_crc64ecma], 'get') + end h = r.headers metas = {} @@ -1097,12 +1110,12 @@ def upload_part(bucket_name, object_name, txn_id, part_no, &block) sub_res = {'partNumber' => part_no, 'uploadId' => txn_id} - payload = HTTP::StreamPayload.new(@config.crc_enable, &block) + payload = HTTP::StreamPayload.new(@config.upload_crc_enable, &block) r = @http.put( {:bucket => bucket_name, :object => object_name, :sub_res => sub_res}, {:body => payload}) - if @config.crc_enable + if @config.upload_crc_enable && !r.headers[:x_oss_hash_crc64ecma].nil? data_crc = payload.read.data_crc Aliyun::OSS::Util.crc_check(data_crc, r.headers[:x_oss_hash_crc64ecma], 'put') end @@ -1398,11 +1411,17 @@ def get_sts_token def sign(string_to_sign) Util.sign(@config.access_key_secret, string_to_sign) end - - # Get the crc status - # @return true(crc enable) or false(crc disable) - def crc_enable - @config.crc_enable + + # Get the download crc status + # @return true(download crc enable) or false(download crc disable) + def download_crc_enable + @config.download_crc_enable + end + + # Get the upload crc status + # @return true(upload crc enable) or false(upload crc disable) + def upload_crc_enable + @config.upload_crc_enable end private diff --git a/spec/aliyun/oss/bucket_spec.rb b/spec/aliyun/oss/bucket_spec.rb index 5cc7695..7e15d3b 100644 --- a/spec/aliyun/oss/bucket_spec.rb +++ b/spec/aliyun/oss/bucket_spec.rb @@ -591,6 +591,60 @@ def err(msg, reqid = '0000') end # acl, logging, cors, etc + context "crc" do + it "should download crc enable equal config setting" do + protocol = Protocol.new( + Config.new(:endpoint => @endpoint, + :access_key_id => 'xxx', :access_key_secret => 'yyy', + :download_crc_enable => 'true')) + expect(protocol.download_crc_enable).to eq(true) + + protocol = Protocol.new( + Config.new(:endpoint => @endpoint, + :access_key_id => 'xxx', :access_key_secret => 'yyy', + :download_crc_enable => true)) + expect(protocol.download_crc_enable).to eq(true) + + protocol = Protocol.new( + Config.new(:endpoint => @endpoint, + :access_key_id => 'xxx', :access_key_secret => 'yyy', + :download_crc_enable => 'false')) + expect(protocol.download_crc_enable).to eq(false) + + protocol = Protocol.new( + Config.new(:endpoint => @endpoint, + :access_key_id => 'xxx', :access_key_secret => 'yyy', + :download_crc_enable => false)) + expect(protocol.download_crc_enable).to eq(false) + end + + it "should upload crc enable equal config setting" do + protocol = Protocol.new( + Config.new(:endpoint => @endpoint, + :access_key_id => 'xxx', :access_key_secret => 'yyy', + :upload_crc_enable => 'true')) + expect(protocol.upload_crc_enable).to eq(true) + + protocol = Protocol.new( + Config.new(:endpoint => @endpoint, + :access_key_id => 'xxx', :access_key_secret => 'yyy', + :upload_crc_enable => true)) + expect(protocol.upload_crc_enable).to eq(true) + + protocol = Protocol.new( + Config.new(:endpoint => @endpoint, + :access_key_id => 'xxx', :access_key_secret => 'yyy', + :upload_crc_enable => 'false')) + expect(protocol.upload_crc_enable).to eq(false) + + protocol = Protocol.new( + Config.new(:endpoint => @endpoint, + :access_key_id => 'xxx', :access_key_secret => 'yyy', + :upload_crc_enable => false)) + expect(protocol.upload_crc_enable).to eq(false) + end + end # crc + end # Bucket end # OSS diff --git a/spec/aliyun/oss/client/bucket_spec.rb b/spec/aliyun/oss/client/bucket_spec.rb index e09a4a9..023ec3a 100644 --- a/spec/aliyun/oss/client/bucket_spec.rb +++ b/spec/aliyun/oss/client/bucket_spec.rb @@ -549,6 +549,72 @@ def err(msg, reqid = '0000') end end # multipart operations + context "crc" do + it "should download crc enable equal config setting" do + bucket = Client.new( + :endpoint => @endpoint, + :access_key_id => 'xxx', :access_key_secret => 'yyy', + :download_crc_enable => 'true').get_bucket(@bucket_name) + expect(bucket.download_crc_enable).to eq(true) + + bucket = Client.new( + :endpoint => @endpoint, + :access_key_id => 'xxx', :access_key_secret => 'yyy', + :download_crc_enable => true).get_bucket(@bucket_name) + expect(bucket.download_crc_enable).to eq(true) + + bucket = Client.new( + :endpoint => @endpoint, + :access_key_id => 'xxx', :access_key_secret => 'yyy', + :download_crc_enable => 'false').get_bucket(@bucket_name) + expect(bucket.download_crc_enable).to eq(false) + + bucket = Client.new( + :endpoint => @endpoint, + :access_key_id => 'xxx', :access_key_secret => 'yyy', + :download_crc_enable => false).get_bucket(@bucket_name) + expect(bucket.download_crc_enable).to eq(false) + + # check default value + bucket = Client.new( + :endpoint => @endpoint, + :access_key_id => 'xxx', :access_key_secret => 'yyy').get_bucket(@bucket_name) + expect(bucket.download_crc_enable).to eq(false) + end + + it "should upload crc enable equal config setting" do + bucket = Client.new( + :endpoint => @endpoint, + :access_key_id => 'xxx', :access_key_secret => 'yyy', + :upload_crc_enable => 'true').get_bucket(@bucket_name) + expect(bucket.upload_crc_enable).to eq(true) + + bucket = Client.new( + :endpoint => @endpoint, + :access_key_id => 'xxx', :access_key_secret => 'yyy', + :upload_crc_enable => true).get_bucket(@bucket_name) + expect(bucket.upload_crc_enable).to eq(true) + + bucket = Client.new( + :endpoint => @endpoint, + :access_key_id => 'xxx', :access_key_secret => 'yyy', + :upload_crc_enable => 'false').get_bucket(@bucket_name) + expect(bucket.upload_crc_enable).to eq(false) + + bucket = Client.new( + :endpoint => @endpoint, + :access_key_id => 'xxx', :access_key_secret => 'yyy', + :upload_crc_enable => false).get_bucket(@bucket_name) + expect(bucket.upload_crc_enable).to eq(false) + + # check default value + bucket = Client.new( + :endpoint => @endpoint, + :access_key_id => 'xxx', :access_key_secret => 'yyy').get_bucket(@bucket_name) + expect(bucket.upload_crc_enable).to eq(true) + end + end # crc + end # Bucket end # OSS end # Aliyun diff --git a/spec/aliyun/oss/multipart_spec.rb b/spec/aliyun/oss/multipart_spec.rb index 2290bfe..d08d9af 100644 --- a/spec/aliyun/oss/multipart_spec.rb +++ b/spec/aliyun/oss/multipart_spec.rb @@ -28,7 +28,8 @@ def crc_protocol Config.new(:endpoint => @endpoint, :access_key_id => 'xxx', :access_key_secret => 'yyy', - :crc_enable => true)) + :upload_crc_enable => true, + :download_crc_enable => true)) end def mock_txn_id(txn_id) diff --git a/spec/aliyun/oss/object_spec.rb b/spec/aliyun/oss/object_spec.rb index c4132cb..524a8e8 100644 --- a/spec/aliyun/oss/object_spec.rb +++ b/spec/aliyun/oss/object_spec.rb @@ -22,7 +22,8 @@ def crc_protocol Config.new(:endpoint => @endpoint, :access_key_id => 'xxx', :access_key_secret => 'yyy', - :crc_enable => true)) + :upload_crc_enable => true, + :download_crc_enable => true)) end def get_request_path(object = nil) @@ -205,8 +206,7 @@ def err(msg, reqid = '0000') content_crc = Aliyun::OSS::Util.crc(content) stub_request(:put, url).to_return( :status => 200, :headers => {:x_oss_hash_crc64ecma => content_crc.to_i + 1}) - - expect(crc_protocol.crc_enable).to eq(true) + expect(crc_protocol.upload_crc_enable).to eq(true) expect { crc_protocol.put_object(@bucket, object_name) do |c| c << content @@ -221,7 +221,7 @@ def err(msg, reqid = '0000') content_crc = Aliyun::OSS::Util.crc(content) stub_request(:put, url).to_return( :status => 200, :headers => {:x_oss_hash_crc64ecma => content_crc}) - expect(crc_protocol.crc_enable).to eq(true) + expect(crc_protocol.upload_crc_enable).to eq(true) expect { crc_protocol.put_object(@bucket, object_name) do |c| c << content @@ -339,7 +339,7 @@ def err(msg, reqid = '0000') return_headers = {'x-oss-next-append-position' => '101', :x_oss_hash_crc64ecma => content_crc.to_i + 1} stub_request(:post, url).with(:query => query) .to_return(:headers => return_headers) - expect(crc_protocol.crc_enable).to eq(true) + expect(crc_protocol.upload_crc_enable).to eq(true) expect { crc_protocol.append_object(@bucket, object_name, 11, :init_crc => 0) do |c| c << content @@ -358,7 +358,7 @@ def err(msg, reqid = '0000') stub_request(:post, url).with(:query => query) .to_return(:headers => return_headers) - expect(crc_protocol.crc_enable).to eq(true) + expect(crc_protocol.upload_crc_enable).to eq(true) next_pos = 0 expect { next_pos = crc_protocol.append_object(@bucket, object_name, 11, :init_crc => 0) do |c| @@ -382,7 +382,7 @@ def err(msg, reqid = '0000') stub_request(:post, url).with(:query => query) .to_return(:headers => return_headers) - expect(crc_protocol.crc_enable).to eq(true) + expect(crc_protocol.upload_crc_enable).to eq(true) next_pos = 0 expect { next_pos = crc_protocol.append_object(@bucket, object_name, 11) do |c| @@ -661,6 +661,55 @@ def err(msg, reqid = '0000') expect(WebMock).to have_requested(:get, url) .with(:body => nil, :query => query) end + + + it "should raise crc exception on error" do + object_name = 'ruby' + url = get_request_path(object_name) + content = "hello world" + content_crc = Aliyun::OSS::Util.crc(content) + stub_request(:get, url).to_return( + :status => 200, :body => content, :headers => {:x_oss_hash_crc64ecma => content_crc.to_i + 1}) + response_content = "" + expect(crc_protocol.download_crc_enable).to eq(true) + expect { + crc_protocol.get_object(@bucket, object_name) {|c| response_content << c} + }.to raise_error(CrcInconsistentError, "The crc of get between client and oss is not inconsistent.") + end + + it "should not raise crc exception on error" do + object_name = 'ruby' + url = get_request_path(object_name) + content = "hello world" + content_crc = Aliyun::OSS::Util.crc(content) + stub_request(:get, url).to_return( + :status => 200, :body => content, :headers => {:x_oss_hash_crc64ecma => content_crc}) + response_content = "" + expect(crc_protocol.download_crc_enable).to eq(true) + expect { + crc_protocol.get_object(@bucket, object_name) {|c| response_content << c} + }.not_to raise_error + expect(response_content).to eq(content) + end + + it "should not raise crc exception on error when setting range" do + object_name = 'ruby' + url = get_request_path(object_name) + content = "hello world 0123456789" + content_crc = Aliyun::OSS::Util.crc(content) + stub_request(:get, url).to_return( + :status => 200, :body => content, :headers => {:x_oss_hash_crc64ecma => content_crc.to_i + 1}) + response_content = "" + expect(crc_protocol.download_crc_enable).to eq(true) + expect { + crc_protocol.get_object(@bucket, object_name, {range: [0, 10]}) {|c| response_content << c} + }.not_to raise_error + expect(WebMock).to have_requested(:get, url) + .with(:body => nil, :query => {}, + :headers => { + 'Range' => 'bytes=0-9' + }) + end end # Get object context "Get object meta" do diff --git a/tests/config.rb b/tests/config.rb index 09640da..4710a06 100644 --- a/tests/config.rb +++ b/tests/config.rb @@ -4,7 +4,8 @@ def creds { access_key_id: ENV['RUBY_SDK_OSS_ID'], access_key_secret: ENV['RUBY_SDK_OSS_KEY'], - crc_enable: ENV['RUBY_SDK_OSS_CRC_ENABLE'], + download_crc_enable: ENV['RUBY_SDK_OSS_DOWNLOAD_CRC_ENABLE'], + upload_crc_enable: ENV['RUBY_SDK_OSS_UPLOAD_CRC_ENABLE'], endpoint: ENV['RUBY_SDK_OSS_ENDPOINT'] } end diff --git a/tests/helper.rb b/tests/helper.rb new file mode 100644 index 0000000..c2ff678 --- /dev/null +++ b/tests/helper.rb @@ -0,0 +1,15 @@ +# -*- encoding: utf-8 -*- + +module Aliyun + module Test + module Helper + def random_string(n) + (1...n).map { (65 + rand(26)).chr }.join + "\n" + end + + def random_bytes(n) + (1...n).map { rand(255).chr }.join + "\n" + end + end + end +end \ No newline at end of file diff --git a/tests/test_crc_check.rb b/tests/test_crc_check.rb new file mode 100644 index 0000000..4bac222 --- /dev/null +++ b/tests/test_crc_check.rb @@ -0,0 +1,184 @@ +require 'minitest/autorun' +require 'yaml' +require 'tempfile' +$LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__)) +require 'aliyun/oss' +require_relative 'config' +require_relative 'helper' + +class TestCrcCheck < Minitest::Test + + include Aliyun::Test::Helper + + @@tests_run = 0 + @@test_file = nil + + def setup + Aliyun::Common::Logging.set_log_level(Logger::DEBUG) + client = Aliyun::OSS::Client.new(TestConf.creds) + @bucket = client.get_bucket(TestConf.bucket) + @prefix = 'tests/crc_check/' + + if @@tests_run == 0 + @@test_file = Tempfile.new('oss_ruby_sdk_test_crc') + (10 * 1024).times { @@test_file.write(random_bytes(1024)) } + end + @@tests_run += 1 + end + + def teardown + if @@tests_run == TestCrcCheck.runnable_methods.length + @@test_file.unlink unless @@test_file.nil? + end + end + + def get_key(k) + @prefix + k + end + + def test_put_object + skip unless TestConf.creds[:upload_crc_enable] + + # Check crc status + assert(@bucket.upload_crc_enable) + + # Create a test file with 10MB random bytes to put. + key = get_key('put_file') + + @bucket.put_object(key, :file => @@test_file.path) + test_object = @bucket.get_object(key) + assert_equal(test_object.size, 10 * 1024 * 1024) + + # Check crc wrong case. + assert_raises Aliyun::OSS::CrcInconsistentError do + @bucket.put_object(key, {:init_crc => 1, :file => @@test_file.path}) do |content| + content << 'hello world.' + end + end + + # Put a string to oss. + key = get_key('put_string') + @bucket.put_object(key, :init_crc => 0) do |content| + content << 'hello world.' + end + test_object = @bucket.get_object(key) + assert_equal(test_object.size, 'hello world.'.size) + + # Check crc wrong case. + assert_raises Aliyun::OSS::CrcInconsistentError do + @bucket.put_object(key, :init_crc => 1) do |content| + content << 'hello world.' + end + end + ensure + @bucket.delete_object(key) + end + + def test_append_object + skip unless TestConf.creds[:upload_crc_enable] + key = get_key('append_file') + + # Check crc status + assert(@bucket.upload_crc_enable) + + # Check $key object doesn't exist. + test_object = @bucket.get_object(key) rescue 0 + @bucket.delete_object(key) if test_object.size + + # Create a append object to oss with a string. + position = @bucket.append_object(key, 0, :init_crc => 0) do |content| + content << 'hello world.' + end + test_object = @bucket.get_object(key) + assert_equal(test_object.size, 'hello world.'.size) + + # Append a test file to oss $key object. + @bucket.append_object(key, position, {:init_crc => test_object.headers[:x_oss_hash_crc64ecma], :file => @@test_file.path}) + test_object = @bucket.get_object(key) + assert_equal(test_object.size, 'hello world.'.size + (10 * 1024 * 1024)) + + # No crc check when init_crc is nil + position = @bucket.append_object(key, test_object.size) do |content| + content << 'hello world.' + end + test_object = @bucket.get_object(key) + assert_equal(test_object.size, 'hello world.'.size * 2 + (10 * 1024 * 1024)) + + # Check crc wrong case. + assert_raises Aliyun::OSS::CrcInconsistentError do + position = @bucket.append_object(key, test_object.size, :init_crc => 0) do |content| + content << 'hello world.' + end + end + + # Check crc wrong case. + test_object = @bucket.get_object(key) + assert_raises Aliyun::OSS::CrcInconsistentError do + @bucket.append_object(key, test_object.size, {:init_crc => 0, :file => @@test_file.path}) + end + ensure + @bucket.delete_object(key) + end + + def test_upload_object + skip unless TestConf.creds[:upload_crc_enable] + key = get_key('upload_file') + + # Check crc status + assert(@bucket.upload_crc_enable) + @bucket.resumable_upload(key, @@test_file.path, :cpt_file => "#{@@test_file.path}.cpt", threads: 2, :part_size => 1024 * 1024) + + test_object = @bucket.get_object(key) + assert_equal(test_object.size, (10 * 1024 * 1024)) + + ensure + @bucket.delete_object(key) + end + + def test_get_small_object + skip unless TestConf.creds[:download_crc_enable] + + # Check crc status + assert(@bucket.download_crc_enable) + + # Put a string to oss. + key = get_key('get_small_object') + @bucket.put_object(key) do |content| + content << 'hello world.' + end + temp_buf = "" + test_object = @bucket.get_object(key) { |c| temp_buf << c } + assert_equal(test_object.size, 'hello world.'.size) + + # Check crc wrong case. + assert_raises Aliyun::OSS::CrcInconsistentError do + @bucket.get_object(key, {:init_crc => 1}) { |c| temp_buf << c } + end + ensure + @bucket.delete_object(key) + end + + def test_get_large_object + skip unless TestConf.creds[:download_crc_enable] + + # Check crc status + assert(@bucket.download_crc_enable) + + # put a test file with 10MB random bytes to oss for testing get. + key = get_key('get_file') + @bucket.put_object(key, :file => @@test_file.path) + + get_temp_file = Tempfile.new('oss_ruby_sdk_test_crc_get') + test_object = @bucket.get_object(key, {:file => get_temp_file}) + assert_equal(test_object.size, 10 * 1024 * 1024) + + # Check crc wrong case. + assert_raises Aliyun::OSS::CrcInconsistentError do + @bucket.get_object(key, {:file => get_temp_file, :init_crc => 1}) + end + ensure + get_temp_file.unlink unless get_temp_file.nil? + @bucket.delete_object(key) + end + +end diff --git a/tests/test_custom_headers.rb b/tests/test_custom_headers.rb index d31201d..034de64 100644 --- a/tests/test_custom_headers.rb +++ b/tests/test_custom_headers.rb @@ -36,6 +36,7 @@ def test_custom_headers content_encoding = 'deflate' expires = (Time.now + 3600).httpdate + @bucket.put_object( key, headers: {'cache-control' => cache_control, @@ -46,12 +47,19 @@ def test_custom_headers end content = '' - obj = @bucket.get_object(key) { |c| content << c } - assert_equal 'hello world', content - assert_equal cache_control, obj.headers[:cache_control] - assert_equal content_disposition, obj.headers[:content_disposition] - assert_equal content_encoding, obj.headers[:content_encoding] - assert_equal expires, obj.headers[:expires] + obj = nil + if @bucket.download_crc_enable + assert_raises Aliyun::OSS::CrcInconsistentError do + obj = @bucket.get_object(key) { |c| content << c } + end + else + obj = @bucket.get_object(key) { |c| content << c } + assert_equal 'hello world', content + assert_equal cache_control, obj.headers[:cache_control] + assert_equal content_disposition, obj.headers[:content_disposition] + assert_equal content_encoding, obj.headers[:content_encoding] + assert_equal expires, obj.headers[:expires] + end end def test_headers_overwrite