...

Source file src/github.com/google/s2a-go/s2a_utils_test.go

Documentation: github.com/google/s2a-go

     1  /*
     2   *
     3   * Copyright 2021 Google LLC
     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   *     https://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 s2a
    20  
    21  import (
    22  	"context"
    23  	"testing"
    24  
    25  	commonpb "github.com/google/s2a-go/internal/proto/common_go_proto"
    26  	"google.golang.org/grpc/credentials"
    27  	"google.golang.org/grpc/peer"
    28  )
    29  
    30  func TestAuthInfoFromContext(t *testing.T) {
    31  	ctx := context.Background()
    32  	s2aAuthInfo := &fakeS2AAuthInfo{}
    33  	p := &peer.Peer{
    34  		AuthInfo: s2aAuthInfo,
    35  	}
    36  	for _, tc := range []struct {
    37  		desc    string
    38  		ctx     context.Context
    39  		success bool
    40  		out     AuthInfo
    41  	}{
    42  		{
    43  			"working case",
    44  			peer.NewContext(ctx, p),
    45  			true,
    46  			s2aAuthInfo,
    47  		},
    48  	} {
    49  		authInfo, err := AuthInfoFromContext(tc.ctx)
    50  		if got, want := (err == nil), tc.success; got != want {
    51  			t.Errorf("%v: AuthInfoFromContext(_)=(err=nil)=%v, want %v", tc.desc, got, want)
    52  		}
    53  		if got, want := authInfo, tc.out; got != want {
    54  			t.Errorf("%v:, AuthInfoFromContext(_)=(%v, _), want (%v, _)", tc.desc, got, want)
    55  		}
    56  	}
    57  }
    58  
    59  func TestAuthInfoFromPeer(t *testing.T) {
    60  	s2aAuthInfo := &fakeS2AAuthInfo{}
    61  	p := &peer.Peer{
    62  		AuthInfo: s2aAuthInfo,
    63  	}
    64  	for _, tc := range []struct {
    65  		desc    string
    66  		p       *peer.Peer
    67  		success bool
    68  		out     AuthInfo
    69  	}{
    70  		{
    71  			"working case",
    72  			p,
    73  			true,
    74  			s2aAuthInfo,
    75  		},
    76  	} {
    77  		authInfo, err := AuthInfoFromPeer(tc.p)
    78  		if got, want := (err == nil), tc.success; got != want {
    79  			t.Errorf("%v: AuthInfoFromPeer(_)=(err=nil)=%v, want %v", tc.desc, got, want)
    80  		}
    81  		if got, want := authInfo, tc.out; got != want {
    82  			t.Errorf("%v:, AuthInfoFromPeer(_)=(%v, _), want (%v, _)", tc.desc, got, want)
    83  		}
    84  	}
    85  }
    86  
    87  type fakeS2AAuthInfo struct{}
    88  
    89  func (*fakeS2AAuthInfo) AuthType() string                { return "" }
    90  func (*fakeS2AAuthInfo) ApplicationProtocol() string     { return "" }
    91  func (*fakeS2AAuthInfo) TLSVersion() commonpb.TLSVersion { return commonpb.TLSVersion_TLS1_3 }
    92  func (*fakeS2AAuthInfo) Ciphersuite() commonpb.Ciphersuite {
    93  	return commonpb.Ciphersuite_AES_128_GCM_SHA256
    94  }
    95  func (*fakeS2AAuthInfo) PeerIdentity() *commonpb.Identity  { return nil }
    96  func (*fakeS2AAuthInfo) LocalIdentity() *commonpb.Identity { return nil }
    97  func (*fakeS2AAuthInfo) PeerCertFingerprint() []byte       { return nil }
    98  func (*fakeS2AAuthInfo) LocalCertFingerprint() []byte      { return nil }
    99  func (*fakeS2AAuthInfo) IsHandshakeResumed() bool          { return false }
   100  func (*fakeS2AAuthInfo) SecurityLevel() credentials.SecurityLevel {
   101  	return credentials.PrivacyAndIntegrity
   102  }
   103  

View as plain text