MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / TestVerifySource

Function TestVerifySource

pkg/cluster/auth_test.go:32–82  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

30)
31
32func TestVerifySource(t *testing.T) {
33 ctx := log.NewContext(test.Context(), test.GetLogger(t))
34
35 a := assertions.New(t)
36
37 key := []byte{0x2A, 0x9C, 0x2C, 0x3C, 0x2A, 0x9C, 0x2A, 0x9C, 0x2A, 0x9C, 0x2A, 0x9C, 0x2A, 0x9C, 0x2A, 0x9C}
38
39 c, err := New(ctx, &Config{
40 Keys: []string{
41 hex.EncodeToString(key),
42 },
43 })
44 a.So(err, should.BeNil)
45
46 t.Run("empty secret", func(t *testing.T) {
47 a := assertions.New(t)
48
49 ctx := c.WithVerifiedSource(ctx)
50 a.So(errors.IsUnauthenticated(clusterauth.Authorized(ctx)), should.BeTrue)
51 })
52
53 t.Run("invalid secret type", func(t *testing.T) {
54 a := assertions.New(t)
55
56 md := metadata.Pairs("authorization", "Basic invalid-secret")
57 ctx := metadata.NewIncomingContext(ctx, md)
58
59 ctx = c.WithVerifiedSource(ctx)
60 a.So(errors.IsInvalidArgument(clusterauth.Authorized(ctx)), should.BeTrue)
61 })
62
63 t.Run("valid secret", func(t *testing.T) {
64 a := assertions.New(t)
65
66 md := metadata.Pairs("authorization", fmt.Sprintf("ClusterKey %s", hex.EncodeToString(key)))
67 ctx := metadata.NewIncomingContext(ctx, md)
68
69 ctx = c.WithVerifiedSource(ctx)
70 a.So(clusterauth.Authorized(ctx), should.BeNil)
71 })
72
73 t.Run("wrong secret", func(t *testing.T) {
74 a := assertions.New(t)
75
76 md := metadata.Pairs("authorization", "ClusterKey 0102030405060708")
77 ctx := metadata.NewIncomingContext(ctx, md)
78
79 ctx = c.WithVerifiedSource(ctx)
80 a.So(errors.IsPermissionDenied(clusterauth.Authorized(ctx)), should.BeTrue)
81 })
82}

Callers

nothing calls this directly

Calls 10

NewContextFunction · 0.92
ContextFunction · 0.92
GetLoggerFunction · 0.92
IsUnauthenticatedFunction · 0.92
IsInvalidArgumentFunction · 0.92
IsPermissionDeniedFunction · 0.92
NewFunction · 0.70
NewMethod · 0.65
RunMethod · 0.65
WithVerifiedSourceMethod · 0.65

Tested by

no test coverage detected