filter_test.go 4.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package multiaddr

import (
	"net"
	"testing"
)

func TestFilterListing(t *testing.T) {
	f := NewFilters()
	expected := map[string]bool{
		"1.2.3.0/24":  true,
		"4.3.2.1/32":  true,
		"fd00::/8":    true,
		"fc00::1/128": true,
	}
	for cidr := range expected {
		_, ipnet, _ := net.ParseCIDR(cidr)
18
		f.AddFilter(*ipnet, ActionDeny)
19 20
	}

21
	for _, filter := range f.FiltersForAction(ActionDeny) {
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
		cidr := filter.String()
		if expected[cidr] {
			delete(expected, cidr)
		} else {
			t.Errorf("unexected filter %s", cidr)
		}
	}
	for cidr := range expected {
		t.Errorf("expected filter %s", cidr)
	}
}

func TestFilterBlocking(t *testing.T) {
	f := NewFilters()

	_, ipnet, _ := net.ParseCIDR("0.1.2.3/24")
38 39
	f.AddFilter(*ipnet, ActionDeny)
	filters := f.FiltersForAction(ActionDeny)
40 41 42 43 44 45 46 47
	if len(filters) != 1 {
		t.Fatal("Expected only 1 filter")
	}

	if a, ok := f.ActionForFilter(*ipnet); !ok || a != ActionDeny {
		t.Fatal("Expected filter with DENY action")
	}

48
	if !f.RemoveLiteral(filters[0]) {
49 50 51 52 53 54 55 56 57 58
		t.Error("expected true value from RemoveLiteral")
	}

	for _, cidr := range []string{
		"1.2.3.0/24",
		"4.3.2.1/32",
		"fd00::/8",
		"fc00::1/128",
	} {
		_, ipnet, _ := net.ParseCIDR(cidr)
59
		f.AddFilter(*ipnet, ActionDeny)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
	}

	// These addresses should all be blocked
	for _, blocked := range []string{
		"/ip4/1.2.3.4/tcp/123",
		"/ip4/4.3.2.1/udp/123",
		"/ip6/fd00::2/tcp/321",
		"/ip6/fc00::1/udp/321",
	} {
		maddr, err := NewMultiaddr(blocked)
		if err != nil {
			t.Error(err)
		}
		if !f.AddrBlocked(maddr) {
			t.Fatalf("expected %s to be blocked", blocked)
		}
	}

	// test that other net intervals are not blocked
	for _, addr := range []string{
		"/ip4/1.2.4.1/tcp/123",
		"/ip4/4.3.2.2/udp/123",
		"/ip6/fe00::1/tcp/321",
		"/ip6/fc00::2/udp/321",
	} {
		maddr, err := NewMultiaddr(addr)
		if err != nil {
			t.Error(err)
		}
		if f.AddrBlocked(maddr) {
			t.Fatalf("expected %s to not be blocked", addr)
		}
	}
}

func TestFilterWhitelisting(t *testing.T) {
	f := NewFilters()

	// Add default reject filter
	f.DefaultAction = ActionDeny

	// Add a whitelist
	_, ipnet, _ := net.ParseCIDR("1.2.3.0/24")
	f.AddFilter(*ipnet, ActionAccept)

	if a, ok := f.ActionForFilter(*ipnet); !ok || a != ActionAccept {
		t.Fatal("Expected filter with ACCEPT action")
	}

	// That /24 should now be allowed
	for _, addr := range []string{
		"/ip4/1.2.3.1/tcp/123",
		"/ip4/1.2.3.254/tcp/123",
		"/ip4/1.2.3.254/udp/321",
	} {
		maddr, err := NewMultiaddr(addr)
		if err != nil {
			t.Error(err)
		}
		if f.AddrBlocked(maddr) {
			t.Fatalf("expected %s to be whitelisted", addr)
		}
	}

	// No policy matches these maddrs, they should be blocked by default
	for _, blocked := range []string{
		"/ip4/1.2.4.4/tcp/123",
		"/ip4/4.3.2.1/udp/123",
		"/ip6/fd00::2/tcp/321",
		"/ip6/fc00::1/udp/321",
	} {
		maddr, err := NewMultiaddr(blocked)
		if err != nil {
			t.Error(err)
		}
		if !f.AddrBlocked(maddr) {
			t.Fatalf("expected %s to be blocked", blocked)
		}
	}
}

func TestFiltersRemoveRules(t *testing.T) {
	f := NewFilters()

	ips := []string{
		"/ip4/1.2.3.1/tcp/123",
		"/ip4/1.2.3.254/tcp/123",
	}

	// Add default reject filter
	f.DefaultAction = ActionDeny

	// Add whitelisting
	_, ipnet, _ := net.ParseCIDR("1.2.3.0/24")
	f.AddFilter(*ipnet, ActionAccept)

	if a, ok := f.ActionForFilter(*ipnet); !ok || a != ActionAccept {
		t.Fatal("Expected filter with ACCEPT action")
	}

	// these are all whitelisted, should be OK
	for _, addr := range ips {
		maddr, err := NewMultiaddr(addr)
		if err != nil {
			t.Error(err)
		}
		if f.AddrBlocked(maddr) {
			t.Fatalf("expected %s to be whitelisted", addr)
		}
	}

	// Test removing the filter. It'll remove multiple, so make a dupe &
	// a complement
173
	f.AddFilter(*ipnet, ActionDeny)
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

	// Show that they all apply, these are now blacklisted & should fail
	for _, addr := range ips {
		maddr, err := NewMultiaddr(addr)
		if err != nil {
			t.Error(err)
		}
		if !f.AddrBlocked(maddr) {
			t.Fatalf("expected %s to be blacklisted", addr)
		}
	}

	// remove those rules
	if !f.RemoveLiteral(*ipnet) {
		t.Error("expected true value from RemoveLiteral")
	}

	// our default is reject, so the 1.2.3.0/24 should be rejected now,
	// along with everything else
	for _, addr := range ips {
		maddr, err := NewMultiaddr(addr)
		if err != nil {
			t.Error(err)
		}
		if !f.AddrBlocked(maddr) {
			t.Fatalf("expected %s to be blocked", addr)
		}
	}
}