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 | :: |
| 28 | /* Copyright 2013, Big Switch Networks, Inc. */ |
| 29 | |
| 30 | /** |
| 31 | * |
| 32 | * Match utility test cases |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | #include <locitest/test_common.h> |
| 37 | |
| 38 | int |
| 39 | test_match_utils(void) |
| 40 | { |
| 41 | of_mac_addr_t m1 = {{0,0,1,1,3,3}}; |
| 42 | of_mac_addr_t m2 = {{0,0,1,1,3,3}}; /* m1 == m2 */ |
| 43 | of_mac_addr_t m3 = {{0,0,1,1,1,1}}; /* m1 is more specific than m3 */ |
| 44 | of_mac_addr_t m4 = {{0xf,0,1,1,3,3}}; /* m1 is not more specific m4 */ |
| 45 | of_mac_addr_t m5 = {{0,0,1,1,3,0xf}}; /* m1 is not more specific m5 */ |
| 46 | |
| 47 | of_mac_addr_t m_mask1 = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}; |
| 48 | of_mac_addr_t m_mask2 = {{0xff, 0xff, 0xff, 0xff, 0, 0}}; |
| 49 | |
| 50 | /* m1 matches m2 for mask1 */ |
| 51 | /* m1 matches m2 for mask2 */ |
| 52 | /* m1 does not match m3, m4, m5 for mask1 */ |
| 53 | /* m1 matches m3, m5 for mask2 */ |
| 54 | /* m1 does not match m4 for mask2 */ |
| 55 | |
| 56 | of_ipv6_t i1 = {{0,0,0,0,1,1,1,1,3,3,3,3,7,7,7,7}}; /* same as above */ |
| 57 | of_ipv6_t i2 = {{0,0,0,0,1,1,1,1,3,3,3,3,7,7,7,7}}; |
| 58 | of_ipv6_t i3 = {{0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1}}; |
| 59 | of_ipv6_t i4 = {{0xf,0,0,0,1,1,1,1,3,3,3,3,7,7,7,7}}; |
| 60 | of_ipv6_t i5 = {{0,0,0,0,1,1,1,1,3,3,3,3,7,7,7,0xf}}; |
| 61 | |
| 62 | of_ipv6_t i_mask1 = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 63 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}; |
| 64 | of_ipv6_t i_mask2 = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 65 | 0, 0, 0, 0, 0, 0, 0, 0}}; |
| 66 | |
| 67 | /* Same relationships as above */ |
| 68 | |
| 69 | uint32_t v1 = 3; /* same as above */ |
| 70 | uint32_t v2 = 3; |
| 71 | uint32_t v3 = 1; |
| 72 | uint32_t v4 = 4; |
| 73 | uint32_t v5 = (1 << 31) | 1; |
| 74 | |
| 75 | uint32_t u32_mask1 = -1; |
| 76 | uint32_t u32_mask2 = 1; |
| 77 | |
| 78 | uint64_t w1 = 3; /* same as above */ |
| 79 | uint64_t w2 = 3; |
| 80 | uint64_t w3 = 1; |
| 81 | uint64_t w4 = 4; |
| 82 | uint64_t w5 = (1LL << 63) | 1LL; |
| 83 | |
| 84 | uint64_t u64_mask1 = -1; |
| 85 | uint64_t u64_mask2 = 1; |
| 86 | |
| 87 | /* Match structures */ |
| 88 | of_match_t match1, match2; |
| 89 | |
| 90 | TEST_ASSERT(OF_MORE_SPECIFIC_MAC_ADDR(&m1, &m2)); |
| 91 | TEST_ASSERT(OF_MORE_SPECIFIC_MAC_ADDR(&m1, &m3)); |
| 92 | TEST_ASSERT(!OF_MORE_SPECIFIC_MAC_ADDR(&m1, &m4)); |
| 93 | TEST_ASSERT(!OF_MORE_SPECIFIC_MAC_ADDR(&m1, &m5)); |
| 94 | |
| 95 | TEST_ASSERT(OF_MORE_SPECIFIC_IPV6(&i1, &i2)); |
| 96 | TEST_ASSERT(OF_MORE_SPECIFIC_IPV6(&i1, &i3)); |
| 97 | TEST_ASSERT(!OF_MORE_SPECIFIC_IPV6(&i1, &i4)); |
| 98 | TEST_ASSERT(!OF_MORE_SPECIFIC_IPV6(&i1, &i5)); |
| 99 | |
| 100 | TEST_ASSERT(OF_MORE_SPECIFIC_INT(v1, v2)); |
| 101 | TEST_ASSERT(OF_MORE_SPECIFIC_INT(v1, v3)); |
| 102 | TEST_ASSERT(!OF_MORE_SPECIFIC_INT(v1, v4)); |
| 103 | TEST_ASSERT(!OF_MORE_SPECIFIC_INT(v1, v5)); |
| 104 | |
| 105 | TEST_ASSERT(OF_MORE_SPECIFIC_INT(w1, w2)); |
| 106 | TEST_ASSERT(OF_MORE_SPECIFIC_INT(w1, w3)); |
| 107 | TEST_ASSERT(!OF_MORE_SPECIFIC_INT(w1, w4)); |
| 108 | TEST_ASSERT(!OF_MORE_SPECIFIC_INT(w1, w5)); |
| 109 | |
| 110 | /* Test restricted matches on macs */ |
| 111 | TEST_ASSERT(OF_RESTRICTED_MATCH_MAC_ADDR(&m1, &m2, &m_mask1)); |
| 112 | TEST_ASSERT(!OF_RESTRICTED_MATCH_MAC_ADDR(&m1, &m3, &m_mask1)); |
| 113 | TEST_ASSERT(!OF_RESTRICTED_MATCH_MAC_ADDR(&m1, &m4, &m_mask1)); |
| 114 | TEST_ASSERT(!OF_RESTRICTED_MATCH_MAC_ADDR(&m1, &m5, &m_mask1)); |
| 115 | |
| 116 | TEST_ASSERT(OF_RESTRICTED_MATCH_MAC_ADDR(&m1, &m2, &m_mask2)); |
| 117 | TEST_ASSERT(OF_RESTRICTED_MATCH_MAC_ADDR(&m1, &m3, &m_mask2)); |
| 118 | TEST_ASSERT(!OF_RESTRICTED_MATCH_MAC_ADDR(&m1, &m4, &m_mask2)); |
| 119 | TEST_ASSERT(OF_RESTRICTED_MATCH_MAC_ADDR(&m1, &m5, &m_mask2)); |
| 120 | |
| 121 | /* Test overlap */ |
| 122 | TEST_ASSERT(OF_OVERLAP_MAC_ADDR(&m1, &m2, &m_mask1, &m_mask2)); |
| 123 | TEST_ASSERT(OF_OVERLAP_MAC_ADDR(&m1, &m3, &m_mask1, &m_mask2)); |
| 124 | TEST_ASSERT(!OF_OVERLAP_MAC_ADDR(&m1, &m3, &m_mask1, &m_mask1)); |
| 125 | TEST_ASSERT(!OF_OVERLAP_MAC_ADDR(&m1, &m4, &m_mask1, &m_mask2)); |
| 126 | TEST_ASSERT(OF_OVERLAP_MAC_ADDR(&m1, &m5, &m_mask1, &m_mask2)); |
| 127 | |
| 128 | /* Test restricted matches on ipv6 */ |
| 129 | TEST_ASSERT(OF_RESTRICTED_MATCH_IPV6(&i1, &i2, &i_mask1)); |
| 130 | TEST_ASSERT(!OF_RESTRICTED_MATCH_IPV6(&i1, &i3, &i_mask1)); |
| 131 | TEST_ASSERT(!OF_RESTRICTED_MATCH_IPV6(&i1, &i4, &i_mask1)); |
| 132 | TEST_ASSERT(!OF_RESTRICTED_MATCH_IPV6(&i1, &i5, &i_mask1)); |
| 133 | |
| 134 | TEST_ASSERT(OF_RESTRICTED_MATCH_IPV6(&i1, &i2, &i_mask2)); |
| 135 | TEST_ASSERT(OF_RESTRICTED_MATCH_IPV6(&i1, &i3, &i_mask2)); |
| 136 | TEST_ASSERT(!OF_RESTRICTED_MATCH_IPV6(&i1, &i4, &i_mask2)); |
| 137 | TEST_ASSERT(OF_RESTRICTED_MATCH_IPV6(&i1, &i5, &i_mask2)); |
| 138 | |
| 139 | /* Test overlap */ |
| 140 | TEST_ASSERT(OF_OVERLAP_IPV6(&i1, &i2, &i_mask1, &i_mask2)); |
| 141 | TEST_ASSERT(OF_OVERLAP_IPV6(&i1, &i3, &i_mask1, &i_mask2)); |
| 142 | TEST_ASSERT(!OF_OVERLAP_IPV6(&i1, &i3, &i_mask1, &i_mask1)); |
| 143 | TEST_ASSERT(!OF_OVERLAP_IPV6(&i1, &i4, &i_mask1, &i_mask2)); |
| 144 | TEST_ASSERT(OF_OVERLAP_IPV6(&i1, &i5, &i_mask1, &i_mask2)); |
| 145 | |
| 146 | /* Test restricted matches on uint32 */ |
| 147 | TEST_ASSERT(OF_RESTRICTED_MATCH_INT(v1, v2, u32_mask1)); |
| 148 | TEST_ASSERT(!OF_RESTRICTED_MATCH_INT(v1, v3, u32_mask1)); |
| 149 | TEST_ASSERT(!OF_RESTRICTED_MATCH_INT(v1, v4, u32_mask1)); |
| 150 | TEST_ASSERT(!OF_RESTRICTED_MATCH_INT(v1, v5, u32_mask1)); |
| 151 | |
| 152 | TEST_ASSERT(OF_RESTRICTED_MATCH_INT(v1, v2, u32_mask2)); |
| 153 | TEST_ASSERT(OF_RESTRICTED_MATCH_INT(v1, v3, u32_mask2)); |
| 154 | TEST_ASSERT(!OF_RESTRICTED_MATCH_INT(v1, v4, u32_mask2)); |
| 155 | TEST_ASSERT(OF_RESTRICTED_MATCH_INT(v1, v5, u32_mask2)); |
| 156 | |
| 157 | /* Test overlap */ |
| 158 | TEST_ASSERT(OF_OVERLAP_INT(v1, v2, u32_mask1, u32_mask2)); |
| 159 | TEST_ASSERT(OF_OVERLAP_INT(v1, v3, u32_mask1, u32_mask2)); |
| 160 | TEST_ASSERT(!OF_OVERLAP_INT(v1, v3, u32_mask1, u32_mask1)); |
| 161 | TEST_ASSERT(!OF_OVERLAP_INT(v1, v4, u32_mask1, u32_mask2)); |
| 162 | TEST_ASSERT(OF_OVERLAP_INT(v1, v5, u32_mask1, u32_mask2)); |
| 163 | |
| 164 | /* Test restricted matches on uint64 */ |
| 165 | TEST_ASSERT(OF_RESTRICTED_MATCH_INT(w1, w2, u64_mask1)); |
| 166 | TEST_ASSERT(!OF_RESTRICTED_MATCH_INT(w1, w3, u64_mask1)); |
| 167 | TEST_ASSERT(!OF_RESTRICTED_MATCH_INT(w1, w4, u64_mask1)); |
| 168 | TEST_ASSERT(!OF_RESTRICTED_MATCH_INT(w1, w5, u64_mask1)); |
| 169 | |
| 170 | TEST_ASSERT(OF_RESTRICTED_MATCH_INT(w1, w2, u64_mask2)); |
| 171 | TEST_ASSERT(OF_RESTRICTED_MATCH_INT(w1, w3, u64_mask2)); |
| 172 | TEST_ASSERT(!OF_RESTRICTED_MATCH_INT(w1, w4, u64_mask2)); |
| 173 | TEST_ASSERT(OF_RESTRICTED_MATCH_INT(w1, w5, u64_mask2)); |
| 174 | |
| 175 | /* Test overlap */ |
| 176 | TEST_ASSERT(OF_OVERLAP_INT(w1, w2, u64_mask1, u64_mask2)); |
| 177 | TEST_ASSERT(OF_OVERLAP_INT(w1, w3, u64_mask1, u64_mask2)); |
| 178 | TEST_ASSERT(!OF_OVERLAP_INT(w1, w3, u64_mask1, u64_mask1)); |
| 179 | TEST_ASSERT(!OF_OVERLAP_INT(w1, w4, u64_mask1, u64_mask2)); |
| 180 | TEST_ASSERT(OF_OVERLAP_INT(w1, w5, u64_mask1, u64_mask2)); |
| 181 | |
| 182 | /* Test match stuctures */ |
| 183 | of_match_populate(&match1, OF_VERSION_1_2, 1); |
| 184 | of_match_populate(&match2, OF_VERSION_1_2, 1); |
| 185 | TEST_ASSERT(of_match_eq(&match1, &match2)); |
| 186 | TEST_ASSERT(of_match_eq(&match2, &match1)); |
| 187 | TEST_ASSERT(of_match_more_specific(&match1, &match2)); |
| 188 | TEST_ASSERT(of_match_more_specific(&match2, &match1)); |
| 189 | TEST_ASSERT(of_match_overlap(&match1, &match2)); |
| 190 | TEST_ASSERT(of_match_overlap(&match2, &match1)); |
| 191 | |
| 192 | /* Change match2 so it still is extended by match1 */ |
| 193 | memset(&match2.masks.eth_dst, 0, sizeof(of_mac_addr_t)); |
| 194 | TEST_ASSERT(of_match_more_specific(&match1, &match2)); |
| 195 | TEST_ASSERT(!of_match_more_specific(&match2, &match1)); |
| 196 | TEST_ASSERT(!of_match_eq(&match1, &match2)); |
| 197 | TEST_ASSERT(of_match_overlap(&match1, &match2)); |
| 198 | TEST_ASSERT(of_match_overlap(&match2, &match1)); |
| 199 | |
| 200 | /* Now change a value so that matches disagree */ |
| 201 | match2.fields.in_port++; |
| 202 | TEST_ASSERT(!of_match_more_specific(&match1, &match2)); |
| 203 | TEST_ASSERT(!of_match_overlap(&match1, &match2)); |
| 204 | TEST_ASSERT(!of_match_overlap(&match2, &match1)); |
| 205 | /* Clear inport mask on match2 and should extend again */ |
| 206 | match2.masks.in_port = 0; |
| 207 | TEST_ASSERT(of_match_more_specific(&match1, &match2)); |
| 208 | TEST_ASSERT(of_match_overlap(&match1, &match2)); |
| 209 | TEST_ASSERT(of_match_overlap(&match2, &match1)); |
| 210 | |
| 211 | /* Now change mask so the overlap, but not more specific */ |
| 212 | match1.fields.in_port = 0x7; |
| 213 | match1.masks.in_port = 0x7; |
| 214 | match2.fields.in_port = 0xe; |
| 215 | match2.masks.in_port = 0xe; |
| 216 | TEST_ASSERT(!of_match_more_specific(&match1, &match2)); |
| 217 | TEST_ASSERT(!of_match_more_specific(&match2, &match1)); |
| 218 | TEST_ASSERT(of_match_overlap(&match1, &match2)); |
| 219 | TEST_ASSERT(of_match_overlap(&match2, &match1)); |
| 220 | |
| 221 | return TEST_PASS; |
| 222 | } |