| 1 |
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html |
| 2 |
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE |
| 3 |
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import unittest |
| 8 |
|
| 9 |
from astroid import MANAGER |
| 10 |
from astroid.nodes.scoped_nodes import ClassDef |
| 11 |
|
| 12 |
|
| 13 |
class HashlibTest(unittest.TestCase): |
| 14 |
def _assert_hashlib_class(self, class_obj: ClassDef) -> None: |
| 15 |
self.assertIn("update", class_obj) |
| 16 |
self.assertIn("digest", class_obj) |
| 17 |
self.assertIn("hexdigest", class_obj) |
| 18 |
self.assertIn("block_size", class_obj) |
| 19 |
self.assertIn("digest_size", class_obj) |
| 20 |
# usedforsecurity was added in Python 3.9, see 8e7174a9 |
| 21 |
self.assertEqual(len(class_obj["__init__"].args.args), 3) |
| 22 |
self.assertEqual(len(class_obj["__init__"].args.defaults), 2) |
| 23 |
self.assertEqual(len(class_obj["update"].args.args), 2) |
| 24 |
|
| 25 |
def test_hashlib(self) -> None: |
| 26 |
"""Tests that brain extensions for hashlib work.""" |
| 27 |
hashlib_module = MANAGER.ast_from_module_name("hashlib") |
| 28 |
for class_name in ( |
| 29 |
"md5", |
| 30 |
"sha1", |
| 31 |
"sha224", |
| 32 |
"sha256", |
| 33 |
"sha384", |
| 34 |
"sha512", |
| 35 |
"sha3_224", |
| 36 |
"sha3_256", |
| 37 |
"sha3_384", |
| 38 |
"sha3_512", |
| 39 |
): |
| 40 |
class_obj = hashlib_module[class_name] |
| 41 |
self._assert_hashlib_class(class_obj) |
| 42 |
self.assertEqual(len(class_obj["digest"].args.args), 1) |
| 43 |
self.assertEqual(len(class_obj["hexdigest"].args.args), 1) |
| 44 |
|
| 45 |
def test_shake(self) -> None: |
| 46 |
"""Tests that the brain extensions for the hashlib shake algorithms work.""" |
| 47 |
hashlib_module = MANAGER.ast_from_module_name("hashlib") |
| 48 |
for class_name in ("shake_128", "shake_256"): |
| 49 |
class_obj = hashlib_module[class_name] |
| 50 |
self._assert_hashlib_class(class_obj) |
| 51 |
self.assertEqual(len(class_obj["digest"].args.args), 2) |
| 52 |
self.assertEqual(len(class_obj["hexdigest"].args.args), 2) |
| 53 |
|
| 54 |
def test_blake2(self) -> None: |
| 55 |
"""Tests that the brain extensions for the hashlib blake2 hash functions work.""" |
| 56 |
hashlib_module = MANAGER.ast_from_module_name("hashlib") |
| 57 |
for class_name in ("blake2b", "blake2s"): |
| 58 |
class_obj = hashlib_module[class_name] |
| 59 |
self.assertEqual(len(class_obj["__init__"].args.args), 2) |
| 60 |
self.assertEqual(len(class_obj["digest"].args.args), 1) |
| 61 |
self.assertEqual(len(class_obj["hexdigest"].args.args), 1) |