From e84ed57a729be2e95e7028bfdf2134d58e8a1e98 Mon Sep 17 00:00:00 2001 From: Matthew Oliver Date: Tue, 13 Mar 2018 06:19:44 +0000 Subject: [PATCH] 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 --- swift/cli/form_signature.py | 11 ++++++++-- test/unit/cli/test_form_signature.py | 31 ++++++++++++++++------------ tox.ini | 1 + 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/swift/cli/form_signature.py b/swift/cli/form_signature.py index 0cf1a99d34..ab0d12f00a 100644 --- a/swift/cli/form_signature.py +++ b/swift/cli/form_signature.py @@ -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) diff --git a/test/unit/cli/test_form_signature.py b/test/unit/cli/test_form_signature.py index 427da89d2b..538baefb2c 100644 --- a/test/unit/cli/test_form_signature.py +++ b/test/unit/cli/test_form_signature.py @@ -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([ diff --git a/tox.ini b/tox.ini index dfa4e34966..c335d36cd5 100644 --- a/tox.ini +++ b/tox.ini @@ -30,6 +30,7 @@ setenv = VIRTUAL_ENV={envdir} commands = nosetests \ test/unit/cli/test_dispersion_report.py \ + test/unit/cli/test_form_signature.py \ test/unit/cli/test_info.py \ test/unit/cli/test_relinker.py \ test/unit/cli/test_ring_builder_analyzer.py \