Coverage for tests/thirdparty/test_sha256.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-07-10 13:43 +0000

1# The MIT License (MIT) 

2# 

3# Copyright (c) 2021 RSK Labs Ltd 

4# 

5# Permission is hereby granted, free of charge, to any person obtaining a copy of 

6# this software and associated documentation files (the "Software"), to deal in 

7# the Software without restriction, including without limitation the rights to 

8# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 

9# of the Software, and to permit persons to whom the Software is furnished to do 

10# so, subject to the following conditions: 

11# 

12# The above copyright notice and this permission notice shall be included in all 

13# copies or substantial portions of the Software. 

14# 

15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 

21# SOFTWARE. 

22 

23from unittest import TestCase 

24import hashlib 

25import random 

26import os 

27import thirdparty.sha256 

28 

29import logging 

30 

31logging.disable(logging.CRITICAL) 

32 

33 

34class TestSha256(TestCase): 

35 _MAX_MESSAGE_LENGTH = 256 

36 _NUM_CASES = 100 

37 

38 def test_sha256_random(self): 

39 for i in range(self._NUM_CASES): 

40 (message, expected) = self.generate_random_testcase() 

41 actual = thirdparty.sha256.SHA256(message).digest() 

42 self.assertEqual(expected, actual) 

43 

44 def test_sha256_multiple_of_64(self): 

45 for i in range(self._NUM_CASES): 

46 (message, expected) = self.generate_random_testcase(64) 

47 actual = thirdparty.sha256.SHA256(message).digest() 

48 self.assertEqual(expected, actual) 

49 

50 def generate_random_testcase(self, multiple_of=1): 

51 len = (int(random.uniform(0, self._MAX_MESSAGE_LENGTH // multiple_of + 1)) * 

52 multiple_of) 

53 message = os.urandom(len) 

54 out = hashlib.sha256(message) 

55 return (message, out.digest())