Compare commits
1 Commits
v2024.5.22
...
v2024.5.22
Author | SHA1 | Date | |
---|---|---|---|
499bb3696d |
@ -57,6 +57,8 @@ func (a *Authenticator) matchAnyAuthorizedCIDRs(ctx context.Context, remoteHostP
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return false, errors.WithStack(err)
|
return false, errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
remoteHost = remoteHostPort
|
||||||
}
|
}
|
||||||
|
|
||||||
remoteAddr := net.ParseIP(remoteHost)
|
remoteAddr := net.ParseIP(remoteHost)
|
||||||
|
@ -0,0 +1,60 @@
|
|||||||
|
package network
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMatchAuthorizedCIDRs(t *testing.T) {
|
||||||
|
|
||||||
|
type testCase struct {
|
||||||
|
RemoteHostPort string
|
||||||
|
AuthorizedCIDRs []string
|
||||||
|
ExpectedResult bool
|
||||||
|
ExpectedError error
|
||||||
|
}
|
||||||
|
|
||||||
|
testCases := []testCase{
|
||||||
|
{
|
||||||
|
RemoteHostPort: "192.168.1.15",
|
||||||
|
AuthorizedCIDRs: []string{
|
||||||
|
"192.168.1.0/24",
|
||||||
|
},
|
||||||
|
ExpectedResult: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
RemoteHostPort: "192.168.1.15:43349",
|
||||||
|
AuthorizedCIDRs: []string{
|
||||||
|
"192.168.1.0/24",
|
||||||
|
},
|
||||||
|
ExpectedResult: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
RemoteHostPort: "192.168.1.15:43349",
|
||||||
|
AuthorizedCIDRs: []string{
|
||||||
|
"192.168.1.5/32",
|
||||||
|
},
|
||||||
|
ExpectedResult: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
auth := Authenticator{}
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
for idx, tc := range testCases {
|
||||||
|
t.Run(fmt.Sprintf("Case #%d", idx), func(t *testing.T) {
|
||||||
|
result, err := auth.matchAnyAuthorizedCIDRs(ctx, tc.RemoteHostPort, tc.AuthorizedCIDRs)
|
||||||
|
|
||||||
|
if g, e := result, tc.ExpectedResult; e != g {
|
||||||
|
t.Errorf("result: expected '%v', got '%v'", e, g)
|
||||||
|
}
|
||||||
|
|
||||||
|
if e, g := tc.ExpectedError, err; !errors.Is(err, tc.ExpectedError) {
|
||||||
|
t.Errorf("err: expected '%v', got '%v'", e, g)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user