this repo has no description
at trunk 117 lines 4.4 kB view raw
1#!/usr/bin/env python3 2# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) 3# WARNING: This is a temporary copy of code from the cpython library to 4# facilitate bringup. Please file a task for anything you change! 5# flake8: noqa 6# fmt: off 7 8import hmac 9import unittest 10 11 12class CompareDigestTestCase(unittest.TestCase): 13 14 def test_compare_digest(self): 15 # Testing input type exception handling 16 a, b = 100, 200 17 self.assertRaises(TypeError, hmac.compare_digest, a, b) 18 a, b = 100, b"foobar" 19 self.assertRaises(TypeError, hmac.compare_digest, a, b) 20 a, b = b"foobar", 200 21 self.assertRaises(TypeError, hmac.compare_digest, a, b) 22 a, b = "foobar", b"foobar" 23 self.assertRaises(TypeError, hmac.compare_digest, a, b) 24 a, b = b"foobar", "foobar" 25 self.assertRaises(TypeError, hmac.compare_digest, a, b) 26 27 # Testing bytes of different lengths 28 a, b = b"foobar", b"foo" 29 self.assertFalse(hmac.compare_digest(a, b)) 30 a, b = b"\xde\xad\xbe\xef", b"\xde\xad" 31 self.assertFalse(hmac.compare_digest(a, b)) 32 33 # Testing bytes of same lengths, different values 34 a, b = b"foobar", b"foobaz" 35 self.assertFalse(hmac.compare_digest(a, b)) 36 a, b = b"\xde\xad\xbe\xef", b"\xab\xad\x1d\xea" 37 self.assertFalse(hmac.compare_digest(a, b)) 38 39 # Testing bytes of same lengths, same values 40 a, b = b"foobar", b"foobar" 41 self.assertTrue(hmac.compare_digest(a, b)) 42 a, b = b"\xde\xad\xbe\xef", b"\xde\xad\xbe\xef" 43 self.assertTrue(hmac.compare_digest(a, b)) 44 45 # Testing bytearrays of same lengths, same values 46 a, b = bytearray(b"foobar"), bytearray(b"foobar") 47 self.assertTrue(hmac.compare_digest(a, b)) 48 49 # Testing bytearrays of different lengths 50 a, b = bytearray(b"foobar"), bytearray(b"foo") 51 self.assertFalse(hmac.compare_digest(a, b)) 52 53 # Testing bytearrays of same lengths, different values 54 a, b = bytearray(b"foobar"), bytearray(b"foobaz") 55 self.assertFalse(hmac.compare_digest(a, b)) 56 57 # Testing byte and bytearray of same lengths, same values 58 a, b = bytearray(b"foobar"), b"foobar" 59 self.assertTrue(hmac.compare_digest(a, b)) 60 self.assertTrue(hmac.compare_digest(b, a)) 61 62 # Testing byte bytearray of different lengths 63 a, b = bytearray(b"foobar"), b"foo" 64 self.assertFalse(hmac.compare_digest(a, b)) 65 self.assertFalse(hmac.compare_digest(b, a)) 66 67 # Testing byte and bytearray of same lengths, different values 68 a, b = bytearray(b"foobar"), b"foobaz" 69 self.assertFalse(hmac.compare_digest(a, b)) 70 self.assertFalse(hmac.compare_digest(b, a)) 71 72 # Testing str of same lengths 73 a, b = "foobar", "foobar" 74 self.assertTrue(hmac.compare_digest(a, b)) 75 76 # Testing str of different lengths 77 a, b = "foo", "foobar" 78 self.assertFalse(hmac.compare_digest(a, b)) 79 80 # Testing bytes of same lengths, different values 81 a, b = "foobar", "foobaz" 82 self.assertFalse(hmac.compare_digest(a, b)) 83 84 # Testing error cases 85 a, b = "foobar", b"foobar" 86 self.assertRaises(TypeError, hmac.compare_digest, a, b) 87 a, b = b"foobar", 1 88 self.assertRaises(TypeError, hmac.compare_digest, a, b) 89 a, b = 100, 200 90 self.assertRaises(TypeError, hmac.compare_digest, a, b) 91 92 # subclasses are supported by ignore __eq__ 93 class mystr(str): 94 def __eq__(self, other): 95 return False 96 97 a, b = mystr("foobar"), mystr("foobar") 98 self.assertTrue(hmac.compare_digest(a, b)) 99 a, b = mystr("foobar"), "foobar" 100 self.assertTrue(hmac.compare_digest(a, b)) 101 a, b = mystr("foobar"), mystr("foobaz") 102 self.assertFalse(hmac.compare_digest(a, b)) 103 104 class mybytes(bytes): 105 def __eq__(self, other): 106 return False 107 108 a, b = mybytes(b"foobar"), mybytes(b"foobar") 109 self.assertTrue(hmac.compare_digest(a, b)) 110 a, b = mybytes(b"foobar"), b"foobar" 111 self.assertTrue(hmac.compare_digest(a, b)) 112 a, b = mybytes(b"foobar"), mybytes(b"foobaz") 113 self.assertFalse(hmac.compare_digest(a, b)) 114 115 116if __name__ == "__main__": 117 unittest.main()