summaryrefslogtreecommitdiff
path: root/tests/analysis
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2016-07-28 21:55:02 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2016-07-28 21:55:02 (GMT)
commit0c92911504f7d267c913fc8d2069cb87139b390b (patch)
tree236cf29352580a48c33ef54778d0a18c77608664 /tests/analysis
parentb509af52114501aff3ef81c49c431570f31a21d3 (diff)
Centralized the checksum computing of binary contents.
Diffstat (limited to 'tests/analysis')
-rw-r--r--tests/analysis/contents/checksum.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/analysis/contents/checksum.py b/tests/analysis/contents/checksum.py
new file mode 100644
index 0000000..ba09a3f
--- /dev/null
+++ b/tests/analysis/contents/checksum.py
@@ -0,0 +1,67 @@
+#!/usr/bin/python3-dbg
+# -*- coding: utf-8 -*-
+
+
+# Tests validant le bon calcul d'empreintes.
+
+
+from chrysacase import ChrysalideTestCase
+from pychrysalide.analysis.contents import FileContent, RestrictedContent
+from pychrysalide.arch import vmpa, mrange
+import hashlib
+import tempfile
+
+
+class TestRestrictedContent(ChrysalideTestCase):
+ """TestCase for analysis.contents.RestrictedContent."""
+
+ @classmethod
+ def setUpClass(cls):
+
+ super(TestRestrictedContent, cls).setUpClass()
+
+ cls._out = tempfile.NamedTemporaryFile()
+
+ cls._out.write(b'AAAABBBBCCCCDDDD')
+
+ cls._out.flush()
+
+ cls.log('Using temporary file "%s"' % cls._out.name)
+
+
+ @classmethod
+ def tearDownClass(cls):
+
+ super(TestRestrictedContent, cls).tearDownClass()
+
+ cls.log('Delete file "%s"' % cls._out.name)
+
+ cls._out.close()
+
+
+ def testFullChecksum(self):
+ """Check checksum of full content."""
+
+ fcnt = FileContent(self._out.name)
+ self.assertIsNotNone(fcnt)
+
+ expected = hashlib.sha256(b'AAAABBBBCCCCDDDD').hexdigest()
+
+ self.assertEqual(fcnt.get_checksum(), expected)
+
+
+ def testPartialChecksum(self):
+ """Check checksum of restricted content."""
+
+ fcnt = FileContent(self._out.name)
+ self.assertIsNotNone(fcnt)
+
+ start = vmpa(4, vmpa.VMPA_NO_VIRTUAL)
+ covered = mrange(start, 4) # 'BBBB'
+
+ rcnt = RestrictedContent(fcnt, covered)
+ self.assertIsNotNone(rcnt)
+
+ expected = hashlib.sha256(b'BBBB').hexdigest()
+
+ self.assertEqual(rcnt.get_checksum(), expected)