blob: 2119363c88e42fa1be942884ce1d947bcd871eab [file] [log] [blame]
Rich Laned47e5a22013-05-09 14:21:16 -07001#!/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
29import unittest
30import loxi_front_end.parser as parser
31import loxi_front_end.frontend as frontend
32from loxi_ir import *
33
34class FrontendTests(unittest.TestCase):
35 maxDiff = None
36
37 def test_simple(self):
38 ast = parser.parse("""
39#version 1
40
41enum 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
53struct of_echo_reply {
54 uint8_t version;
55 uint8_t type;
56 uint16_t length;
57 uint32_t xid;
58 of_octets_t data;
59};
60
61enum ofp_queue_op_failed_code {
62 OFPQOFC_BAD_PORT = 0,
63 OFPQOFC_BAD_QUEUE = 1,
64 OFPQOFC_EPERM = 2,
65};
66
67struct 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 Lanef424e972013-05-09 21:00:13 -070088 ['data', 'uint8_t', 'version'],
89 ['data', 'uint8_t', 'type'],
90 ['data', 'uint16_t', 'length'],
91 ['data', 'uint32_t', 'xid'],
92 ['data', 'of_octets_t', 'data']]],
Rich Laned47e5a22013-05-09 14:21:16 -070093 ['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 Lanef424e972013-05-09 21:00:13 -070098 ['data', 'uint32_t', 'queue_id'],
99 ['data', 'uint16_t', 'len'],
Rich Laned47e5a22013-05-09 14:21:16 -0700100 ['pad', 2],
Rich Lanef424e972013-05-09 21:00:13 -0700101 ['data', 'list(of_queue_prop_t)', 'properties']]],
Rich Laned47e5a22013-05-09 14:21:16 -0700102 ]
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
110 OFDataMember('type', 'uint8_t'), # XXX
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
137if __name__ == '__main__':
138 unittest.main()