gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/credentials/alts/internal/authinfo/authinfo_test.go (about)

     1  /*
     2   *
     3   * Copyright 2018 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  package authinfo
    20  
    21  import (
    22  	"reflect"
    23  	"testing"
    24  
    25  	altspb "gitee.com/ks-custle/core-gm/grpc/credentials/alts/internal/proto/grpc_gcp"
    26  	"gitee.com/ks-custle/core-gm/grpc/internal/grpctest"
    27  )
    28  
    29  type s struct {
    30  	grpctest.Tester
    31  }
    32  
    33  func Test(t *testing.T) {
    34  	grpctest.RunSubTests(t, s{})
    35  }
    36  
    37  const (
    38  	testAppProtocol             = "my_app"
    39  	testRecordProtocol          = "very_secure_protocol"
    40  	testPeerAccount             = "peer_service_account"
    41  	testLocalAccount            = "local_service_account"
    42  	testPeerHostname            = "peer_hostname"
    43  	testLocalHostname           = "local_hostname"
    44  	testLocalPeerAttributeKey   = "peer"
    45  	testLocalPeerAttributeValue = "attributes"
    46  )
    47  
    48  func (s) TestALTSAuthInfo(t *testing.T) {
    49  	testPeerAttributes := make(map[string]string)
    50  	testPeerAttributes[testLocalPeerAttributeKey] = testLocalPeerAttributeValue
    51  	for _, tc := range []struct {
    52  		result             *altspb.HandshakerResult
    53  		outAppProtocol     string
    54  		outRecordProtocol  string
    55  		outSecurityLevel   altspb.SecurityLevel
    56  		outPeerAccount     string
    57  		outLocalAccount    string
    58  		outPeerRPCVersions *altspb.RpcProtocolVersions
    59  		outPeerAttributes  map[string]string
    60  	}{
    61  		{
    62  			&altspb.HandshakerResult{
    63  				ApplicationProtocol: testAppProtocol,
    64  				RecordProtocol:      testRecordProtocol,
    65  				PeerIdentity: &altspb.Identity{
    66  					IdentityOneof: &altspb.Identity_ServiceAccount{
    67  						ServiceAccount: testPeerAccount,
    68  					},
    69  					Attributes: testPeerAttributes,
    70  				},
    71  				LocalIdentity: &altspb.Identity{
    72  					IdentityOneof: &altspb.Identity_ServiceAccount{
    73  						ServiceAccount: testLocalAccount,
    74  					},
    75  				},
    76  			},
    77  			testAppProtocol,
    78  			testRecordProtocol,
    79  			altspb.SecurityLevel_INTEGRITY_AND_PRIVACY,
    80  			testPeerAccount,
    81  			testLocalAccount,
    82  			nil,
    83  			testPeerAttributes,
    84  		},
    85  		{
    86  			&altspb.HandshakerResult{
    87  				ApplicationProtocol: testAppProtocol,
    88  				RecordProtocol:      testRecordProtocol,
    89  				PeerIdentity: &altspb.Identity{
    90  					IdentityOneof: &altspb.Identity_Hostname{
    91  						Hostname: testPeerHostname,
    92  					},
    93  					Attributes: testPeerAttributes,
    94  				},
    95  				LocalIdentity: &altspb.Identity{
    96  					IdentityOneof: &altspb.Identity_Hostname{
    97  						Hostname: testLocalHostname,
    98  					},
    99  				},
   100  				PeerRpcVersions: &altspb.RpcProtocolVersions{
   101  					MaxRpcVersion: &altspb.RpcProtocolVersions_Version{
   102  						Major: 20,
   103  						Minor: 21,
   104  					},
   105  					MinRpcVersion: &altspb.RpcProtocolVersions_Version{
   106  						Major: 10,
   107  						Minor: 11,
   108  					},
   109  				},
   110  			},
   111  			testAppProtocol,
   112  			testRecordProtocol,
   113  			altspb.SecurityLevel_INTEGRITY_AND_PRIVACY,
   114  			"",
   115  			"",
   116  			&altspb.RpcProtocolVersions{
   117  				MaxRpcVersion: &altspb.RpcProtocolVersions_Version{
   118  					Major: 20,
   119  					Minor: 21,
   120  				},
   121  				MinRpcVersion: &altspb.RpcProtocolVersions_Version{
   122  					Major: 10,
   123  					Minor: 11,
   124  				},
   125  			},
   126  			testPeerAttributes,
   127  		},
   128  	} {
   129  		authInfo := newAuthInfo(tc.result)
   130  		if got, want := authInfo.AuthType(), "alts"; got != want {
   131  			t.Errorf("authInfo.AuthType()=%v, want %v", got, want)
   132  		}
   133  		if got, want := authInfo.ApplicationProtocol(), tc.outAppProtocol; got != want {
   134  			t.Errorf("authInfo.ApplicationProtocol()=%v, want %v", got, want)
   135  		}
   136  		if got, want := authInfo.RecordProtocol(), tc.outRecordProtocol; got != want {
   137  			t.Errorf("authInfo.RecordProtocol()=%v, want %v", got, want)
   138  		}
   139  		if got, want := authInfo.SecurityLevel(), tc.outSecurityLevel; got != want {
   140  			t.Errorf("authInfo.SecurityLevel()=%v, want %v", got, want)
   141  		}
   142  		if got, want := authInfo.PeerServiceAccount(), tc.outPeerAccount; got != want {
   143  			t.Errorf("authInfo.PeerServiceAccount()=%v, want %v", got, want)
   144  		}
   145  		if got, want := authInfo.LocalServiceAccount(), tc.outLocalAccount; got != want {
   146  			t.Errorf("authInfo.LocalServiceAccount()=%v, want %v", got, want)
   147  		}
   148  		if got, want := authInfo.PeerRPCVersions(), tc.outPeerRPCVersions; !reflect.DeepEqual(got, want) {
   149  			t.Errorf("authinfo.PeerRpcVersions()=%v, want %v", got, want)
   150  		}
   151  		if got, want := authInfo.PeerAttributes(), tc.outPeerAttributes; !reflect.DeepEqual(got, want) {
   152  			t.Errorf("authinfo.PeerAttributes()=%v, want %v", got, want)
   153  		}
   154  
   155  	}
   156  }