Search in sources :

Example 1 with NetflowDecoder

use of loghub.netflow.NetflowDecoder in project LogHub by fbacchella.

the class PacketsTest method testDecode.

@SuppressWarnings("unchecked")
@Test
public void testDecode() {
    Decoder nfd = new NetflowDecoder();
    IpConnectionContext dummyctx = new IpConnectionContext(new InetSocketAddress(0), new InetSocketAddress(0), null);
    Arrays.stream(captures).map(i -> {
        logger.debug(i + ": ");
        return i;
    }).map(i -> "/netflow/packets/" + i).map(i -> getClass().getResourceAsStream(i)).filter(i -> i != null).map(i -> {
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buffer = new byte[8 * 1024];
            for (int length; (length = i.read(buffer)) != -1; ) {
                out.write(buffer, 0, length);
            }
            return out;
        } catch (Exception e) {
            Assert.fail(e.getMessage());
            return null;
        }
    }).filter(i -> i != null).map(i -> Unpooled.wrappedBuffer(i.toByteArray())).forEach(i -> {
        try {
            while (i.isReadable()) {
                Map<String, Object> content = nfd.decode(dummyctx, i);
                Assert.assertTrue(content.containsKey("version"));
                Assert.assertTrue(content.containsKey("sequenceNumber"));
                Assert.assertTrue(content.containsKey("records"));
                ((List<Map<String, Object>>) content.get("records")).forEach(j -> Assert.assertTrue(j.containsKey("_type")));
                if (((Integer) content.get("version")) < 10) {
                    Assert.assertTrue(content.containsKey("SysUptime"));
                }
                logger.debug(content);
            }
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    });
}
Also used : Arrays(java.util.Arrays) Decoder(loghub.Decoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BeforeClass(org.junit.BeforeClass) Level(org.apache.logging.log4j.Level) IOException(java.io.IOException) Test(org.junit.Test) LogUtils(loghub.LogUtils) InetSocketAddress(java.net.InetSocketAddress) Tools(loghub.Tools) NetflowPacket(loghub.netflow.NetflowPacket) ArrayList(java.util.ArrayList) Unpooled(io.netty.buffer.Unpooled) InetAddress(java.net.InetAddress) List(java.util.List) Logger(org.apache.logging.log4j.Logger) IpConnectionContext(loghub.IpConnectionContext) Map(java.util.Map) PacketFactory(loghub.netflow.PacketFactory) Assert(org.junit.Assert) LogManager(org.apache.logging.log4j.LogManager) NetflowDecoder(loghub.netflow.NetflowDecoder) NetflowDecoder(loghub.netflow.NetflowDecoder) InetSocketAddress(java.net.InetSocketAddress) IpConnectionContext(loghub.IpConnectionContext) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Decoder(loghub.Decoder) NetflowDecoder(loghub.netflow.NetflowDecoder) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Unpooled (io.netty.buffer.Unpooled)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Decoder (loghub.Decoder)1 IpConnectionContext (loghub.IpConnectionContext)1 LogUtils (loghub.LogUtils)1 Tools (loghub.Tools)1 NetflowDecoder (loghub.netflow.NetflowDecoder)1 NetflowPacket (loghub.netflow.NetflowPacket)1 PacketFactory (loghub.netflow.PacketFactory)1 Level (org.apache.logging.log4j.Level)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 Assert (org.junit.Assert)1