summaryrefslogtreecommitdiff
path: root/maximus/python/test/test_mme.py
blob: f33c5615b89ba3a0ebb98e0a81f1743b4201fa03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#! usr/bin/env python

print "\n*** " + __file__ + " ***\n"

import sys, startup

from maximus.mme import *
from maximus.mme.create import create_mme
from maximus.utils.exception import OutOfRangeError
from interface import *
from struct import pack


# MME TEST

# Create a Maximus instance
m = Maximus()

# Initialize Maximus with command line arguments
m.init(sys.argv)

# Create the destination station
staRx = m.create_sta()
staRx.debug()

# Create an MME
mme1 = MME(MMHeader=MMHeader(MMV=0x01), MMEntry=MMEntry('This is the MM Entry'))

# Send the MME asynchronously
mme1.send(m, staRx)

# Create an MME
mme2 = MME()

# Send the MME asynchronously
rsp = mme2.sendnrecv(m, staRx, timeout=250000)

# Remove the destination station
staRx.remove()


# DOC TEST

import doctest
doctest.testmod(mme)
doctest.testmod(mmentry)
doctest.testmod(mmheader)
doctest.testmod(mmtype)


# UNIT TEST

import unittest

class TestMMHeaderFunctions(unittest.TestCase):

	def setUp(self):
		self.mmheader = MMHeader()

	def tearDown(self):
		pass

	def test_set_oda(self):
		# Test with a Python long
		oda = 0x112233445566
		self.mmheader.set_oda(oda)
		self.assertEqual(self.mmheader.get_oda(), oda)
		# Test with a Python string of length equals to 6 octets
		oda = '123456'
		self.mmheader.set_oda(oda)
		self.assertEqual(self.mmheader.get_oda(), oda)

	def test_set_osa(self):
		# Test with a Python long
		osa = 0xFFFFFFFFFFFF
		self.mmheader.set_osa(osa)
		self.assertEqual(self.mmheader.get_osa(), osa)
		# Test with a Python string of length equals to 6 octets
		osa = pack('Q', 0x112233445566)[0:6]
		self.mmheader.set_osa(osa)
		self.assertEqual(self.mmheader.get_osa(), osa)

	def test_set_vlantag(self):
		# Test with a Python integer
		#
		vlantag = 0x8100FFFF
		self.mmheader.set_vlantag(vlantag)
		self.assertEqual(self.mmheader.get_vlantag(), vlantag)
		
		# Test with a bad vlantag
		test = False
		vlantag = 0xFFFFFFFF
		try:
			self.mmheader.set_vlantag(vlantag)
		except OutOfRangeError:
			test = True
		self.assert_(test)
		
		# Test with a Python string of length equals to 4 octets
		#
		vlantag = pack('!I', 0x81000000)
		self.mmheader.set_vlantag(vlantag)
		self.assertEqual(self.mmheader.get_vlantag(), vlantag)
		
		# Test with a bad vlantag
		test = False
		vlantag = pack('!I', 0x00000000)
		try:
			self.mmheader.set_vlantag(vlantag)
		except OutOfRangeError:
			test = True
		self.assert_(test)

	def test_set_mtype(self):
		# Test with a Python integer
		mtype = 0xFFFF
		self.mmheader.set_mtype(mtype)
		self.assertEqual(self.mmheader.get_mtype(), mtype)
		# Test with a Python string of length equals to 2 octets
		mtype = '12'
		self.mmheader.set_mtype(mtype)
		self.assertEqual(self.mmheader.get_mtype(), mtype)
		mtype = pack('H', 0x1122)
		self.mmheader.set_mtype(mtype)
		self.assertEqual(self.mmheader.get_mtype(), mtype)

	def test_set_mmv(self):
		# Test with a Python integer
		mmv = 0xFF
		self.mmheader.set_mmv(mmv)
		self.assertEqual(self.mmheader.get_mmv(), mmv)
		# Test with a Python string of length equals to 1 octet
		mmv = '1'
		self.mmheader.set_mmv(mmv)
		self.assertEqual(self.mmheader.get_mmv(), mmv)
		mmv = pack('B', 0x01)
		self.mmheader.set_mmv(mmv)
		self.assertEqual(self.mmheader.get_mmv(), mmv)

	def test_set_mmtype(self):
		# Test with a Python integer
		mmtype = 0x1234
		self.mmheader.set_mmtype(mmtype)
		self.assertEqual(self.mmheader.get_mmtype(), mmtype)
		# Test with a Python string of length equals to 2 octets
		mmtype = 'ab'
		self.mmheader.set_mmtype(mmtype)
		self.assertEqual(self.mmheader.get_mmtype(), mmtype)
		mmtype = pack('H', 0x1234)
		self.mmheader.set_mmtype(mmtype)
		self.assertEqual(self.mmheader.get_mmtype(), mmtype)

	def test_set_fmi(self):
		# Test with a Python integer
		fmi = 0xFEDC
		self.mmheader.set_fmi(fmi)
		self.assertEqual(self.mmheader.get_fmi(), fmi)
		# Test with a Python string of length equals to 2 octets
		fmi = 'AB'
		self.mmheader.set_fmi(fmi)
		self.assertEqual(self.mmheader.get_fmi(), fmi)
		fmi = pack('H', 0xFEDC)
		self.mmheader.set_fmi(fmi)
		self.assertEqual(self.mmheader.get_fmi(), fmi)

	def test_get(self):
		# Test without vlantag
		mmheader = MMHeader(ODA=0x616263646566, OSA=0x6768696A6B6C, MTYPE=0x6D6E, MMV=0x6F, MMTYPE=0x7170, FMI=0x7372)
		self.assertEqual(mmheader.get(), 'abcdefghijklmnopqrs')
		
		# Test with vlantag
		mmheader = MMHeader(ODA=0x616263646566, OSA=0x6768696A6B6C, VLANTag=0x81006D6E, MTYPE=0x6F70, MMV=0x71, MMTYPE=0x7372, FMI=0x7574)
		self.assertEqual(mmheader.get(), 'abcdefghijkl' + pack('!I', 0x81006D6E) + 'opqrstu')

