summaryrefslogtreecommitdiff
path: root/internal/config/bind_test.go
blob: 8e0ab4b1b17973a2855ee92002d56efc3d68e6ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package config

import "testing"

func TestValidateBindAddr(t *testing.T) {
	cases := []struct {
		addr     string
		external bool
		wantErr  bool
	}{
		{"127.0.0.1:8484", false, false}, // loopback ok
		{"localhost:8484", false, false}, // loopback ok
		{"[::1]:8484", false, false},     // ipv6 loopback ok
		{":8484", false, true},           // all interfaces, not allowed
		{"0.0.0.0:8484", false, true},    // all interfaces, not allowed
		{"192.168.1.10:8484", false, true}, // LAN ip, not allowed
		{":8484", true, false},           // all interfaces, explicitly allowed
		{"0.0.0.0:8484", true, false},    // explicitly allowed
		{"192.168.1.10:8484", true, false}, // explicitly allowed
	}
	for _, c := range cases {
		err := ValidateBindAddr(c.addr, c.external)
		if (err != nil) != c.wantErr {
			t.Errorf("ValidateBindAddr(%q, external=%v): err=%v, wantErr=%v", c.addr, c.external, err, c.wantErr)
		}
	}
}