blob: 858ec4fa602dee866d2a8f54917b27abfe01c92b [file] [log] [blame]
Rich Lanea06d0c32013-03-25 08:52:03 -07001:: # Copyright 2013, Big Switch Networks, Inc.
2:: #
3:: # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
4:: # the following special exception:
5:: #
6:: # LOXI Exception
7:: #
8:: # As a special exception to the terms of the EPL, you may distribute libraries
9:: # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
10:: # that copyright and licensing notices generated by LoxiGen are not altered or removed
11:: # from the LoxiGen Libraries and the notice provided below is (i) included in
12:: # the LoxiGen Libraries, if distributed in source code form and (ii) included in any
13:: # documentation for the LoxiGen Libraries, if distributed in binary form.
14:: #
15:: # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
16:: #
17:: # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
18:: # a copy of the EPL at:
19:: #
20:: # http://www.eclipse.org/legal/epl-v10.html
21:: #
22:: # Unless required by applicable law or agreed to in writing, software
23:: # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24:: # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
25:: # EPL for the specific language governing permissions and limitations
26:: # under the EPL.
27::
Rich Laned983aa52013-06-13 11:48:37 -070028:: include('_copyright.c')
Andreas Wundsam76db0062013-11-15 13:34:41 -080029:: import c_gen.of_g_legacy as of_g
Andreas Wundsam542a13c2013-11-15 13:28:55 -080030:: import c_gen.loxi_utils_legacy as loxi_utils
31:: from c_gen import type_maps
Rich Lanea06d0c32013-03-25 08:52:03 -070032
33/**
34 * Test message validator
35 *
36 * Run the message validator on corrupt messages to ensure it catches them.
37 */
38
39#include "loci_log.h"
40
41#include <locitest/test_common.h>
42#include <loci/loci_validator.h>
43
44static int
45test_validate_fixed_length(void)
46{
47 of_table_stats_request_t *obj = of_table_stats_request_new(OF_VERSION_1_0);
48 of_message_t msg = OF_OBJECT_TO_MESSAGE(obj);
49
50 TEST_ASSERT(of_validate_message(msg, of_message_length_get(msg)) == 0);
51
Rich Lanecc327672014-04-10 13:37:35 -070052#if 0
Rich Lanea06d0c32013-03-25 08:52:03 -070053 of_message_length_set(msg, of_message_length_get(msg) - 1);
54 TEST_ASSERT(of_validate_message(msg, of_message_length_get(msg)) == -1);
Rich Lanecc327672014-04-10 13:37:35 -070055#endif
Rich Lanea06d0c32013-03-25 08:52:03 -070056
57 of_table_stats_request_delete(obj);
58 return TEST_PASS;
59}
60
61static int
62test_validate_fixed_length_list(void)
63{
64 of_table_stats_reply_t *obj = of_table_stats_reply_new(OF_VERSION_1_0);
65 of_list_table_stats_entry_t list;
66 of_table_stats_entry_t element;
67 of_message_t msg;
68 of_table_stats_reply_entries_bind(obj, &list);
69 of_table_stats_entry_init(&element, OF_VERSION_1_0, -1, 1);
70 of_list_table_stats_entry_append_bind(&list, &element);
71 of_list_table_stats_entry_append_bind(&list, &element);
72 msg = OF_OBJECT_TO_MESSAGE(obj);
73
74 TEST_ASSERT(of_validate_message(msg, of_message_length_get(msg)) == 0);
75
Rich Lanecc327672014-04-10 13:37:35 -070076#if 0
Rich Lanea06d0c32013-03-25 08:52:03 -070077 of_message_length_set(msg, of_message_length_get(msg) - 1);
78 TEST_ASSERT(of_validate_message(msg, of_message_length_get(msg)) == -1);
Rich Lanecc327672014-04-10 13:37:35 -070079#endif
Rich Lanea06d0c32013-03-25 08:52:03 -070080
81 of_table_stats_reply_delete(obj);
82 return TEST_PASS;
83}
84
85static int
86test_validate_tlv16_list(void)
87{
88 of_flow_modify_t *obj = of_flow_modify_new(OF_VERSION_1_0);
89 of_list_action_t list;
90 of_action_set_tp_dst_t element1;
91 of_action_output_t element2;
92 of_message_t msg;
93 of_flow_modify_actions_bind(obj, &list);
94 of_action_set_tp_dst_init(&element1, OF_VERSION_1_0, -1, 1);
95 of_list_action_append_bind(&list, (of_action_t *)&element1);
96 of_action_output_init(&element2, OF_VERSION_1_0, -1, 1);
97 of_list_action_append_bind(&list, (of_action_t *)&element2);
98 msg = OF_OBJECT_TO_MESSAGE(obj);
99
100 TEST_ASSERT(of_validate_message(msg, of_message_length_get(msg)) == 0);
101
Rich Lanecc327672014-04-10 13:37:35 -0700102#if 0
Rich Lanea06d0c32013-03-25 08:52:03 -0700103 of_message_length_set(msg, of_message_length_get(msg) - 1);
104 TEST_ASSERT(of_validate_message(msg, of_message_length_get(msg)) == -1);
105
106 of_message_length_set(msg, of_message_length_get(msg) + 2);
107 TEST_ASSERT(of_validate_message(msg, of_message_length_get(msg)) == -1);
Rich Lanecc327672014-04-10 13:37:35 -0700108#endif
Rich Lanea06d0c32013-03-25 08:52:03 -0700109
110 of_flow_modify_delete(obj);
111 return TEST_PASS;
112}
113
Rich Lane32b13b02013-07-12 09:46:30 -0700114/*
115 * Create an instance of every message and run it through the validator.
116 */
117static int
118test_validate_all(void)
119{
120:: for version in of_g.of_version_range:
121:: ver_name = loxi_utils.version_to_name(version)
122::
123:: for cls in reversed(of_g.standard_class_order):
124:: if not loxi_utils.class_in_version(cls, version):
125:: continue
Rich Lanea8775822013-07-22 15:47:58 -0700126:: elif type_maps.class_is_virtual(cls):
Rich Lane32b13b02013-07-12 09:46:30 -0700127:: continue
128:: elif not loxi_utils.class_is_message(cls):
129:: continue
130:: #endif
131 {
132 ${cls}_t *obj = ${cls}_new(${ver_name});
133 of_message_t msg;
134 ${cls}_${ver_name}_populate(obj, 1);
135 msg = OF_OBJECT_TO_MESSAGE(obj);
136 TEST_ASSERT(of_validate_message(msg, of_message_length_get(msg)) == 0);
137 ${cls}_delete(obj);
138 }
139
140:: #endfor
141:: #endfor
142
143 return TEST_PASS;
144}
145
Rich Lanea06d0c32013-03-25 08:52:03 -0700146int
147run_validator_tests(void)
148{
149 RUN_TEST(validate_fixed_length);
150 RUN_TEST(validate_fixed_length_list);
151 RUN_TEST(validate_tlv16_list);
Rich Lane32b13b02013-07-12 09:46:30 -0700152 RUN_TEST(validate_all);
Rich Lanea06d0c32013-03-25 08:52:03 -0700153
154 return TEST_PASS;
155}