Skip to content

Commit

Permalink
add a test that tests the template sharing better
Browse files Browse the repository at this point in the history
  • Loading branch information
simioa committed Dec 18, 2024
1 parent 00e0785 commit 6eab4f0
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions x-pack/filebeat/input/netflow/decoder/v9/v9_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ func TestCustomFields(t *testing.T) {
}

func TestSharedTemplates(t *testing.T) {
const sourceID = 1234
addr := test.MakeAddress(t, "127.0.0.1:12345")
templateAddr := test.MakeAddress(t, "127.0.0.1:12345")
flowsAddr := test.MakeAddress(t, "127.0.0.2:21234")
templatePacket := []uint16{
// Header
// Version, Count, Uptime, Ts, SeqNo, Source
Expand All @@ -264,44 +264,38 @@ func TestSharedTemplates(t *testing.T) {
2, 4,
3, 4,
}
flowsPacket := []uint16{
// Header
// Version, Count, Uptime, Ts, SeqNo, Source
9, 1, 11, 11, 22, 22, 33, 34, 0, 1234,
// Set #1 (template)
999, 16, /*len of set*/
1, 1,
2, 2,
3, 3,
}

t.Run("Template sharing enabled", func(t *testing.T) {
sharedTemplates := true
key := MakeSessionKey(addr, sourceID, sharedTemplates)
cfg := config.Defaults()
cfg.WithSharedTemplates(sharedTemplates)
cfg.WithSharedTemplates(true)
proto := New(cfg)
flows, err := proto.OnPacket(test.MakePacket(templatePacket), addr)
flows, err := proto.OnPacket(test.MakePacket(templatePacket), templateAddr)
assert.NoError(t, err)
assert.Empty(t, flows)

v9proto, ok := proto.(*NetflowV9Protocol)
assert.True(t, ok)
assert.True(t, v9proto.shareTemplates)

assert.Len(t, v9proto.Session.Sessions, 1)
s, found := v9proto.Session.Sessions[key]
assert.True(t, found)
assert.Len(t, s.Templates, 1)
flows, err = proto.OnPacket(test.MakePacket(flowsPacket), flowsAddr)
assert.NoError(t, err)
assert.Len(t, flows, 1)
})

t.Run("Template sharing disabled", func(t *testing.T) {
sharedTemplates := false
key := MakeSessionKey(addr, sourceID, sharedTemplates)
cfg := config.Defaults()
cfg.WithSharedTemplates(sharedTemplates)
cfg.WithSharedTemplates(false)
proto := New(cfg)
flows, err := proto.OnPacket(test.MakePacket(templatePacket), addr)
flows, err := proto.OnPacket(test.MakePacket(templatePacket), templateAddr)
assert.NoError(t, err)
assert.Empty(t, flows)
flows, err = proto.OnPacket(test.MakePacket(flowsPacket), flowsAddr)
assert.NoError(t, err)
assert.Empty(t, flows)

v9proto, ok := proto.(*NetflowV9Protocol)
assert.True(t, ok)
assert.False(t, v9proto.shareTemplates)

assert.Len(t, v9proto.Session.Sessions, 1)
s, found := v9proto.Session.Sessions[key]
assert.True(t, found)
assert.Len(t, s.Templates, 1)
})
}

0 comments on commit 6eab4f0

Please sign in to comment.