Ϫf 6dZddlZddlmZddlmZddlmZddlmZGddejZ d Z eed Z Gd d ejZ eed ZGddejZy)z* Test cases for L{twisted.names.rfc1982}. N)datetime)partial SerialNumber)unittestceZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZy)SerialNumberTestsz$ Tests for L{SerialNumber}. cN|jtdjdy)zB L{SerialNumber.serialBits} has default value 32.  N assertEqualr _serialBitsselfs A/usr/lib/python3/dist-packages/twisted/names/test/test_rfc1982.pytest_serialBitsDefaultz(SerialNumberTests.test_serialBitsDefaults a44b9cR|jtddjdy)z L{SerialNumber.__init__} accepts a C{serialBits} argument whose value is assigned to L{SerialNumber.serialBits}. r  serialBitsNr rs rtest_serialBitsOverridez)SerialNumberTests.test_serialBitsOverrides! aA6BBAFrc P|jdttddy)ze L{SerialNumber.__repr__} returns a string containing number and serialBits. z'{r rN)rreprrrs r test_reprzSerialNumberTests.test_repr#s$  5 cb1 2 rcL|jttddy)zg L{SerialNumber.__str__} returns a string representation of the current value. r123N)rstrrrs rtest_strzSerialNumberTests.test_str-s \#./7rcL|jttddy)zi L{SerialNumber.__int__} returns an integer representation of the current value. rN)rintrrs rtest_intzSerialNumberTests.test_int4s \#./5rc|jttdttd|jttdttdy)z| L{SerialNumber.__hash__} allows L{SerialNumber} instances to be hashed for use as dictionary keys. r N)rhashrassertNotEqualrs r test_hashzSerialNumberTests.test_hash;sH l1o.\!_0EF Da14 Q3HIrcztdd}tdd}|jt|j|y)z L{SerialNumber._convertOther} raises L{TypeError} if the other SerialNumber instance has a different C{serialBits} value. rrrN)r assertRaises TypeError _convertOther)rs1s2s r#test_convertOtherSerialBitsMismatchz5SerialNumberTests.test_convertOtherSerialBitsMismatchCs4 ! * ! + )R%5%5r:rcL|jtdtdy)zK L{SerialNumber.__eq__} provides rich equality comparison. r Nrrrs rtest_eqzSerialNumberTests.test_eqMs a,q/:rc|jtdtk(|jtdj tt y)zy == comparison of L{SerialNumber} with a non-L{SerialNumber} instance returns L{NotImplemented}. r N) assertFalserobjectassertIs__eq__NotImplementedrs rtest_eqForeignTypez$SerialNumberTests.test_eqForeignTypeSs< aFH45 l1o,,VX6Grc|jtdtdk7|jtdtdy)zK L{SerialNumber.__ne__} provides rich equality comparison. r r&N)r6rr(rs rtest_nezSerialNumberTests.test_ne[s7 aLO;< LO\!_=rc|jtdtk7|jtdj tt y)zy != comparison of L{SerialNumber} with a non-L{SerialNumber} instance returns L{NotImplemented}. r N) assertTruerr7r8__ne__r:rs rtest_neForeignTypez$SerialNumberTests.test_neForeignTypebs:  Q6834 l1o,,VX6Grc|jtdtdk|jtdtdky)zE L{SerialNumber.__le__} provides rich <= comparison. r r&Nr?rrs rtest_lezSerialNumberTests.test_lej6  Q<?:;  Q<?:;rc2|jtdy)zs <= comparison of L{SerialNumber} with a non-L{SerialNumber} instance raises L{TypeError}. c.tdtkSNr rr7rrz6SerialNumberTests.test_leForeignType..v\!_-HrNr,r-rs rtest_leForeignTypez$SerialNumberTests.test_leForeignTypeq )%HIrc|jtdtdk\|jtdtdk\y)zE L{SerialNumber.__ge__} provides rich >= comparison. r r&NrCrs rtest_gezSerialNumberTests.test_gexrErc2|jtdy)zs >= comparison of L{SerialNumber} with a non-L{SerialNumber} instance raises L{TypeError}. c.tdtk\SrHrIrJrrrKz6SerialNumberTests.test_geForeignType..rLrNrMrs rtest_geForeignTypez$SerialNumberTests.test_geForeignTyperOrcP|jtdtdky)zD L{SerialNumber.__lt__} provides rich < comparison. r r&NrCrs rtest_ltzSerialNumberTests.test_lt  Q,q/9:rc2|jtdy)zr < comparison of L{SerialNumber} with a non-L{SerialNumber} instance raises L{TypeError}. c.tdtkSrHrIrJrrrKz6SerialNumberTests.test_ltForeignType..\!_vx-GrNrMrs rtest_ltForeignTypez$SerialNumberTests.test_ltForeignType )%GHrcP|jtdtdkDy)zD L{SerialNumber.__gt__} provides rich > comparison. r&r NrCrs rtest_gtzSerialNumberTests.test_gtrWrc2|jtdy)zt > comparison of L{SerialNumber} with a non-L{SerialNumber} instance raises L{TypeError}. c.tdtkDS)Nr&rIrJrrrKz6SerialNumberTests.test_gtForeignType..rZrNrMrs rtest_gtForeignTypez$SerialNumberTests.test_gtForeignTyper\rcd|jtdtdztdy)zX L{SerialNumber.__add__} allows L{SerialNumber} instances to be summed. r r&Nr3rs rtest_addzSerialNumberTests.test_adds% a<?:LOLrc2|jtdy)zn Addition of L{SerialNumber} with a non-L{SerialNumber} instance raises L{TypeError}. c.tdtzSrHrIrJrrrKz7SerialNumberTests.test_addForeignType..rZrNrMrs rtest_addForeignTypez%SerialNumberTests.test_addForeignTyper\rcbtdj|jtfdy)zp L{SerialNumber} cannot be added with other SerialNumber values larger than C{_maxAdd}. r c8tdtdzzSrHr)maxAddsrrKz:SerialNumberTests.test_addOutOfRangeHigh..s\!_|FQJ7O%OrN)r_maxAddr,ArithmeticError)rris @rtest_addOutOfRangeHighz(SerialNumberTests.test_addOutOfRangeHighs( a((  O rctd}|j|jzdz }|dz}|jt|t|kD|jt|tdy)z L{SerialNumber.__add__} returns a wrapped value when s1 plus the s2 would result in a value greater than the C{maxVal}. r rN)r _halfRingr?r)rsmaxVal maxValPlus1s r test_maxValzSerialNumberTests.test_maxValsa Oq{{*Q.qj   [1L4HHI k2LODrc`|jtdtjdy)z L{SerialNumber.fromRFC4034DateString} accepts a datetime string argument of the form 'YYYYMMDDhhmmss' and returns an L{SerialNumber} instance whose value is the unix timestamp corresponding to that UTC date. N20120101000000N)rrfromRFC4034DateStringrs rtest_fromRFC4034DateStringz,SerialNumberTests.test_fromRFC4034DateStrings)   $  . ./? @ rcV|jdtdjy)z L{DateSerialNumber.toRFC4034DateString} interprets the current value as a unix timestamp and returns a date string representation of that date. rurtNrrtoRFC4034DateStringrs rtest_toRFC4034DateStringz*SerialNumberTests.test_toRFC4034DateStrings%  l:6JJL rcV|jtdjdy)zq L{SerialNumber.toRFC4034DateString} stores 32bit timestamps relative to the UNIX epoch. r19700101000000Nryrs rtest_unixEpochz SerialNumberTests.test_unixEpochs" a<<>@PQrcV|jtdjdy)zI L{SerialNumber} wraps unix timestamps in the year 2106. 21060207062815Nryrs rtest_Y2106Problemz#SerialNumberTests.test_Y2106Problems# b)==?AQRrc tjtddddddj|j t dj |jtfdy ) zy L{SerialNumber} raises ArithmeticError when used to add dates more than 68 years in the future. ir rc8tdtdzzS)Nrr r) maxAddTimesrrKz5SerialNumberTests.test_Y2038Problem..s\!_|JQRN7S%SrN) calendartimegmr utctimetuplerrrjr,rk)rrs @rtest_Y2038Problemz#SerialNumberTests.test_Y2038Problems_ __XdAr1b!%D%Q%Q%ST    O # #  S rN) __name__ __module__ __qualname____doc__rrrr!r$r)r1r4r;r=rArDrNrQrTrVr[r^rarcrfrlrrrwr{r~rrrJrrr r s: G 86J;; H>H<J<J; I; IM I  E   RS  rr c|j||k(|j||k|j||k|j||kD|j||k\y)a A custom assertion for L{SerialNumber} values that cannot be meaningfully compared. "Note that there are some pairs of values s1 and s2 for which s1 is not equal to s2, but for which s1 is neither greater than, nor less than, s2. An attempt to use these ordering operators on such pairs of values produces an undefined result." @see: U{https://tools.ietf.org/html/rfc1982#section-3.2} @param testCase: The L{unittest.TestCase} on which to call assertion methods. @type testCase: L{unittest.TestCase} @param s1: The first value to compare. @type s1: L{SerialNumber} @param s2: The second value to compare. @type s2: L{SerialNumber} N)r6)testCaser/r0s rassertUndefinedComparisonrs^, r" r" b! b! r"rr&rc(eZdZdZdZdZdZdZy)SerialNumber2BitTestsaF Tests for correct answers to example calculations in RFC1982 5.1. The simplest meaningful serial number space has SERIAL_BITS == 2. In this space, the integers that make up the serial number space are 0, 1, 2, and 3. That is, 3 == 2^SERIAL_BITS - 1. https://tools.ietf.org/html/rfc1982#section-5.1 cR|jtddjdy)z In this space, the largest integer that it is meaningful to add to a sequence number is 2^(SERIAL_BITS - 1) - 1, or 1. rr&rr Nrrrjrs r test_maxaddz!SerialNumber2BitTests.test_maxadds! aA6>>Brc|jtdtdztd|jtdtdztd|jtdtdztd|jtdtdztdy)zN Then, as defined 0+1 == 1, 1+1 == 2, 2+1 == 3, and 3+1 == 0. rr r&rN)r serialNumber2rs rrczSerialNumber2BitTests.test_add&s q)M!,<NO q)M!,<NO q)M!,<NO q)M!,<NOrc4|jtdtdkD|jtdtdkD|jtdtdkD|jtdtdkDy)z: Further, 1 > 0, 2 > 1, 3 > 2, and 0 > 3. r rr&rN)r?rrs rr^zSerialNumber2BitTests.test_gt/sp  a(=+;;<  a(=+;;<  a(=+;;<  a(=+;; 0 or 0 > 2, and whether 1 > 3 or 3 > 1. r&rr rN)rrrs rtest_undefinedz$SerialNumber2BitTests.test_undefined8s\ "$ a(8-:JK!$ a(8-:JK!$ a(8-:JK!$ a(8-:JKrN)rrrrrrcr^rrJrrrrsCP=Lrrrc.eZdZdZdZdZdZdZdZy)SerialNumber8BitTestsa. Tests for correct answers to example calculations in RFC1982 5.2. Consider the case where SERIAL_BITS == 8. In this space the integers that make up the serial number space are 0, 1, 2, ... 254, 255. 255 == 2^SERIAL_BITS - 1. https://tools.ietf.org/html/rfc1982#section-5.2 cR|jtddjdy)z In this space, the largest integer that it is meaningful to add to a sequence number is 2^(SERIAL_BITS - 1) - 1, or 127. rrrNrrs rrz!SerialNumber8BitTests.test_maxaddPs! aA6>>Drc$|jtdtdztd|jtdtdztd|jtdtdztdy)z| Addition is as expected in this space, for example: 255+1 == 0, 100+100 == 200, and 200+100 == 44. r rd,N)r serialNumber8rs rrczSerialNumber8BitTests.test_addWsr s+mA.>> a@PQ s+mC.@@-PSBTU s+mC.@@-PRBSTrc|jtdtdkD|jtdtdkD|jtdtdkD|jtdtdkD|jtdtdkD|jtdtdkD|jtdtdkD|jtdtdkD|jtdtdkDy)z Comparison is more interesting, 1 > 0, 44 > 0, 100 > 0, 100 > 44, 200 > 100, 255 > 200, 0 > 255, 100 > 255, 0 > 200, and 44 > 200. r rrrrrNr?rrs rr^zSerialNumber8BitTests.test_gt`s  a(=+;;<  b)M!,<<=  c*]1-==>  c*]2->>?  c*]3-??@  c*]3-??@  c*]3-??@  a(=+==>  b)M#,>>?rc|jtdtdztdkD|jtdtdztdztdky)a Note that 100+100 > 100, but that (100+100)+100 < 100. Incrementing a serial number can cause it to become "smaller". Of course, incrementing by a smaller number will allow many more increments to be made before this occurs. However this is always something to be aware of, it can cause surprising errors, or be useful as it is the only defined way to actually cause a serial number to decrease. rNrrs rtest_surprisingAdditionz-SerialNumber8BitTests.test_surprisingAdditionos_  c*]3-??-PSBTTU  # s!3 3mC6H HC  ! rct|tdtdt|tdtdt|tdtdt|tdtdy ) z The pairs of values 0 and 128, 1 and 129, 2 and 130, etc, to 127 and 255 are not equal, but in each pair, neither number is defined as being greater than, or less than, the other. rr r&rrN)rrrs rrz$SerialNumber8BitTests.test_undefined~s\ "$ a(8-:LM!$ a(8-:LM!$ a(8-:LM!$ c(:M#rs| /"` ))` F#: 3 +LH--+L\ 3 BPH--BPr