Esempio n. 1
0
    def test_misc(self):
        # Creating sub-OIDs, comparison, hashing, etc.
        self.assertEqual(str(Oid((2, 5, 4, 3))), '2.5.4.3')
        pkcs = Oid('1.2.840.113549.1')
        pkcs1 = pkcs + (1, )
        self.assertEqual(repr(pkcs1), 'Oid((1, 2, 840, 113549, 1, 1))')
        pkcs_ = Oid(b'\x06\x07*\x86H\x86\xf7\x0d\x01')
        self.assertEqual(pkcs, pkcs_)
        self.assertLess(pkcs_, pkcs1)
        self.assertGreater(pkcs1, pkcs)

        s = set()
        self.assertNotIn(pkcs, s)
        self.assertNotIn(pkcs_, s)
        self.assertNotIn(pkcs1, s)
        s.add(pkcs)
        self.assertIn(pkcs, s)
        self.assertIn(pkcs_, s)
        self.assertNotIn(pkcs1, s)
        s.add(pkcs_)
        self.assertEqual(len(s), 1)
        s.add(pkcs1)
        self.assertEqual(len(s), 2)
        self.assertIn(pkcs, s)
        self.assertIn(pkcs1, s)
Esempio n. 2
0
    def test_seqs(self):

        thing = derlite.Structure(
            (int, datetime.datetime, derlite.SequenceOf(Oid), bytes))

        self.roundtrip((1000, datetime.datetime(
            1969, 7, 20, 10, 56), [Oid((1, k, 10))
                                   for k in (0, 3, 9)], b'some more stuff'),
                       thing, '3033'
                       '020203E8'
                       '180E' + bytes.hex(b'19690720105600') + '300C'
                       '0602280A 06022B0A 0602310A'
                       '040F' + bytes.hex(b'some more stuff'))

        self.roundtrip((-1, datetime.datetime(1234, 4, 5, 6, 7, 8), [], b''),
                       thing, '3017'
                       '0201FF'
                       '180E' + bytes.hex(b'12340405060708') + '3000'
                       '0400')

        thing = (int, bool, bytes)
        self.roundtrip((-129, False, b''), thing, '0202FF7F 010100 0400')

        thing = derlite.ExplicitlyTagged(
            Tag(16, True, Tag.Application),
            derlite.Structure((derlite.SequenceOf(int), Oid)))
        self.roundtrip(([1, 2, 3], Oid((1, 2, 3))), thing, '7011 '
                       '300F '
                       '3009 020101 020102 020103'
                       '06022A03')
Esempio n. 3
0
    def test_parses(self):
        self.assertEqual(Oid('2.5.4.3').as_der(), b'\x06\x03\x55\x04\x03')
        self.assertEqual(
            Oid('{ 1.2.840.10040.4.1 }').as_der(),
            b'\x06\x07\x2A\x86\x48\xCE\x38\x04\x01')
        self.assertEqual(Oid([3, 9]).arcs(), (3, 9))

        self.assertRaises(Exception, Oid, 42)
        self.assertRaises(derlite.DecodeError, Oid, b'\x03\x03\x55\x04\x03')
        self.assertRaises(derlite.DecodeError, lambda x: Oid(x).arcs(),
                          b'\x06\x02\x2A\x03\x01')
Esempio n. 4
0
    def test_bad_values(self):

        oid = Oid('1.2.3')
        self.assertRaises(TypeError, lambda: oid + (5, 6, "bananas", 7))

        # There's no encoding for a 0- or 1-element OID.
        self.assertRaises(ValueError, lambda a: Oid(a).as_der(), (1, ))
        self.assertRaises(ValueError, lambda a: Oid(a).as_der(), ())

        # The first two elements have constrained ranges because
        # of the way they're encoded.
        self.assertRaises(ValueError, lambda a: Oid(a).as_der(),
                          (1000, 2, 3, 4))
        self.assertRaises(ValueError, lambda a: Oid(a).as_der(), (1, 50, 3))
