move args int validation to their creation
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
|||||||
"github.com/ipld/go-ipld-prime/node/basicnode"
|
"github.com/ipld/go-ipld-prime/node/basicnode"
|
||||||
"github.com/ipld/go-ipld-prime/printer"
|
"github.com/ipld/go-ipld-prime/printer"
|
||||||
|
|
||||||
|
"github.com/ucan-wg/go-ucan/pkg/policy/limits"
|
||||||
"github.com/ucan-wg/go-ucan/pkg/policy/literal"
|
"github.com/ucan-wg/go-ucan/pkg/policy/literal"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -62,6 +63,10 @@ func (a *Args) Add(key string, val any) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := limits.ValidateIntegerBoundsIPLD(node); err != nil {
|
||||||
|
return fmt.Errorf("value for key %q: %w", key, err)
|
||||||
|
}
|
||||||
|
|
||||||
a.Values[key] = node
|
a.Values[key] = node
|
||||||
a.Keys = append(a.Keys, key)
|
a.Keys = append(a.Keys, key)
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/ucan-wg/go-ucan/pkg/args"
|
"github.com/ucan-wg/go-ucan/pkg/args"
|
||||||
|
"github.com/ucan-wg/go-ucan/pkg/policy/limits"
|
||||||
"github.com/ucan-wg/go-ucan/pkg/policy/literal"
|
"github.com/ucan-wg/go-ucan/pkg/policy/literal"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -185,6 +186,71 @@ func TestInclude(t *testing.T) {
|
|||||||
}, maps.Collect(a1.Iter()))
|
}, maps.Collect(a1.Iter()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestArgsIntegerBounds(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
key string
|
||||||
|
val int64
|
||||||
|
wantErr string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "valid int",
|
||||||
|
key: "valid",
|
||||||
|
val: 42,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "max safe integer",
|
||||||
|
key: "max",
|
||||||
|
val: limits.MaxInt53,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "min safe integer",
|
||||||
|
key: "min",
|
||||||
|
val: limits.MinInt53,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "exceeds max safe integer",
|
||||||
|
key: "tooBig",
|
||||||
|
val: limits.MaxInt53 + 1,
|
||||||
|
wantErr: "exceeds safe integer bounds",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "below min safe integer",
|
||||||
|
key: "tooSmall",
|
||||||
|
val: limits.MinInt53 - 1,
|
||||||
|
wantErr: "exceeds safe integer bounds",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "duplicate key",
|
||||||
|
key: "duplicate",
|
||||||
|
val: 42,
|
||||||
|
wantErr: "duplicate key",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
a := args.New()
|
||||||
|
require.NoError(t, a.Add("duplicate", 1)) // tests duplicate key
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
err := a.Add(tt.key, tt.val)
|
||||||
|
if tt.wantErr != "" {
|
||||||
|
require.Error(t, err)
|
||||||
|
require.Contains(t, err.Error(), tt.wantErr)
|
||||||
|
} else {
|
||||||
|
require.NoError(t, err)
|
||||||
|
val, err := a.GetNode(tt.key)
|
||||||
|
require.NoError(t, err)
|
||||||
|
i, err := val.AsInt()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, tt.val, i)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
argsSchema = "type Args { String : Any }"
|
argsSchema = "type Args { String : Any }"
|
||||||
argsName = "Args"
|
argsName = "Args"
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
"github.com/ipld/go-ipld-prime/datamodel"
|
"github.com/ipld/go-ipld-prime/datamodel"
|
||||||
"github.com/ipld/go-ipld-prime/fluent/qp"
|
"github.com/ipld/go-ipld-prime/fluent/qp"
|
||||||
"github.com/ipld/go-ipld-prime/node/basicnode"
|
"github.com/ipld/go-ipld-prime/node/basicnode"
|
||||||
"github.com/ucan-wg/go-ucan/pkg/policy/limits"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Selector describes a UCAN policy selector, as specified here:
|
// Selector describes a UCAN policy selector, as specified here:
|
||||||
@@ -23,10 +22,6 @@ type Selector []segment
|
|||||||
// - a resolutionerr error if not being able to resolve to a node
|
// - a resolutionerr error if not being able to resolve to a node
|
||||||
// - nil and no errors, if the selector couldn't match on an optional segment (with ?).
|
// - nil and no errors, if the selector couldn't match on an optional segment (with ?).
|
||||||
func (s Selector) Select(subject ipld.Node) (ipld.Node, error) {
|
func (s Selector) Select(subject ipld.Node) (ipld.Node, error) {
|
||||||
if err := limits.ValidateIntegerBoundsIPLD(subject); err != nil {
|
|
||||||
return nil, fmt.Errorf("node contains integer values outside safe bounds: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return resolve(s, subject, nil)
|
return resolve(s, subject, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user