def test_register_response(self): resp = b'\x01\x12\x10\x00\x00\x00\x00"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00,\x00\x0eL\xb5\x01\x07\x00' \ b'\x00\x00\x05\x00\x00\x07\x04\x00\x00\x00\x00\x00\x01\x00\x00\x17\x8b\x00\x00\x00\x03\x00\x00\x00\n' \ b'\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\t' pdu = PDU.decode(resp) self.assertEqual(PDU.decode(pdu.encode()), pdu)
def test_OpenPDU_response(self): resp = b'\x01\x12\x10\x00\x00\x00\x00\x0e\x00\x00\x00\x00\x00\x00\x00\x00' \ b'\x00\x00\x00@\x00\x02h\x8b\x00\x00\x00\x00\x00\x04\x00\x00\x02\x00' \ b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00$Azure Cloud ' \ b'Switch -- SNMP sub-agent' resp1 = PDU.decode(resp) resp2 = PDU.decode(resp1.encode()) self.assertEqual(resp1, resp2)
def test_first_index(self): # walk walk = b'\x01\x06\x10\x00\x00\x00\x00O\x00\x01\x93\x9b\x00\x01\x93\x9c\x00\x00\x00(\x06\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x02\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x03' pdu = PDU.decode(walk) resp = pdu.make_response(self.lut) print(resp) # step step = b'\x01\x06\x10\x00\x00\x00\x00O\x00\x01\x94\x03\x00\x01\x94\x04\x00\x00\x00(\x06\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x05\x02\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x03' pdu = PDU.decode(step) resp = pdu.make_response(self.lut) print(resp)
def test_roundtrip(self): """ 1.3.6.1.2.1.2.2.1.[1-22].7 index 10 6,2,2,0,(1,2,2,1,1,7) upper bound 22 :return: """ register = RegisterPDU( header=PDUHeader(1, PduTypes.REGISTER, 16, 0, 42, 0, 0, 0), timeout=40, priority=50, range_subid=6, subtree=ObjectIdentifier(4, 2, 0, 0, (1, 1, 1, 0)), upper_bound=7 ) encoded = register.encode() decoded = PDU.decode(encoded) self.assertEqual(decoded.header.type_, PduTypes.REGISTER) self.assertIsInstance(register, RegisterPDU) self.assertEqual(register, decoded) print(register)
def test_factory(self): with self.assertRaises(exceptions.UnsupportedPDUError): # type 0 PDU.decode(b'\x00\x00\x00\x00' * 5) with self.assertRaises(exceptions.UnsupportedPDUError): # type 19 PDU.decode(b'\x00\x13\x00\x00' * 5) with self.assertRaisesRegex(TypeError, r'Abstract'): # Abstract -- not allowed. PDU(None) with self.assertRaisesRegex(TypeError, r'Abstract'): # Abstract -- not allowed. ContextOptionalPDU(None)
def test_bad_if_names(self): """ Triggered by mis-configured interface names. Fine otherwise. TODO: exemplary bad DB """ resp = b'\x01\x06\x10\x00\x00\x00\x00\x15\x00\x00\x01\x0c\x00\x00\x01\r\x00\x00\x00,\x06\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x03\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x02\x01\x06\x10\x00\x00\x00\x00\x15\x00\x00\x01\x12\x00\x00\x01\x13\x00\x00\x00,\x06\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x03\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x02\x01\x06\x10\x00\x00\x00\x00\x15\x00\x00\x01\x18\x00\x00\x01\x19\x00\x00\x00,\x06\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x03\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x02\x01\x06\x10\x00\x00\x00\x00\x15\x00\x00\x01\x1e\x00\x00\x01\x1f\x00\x00\x00,\x06\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x03\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x02' pdu = PDU.decode(resp) resp = pdu.make_response(self.lut) print(resp)
def test_make_response(self): # start of .2.9 # get_bytes = b'\x01\x06\x10\x00\x00\x00\x00\x0b\x00\x00\x00\x8e\x00\x00\x00\x8f\x00\x00\x00@\x07\x04\x00\x00\x00\x00\x00\x01\x00\x00\x17\x8b\x00\x00\x00\x03\x00\x00\x00\n\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\t\x07\x04\x00\x00\x00\x00\x00\x01\x00\x00\x17\x8b\x00\x00\x00\x03\x00\x00\x00\n\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\n' # end of mib get_bytes = b'\x01\x06\x10\x00\x00\x00\x00\x0f\x00\x00\x01\x11\x00\x00\x01\x12\x00\x00\x00H\t\x04\x00\x00\x00\x00\x00\x01\x00\x00\x17\x8b\x00\x00\x00\x03\x00\x00\x00\n\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\t\x00\x00\x00\x01\x00\x00\x00\x05\x07\x04\x00\x00\x00\x00\x00\x01\x00\x00\x17\x8b\x00\x00\x00\x03\x00\x00\x00\n\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\n' # snmpwalk -c strcommunity -v2c 10.3.147.172 .1.3.6.1.4.1.6027.3.10.1.2.9.5 get_bytes = b'\x01\x06\x10\x00\x00\x00\x00\x1d\x00\x00\x03\xbc\x00\x00\x03\xbd\x00\x00\x00D\x08\x04\x00\x00\x00\x00\x00\x01\x00\x00\x17\x8b\x00\x00\x00\x03\x00\x00\x00\n\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\t\x00\x00\x00\x05\x07\x04\x00\x00\x00\x00\x00\x01\x00\x00\x17\x8b\x00\x00\x00\x03\x00\x00\x00\n\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\n' get_bytes2 = b'\x01\x05\x10\x00\x00\x00\x00\x1d\x00\x00\x03\xbe\x00\x00\x03\xbf\x00\x00\x00(\x08\x04\x00\x00\x00\x00\x00\x01\x00\x00\x17\x8b\x00\x00\x00\x03\x00\x00\x00\n\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\t\x00\x00\x00\x05\x00\x00\x00\x00' get_pdu = PDU.decode(get_bytes) get_pdu.make_response(self.lut)
def test_lab_breaks(self): break1 = b'\x01\x06\x10\x00\x00\x00\x00q\x00\x01\xd1\x02\x00\x01\xd1\x03\x00\x00\x00P\t\x00\x01\x00\x00' \ b'\x00\x00\x01\x00\x00\x00\x00\x00\x00"b\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x02\x00' \ b'\x00\x00\x01\x00\x00\x00\x03\x00\x00\x00\x07\t\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00' \ b'\x00"b\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x03\x00' \ b'\x00\x00\x08' pdu = PDU.decode(break1) resp = pdu.make_response(self.lut) print(resp) break2 = b'\x01\x06\x10\x00\x00\x00\x00\x15\x00\x00\x08\x98\x00\x00\x08\x9a\x00\x00\x00P\t\x00\x01\x00' \ b'\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00"b\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x02' \ b'\x00\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00\x01\t\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00' \ b'\x00\x00\x00"b\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00' \ b'\x04\x00\x00\x00\x02' pdu = PDU.decode(break2) resp = pdu.make_response(self.lut) print(resp)
def test_missing_counter(self): """ KeyError: b'OUT_QLEN' counter_value = self.if_counters[sai_id][_table_name] snmp-subagent[242]: File "/usr/lib/python3.5/site-packages/ax_interface/mib.py", line 133, in __call__ KeyError triggered when attribute is absent from interface counters. TODO: exemplary bad DB """ resp = b'\x01\x06\x10\x00\x00\x00\x00[\x00\x00Ek\x00\x00En\x00\x00\x00(\x06\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x14\x00\x00\x00}\x02\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x03\x01\x06\x10\x00\x00\x00\x00[\x00\x00Eo\x00\x00Eq\x00\x00\x00(\x06\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x14\x00\x00\x00y\x02\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x03' pdu = PDU.decode(resp) resp = pdu.make_response(self.lut) print(resp)
def test_roundtrip(self): response_pdu = ResponsePDU( header=PDUHeader(1, 18, 16, 0, 42, 0, 0, 8), sys_up_time=0, error=0, index=0, ) encoded = response_pdu.encode() decoded = PDU.decode(encoded) self.assertEqual(decoded.header.type_, PduTypes.RESPONSE) self.assertIsInstance(response_pdu, ResponsePDU) self.assertEqual(response_pdu, decoded) print(response_pdu)
def test_roundtrip(self): open_pdu = OpenPDU( header=PDUHeader(1, 1, 16, 0, 0, 0, 0, 0), timeout=42, oid=ObjectIdentifier(4, 2, 0, 0, (1, 1, 1, 0)), descr="HOWDY-HO, NEIGHBOR!" ) encoded = open_pdu.encode() decoded = PDU.decode(encoded) self.assertEqual(decoded.header.type_, PduTypes.OPEN) self.assertIsInstance(open_pdu, OpenPDU) self.assertEqual(open_pdu, decoded) print(open_pdu)
def test_roundtrip(self): get_pdu = GetPDU( header=PDUHeader(1, PduTypes.GET, 16, 0, 42, 0, 0, 0), oids=( ObjectIdentifier(4, 2, 0, 0, (1, 1, 1, 0)), ObjectIdentifier(4, 2, 0, 0, (2, 2, 2, 0)), ) ) encoded = get_pdu.encode() decoded = PDU.decode(encoded) self.assertEqual(decoded.header.type_, PduTypes.GET) self.assertIsInstance(get_pdu, GetPDU) self.assertEqual(get_pdu, decoded) print(get_pdu)
def test_get_next3(self): payload = b'\x01\x06\x10\x00\x00\x00\x00\x17\x00\x00\x01V\x00\x00\x01W\x00\x00\x00,\x06\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x03\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x02\x01\x06\x10\x00\x00\x00\x00\x17\x00\x00\x01\\\x00\x00\x01]\x00\x00\x00,\x06\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x03\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x02\x01\x06\x10\x00\x00\x00\x00\x17\x00\x00\x01b\x00\x00\x01c\x00\x00\x00,\x06\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x03\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x02\x01\x06\x10\x00\x00\x00\x00\x17\x00\x00\x01h\x00\x00\x01i\x00\x00\x00,\x06\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x03\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x02' pdu = PDU.decode(payload) resp = pdu.make_response(self.lut) print(resp)
def test_get_next1(self): payload = b'\x01\x06\x10\x00\x00\x00\x00\x17\x00\x00\x01R\x00\x00\x01S\x00\x00\x00,\x06\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x03\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x02' pdu = PDU.decode(payload) resp = pdu.make_response(self.lut) print(resp)
def test_get_next_alias(self): if_alias = b'\x01\x06\x10\x00\x00\x00\x00o\x00\x01\xcc4\x00\x01\xcc5\x00\x00\x000\x07\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x11\x00\x00\x00}\x03\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x02' pdu = PDU.decode(if_alias) resp = pdu.make_response(self.lut) print(resp)
def test_register_prep(self): reg = b'\x01\x03\x10\x00\x00\x00\x00\x1e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00$\x05\x00\x00\x00\x07\x04' \ b'\x00\x00\x00\x00\x00\x01\x00\x00\x17\x8b\x00\x00\x00\x03\x00\x00\x00\n\x00\x00\x00\x01\x00\x00\x00' \ b'\x02\x00\x00\x00\t' pdu = PDU.decode(reg) self.assertEqual(PDU.decode(pdu.encode()), pdu)
def test_oid_response(self): get_next = b'\x01\x06\x10\x00\x00\x00\x00I\x00\x01m\xe3\x00\x01m\xe4\x00\x00\x00(\x06\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x15\x00\x00\x00}\x02\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x03' pdu = PDU.decode(get_next) resp = pdu.make_response(self.lut) print(resp)
def test_interfaces_walk(self): resp = b'\x01\x06\x10\x00\x00\x00\x00C\x00\x01ay\x00\x01az\x00\x00\x00(\x06\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00}\x02\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x03' resp_pdu = PDU.decode(resp) resp_pdu.make_response(self.lut) print(resp_pdu)
def test_regisiter_response(self): mib_2_response = b'\x01\x12\x10\x00\x00\x00\x001\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00,\x01d`\xab\x00\x00\x00\x00\x00\x05\x00\x00\x07\x04\x00\x00\x00\x00\x00\x01\x00\x00\x17\x8b\x00\x00\x00\x03\x00\x00\x00\n\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\t\x01\x12\x10\x00\x00\x00\x001\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x18\x01d`\xab\x00\x00\x00\x00\x00\x05\x00\x00\x02\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02' # TODO: needs recursive response resp_pdu = PDU.decode(mib_2_response) print(resp_pdu)