mirror of
https://github.com/lightninglabs/loop
synced 2024-11-16 00:12:52 +00:00
1eb8ed3da5
We introduce a new package: `lsat`, which aims to provide utilities that will serve useful in the context of LSAT creation and verification for LSAT-enabled services.
84 lines
1.5 KiB
Go
84 lines
1.5 KiB
Go
package lsat
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
// TestServicesCaveatSerialization ensures that we can properly encode/decode
|
|
// valid services from a caveat and cannot do so for invalid ones.
|
|
func TestServicesCaveatSerialization(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
value string
|
|
err error
|
|
}{
|
|
{
|
|
name: "single service",
|
|
value: "a:0",
|
|
err: nil,
|
|
},
|
|
{
|
|
name: "multiple services",
|
|
value: "a:0,b:1,c:0",
|
|
err: nil,
|
|
},
|
|
{
|
|
name: "no services",
|
|
value: "",
|
|
err: ErrNoServices,
|
|
},
|
|
{
|
|
name: "service missing name",
|
|
value: ":0",
|
|
err: ErrInvalidService,
|
|
},
|
|
{
|
|
name: "service missing tier",
|
|
value: "a",
|
|
err: ErrInvalidService,
|
|
},
|
|
{
|
|
name: "service empty tier",
|
|
value: "a:",
|
|
err: ErrInvalidService,
|
|
},
|
|
{
|
|
name: "service non-numeric tier",
|
|
value: "a:b",
|
|
err: ErrInvalidService,
|
|
},
|
|
{
|
|
name: "empty services",
|
|
value: ",,",
|
|
err: ErrInvalidService,
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
test := test
|
|
success := t.Run(test.name, func(t *testing.T) {
|
|
services, err := decodeServicesCaveatValue(test.value)
|
|
if !errors.Is(err, test.err) {
|
|
t.Fatalf("expected err \"%v\", got \"%v\"",
|
|
test.err, err)
|
|
}
|
|
|
|
if test.err != nil {
|
|
return
|
|
}
|
|
|
|
value, _ := encodeServicesCaveatValue(services...)
|
|
if value != test.value {
|
|
t.Fatalf("expected encoded services \"%v\", "+
|
|
"got \"%v\"", test.value, value)
|
|
}
|
|
})
|
|
if !success {
|
|
return
|
|
}
|
|
}
|
|
}
|