aliyun-sdk 0.4.1 → 0.5.0

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.
@@ -44,6 +44,56 @@ module Aliyun
44
44
  expect(signature).to eq("7Oh2wobzeg6dw/cWYbF/2m6s6qc=")
45
45
  end
46
46
 
47
+ # 测试CRC计算是否正确
48
+ it "should calculate a correct data crc" do
49
+ content = ""
50
+ crc = Util.crc(content)
51
+ expect(crc).to eq(0)
52
+
53
+ content = "hello world"
54
+ crc = Util.crc(content)
55
+ expect(crc).to eq(5981764153023615706)
56
+
57
+ content = "test\0hello\1world\2!\3"
58
+ crc = Util.crc(content)
59
+ expect(crc).to eq(6745424696046691431)
60
+ end
61
+
62
+ # 测试CRC Combine计算是否正确
63
+ it "should calculate a correct crc that crc_a combine with crc_b" do
64
+ content_a = "test\0hello\1world\2!\3"
65
+ crc_a = Util.crc(content_a)
66
+ expect(crc_a).to eq(6745424696046691431)
67
+
68
+ content_b = "hello world"
69
+ crc_b = Util.crc(content_b)
70
+ expect(crc_b).to eq(5981764153023615706)
71
+
72
+ crc_c = Util.crc_combine(crc_a, crc_b, content_b.size)
73
+ expect(crc_c).to eq(13027479509578346683)
74
+
75
+ crc_ab = Util.crc(content_a + content_b)
76
+ expect(crc_ab).to eq(crc_c)
77
+
78
+ crc_ab = Util.crc(content_b, crc_a)
79
+ expect(crc_ab).to eq(crc_c)
80
+ end
81
+
82
+ # 测试CRC校验和异常处理是否正确
83
+ it "should check inconsistent crc" do
84
+ expect {
85
+ Util.crc_check(6745424696046691431, 6745424696046691431, 'put')
86
+ }.not_to raise_error
87
+
88
+ expect {
89
+ Util.crc_check(6745424696046691431, 5981764153023615706, 'append')
90
+ }.to raise_error(CrcInconsistentError, "The crc of append between client and oss is not inconsistent.")
91
+
92
+ expect {
93
+ Util.crc_check(6745424696046691431, -1, 'post')
94
+ }.to raise_error(CrcInconsistentError, "The crc of post between client and oss is not inconsistent.")
95
+ end
96
+
47
97
  end # Util
48
98
 
49
99
  end # OSS
@@ -4,6 +4,8 @@ class TestConf
4
4
  {
5
5
  access_key_id: ENV['RUBY_SDK_OSS_ID'],
6
6
  access_key_secret: ENV['RUBY_SDK_OSS_KEY'],
7
+ download_crc_enable: ENV['RUBY_SDK_OSS_DOWNLOAD_CRC_ENABLE'],
8
+ upload_crc_enable: ENV['RUBY_SDK_OSS_UPLOAD_CRC_ENABLE'],
7
9
  endpoint: ENV['RUBY_SDK_OSS_ENDPOINT']
8
10
  }
9
11
  end
