Repository navigation
Expand file tree
/
Copy pathprincipal_test.go
More file actions
84 lines (75 loc) · 2.86 KB
/
Copy pathprincipal_test.go
File metadata and controls
84 lines (75 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package cortex_test
import (
"context"
"errors"
"testing"
"github.com/xraph/cortex"
)
type testPrincipal struct {
User string `json:"user"`
Org string `json:"org"`
}
// The codec hands back a value of the type it was built for, so an
// authorizer's type assertion on a resumed run's principal holds exactly
// as it does on a fresh one.
func TestJSONPrincipalCodecRoundTrip(t *testing.T) {
ctx := context.Background()
codec := cortex.JSONPrincipalCodec[testPrincipal]()
want := testPrincipal{User: "alice", Org: "o1"}
data, err := codec.EncodePrincipal(ctx, want)
if err != nil {
t.Fatalf("EncodePrincipal: %v", err)
}
decoded, err := codec.DecodePrincipal(ctx, data)
if err != nil {
t.Fatalf("DecodePrincipal: %v", err)
}
got, ok := decoded.(testPrincipal)
if !ok {
t.Fatalf("DecodePrincipal returned %T, want testPrincipal", decoded)
}
if got != want {
t.Errorf("round trip = %+v, want %+v", got, want)
}
}
// A principal of another type is refused at encode time. Accepting it
// would persist bytes that decode into a T the host never put on the
// context, and the authorizer would meet a stranger after the pause.
func TestJSONPrincipalCodecRefusesOtherTypes(t *testing.T) {
codec := cortex.JSONPrincipalCodec[testPrincipal]()
if _, err := codec.EncodePrincipal(context.Background(), "alice"); err == nil {
t.Fatal("EncodePrincipal accepted a string for a testPrincipal codec")
}
}
// failingPrincipal refuses to decode for a reason that is not about its
// bytes, the way a type that looks itself up somewhere can.
type failingPrincipal struct{}
func (*failingPrincipal) UnmarshalJSON([]byte) error { return errors.New("directory unreachable") }
// Bytes that can never decode into a T are revoked, because no retry
// changes them. A decode that failed for any other reason may work next
// time, so it stays transient.
func TestJSONPrincipalCodecClassesUnreadableBytesAsRevoked(t *testing.T) {
ctx := context.Background()
stored, err := cortex.JSONPrincipalCodec[testPrincipal]().EncodePrincipal(ctx, testPrincipal{User: "alice", Org: "o1"})
if err != nil {
t.Fatalf("EncodePrincipal: %v", err)
}
for name, decode := range map[string]func() error{
"corrupt bytes": func() error {
_, decodeErr := cortex.JSONPrincipalCodec[testPrincipal]().DecodePrincipal(ctx, []byte("{"))
return decodeErr
},
"type changed": func() error {
_, decodeErr := cortex.JSONPrincipalCodec[int]().DecodePrincipal(ctx, stored)
return decodeErr
},
} {
if decodeErr := decode(); !errors.Is(decodeErr, cortex.ErrPrincipalRevoked) {
t.Errorf("%s: DecodePrincipal = %v, want ErrPrincipalRevoked", name, decodeErr)
}
}
_, err = cortex.JSONPrincipalCodec[failingPrincipal]().DecodePrincipal(ctx, []byte("{}"))
if err == nil || errors.Is(err, cortex.ErrPrincipalRevoked) {
t.Errorf("DecodePrincipal with a failing UnmarshalJSON = %v, want a transient error", err)
}
}