You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
4.0 KiB
Go
102 lines
4.0 KiB
Go
package networkobserve
|
|
|
|
import (
|
|
"net/netip"
|
|
"slices"
|
|
"testing"
|
|
)
|
|
|
|
func TestAggregatorSeparatesFamiliesDirectionsAndProtocols(t *testing.T) {
|
|
aggregator, err := NewAggregator(DefaultLimits())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
aggregator.Add(Flow{Direction: DirectionOutbound, Protocol: ProtocolTCP,
|
|
RemoteIP: netip.MustParseAddr("192.0.2.10"), LocalPort: 50001, RemotePort: 22, New: true, SYN: true, Unreplied: true})
|
|
aggregator.Add(Flow{Direction: DirectionOutbound, Protocol: ProtocolTCP,
|
|
RemoteIP: netip.MustParseAddr("192.0.2.10"), LocalPort: 50002, RemotePort: 443})
|
|
aggregator.Add(Flow{Direction: DirectionOutbound, Protocol: ProtocolUDP,
|
|
RemoteIP: netip.MustParseAddr("192.0.2.11"), LocalPort: 50003, RemotePort: 53, New: true})
|
|
aggregator.Add(Flow{IPv6: true, Direction: DirectionInbound, Protocol: ProtocolOther,
|
|
RemoteIP: netip.MustParseAddr("2001:db8::1")})
|
|
ipv4, ipv6, flags := aggregator.Result()
|
|
if len(flags) != 0 || ipv4 == nil || ipv6 == nil {
|
|
t.Fatalf("result ipv4=%#v ipv6=%#v flags=%v", ipv4, ipv6, flags)
|
|
}
|
|
out := ipv4.Outbound
|
|
if out.TCPActiveFlows != 2 || out.TCPNewFlows != 1 || out.TCPSYNFlows != 1 ||
|
|
out.TCPUnrepliedFlows != 1 || out.UDPActiveFlows != 1 || out.UDPNewFlows != 1 ||
|
|
out.UniqueRemoteIPs != 2 || out.UniqueRemotePorts != 3 || out.UniqueLocalPorts != 3 ||
|
|
out.MaxRemotePortsPerRemoteIP != 2 || out.MaxRemoteIPsPerRemotePort != 1 ||
|
|
out.MaxLocalPortsPerRemoteIP != 2 || out.MaxRemoteIPsPerLocalPort != 1 {
|
|
t.Fatalf("IPv4 outbound summary = %#v", out)
|
|
}
|
|
if ipv6.Inbound.OtherActiveFlows != 1 {
|
|
t.Fatalf("IPv6 inbound summary = %#v", ipv6.Inbound)
|
|
}
|
|
}
|
|
|
|
func TestAggregatorEnforcesHardMemoryBounds(t *testing.T) {
|
|
aggregator, err := NewAggregator(Limits{
|
|
MaxFlows: 4, MaxUniqueRemoteIPs: 2, MaxUniqueRemotePorts: 2,
|
|
MaxUniqueLocalPorts: 2, MaxRelations: 1,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
addresses := []byte{1, 1, 2, 3, 3, 3}
|
|
remotePorts := []uint16{1001, 1002, 1001, 1003, 1004, 1005}
|
|
localPorts := []uint16{2001, 2002, 2001, 2003, 2004, 2005}
|
|
for index := range addresses {
|
|
aggregator.Add(Flow{Direction: DirectionOutbound, Protocol: ProtocolTCP,
|
|
RemoteIP: netip.AddrFrom4([4]byte{192, 0, 2, addresses[index]}),
|
|
LocalPort: localPorts[index], RemotePort: remotePorts[index]})
|
|
}
|
|
ipv4, _, flags := aggregator.Result()
|
|
want := []QualityFlag{QualityFlowLimitReached, QualityIncomplete, QualityLocalPortCardinality,
|
|
QualityRelationCardinality, QualityRemoteIPCardinality, QualityRemotePortCardinality}
|
|
if !slices.Equal(flags, want) {
|
|
t.Fatalf("quality flags = %v, want %v", flags, want)
|
|
}
|
|
if ipv4.Outbound.TCPActiveFlows != 4 || ipv4.Outbound.UniqueRemoteIPs != 2 ||
|
|
ipv4.Outbound.UniqueRemotePorts != 2 || ipv4.Outbound.UniqueLocalPorts != 2 {
|
|
t.Fatalf("bounded summary = %#v", ipv4.Outbound)
|
|
}
|
|
}
|
|
|
|
func TestAggregatorRejectsMissingRemoteIdentity(t *testing.T) {
|
|
aggregator, err := NewAggregator(DefaultLimits())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
aggregator.Add(Flow{Direction: DirectionInbound, Protocol: ProtocolTCP, LocalPort: 22})
|
|
ipv4, ipv6, flags := aggregator.Result()
|
|
if ipv4 != nil || ipv6 != nil || !slices.Equal(flags, []QualityFlag{QualityIncomplete}) {
|
|
t.Fatalf("missing remote identity produced ipv4=%#v ipv6=%#v flags=%v", ipv4, ipv6, flags)
|
|
}
|
|
}
|
|
|
|
func TestInvalidLimitsAreRejected(t *testing.T) {
|
|
if _, err := NewAggregator(Limits{}); err == nil {
|
|
t.Fatal("zero limits accepted")
|
|
}
|
|
limits := DefaultLimits()
|
|
limits.MaxFlows = HardMaxFlowsPerWindow + 1
|
|
if _, err := NewAggregator(limits); err == nil {
|
|
t.Fatal("unbounded flow limit accepted")
|
|
}
|
|
}
|
|
|
|
func TestAggregatorRejectsAddressFamilyMismatch(t *testing.T) {
|
|
aggregator, err := NewAggregator(DefaultLimits())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
aggregator.Add(Flow{IPv6: false, Direction: DirectionOutbound, Protocol: ProtocolTCP,
|
|
RemoteIP: netip.MustParseAddr("2001:db8::1"), RemotePort: 443})
|
|
ipv4, ipv6, flags := aggregator.Result()
|
|
if ipv4 != nil || ipv6 != nil || !slices.Equal(flags, []QualityFlag{QualityIncomplete}) {
|
|
t.Fatalf("family mismatch produced ipv4=%#v ipv6=%#v flags=%v", ipv4, ipv6, flags)
|
|
}
|
|
}
|