summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorOlle Lundberg <geek@nerd.sh>2014-08-14 15:13:58 +0200
committerOlle Lundberg <geek@nerd.sh>2014-08-14 15:16:42 +0200
commit2dec0a7671d83f21a290e1a2b3ea9159da45757a (patch)
treea6419ca78b85f19509eb9f099cecab2ad962fe62
parentb5c537c4c88cc39323717531a46a3241eb7ca7f5 (diff)
Add a utility method for value clamping.
-rw-r--r--paramiko/util.py3
-rw-r--r--tests/test_util.py5
2 files changed, 8 insertions, 0 deletions
diff --git a/paramiko/util.py b/paramiko/util.py
index f4ee3adc..d029f52e 100644
--- a/paramiko/util.py
+++ b/paramiko/util.py
@@ -320,3 +320,6 @@ def constant_time_bytes_eq(a, b):
for i in (xrange if PY2 else range)(len(a)):
res |= byte_ord(a[i]) ^ byte_ord(b[i])
return res == 0
+
+def clamp_value(minimum, val, maximum):
+ return max(minimum, min(val, maximum))
diff --git a/tests/test_util.py b/tests/test_util.py
index 69c75518..24f58076 100644
--- a/tests/test_util.py
+++ b/tests/test_util.py
@@ -333,3 +333,8 @@ IdentityFile something_%l_using_fqdn
"""
config = paramiko.util.parse_ssh_config(StringIO(test_config))
assert config.lookup('meh') # will die during lookup() if bug regresses
+
+ def test_12_clamp_value(self):
+ self.assertEqual(32768, paramiko.util.clamp_value(32767, 32768, 32769))
+ self.assertEqual(32767, paramiko.util.clamp_value(32767, 32765, 32769))
+ self.assertEqual(32769, paramiko.util.clamp_value(32767, 32770, 32769))