vitess.io/vitess@v0.16.2/go/vt/callerid/testsuite/testsuite.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package testsuite
    18  
    19  import (
    20  	"testing"
    21  
    22  	"context"
    23  
    24  	"google.golang.org/protobuf/proto"
    25  
    26  	"vitess.io/vitess/go/vt/callerid"
    27  
    28  	querypb "vitess.io/vitess/go/vt/proto/query"
    29  	vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
    30  )
    31  
    32  const (
    33  	// FakePrincipal is the principal of testing effective CallerID
    34  	FakePrincipal = "TestPrincipal"
    35  	// FakeComponent is the component of testing effective CallerID
    36  	FakeComponent = "TestComponent"
    37  	// FakeSubcomponent is the subcomponent of testing effective CallerID
    38  	FakeSubcomponent = "TestSubcomponent"
    39  	// FakeUsername is the username of testing immediate CallerID
    40  	FakeUsername = "TestUsername"
    41  )
    42  
    43  // RunTests performs the necessary testsuite for CallerID operations
    44  func RunTests(t *testing.T, im *querypb.VTGateCallerID, ef *vtrpcpb.CallerID, newContext func(context.Context, *vtrpcpb.CallerID, *querypb.VTGateCallerID) context.Context) {
    45  	ctx := context.TODO()
    46  	ctxim := callerid.ImmediateCallerIDFromContext(ctx)
    47  	// For Contexts without immediate CallerID, ImmediateCallerIDFromContext should fail
    48  	if ctxim != nil {
    49  		t.Errorf("Expect nil from ImmediateCallerIDFromContext, but got %v", ctxim)
    50  	}
    51  	// For Contexts without effective CallerID, EffectiveCallerIDFromContext should fail
    52  	ctxef := callerid.EffectiveCallerIDFromContext(ctx)
    53  	if ctxef != nil {
    54  		t.Errorf("Expect nil from EffectiveCallerIDFromContext, but got %v", ctxef)
    55  	}
    56  
    57  	ctx = newContext(ctx, nil, nil)
    58  	ctxim = callerid.ImmediateCallerIDFromContext(ctx)
    59  	// For Contexts with nil immediate CallerID, ImmediateCallerIDFromContext should fail
    60  	if ctxim != nil {
    61  		t.Errorf("Expect nil from ImmediateCallerIDFromContext, but got %v", ctxim)
    62  	}
    63  	// For Contexts with nil effective CallerID, EffectiveCallerIDFromContext should fail
    64  	ctxef = callerid.EffectiveCallerIDFromContext(ctx)
    65  	if ctxef != nil {
    66  		t.Errorf("Expect nil from EffectiveCallerIDFromContext, but got %v", ctxef)
    67  	}
    68  
    69  	// Test GetXxx on nil receivers, should get all empty strings
    70  	if u := callerid.GetUsername(ctxim); u != "" {
    71  		t.Errorf("Expect empty string from GetUsername(nil), but got %v", u)
    72  	}
    73  	if p := callerid.GetPrincipal(ctxef); p != "" {
    74  		t.Errorf("Expect empty string from GetPrincipal(nil), but got %v", p)
    75  	}
    76  	if c := callerid.GetComponent(ctxef); c != "" {
    77  		t.Errorf("Expect empty string from GetComponent(nil), but got %v", c)
    78  	}
    79  	if s := callerid.GetSubcomponent(ctxef); s != "" {
    80  		t.Errorf("Expect empty string from GetSubcomponent(nil), but got %v", s)
    81  	}
    82  
    83  	ctx = newContext(ctx, ef, im)
    84  	ctxim = callerid.ImmediateCallerIDFromContext(ctx)
    85  	// retrieved immediate CallerID should be equal to the one we put into Context
    86  	if !proto.Equal(ctxim, im) {
    87  		t.Errorf("Expect %v from ImmediateCallerIDFromContext, but got %v", im, ctxim)
    88  	}
    89  	if u := callerid.GetUsername(im); u != FakeUsername {
    90  		t.Errorf("Expect %v from GetUsername(im), but got %v", FakeUsername, u)
    91  	}
    92  
    93  	ctxef = callerid.EffectiveCallerIDFromContext(ctx)
    94  	// retrieved effective CallerID should be equal to the one we put into Context
    95  	if !proto.Equal(ctxef, ef) {
    96  		t.Errorf("Expect %v from EffectiveCallerIDFromContext, but got %v", ef, ctxef)
    97  	}
    98  	if p := callerid.GetPrincipal(ef); p != FakePrincipal {
    99  		t.Errorf("Expect %v from GetPrincipal(ef), but got %v", FakePrincipal, p)
   100  	}
   101  	if c := callerid.GetComponent(ef); c != FakeComponent {
   102  		t.Errorf("Expect %v from GetComponent(ef), but got %v", FakeComponent, c)
   103  	}
   104  	if s := callerid.GetSubcomponent(ef); s != FakeSubcomponent {
   105  		t.Errorf("Expect %v from GetSubcomponent(ef), but got %v", FakeSubcomponent, s)
   106  	}
   107  }