def _cached_entries_handler(self, raw_entries): """ Handles cached (through ZK) TRCs and Cert Chains. """ for entry in raw_entries: try: pkt = SCIONL4Packet(raw=entry) pkt.parse_payload() # FIXME(PSz): some checks are necessary, as filesystem may not # be synced with ZK. Also, when we change topology, new TRCs and # certs are generated, while old ones are still in ZK. It looks # to CS like an attack. This will be fixed when more elements # of trust infrastructure are specified and implemented (like # TRC cross-signing). except SCIONParseError: log_exception("Error parsing cached entry: %s" % entry, level=logging.ERROR) continue payload = pkt.get_payload() if isinstance(payload, CertChainReply): self.process_cert_chain_reply(pkt, from_zk=True) elif isinstance(payload, TRCReply): self.process_trc_reply(pkt, from_zk=True) else: logging.warning("Entry with unsupported type: %s" % entry)
def test(self, super_offset): inst = SCIONL4Packet() inst.l4_hdr = create_mock(["total_len"]) inst.l4_hdr.total_len = 12 super_offset.return_value = 42 # Call ntools.eq_(inst._get_offset_len(), 54)
def test_no_l4hdr(self, super_update): inst = SCIONL4Packet() inst._l4_proto = 47 # Call inst.update() # Tests super_update.assert_called_once_with(inst) ntools.eq_(inst._l4_proto, 47)
def _recv(self): try: packet = self.sock.recv()[0] except socket.timeout: return None except Exception as e: logging.critical("Error receiving packet: %s" % e) sys.exit(1) return SCIONL4Packet(packet)
def test_l4hdr(self, super_update): inst = SCIONL4Packet() inst.addrs = create_mock(["src", "dst"]) inst._l4_proto = 47 inst.l4_hdr = create_mock(["TYPE", "update"]) # Call inst.update() # Tests inst.l4_hdr.update.assert_called_once_with(src=inst.addrs.src, dst=inst.addrs.dst) ntools.eq_(inst._l4_proto, inst.l4_hdr.TYPE)
def test(self, super_pack, super_pld): inst = SCIONL4Packet() inst.update = create_mock() super_pack.return_value = b"super" super_pld.return_value = b"pld" inst.l4_hdr = create_mock(["pack"]) inst.l4_hdr.pack.return_value = b"l4 hdr" # Call ntools.eq_(inst._inner_pack(), b"super" b"l4 hdr" b"pld") # Tests inst.update.assert_called_once_with() inst.l4_hdr.pack.assert_called_once_with(b"pld")
def _setup(self, l4_type): inst = SCIONL4Packet() inst._parse_pld_ctrl = create_mock() inst._parse_pld_ctrl.return_value = "pld ctrl" inst._parse_pld_scmp = create_mock() inst._parse_pld_scmp.return_value = "pld scmp" inst.set_payload = create_mock() inst._payload = create_mock(["pack"]) if l4_type is not None: inst.l4_hdr = create_mock(["TYPE"]) inst.l4_hdr.TYPE = l4_type return inst
def test(self, super_values): inst = SCIONL4Packet() l4_hdr = MagicMock(spec_set=L4HeaderBase) l4_hdr.TYPE = 22 # Call inst._inner_from_values("cmn hdr", "addr hdr", "path hdr", "ext hdrs", l4_hdr) # Tests super_values.assert_called_once_with(inst, "cmn hdr", "addr hdr", "path hdr", "ext hdrs") ntools.eq_(inst.l4_hdr, l4_hdr) ntools.eq_(inst._l4_proto, 22)
def test(self, super_parse, parse_l4_hdr): inst = SCIONL4Packet() inst._l4_proto = 42 inst.addrs = create_mock(["src", "dst"]) data = create_mock() parse_l4_hdr.return_value.__len__.return_value = 8 # Call inst._inner_parse(data) # Tests super_parse.assert_called_once_with(inst, data) parse_l4_hdr.assert_called_once_with( 42, data, src=inst.addrs.src, dst=inst.addrs.dst, ) ntools.eq_(inst.l4_hdr, parse_l4_hdr.return_value)
def _setup(self, l4_type): inst = SCIONL4Packet() inst._parse_pld_ctrl = create_mock() inst._parse_pld_ctrl.return_value = "pld ctrl" inst._parse_pld_scmp = create_mock() inst._parse_pld_scmp.return_value = "pld scmp" inst.set_payload = create_mock() inst._payload = create_mock(["pack"]) if l4_type == L4Proto.UDP: inst.l4_hdr = create_mock_full({"TYPE": l4_type}) elif l4_type == L4Proto.SCMP: inst.l4_hdr = create_mock_full({"TYPE": l4_type, "class_": "scmp_class", "type": "scmp_type"}) return inst
def _check_known(self, class_, from_bytes, parse_pcb, parse_ifid, parse_cert, parse_path, parse_sibra): from_bytes.return_value = self._mk_proto_obj(class_) class_map = { PayloadClass.PCB: parse_pcb, PayloadClass.IFID: parse_ifid, PayloadClass.CERT: parse_cert, PayloadClass.PATH: parse_path, PayloadClass.SIBRA: parse_sibra, } handler = class_map[class_] inst = SCIONL4Packet() data = self._mk_data() # Call ntools.eq_(inst._parse_pld_ctrl(data), handler.return_value) # Tests handler.assert_called_once_with("inner")
def _check_known(self, class_, parse_pcb, parse_ifid, parse_cert, parse_path, parse_sibra): class_map = { PayloadClass.PCB: parse_pcb, PayloadClass.IFID: parse_ifid, PayloadClass.CERT: parse_cert, PayloadClass.PATH: parse_path, PayloadClass.SIBRA: parse_sibra, } handler = class_map[class_] inst = SCIONL4Packet() data = create_mock(["pop"]) data.pop.side_effect = class_, 42 # Call ntools.eq_(inst._parse_pld_ctrl(data), handler.return_value) # Tests handler.assert_called_once_with(42, data)
def _parse_packet(self, packet): try: pkt = SCIONL4Packet(packet) except SCMPError as e: self._scmp_parse_error(packet, e) return None except SCIONBaseError: log_exception("Error parsing packet: %s" % hex_str(packet), level=logging.ERROR) return None try: pkt.validate(len(packet)) except SCMPError as e: self._scmp_validate_error(pkt, e) return None except SCIONChecksumFailed: logging.debug("Dropping packet due to failed checksum:\n%s", pkt) return pkt
def _recv(self): try: packet = self.sock.recv()[0] except socket.timeout: return None return SCIONL4Packet(packet)
def test_parse_error(self, from_bytes): inst = SCIONL4Packet() data = self._mk_data(4) from_bytes.side_effect = self._raise_parse_error # Call ntools.assert_raises(SCIONParseError, inst._parse_pld_ctrl, data)
def test_bad_len(self): inst = SCIONL4Packet() data = self._mk_data(3) # Call ntools.assert_raises(SCIONParseError, inst._parse_pld_ctrl, data)
def test_unknown(self): inst = SCIONL4Packet() data = create_mock(["pop"]) data.pop.return_value = 42 # Call ntools.assert_raises(SCIONParseError, inst._parse_pld_ctrl, data)
def test_unknown(self, from_bytes): from_bytes.return_value = self._mk_proto_obj("unknown") inst = SCIONL4Packet() data = self._mk_data() # Call ntools.assert_raises(SCIONParseError, inst._parse_pld_ctrl, data)