...

Source file src/cloud.google.com/go/auth/internal/transport/transport_test.go

Documentation: cloud.google.com/go/auth/internal/transport

     1  // Copyright 2023 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package transport
    16  
    17  import (
    18  	"net/http"
    19  	"reflect"
    20  	"testing"
    21  
    22  	"cloud.google.com/go/auth"
    23  	"cloud.google.com/go/auth/credentials"
    24  )
    25  
    26  // TestCloneDetectOptions_FieldTest is meant to fail every time a new field is
    27  // added to the detect.Options type. This tests exists to make sure the
    28  // CloneDetectOptions function is updated to copy over any new fields in the
    29  // future. To make the test pass simply bump the int, but please also clone the
    30  // relevant fields.
    31  func TestCloneDetectOptions_FieldTest(t *testing.T) {
    32  	const WantNumberOfFields = 12
    33  	o := credentials.DetectOptions{}
    34  	got := reflect.TypeOf(o).NumField()
    35  	if got != WantNumberOfFields {
    36  		t.Errorf("if this fails please read comment above the test: got %v, want %v", got, WantNumberOfFields)
    37  	}
    38  }
    39  
    40  func TestCloneDetectOptions(t *testing.T) {
    41  	oldDo := &credentials.DetectOptions{
    42  		Audience:          "aud",
    43  		Subject:           "sub",
    44  		EarlyTokenRefresh: 42,
    45  		TokenURL:          "TokenURL",
    46  		STSAudience:       "STSAudience",
    47  		CredentialsFile:   "CredentialsFile",
    48  		UseSelfSignedJWT:  true,
    49  		CredentialsJSON:   []byte{1, 2, 3, 4, 5},
    50  		Scopes:            []string{"a", "b"},
    51  		Client:            &http.Client{},
    52  		AuthHandlerOptions: &auth.AuthorizationHandlerOptions{
    53  			Handler: func(authCodeURL string) (code string, state string, err error) {
    54  				return "", "", nil
    55  			},
    56  			State: "state",
    57  			PKCEOpts: &auth.PKCEOptions{
    58  				Challenge:       "Challenge",
    59  				ChallengeMethod: "ChallengeMethod",
    60  				Verifier:        "Verifier",
    61  			},
    62  		},
    63  	}
    64  	newDo := CloneDetectOptions(oldDo)
    65  
    66  	// Simple fields
    67  	if got, want := newDo.Audience, oldDo.Audience; got != want {
    68  		t.Fatalf("got %v, want %v", got, want)
    69  	}
    70  	if got, want := newDo.Subject, oldDo.Subject; got != want {
    71  		t.Fatalf("got %v, want %v", got, want)
    72  	}
    73  	if got, want := newDo.EarlyTokenRefresh, oldDo.EarlyTokenRefresh; got != want {
    74  		t.Fatalf("got %v, want %v", got, want)
    75  	}
    76  	if got, want := newDo.TokenURL, oldDo.TokenURL; got != want {
    77  		t.Fatalf("got %v, want %v", got, want)
    78  	}
    79  	if got, want := newDo.STSAudience, oldDo.STSAudience; got != want {
    80  		t.Fatalf("got %v, want %v", got, want)
    81  	}
    82  	if got, want := newDo.CredentialsFile, oldDo.CredentialsFile; got != want {
    83  		t.Fatalf("got %v, want %v", got, want)
    84  	}
    85  	if got, want := newDo.UseSelfSignedJWT, oldDo.UseSelfSignedJWT; got != want {
    86  		t.Fatalf("got %v, want %v", got, want)
    87  	}
    88  
    89  	// Slices
    90  	if got, want := len(newDo.CredentialsJSON), len(oldDo.CredentialsJSON); got != want {
    91  		t.Fatalf("got %v, want %v", got, want)
    92  	}
    93  	if got, want := newDo.CredentialsJSON, oldDo.CredentialsJSON; reflect.ValueOf(got).Pointer() == reflect.ValueOf(want).Pointer() {
    94  		t.Fatalf("CredentialsJSON should not reference the same slice")
    95  	}
    96  	if got, want := len(newDo.Scopes), len(oldDo.Scopes); got != want {
    97  		t.Fatalf("got %v, want %v", got, want)
    98  	}
    99  	if got, want := newDo.Scopes, oldDo.Scopes; reflect.ValueOf(got).Pointer() == reflect.ValueOf(want).Pointer() {
   100  		t.Fatalf("Scopes should not reference the same slice")
   101  	}
   102  
   103  	// Pointer types that should be the same memory
   104  	if got, want := newDo.Client, oldDo.Client; reflect.ValueOf(got).Pointer() != reflect.ValueOf(want).Pointer() {
   105  		t.Fatalf("Scopes should not reference the same slice")
   106  	}
   107  	if got, want := newDo.AuthHandlerOptions, oldDo.AuthHandlerOptions; reflect.ValueOf(got).Pointer() != reflect.ValueOf(want).Pointer() {
   108  		t.Fatalf("Scopes should not reference the same slice")
   109  	}
   110  }
   111  

View as plain text