mirror of
https://github.com/aler9/gortsplib
synced 2025-10-09 00:50:24 +08:00
auth: add negative tests
This commit is contained in:
53
pkg/auth/sender_test.go
Normal file
53
pkg/auth/sender_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/aler9/gortsplib/pkg/base"
|
||||
)
|
||||
|
||||
func TestSenderError(t *testing.T) {
|
||||
for _, ca := range []struct {
|
||||
name string
|
||||
hv base.HeaderValue
|
||||
err string
|
||||
}{
|
||||
{
|
||||
"invalid method",
|
||||
base.HeaderValue{`Invalid`},
|
||||
"no authentication methods available",
|
||||
},
|
||||
{
|
||||
"digest invalid",
|
||||
base.HeaderValue{`Digest`},
|
||||
"unable to split between method and keys (Digest)",
|
||||
},
|
||||
{
|
||||
"digest, missing realm",
|
||||
base.HeaderValue{`Digest nonce=123`},
|
||||
"realm is missing",
|
||||
},
|
||||
{
|
||||
"digest, missing nonce",
|
||||
base.HeaderValue{`Digest realm=123`},
|
||||
"nonce is missing",
|
||||
},
|
||||
{
|
||||
"basic invalid",
|
||||
base.HeaderValue{`Basic`},
|
||||
"unable to split between method and keys (Basic)",
|
||||
},
|
||||
{
|
||||
"basic, missing realm",
|
||||
base.HeaderValue{`Basic nonce=123`},
|
||||
"realm is missing",
|
||||
},
|
||||
} {
|
||||
t.Run(ca.name, func(t *testing.T) {
|
||||
_, err := NewSender(ca.hv, "myuser", "mypass")
|
||||
require.Equal(t, ca.err, err.Error())
|
||||
})
|
||||
}
|
||||
}
|
65
pkg/auth/validator_test.go
Normal file
65
pkg/auth/validator_test.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/aler9/gortsplib/pkg/base"
|
||||
)
|
||||
|
||||
func TestValidatorError(t *testing.T) {
|
||||
for _, ca := range []struct {
|
||||
name string
|
||||
hv base.HeaderValue
|
||||
err string
|
||||
}{
|
||||
{
|
||||
"invalid auth",
|
||||
base.HeaderValue{`Invalid`},
|
||||
"invalid authorization header",
|
||||
},
|
||||
{
|
||||
"digest missing realm",
|
||||
base.HeaderValue{`Digest `},
|
||||
"realm is missing",
|
||||
},
|
||||
{
|
||||
"digest missing nonce",
|
||||
base.HeaderValue{`Digest realm=123`},
|
||||
"nonce is missing",
|
||||
},
|
||||
{
|
||||
"digest missing username",
|
||||
base.HeaderValue{`Digest realm=123,nonce=123`},
|
||||
"username is missing",
|
||||
},
|
||||
{
|
||||
"digest missing uri",
|
||||
base.HeaderValue{`Digest realm=123,nonce=123,username=123`},
|
||||
"uri is missing",
|
||||
},
|
||||
{
|
||||
"digest missing response",
|
||||
base.HeaderValue{`Digest realm=123,nonce=123,username=123,uri=123`},
|
||||
"response is missing",
|
||||
},
|
||||
{
|
||||
"digest wrong nonce",
|
||||
base.HeaderValue{`Digest realm=123,nonce=123,username=123,uri=123,response=123`},
|
||||
"wrong nonce",
|
||||
},
|
||||
{
|
||||
"digest wrong realm",
|
||||
base.HeaderValue{`Digest realm=123,nonce=abcde,username=123,uri=123,response=123`},
|
||||
"wrong realm",
|
||||
},
|
||||
} {
|
||||
t.Run(ca.name, func(t *testing.T) {
|
||||
va := NewValidator("myuser", "mypass", nil)
|
||||
va.nonce = "abcde"
|
||||
err := va.ValidateHeader(ca.hv, base.Describe, nil, nil)
|
||||
require.Equal(t, ca.err, err.Error())
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user