* Test ported from kernel implementation: * selftest/allowedips.h */
(t *testing.T)
| 87 | * selftest/allowedips.h |
| 88 | */ |
| 89 | func TestTrieIPv4(t *testing.T) { |
| 90 | a := &Peer{} |
| 91 | b := &Peer{} |
| 92 | c := &Peer{} |
| 93 | d := &Peer{} |
| 94 | e := &Peer{} |
| 95 | g := &Peer{} |
| 96 | h := &Peer{} |
| 97 | |
| 98 | var allowedIPs AllowedIPs |
| 99 | |
| 100 | insert := func(peer *Peer, a, b, c, d byte, cidr uint8) { |
| 101 | allowedIPs.Insert(netip.PrefixFrom(netip.AddrFrom4([4]byte{a, b, c, d}), int(cidr)), peer) |
| 102 | } |
| 103 | |
| 104 | remove := func(peer *Peer, a, b, c, d byte, cidr uint8) { |
| 105 | allowedIPs.Remove(netip.PrefixFrom(netip.AddrFrom4([4]byte{a, b, c, d}), int(cidr)), peer) |
| 106 | } |
| 107 | |
| 108 | assertEQ := func(peer *Peer, a, b, c, d byte) { |
| 109 | p := allowedIPs.Lookup([]byte{a, b, c, d}) |
| 110 | if p != peer { |
| 111 | t.Error("Assert EQ failed") |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | assertNEQ := func(peer *Peer, a, b, c, d byte) { |
| 116 | p := allowedIPs.Lookup([]byte{a, b, c, d}) |
| 117 | if p == peer { |
| 118 | t.Error("Assert NEQ failed") |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | insert(a, 192, 168, 4, 0, 24) |
| 123 | insert(b, 192, 168, 4, 4, 32) |
| 124 | insert(c, 192, 168, 0, 0, 16) |
| 125 | insert(d, 192, 95, 5, 64, 27) |
| 126 | insert(c, 192, 95, 5, 65, 27) |
| 127 | insert(e, 0, 0, 0, 0, 0) |
| 128 | insert(g, 64, 15, 112, 0, 20) |
| 129 | insert(h, 64, 15, 123, 211, 25) |
| 130 | insert(a, 10, 0, 0, 0, 25) |
| 131 | insert(b, 10, 0, 0, 128, 25) |
| 132 | insert(a, 10, 1, 0, 0, 30) |
| 133 | insert(b, 10, 1, 0, 4, 30) |
| 134 | insert(c, 10, 1, 0, 8, 29) |
| 135 | insert(d, 10, 1, 0, 16, 29) |
| 136 | |
| 137 | assertEQ(a, 192, 168, 4, 20) |
| 138 | assertEQ(a, 192, 168, 4, 0) |
| 139 | assertEQ(b, 192, 168, 4, 4) |
| 140 | assertEQ(c, 192, 168, 200, 182) |
| 141 | assertEQ(c, 192, 95, 5, 68) |
| 142 | assertEQ(e, 192, 95, 5, 96) |
| 143 | assertEQ(g, 64, 15, 116, 26) |
| 144 | assertEQ(g, 64, 15, 127, 3) |
| 145 | |
| 146 | insert(a, 1, 0, 0, 0, 32) |
nothing calls this directly
no test coverage detected