/** Returns a {@link PrefixRange} that represents all more specific prefixes. */ public static PrefixRange moreSpecificThan(Prefix prefix) { return new PrefixRange( prefix, new SubRange(prefix.getPrefixLength() + 1, Prefix.MAX_PREFIX_LENGTH)); }
/** Returns a {@link PrefixRange} that contains exactly the specified {@link Prefix}. */ public static PrefixRange fromPrefix(Prefix prefix) { int prefixLength = prefix.getPrefixLength(); return new PrefixRange(prefix, new SubRange(prefixLength, prefixLength)); }
@Override public void applyTo(RouteFilterList rfl) { int prefixLength = _prefix.getPrefixLength(); org.batfish.datamodel.RouteFilterLine line = new org.batfish.datamodel.RouteFilterLine( LineAction.PERMIT, _prefix, new SubRange(prefixLength, Prefix.MAX_PREFIX_LENGTH)); rfl.addLine(line); }
@Override public void exitFftf_first_fragment(Fftf_first_fragmentContext ctx) { SubRange subRange = new SubRange(0, 0); FwFrom from = new FwFromFragmentOffset(subRange, false); _currentFwTerm.getFroms().add(from); }
@Override public AclLineMatchExpr toAclLineMatchExpr() { return new MatchHeaderSpace( HeaderSpace.builder().setIcmpTypes(ImmutableList.of(new SubRange(_type))).build()); } }
private static SubRange toSubRange(SubrangeContext ctx) { int low = toInt(ctx.low); int high = (ctx.high != null) ? toInt(ctx.high) : low; return new SubRange(low, high); }
public SubRange getLengthRange() { if (_prefix.getPrefixLength() <= _ge && _ge <= _le) { return new SubRange(_ge, _le); } else { throw new BatfishException("Invalid length range restriction"); } }
public static MatchHeaderSpace match5Tuple( Ip srcIp, int srcPort, Ip dstIp, int dstPort, IpProtocol ipProtocol) { return new MatchHeaderSpace( HeaderSpace.builder() .setSrcIps(srcIp.toIpSpace()) .setSrcPorts(ImmutableList.of(new SubRange(srcPort, srcPort))) .setDstIps(dstIp.toIpSpace()) .setDstPorts(ImmutableList.of(new SubRange(dstPort, dstPort))) .setIpProtocols(ImmutableList.of(ipProtocol)) .build()); } }
@Override public Void visitNatRuleMatchSrcPort(NatRuleMatchSrcPort natRuleMatchSrcPort) { _headerSpace.setSrcPorts( ImmutableList.of(new SubRange(natRuleMatchSrcPort.getFrom(), natRuleMatchSrcPort.getTo()))); return null; } }
@Override public Void visitNatRuleMatchDstPort(NatRuleMatchDstPort natRuleMatchDstPort) { _headerSpace.setDstPorts( ImmutableList.of(new SubRange(natRuleMatchDstPort.getFrom(), natRuleMatchDstPort.getTo()))); return null; }
@Test public void isSingleValue() { SubRange empty = new SubRange(2, 1); SubRange base = new SubRange(0, 10); SubRange singleton = new SubRange(5, 5); assertThat(empty.isSingleValue(), equalTo(false)); assertThat(base.isSingleValue(), equalTo(false)); assertThat(singleton.isSingleValue(), equalTo(true)); } }
/** Test boundary values for ECN */ @Test public void testIsValidEcn() { assertThat(isValidEcn(IntegerSpace.of(new SubRange(0, 3))), equalTo(true)); assertThat(isValidEcn(IntegerSpace.of(new SubRange(1, 1))), equalTo(true)); assertThat(isValidEcn(IntegerSpace.of(new SubRange(-1, 0))), equalTo(false)); assertThat(isValidEcn(IntegerSpace.of(new SubRange(2, 4))), equalTo(false)); assertThat(isValidEcn(IntegerSpace.of(new SubRange(2, 1))), equalTo(false)); }
@Override public void applyTo(Route6FilterList rfl) { int prefixLength = _prefix6.getPrefixLength(); org.batfish.datamodel.Route6FilterLine line = new org.batfish.datamodel.Route6FilterLine( LineAction.PERMIT, new Ip6Wildcard( new Prefix6(_prefix6.getAddress(), prefixLength).getAddress(), _addressMask), new SubRange(prefixLength, prefixLength)); rfl.addLine(line); }
@Test public void testAreProtocolsAndPortsIncompatibleEmptyPortRange() { thrown.expect(IllegalArgumentException.class); // empty port subrange areProtocolsAndPortsCompatible( ImmutableSet.of(IpProtocol.IP), IntegerSpace.of(new SubRange(20, 1)), ImmutableSet.of(SSH)); }
@Test public void testToString() { assertThat( IntegerSpace.builder().including(new SubRange(5, 10)).excluding(9).build().toString(), equalTo("5-8,10")); assertThat(IntegerSpace.builder().build().toString(), equalTo("")); } }
@Test public void testConversionToBuilder() { IntegerSpace space = _b.including(PORTS).excluding(new SubRange(22, 22)).build(); Builder newBuilder = space.toBuilder(); newBuilder.including(Range.closed(-10, -5)); newBuilder.excluding(Range.closed(53, 53)); IntegerSpace newSpace = newBuilder.build(); assertThat("Has newly added value", newSpace.contains(-7)); assertThat("Does not have newly excluded value", !newSpace.contains(53)); }
@Test public void testValidationInvalidDstPort() { thrown.expect(IllegalArgumentException.class); PacketHeaderConstraints.builder() .setIpProtocols(ImmutableSet.of(IpProtocol.TCP)) .setDstPorts(IntegerSpace.of(new SubRange(0, 1 << 16))) .build(); }
@Test public void testValidationSrcProtocolsIncompatible() { // Reject empty port intersections thrown.expect(IllegalArgumentException.class); PacketHeaderConstraints.builder() .setApplications(ImmutableSet.of(Protocol.DNS)) .setDstPorts(IntegerSpace.of(new SubRange(1, 2))) .build(); }
@Test public void testSetSrcPortMultiple() { Builder builder = Flow.builder().setIngressNode("node").setTag("tag"); PacketHeaderConstraints phc = PacketHeaderConstraints.builder().setSrcPorts(IntegerSpace.of(new SubRange(1, 10))).build(); thrown.expect(IllegalArgumentException.class); setSrcPort(phc, builder); }