suite = unittest.TestLoader().loadTestsFromTestCase(TestMMHeaderFunctions)

class TestMMEntryFunctions(unittest.TestCase):

	def setUp(self):
		self.mmentry = MMEntry()

	def tearDown(self):
		pass

	def test_set_data(self):
		# Test with a Python string of length smaller than 1518 - 23 = 1495
		data = 1495*'M'
		self.mmentry.set_data(data)
		self.assertEqual(self.mmentry.get_data(), data)

	def test_get(self):
		data = 'This is the Management Message Entry Data'
		mmentry = MMEntry(data)
		self.assertEqual(mmentry.get(), data)

suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestMMEntryFunctions))

class TestMMEFunctions(unittest.TestCase):

	def setUp(self):
		self.mme = MME()
		self.m = m

	def tearDown(self):
		pass

	def test_set_mmheader(self):
		# Test with an MM Header object with VLAN Tag
		mmheader = MMHeader(VLANTag=0x81000000)
		self.mme.set_mmheader(mmheader)
		self.assertEqual(self.mme.get_mmheader(), mmheader)
		
		# Test with an MM Header object without VLAN Tag
		mmheader = MMHeader()
		self.mme.set_mmheader(mmheader)
		self.assertEqual(self.mme.get_mmheader(), mmheader)
		
		# Test with a Python string of length equals to 19 octets
		mmheader = 'abcdefghijklmnopqrs'
		self.mme.set_mmheader(mmheader)
		self.assertEqual(self.mme.get_mmheader(), mmheader)
		
		# Test with a Python string of length equals to 23 octets
		mmheader = 'abcdefghijklmnopqrstuvw'
		self.mme.set_mmheader(mmheader)
		self.assertEqual(self.mme.get_mmheader(), mmheader)

	def test_set_mmentry(self):
		# Test with a Python string of length smaller than 1500 octets
		mmentry = 'test'
		self.mme.set_mmentry(mmentry)
		self.assertEqual(self.mme.get_mmentry(), mmentry)

	def test_set_msdu_attr(self):
		# Test with VLAN Tag
		payload = 12*'H' + pack('!I', 0x81000000) + 7*'H' + 'Payload'
		self.mme.set_msdu_attr(payload)
		self.assertEqual(self.mme.get_mmheader(), 12*'H' + pack('!I', 0x81000000) + 7*'H')
		self.assertEqual(self.mme.get_mmentry(), 'Payload')
		
		# Test without VLAN Tag
		payload = 19*'H' + 'Payload'
		self.mme.set_msdu_attr(payload)
		self.assertEqual(self.mme.get_mmheader(), 19*'H')
		self.assertEqual(self.mme.get_mmentry(), 'Payload')

	def test_sendnrecv(self):
		# Tested in 'py/test_ether.py' because another station is needed for this test
		# Here, just test the timeout
		sta = self.m.create_sta()
		t = self.m.get_date()
		d = 250000
		rsp = self.mme.sendnrecv(self.m, sta, timeout=d)
		self.assertEqual(rsp, None)
		self.assertEqual(self.m.get_date(), t + d)
		sta.remove()

	def test_send(self):
		sta = self.m.create_sta()
		self.mme.send(self.m, sta)
		sta.remove()

	def test_get(self):
		# Test with VLAN Tag
		mme1 = MME(MMHeader=23*'A', MMEntry='B')
		self.assertEqual(mme1.get(), 23*'A'+'B'+36*pack('B',0))
		mme2 = MME(MMHeader=23*'A', MMEntry=36*'B')
		self.assertEqual(mme2.get(), 23*'A'+36*'B'+pack('B',0))
		mme3 = MME(MMHeader=23*'A', MMEntry=37*'B')
		self.assertEqual(mme3.get(), 23*'A'+37*'B')
		mme4 = MME(MMHeader=23*'A', MMEntry=1000*'B')
		self.assertEqual(mme4.get(), 23*'A'+1000*'B')
		
		# Test without VLAN Tag
		mme1 = MME(MMHeader=19*'A', MMEntry='B')
		self.assertEqual(mme1.get(), 19*'A'+'B'+40*pack('B',0))
		mme2 = MME(MMHeader=19*'A', MMEntry=40*'B')
		self.assertEqual(mme2.get(), 19*'A'+40*'B'+pack('B',0))
		mme3 = MME(MMHeader=19*'A', MMEntry=41*'B')
		self.assertEqual(mme3.get(), 19*'A'+41*'B')
		mme4 = MME(MMHeader=19*'A', MMEntry=1000*'B')
		self.assertEqual(mme4.get(), 19*'A'+1000*'B')

	def test_get_ether_type(self):
		self.assertEqual(self.mme.get_ether_type(), 2)

	def test_get_type(self):
		self.assertEqual(self.mme.get_type(), 'ETHERNET_TYPE_MME')

	def test_create_mme(self):
		self.assertNotEqual(create_mme(), None)

suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestMMEFunctions))

try:
	suite.addTest(doctest.DocTestSuite(mme))
	suite.addTest(doctest.DocTestSuite(mmentry))
	suite.addTest(doctest.DocTestSuite(mmheader))
	suite.addTest(doctest.DocTestSuite(mmtype))
except ValueError:
	print "has no tests"

if __name__ == '__main__':
	testResult = unittest.TextTestRunner(verbosity=2).run(suite)