class BaseTestCase(unittest.TestCase):

    def setUp(self):
        debug.setLogger(debug.Debug('all', printer=lambda *x: None))

    def tearDown(self):
        debug.setLogger(None) class Integer(base.SimpleAsn1Type):
class Boolean(Integer):
class SizedInteger(SizedIntegerBase):
class BitString(base.SimpleAsn1Type):
class OctetString(base.SimpleAsn1Type):
class Null(OctetString):
class ObjectIdentifier(base.SimpleAsn1Type):
class Real(base.SimpleAsn1Type):
class Enumerated(Integer):
class SequenceOfAndSetOfBase(base.ConstructedAsn1Type):
class SequenceOf(SequenceOfAndSetOfBase):
class SetOf(SequenceOfAndSetOfBase):
class SequenceAndSetBase(base.ConstructedAsn1Type):
    class DynamicNames(object):
class Sequence(SequenceAndSetBase):
class Set(SequenceAndSetBase):
class Choice(Set):
class Any(OctetString):
    def __init__(self, value=noValue, **kwargs):
    def __and__(self, value):
    def __rand__(self, value):
    def __or__(self, value):
    def __ror__(self, value):
    def __xor__(self, value):
    def __rxor__(self, value):
    def __lshift__(self, value):
    def __rshift__(self, value):
    def __add__(self, value):
    def __radd__(self, value):
    def __sub__(self, value):
    def __rsub__(self, value):
    def __mul__(self, value):
    def __rmul__(self, value):
    def __mod__(self, value):
    def __rmod__(self, value):
    def __pow__(self, value, modulo=None):
    def __rpow__(self, value):
    def __floordiv__(self, value):
    def __rfloordiv__(self, value):
        def __div__(self, value):
        def __rdiv__(self, value):
        def __truediv__(self, value):
        def __rtruediv__(self, value):
        def __divmod__(self, value):
        def __rdivmod__(self, value):
    def __int__(self):
        def __long__(self):
    def __float__(self):
    def __abs__(self):
    def __index__(self):
    def __pos__(self):
    def __neg__(self):
    def __invert__(self):
    def __round__(self, n=0):
    def __floor__(self):
    def __ceil__(self):
    def __trunc__(self):
    def __lt__(self, value):
    def __le__(self, value):
    def __eq__(self, value):
    def __ne__(self, value):
    def __gt__(self, value):
    def __ge__(self, value):
    def prettyIn(self, value):
    def prettyOut(self, value):
    def getNamedValues(self):
    def setBitLength(self, bitLength):
    def __len__(self):
    def __init__(self, value=noValue, **kwargs):
    def __str__(self):
    def __eq__(self, other):
    def __ne__(self, other):
    def __lt__(self, other):
    def __le__(self, other):
    def __gt__(self, other):
    def __ge__(self, other):
    def __len__(self):
    def __getitem__(self, i):
    def __iter__(self):
    def __reversed__(self):
    def __add__(self, value):
    def __radd__(self, value):
    def __mul__(self, value):
    def __rmul__(self, value):
    def __lshift__(self, count):
    def __rshift__(self, count):
    def __int__(self):
    def __float__(self):
        def __long__(self):
    def asNumbers(self):
    def asOctets(self):
    def asInteger(self):
    def asBinary(self):
    def fromHexString(cls, value, internalFormat=False, prepend=None):
    def fromBinaryString(cls, value, internalFormat=False, prepend=None):
    def fromOctetString(cls, value, internalFormat=False, prepend=None, padding=0):
    def prettyIn(self, value):
    def __init__(self, value=noValue, **kwargs):
        def prettyIn(self, value):
        def __str__(self):
        def __unicode__(self):
        def asOctets(self):
        def asNumbers(self):
        def prettyIn(self, value):
        def __str__(self):
        def __bytes__(self):
        def asOctets(self):
        def asNumbers(self):
    def prettyOut(self, value):
    def prettyPrint(self, scope=0):
    def fromBinaryString(value):
    def fromHexString(value):
    def __len__(self):
    def __getitem__(self, i):
    def __iter__(self):
    def __contains__(self, value):
    def __add__(self, value):
    def __radd__(self, value):
    def __mul__(self, value):
    def __rmul__(self, value):
    def __int__(self):
    def __float__(self):
    def __reversed__(self):
    def prettyIn(self, value):
    def __add__(self, other):
    def __radd__(self, other):
    def asTuple(self):
    def __len__(self):
    def __getitem__(self, i):
    def __iter__(self):
    def __contains__(self, value):
    def index(self, suboid):
    def isPrefixOf(self, other):
    def prettyIn(self, value):
    def prettyOut(self, value):
    def __normalizeBase10(value):
    def prettyIn(self, value):
    def prettyPrint(self, scope=0):
    def isPlusInf(self):
    def isMinusInf(self):
    def isInf(self):
    def __add__(self, value):
    def __radd__(self, value):
    def __mul__(self, value):
    def __rmul__(self, value):
    def __sub__(self, value):
    def __rsub__(self, value):
    def __mod__(self, value):
    def __rmod__(self, value):
    def __pow__(self, value, modulo=None):
    def __rpow__(self, value):
        def __div__(self, value):
        def __rdiv__(self, value):
        def __truediv__(self, value):
        def __rtruediv__(self, value):
        def __divmod__(self, value):
        def __rdivmod__(self, value):
    def __int__(self):
        def __long__(self):
    def __float__(self):
    def __abs__(self):
    def __pos__(self):
    def __neg__(self):
    def __round__(self, n=0):
    def __floor__(self):
    def __ceil__(self):
    def __trunc__(self):
    def __lt__(self, value):
    def __le__(self, value):
    def __eq__(self, value):
    def __ne__(self, value):
    def __gt__(self, value):
    def __ge__(self, value):
        def __nonzero__(self):
        def __bool__(self):
    def __getitem__(self, idx):
    def isPlusInfinity(self):
    def isMinusInfinity(self):
    def isInfinity(self):
    def __init__(self, *args, **kwargs):
    def __getitem__(self, idx):
    def __setitem__(self, idx, value):
    def append(self, value):
    def count(self, value):
    def extend(self, values):
    def index(self, value, start=0, stop=None):
    def reverse(self):
    def sort(self, key=None, reverse=False):
    def __len__(self):
    def __iter__(self):
    def _cloneComponentValues(self, myClone, cloneValueFlag):
    def getComponentByPosition(self, idx, default=noValue, instantiate=True):
    def setComponentByPosition(self, idx, value=noValue,
                               verifyConstraints=True,
                               matchTags=True,
                               matchConstraints=True):
    def componentTagMap(self):
    def components(self):
    def clear(self):
    def reset(self):
    def prettyPrint(self, scope=0):
    def prettyPrintType(self, scope=0):
    def isValue(self):
    def isInconsistent(self):
        def __init__(self):
        def __len__(self):
        def __contains__(self, item):
        def __iter__(self):
        def __getitem__(self, item):
        def getNameByPosition(self, idx):
        def getPositionByName(self, name):
        def addField(self, idx):
    def __init__(self, **kwargs):
    def __getitem__(self, idx):
    def __setitem__(self, idx, value):
    def __contains__(self, key):
    def __len__(self):
    def __iter__(self):
    def values(self):
    def keys(self):
    def items(self):
    def update(self, *iterValue, **mappingValue):
    def clear(self):
    def reset(self):
    def components(self):
    def _cloneComponentValues(self, myClone, cloneValueFlag):
    def getComponentByName(self, name, default=noValue, instantiate=True):
    def setComponentByName(self, name, value=noValue,
                           verifyConstraints=True,
                           matchTags=True,
                           matchConstraints=True):
    def getComponentByPosition(self, idx, default=noValue, instantiate=True):
    def setComponentByPosition(self, idx, value=noValue,
                               verifyConstraints=True,
                               matchTags=True,
                               matchConstraints=True):
    def isValue(self):
    def isInconsistent(self):
    def prettyPrint(self, scope=0):
    def prettyPrintType(self, scope=0):
    def setDefaultComponents(self):
    def getComponentType(self):
    def getNameByPosition(self, idx):
    def getComponentTagMapNearPosition(self, idx):
    def getComponentPositionNearType(self, tagSet, idx):
    def getComponent(self, innerFlag=False):
    def getComponentByType(self, tagSet, default=noValue,
                           instantiate=True, innerFlag=False):
    def setComponentByType(self, tagSet, value=noValue,
                           verifyConstraints=True,
                           matchTags=True,
                           matchConstraints=True,
                           innerFlag=False):
    def componentTagMap(self):
    def __eq__(self, other):
    def __ne__(self, other):
    def __lt__(self, other):
    def __le__(self, other):
    def __gt__(self, other):
    def __ge__(self, other):
        def __nonzero__(self):
        def __bool__(self):
    def __len__(self):
    def __contains__(self, key):
    def __iter__(self):
    def values(self):
    def keys(self):
    def items(self):
    def checkConsistency(self):
    def _cloneComponentValues(self, myClone, cloneValueFlag):
    def getComponentByPosition(self, idx, default=noValue, instantiate=True):
    def setComponentByPosition(self, idx, value=noValue,
                               verifyConstraints=True,
                               matchTags=True,
                               matchConstraints=True):
    def effectiveTagSet(self):
    def tagMap(self):
    def getComponent(self, innerFlag=False):
    def getName(self, innerFlag=False):
    def isValue(self):
    def clear(self):
    def getMinTagSet(self):
    def tagMap(self): class Tag(object):
