| 1 |  |  | """ Tests for /backends/of_parser.py """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | from unittest import TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | from unittest.mock import patch, MagicMock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from napps.amlight.sdntrace.backends.of_parser import ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |     process_packet_in, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     send_packet_out, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | # pylint: disable=too-many-public-methods, too-many-lines, no-self-use | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class TestOFParser(TestCase): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     """Unit tests for backends.of_parser functions""" | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     def test_process_packet_in(self): | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         """Test process packet in for openflow10 with data.""" | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         raw = b"\x00\x15\xaf\xd58\x98\xee\xee\xee\xee\xee\x01\x08\x00testdata" | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |         # Ethernet(destination='00:15:af:d5:38:98', | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |         #          source='ee:ee:ee:ee:ee:01', ether_type=0x800, | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         #          data=b'testdata') | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         packet_in_msg = MagicMock() | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         packet_in_msg.data.value = raw | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         packet_in_msg.in_port.value = 1 | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         event = MagicMock() | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         event.source.switch = "ee:ee:ee:ee:ee:01" | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         event.content = {"message": packet_in_msg} | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         event.content["message"].header.version.value = 1 | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         ethernet, in_port, switch = process_packet_in(event) | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         self.assertEqual(ethernet.source.value, "ee:ee:ee:ee:ee:01") | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         self.assertEqual(in_port, 1) | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         self.assertEqual(switch, "ee:ee:ee:ee:ee:01") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     @patch("napps.amlight.sdntrace.backends.openflow10.packet_in") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     @patch("napps.amlight.sdntrace.backends.openflow13.packet_in") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     def test_process_of10_packet_in_patched( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         self, mock_of13_packet_in, mock_of10_packet_in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     ): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         """Test process packet in for openflow10.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         event = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         event.content = {"message": MagicMock()} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         event.content["message"].header.version.value = 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         process_packet_in(event) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         mock_of10_packet_in.assert_called_once() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         mock_of13_packet_in.assert_not_called() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     @patch("napps.amlight.sdntrace.backends.openflow10.packet_in") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     @patch("napps.amlight.sdntrace.backends.openflow13.packet_in") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     def test_process_of13_packet_in_patched( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         self, mock_of13_packet_in, mock_of10_packet_in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     ): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         """Test process packet in for openflow13.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         event = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         event.content = {"message": MagicMock()} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         event.content["message"].header.version.value = 4 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         process_packet_in(event) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         mock_of10_packet_in.assert_not_called() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         mock_of13_packet_in.assert_called_once() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     @patch("napps.amlight.sdntrace.backends.openflow10.packet_in") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     @patch("napps.amlight.sdntrace.backends.openflow13.packet_in") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     def test_process_packet_in_error( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         self, mock_of13_packet_in, mock_of10_packet_in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     ): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         """Test process packet in for openflow13.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         event = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         event.content = {"message": MagicMock()} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         event.content["message"].header.version.value = 9999 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         ethernet, in_port, switch = process_packet_in(event) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         self.assertEqual(ethernet, 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         self.assertEqual(in_port, 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         self.assertEqual(switch, 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         mock_of10_packet_in.assert_not_called() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         mock_of13_packet_in.assert_not_called() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     @patch("napps.amlight.sdntrace.backends.openflow10.send_packet_out") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     @patch("napps.amlight.sdntrace.backends.openflow13.send_packet_out") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     def test_process_of10_packet_out_patched( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         self, mock_of13_packet_out, mock_of10_packet_out | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     ): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         """Test process packet out for openflow10.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         controller = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         switch = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         switch.features.header.version.value = 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         port = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         data = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         send_packet_out(controller, switch, port, data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         mock_of10_packet_out.assert_called_once() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         mock_of13_packet_out.assert_not_called() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     @patch("napps.amlight.sdntrace.backends.openflow10.send_packet_out") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     @patch("napps.amlight.sdntrace.backends.openflow13.send_packet_out") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     def test_process_of13_packet_out_patched( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         self, mock_of13_packet_out, mock_of10_packet_out | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     ): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         """Test process packet out for openflow13.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         controller = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         switch = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         switch.features.header.version.value = 4 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         port = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         data = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         send_packet_out(controller, switch, port, data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         mock_of10_packet_out.assert_not_called() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         mock_of13_packet_out.assert_called_once() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     @patch("napps.amlight.sdntrace.backends.openflow10.send_packet_out") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     @patch("napps.amlight.sdntrace.backends.openflow13.send_packet_out") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     def test_process_packet_out_error( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         self, mock_of13_packet_out, mock_of10_packet_out | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     ): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         """Test process packet out for openflow13.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         controller = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         switch = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         switch.features.header.version.value = 999 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         port = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         data = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         send_packet_out(controller, switch, port, data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         mock_of10_packet_out.assert_not_called() | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 140 |  |  |         mock_of13_packet_out.assert_not_called() | 
            
                                                        
            
                                    
            
            
                | 141 |  |  |  |