@@ -0,0 +1,15 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Aliyun
4
+ module Test
5
+ module Helper
6
+ def random_string(n)
7
+ (1...n).map { (65 + rand(26)).chr }.join + "\n"
8
+ end
9
+
10
+ def random_bytes(n)
11
+ (1...n).map { rand(255).chr }.join + "\n"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,184 @@
1
+ require 'minitest/autorun'
2
+ require 'yaml'
3
+ require 'tempfile'
4
+ $LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
5
+ require 'aliyun/oss'
6
+ require_relative 'config'
7
+ require_relative 'helper'
8
+
9
+ class TestCrcCheck < Minitest::Test
10
+
11
+ include Aliyun::Test::Helper
12
+
13
+ @@tests_run = 0
14
+ @@test_file = nil
15
+
16
+ def setup
17
+ Aliyun::Common::Logging.set_log_level(Logger::DEBUG)
18
+ client = Aliyun::OSS::Client.new(TestConf.creds)
19
+ @bucket = client.get_bucket(TestConf.bucket)
20
+ @prefix = 'tests/crc_check/'
21
+
22
+ if @@tests_run == 0
23
+ @@test_file = Tempfile.new('oss_ruby_sdk_test_crc')
24
+ (10 * 1024).times { @@test_file.write(random_bytes(1024)) }
25
+ end
26
+ @@tests_run += 1
27
+ end
28
+
29
+ def teardown
30
+ if @@tests_run == TestCrcCheck.runnable_methods.length
31
+ @@test_file.unlink unless @@test_file.nil?
32
+ end
33
+ end
34
+
35
+ def get_key(k)
36
+ @prefix + k
37
+ end
38
+
39
+ def test_put_object
40
+ skip unless TestConf.creds[:upload_crc_enable]
41
+
42
+ # Check crc status
43
+ assert(@bucket.upload_crc_enable)
44
+
45
+ # Create a test file with 10MB random bytes to put.
46
+ key = get_key('put_file')
47
+
48
+ @bucket.put_object(key, :file => @@test_file.path)
49
+ test_object = @bucket.get_object(key)
50
+ assert_equal(test_object.size, 10 * 1024 * 1024)
51
+
52
+ # Check crc wrong case.
53
+ assert_raises Aliyun::OSS::CrcInconsistentError do
54
+ @bucket.put_object(key, {:init_crc => 1, :file => @@test_file.path}) do |content|
55
+ content << 'hello world.'
56
+ end
57
+ end
58
+
59
+ # Put a string to oss.
60
+ key = get_key('put_string')
61
+ @bucket.put_object(key, :init_crc => 0) do |content|
62
+ content << 'hello world.'
63
+ end
64
+ test_object = @bucket.get_object(key)
65
+ assert_equal(test_object.size, 'hello world.'.size)
66
+
67
+ # Check crc wrong case.
68
+ assert_raises Aliyun::OSS::CrcInconsistentError do
69
+ @bucket.put_object(key, :init_crc => 1) do |content|
70
+ content << 'hello world.'
71
+ end
72
+ end
73
+ ensure
74
+ @bucket.delete_object(key)
75
+ end
76
+
77
+ def test_append_object
78
+ skip unless TestConf.creds[:upload_crc_enable]
79
+ key = get_key('append_file')
80
+
81
+ # Check crc status
82
+ assert(@bucket.upload_crc_enable)
83
+
84
+ # Check $key object doesn't exist.
85
+ test_object = @bucket.get_object(key) rescue 0
86
+ @bucket.delete_object(key) if test_object.size
87
+
88
+ # Create a append object to oss with a string.
89
+ position = @bucket.append_object(key, 0, :init_crc => 0) do |content|
90
+ content << 'hello world.'
91
+ end
92
+ test_object = @bucket.get_object(key)
93
+ assert_equal(test_object.size, 'hello world.'.size)
94
+
95
+ # Append a test file to oss $key object.
96
+ @bucket.append_object(key, position, {:init_crc => test_object.headers[:x_oss_hash_crc64ecma], :file => @@test_file.path})
97
+ test_object = @bucket.get_object(key)
98
+ assert_equal(test_object.size, 'hello world.'.size + (10 * 1024 * 1024))
99
+
100
+ # No crc check when init_crc is nil
101
+ position = @bucket.append_object(key, test_object.size) do |content|
102
+ content << 'hello world.'
103
+ end
104
+ test_object = @bucket.get_object(key)
105
+ assert_equal(test_object.size, 'hello world.'.size * 2 + (10 * 1024 * 1024))
106
+
107
+ # Check crc wrong case.
108
+ assert_raises Aliyun::OSS::CrcInconsistentError do
109
+ position = @bucket.append_object(key, test_object.size, :init_crc => 0) do |content|
110
+ content << 'hello world.'
111
+ end
112
+ end
113
+
114
+ # Check crc wrong case.
115
+ test_object = @bucket.get_object(key)
116
+ assert_raises Aliyun::OSS::CrcInconsistentError do
117
+ @bucket.append_object(key, test_object.size, {:init_crc => 0, :file => @@test_file.path})
118
+ end
119
+ ensure
120
+ @bucket.delete_object(key)
121
+ end
122
+
123
+ def test_upload_object
124
+ skip unless TestConf.creds[:upload_crc_enable]
125
+ key = get_key('upload_file')
126
+
127
+ # Check crc status
128
+ assert(@bucket.upload_crc_enable)
129
+ @bucket.resumable_upload(key, @@test_file.path, :cpt_file => "#{@@test_file.path}.cpt", threads: 2, :part_size => 1024 * 1024)
130
+
131
+ test_object = @bucket.get_object(key)
132
+ assert_equal(test_object.size, (10 * 1024 * 1024))
133
+
134
+ ensure
135
+ @bucket.delete_object(key)
136
+ end
137
+
138
+ def test_get_small_object
139
+ skip unless TestConf.creds[:download_crc_enable]
140
+
141
+ # Check crc status
142
+ assert(@bucket.download_crc_enable)
143
+
144
+ # Put a string to oss.
145
+ key = get_key('get_small_object')
146
+ @bucket.put_object(key) do |content|
147
+ content << 'hello world.'
148
+ end
149
+ temp_buf = ""
150
+ test_object = @bucket.get_object(key) { |c| temp_buf << c }
151
+ assert_equal(test_object.size, 'hello world.'.size)
152
+
153
+ # Check crc wrong case.
154
+ assert_raises Aliyun::OSS::CrcInconsistentError do
155
+ @bucket.get_object(key, {:init_crc => 1}) { |c| temp_buf << c }
156
+ end
157
+ ensure
158
+ @bucket.delete_object(key)
159
+ end
160
+
161
+ def test_get_large_object
162
+ skip unless TestConf.creds[:download_crc_enable]
163
+
164
+ # Check crc status
165
+ assert(@bucket.download_crc_enable)
166
+
167
+ # put a test file with 10MB random bytes to oss for testing get.
168
+ key = get_key('get_file')
169
+ @bucket.put_object(key, :file => @@test_file.path)
170
+
171
+ get_temp_file = Tempfile.new('oss_ruby_sdk_test_crc_get')
172
+ test_object = @bucket.get_object(key, {:file => get_temp_file})
173
+ assert_equal(test_object.size, 10 * 1024 * 1024)
174
+
175
+ # Check crc wrong case.
176
+ assert_raises Aliyun::OSS::CrcInconsistentError do
177
+ @bucket.get_object(key, {:file => get_temp_file, :init_crc => 1})
178
+ end
179
+ ensure
180
+ get_temp_file.unlink unless get_temp_file.nil?
181
+ @bucket.delete_object(key)
182
+ end
183
+
184
+ end
@@ -36,6 +36,7 @@ class TestCustomHeaders < Minitest::Test
36
36
 