class TagSet(object):
    def __init__(self, tagClass, tagFormat, tagId):
    def __repr__(self):
    def __eq__(self, other):
    def __ne__(self, other):
    def __lt__(self, other):
    def __le__(self, other):
    def __gt__(self, other):
    def __ge__(self, other):
    def __hash__(self):
    def __getitem__(self, idx):
    def __iter__(self):
    def __and__(self, otherTag):
    def __or__(self, otherTag):
    def tagClass(self):
    def tagFormat(self):
    def tagId(self):
    def __init__(self, baseTag=(), *superTags):
    def __repr__(self):
    def __add__(self, superTag):
    def __radd__(self, superTag):
    def __getitem__(self, i):
    def __eq__(self, other):
    def __ne__(self, other):
    def __lt__(self, other):
    def __le__(self, other):
    def __gt__(self, other):
    def __ge__(self, other):
    def __hash__(self):
    def __len__(self):
    def baseTag(self):
    def superTags(self):
    def tagExplicitly(self, superTag):
    def tagImplicitly(self, superTag):
    def isSuperTagSetOf(self, tagSet):
    def getBaseTag(self):
def initTagSet(tag): class AbstractConstraint(object):
class SingleValueConstraint(AbstractConstraint):
class ContainedSubtypeConstraint(AbstractConstraint):
class ValueRangeConstraint(AbstractConstraint):
class ValueSizeConstraint(ValueRangeConstraint):
class PermittedAlphabetConstraint(SingleValueConstraint):
class ComponentPresentConstraint(AbstractConstraint):
class ComponentAbsentConstraint(AbstractConstraint):
class WithComponentsConstraint(AbstractConstraint):
class InnerTypeConstraint(AbstractConstraint):
class ConstraintsExclusion(AbstractConstraint):
class AbstractConstraintSet(AbstractConstraint):
class ConstraintsIntersection(AbstractConstraintSet):
class ConstraintsUnion(AbstractConstraintSet):
    def __init__(self, *values):
    def __call__(self, value, idx=None):
    def __repr__(self):
    def __eq__(self, other):
    def __ne__(self, other):
    def __lt__(self, other):
    def __le__(self, other):
    def __gt__(self, other):
    def __ge__(self, other):
        def __nonzero__(self):
        def __bool__(self):
    def __hash__(self):
    def _setValues(self, values):
    def _testValue(self, value, idx):
    def getValueMap(self):
    def isSuperTypeOf(self, otherConstraint):
    def isSubTypeOf(self, otherConstraint):
    def _setValues(self, values):
    def _testValue(self, value, idx):
    def __contains__(self, item):
    def __iter__(self):
    def __sub__(self, constraint):
    def __add__(self, constraint):
    def __sub__(self, constraint):
    def _testValue(self, value, idx):
    def _testValue(self, value, idx):
    def _setValues(self, values):
    def _testValue(self, value, idx):
    def _setValues(self, values):
    def _testValue(self, value, idx):
    def _setValues(self, values):
    def _testValue(self, value, idx):
    def _setValues(self, values):
    def _testValue(self, value, idx):
    def _testValue(self, value, idx):
    def _setValues(self, values):
    def _testValue(self, value, idx):
    def _setValues(self, values):
    def _testValue(self, value, idx):
    def _setValues(self, values):
    def __getitem__(self, idx):
    def __iter__(self):
    def __add__(self, value):
    def __radd__(self, value):
    def __len__(self):
    def _setValues(self, values):
    def _testValue(self, value, idx):
    def _testValue(self, value, idx): class NamedType(object):
