|
| 1 | +package endpoint |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +// mapPortToPids collects info about connections between specific |
| 8 | +// address pairs and destination port. |
| 9 | + |
| 10 | +// a set of connections from a single pid to a destination off-box (pid is zero) |
| 11 | +func fakeConnectionsOffBox(count int, fromPid uint32, startPort uint16) mapPortToPids { |
| 12 | + ret := make(mapPortToPids, count) |
| 13 | + for i := 0; i < count; i++ { |
| 14 | + ret[uint16(i)+startPort] = pidPair{fromPid: fromPid, toPid: 0} |
| 15 | + } |
| 16 | + return ret |
| 17 | +} |
| 18 | + |
| 19 | +// a set of connections to a single pid from a destination off-box (pid is zero) |
| 20 | +func fakeConnectionsFromOffBox(count int, toPid uint32, startPort uint16) mapPortToPids { |
| 21 | + ret := make(mapPortToPids, count) |
| 22 | + for i := 0; i < count; i++ { |
| 23 | + ret[uint16(i)+startPort] = pidPair{fromPid: 0, toPid: toPid} |
| 24 | + } |
| 25 | + return ret |
| 26 | +} |
| 27 | + |
| 28 | +// a set of connections from a range of pids and a range of ports between two pids |
| 29 | +func fakeConnections(count int, fromPid, toPid uint32, startPort uint16) mapPortToPids { |
| 30 | + ret := make(mapPortToPids, count) |
| 31 | + for i := 0; i < count; i++ { |
| 32 | + ret[uint16(i)+startPort] = pidPair{fromPid: fromPid, toPid: toPid} |
| 33 | + } |
| 34 | + return ret |
| 35 | +} |
| 36 | + |
| 37 | +// union N existing sets |
| 38 | +func concatMapPortToPids(maps ...mapPortToPids) mapPortToPids { |
| 39 | + ret := make(mapPortToPids) |
| 40 | + for _, m := range maps { |
| 41 | + for port, pair := range m { |
| 42 | + ret[port] = pair |
| 43 | + } |
| 44 | + } |
| 45 | + return ret |
| 46 | +} |
| 47 | + |
| 48 | +func TestConnectionThinning(t *testing.T) { |
| 49 | + for _, d := range []struct { |
| 50 | + name string |
| 51 | + m mapPortToPids |
| 52 | + expC int |
| 53 | + }{ |
| 54 | + { |
| 55 | + name: "0 ports", |
| 56 | + m: mapPortToPids{}, |
| 57 | + expC: 0, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "5 connections off-box", |
| 61 | + m: fakeConnectionsOffBox(5, 1000, 30000), |
| 62 | + expC: 5, // 5 is too few to thin down |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "50 connections off-box", |
| 66 | + m: fakeConnectionsOffBox(50, 1000, 30000), |
| 67 | + expC: 5, // 50 connections should be thinned down |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "50 connections from off-box", |
| 71 | + m: fakeConnectionsFromOffBox(50, 1000, 30000), |
| 72 | + expC: 5, // 50 connections should be thinned down |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "5 connections from pid 1000 to 2000", |
| 76 | + m: fakeConnections(5, 1000, 2000, 30000), |
| 77 | + expC: 5, // 5 is too few to thin down |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "50 connections from pid 1000 to 2000", |
| 81 | + m: fakeConnections(50, 1000, 2000, 30000), |
| 82 | + expC: 5, // 50 connections should be thinned down |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "connections both ways", |
| 86 | + m: concatMapPortToPids( |
| 87 | + fakeConnections(50, 1000, 2000, 30000), |
| 88 | + fakeConnections(50, 2000, 1000, 40000)), |
| 89 | + expC: 100, // no thinning because in both directions |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "connections to two different pids", |
| 93 | + m: concatMapPortToPids( |
| 94 | + fakeConnections(50, 1000, 2000, 30000), |
| 95 | + fakeConnections(50, 1000, 3000, 40000)), |
| 96 | + expC: 100, // no thinning because two different pids |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "connections from off-box and on-box", |
| 100 | + m: concatMapPortToPids(fakeConnectionsFromOffBox(50, 1000, 30000), fakeConnections(50, 2000, 1000, 40000)), |
| 101 | + expC: 100, // no thinning because pid and no-pid |
| 102 | + }, |
| 103 | + } { |
| 104 | + t.Run(d.name, func(t *testing.T) { |
| 105 | + filter, count := makeFilter(d.m) |
| 106 | + _ = filter |
| 107 | + if d.expC != count { |
| 108 | + t.Errorf("expected count %d, got %d", d.expC, count) |
| 109 | + } |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
0 commit comments