def testMessageDecode(self): """Test that encoded messages decode back to something that is identical to what we started with.""" suffix = "\nabc" for cmd, args in sample_msgs: r = codec.encode_msg(cmd, **args) dcmd, dargs, rest = codec.decode_msg(r) self.assertEqual(cmd, dcmd) self.assertEqual(args, dargs) self.assertEqual(rest, '') # As a bonus, test that decoding with more data works # right for all messages. dcmd, dargs, rest = codec.decode_msg(r + suffix) self.assertEqual(rest, suffix)
def get_msg(self, eof_ok=False): """Retrieve the next message from the connection message. Returns the decoded message as a tuple of (cmd, paramdict). Raises MilterDecodeError if we see EOF with an incomplete packet. If we see a clean EOF, we normally raise MilterConvoError. If eof_ok is True, we instead return None.""" while 1: try: # .decode_msg will fail with an incomplete # error if self.buf is empty, so we don't # have to check for that ourselves. (rcmd, rdict, data) = codec.decode_msg(self.buf) self.buf = data return (rcmd, rdict) except codec.MilterIncomplete: # This falls through to cause us to read # stuff. pass data = self.sock.recv(self.blksize) # Check for EOF on the read. # If we have data left in self.buf, it axiomatically # failed to decode above and so it must be an # incomplete packet. if not data: if self.buf: raise codec.MilterDecodeError("packet truncated by EOF") elif not eof_ok: raise MilterConvoError("unexpected EOF") else: return None self.buf += data del data
def testOptnegMilterEncode(self): """Test encode_optneg() with is_milter=True, which should not clamp protocol to SMFI_V2_PROT.""" r = codec.encode_optneg(actions=0xff, protocol=0x180, is_milter = True) rcmd, rdict, data = codec.decode_msg(r) self.assertEqual(rdict['actions'], SMFI_V2_ACTS) self.assertEqual(rdict['protocol'], 0x180)
def testOptnegEncode(self): """Test that encode_optneg() works right.""" return for a, b in self.optneg_tests: r = codec.encode_optneg(actions=a[0], protocol=a[1]) rcmd, rdict, data = codec.decode_msg(r) self.assertEqual(data, '') self.assertEqual(rcmd, SMFIC_OPTNEG) rpair = (rdict['actions'], rdict['protocol']) self.assertEqual(rpair, b)
def sendall(self, buf): self._verify_conv(WRITE) # We verify that we got the right sort of stuff r = codec.decode_msg(buf) _, wres = self.conv[self.cindex] self.cindex += 1 otype = wres[0] if r[0] != otype: raise ConvError("received unexpected reply '%s' vs '%s" % \ (r[0], otype)) if len(wres) > 1 and r[1] != wres[1]: raise ConvError("unexpected reply parameters: reply %s vs %s" % \ (r[1], wres[1]))