示例#1
0
  @Test
  public void testSerialization() throws IOException, ClassNotFoundException {
    Condition cond = new Condition();
    Set<UUID> ids = new HashSet<>();
    ids.add(UUID.randomUUID());
    ids.add(UUID.randomUUID());
    cond.inPortIds = ids;
    cond.portGroup = UUID.randomUUID();
    cond.tpSrc = new Range<>(40000, 41000);
    cond.tpSrcInv = false;
    cond.tpDst = new Range<>(42000, 43000);
    cond.tpDstInv = true;
    cond.etherType = 0x86DD;

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    OutputStream out = new BufferedOutputStream(bos);
    JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(new OutputStreamWriter(out));
    jsonGenerator.writeObject(cond);
    out.close();
    byte[] data = bos.toByteArray();
    ByteArrayInputStream bis = new ByteArrayInputStream(data);
    InputStream in = new BufferedInputStream(bis);
    JsonParser jsonParser = jsonFactory.createJsonParser(new InputStreamReader(in));
    Condition c = jsonParser.readValueAs(Condition.class);
    in.close();
    Assert.assertTrue(cond.equals(c));
  }
示例#2
0
 @Test
 public void testInPortIds() {
   Condition cond = new Condition();
   UUID inPort = new UUID(rand.nextLong(), rand.nextLong());
   cond.inPortIds = new HashSet<>();
   cond.inPortIds.add(new UUID(rand.nextLong(), rand.nextLong()));
   cond.inPortIds.add(new UUID(rand.nextLong(), rand.nextLong()));
   // The condition should not match the packet.
   Assert.assertFalse(cond.matches(pktCtx));
   pktCtx.inPortId_$eq(inPort);
   Assert.assertFalse(cond.matches(pktCtx));
   // Verify that inPortInv causes a match.
   cond.inPortInv = true;
   Assert.assertTrue(cond.matches(pktCtx));
   pktCtx.inPortId_$eq(null);
   Assert.assertTrue(cond.matches(pktCtx));
   // Now add inPort to the condition - it stops matching due to invert.
   cond.inPortIds.add(inPort);
   pktCtx.inPortId_$eq(inPort);
   Assert.assertFalse(cond.matches(pktCtx));
   cond.inPortInv = false;
   Assert.assertTrue(cond.matches(pktCtx));
 }