Skip to content

Commit

Permalink
moved unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiobozzo committed Dec 2, 2024
1 parent 5b816cc commit 105323b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 71 deletions.
71 changes: 0 additions & 71 deletions token/delegation/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 changes: 64 additions & 0 deletions token/internal/parse/parse_test.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 105323b

Please sign in to comment.