|
| 1 | +// Copyright (c) The go-grpc-middleware Authors. |
| 2 | +// Licensed under the Apache License 2.0. |
| 3 | + |
| 4 | +package selector |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/pkg/errors" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "google.golang.org/grpc" |
| 13 | + |
| 14 | + "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth" |
| 15 | +) |
| 16 | + |
| 17 | +var blockList = []string{"/auth.v1beta1.AuthService/Login"} |
| 18 | + |
| 19 | +const errMsgFake = "fake error" |
| 20 | + |
| 21 | +var ctxKey = struct{}{} |
| 22 | + |
| 23 | +// allow After the method is matched, the interceptor is run |
| 24 | +func allow(methods []string) MatchFunc { |
| 25 | + return func(ctx context.Context, fullMethod string) bool { |
| 26 | + for _, s := range methods { |
| 27 | + if s == fullMethod { |
| 28 | + return true |
| 29 | + } |
| 30 | + } |
| 31 | + return false |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// Block the interceptor will not run after the method matches |
| 36 | +func block(methods []string) MatchFunc { |
| 37 | + allow := allow(methods) |
| 38 | + return func(ctx context.Context, fullMethod string) bool { |
| 39 | + return !allow(ctx, fullMethod) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +type mockGRPCServerStream struct { |
| 44 | + grpc.ServerStream |
| 45 | + |
| 46 | + ctx context.Context |
| 47 | +} |
| 48 | + |
| 49 | +func (m *mockGRPCServerStream) Context() context.Context { |
| 50 | + return m.ctx |
| 51 | +} |
| 52 | + |
| 53 | +func TestUnaryServerInterceptor(t *testing.T) { |
| 54 | + ctx := context.Background() |
| 55 | + interceptor := UnaryServerInterceptor(auth.UnaryServerInterceptor( |
| 56 | + func(ctx context.Context) (context.Context, error) { |
| 57 | + newCtx := context.WithValue(ctx, ctxKey, true) |
| 58 | + return newCtx, nil |
| 59 | + }, |
| 60 | + ), block(blockList)) |
| 61 | + handler := func(ctx context.Context, req interface{}) (interface{}, error) { |
| 62 | + val := ctx.Value(ctxKey) |
| 63 | + if b, ok := val.(bool); ok && b { |
| 64 | + return "good", nil |
| 65 | + } |
| 66 | + return nil, errors.New(errMsgFake) |
| 67 | + } |
| 68 | + |
| 69 | + t.Run("nextStep", func(t *testing.T) { |
| 70 | + info := &grpc.UnaryServerInfo{ |
| 71 | + FullMethod: "FakeMethod", |
| 72 | + } |
| 73 | + resp, err := interceptor(ctx, nil, info, handler) |
| 74 | + assert.Nil(t, err) |
| 75 | + assert.Equal(t, resp, "good") |
| 76 | + }) |
| 77 | + |
| 78 | + t.Run("skipped", func(t *testing.T) { |
| 79 | + info := &grpc.UnaryServerInfo{ |
| 80 | + FullMethod: "/auth.v1beta1.AuthService/Login", |
| 81 | + } |
| 82 | + resp, err := interceptor(ctx, nil, info, handler) |
| 83 | + assert.Nil(t, resp) |
| 84 | + assert.EqualError(t, err, errMsgFake) |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +func TestStreamServerInterceptor(t *testing.T) { |
| 89 | + ctx := context.Background() |
| 90 | + interceptor := StreamServerInterceptor(auth.StreamServerInterceptor( |
| 91 | + func(ctx context.Context) (context.Context, error) { |
| 92 | + newCtx := context.WithValue(ctx, ctxKey, true) |
| 93 | + return newCtx, nil |
| 94 | + }, |
| 95 | + ), block(blockList)) |
| 96 | + |
| 97 | + handler := func(srv interface{}, stream grpc.ServerStream) error { |
| 98 | + ctx := stream.Context() |
| 99 | + val := ctx.Value(ctxKey) |
| 100 | + if b, ok := val.(bool); ok && b { |
| 101 | + return nil |
| 102 | + } |
| 103 | + return errors.New(errMsgFake) |
| 104 | + } |
| 105 | + |
| 106 | + t.Run("nextStep", func(t *testing.T) { |
| 107 | + info := &grpc.StreamServerInfo{ |
| 108 | + FullMethod: "FakeMethod", |
| 109 | + } |
| 110 | + err := interceptor(nil, &mockGRPCServerStream{ctx: ctx}, info, handler) |
| 111 | + assert.Nil(t, err) |
| 112 | + }) |
| 113 | + |
| 114 | + t.Run("skipped", func(t *testing.T) { |
| 115 | + info := &grpc.StreamServerInfo{ |
| 116 | + FullMethod: "/auth.v1beta1.AuthService/Login", |
| 117 | + } |
| 118 | + err := interceptor(nil, &mockGRPCServerStream{ctx: ctx}, info, handler) |
| 119 | + assert.EqualError(t, err, errMsgFake) |
| 120 | + }) |
| 121 | +} |
| 122 | + |
| 123 | +func TestAllow(t *testing.T) { |
| 124 | + type args struct { |
| 125 | + methods []string |
| 126 | + } |
| 127 | + tests := []struct { |
| 128 | + name string |
| 129 | + args args |
| 130 | + method string |
| 131 | + want bool |
| 132 | + }{ |
| 133 | + { |
| 134 | + name: "false", |
| 135 | + args: args{ |
| 136 | + methods: []string{"/auth.v1beta1.AuthService/Login"}, |
| 137 | + }, |
| 138 | + method: "/testing.testpb.v1.TestService/PingList", |
| 139 | + want: false, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "true", |
| 143 | + args: args{ |
| 144 | + methods: []string{"/auth.v1beta1.AuthService/Login"}, |
| 145 | + }, |
| 146 | + method: "/auth.v1beta1.AuthService/Login", |
| 147 | + want: true, |
| 148 | + }, |
| 149 | + } |
| 150 | + for _, tt := range tests { |
| 151 | + t.Run(tt.name, func(t *testing.T) { |
| 152 | + allow := allow(tt.args.methods) |
| 153 | + want := allow(context.Background(), tt.method) |
| 154 | + assert.Equalf(t, tt.want, want, "Allow(%v)(ctx, %v)", tt.args.methods, tt.method) |
| 155 | + }) |
| 156 | + } |
| 157 | +} |
0 commit comments