37
37
  content_encoding = 'deflate'
38
38
  expires = (Time.now + 3600).httpdate
39
+
39
40
  @bucket.put_object(
40
41
  key,
41
42
  headers: {'cache-control' => cache_control,
@@ -46,12 +47,19 @@ class TestCustomHeaders < Minitest::Test
46
47
  end
47
48
 
48
49
  content = ''
49
- obj = @bucket.get_object(key) { |c| content << c }
50
- assert_equal 'hello world', content
51
- assert_equal cache_control, obj.headers[:cache_control]
52
- assert_equal content_disposition, obj.headers[:content_disposition]
53
- assert_equal content_encoding, obj.headers[:content_encoding]
54
- assert_equal expires, obj.headers[:expires]
50
+ obj = nil
51
+ if @bucket.download_crc_enable
52
+ assert_raises Aliyun::OSS::CrcInconsistentError do
53
+ obj = @bucket.get_object(key) { |c| content << c }
54
+ end
55
+ else
56
+ obj = @bucket.get_object(key) { |c| content << c }
57
+ assert_equal 'hello world', content
58
+ assert_equal cache_control, obj.headers[:cache_control]
59
+ assert_equal content_disposition, obj.headers[:content_disposition]
60
+ assert_equal content_encoding, obj.headers[:content_encoding]
61
+ assert_equal expires, obj.headers[:expires]
62
+ end
55
63
  end
56
64
 
57
65
  def test_headers_overwrite
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aliyun-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tianlong Wu
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: lib/aliyun
10
10
  cert_chain: []
11
- date: 2016-07-19 00:00:00.000000000 Z
11
+ date: 2016-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '10.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake-compiler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.9.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.9.0
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rspec
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -126,7 +140,8 @@ description: A Ruby program to facilitate accessing Aliyun Object Storage Servic
126
140
  email:
127
141
  - rockuw.@gmail.com
128
142
  executables: []
129
- extensions: []
143
+ extensions:
144
+ - ext/crcx/extconf.rb
130
145
  extra_rdoc_files:
131
146
  - README.md
132
147
  - CHANGELOG.md
@@ -141,6 +156,10 @@ files:
141
156
  - examples/aliyun/oss/streaming.rb
142
157
  - examples/aliyun/oss/using_sts.rb
143
158
  - examples/aliyun/sts/assume_role.rb
159
+ - ext/crcx/crc64_ecma.c
160
+ - ext/crcx/crcx.c
161
+ - ext/crcx/crcx.h
162
+ - ext/crcx/extconf.rb
144
163
  - lib/aliyun/common.rb
145
164
  - lib/aliyun/common/exception.rb
146
165
  - lib/aliyun/common/logging.rb
@@ -180,8 +199,10 @@ files:
180
199
  - spec/aliyun/sts/client_spec.rb
181
200
  - spec/aliyun/sts/util_spec.rb
182
201
  - tests/config.rb
202
+ - tests/helper.rb
183
203
  - tests/test_content_encoding.rb
184
204
  - tests/test_content_type.rb
205
+ - tests/test_crc_check.rb
185
206
  - tests/test_custom_headers.rb
186
207
  - tests/test_encoding.rb
187
208
  - tests/test_large_file.rb
@@ -228,8 +249,10 @@ test_files:
228
249
  - spec/aliyun/sts/client_spec.rb
229
250
  - spec/aliyun/sts/util_spec.rb
230
251
  - tests/config.rb
252
+ - tests/helper.rb
231
253
  - tests/test_content_encoding.rb
232
254
  - tests/test_content_type.rb
255
+ - tests/test_crc_check.rb
233
256
  - tests/test_custom_headers.rb
234
257
  - tests/test_encoding.rb
235
258
  - tests/test_large_file.rb