[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [stem/master] Better exception if provided with an invalid digest encoding
commit 616026e9fb26a3f594df2b5df7916c5117858b76
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date: Sat Nov 17 14:07:35 2018 -0800
Better exception if provided with an invalid digest encoding
Our digest methods rightfully raise a NotImplementedException if I bugger up
and add to the DigestEncoding enumeration without actually implementing it. But
if users provide other bad data for an encoding argument we should provide a
ValueError instead.
---
stem/descriptor/__init__.py | 2 ++
test/unit/descriptor/server_descriptor.py | 20 +++++++++++++++++++-
2 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py
index 66a34f6e..25b99ead 100644
--- a/stem/descriptor/__init__.py
+++ b/stem/descriptor/__init__.py
@@ -676,6 +676,8 @@ def _encode_digest(hash_value, encoding):
return stem.util.str_tools._to_unicode(hash_value.hexdigest().upper())
elif encoding == DigestEncoding.BASE64:
return stem.util.str_tools._to_unicode(base64.b64encode(hash_value.digest()).rstrip(b'='))
+ elif encoding not in DigestEncoding:
+ raise ValueError('Digest encodings should be among our DigestEncoding enumeration (%s), not %s' % (', '.join(DigestEncoding), encoding))
else:
raise NotImplementedError('BUG: stem.descriptor._encode_digest should recognize all DigestEncoding, lacked %s' % encoding)
diff --git a/test/unit/descriptor/server_descriptor.py b/test/unit/descriptor/server_descriptor.py
index 796286c5..7c0ebe97 100644
--- a/test/unit/descriptor/server_descriptor.py
+++ b/test/unit/descriptor/server_descriptor.py
@@ -4,6 +4,7 @@ Unit tests for stem.descriptor.server_descriptor.
import datetime
import functools
+import hashlib
import io
import pickle
import tarfile
@@ -19,6 +20,7 @@ import stem.version
import stem.util.str_tools
import test.require
+from stem.descriptor import DigestHash, DigestEncoding
from stem.descriptor.certificate import CertType, ExtensionType
from stem.descriptor.server_descriptor import BridgeDistribution, RelayDescriptor, BridgeDescriptor
@@ -154,7 +156,6 @@ Qlx9HNCqCY877ztFRC624ja2ql6A2hBcuoYMbkHjcQ4=
self.assertEqual(expected_signature, desc.signature)
self.assertEqual([], desc.get_unrecognized_lines())
self.assertEqual('2C7B27BEAB04B4E2459D89CA6D5CD1CC5F95A689', desc.digest())
- self.assertEqual('LHsnvqsEtOJFnYnKbVzRzF+Vpok', desc.digest(encoding = stem.descriptor.DigestEncoding.BASE64))
self.assertEqual('@type server-descriptor 1.0', str(desc.type_annotation()))
self.assertEqual(['2'], desc.hidden_service_dir) # obsolete field
@@ -531,6 +532,23 @@ Qlx9HNCqCY877ztFRC624ja2ql6A2hBcuoYMbkHjcQ4=
self.assertEqual('71.35.133.197', desc.address)
self.assertEqual(None, desc.fingerprint)
+ def test_digest(self):
+ with open(get_resource('example_descriptor'), 'rb') as descriptor_file:
+ desc = next(stem.descriptor.parse_file(descriptor_file, 'server-descriptor 1.0'))
+
+ self.assertEqual('2C7B27BEAB04B4E2459D89CA6D5CD1CC5F95A689', desc.digest(DigestHash.SHA1, DigestEncoding.HEX))
+ self.assertEqual('55F87C93AA5C16311308D09B94315DD1435174A95020E4FD47A70F82338BE9EA', desc.digest(DigestHash.SHA256, DigestEncoding.HEX))
+
+ self.assertEqual('LHsnvqsEtOJFnYnKbVzRzF+Vpok', desc.digest(DigestHash.SHA1, DigestEncoding.BASE64))
+ self.assertEqual('Vfh8k6pcFjETCNCblDFd0UNRdKlQIOT9R6cPgjOL6eo', desc.digest(DigestHash.SHA256, DigestEncoding.BASE64))
+
+ digested_content = desc._content_range(start = 'router', end = '\nrouter-signature\n')
+ self.assertEqual(hashlib.sha1(digested_content).digest(), desc.digest(DigestHash.SHA1, DigestEncoding.RAW).digest())
+ self.assertEqual(hashlib.sha256(digested_content).digest(), desc.digest(DigestHash.SHA256, DigestEncoding.RAW).digest())
+
+ self.assertRaisesWith(NotImplementedError, 'Server descriptor digests are only available in sha1 and sha256, not bad-hash', desc.digest, 'bad-hash')
+ self.assertRaisesWith(ValueError, 'Digest encodings should be among our DigestEncoding enumeration (RAW, HEX, BASE64), not BAD_ENCODING', desc.digest, DigestHash.SHA1, 'BAD_ENCODING')
+
def test_with_opt(self):
"""
Includes an 'opt <keyword> <value>' entry.
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits