From 0aed59b691507ad6a4927f588c61c3b630589646 Mon Sep 17 00:00:00 2001 From: Denton Gentry Date: Sun, 10 Jan 2021 12:14:39 -0800 Subject: [PATCH] portlist: add a test for SameInodes Exercise all cases. Signed-off-by: Denton Gentry --- portlist/portlist_test.go | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/portlist/portlist_test.go b/portlist/portlist_test.go index deda695c7..7fde87e1b 100644 --- a/portlist/portlist_test.go +++ b/portlist/portlist_test.go @@ -141,6 +141,59 @@ func TestLessThan(t *testing.T) { } } +func TestSameInodes(t *testing.T) { + port1 := Port{Proto: "tcp", Port: 100, Process: "proc", inode: "inode1"} + port2 := Port{Proto: "tcp", Port: 100, Process: "proc", inode: "inode1"} + portProto := Port{Proto: "udp", Port: 100, Process: "proc", inode: "inode1"} + portPort := Port{Proto: "tcp", Port: 101, Process: "proc", inode: "inode1"} + portInode := Port{Proto: "tcp", Port: 100, Process: "proc", inode: "inode2"} + portProcess := Port{Proto: "tcp", Port: 100, Process: "other", inode: "inode1"} + + tests := []struct { + name string + a, b List + want bool + }{ + { + "identical", + List{port1, port1}, + List{port2, port2}, + true, + }, + { + "proto differs", + List{port1, port1}, + List{port2, portProto}, + false, + }, + { + "port differs", + List{port1, port1}, + List{port2, portPort}, + false, + }, + { + "inode differs", + List{port1, port1}, + List{port2, portInode}, + false, + }, + { + // SameInodes does not check the Process field + "Process differs", + List{port1, port1}, + List{port2, portProcess}, + true, + }, + } + for _, tt := range tests { + got := tt.a.SameInodes(tt.b) + if got != tt.want { + t.Errorf("%s: Equal = %v; want %v", tt.name, got, tt.want) + } + } +} + func BenchmarkGetList(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ {