packet lib: udp: support default parameters

Signed-off-by: Yuichi Ito <ito.yuichi0@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
Yuichi Ito 2013-11-01 14:16:10 +09:00 committed by FUJITA Tomonori
parent 96d5be05e0
commit ae9470136e
2 changed files with 11 additions and 1 deletions

View File

@ -41,7 +41,7 @@ class udp(packet_base.PacketBase):
_PACK_STR = '!HHHH'
_MIN_LEN = struct.calcsize(_PACK_STR)
def __init__(self, src_port, dst_port, total_length=0, csum=0):
def __init__(self, src_port=0, dst_port=0, total_length=0, csum=0):
super(udp, self).__init__()
self.src_port = src_port
self.dst_port = dst_port

View File

@ -94,3 +94,13 @@ class Test_udp(unittest.TestCase):
def test_malformed_udp(self):
m_short_buf = self.buf[1:udp._MIN_LEN]
udp.parser(m_short_buf)
def test_default_args(self):
prev = ipv4(proto=inet.IPPROTO_UDP)
u = udp()
buf = u.serialize(bytearray(), prev)
res = struct.unpack(udp._PACK_STR, buf)
eq_(res[0], 0)
eq_(res[1], 0)
eq_(res[2], udp._MIN_LEN)