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') |
Rich Lane | 32b13b0 | 2013-07-12 09:46:30 -0700 | [diff] [blame] | 29 | :: import of_g |
| 30 | :: from loxi_utils import loxi_utils |
| 31 | :: from loxi_front_end 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 | |
| 52 | of_message_length_set(msg, of_message_length_get(msg) - 1); |
| 53 | TEST_ASSERT(of_validate_message(msg, of_message_length_get(msg)) == -1); |
| 54 | |
| 55 | of_table_stats_request_delete(obj); |
| 56 | return TEST_PASS; |
| 57 | } |
| 58 | |
| 59 | static int |
| 60 | test_validate_fixed_length_list(void) |
| 61 | { |
| 62 | of_table_stats_reply_t *obj = of_table_stats_reply_new(OF_VERSION_1_0); |
| 63 | of_list_table_stats_entry_t list; |
| 64 | of_table_stats_entry_t element; |
| 65 | of_message_t msg; |
| 66 | of_table_stats_reply_entries_bind(obj, &list); |
| 67 | of_table_stats_entry_init(&element, OF_VERSION_1_0, -1, 1); |
| 68 | of_list_table_stats_entry_append_bind(&list, &element); |
| 69 | of_list_table_stats_entry_append_bind(&list, &element); |
| 70 | msg = OF_OBJECT_TO_MESSAGE(obj); |
| 71 | |
| 72 | TEST_ASSERT(of_validate_message(msg, of_message_length_get(msg)) == 0); |
| 73 | |
| 74 | of_message_length_set(msg, of_message_length_get(msg) - 1); |
| 75 | TEST_ASSERT(of_validate_message(msg, of_message_length_get(msg)) == -1); |
| 76 | |
| 77 | of_table_stats_reply_delete(obj); |
| 78 | return TEST_PASS; |
| 79 | } |
| 80 | |
| 81 | static int |
| 82 | test_validate_tlv16_list(void) |
| 83 | { |
| 84 | of_flow_modify_t *obj = of_flow_modify_new(OF_VERSION_1_0); |
| 85 | of_list_action_t list; |
| 86 | of_action_set_tp_dst_t element1; |
| 87 | of_action_output_t element2; |
| 88 | of_message_t msg; |
| 89 | of_flow_modify_actions_bind(obj, &list); |
| 90 | of_action_set_tp_dst_init(&element1, OF_VERSION_1_0, -1, 1); |
| 91 | of_list_action_append_bind(&list, (of_action_t *)&element1); |
| 92 | of_action_output_init(&element2, OF_VERSION_1_0, -1, 1); |
| 93 | of_list_action_append_bind(&list, (of_action_t *)&element2); |
| 94 | msg = OF_OBJECT_TO_MESSAGE(obj); |
| 95 | |
| 96 | TEST_ASSERT(of_validate_message(msg, of_message_length_get(msg)) == 0); |
| 97 | |
| 98 | of_message_length_set(msg, of_message_length_get(msg) - 1); |
| 99 | TEST_ASSERT(of_validate_message(msg, of_message_length_get(msg)) == -1); |
| 100 | |
| 101 | of_message_length_set(msg, of_message_length_get(msg) + 2); |
| 102 | TEST_ASSERT(of_validate_message(msg, of_message_length_get(msg)) == -1); |
| 103 | |
| 104 | of_flow_modify_delete(obj); |
| 105 | return TEST_PASS; |
| 106 | } |
| 107 | |
Rich Lane | 32b13b0 | 2013-07-12 09:46:30 -0700 | [diff] [blame] | 108 | /* |
| 109 | * Create an instance of every message and run it through the validator. |
| 110 | */ |
| 111 | static int |
| 112 | test_validate_all(void) |
| 113 | { |
| 114 | :: for version in of_g.of_version_range: |
| 115 | :: ver_name = loxi_utils.version_to_name(version) |
| 116 | :: |
| 117 | :: for cls in reversed(of_g.standard_class_order): |
| 118 | :: if not loxi_utils.class_in_version(cls, version): |
| 119 | :: continue |
Rich Lane | a877582 | 2013-07-22 15:47:58 -0700 | [diff] [blame] | 120 | :: elif type_maps.class_is_virtual(cls): |
Rich Lane | 32b13b0 | 2013-07-12 09:46:30 -0700 | [diff] [blame] | 121 | :: continue |
| 122 | :: elif not loxi_utils.class_is_message(cls): |
| 123 | :: continue |
| 124 | :: #endif |
| 125 | { |
| 126 | ${cls}_t *obj = ${cls}_new(${ver_name}); |
| 127 | of_message_t msg; |
| 128 | ${cls}_${ver_name}_populate(obj, 1); |
| 129 | msg = OF_OBJECT_TO_MESSAGE(obj); |
| 130 | TEST_ASSERT(of_validate_message(msg, of_message_length_get(msg)) == 0); |
| 131 | ${cls}_delete(obj); |
| 132 | } |
| 133 | |
| 134 | :: #endfor |
| 135 | :: #endfor |
| 136 | |
| 137 | return TEST_PASS; |
| 138 | } |
| 139 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 140 | int |
| 141 | run_validator_tests(void) |
| 142 | { |
| 143 | RUN_TEST(validate_fixed_length); |
| 144 | RUN_TEST(validate_fixed_length_list); |
| 145 | RUN_TEST(validate_tlv16_list); |
Rich Lane | 32b13b0 | 2013-07-12 09:46:30 -0700 | [diff] [blame] | 146 | RUN_TEST(validate_all); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 147 | |
| 148 | return TEST_PASS; |
| 149 | } |