forked from etcd-io/etcd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests(etcdserver): add server_access_control_test.go
Signed-off-by: wafuwafu13 <[email protected]>
- Loading branch information
wafuwafu13
committed
Dec 15, 2022
1 parent
677b07b
commit 8dcfca0
Showing
1 changed file
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright 2016 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package etcdserver | ||
|
||
import "testing" | ||
|
||
func TestOriginAllowed(t *testing.T) { | ||
tests := []struct { | ||
accessController *AccessController | ||
origin string | ||
allowed bool | ||
}{ | ||
{ | ||
&AccessController{ | ||
CORS: map[string]struct{}{}, | ||
}, | ||
"https://example.com", | ||
true, | ||
}, | ||
{ | ||
&AccessController{ | ||
CORS: map[string]struct{}{"*": {}}, | ||
}, | ||
"https://example.com", | ||
true, | ||
}, | ||
{ | ||
&AccessController{ | ||
CORS: map[string]struct{}{"https://example.com": {}, "http://example.org": {}}, | ||
}, | ||
"https://example.com", | ||
true, | ||
}, | ||
{ | ||
&AccessController{ | ||
CORS: map[string]struct{}{"http://example.org": {}}, | ||
}, | ||
"https://example.com", | ||
false, | ||
}, | ||
{ | ||
&AccessController{ | ||
CORS: map[string]struct{}{"*": {}, "http://example.org/": {}}, | ||
}, | ||
"https://example.com", | ||
true, | ||
}, | ||
} | ||
|
||
for i, tt := range tests { | ||
allowed := tt.accessController.OriginAllowed(tt.origin) | ||
if allowed != tt.allowed { | ||
t.Fatalf("#%d: allowd = %t, want %t", i, allowed, tt.allowed) | ||
} | ||
} | ||
} | ||
|
||
func TestIsHostWhitelisted(t *testing.T) { | ||
tests := []struct{ | ||
accessController *AccessController | ||
host string | ||
whitelisted bool | ||
}{ | ||
{ | ||
&AccessController{ | ||
HostWhitelist: map[string]struct{}{}, | ||
}, | ||
"example.com", | ||
true, | ||
}, | ||
{ | ||
&AccessController{ | ||
HostWhitelist: map[string]struct{}{"*": {}}, | ||
}, | ||
"example.com", | ||
true, | ||
}, | ||
{ | ||
&AccessController{ | ||
HostWhitelist: map[string]struct{}{"example.com": {}, "example.org": {}}, | ||
}, | ||
"example.com", | ||
true, | ||
}, | ||
{ | ||
&AccessController{ | ||
HostWhitelist: map[string]struct{}{"example.org": {}}, | ||
}, | ||
"example.com", | ||
false, | ||
}, | ||
{ | ||
&AccessController{ | ||
HostWhitelist: map[string]struct{}{"*": {}, "example.org/": {}}, | ||
}, | ||
"example.com", | ||
true, | ||
}, | ||
} | ||
|
||
for i, tt := range tests { | ||
whitelisted := tt.accessController.IsHostWhitelisted(tt.host) | ||
if whitelisted != tt.whitelisted { | ||
t.Fatalf("#%d:whitelisted = %t, want %t", i, whitelisted, tt.whitelisted) | ||
} | ||
} | ||
} |