1#!/usr/bin/env python
2
3# sorry, this is very ugly, but I'm in python 2.5
4import sys
5sys.path.insert(0,"../..")
6
7from impacket.ImpactPacket import TCP, ImpactPacketException
8from binascii import hexlify
9import unittest
10from threading import Thread
11
12class TestTCP(unittest.TestCase):
13
14    def setUp(self):
15	# Dummy TCP header with "Maximum Segment Size" Option and zero length
16	self.frame = '\x12\x34\x00\x50\x00\x00\x00\x01\x00\x00\x00\x00\x60\x00\x00\x00\x8d\x5c\x00\x00\x02\x00\x00\x00'
17
18    def test_01(self):
19        'Test TCP options parsing hangs'
20	class it_hangs(Thread):
21		def __init__(self):
22			Thread.__init__(self)
23		def run(self):
24			try:
25				frame = '\x12\x34\x00\x50\x00\x00\x00\x01\x00\x00\x00\x00' \
26					'\x60\x00\x00\x00\x8d\x5c\x00\x00\x02\x00\x00\x00'
27				tcp = TCP(frame)
28			#except Exception,e:
29			#	print "aaaaaaaaaaaaaaa"
30			#	print e
31			#except Exception,e:
32			except ImpactPacketException,e:
33				if str(e) != "'TCP Option length is too low'":
34					raise e
35			except:
36				pass
37
38	thread_hangs = it_hangs()
39	thread_hangs.setDaemon(True)
40	thread_hangs.start()
41        thread_hangs.join(1.0) # 1 seconds timeout
42       	self.assertEqual(thread_hangs.isAlive(), False)
43	#if thread_hang.isAlive():
44
45
46suite = unittest.TestLoader().loadTestsFromTestCase(TestTCP)
47unittest.TextTestRunner(verbosity=1).run(suite)
48
49