Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.net.flow.criteria; |
| 18 | |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 19 | import com.google.common.testing.EqualsTester; |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 20 | import org.junit.Test; |
| 21 | import org.onosproject.net.pi.runtime.PiExactFieldMatch; |
| 22 | import org.onosproject.net.pi.runtime.PiFieldMatch; |
| 23 | import org.onosproject.net.pi.runtime.PiHeaderFieldId; |
| 24 | import org.onosproject.net.pi.runtime.PiLpmFieldMatch; |
| 25 | import org.onosproject.net.pi.runtime.PiRangeFieldMatch; |
| 26 | import org.onosproject.net.pi.runtime.PiTernaryFieldMatch; |
| 27 | import org.onosproject.net.pi.runtime.PiValidFieldMatch; |
| 28 | |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 29 | import static org.hamcrest.MatcherAssert.assertThat; |
| 30 | import static org.hamcrest.Matchers.equalTo; |
| 31 | import static org.hamcrest.Matchers.instanceOf; |
| 32 | import static org.hamcrest.Matchers.is; |
| 33 | import static org.hamcrest.Matchers.notNullValue; |
| 34 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
| 35 | |
| 36 | /** |
| 37 | * Unit tests for the PiCriteria class. |
| 38 | */ |
| 39 | public class PiCriteriaTest { |
| 40 | |
| 41 | PiHeaderFieldId piEthHeaderFieldId = PiHeaderFieldId.of("ethernet_t", "etherType"); |
| 42 | byte[] matchExactBytes1 = {0x08, 0x00}; |
| 43 | byte[] matchExactBytes2 = {0x08, 0x06}; |
| 44 | Criterion matchPiExactByte1 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactBytes1).build(); |
| 45 | Criterion sameAsMatchPiExactByte1 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactBytes1).build(); |
| 46 | Criterion matchPiExactByte2 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactBytes2).build(); |
| 47 | |
| 48 | short matchExactShort1 = 0x800; |
| 49 | short matchExactShort2 = 0x806; |
| 50 | Criterion matchPiExactShort1 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactShort1).build(); |
| 51 | Criterion sameAsMatchPiExactShort1 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactShort1).build(); |
| 52 | Criterion matchPiExactShort2 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactShort2).build(); |
| 53 | |
| 54 | int matchExactInt1 = 0x800; |
| 55 | int matchExactInt2 = 0x806; |
| 56 | Criterion matchPiExactInt1 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactInt1).build(); |
| 57 | Criterion sameAsMatchPiExactInt1 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactInt1).build(); |
| 58 | Criterion matchPiExactInt2 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactInt2).build(); |
| 59 | |
| 60 | long matchExactLong1 = 0x800; |
| 61 | long matchExactLong2 = 0x806; |
| 62 | Criterion matchPiExactLong1 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactLong1).build(); |
| 63 | Criterion sameAsMatchPiExactLong1 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactLong1).build(); |
| 64 | Criterion matchPiExactLong2 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactLong2).build(); |
| 65 | |
| 66 | PiHeaderFieldId piIpv4HeaderFieldId = PiHeaderFieldId.of("ipv4_t", "dstAddr"); |
| 67 | int mask = 0x00ffffff; |
| 68 | byte[] matchLpmBytes1 = {0x0a, 0x01, 0x01, 0x01}; |
| 69 | byte[] matchLpmBytes2 = {0x0a, 0x01, 0x01, 0x02}; |
| 70 | Criterion matchPiLpmByte1 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmBytes1, mask).build(); |
| 71 | Criterion sameAsMatchPiLpmByte1 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmBytes1, mask).build(); |
| 72 | Criterion matchPiLpmByte2 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmBytes2, mask).build(); |
| 73 | |
| 74 | short matchLpmShort1 = 0x0a0a; |
| 75 | short matchLpmShort2 = 0x0a0b; |
| 76 | Criterion matchPiLpmShort1 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmShort1, mask).build(); |
| 77 | Criterion sameAsMatchPiLpmShort1 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, |
| 78 | matchLpmShort1, mask) |
| 79 | .build(); |
| 80 | Criterion matchPiLpmShort2 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmShort2, mask).build(); |
| 81 | |
| 82 | int matchLpmInt1 = 0x0a010101; |
| 83 | int matchLpmInt2 = 0x0a010102; |
| 84 | Criterion matchPiLpmInt1 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmInt1, mask).build(); |
| 85 | Criterion sameAsMatchPiLpmInt1 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmInt1, mask).build(); |
| 86 | Criterion matchPiLpmInt2 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmInt2, mask).build(); |
| 87 | |
| 88 | long matchLpmLong1 = 0x0a010101; |
| 89 | long matchLpmLong2 = 0x0a010102; |
| 90 | Criterion matchPiLpmLong1 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmLong1, mask).build(); |
| 91 | Criterion sameAsMatchPiLpmLong1 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmLong1, mask).build(); |
| 92 | Criterion matchPiLpmLong2 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmLong2, mask).build(); |
| 93 | |
| 94 | |
| 95 | byte[] matchTernaryBytes1 = {0x0a, 0x01, 0x01, 0x01}; |
| 96 | byte[] matchTernaryBytes2 = {0x0a, 0x01, 0x01, 0x02}; |
| 97 | byte[] matchTernaryMaskBytes = {0x7f, 0x7f, 0x7f, 0x00}; |
| 98 | Criterion matchPiTernaryByte1 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, |
| 99 | matchTernaryBytes1, |
| 100 | matchTernaryMaskBytes) |
| 101 | .build(); |
| 102 | Criterion sameAsMatchPiTernaryByte1 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, |
| 103 | matchTernaryBytes1, |
| 104 | matchTernaryMaskBytes) |
| 105 | .build(); |
| 106 | Criterion matchPiTernaryByte2 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, |
| 107 | matchTernaryBytes2, |
| 108 | matchTernaryMaskBytes) |
| 109 | .build(); |
| 110 | |
| 111 | short matchTernaryShort1 = 0x0a0a; |
| 112 | short matchTernaryShort2 = 0x0a0b; |
| 113 | short matchTernaryMaskShort = 0xff0; |
| 114 | Criterion matchPiTernaryShort1 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, |
| 115 | matchTernaryShort1, |
| 116 | matchTernaryMaskShort) |
| 117 | .build(); |
| 118 | Criterion sameAsMatchPiTernaryShort1 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, |
| 119 | matchTernaryShort1, |
| 120 | matchTernaryMaskShort) |
| 121 | .build(); |
| 122 | Criterion matchPiTernaryShort2 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, |
| 123 | matchTernaryShort2, |
| 124 | matchTernaryMaskShort) |
| 125 | .build(); |
| 126 | |
| 127 | int matchTernaryInt1 = 0x0a010101; |
| 128 | int matchTernaryInt2 = 0x0a010102; |
| 129 | int matchTernaryMaskInt = 0xffff; |
| 130 | Criterion matchPiTernaryInt1 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, |
| 131 | matchTernaryInt1, |
| 132 | matchTernaryMaskInt) |
| 133 | .build(); |
| 134 | Criterion sameAsMatchPiTernaryInt1 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, |
| 135 | matchTernaryInt1, |
| 136 | matchTernaryMaskInt) |
| 137 | .build(); |
| 138 | Criterion matchPiTernaryInt2 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, |
| 139 | matchTernaryInt2, |
| 140 | matchTernaryMaskInt) |
| 141 | .build(); |
| 142 | |
| 143 | long matchTernaryLong1 = 0x0a010101; |
| 144 | long matchTernaryLong2 = 0x0a010102; |
| 145 | long matchTernaryMaskLong = 0xffff; |
| 146 | Criterion matchPiTernaryLong1 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, |
| 147 | matchTernaryLong1, |
| 148 | matchTernaryMaskLong) |
| 149 | .build(); |
| 150 | Criterion sameAsMatchPiTernaryLong1 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, |
| 151 | matchTernaryLong1, |
| 152 | matchTernaryMaskLong) |
| 153 | .build(); |
| 154 | Criterion matchPiTernaryLong2 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, |
| 155 | matchTernaryLong2, |
| 156 | matchTernaryMaskLong) |
| 157 | .build(); |
| 158 | |
| 159 | Criterion matchPiValid1 = PiCriterion.builder().matchValid(piIpv4HeaderFieldId, false).build(); |
| 160 | Criterion sameAsMatchPiValid1 = PiCriterion.builder().matchValid(piIpv4HeaderFieldId, false).build(); |
| 161 | Criterion matchPiValid2 = PiCriterion.builder().matchValid(piIpv4HeaderFieldId, true).build(); |
| 162 | |
| 163 | byte[] matchRangeBytes1 = {0x10}; |
| 164 | byte[] matchRangeBytes2 = {0x20}; |
| 165 | byte[] matchRangeHighBytes = {0x30}; |
| 166 | Criterion matchPiRangeByte1 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, |
| 167 | matchRangeBytes1, |
| 168 | matchRangeHighBytes) |
| 169 | .build(); |
| 170 | Criterion sameAsMatchPiRangeByte1 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, |
| 171 | matchRangeBytes1, |
| 172 | matchRangeHighBytes) |
| 173 | .build(); |
| 174 | Criterion matchPiRangeByte2 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, |
| 175 | matchRangeBytes2, |
| 176 | matchRangeHighBytes) |
| 177 | .build(); |
| 178 | |
| 179 | short matchRangeShort1 = 0x100; |
| 180 | short matchRangeShort2 = 0x200; |
| 181 | short matchRangeHighShort = 0x300; |
| 182 | Criterion matchPiRangeShort1 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, |
| 183 | matchRangeShort1, |
| 184 | matchRangeHighShort) |
| 185 | .build(); |
| 186 | Criterion sameAsMatchPiRangeShort1 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, |
| 187 | matchRangeShort1, |
| 188 | matchRangeHighShort) |
| 189 | .build(); |
| 190 | Criterion matchPiRangeShort2 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, |
| 191 | matchRangeShort2, |
| 192 | matchRangeHighShort) |
| 193 | .build(); |
| 194 | |
| 195 | int matchRangeInt1 = 0x100; |
| 196 | int matchRangeInt2 = 0x200; |
| 197 | int matchRangeHighInt = 0x300; |
| 198 | Criterion matchPiRangeInt1 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, |
| 199 | matchRangeInt1, |
| 200 | matchRangeHighInt) |
| 201 | .build(); |
| 202 | Criterion sameAsMatchPiRangeInt1 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, |
| 203 | matchRangeInt1, |
| 204 | matchRangeHighInt) |
| 205 | .build(); |
| 206 | Criterion matchPiRangeInt2 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, |
| 207 | matchRangeInt2, |
| 208 | matchRangeHighInt) |
| 209 | .build(); |
| 210 | |
| 211 | long matchRangeLong1 = 0x100; |
| 212 | long matchRangeLong2 = 0x200; |
| 213 | long matchRangeHighLong = 0x300; |
| 214 | Criterion matchPiRangeLong1 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, |
| 215 | matchRangeLong1, |
| 216 | matchRangeHighLong) |
| 217 | .build(); |
| 218 | Criterion sameAsMatchPiRangeLong1 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, |
| 219 | matchRangeLong1, |
| 220 | matchRangeHighLong) |
| 221 | .build(); |
| 222 | Criterion matchPiRangeLong2 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, |
| 223 | matchRangeLong2, |
| 224 | matchRangeHighLong) |
| 225 | .build(); |
| 226 | |
| 227 | /** |
| 228 | * Checks that a Criterion object has the proper type, and then converts |
| 229 | * it to the proper type. |
| 230 | * |
| 231 | * @param criterion Criterion object to convert |
| 232 | * @param type Enumerated type value for the Criterion class |
| 233 | * @param clazz Desired Criterion class |
| 234 | * @param <T> The type the caller wants returned |
| 235 | * @return converted object |
| 236 | */ |
| 237 | @SuppressWarnings("unchecked") |
| 238 | private <T> T checkAndConvert(Criterion criterion, Criterion.Type type, Class clazz) { |
| 239 | assertThat(criterion, is(notNullValue())); |
| 240 | assertThat(criterion.type(), is(equalTo(type))); |
| 241 | assertThat(criterion, instanceOf(clazz)); |
| 242 | return (T) criterion; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Test the ExactMatchPi method. |
| 247 | */ |
| 248 | @Test |
| 249 | public void testExactMatchPiMethod() { |
| 250 | |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 251 | Criterion matchPiBytes = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactBytes1).build(); |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 252 | PiCriterion piCriterionBytes = checkAndConvert(matchPiBytes, Criterion.Type.PROTOCOL_INDEPENDENT, |
| 253 | PiCriterion.class); |
| 254 | PiFieldMatch expectedMatchBytes = new PiExactFieldMatch(piEthHeaderFieldId, copyFrom(matchExactBytes1)); |
| 255 | assertThat(piCriterionBytes.fieldMatches().iterator().next(), is(expectedMatchBytes)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 256 | |
| 257 | Criterion matchPiShort = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactShort1).build(); |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 258 | PiCriterion piCriterionShort = checkAndConvert(matchPiShort, Criterion.Type.PROTOCOL_INDEPENDENT, |
| 259 | PiCriterion.class); |
| 260 | PiFieldMatch expectedMatchShort = new PiExactFieldMatch(piEthHeaderFieldId, copyFrom(matchExactShort1)); |
| 261 | assertThat(piCriterionShort.fieldMatches().iterator().next(), is(expectedMatchShort)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 262 | |
| 263 | Criterion matchPiInt = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactInt1).build(); |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 264 | PiCriterion piCriterionInt = checkAndConvert(matchPiInt, Criterion.Type.PROTOCOL_INDEPENDENT, |
| 265 | PiCriterion.class); |
| 266 | PiFieldMatch expectedMatchInt = new PiExactFieldMatch(piEthHeaderFieldId, copyFrom(matchExactInt1)); |
| 267 | assertThat(piCriterionInt.fieldMatches().iterator().next(), is(expectedMatchInt)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 268 | |
| 269 | Criterion matchPiLong = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactLong1).build(); |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 270 | PiCriterion piCriterionLong = checkAndConvert(matchPiLong, Criterion.Type.PROTOCOL_INDEPENDENT, |
| 271 | PiCriterion.class); |
| 272 | PiFieldMatch expectedMatchLong = new PiExactFieldMatch(piEthHeaderFieldId, copyFrom(matchExactLong1)); |
| 273 | assertThat(piCriterionLong.fieldMatches().iterator().next(), is(expectedMatchLong)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Test the LpmMatchPi method. |
| 278 | */ |
| 279 | @Test |
| 280 | public void testLpmMatchPiMethod() { |
| 281 | |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 282 | Criterion matchPiBytes = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmBytes1, mask).build(); |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 283 | PiCriterion piCriterionBytes = checkAndConvert(matchPiBytes, Criterion.Type.PROTOCOL_INDEPENDENT, |
| 284 | PiCriterion.class); |
| 285 | PiFieldMatch expectedMatchBytes = new PiLpmFieldMatch(piIpv4HeaderFieldId, copyFrom(matchLpmBytes1), mask); |
| 286 | assertThat(piCriterionBytes.fieldMatches().iterator().next(), is(expectedMatchBytes)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 287 | |
| 288 | Criterion matchPiShort = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmShort1, mask).build(); |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 289 | PiCriterion piCriterionShort = checkAndConvert(matchPiShort, Criterion.Type.PROTOCOL_INDEPENDENT, |
| 290 | PiCriterion.class); |
| 291 | PiFieldMatch expectedMatchShort = new PiLpmFieldMatch(piIpv4HeaderFieldId, copyFrom(matchLpmShort1), mask); |
| 292 | assertThat(piCriterionShort.fieldMatches().iterator().next(), is(expectedMatchShort)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 293 | |
| 294 | Criterion matchPiInt = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmInt1, mask).build(); |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 295 | PiCriterion piCriterionInt = checkAndConvert(matchPiInt, Criterion.Type.PROTOCOL_INDEPENDENT, |
| 296 | PiCriterion.class); |
| 297 | PiFieldMatch expectedMatchInt = new PiLpmFieldMatch(piIpv4HeaderFieldId, copyFrom(matchLpmInt1), mask); |
| 298 | assertThat(piCriterionInt.fieldMatches().iterator().next(), is(expectedMatchInt)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 299 | |
| 300 | Criterion matchPiLong = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmLong1, mask).build(); |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 301 | PiCriterion piCriterionLong = checkAndConvert(matchPiLong, Criterion.Type.PROTOCOL_INDEPENDENT, |
| 302 | PiCriterion.class); |
| 303 | PiFieldMatch expectedMatchLong = new PiLpmFieldMatch(piIpv4HeaderFieldId, copyFrom(matchLpmLong1), mask); |
| 304 | assertThat(piCriterionLong.fieldMatches().iterator().next(), is(expectedMatchLong)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Test the TernaryMatchPi method. |
| 309 | */ |
| 310 | @Test |
| 311 | public void testTernaryMatchPiMethod() { |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 312 | |
| 313 | Criterion matchPiBytes = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, matchTernaryBytes1, |
| 314 | matchTernaryMaskBytes) |
| 315 | .build(); |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 316 | PiCriterion piCriterionBytes = checkAndConvert(matchPiBytes, Criterion.Type.PROTOCOL_INDEPENDENT, |
| 317 | PiCriterion.class); |
| 318 | PiFieldMatch expectedMatchBytes = new PiTernaryFieldMatch(piIpv4HeaderFieldId, copyFrom(matchTernaryBytes1), |
| 319 | copyFrom(matchTernaryMaskBytes)); |
| 320 | assertThat(piCriterionBytes.fieldMatches().iterator().next(), is(expectedMatchBytes)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 321 | |
| 322 | Criterion matchPiShort = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, matchTernaryShort1, |
| 323 | matchTernaryMaskShort) |
| 324 | .build(); |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 325 | PiCriterion piCriterionShort = checkAndConvert(matchPiShort, Criterion.Type.PROTOCOL_INDEPENDENT, |
| 326 | PiCriterion.class); |
| 327 | PiFieldMatch expectedMatchShort = new PiTernaryFieldMatch(piIpv4HeaderFieldId, copyFrom(matchTernaryShort1), |
| 328 | copyFrom(matchTernaryMaskShort)); |
| 329 | assertThat(piCriterionShort.fieldMatches().iterator().next(), is(expectedMatchShort)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 330 | |
| 331 | Criterion matchPiInt = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, matchTernaryInt1, |
| 332 | matchTernaryMaskInt) |
| 333 | .build(); |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 334 | PiCriterion piCriterionInt = checkAndConvert(matchPiInt, Criterion.Type.PROTOCOL_INDEPENDENT, |
| 335 | PiCriterion.class); |
| 336 | PiFieldMatch expectedMatchInt = new PiTernaryFieldMatch(piIpv4HeaderFieldId, copyFrom(matchTernaryInt1), |
| 337 | copyFrom(matchTernaryMaskInt)); |
| 338 | assertThat(piCriterionInt.fieldMatches().iterator().next(), is(expectedMatchInt)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 339 | |
| 340 | Criterion matchPiLong = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, matchTernaryLong1, |
| 341 | matchTernaryMaskLong) |
| 342 | .build(); |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 343 | PiCriterion piCriterionLong = checkAndConvert(matchPiLong, Criterion.Type.PROTOCOL_INDEPENDENT, |
| 344 | PiCriterion.class); |
| 345 | PiFieldMatch expectedMatchLong = new PiTernaryFieldMatch(piIpv4HeaderFieldId, copyFrom(matchTernaryLong1), |
| 346 | copyFrom(matchTernaryMaskLong)); |
| 347 | assertThat(piCriterionLong.fieldMatches().iterator().next(), is(expectedMatchLong)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Test the ValidMatchPi method. |
| 352 | */ |
| 353 | @Test |
| 354 | public void testValidMatchPiMethod() { |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 355 | |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 356 | Criterion matchPiBytes = PiCriterion.builder().matchValid(piIpv4HeaderFieldId, true).build(); |
| 357 | PiCriterion piCriterionBytes = checkAndConvert(matchPiBytes, Criterion.Type.PROTOCOL_INDEPENDENT, |
| 358 | PiCriterion.class); |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 359 | PiFieldMatch expectedMatch = new PiValidFieldMatch(piIpv4HeaderFieldId, true); |
| 360 | assertThat(piCriterionBytes.fieldMatches().iterator().next(), is(expectedMatch)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Test the RangeMatchPi method. |
| 365 | */ |
| 366 | @Test |
| 367 | public void testRangeMatchPiMethod() { |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 368 | |
| 369 | Criterion matchPiBytes = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, matchRangeBytes1, |
| 370 | matchRangeHighBytes) |
| 371 | .build(); |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 372 | PiCriterion piCriterionBytes = checkAndConvert(matchPiBytes, Criterion.Type.PROTOCOL_INDEPENDENT, |
| 373 | PiCriterion.class); |
| 374 | PiFieldMatch expectedMatchBytes = new PiRangeFieldMatch(piIpv4HeaderFieldId, copyFrom(matchRangeBytes1), |
| 375 | copyFrom(matchRangeHighBytes)); |
| 376 | assertThat(piCriterionBytes.fieldMatches().iterator().next(), is(expectedMatchBytes)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 377 | |
| 378 | Criterion matchPiShort = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, matchRangeShort1, |
| 379 | matchRangeHighShort) |
| 380 | .build(); |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 381 | PiCriterion piCriterionShort = checkAndConvert(matchPiShort, Criterion.Type.PROTOCOL_INDEPENDENT, |
| 382 | PiCriterion.class); |
| 383 | PiFieldMatch expectedMatchShort = new PiRangeFieldMatch(piIpv4HeaderFieldId, copyFrom(matchRangeShort1), |
| 384 | copyFrom(matchRangeHighShort)); |
| 385 | assertThat(piCriterionShort.fieldMatches().iterator().next(), is(expectedMatchShort)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 386 | |
| 387 | Criterion matchPiInt = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, matchRangeInt1, |
| 388 | matchRangeHighInt) |
| 389 | .build(); |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 390 | PiCriterion piCriterionInt = checkAndConvert(matchPiInt, Criterion.Type.PROTOCOL_INDEPENDENT, |
| 391 | PiCriterion.class); |
| 392 | PiFieldMatch expectedMatchInt = new PiRangeFieldMatch(piIpv4HeaderFieldId, copyFrom(matchRangeInt1), |
| 393 | copyFrom(matchRangeHighInt)); |
| 394 | assertThat(piCriterionInt.fieldMatches().iterator().next(), is(expectedMatchInt)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 395 | |
| 396 | Criterion matchPiLong = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, matchRangeLong1, |
| 397 | matchRangeHighLong) |
| 398 | .build(); |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 399 | PiCriterion piCriterionLong = checkAndConvert(matchPiLong, Criterion.Type.PROTOCOL_INDEPENDENT, |
| 400 | PiCriterion.class); |
| 401 | PiFieldMatch expectedMatchLong = new PiRangeFieldMatch(piIpv4HeaderFieldId, copyFrom(matchRangeLong1), |
| 402 | copyFrom(matchRangeHighLong)); |
| 403 | assertThat(piCriterionLong.fieldMatches().iterator().next(), is(expectedMatchLong)); |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Test the equals() method of the PiCriterion class. |
| 408 | */ |
| 409 | @Test |
| 410 | public void testPiExactCriterionEquals() { |
| 411 | new EqualsTester() |
Carmelo Cascone | 2261917 | 2017-07-13 16:08:17 -0400 | [diff] [blame] | 412 | .addEqualityGroup(matchPiExactByte1, sameAsMatchPiExactByte1) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 413 | .addEqualityGroup(matchPiExactByte2) |
| 414 | .testEquals(); |
| 415 | |
| 416 | new EqualsTester() |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 417 | .addEqualityGroup(matchPiExactShort1, sameAsMatchPiExactShort1) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 418 | .addEqualityGroup(matchPiExactShort2) |
| 419 | .testEquals(); |
| 420 | |
| 421 | new EqualsTester() |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 422 | .addEqualityGroup(matchPiExactInt1, sameAsMatchPiExactInt1) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 423 | .addEqualityGroup(matchPiExactInt2) |
| 424 | .testEquals(); |
| 425 | |
| 426 | new EqualsTester() |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 427 | .addEqualityGroup(matchPiExactLong1, sameAsMatchPiExactLong1) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 428 | .addEqualityGroup(matchPiExactLong2) |
| 429 | .testEquals(); |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Test the equals() method of the PiCriterion class. |
| 434 | */ |
| 435 | @Test |
| 436 | public void testPiLpmCriterionEquals() { |
| 437 | new EqualsTester() |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 438 | .addEqualityGroup(matchPiLpmByte1, sameAsMatchPiLpmByte1) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 439 | .addEqualityGroup(matchPiLpmByte2) |
| 440 | .testEquals(); |
| 441 | |
| 442 | new EqualsTester() |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 443 | .addEqualityGroup(matchPiLpmShort1, sameAsMatchPiLpmShort1) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 444 | .addEqualityGroup(matchPiLpmShort2) |
| 445 | .testEquals(); |
| 446 | |
| 447 | new EqualsTester() |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 448 | .addEqualityGroup(matchPiLpmInt1, sameAsMatchPiLpmInt1) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 449 | .addEqualityGroup(matchPiLpmInt2) |
| 450 | .testEquals(); |
| 451 | |
| 452 | new EqualsTester() |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 453 | .addEqualityGroup(matchPiLpmLong1, sameAsMatchPiLpmLong1) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 454 | .addEqualityGroup(matchPiLpmLong2) |
| 455 | .testEquals(); |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Test the equals() method of the PiCriterion class. |
| 460 | */ |
| 461 | @Test |
| 462 | public void testPiTernaryCriterionEquals() { |
| 463 | new EqualsTester() |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 464 | .addEqualityGroup(matchPiTernaryByte1, sameAsMatchPiTernaryByte1) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 465 | .addEqualityGroup(matchPiTernaryByte2) |
| 466 | .testEquals(); |
| 467 | |
| 468 | new EqualsTester() |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 469 | .addEqualityGroup(matchPiTernaryShort1, sameAsMatchPiTernaryShort1) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 470 | .addEqualityGroup(matchPiTernaryShort2) |
| 471 | .testEquals(); |
| 472 | |
| 473 | new EqualsTester() |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 474 | .addEqualityGroup(matchPiTernaryInt1, sameAsMatchPiTernaryInt1) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 475 | .addEqualityGroup(matchPiTernaryInt2) |
| 476 | .testEquals(); |
| 477 | |
| 478 | new EqualsTester() |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 479 | .addEqualityGroup(matchPiTernaryLong1, sameAsMatchPiTernaryLong1) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 480 | .addEqualityGroup(matchPiTernaryLong2) |
| 481 | .testEquals(); |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Test the equals() method of the PiCriterion class. |
| 486 | */ |
| 487 | @Test |
| 488 | public void testPiValidCriterionEquals() { |
| 489 | new EqualsTester() |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 490 | .addEqualityGroup(matchPiValid1, sameAsMatchPiValid1) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 491 | .addEqualityGroup(matchPiValid2) |
| 492 | .testEquals(); |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Test the equals() method of the PiCriterion class. |
| 497 | */ |
| 498 | @Test |
| 499 | public void testPiRangeCriterionEquals() { |
| 500 | new EqualsTester() |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 501 | .addEqualityGroup(matchPiRangeByte1, sameAsMatchPiRangeByte1) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 502 | .addEqualityGroup(matchPiRangeByte2) |
| 503 | .testEquals(); |
| 504 | |
| 505 | new EqualsTester() |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 506 | .addEqualityGroup(matchPiRangeShort1, sameAsMatchPiRangeShort1) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 507 | .addEqualityGroup(matchPiRangeShort2) |
| 508 | .testEquals(); |
| 509 | |
| 510 | new EqualsTester() |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 511 | .addEqualityGroup(matchPiRangeInt1, sameAsMatchPiRangeInt1) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 512 | .addEqualityGroup(matchPiRangeInt2) |
| 513 | .testEquals(); |
| 514 | |
| 515 | new EqualsTester() |
Frank Wang | 76057cd | 2017-07-15 10:22:03 +0800 | [diff] [blame] | 516 | .addEqualityGroup(matchPiRangeLong1, sameAsMatchPiRangeLong1) |
Frank Wang | e33e4ed | 2017-06-28 10:01:07 +0800 | [diff] [blame] | 517 | .addEqualityGroup(matchPiRangeLong2) |
| 518 | .testEquals(); |
| 519 | } |
| 520 | } |