1# Copyright (C) Jean-Paul Calderone 2# Copyright (C) Twisted Matrix Laboratories. 3# See LICENSE for details. 4""" 5Helpers for the OpenSSL test suite, largely copied from 6U{Twisted<http://twistedmatrix.com/>}. 7""" 8 9from six import PY2 10 11 12# This is the UTF-8 encoding of the SNOWMAN unicode code point. 13NON_ASCII = b"\xe2\x98\x83".decode("utf-8") 14 15 16def is_consistent_type(theType, name, *constructionArgs): 17 """ 18 Perform various assertions about *theType* to ensure that it is a 19 well-defined type. This is useful for extension types, where it's 20 pretty easy to do something wacky. If something about the type is 21 unusual, an exception will be raised. 22 23 :param theType: The type object about which to make assertions. 24 :param name: A string giving the name of the type. 25 :param constructionArgs: Positional arguments to use with 26 *theType* to create an instance of it. 27 """ 28 assert theType.__name__ == name 29 assert isinstance(theType, type) 30 instance = theType(*constructionArgs) 31 assert type(instance) is theType 32 return True 33 34 35class EqualityTestsMixin(object): 36 """ 37 A mixin defining tests for the standard implementation of C{==} and C{!=}. 38 """ 39 40 def anInstance(self): 41 """ 42 Return an instance of the class under test. Each call to this method 43 must return a different object. All objects returned must be equal to 44 each other. 45 """ 46 raise NotImplementedError() 47 48 def anotherInstance(self): 49 """ 50 Return an instance of the class under test. Each call to this method 51 must return a different object. The objects must not be equal to the 52 objects returned by C{anInstance}. They may or may not be equal to 53 each other (they will not be compared against each other). 54 """ 55 raise NotImplementedError() 56 57 def test_identicalEq(self): 58 """ 59 An object compares equal to itself using the C{==} operator. 60 """ 61 o = self.anInstance() 62 assert o == o 63 64 def test_identicalNe(self): 65 """ 66 An object doesn't compare not equal to itself using the C{!=} operator. 67 """ 68 o = self.anInstance() 69 assert not (o != o) 70 71 def test_sameEq(self): 72 """ 73 Two objects that are equal to each other compare equal to each other 74 using the C{==} operator. 75 """ 76 a = self.anInstance() 77 b = self.anInstance() 78 assert a == b 79 80 def test_sameNe(self): 81 """ 82 Two objects that are equal to each other do not compare not equal to 83 each other using the C{!=} operator. 84 """ 85 a = self.anInstance() 86 b = self.anInstance() 87 assert not (a != b) 88 89 def test_differentEq(self): 90 """ 91 Two objects that are not equal to each other do not compare equal to 92 each other using the C{==} operator. 93 """ 94 a = self.anInstance() 95 b = self.anotherInstance() 96 assert not (a == b) 97 98 def test_differentNe(self): 99 """ 100 Two objects that are not equal to each other compare not equal to each 101 other using the C{!=} operator. 102 """ 103 a = self.anInstance() 104 b = self.anotherInstance() 105 assert a != b 106 107 def test_anotherTypeEq(self): 108 """ 109 The object does not compare equal to an object of an unrelated type 110 (which does not implement the comparison) using the C{==} operator. 111 """ 112 a = self.anInstance() 113 b = object() 114 assert not (a == b) 115 116 def test_anotherTypeNe(self): 117 """ 118 The object compares not equal to an object of an unrelated type (which 119 does not implement the comparison) using the C{!=} operator. 120 """ 121 a = self.anInstance() 122 b = object() 123 assert a != b 124 125 def test_delegatedEq(self): 126 """ 127 The result of comparison using C{==} is delegated to the right-hand 128 operand if it is of an unrelated type. 129 """ 130 131 class Delegate(object): 132 def __eq__(self, other): 133 # Do something crazy and obvious. 134 return [self] 135 136 a = self.anInstance() 137 b = Delegate() 138 assert (a == b) == [b] 139 140 def test_delegateNe(self): 141 """ 142 The result of comparison using C{!=} is delegated to the right-hand 143 operand if it is of an unrelated type. 144 """ 145 146 class Delegate(object): 147 def __ne__(self, other): 148 # Do something crazy and obvious. 149 return [self] 150 151 a = self.anInstance() 152 b = Delegate() 153 assert (a != b) == [b] 154 155 156# The type name expected in warnings about using the wrong string type. 157if PY2: 158 WARNING_TYPE_EXPECTED = "unicode" 159else: 160 WARNING_TYPE_EXPECTED = "str" 161