moved unit test
This commit is contained in:
@@ -11,7 +11,6 @@ import (
|
||||
"github.com/ucan-wg/go-ucan/did/didtest"
|
||||
"github.com/ucan-wg/go-ucan/pkg/command"
|
||||
"github.com/ucan-wg/go-ucan/pkg/policy"
|
||||
"github.com/ucan-wg/go-ucan/pkg/policy/limits"
|
||||
"github.com/ucan-wg/go-ucan/token/delegation"
|
||||
)
|
||||
|
||||
@@ -208,73 +207,3 @@ func TestEncryptedMeta(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestTokenTimestampBounds(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cmd, err := command.Parse("/foo/bar")
|
||||
require.NoError(t, err)
|
||||
pol, err := policy.FromDagJson("[]")
|
||||
require.NoError(t, err)
|
||||
|
||||
tomorrow := time.Now().Add(24 * time.Hour).Unix()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
nbf int64
|
||||
exp int64
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "valid timestamps",
|
||||
nbf: tomorrow,
|
||||
exp: tomorrow + 3600,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "max safe integer",
|
||||
nbf: tomorrow,
|
||||
exp: limits.MaxInt53,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "exceeds max safe integer",
|
||||
nbf: tomorrow,
|
||||
exp: limits.MaxInt53 + 1,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, err = delegation.New(didtest.PersonaAlice.DID(), didtest.PersonaBob.DID(),
|
||||
cmd, pol,
|
||||
delegation.WithNotBefore(time.Unix(tt.nbf, 0)),
|
||||
delegation.WithExpiration(time.Unix(tt.exp, 0)),
|
||||
)
|
||||
|
||||
if tt.wantErr {
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "exceeds safe integer bounds")
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("nbf overflow", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
futureExp := time.Now().Add(48 * time.Hour).Unix()
|
||||
_, err := delegation.New(didtest.PersonaAlice.DID(), didtest.PersonaBob.DID(),
|
||||
cmd, pol,
|
||||
delegation.WithNotBefore(time.Unix(limits.MaxInt53+1, 0)),
|
||||
delegation.WithExpiration(time.Unix(futureExp, 0)),
|
||||
)
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "exceeds safe integer bounds")
|
||||
})
|
||||
}
|
||||
|
||||
64
token/internal/parse/parse_test.go
Normal file
64
token/internal/parse/parse_test.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package parse
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/ucan-wg/go-ucan/pkg/policy/limits"
|
||||
)
|
||||
|
||||
func TestOptionalTimestamp(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input *int64
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "nil timestamp",
|
||||
input: nil,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "valid timestamp",
|
||||
input: int64Ptr(1625097600),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "max safe integer",
|
||||
input: int64Ptr(limits.MaxInt53),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "exceeds max safe integer",
|
||||
input: int64Ptr(limits.MaxInt53 + 1),
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "below min safe integer",
|
||||
input: int64Ptr(limits.MinInt53 - 1),
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := OptionalTimestamp(tt.input)
|
||||
if tt.wantErr {
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "exceeds safe integer bounds")
|
||||
require.Nil(t, result)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
if tt.input == nil {
|
||||
require.Nil(t, result)
|
||||
} else {
|
||||
require.NotNil(t, result)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func int64Ptr(i int64) *int64 {
|
||||
return &i
|
||||
}
|
||||
Reference in New Issue
Block a user