summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2014-12-04 18:44:12 +0000
committerGerrit Code Review <review@openstack.org>2014-12-04 18:44:12 +0000
commit7a7cdf3d6a2e2078658df3783170b68f25973db1 (patch)
tree97413c5ebd4f802fed369ce5669d09d447bb201a
parente129c7cc770fc23ef25fc12f100300f82e791b31 (diff)
parent83834eb2769a6dd4193e514007abcdbfa4241001 (diff)
downloadswift-7a7cdf3d6a2e2078658df3783170b68f25973db1.tar.gz
Merge "Raise ValueError for offset on Timestamp over limit"
-rw-r--r--swift/common/utils.py3
-rw-r--r--test/unit/common/test_utils.py15
2 files changed, 18 insertions, 0 deletions
diff --git a/swift/common/utils.py b/swift/common/utils.py
index 239d7f067..a6633a9cd 100644
--- a/swift/common/utils.py
+++ b/swift/common/utils.py
@@ -587,6 +587,7 @@ def drop_buffer_cache(fd, offset, length):
NORMAL_FORMAT = "%016.05f"
INTERNAL_FORMAT = NORMAL_FORMAT + '_%016x'
+MAX_OFFSET = (16 ** 16) - 1
# Setting this to True will cause the internal format to always display
# extended digits - even when the value is equivalent to the normalized form.
# This isn't ideal during an upgrade when some servers might not understand
@@ -647,6 +648,8 @@ class Timestamp(object):
self.offset += offset
else:
raise ValueError('offset must be non-negative')
+ if self.offset > MAX_OFFSET:
+ raise ValueError('offset must be smaller than %d' % MAX_OFFSET)
def __repr__(self):
return INTERNAL_FORMAT % (self.timestamp, self.offset)
diff --git a/test/unit/common/test_utils.py b/test/unit/common/test_utils.py
index e62abc45d..87a136270 100644
--- a/test/unit/common/test_utils.py
+++ b/test/unit/common/test_utils.py
@@ -163,6 +163,21 @@ class TestTimestamp(unittest.TestCase):
t = utils.Timestamp(time.time())
self.assertRaises(TypeError, str, t)
+ def test_offset_limit(self):
+ t = 1417462430.78693
+ # can't have a offset above MAX_OFFSET
+ self.assertRaises(ValueError, utils.Timestamp, t,
+ offset=utils.MAX_OFFSET + 1)
+ # exactly max offset is fine
+ ts = utils.Timestamp(t, offset=utils.MAX_OFFSET)
+ self.assertEqual(ts.internal, '1417462430.78693_ffffffffffffffff')
+ # but you can't offset it further
+ self.assertRaises(ValueError, utils.Timestamp, ts.internal, offset=1)
+ # unless you start below it
+ ts = utils.Timestamp(t, offset=utils.MAX_OFFSET - 1)
+ self.assertEqual(utils.Timestamp(ts.internal, offset=1),
+ '1417462430.78693_ffffffffffffffff')
+
def test_normal_format_no_offset(self):
expected = '1402436408.91203'
test_values = (