blob: fbceaaaf4731224dd204d3c2a5fe78f061877860 [file] [log] [blame]
Dan Talaycodba244e2010-02-15 14:08:53 -08001"""
2Basic test cases for the oftest OpenFlow test framework
3
4Current Assumptions:
5
6 The oftest framework source is in ../src/python/oftest
7 Configuration of the platform and system is stored in oft_config in that dir
8 The switch is actively attempting to contact the controller at the address
9indicated oin oft_config
10
11"""
12
13from scapy.all import *
14import unittest
15
16import time
17import sys
18sys.path.append("../src/python/oftest")
19from message import *
20from dataplane import *
21from controller import *
22from oft_config import *
23
24class SimpleProtocolTestCase(unittest.TestCase):
25 """
26 Root class for setting up the controller
27 """
28
29 def setUp(self):
30 debug_log("CTTC", debug_level_default,
31 DEBUG_INFO, "setup for " + str(self))
32 self.controller = Controller()
33 self.controller.connect()
34
35 def tearDown(self):
36 debug_log("CTTC", debug_level_default,
37 DEBUG_INFO, "teardown for simple proto test")
38 self.controller.shutdown()
39 self.controller.join()
40
41 def runTest(self):
42 self.assertTrue(self.controller.connected,
43 str(self) + 'No connection to switch')
44
45class SimpleDataPlaneTestCase(SimpleProtocolTestCase):
46 """
47 Root class that sets up the controller and dataplane
48 """
49 def setUp(self):
50 SimpleProtocolTestCase.setUp(self)
51 self.dataplane = DataPlane()
52 for of_port, ifname in interface_ofport_map.items():
53 self.dataplane.port_add(ifname, of_port)
54
55 def tearDown(self):
56 debug_log("DPTC", debug_level_default,
57 DEBUG_INFO, "teardown for simple dataplane test")
58 SimpleProtocolTestCase.tearDown(self)
59 self.dataplane.kill(join_threads=False)
60
61 def runTest(self):
62 self.assertTrue(self.controller.connected,
63 str(self) + 'No connection to switch')
64 # self.dataplane.show()
65 # Would like an assert that checks the data plane
66
67class EchoTestCase(SimpleProtocolTestCase):
68 """
69 Test echo response with no data
70 """
71 def runTest(self):
72 request = echo_request()
73 response = self.controller.transact(request)
74 self.assertEqual(response.header.type, OFPT_ECHO_REPLY,
75 'response is not echo_request')
76 self.assertEqual(request.header.xid, response.header.xid,
77 'response xid != request xid')
78 self.assertEqual(len(response.data), 0, 'response data non-empty')
79
80class EchoWithDataTestCase(SimpleProtocolTestCase):
81 """
82 Test echo response with short string data
83 """
84 def runTest(self):
85 request = echo_request()
86 request.data = 'OpenFlow Will Rule The World'
87 response = self.controller.transact(request)
88 self.assertEqual(response.header.type, OFPT_ECHO_REPLY,
89 'response is not echo_request')
90 self.assertEqual(request.header.xid, response.header.xid,
91 'response xid != request xid')
92 self.assertEqual(request.data, response.data,
93 'response data does not match request')
94
95class PacketInTestCase(SimpleDataPlaneTestCase):
96 """
97 Test packet in function
98 """
99 def runTest(self):
100 # Construct packet to send to dataplane
101 # Send packet to dataplane
102 # Poll controller with expect message type packet in
103 # For now, a random packet from scapy tutorial
104 pkt=Ether()/IP(dst="www.slashdot.org")/TCP()/\
105 "GET /index.html HTTP/1.0 \n\n"
106 for of_port in interface_ofport_map.keys():
107 self.dataplane.send(of_port, str(pkt))
108 # For now, just send one packet
109 break
110
111 time.sleep(2) # @todo Implement poll timeout for test cases
112 #@todo Check for unexpected messages?
113 (response, raw) = self.controller.poll(OFPT_PACKET_IN)
114
115 self.assertTrue(not response is None, 'Packet in message not received')
116 # Data has CRC on it, so take off last 4 bytes
117 self.assertEqual(str(pkt), response.data[:-4],
118 'Response packet does not match send packet')
119
120class PacketOutTestCase(SimpleDataPlaneTestCase):
121 """
122 Test packet out function
123 """
124 def runTest(self):
125 # Construct packet to send to dataplane
126 # Send packet to dataplane
127 # Poll controller with expect message type packet in
128 # For now, a random packet from scapy tutorial
129
130 # These will get put into function
131 outpkt=Ether()/IP(dst="www.slashdot.org")/TCP()/\
132 "GET /index.html HTTP/1.0 \n\n"
133 msg = packet_out()
134 msg.data = str(outpkt)
135 act = action_output()
136 act.port = interface_ofport_map.keys()[0]
137 self.assertTrue(msg.actions.add(act), 'Could not add action to msg')
138
139 msg.header.xid = 0x12345678
140 self.controller.message_send(msg.pack())
141
142 time.sleep(2) # @todo Implement poll timeout for test cases
143 (of_port, pkt, pkt_time) = self.dataplane.packet_get()
144 print "pkt in on of_port" + str(of_port)
145 hexdump(str(pkt))
146
147 self.assertTrue(not pkt is None, 'Packet not received')
148 # Data has CRC on it, so take off last 4 bytes
149 self.assertEqual(str(outpkt), str(pkt),
150 'Response packet does not match send packet')
151
152
153if __name__ == "__main__":
154 unittest.main()