Esempio n. 5
0
    def test_set2(self):
        # More set tests
        enc = derlite.Encoder()
        enc.write_set(bytes([1, b]) for b in (4, 2, 8))
        dec = self.around(enc, '310C 04020102 04020104 04020108')

        enc = derlite.Encoder()
        enc.write_set([None, False, [], Oid((1, 10)), True])
        dec = self.around(enc, '310D 010100 0101FF 0500 060132 3000')
Esempio n. 6
0
    def test_optionals(self):
        thing = derlite.Structure(
            (int, derlite.Optional(derlite.ExplicitlyTagged(2, int))))
        self.roundtrip((3, None), thing, '3003 020103')
        self.roundtrip((3, 5), thing, '3008 020103 A203 020105')
        self.roundtrip((0, 0), thing, '3008 020100 A203 020100')

        thing = derlite.Structure(
            (derlite.Optional(derlite.ExplicitlyTagged(31, bytes)),
             derlite.Optional(int)))
        self.roundtrip((None, None), thing, '3000')
        self.roundtrip((None, 1), thing, '3003 020101')
        self.roundtrip((b'A', None), thing, '3006 BF1F03 040141')
        self.roundtrip((b'B', 128), thing, '300A BF1F03 040142 02020080')

        thing = derlite.Optional(derlite.Structure((int, Oid)))
        self.roundtrip(None, thing, '')
        self.roundtrip((4, Oid('2.1.128')), thing, '3008 020104 0603518100')
Esempio n. 7
0
    def test_choice(self):

        thing = derlite.SequenceOf(derlite.Choice((
            int,
            Oid,
            bool,
        )))

        self.roundtrip([], thing, '3000')

        self.roundtrip([42], thing, '3003 02012A')

        self.roundtrip([Oid('1.8.10000'), -128, False], thing, '300B'
                       '060330CE10'
                       '020180'
                       '010100')

        thing = derlite.Structure((derlite.Choice(
            (bytes, bool, derlite.Choice((datetime.datetime, int)))), ))

        self.roundtrip((b'foo', ), thing, '3005 0403666F6F')
        self.roundtrip((12, ), thing, '3003 02010C')
        self.roundtrip((False, ), thing, '3003 010100')
Esempio n. 8
0
 def roundtrip(self, arcs, der):
     o = Oid(arcs)
     self.assertEqual(o.as_der(), der)
     o = Oid(der)
     self.assertEqual(o.arcs(), arcs)
Esempio n. 9
0
    print("\nWaiting for response...")
    
    while True:
        (dgram, fromaddr) = socket.recvfrom(8192)
        print("\nDatagram from", fromaddr)
        decoder = derlite.Decoder(dgram)
        (resp_community, resp_pdu) = decode_v2c_message(dgram)
        print("Community:", resp_community)
        print("Response:", resp_pdu)
        resp_pdu.debug_print(indent='  ')
        if community == resp_community and query_message.request_id == resp_pdu.request_id:
            break

# Some useful OIDs
snmp_mib_2 = Oid('1.3.6.1.2.1')       # The MIB-II oid
snmp_system_group = snmp_mib_2 + (1,) # The 'system' group from RFC1213

# Perform a simple SNMP query. We ask for three OIDs:
# MIB-II.system.sysDescr.0 and .1, and MIB-II.system.sysUpTime.0.
# The trailing 0 refers to the value of the object, more or less
# (see RFC1212 [4.1.6]). So we expect sysDescr.0 and sysUpTime.0 to
# succeed, but sysDescr.1 to fail since there is only one value
# for sysDescr; it's a scalar object not a table.
query_variables(socket.socket(socket.AF_INET, socket.SOCK_DGRAM),
                '127.0.0.1',   # Agent address
                b'public',     # Community string
                snmp_mib_base + (1,0),  # sysDescr.0
                snmp_mib_base + (1,1),  # sysDescr.1
                snmp_mib_base + (3,0),  # sysUpTime.0
)