2024-02-20 09:59:56 +01:00
|
|
|
package posture
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/server/peer"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGeoLocationCheck_Check(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
input peer.Peer
|
|
|
|
check GeoLocationCheck
|
|
|
|
wantErr bool
|
|
|
|
isValid bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Peer location matches the location in the allow sets",
|
|
|
|
input: peer.Peer{
|
|
|
|
Location: peer.Location{
|
|
|
|
CountryCode: "DE",
|
|
|
|
CityName: "Berlin",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
check: GeoLocationCheck{
|
|
|
|
Locations: []Location{
|
|
|
|
{
|
|
|
|
CountryCode: "US",
|
|
|
|
CityName: "Los Angeles",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CountryCode: "DE",
|
|
|
|
CityName: "Berlin",
|
|
|
|
},
|
|
|
|
},
|
2024-02-22 17:22:43 +01:00
|
|
|
Action: CheckActionAllow,
|
2024-02-20 09:59:56 +01:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
isValid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Peer location matches the location in the allow country only",
|
|
|
|
input: peer.Peer{
|
|
|
|
Location: peer.Location{
|
|
|
|
CountryCode: "DE",
|
|
|
|
CityName: "Berlin",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
check: GeoLocationCheck{
|
|
|
|
Locations: []Location{
|
|
|
|
{
|
|
|
|
CountryCode: "DE",
|
|
|
|
},
|
|
|
|
},
|
2024-02-22 17:22:43 +01:00
|
|
|
Action: CheckActionAllow,
|
2024-02-20 09:59:56 +01:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
isValid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Peer location doesn't match the location in the allow sets",
|
|
|
|
input: peer.Peer{
|
|
|
|
Location: peer.Location{
|
|
|
|
CountryCode: "DE",
|
|
|
|
CityName: "Frankfurt am Main",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
check: GeoLocationCheck{
|
|
|
|
Locations: []Location{
|
|
|
|
{
|
|
|
|
CountryCode: "DE",
|
|
|
|
CityName: "Berlin",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CountryCode: "US",
|
|
|
|
CityName: "Los Angeles",
|
|
|
|
},
|
|
|
|
},
|
2024-02-22 17:22:43 +01:00
|
|
|
Action: CheckActionAllow,
|
2024-02-20 09:59:56 +01:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
isValid: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Peer location doesn't match the location in the allow country only",
|
|
|
|
input: peer.Peer{
|
|
|
|
Location: peer.Location{
|
|
|
|
CountryCode: "DE",
|
|
|
|
CityName: "Frankfurt am Main",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
check: GeoLocationCheck{
|
|
|
|
Locations: []Location{
|
|
|
|
{
|
|
|
|
CountryCode: "US",
|
|
|
|
},
|
|
|
|
},
|
2024-02-22 17:22:43 +01:00
|
|
|
Action: CheckActionAllow,
|
2024-02-20 09:59:56 +01:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
isValid: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Peer location matches the location in the deny sets",
|
|
|
|
input: peer.Peer{
|
|
|
|
Location: peer.Location{
|
|
|
|
CountryCode: "DE",
|
|
|
|
CityName: "Berlin",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
check: GeoLocationCheck{
|
|
|
|
Locations: []Location{
|
|
|
|
{
|
|
|
|
CountryCode: "DE",
|
|
|
|
CityName: "Berlin",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CountryCode: "US",
|
|
|
|
CityName: "Los Angeles",
|
|
|
|
},
|
|
|
|
},
|
2024-02-22 17:22:43 +01:00
|
|
|
Action: CheckActionDeny,
|
2024-02-20 09:59:56 +01:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
isValid: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Peer location matches the location in the deny country only",
|
|
|
|
input: peer.Peer{
|
|
|
|
Location: peer.Location{
|
|
|
|
CountryCode: "DE",
|
|
|
|
CityName: "Berlin",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
check: GeoLocationCheck{
|
|
|
|
Locations: []Location{
|
|
|
|
{
|
|
|
|
CountryCode: "DE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CountryCode: "US",
|
|
|
|
},
|
|
|
|
},
|
2024-02-22 17:22:43 +01:00
|
|
|
Action: CheckActionDeny,
|
2024-02-20 09:59:56 +01:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
isValid: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Peer location doesn't match the location in the deny sets",
|
|
|
|
input: peer.Peer{
|
|
|
|
Location: peer.Location{
|
|
|
|
CountryCode: "DE",
|
|
|
|
CityName: "Frankfurt am Main",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
check: GeoLocationCheck{
|
|
|
|
Locations: []Location{
|
|
|
|
{
|
|
|
|
CountryCode: "DE",
|
|
|
|
CityName: "Berlin",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CountryCode: "US",
|
|
|
|
CityName: "Los Angeles",
|
|
|
|
},
|
|
|
|
},
|
2024-02-22 17:22:43 +01:00
|
|
|
Action: CheckActionDeny,
|
2024-02-20 09:59:56 +01:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
isValid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Peer location doesn't match the location in the deny country only",
|
|
|
|
input: peer.Peer{
|
|
|
|
Location: peer.Location{
|
|
|
|
CountryCode: "DE",
|
|
|
|
CityName: "Frankfurt am Main",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
check: GeoLocationCheck{
|
|
|
|
Locations: []Location{
|
|
|
|
{
|
|
|
|
CountryCode: "US",
|
|
|
|
CityName: "Los Angeles",
|
|
|
|
},
|
|
|
|
},
|
2024-02-22 17:22:43 +01:00
|
|
|
Action: CheckActionDeny,
|
2024-02-20 09:59:56 +01:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
isValid: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Peer with no location in the allow sets",
|
|
|
|
input: peer.Peer{},
|
|
|
|
check: GeoLocationCheck{
|
|
|
|
Locations: []Location{
|
|
|
|
{
|
|
|
|
CountryCode: "DE",
|
|
|
|
CityName: "Berlin",
|
|
|
|
},
|
|
|
|
},
|
2024-02-22 17:22:43 +01:00
|
|
|
Action: CheckActionAllow,
|
2024-02-20 09:59:56 +01:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
isValid: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Peer with no location in the deny sets",
|
|
|
|
input: peer.Peer{},
|
|
|
|
check: GeoLocationCheck{
|
|
|
|
Locations: []Location{
|
|
|
|
{
|
|
|
|
CountryCode: "DE",
|
|
|
|
CityName: "Berlin",
|
|
|
|
},
|
|
|
|
},
|
2024-02-22 17:22:43 +01:00
|
|
|
Action: CheckActionDeny,
|
2024-02-20 09:59:56 +01:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
isValid: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
isValid, err := tt.check.Check(tt.input)
|
|
|
|
if tt.wantErr {
|
|
|
|
assert.Error(t, err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, tt.isValid, isValid)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|