class OptionalNamedType(NamedType):
class DefaultedNamedType(NamedType):
class NamedTypes(object):
    class PostponedError(object):
    def __init__(self, name, asn1Object, openType=None):
    def __repr__(self):
    def __eq__(self, other):
    def __ne__(self, other):
    def __lt__(self, other):
    def __le__(self, other):
    def __gt__(self, other):
    def __ge__(self, other):
    def __hash__(self):
    def __getitem__(self, idx):
    def __iter__(self):
    def name(self):
    def asn1Object(self):
    def openType(self):
    def getName(self):
    def getType(self):
    def __init__(self, *namedTypes, **kwargs):
    def __repr__(self):
    def __eq__(self, other):
    def __ne__(self, other):
    def __lt__(self, other):
    def __le__(self, other):
    def __gt__(self, other):
    def __ge__(self, other):
    def __hash__(self):
    def __getitem__(self, idx):
    def __contains__(self, key):
    def __iter__(self):
        def __nonzero__(self):
        def __bool__(self):
    def __len__(self):
    def values(self):
    def keys(self):
    def items(self):
    def clone(self):
        def __init__(self, errorMsg):
        def __getitem__(self, item):
    def __computeTagToPosMap(self):
    def __computeNameToPosMap(self):
    def __computeAmbiguousTypes(self):
    def getTypeByPosition(self, idx):
    def getPositionByType(self, tagSet):
    def getNameByPosition(self, idx):
    def getPositionByName(self, name):
    def getTagMapNearPosition(self, idx):
    def getPositionNearType(self, tagSet, idx):
    def __computeMinTagSet(self):
    def minTagSet(self):
    def __computeTagMaps(self, unique):
    def tagMap(self):
    def tagMapUnique(self):
    def hasOptionalOrDefault(self):
    def hasOpenTypes(self):
    def namedTypes(self):
    def requiredComponents(self): class NamedValues(object):
    def __init__(self, *args, **kwargs):
    def __repr__(self):
    def __eq__(self, other):
    def __ne__(self, other):
    def __lt__(self, other):
    def __le__(self, other):
    def __gt__(self, other):
    def __ge__(self, other):
    def __hash__(self):
    def __getitem__(self, key):
    def __len__(self):
    def __contains__(self, key):
    def __iter__(self):
    def values(self):
    def keys(self):
    def items(self):
    def __add__(self, namedValues):
    def clone(self, *args, **kwargs):
    def getName(self, value):
    def getValue(self, name):
    def getValues(self, *names): class ValueConstraintError(PyAsn1Error): class PyAsn1Error(Exception):
    """Base pyasn1 exception

    `PyAsn1Error` is the base exception class (based on
    :class:`Exception`) that represents all possible ASN.1 related
    errors.

    Parameters
    ----------
    args:
        Opaque positional parameters

    Keyword Args
    ------------
    kwargs:
        Opaque keyword parameters

    """
    def __init__(self, *args, **kwargs):
        self._args = args
        self._kwargs = kwargs

    @property
    def context(self):
        """Return exception context

        When exception object is created, the caller can supply some opaque
        context for the upper layers to better understand the cause of the
        exception.

        Returns
        -------
        : :py:class:`dict`
            Dict holding context specific data
        """
        return self._kwargs.get('context', {}) class PyAsn1UnicodeEncodeError(PyAsn1UnicodeError, UnicodeEncodeError):
    """Unicode text encoding error

    The `PyAsn1UnicodeEncodeError` exception represents a failure to
    serialize unicode text.

    Apart from inheriting from :class:`PyAsn1UnicodeError`, it also inherits
    from :class:`UnicodeEncodeError` to help the caller catching
    unicode-related errors.
    """ class PyAsn1UnicodeDecodeError(PyAsn1UnicodeError, UnicodeDecodeError):
    """Unicode text decoding error

    The `PyAsn1UnicodeDecodeError` exception represents a failure to
    deserialize unicode text.

    Apart from inheriting from :class:`PyAsn1UnicodeError`, it also inherits
    from :class:`UnicodeDecodeError` to help the caller catching unicode-related
    errors.
    """
