py3: port cli form_signature and unit tests
This patch ports the form_signature cli `swift-form-signature` and it's corrisponding tests to py3. In essence in Py3 the HMAC function expects binary strings. Change-Id: I5dded4ceb80f0cc595403775e8f9c17873e1e37b
This commit is contained in:
parent
0a4d1b7e22
commit
e84ed57a72
@ -17,6 +17,7 @@ Script for generating a form signature for use with FormPost middleware.
|
||||
"""
|
||||
from __future__ import print_function
|
||||
import hmac
|
||||
import six
|
||||
from hashlib import sha1
|
||||
from os.path import basename
|
||||
from time import time
|
||||
@ -92,8 +93,14 @@ def main(argv):
|
||||
print('For example: /v1/account/container')
|
||||
print(' Or: /v1/account/container/object_prefix')
|
||||
return 1
|
||||
sig = hmac.new(key, '%s\n%s\n%s\n%s\n%s' % (path, redirect, max_file_size,
|
||||
max_file_count, expires),
|
||||
data = '%s\n%s\n%s\n%s\n%s' % (path, redirect, max_file_size,
|
||||
max_file_count, expires)
|
||||
if six.PY3:
|
||||
data = data if isinstance(data, six.binary_type) else \
|
||||
data.encode('utf8')
|
||||
key = key if isinstance(key, six.binary_type) else \
|
||||
key.encode('utf8')
|
||||
sig = hmac.new(key, data,
|
||||
sha1).hexdigest()
|
||||
print(' Expires:', expires)
|
||||
print('Signature:', sig)
|
||||
|
@ -17,7 +17,7 @@
|
||||
import hashlib
|
||||
import hmac
|
||||
import mock
|
||||
from six import StringIO
|
||||
import six
|
||||
import unittest
|
||||
|
||||
from swift.cli import form_signature
|
||||
@ -33,14 +33,19 @@ class TestFormSignature(unittest.TestCase):
|
||||
max_file_size = str(int(1024 * 1024 * 1024 * 3.14159)) # π GiB
|
||||
max_file_count = '3'
|
||||
|
||||
expected_signature = hmac.new(
|
||||
key,
|
||||
"\n".join((
|
||||
path, redirect, max_file_size, max_file_count,
|
||||
str(int(the_time + expires)))),
|
||||
hashlib.sha1).hexdigest()
|
||||
data = "\n".join((
|
||||
path, redirect, max_file_size, max_file_count,
|
||||
str(int(the_time + expires))))
|
||||
|
||||
out = StringIO()
|
||||
if six.PY3:
|
||||
data = data if isinstance(data, six.binary_type) else \
|
||||
data.encode('utf8')
|
||||
key = key if isinstance(key, six.binary_type) else \
|
||||
key.encode('utf8')
|
||||
|
||||
expected_signature = hmac.new(key, data, hashlib.sha1).hexdigest()
|
||||
|
||||
out = six.StringIO()
|
||||
with mock.patch('swift.cli.form_signature.time', lambda: the_time):
|
||||
with mock.patch('sys.stdout', out):
|
||||
exitcode = form_signature.main([
|
||||
@ -59,7 +64,7 @@ class TestFormSignature(unittest.TestCase):
|
||||
self.assertIn(sig_input, out.getvalue())
|
||||
|
||||
def test_too_few_args(self):
|
||||
out = StringIO()
|
||||
out = six.StringIO()
|
||||
with mock.patch('sys.stdout', out):
|
||||
exitcode = form_signature.main([
|
||||
'/path/to/swift-form-signature',
|
||||
@ -70,7 +75,7 @@ class TestFormSignature(unittest.TestCase):
|
||||
self.assertIn(usage, out.getvalue())
|
||||
|
||||
def test_invalid_filesize_arg(self):
|
||||
out = StringIO()
|
||||
out = six.StringIO()
|
||||
key = 'secret squirrel'
|
||||
with mock.patch('sys.stdout', out):
|
||||
exitcode = form_signature.main([
|
||||
@ -79,7 +84,7 @@ class TestFormSignature(unittest.TestCase):
|
||||
self.assertNotEqual(exitcode, 0)
|
||||
|
||||
def test_invalid_filecount_arg(self):
|
||||
out = StringIO()
|
||||
out = six.StringIO()
|
||||
key = 'secret squirrel'
|
||||
with mock.patch('sys.stdout', out):
|
||||
exitcode = form_signature.main([
|
||||
@ -88,7 +93,7 @@ class TestFormSignature(unittest.TestCase):
|
||||
self.assertNotEqual(exitcode, 0)
|
||||
|
||||
def test_invalid_path_arg(self):
|
||||
out = StringIO()
|
||||
out = six.StringIO()
|
||||
key = 'secret squirrel'
|
||||
with mock.patch('sys.stdout', out):
|
||||
exitcode = form_signature.main([
|
||||
@ -97,7 +102,7 @@ class TestFormSignature(unittest.TestCase):
|
||||
self.assertNotEqual(exitcode, 0)
|
||||
|
||||
def test_invalid_seconds_arg(self):
|
||||
out = StringIO()
|
||||
out = six.StringIO()
|
||||
key = 'secret squirrel'
|
||||
with mock.patch('sys.stdout', out):
|
||||
exitcode = form_signature.main([
|
||||
|
Loading…
Reference in New Issue
Block a user