| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package vhost |
| 16 |
| 17 | import ( |
| 18 | "context" |
| 19 | "fmt" |
| 20 | "sync" |
| 21 | "testing" |
| 22 | "time" |
| 23 |
| 24 | "github.com/stretchr/testify/require" |
| 25 | ) |
| 26 |
| 27 | func TestRoutersGet(t *testing.T) { |
| 28 | routers := NewRouters() |
| 29 | require.NoError(t, routers.Add("example.com", "/api", "alice", "exact-user")) |
| 30 | require.NoError(t, routers.Add("example.com", "/public", "", "exact-all-users")) |
| 31 | require.NoError(t, routers.Add("*.example.com", "/api", "", "wildcard-all-users")) |
| 32 | require.NoError(t, routers.Add("*", "/api", "", "all-domains")) |
| 33 |
| 34 | t.Run("exact host and user match with normalized domain", func(t *testing.T) { |
| 35 | router, ok := routers.Get("EXAMPLE.COM", "/api/users", "alice") |
| 36 | require.True(t, ok) |
| 37 | require.Equal(t, "exact-user", router.payload) |
| 38 | }) |
| 39 |
| 40 | t.Run("exact host and empty user match", func(t *testing.T) { |
| 41 | router, ok := routers.Get("EXAMPLE.COM", "/public/docs", "") |
| 42 | require.True(t, ok) |
| 43 | require.Equal(t, "exact-all-users", router.payload) |
| 44 | }) |
| 45 |
| 46 | t.Run("does not fall back from named user to empty user", func(t *testing.T) { |
| 47 | // Get intentionally requires an exact HTTP user; route-level fallbacks live in getByRoute. |
| 48 | _, ok := routers.Get("EXAMPLE.COM", "/public/docs", "alice") |
| 49 | require.False(t, ok) |
| 50 | }) |
| 51 |
| 52 | t.Run("does not fall back to wildcard domains", func(t *testing.T) { |
| 53 | _, ok := routers.Get("foo.example.com", "/api/users", "") |
| 54 | require.False(t, ok) |
| 55 | }) |
| 56 |
| 57 | t.Run("does not fall back to catch-all domain", func(t *testing.T) { |
| 58 | _, ok := routers.Get("missing.test", "/api/users", "") |
| 59 | require.False(t, ok) |
| 60 | }) |
| 61 | } |
| 62 |
| 63 | func TestRoutersGetByRoute(t *testing.T) { |
| 64 | routers := NewRouters() |
| 65 | require.NoError(t, routers.Add("example.com", "/api", "alice", "exact-user")) |
| 66 | require.NoError(t, routers.Add("example.com", "/api", "", "exact-all-users")) |
| 67 | require.NoError(t, routers.Add("exact.example.com", "/api", "", "exact-subdomain")) |
| 68 | require.NoError(t, routers.Add("*.example.com", "/api", "", "wildcard-all-users")) |
| 69 | require.NoError(t, routers.Add("*.foo.example.com", "/api", "", "specific-wildcard")) |
| 70 | require.NoError(t, routers.Add("*.bar.com", "/api", "", "wildcard-parent-domain")) |
| 71 | require.NoError(t, routers.Add("*", "/admin", "root", "all-domains-user")) |
| 72 | require.NoError(t, routers.Add("*", "/", "", "all-domains")) |
| 73 |
| 74 | tests := []struct { |
| 75 | name string |
| 76 | domain string |
| 77 | location string |
| 78 | httpUser string |
| 79 | want string |
| 80 | }{ |
| 81 | { |
| 82 | name: "exact domain and http user", |
| 83 | domain: "example.com", |
| 84 | location: "/api/users", |
| 85 | httpUser: "alice", |
| 86 | want: "exact-user", |
| 87 | }, |
| 88 | { |
| 89 | name: "exact domain falls back to all users", |
| 90 | domain: "example.com", |
| 91 | location: "/api/users", |
| 92 | httpUser: "bob", |
| 93 | want: "exact-all-users", |
| 94 | }, |
| 95 | { |
| 96 | name: "wildcard domain uses all users fallback", |
| 97 | domain: "foo.example.com", |
| 98 | location: "/api/users", |
| 99 | httpUser: "bob", |
| 100 | want: "wildcard-all-users", |
| 101 | }, |
| 102 | { |
| 103 | name: "mixed-case domain is normalized", |
| 104 | domain: "Foo.Example.Com", |
| 105 | location: "/api/users", |
| 106 | httpUser: "bob", |
| 107 | want: "wildcard-all-users", |
| 108 | }, |
| 109 | { |
| 110 | name: "exact domain wins over wildcard domain", |
| 111 | domain: "exact.example.com", |
| 112 | location: "/api/users", |
| 113 | httpUser: "bob", |
| 114 | want: "exact-subdomain", |
| 115 | }, |
| 116 | { |
| 117 | name: "more specific wildcard wins over broader wildcard", |
| 118 | domain: "bar.foo.example.com", |
| 119 | location: "/api/users", |
| 120 | httpUser: "bob", |
| 121 | want: "specific-wildcard", |
| 122 | }, |
| 123 | { |
| 124 | name: "wildcard walk checks parent domains", |
| 125 | domain: "a.b.bar.com", |
| 126 | location: "/api/users", |
| 127 | httpUser: "bob", |
| 128 | want: "wildcard-parent-domain", |
| 129 | }, |
| 130 | { |
| 131 | name: "catch-all domain fallback", |
| 132 | domain: "foo.test.com", |
| 133 | location: "/other", |
| 134 | httpUser: "bob", |
| 135 | want: "all-domains", |
| 136 | }, |
| 137 | { |
| 138 | name: "catch-all domain honors http user", |
| 139 | domain: "foo.test.com", |
| 140 | location: "/admin/panel", |
| 141 | httpUser: "root", |
| 142 | want: "all-domains-user", |
| 143 | }, |
| 144 | } |
| 145 |
| 146 | for _, tt := range tests { |
| 147 | t.Run(tt.name, func(t *testing.T) { |
| 148 | router, ok := routers.getByRoute(tt.domain, tt.location, tt.httpUser) |
| 149 | require.True(t, ok) |
| 150 | require.Equal(t, tt.want, router.payload) |
| 151 | }) |
| 152 | } |
| 153 | } |
| 154 |
| 155 | func TestRoutersGetByRouteNoMatch(t *testing.T) { |
| 156 | routers := NewRouters() |
| 157 | require.NoError(t, routers.Add("*.example.com", "/api", "", "wildcard-all-users")) |
| 158 | require.NoError(t, routers.Add("*.com", "/api", "", "top-level-wildcard")) |
| 159 |
| 160 | tests := []struct { |
| 161 | name string |
| 162 | domain string |
| 163 | location string |
| 164 | }{ |
| 165 | { |
| 166 | name: "two-label domain does not enter wildcard walk", |
| 167 | domain: "example.com", |
| 168 | location: "/api/users", |
| 169 | }, |
| 170 | { |
| 171 | name: "missing catch-all remains no match", |
| 172 | domain: "foo.test.com", |
| 173 | location: "/api/users", |
| 174 | }, |
| 175 | { |
| 176 | name: "wrong path remains no match", |
| 177 | domain: "foo.example.com", |
| 178 | location: "/other", |
| 179 | }, |
| 180 | } |
| 181 |
| 182 | for _, tt := range tests { |
| 183 | t.Run(tt.name, func(t *testing.T) { |
| 184 | router, ok := routers.getByRoute(tt.domain, tt.location, "bob") |
| 185 | require.False(t, ok) |
| 186 | require.Nil(t, router) |
| 187 | }) |
| 188 | } |
| 189 | } |
| 190 |
| 191 | func TestRoutersConcurrentGetByRouteAndAdd(t *testing.T) { |
| 192 | routers := NewRouters() |
| 193 | require.NoError(t, routers.Add("*.example.com", "/api", "", "wildcard")) |
| 194 |
| 195 | const readers = 8 |
| 196 | const iterations = 200 |
| 197 |
| 198 | ctx, cancel := context.WithCancel(context.Background()) |
| 199 | defer cancel() |
| 200 | start := make(chan struct{}) |
| 201 | errCh := make(chan error, readers+1) |
| 202 | var wg sync.WaitGroup |
| 203 |
| 204 | for id := range readers { |
| 205 | wg.Go(func() { |
| 206 | <-start |
| 207 | for j := range iterations { |
| 208 | select { |
| 209 | case <-ctx.Done(): |
| 210 | return |
| 211 | default: |
| 212 | } |
| 213 | router, ok := routers.getByRoute("foo.example.com", "/api/users", "") |
| 214 | if !ok || router == nil || router.payload != "wildcard" { |
| 215 | errCh <- fmt.Errorf("reader %d iteration %d got router=%v ok=%v", id, j, router, ok) |
| 216 | return |
| 217 | } |
| 218 | } |
| 219 | }) |
| 220 | } |
| 221 |
| 222 | wg.Go(func() { |
| 223 | <-start |
| 224 | for i := range iterations { |
| 225 | select { |
| 226 | case <-ctx.Done(): |
| 227 | return |
| 228 | default: |
| 229 | } |
| 230 | err := routers.Add(fmt.Sprintf("host-%d.example.com", i), "/api", "", i) |
| 231 | if err != nil { |
| 232 | errCh <- err |
| 233 | return |
| 234 | } |
| 235 | } |
| 236 | }) |
| 237 |
| 238 | close(start) |
| 239 |
| 240 | done := make(chan struct{}) |
| 241 | go func() { |
| 242 | wg.Wait() |
| 243 | close(done) |
| 244 | }() |
| 245 |
| 246 | select { |
| 247 | case <-done: |
| 248 | case <-time.After(5 * time.Second): |
| 249 | cancel() |
| 250 | t.Fatal("concurrent route lookup and add timed out") |
| 251 | } |
| 252 |
| 253 | close(errCh) |
| 254 | for err := range errCh { |
| 255 | require.NoError(t, err) |
| 256 | } |
| 257 | } |