import math
import pickle
import sys
import unittest
from tests.base import BaseTestCase
from pyasn1.type import univ
from pyasn1.type import tag
from pyasn1.type import constraint
from pyasn1.type import namedtype
from pyasn1.type import namedval
from pyasn1.type import error
from pyasn1.compat.octets import str2octs, ints2octs, octs2ints, octs2str
from pyasn1.error import PyAsn1Error
from pyasn1.error import PyAsn1UnicodeEncodeError, PyAsn1UnicodeDecodeError

    def testSchemaPickling(self):
        old_asn1 = univ.Boolean()
        serialised = pickle.dumps(old_asn1)
        assert serialised
        new_asn1 = pickle.loads(serialised)
        assert type(new_asn1) == univ.Boolean
        assert old_asn1.isSameTypeWith(new_asn1)

    def testValuePickling(self):
        old_asn1 = univ.Boolean(True)
        serialised = pickle.dumps(old_asn1)
        assert serialised
        new_asn1 = pickle.loads(serialised)
        assert new_asn1 == True


class BitStringTestCase(BaseTestCase):
    def setUp(self):
        BaseTestCase.setUp(self)

        self.b = univ.BitString(
            namedValues=namedval.NamedValues(('Active', 0), ('Urgent', 1))
        )

    def testBinDefault(self):

        class BinDefault(univ.BitString):
            defaultBinValue = '1010100110001010'

        assert BinDefault() == univ.BitString(binValue='1010100110001010')

    def testHexDefault(self):

        class HexDefault(univ.BitString):
            defaultHexValue = 'A98A'

        assert HexDefault() == univ.BitString(hexValue='A98A')

    def testSet(self):
        assert self.b.clone('Active') == (1,)
        assert self.b.clone('Urgent') == (0, 1)
        assert self.b.clone('Urgent, Active') == (1, 1)
        assert self.b.clone("'1010100110001010'B") == (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0)
        assert self.b.clone("'A98A'H") == (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0)
        assert self.b.clone(binValue='1010100110001010') == (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0)
        assert self.b.clone(hexValue='A98A') == (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0)
        assert self.b.clone('1010100110001010') == (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0)
        assert self.b.clone((1, 0, 1)) == (1, 0, 1)

    def testStr(self):
        assert str(self.b.clone('Urgent')) == '01'

    def testRepr(self):
        assert 'BitString' in repr(self.b.clone('Urgent,Active'))

    def testTag(self):
        assert univ.BitString().tagSet == tag.TagSet(
            (),
            tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x03)
        )

    def testLen(self):
        assert len(self.b.clone("'A98A'H")) == 16

    def testGetItem(self):
        assert self.b.clone("'A98A'H")[0] == 1
        assert self.b.clone("'A98A'H")[1] == 0
        assert self.b.clone("'A98A'H")[2] == 1

    def testReverse(self):
        assert list(reversed(univ.BitString([0, 0, 1]))) == list(univ.BitString([1, 0, 0]))

    def testAsOctets(self):
        assert self.b.clone(hexValue='A98A').asOctets() == ints2octs((0xa9, 0x8a)), 'testAsOctets() fails'

    def testAsInts(self):
        assert self.b.clone(hexValue='A98A').asNumbers() == (0xa9, 0x8a), 'testAsNumbers() fails'

    def testMultipleOfEightPadding(self):
        assert self.b.clone((1, 0, 1)).asNumbers() == (5,)

    def testAsInteger(self):
        assert self.b.clone('11000000011001').asInteger() == 12313
        assert self.b.clone('1100110011011111').asInteger() == 52447

    def testStaticDef(self):

        class BitString(univ.BitString):
            pass

        assert BitString('11000000011001').asInteger() == 12313


class BitStringPicklingTestCase(unittest.TestCase):

    def testSchemaPickling(self):
        old_asn1 = univ.BitString()
        serialised = pickle.dumps(old_asn1)
        assert serialised
        new_asn1 = pickle.loads(serialised)
        assert type(new_asn1) == univ.BitString
        assert old_asn1.isSameTypeWith(new_asn1)

    def testValuePickling(self):
        old_asn1 = univ.BitString((1, 0, 1, 0))
        serialised = pickle.dumps(old_asn1)
        assert serialised
        new_asn1 = pickle.loads(serialised)
        assert new_asn1 == (1, 0, 1, 0)


class OctetStringWithUnicodeMixIn(object):

    initializer = ()
    encoding = 'us-ascii'

    def setUp(self):
        self.pythonString = ints2octs(self.initializer).decode(self.encoding)
        self.encodedPythonString = self.pythonString.encode(self.encoding)

        self.numbersString = tuple(octs2ints(self.encodedPythonString))