Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1 | :: # 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 Lane | d983aa5 | 2013-06-13 11:48:37 -0700 | [diff] [blame] | 28 | :: include('_copyright.c') |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 29 | :: import c_gen.of_g_legacy as of_g |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 30 | :: import c_gen.loxi_utils_legacy as loxi_utils |
| 31 | :: from c_gen import type_maps |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 32 | |
| 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 | |
| 44 | static int |
| 45 | test_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 Lane | cc32767 | 2014-04-10 13:37:35 -0700 | [diff] [blame^] | 52 | #if 0 |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 53 | 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 Lane | cc32767 | 2014-04-10 13:37:35 -0700 | [diff] [blame^] | 55 | #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 56 | |
| 57 | of_table_stats_request_delete(obj); |
| 58 | return TEST_PASS; |
| 59 | } |
| 60 | |
| 61 | static int |
| 62 | test_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 Lane | cc32767 | 2014-04-10 13:37:35 -0700 | [diff] [blame^] | 76 | #if 0 |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 77 | 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 Lane | cc32767 | 2014-04-10 13:37:35 -0700 | [diff] [blame^] | 79 | #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 80 | |
| 81 | of_table_stats_reply_delete(obj); |
| 82 | return TEST_PASS; |
| 83 | } |
| 84 | |
| 85 | static int |
| 86 | test_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 Lane | cc32767 | 2014-04-10 13:37:35 -0700 | [diff] [blame^] | 102 | #if 0 |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 103 | 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 Lane | cc32767 | 2014-04-10 13:37:35 -0700 | [diff] [blame^] | 108 | #endif |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 109 | |
| 110 | of_flow_modify_delete(obj); |
| 111 | return TEST_PASS; |
| 112 | } |
| 113 | |
Rich Lane | 32b13b0 | 2013-07-12 09:46:30 -0700 | [diff] [blame] | 114 | /* |
| 115 | * Create an instance of every message and run it through the validator. |
| 116 | */ |
| 117 | static int |
| 118 | test_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 Lane | a877582 | 2013-07-22 15:47:58 -0700 | [diff] [blame] | 126 | :: elif type_maps.class_is_virtual(cls): |
Rich Lane | 32b13b0 | 2013-07-12 09:46:30 -0700 | [diff] [blame] | 127 | :: 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 Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 146 | int |
| 147 | run_validator_tests(void) |
| 148 | { |
| 149 | RUN_TEST(validate_fixed_length); |
| 150 | RUN_TEST(validate_fixed_length_list); |
| 151 | RUN_TEST(validate_tlv16_list); |
Rich Lane | 32b13b0 | 2013-07-12 09:46:30 -0700 | [diff] [blame] | 152 | RUN_TEST(validate_all); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 153 | |
| 154 | return TEST_PASS; |
| 155 | } |