下面列出了com.google.common.collect.ImmutableMultiset#of ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void selectColumns() {
Multiset<Row> rows =
ImmutableMultiset.of(
Row.builder().put("col1", null).put("col2", 12).put("col3", 13).build(),
Row.builder().put("col1", 41).put("col2", 24).put("col3", 32).build());
Multiset<Row> selectedRows = FilterTableAnswerer.selectColumns(ImmutableSet.of("col1"), rows);
// only col1 should remain with expected values
assertThat(
selectedRows,
equalTo(
new ImmutableMultiset.Builder<Row>()
.add(Row.builder().put("col1", 41).build())
.add(Row.builder().put("col1", null).build())
.build()));
}
@Test
public void testUndefinedReference() {
ExprAclLine aclLine =
ExprAclLine.accepting().setMatchCondition(new PermittedByAcl("???")).build();
_aclb.setLines(ImmutableList.of(aclLine)).setName("acl").build();
TableAnswerElement answer = answer(new FilterLineReachabilityQuestion());
// Construct the expected result. Should find an undefined ACL result.
Multiset<Row> expected =
ImmutableMultiset.of(
Row.builder(COLUMN_METADATA)
.put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": acl"))
.put(COL_UNREACHABLE_LINE, aclLine.toString())
.put(COL_UNREACHABLE_LINE_ACTION, PERMIT)
.put(COL_BLOCKING_LINES, ImmutableList.of())
.put(COL_DIFF_ACTION, false)
.put(COL_REASON, UNDEFINED_REFERENCE)
.build());
assertThat(answer.getRows().getData(), equalTo(expected));
}
@Test
public void testWithIcmpType() {
// First line accepts IP 1.2.3.4
// Second line accepts same but only ICMP of type 8
List<AclLine> lines =
ImmutableList.of(
ExprAclLine.acceptingHeaderSpace(
HeaderSpace.builder().setSrcIps(Ip.parse("1.2.3.4").toIpSpace()).build()),
ExprAclLine.acceptingHeaderSpace(
HeaderSpace.builder()
.setSrcIps(Ip.parse("1.2.3.4").toIpSpace())
.setIpProtocols(ImmutableSet.of(IpProtocol.ICMP))
.setIcmpTypes(ImmutableList.of(new SubRange(8)))
.build()));
_aclb.setLines(lines).setName("acl").build();
List<String> lineNames = lines.stream().map(Object::toString).collect(Collectors.toList());
TableAnswerElement answer = answer(new FilterLineReachabilityQuestion());
// Construct the expected result. First line should block second.
Multiset<Row> expected =
ImmutableMultiset.of(
Row.builder(COLUMN_METADATA)
.put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": acl"))
.put(COL_UNREACHABLE_LINE, lineNames.get(1))
.put(COL_UNREACHABLE_LINE_ACTION, LineAction.PERMIT)
.put(COL_BLOCKING_LINES, ImmutableList.of(lineNames.get(0)))
.put(COL_DIFF_ACTION, false)
.put(COL_REASON, BLOCKING_LINES)
.build());
assertThat(answer.getRows().getData(), equalTo(expected));
}
@Test
public void testIpWildcards() {
// First line accepts src IPs 1.2.3.4/30
// Second line accepts src IPs 1.2.3.4/32
List<AclLine> lines =
ImmutableList.of(
ExprAclLine.acceptingHeaderSpace(
HeaderSpace.builder()
.setSrcIps(
IpWildcard.create(Prefix.create(Ip.parse("1.2.3.4"), 30)).toIpSpace())
.build()),
ExprAclLine.acceptingHeaderSpace(
HeaderSpace.builder()
.setSrcIps(IpWildcard.create(Ip.parse("1.2.3.4").toPrefix()).toIpSpace())
.build()),
ExprAclLine.acceptingHeaderSpace(
HeaderSpace.builder()
.setSrcIps(
IpWildcard.create(Prefix.create(Ip.parse("1.2.3.4"), 28)).toIpSpace())
.build()));
_aclb.setLines(lines).setName("acl").build();
List<String> lineNames = lines.stream().map(Object::toString).collect(Collectors.toList());
TableAnswerElement answer = answer(new FilterLineReachabilityQuestion());
// Construct the expected result. First line should block second.
Multiset<Row> expected =
ImmutableMultiset.of(
Row.builder(COLUMN_METADATA)
.put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": acl"))
.put(COL_UNREACHABLE_LINE, lineNames.get(1))
.put(COL_UNREACHABLE_LINE_ACTION, LineAction.PERMIT)
.put(COL_BLOCKING_LINES, ImmutableList.of(lineNames.get(0)))
.put(COL_DIFF_ACTION, false)
.put(COL_REASON, BLOCKING_LINES)
.build());
assertThat(answer.getRows().getData(), equalTo(expected));
}
@Test
public void testCycleAppearsOnce() {
// acl1 permits anything acl2 permits... twice
// acl2 permits anything acl1 permits... twice
_aclb
.setLines(
ImmutableList.of(
ExprAclLine.accepting().setMatchCondition(new PermittedByAcl("acl2")).build(),
ExprAclLine.accepting().setMatchCondition(new PermittedByAcl("acl2")).build()))
.setName("acl1")
.build();
_aclb
.setLines(
ImmutableList.of(
ExprAclLine.accepting().setMatchCondition(new PermittedByAcl("acl1")).build(),
ExprAclLine.accepting().setMatchCondition(new PermittedByAcl("acl1")).build()))
.setName("acl2")
.build();
TableAnswerElement answer = answer(new FilterLineReachabilityQuestion());
// Construct the expected result. Should find only one cycle result.
Multiset<Row> expected =
ImmutableMultiset.of(
Row.builder(COLUMN_METADATA)
.put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": acl1, acl2"))
.put(COL_UNREACHABLE_LINE, null)
.put(COL_UNREACHABLE_LINE_ACTION, null)
.put(COL_BLOCKING_LINES, null)
.put(COL_DIFF_ACTION, null)
.put(COL_REASON, CYCLICAL_REFERENCE)
.put(COL_ADDITIONAL_INFO, "Cyclic references in node 'c1': acl1 -> acl2 -> acl1")
.build());
assertThat(answer.getRows().getData(), equalTo(expected));
}
@Test
public void testMultipleCoveringLines() {
List<AclLine> aclLines =
ImmutableList.of(
acceptingHeaderSpace(
HeaderSpace.builder()
.setSrcIps(IpWildcard.parse("1.0.0.0:0.0.0.0").toIpSpace())
.build()),
acceptingHeaderSpace(
HeaderSpace.builder()
.setSrcIps(IpWildcard.parse("1.0.0.1:0.0.0.0").toIpSpace())
.build()),
acceptingHeaderSpace(
HeaderSpace.builder()
.setSrcIps(IpWildcard.parse("1.0.0.0:0.0.0.1").toIpSpace())
.build()));
IpAccessList acl = _aclb.setLines(aclLines).setName("acl").build();
List<String> lineNames = aclLines.stream().map(Object::toString).collect(Collectors.toList());
TableAnswerElement answer = answer(new FilterLineReachabilityQuestion());
/*
Construct the expected result. Line 2 should be blocked by both previous lines.
*/
Multiset<Row> expected =
ImmutableMultiset.of(
Row.builder(COLUMN_METADATA)
.put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": " + acl.getName()))
.put(COL_UNREACHABLE_LINE, lineNames.get(2))
.put(COL_UNREACHABLE_LINE_ACTION, PERMIT)
.put(COL_BLOCKING_LINES, ImmutableList.of(lineNames.get(0), lineNames.get(1)))
.put(COL_DIFF_ACTION, false)
.put(COL_REASON, BLOCKING_LINES)
.build());
assertThat(answer.getRows().getData(), equalTo(expected));
}
@Test
public void testWithSrcInterfaceReference() {
List<AclLine> aclLines =
ImmutableList.of(
ExprAclLine.accepting()
.setMatchCondition(new MatchSrcInterface(ImmutableList.of("iface", "iface2")))
.build(),
ExprAclLine.accepting()
.setMatchCondition(new MatchSrcInterface(ImmutableList.of("iface")))
.build());
IpAccessList acl = _aclb.setLines(aclLines).setName("acl").build();
List<String> lineNames = aclLines.stream().map(Object::toString).collect(Collectors.toList());
TableAnswerElement answer = answer(new FilterLineReachabilityQuestion());
/* Construct the expected result. Line 1 should be blocked by line 0. */
Multiset<Row> expected =
ImmutableMultiset.of(
Row.builder(COLUMN_METADATA)
.put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": " + acl.getName()))
.put(COL_UNREACHABLE_LINE, lineNames.get(1))
.put(COL_UNREACHABLE_LINE_ACTION, PERMIT)
.put(COL_BLOCKING_LINES, ImmutableList.of(lineNames.get(0)))
.put(COL_DIFF_ACTION, false)
.put(COL_REASON, BLOCKING_LINES)
.build());
assertThat(answer.getRows().getData(), equalTo(expected));
}
@Override
protected ImmutableMultiset<Object> _createEmpty(DeserializationContext ctxt) throws IOException {
return ImmutableMultiset.of();
}
@Override
protected ImmutableMultiset<Object> _createWithSingleElement(DeserializationContext ctxt, Object value) throws IOException {
return ImmutableMultiset.of(value);
}
@Override
public Multiset<String> getAssignedNameCounts() {
return ImmutableMultiset.of();
}
@Test
public void testCircularReferences() {
// acl0 permits anything acl1 permits
// acl1 permits anything acl2 permits, plus 1 other line to avoid acl3's line being unmatchable
// acl2 permits anything acl0 permits
// acl3 permits anything acl1 permits (not part of cycle)
_aclb
.setLines(
ImmutableList.of(
ExprAclLine.accepting().setMatchCondition(new PermittedByAcl("acl1")).build()))
.setName("acl0")
.build();
_aclb
.setLines(
ImmutableList.of(
ExprAclLine.accepting().setMatchCondition(new PermittedByAcl("acl2")).build(),
acceptingHeaderSpace(
HeaderSpace.builder()
.setSrcIps(Prefix.parse("1.0.0.0/24").toIpSpace())
.build())))
.setName("acl1")
.build();
_aclb
.setLines(
ImmutableList.of(
ExprAclLine.accepting().setMatchCondition(new PermittedByAcl("acl0")).build()))
.setName("acl2")
.build();
_aclb
.setLines(
ImmutableList.of(
ExprAclLine.accepting().setMatchCondition(new PermittedByAcl("acl1")).build()))
.setName("acl3")
.build();
TableAnswerElement answer = answer(new FilterLineReachabilityQuestion());
// Construct the expected result. Should find a single cycle result.
Multiset<Row> expected =
ImmutableMultiset.of(
Row.builder(COLUMN_METADATA)
.put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": acl0, acl1, acl2"))
.put(COL_UNREACHABLE_LINE, null)
.put(COL_UNREACHABLE_LINE_ACTION, null)
.put(COL_BLOCKING_LINES, null)
.put(COL_DIFF_ACTION, null)
.put(COL_REASON, CYCLICAL_REFERENCE)
.put(
COL_ADDITIONAL_INFO,
"Cyclic references in node 'c1': acl0 -> acl1 -> acl2 -> acl0")
.build());
assertThat(answer.getRows().getData(), equalTo(expected));
}
@Test
public void testIndirection() {
/*
Referenced ACL contains 1 line: Permit 1.0.0.0/24
Main ACL contains 2 lines:
0. Permit anything that referenced ACL permits
1. Permit 1.0.0.0/24
*/
List<AclLine> referencedAclLines =
ImmutableList.of(
acceptingHeaderSpace(
HeaderSpace.builder().setSrcIps(Prefix.parse("1.0.0.0/24").toIpSpace()).build()));
IpAccessList referencedAcl = _aclb.setLines(referencedAclLines).setName("acl1").build();
List<AclLine> aclLines =
ImmutableList.of(
ExprAclLine.accepting()
.setMatchCondition(new PermittedByAcl(referencedAcl.getName()))
.build(),
acceptingHeaderSpace(
HeaderSpace.builder().setSrcIps(Prefix.parse("1.0.0.0/24").toIpSpace()).build()));
IpAccessList acl = _aclb.setLines(aclLines).setName("acl2").build();
List<String> lineNames = aclLines.stream().map(Object::toString).collect(Collectors.toList());
/*
Runs two questions:
1. General ACL reachability (referenced ACL won't be encoded after first NoD step)
2. Reachability specifically for main ACL (referenced ACL won't be encoded at all)
Will test that both give the same result.
*/
TableAnswerElement generalAnswer = answer(new FilterLineReachabilityQuestion());
TableAnswerElement specificAnswer = answer(new FilterLineReachabilityQuestion(acl.getName()));
// Construct the expected result. Should find line 1 to be blocked by line 0 in main ACL.
Multiset<Row> expected =
ImmutableMultiset.of(
Row.builder(COLUMN_METADATA)
.put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": " + acl.getName()))
.put(COL_UNREACHABLE_LINE, lineNames.get(1))
.put(COL_UNREACHABLE_LINE_ACTION, PERMIT)
.put(COL_BLOCKING_LINES, ImmutableList.of(lineNames.get(0)))
.put(COL_DIFF_ACTION, false)
.put(COL_REASON, BLOCKING_LINES)
.build());
assertThat(generalAnswer.getRows().getData(), equalTo(expected));
assertThat(specificAnswer.getRows().getData(), equalTo(expected));
}
@Test
public void testIndependentlyUnmatchableLines() {
/*
Construct ACL with lines:
0. Reject 1.0.0.0/24 (unblocked)
1. Accept 1.0.0.0/24 (blocked by line 0)
2. Accept [empty set] (unmatchable)
3. Accept 1.0.0.0/32 (blocked by line 0)
4. Accept 1.2.3.4/32 (unblocked)
*/
List<AclLine> aclLines =
ImmutableList.of(
rejectingHeaderSpace(
HeaderSpace.builder().setSrcIps(Prefix.parse("1.0.0.0/24").toIpSpace()).build()),
acceptingHeaderSpace(
HeaderSpace.builder().setSrcIps(Prefix.parse("1.0.0.0/24").toIpSpace()).build()),
ExprAclLine.accepting().setMatchCondition(FalseExpr.INSTANCE).build(),
acceptingHeaderSpace(
HeaderSpace.builder().setSrcIps(Prefix.parse("1.0.0.0/32").toIpSpace()).build()),
acceptingHeaderSpace(
HeaderSpace.builder().setSrcIps(Prefix.parse("1.2.3.4/32").toIpSpace()).build()));
IpAccessList acl = _aclb.setLines(aclLines).setName("acl").build();
List<String> lineNames = aclLines.stream().map(Object::toString).collect(Collectors.toList());
TableAnswerElement answer = answer(new FilterLineReachabilityQuestion());
// Construct the expected result
Multiset<Row> expected =
ImmutableMultiset.of(
Row.builder(COLUMN_METADATA)
.put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": " + acl.getName()))
.put(COL_UNREACHABLE_LINE, lineNames.get(1))
.put(COL_UNREACHABLE_LINE_ACTION, PERMIT)
.put(COL_BLOCKING_LINES, ImmutableList.of(lineNames.get(0)))
.put(COL_DIFF_ACTION, true)
.put(COL_REASON, BLOCKING_LINES)
.build(),
Row.builder(COLUMN_METADATA)
.put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": " + acl.getName()))
.put(COL_UNREACHABLE_LINE, lineNames.get(2))
.put(COL_UNREACHABLE_LINE_ACTION, PERMIT)
.put(COL_BLOCKING_LINES, ImmutableList.of())
.put(COL_DIFF_ACTION, false)
.put(COL_REASON, INDEPENDENTLY_UNMATCHABLE)
.build(),
Row.builder(COLUMN_METADATA)
.put(COL_SOURCES, ImmutableList.of(_c1.getHostname() + ": " + acl.getName()))
.put(COL_UNREACHABLE_LINE, lineNames.get(3))
.put(COL_UNREACHABLE_LINE_ACTION, PERMIT)
.put(COL_BLOCKING_LINES, ImmutableList.of(lineNames.get(0)))
.put(COL_DIFF_ACTION, true)
.put(COL_REASON, BLOCKING_LINES)
.build());
assertThat(answer.getRows().getData(), equalTo(expected));
}