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 | |
| 29 | import unittest |
| 30 | import loxi_front_end.parser as parser |
| 31 | import loxi_front_end.frontend as frontend |
| 32 | from loxi_ir import * |
| 33 | |
| 34 | class FrontendTests(unittest.TestCase): |
| 35 | maxDiff = None |
| 36 | |
| 37 | def test_simple(self): |
| 38 | ast = parser.parse(""" |
| 39 | #version 1 |
| 40 | |
| 41 | enum ofp_port_config { |
| 42 | OFPPC_PORT_DOWN = 0x1, |
| 43 | OFPPC_NO_STP = 0x2, |
| 44 | OFPPC_NO_RECV = 0x4, |
| 45 | OFPPC_NO_RECV_STP = 0x8, |
| 46 | OFPPC_NO_FLOOD = 0x10, |
| 47 | OFPPC_NO_FWD = 0x20, |
| 48 | OFPPC_NO_PACKET_IN = 0x40, |
| 49 | }; |
| 50 | |
| 51 | #version 2 |
| 52 | |
| 53 | struct of_echo_reply { |
| 54 | uint8_t version; |
Rich Lane | 3214287 | 2013-05-09 21:16:47 -0700 | [diff] [blame] | 55 | uint8_t type == 3; |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 56 | uint16_t length; |
| 57 | uint32_t xid; |
| 58 | of_octets_t data; |
| 59 | }; |
| 60 | |
| 61 | enum ofp_queue_op_failed_code { |
| 62 | OFPQOFC_BAD_PORT = 0, |
| 63 | OFPQOFC_BAD_QUEUE = 1, |
| 64 | OFPQOFC_EPERM = 2, |
| 65 | }; |
| 66 | |
| 67 | struct of_packet_queue { |
| 68 | uint32_t queue_id; |
| 69 | uint16_t len; |
| 70 | pad(2); |
| 71 | list(of_queue_prop_t) properties; |
| 72 | }; |
| 73 | """) |
| 74 | |
| 75 | # Not testing the parser, just making sure the AST is what we expect |
| 76 | expected_ast = [ |
| 77 | ['metadata', 'version', '1'], |
| 78 | ['enum', 'ofp_port_config', [ |
| 79 | ['OFPPC_PORT_DOWN', 1], |
| 80 | ['OFPPC_NO_STP', 2], |
| 81 | ['OFPPC_NO_RECV', 4], |
| 82 | ['OFPPC_NO_RECV_STP', 8], |
| 83 | ['OFPPC_NO_FLOOD', 16], |
| 84 | ['OFPPC_NO_FWD', 32], |
| 85 | ['OFPPC_NO_PACKET_IN', 64]]], |
| 86 | ['metadata', 'version', '2'], |
| 87 | ['struct', 'of_echo_reply', [ |
Rich Lane | f424e97 | 2013-05-09 21:00:13 -0700 | [diff] [blame] | 88 | ['data', 'uint8_t', 'version'], |
Rich Lane | 3214287 | 2013-05-09 21:16:47 -0700 | [diff] [blame] | 89 | ['type', 'uint8_t', 'type', 3], |
Rich Lane | f424e97 | 2013-05-09 21:00:13 -0700 | [diff] [blame] | 90 | ['data', 'uint16_t', 'length'], |
| 91 | ['data', 'uint32_t', 'xid'], |
| 92 | ['data', 'of_octets_t', 'data']]], |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 93 | ['enum', 'ofp_queue_op_failed_code', [ |
| 94 | ['OFPQOFC_BAD_PORT', 0], |
| 95 | ['OFPQOFC_BAD_QUEUE', 1], |
| 96 | ['OFPQOFC_EPERM', 2]]], |
| 97 | ['struct', 'of_packet_queue', [ |
Rich Lane | f424e97 | 2013-05-09 21:00:13 -0700 | [diff] [blame] | 98 | ['data', 'uint32_t', 'queue_id'], |
| 99 | ['data', 'uint16_t', 'len'], |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 100 | ['pad', 2], |
Rich Lane | f424e97 | 2013-05-09 21:00:13 -0700 | [diff] [blame] | 101 | ['data', 'list(of_queue_prop_t)', 'properties']]], |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 102 | ] |
| 103 | self.assertEquals(expected_ast, ast) |
| 104 | |
| 105 | ofinput = frontend.create_ofinput(ast) |
| 106 | self.assertEquals(set([1, 2]), ofinput.wire_versions) |
| 107 | expected_classes = [ |
| 108 | OFClass('of_echo_reply', [ |
| 109 | OFDataMember('version', 'uint8_t'), # XXX |
Rich Lane | 3214287 | 2013-05-09 21:16:47 -0700 | [diff] [blame] | 110 | OFTypeMember('type', 'uint8_t', 3), |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 111 | OFLengthMember('length', 'uint16_t'), |
| 112 | OFDataMember('xid', 'uint32_t'), |
| 113 | OFDataMember('data', 'of_octets_t')]), |
| 114 | OFClass('of_packet_queue', [ |
| 115 | OFDataMember('queue_id', 'uint32_t'), |
| 116 | OFLengthMember('len', 'uint16_t'), |
| 117 | OFPadMember(2), |
| 118 | OFDataMember('properties', 'list(of_queue_prop_t)')]), |
| 119 | ] |
| 120 | self.assertEquals(expected_classes, ofinput.classes) |
| 121 | expected_enums = [ |
| 122 | OFEnum('ofp_port_config', [ |
| 123 | ('OFPPC_PORT_DOWN', 1), |
| 124 | ('OFPPC_NO_STP', 2), |
| 125 | ('OFPPC_NO_RECV', 4), |
| 126 | ('OFPPC_NO_RECV_STP', 8), |
| 127 | ('OFPPC_NO_FLOOD', 16), |
| 128 | ('OFPPC_NO_FWD', 32), |
| 129 | ('OFPPC_NO_PACKET_IN', 64)]), |
| 130 | OFEnum('ofp_queue_op_failed_code', [ |
| 131 | ('OFPQOFC_BAD_PORT', 0), |
| 132 | ('OFPQOFC_BAD_QUEUE', 1), |
| 133 | ('OFPQOFC_EPERM', 2)]), |
| 134 | ] |
| 135 | self.assertEquals(expected_enums, ofinput.enums) |
| 136 | |
| 137 | if __name__ == '__main__': |
| 138 | unittest.main() |