1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
#
# This file is part of pyasn1 software.
#
# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
# License: http://snmplabs.com/pyasn1/license.html
#
import datetime
import pickle
import sys
from copy import deepcopy
try:
import unittest2 as unittest
except ImportError:
import unittest
from tests.base import BaseTestCase
from pyasn1.type import useful
class FixedOffset(datetime.tzinfo):
def __init__(self, offset, name):
self.__offset = datetime.timedelta(minutes=offset)
self.__name = name
def utcoffset(self, dt):
return self.__offset
def tzname(self, dt):
return self.__name
def dst(self, dt):
return datetime.timedelta(0)
UTC = FixedOffset(0, "UTC")
UTC2 = FixedOffset(120, "UTC")
class ObjectDescriptorTestCase(BaseTestCase):
pass
class GeneralizedTimeTestCase(BaseTestCase):
def testFromDateTime(self):
assert (
useful.GeneralizedTime.fromDateTime(
datetime.datetime(2017, 7, 11, 0, 1, 2, 3000, tzinfo=UTC)
)
== "20170711000102.3Z"
)
def testToDateTime0(self):
assert (
datetime.datetime(2017, 7, 11, 0, 1, 2)
== useful.GeneralizedTime("20170711000102").asDateTime
)
def testToDateTime1(self):
assert (
datetime.datetime(2017, 7, 11, 0, 1, 2, tzinfo=UTC)
== useful.GeneralizedTime("20170711000102Z").asDateTime
)
def testToDateTime2(self):
assert (
datetime.datetime(2017, 7, 11, 0, 1, 2, 3000, tzinfo=UTC)
== useful.GeneralizedTime("20170711000102.3Z").asDateTime
)
def testToDateTime3(self):
assert (
datetime.datetime(2017, 7, 11, 0, 1, 2, 3000, tzinfo=UTC)
== useful.GeneralizedTime("20170711000102,3Z").asDateTime
)
def testToDateTime4(self):
assert (
datetime.datetime(2017, 7, 11, 0, 1, 2, 3000, tzinfo=UTC)
== useful.GeneralizedTime("20170711000102.3+0000").asDateTime
)
def testToDateTime5(self):
assert (
datetime.datetime(2017, 7, 11, 0, 1, 2, 3000, tzinfo=UTC2)
== useful.GeneralizedTime("20170711000102.3+0200").asDateTime
)
def testToDateTime6(self):
assert (
datetime.datetime(2017, 7, 11, 0, 1, 2, 3000, tzinfo=UTC2)
== useful.GeneralizedTime("20170711000102.3+02").asDateTime
)
def testToDateTime7(self):
assert (
datetime.datetime(2017, 7, 11, 0, 1)
== useful.GeneralizedTime("201707110001").asDateTime
)
def testToDateTime8(self):
assert (
datetime.datetime(2017, 7, 11, 0)
== useful.GeneralizedTime("2017071100").asDateTime
)
def testCopy(self):
dt = useful.GeneralizedTime("20170916234254+0130").asDateTime
assert dt == deepcopy(dt)
class GeneralizedTimePicklingTestCase(unittest.TestCase):
def testSchemaPickling(self):
old_asn1 = useful.GeneralizedTime()
serialised = pickle.dumps(old_asn1)
assert serialised
new_asn1 = pickle.loads(serialised)
assert type(new_asn1) == useful.GeneralizedTime
assert old_asn1.isSameTypeWith(new_asn1)
def testValuePickling(self):
old_asn1 = useful.GeneralizedTime("20170916234254+0130")
serialised = pickle.dumps(old_asn1)
assert serialised
new_asn1 = pickle.loads(serialised)
assert new_asn1 == old_asn1
class UTCTimeTestCase(BaseTestCase):
def testFromDateTime(self):
assert (
useful.UTCTime.fromDateTime(
datetime.datetime(2017, 7, 11, 0, 1, 2, tzinfo=UTC)
)
== "170711000102Z"
)
def testToDateTime0(self):
assert (
datetime.datetime(2017, 7, 11, 0, 1, 2)
== useful.UTCTime("170711000102").asDateTime
)
def testToDateTime1(self):
assert (
datetime.datetime(2017, 7, 11, 0, 1, 2, tzinfo=UTC)
== useful.UTCTime("170711000102Z").asDateTime
)
def testToDateTime2(self):
assert (
datetime.datetime(2017, 7, 11, 0, 1, 2, tzinfo=UTC)
== useful.UTCTime("170711000102+0000").asDateTime
)
def testToDateTime3(self):
assert (
datetime.datetime(2017, 7, 11, 0, 1, 2, tzinfo=UTC2)
== useful.UTCTime("170711000102+0200").asDateTime
)
def testToDateTime4(self):
assert (
datetime.datetime(2017, 7, 11, 0, 1)
== useful.UTCTime("1707110001").asDateTime
)
class UTCTimePicklingTestCase(unittest.TestCase):
def testSchemaPickling(self):
old_asn1 = useful.UTCTime()
serialised = pickle.dumps(old_asn1)
assert serialised
new_asn1 = pickle.loads(serialised)
assert type(new_asn1) == useful.UTCTime
assert old_asn1.isSameTypeWith(new_asn1)
def testValuePickling(self):
old_asn1 = useful.UTCTime("170711000102")
serialised = pickle.dumps(old_asn1)
assert serialised
new_asn1 = pickle.loads(serialised)
assert new_asn1 == old_asn1
suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
if __name__ == "__main__":
unittest.TextTestRunner(verbosity=2).run(suite)
|