Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2013, Big Switch Networks, Inc. |
| 3 | # |
| 4 | # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with |
| 5 | # the following special exception: |
| 6 | # |
| 7 | # LOXI Exception |
| 8 | # |
| 9 | # As a special exception to the terms of the EPL, you may distribute libraries |
| 10 | # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided |
| 11 | # that copyright and licensing notices generated by LoxiGen are not altered or removed |
| 12 | # from the LoxiGen Libraries and the notice provided below is (i) included in |
| 13 | # the LoxiGen Libraries, if distributed in source code form and (ii) included in any |
| 14 | # documentation for the LoxiGen Libraries, if distributed in binary form. |
| 15 | # |
| 16 | # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler." |
| 17 | # |
| 18 | # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain |
| 19 | # a copy of the EPL at: |
| 20 | # |
| 21 | # http://www.eclipse.org/legal/epl-v10.html |
| 22 | # |
| 23 | # Unless required by applicable law or agreed to in writing, software |
| 24 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 25 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 26 | # EPL for the specific language governing permissions and limitations |
| 27 | # under the EPL. |
| 28 | |
Rich Lane | 569338c | 2013-06-17 15:51:24 -0700 | [diff] [blame] | 29 | import sys |
| 30 | import os |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 31 | import unittest |
Rich Lane | 569338c | 2013-06-17 15:51:24 -0700 | [diff] [blame] | 32 | |
| 33 | root_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..') |
| 34 | sys.path.insert(0, root_dir) |
| 35 | |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 36 | import loxi_front_end.parser as parser |
| 37 | import loxi_front_end.frontend as frontend |
| 38 | from loxi_ir import * |
| 39 | |
| 40 | class FrontendTests(unittest.TestCase): |
| 41 | maxDiff = None |
| 42 | |
| 43 | def test_simple(self): |
| 44 | ast = parser.parse(""" |
| 45 | #version 1 |
| 46 | |
| 47 | enum ofp_port_config { |
| 48 | OFPPC_PORT_DOWN = 0x1, |
| 49 | OFPPC_NO_STP = 0x2, |
| 50 | OFPPC_NO_RECV = 0x4, |
| 51 | OFPPC_NO_RECV_STP = 0x8, |
| 52 | OFPPC_NO_FLOOD = 0x10, |
| 53 | OFPPC_NO_FWD = 0x20, |
| 54 | OFPPC_NO_PACKET_IN = 0x40, |
| 55 | }; |
| 56 | |
| 57 | #version 2 |
| 58 | |
| 59 | struct of_echo_reply { |
| 60 | uint8_t version; |
Rich Lane | 3214287 | 2013-05-09 21:16:47 -0700 | [diff] [blame] | 61 | uint8_t type == 3; |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 62 | uint16_t length; |
| 63 | uint32_t xid; |
| 64 | of_octets_t data; |
| 65 | }; |
| 66 | |
| 67 | enum ofp_queue_op_failed_code { |
| 68 | OFPQOFC_BAD_PORT = 0, |
| 69 | OFPQOFC_BAD_QUEUE = 1, |
| 70 | OFPQOFC_EPERM = 2, |
| 71 | }; |
| 72 | |
| 73 | struct of_packet_queue { |
| 74 | uint32_t queue_id; |
| 75 | uint16_t len; |
| 76 | pad(2); |
| 77 | list(of_queue_prop_t) properties; |
| 78 | }; |
| 79 | """) |
| 80 | |
| 81 | # Not testing the parser, just making sure the AST is what we expect |
| 82 | expected_ast = [ |
| 83 | ['metadata', 'version', '1'], |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame^] | 84 | ['enum', 'ofp_port_config', [], [ |
| 85 | ['OFPPC_PORT_DOWN', [], 1], |
| 86 | ['OFPPC_NO_STP', [], 2], |
| 87 | ['OFPPC_NO_RECV', [], 4], |
| 88 | ['OFPPC_NO_RECV_STP', [], 8], |
| 89 | ['OFPPC_NO_FLOOD', [], 16], |
| 90 | ['OFPPC_NO_FWD', [], 32], |
| 91 | ['OFPPC_NO_PACKET_IN', [], 64]]], |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 92 | ['metadata', 'version', '2'], |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame^] | 93 | ['struct', 'of_echo_reply', [], None, [ |
Rich Lane | f424e97 | 2013-05-09 21:00:13 -0700 | [diff] [blame] | 94 | ['data', 'uint8_t', 'version'], |
Rich Lane | 3214287 | 2013-05-09 21:16:47 -0700 | [diff] [blame] | 95 | ['type', 'uint8_t', 'type', 3], |
Rich Lane | f424e97 | 2013-05-09 21:00:13 -0700 | [diff] [blame] | 96 | ['data', 'uint16_t', 'length'], |
| 97 | ['data', 'uint32_t', 'xid'], |
| 98 | ['data', 'of_octets_t', 'data']]], |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame^] | 99 | ['enum', 'ofp_queue_op_failed_code', [], [ |
| 100 | ['OFPQOFC_BAD_PORT', [], 0], |
| 101 | ['OFPQOFC_BAD_QUEUE', [], 1], |
| 102 | ['OFPQOFC_EPERM', [], 2]]], |
| 103 | ['struct', 'of_packet_queue', [], None, [ |
Rich Lane | f424e97 | 2013-05-09 21:00:13 -0700 | [diff] [blame] | 104 | ['data', 'uint32_t', 'queue_id'], |
| 105 | ['data', 'uint16_t', 'len'], |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 106 | ['pad', 2], |
Rich Lane | f424e97 | 2013-05-09 21:00:13 -0700 | [diff] [blame] | 107 | ['data', 'list(of_queue_prop_t)', 'properties']]], |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 108 | ] |
| 109 | self.assertEquals(expected_ast, ast) |
| 110 | |
| 111 | ofinput = frontend.create_ofinput(ast) |
| 112 | self.assertEquals(set([1, 2]), ofinput.wire_versions) |
| 113 | expected_classes = [ |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame^] | 114 | OFClass(name='of_echo_reply', superclass=None, members=[ |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 115 | OFDataMember('version', 'uint8_t'), # XXX |
Rich Lane | 3214287 | 2013-05-09 21:16:47 -0700 | [diff] [blame] | 116 | OFTypeMember('type', 'uint8_t', 3), |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 117 | OFLengthMember('length', 'uint16_t'), |
| 118 | OFDataMember('xid', 'uint32_t'), |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame^] | 119 | OFDataMember('data', 'of_octets_t')], virtual=False, params={}), |
| 120 | OFClass(name='of_packet_queue', superclass=None, members=[ |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 121 | OFDataMember('queue_id', 'uint32_t'), |
| 122 | OFLengthMember('len', 'uint16_t'), |
| 123 | OFPadMember(2), |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame^] | 124 | OFDataMember('properties', 'list(of_queue_prop_t)')], virtual=False, params={}), |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 125 | ] |
| 126 | self.assertEquals(expected_classes, ofinput.classes) |
| 127 | expected_enums = [ |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame^] | 128 | OFEnum(name='ofp_port_config', entries=[ |
| 129 | OFEnumEntry('OFPPC_PORT_DOWN', 1, {}), |
| 130 | OFEnumEntry('OFPPC_NO_STP', 2, {}), |
| 131 | OFEnumEntry('OFPPC_NO_RECV', 4, {}), |
| 132 | OFEnumEntry('OFPPC_NO_RECV_STP', 8, {}), |
| 133 | OFEnumEntry('OFPPC_NO_FLOOD', 16, {}), |
| 134 | OFEnumEntry('OFPPC_NO_FWD', 32, {}), |
| 135 | OFEnumEntry('OFPPC_NO_PACKET_IN', 64, {})], params={}), |
| 136 | OFEnum(name='ofp_queue_op_failed_code', entries=[ |
| 137 | OFEnumEntry('OFPQOFC_BAD_PORT', 0, {}), |
| 138 | OFEnumEntry('OFPQOFC_BAD_QUEUE', 1, {}), |
| 139 | OFEnumEntry('OFPQOFC_EPERM', 2, {})], params={}), |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 140 | ] |
| 141 | self.assertEquals(expected_enums, ofinput.enums) |
| 142 | |
Rich Lane | bd43150 | 2013-06-21 16:30:20 -0700 | [diff] [blame] | 143 | def test_inheritance(self): |
| 144 | ast = parser.parse(""" |
| 145 | #version 1 |
| 146 | |
| 147 | struct of_queue_prop { |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame^] | 148 | uint16_t type == ?; |
Rich Lane | bd43150 | 2013-06-21 16:30:20 -0700 | [diff] [blame] | 149 | uint16_t len; |
| 150 | pad(4); |
| 151 | }; |
| 152 | |
| 153 | struct of_queue_prop_min_rate : of_queue_prop { |
| 154 | uint16_t type == 1; |
| 155 | uint16_t len; |
| 156 | pad(4); |
| 157 | uint16_t rate; |
| 158 | pad(6); |
| 159 | }; |
| 160 | """) |
| 161 | |
| 162 | # Not testing the parser, just making sure the AST is what we expect |
| 163 | expected_ast = [ |
| 164 | ['metadata', 'version', '1'], |
| 165 | |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame^] | 166 | ['struct', 'of_queue_prop', [], None, [ |
| 167 | ['discriminator', 'uint16_t', 'type'], |
Rich Lane | bd43150 | 2013-06-21 16:30:20 -0700 | [diff] [blame] | 168 | ['data', 'uint16_t', 'len'], |
| 169 | ['pad', 4]]], |
| 170 | |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame^] | 171 | ['struct', 'of_queue_prop_min_rate', [], 'of_queue_prop', [ |
Rich Lane | bd43150 | 2013-06-21 16:30:20 -0700 | [diff] [blame] | 172 | ['type', 'uint16_t', 'type', 1], |
| 173 | ['data', 'uint16_t', 'len'], |
| 174 | ['pad', 4], |
| 175 | ['data', 'uint16_t', 'rate'], |
| 176 | ['pad', 6]]], |
| 177 | ] |
| 178 | self.assertEquals(expected_ast, ast) |
| 179 | |
| 180 | ofinput = frontend.create_ofinput(ast) |
| 181 | expected_classes = [ |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame^] | 182 | OFClass(name='of_queue_prop', superclass=None, members=[ |
| 183 | OFDiscriminatorMember('type', 'uint16_t'), |
Rich Lane | bd43150 | 2013-06-21 16:30:20 -0700 | [diff] [blame] | 184 | OFLengthMember('len', 'uint16_t'), |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame^] | 185 | OFPadMember(4)], virtual=True, params={}), |
| 186 | OFClass(name='of_queue_prop_min_rate', superclass='of_queue_prop', members= [ |
Rich Lane | bd43150 | 2013-06-21 16:30:20 -0700 | [diff] [blame] | 187 | OFTypeMember('type', 'uint16_t', 1), |
| 188 | OFLengthMember('len', 'uint16_t'), |
| 189 | OFPadMember(4), |
| 190 | OFDataMember('rate', 'uint16_t'), |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame^] | 191 | OFPadMember(6)], virtual=False, params= {}), |
Rich Lane | bd43150 | 2013-06-21 16:30:20 -0700 | [diff] [blame] | 192 | ] |
| 193 | self.assertEquals(expected_classes, ofinput.classes) |
| 194 | |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 195 | if __name__ == '__main__': |
| 196 | unittest.main() |