internal/honeypot/packetlogger/packetlogger_test.go

package packetlogger

import (
	"honeypot/internal/honeypot"
	"testing"
)

func TestFilterSubnets(t *testing.T) {
	tests := []struct {
		name     string
		input    honeypot.ScoreMap
		expected []string
	}{
		{
			name: "overlapping subnets",
			input: honeypot.ScoreMap{
				"192.168.128.0/24": honeypot.Score{Score: 500},
				"192.168.0.0/16":   honeypot.Score{Score: 1000},
			},
			expected: []string{"192.168.128.0/24"},
		},
		{
			name: "non-overlapping subnets",
			input: honeypot.ScoreMap{
				"192.168.0.0/24": honeypot.Score{Score: 500},
				"10.0.0.0/8":     honeypot.Score{Score: 1000},
			},
			expected: []string{"192.168.0.0/24", "10.0.0.0/8"},
		},
		{
			name: "multiple nested subnets",
			input: honeypot.ScoreMap{
				"192.168.1.0/24": honeypot.Score{Score: 100},
				"192.168.0.0/24": honeypot.Score{Score: 100},
				"192.168.0.0/16": honeypot.Score{Score: 500},
				"172.16.0.0/12":  honeypot.Score{Score: 1000},
				"172.16.10.0/24": honeypot.Score{Score: 200},
			},
			expected: []string{"192.168.1.0/24", "192.168.0.0/24", "172.16.10.0/24"},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := filterSubnets(tt.input)
			if len(got) != len(tt.expected) {
				t.Errorf("filterSubnets() length = %v, want %v. Got: %v", len(got), len(tt.expected), got)
			}
			for _, exp := range tt.expected {
				if _, ok := got[exp]; !ok {
					t.Errorf("filterSubnets() missing expected subnet %v. Got: %v", exp, got)
				}
			}
		})
	}
}