@@ -15,7 +15,10 @@ package netns
1515
1616import (
1717 "flag"
18+ "fmt"
1819 "testing"
20+
21+ "tailscale.com/net/tsaddr"
1922)
2023
2124var extNetwork = flag .Bool ("use-external-network" , false , "use the external network in tests" )
@@ -76,3 +79,74 @@ func TestIsLocalhost(t *testing.T) {
7679 }
7780 }
7881}
82+
83+ func TestShouldBindToDefaultInterface (t * testing.T ) {
84+ t .Run ("Normal" , func (t * testing.T ) {
85+ tests := []struct {
86+ address string
87+ want bool
88+ }{
89+ {"127.0.0.1:0" , false },
90+ {"127.0.0.1:1234" , false },
91+ {"1.2.3.4:0" , true },
92+ {"1.2.3.4:1234" , true },
93+ }
94+
95+ for _ , test := range tests {
96+ t .Run (test .address , func (t * testing.T ) {
97+ got := shouldBindToDefaultInterface (t .Logf , test .address )
98+ if got != test .want {
99+ t .Errorf ("want %v, got %v" , test .want , got )
100+ }
101+ })
102+ }
103+ })
104+
105+ t .Run ("CoderSoftIsolation" , func (t * testing.T ) {
106+ SetCoderSoftIsolation (true )
107+ t .Cleanup (func () {
108+ SetCoderSoftIsolation (false )
109+ })
110+
111+ tests := []struct {
112+ address string
113+ want bool
114+ }{
115+ // localhost should still not bind to any interface.
116+ {"127.0.0.1:0" , false },
117+ {"127.0.0.1:0" , false },
118+ {"127.0.0.1:1234" , false },
119+ {"127.0.0.1:1234" , false },
120+
121+ // Unspecified addresses should not be bound to any interface.
122+ {":1234" , false },
123+ {":1234" , false },
124+ {"0.0.0.0:1234" , false },
125+ {"0.0.0.0:1234" , false },
126+ {"[::]:1234" , false },
127+ {"[::]:1234" , false },
128+
129+ // Special cases should always bind to default:
130+ {"[::%eth0]:1234" , true }, // zones are not supported
131+ {"a:1234" , true }, // not an IP
132+
133+ // Coder IPs should bind to default.
134+ {fmt .Sprintf ("[%s]:8080" , tsaddr .CoderServiceIPv6 ()), true },
135+ {fmt .Sprintf ("[%s]:8080" , tsaddr .CoderV6Range ().Addr ().Next ()), true },
136+
137+ // Non-Coder IPs should not bind to default.
138+ {fmt .Sprintf ("[%s]:8080" , tsaddr .TailscaleServiceIPv6 ()), false },
139+ {fmt .Sprintf ("%s:8080" , tsaddr .TailscaleServiceIP ()), false },
140+ {"1.2.3.4:8080" , false },
141+ }
142+
143+ for _ , test := range tests {
144+ t .Run (test .address , func (t * testing.T ) {
145+ got := shouldBindToDefaultInterface (t .Logf , test .address )
146+ if got != test .want {
147+ t .Errorf ("want %v, got %v" , test .want , got )
148+ }
149+ })
150+ }
151+ })
152+ }
0 commit comments