From 3c0f47f377eab95a373e84fc437f8f0e66dd8cdc Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Sun, 2 Sep 2012 12:02:04 +0100 Subject: [PATCH] [AWS|Glacier] byteslice is only available in 1.9.3 - add fallback for 1.9.2 --- lib/fog/aws/glacier.rb | 9 ++++++++- tests/aws/requests/glacier/tree_hash_tests.rb | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/fog/aws/glacier.rb b/lib/fog/aws/glacier.rb index 17474b8e7..0d1e78131 100644 --- a/lib/fog/aws/glacier.rb +++ b/lib/fog/aws/glacier.rb @@ -67,7 +67,14 @@ module Fog def digest_for_part(body) chunk_count = [body.bytesize / MEGABYTE + (body.bytesize % MEGABYTE > 0 ? 1 : 0), 1].max - digests_for_part = chunk_count.times.collect {|chunk_index| Digest::SHA256.digest(body.byteslice(chunk_index * MEGABYTE, MEGABYTE))} + if body.respond_to? :byteslice + digests_for_part = chunk_count.times.collect {|chunk_index| Digest::SHA256.digest(body.byteslice(chunk_index * MEGABYTE, MEGABYTE))} + else + old_encoding = body.encoding + body.force_encoding('BINARY') + digests_for_part = chunk_count.times.collect {|chunk_index| Digest::SHA256.digest(body.byteslice(chunk_index * MEGABYTE, MEGABYTE))} + body.force_encoding(old_encoding) + end reduce_digests(digests_for_part) end diff --git a/tests/aws/requests/glacier/tree_hash_tests.rb b/tests/aws/requests/glacier/tree_hash_tests.rb index e00b9f2fb..03a0300ed 100644 --- a/tests/aws/requests/glacier/tree_hash_tests.rb +++ b/tests/aws/requests/glacier/tree_hash_tests.rb @@ -4,6 +4,14 @@ Shindo.tests('AWS::Glacier | glacier tree hash calcuation', ['aws']) do returns(Digest::SHA256.hexdigest('')) { Fog::AWS::Glacier::TreeHash.digest('')} end + tests('tree_hash(multibyte characters)') do + body = ("\xC2\xA1".force_encoding('UTF-8') * 1024*1024) + expected = Digest::SHA256.hexdigest( + Digest::SHA256.digest("\xC2\xA1" * 1024*512) + Digest::SHA256.digest("\xC2\xA1" * 1024*512) + ) + returns(expected) { Fog::AWS::Glacier::TreeHash.digest(body)} + end + tests('tree_hash(power of 2 number of parts)') do body = ('x' * 1024*1024) + ('y'*1024*1024) + ('z'*1024*1024) + ('t'*1024*1024) expected = Digest::SHA256.hexdigest(