...

Source file src/cloud.google.com/go/auth/internal/transport/s2a_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  	"testing"
    19  	"time"
    20  )
    21  
    22  const (
    23  	testS2AAddr = "testS2AAddress:port"
    24  )
    25  
    26  func TestGetS2AAddress(t *testing.T) {
    27  	testCases := []struct {
    28  		name   string
    29  		respFn func() (string, error)
    30  		want   string
    31  	}{
    32  		{
    33  			name:   "test valid config",
    34  			respFn: validConfigResp,
    35  			want:   testS2AAddr,
    36  		},
    37  		{
    38  			name:   "test error when getting config",
    39  			respFn: errorConfigResp,
    40  			want:   "",
    41  		},
    42  		{
    43  			name:   "test invalid config",
    44  			respFn: invalidConfigResp,
    45  			want:   "",
    46  		},
    47  		{
    48  			name:   "test invalid JSON response",
    49  			respFn: invalidJSONResp,
    50  			want:   "",
    51  		},
    52  	}
    53  
    54  	oldHTTPGet := httpGetMetadataMTLSConfig
    55  	oldExpiry := configExpiry
    56  	configExpiry = time.Millisecond
    57  	defer func() {
    58  		httpGetMetadataMTLSConfig = oldHTTPGet
    59  		configExpiry = oldExpiry
    60  	}()
    61  	for _, tc := range testCases {
    62  		t.Run(tc.name, func(t *testing.T) {
    63  			httpGetMetadataMTLSConfig = tc.respFn
    64  			if want, got := tc.want, GetS2AAddress(); got != want {
    65  				t.Errorf("want address [%s], got address [%s]", want, got)
    66  			}
    67  			// Let the MTLS config expire at the end of each test case.
    68  			time.Sleep(2 * time.Millisecond)
    69  		})
    70  	}
    71  }
    72  
    73  func TestMTLSConfigExpiry(t *testing.T) {
    74  	oldHTTPGet := httpGetMetadataMTLSConfig
    75  	oldExpiry := configExpiry
    76  	configExpiry = 1 * time.Second
    77  	defer func() {
    78  		httpGetMetadataMTLSConfig = oldHTTPGet
    79  		configExpiry = oldExpiry
    80  	}()
    81  	httpGetMetadataMTLSConfig = validConfigResp
    82  	if got, want := GetS2AAddress(), testS2AAddr; got != want {
    83  		t.Errorf("expected address: [%s], got [%s]", want, got)
    84  	}
    85  	httpGetMetadataMTLSConfig = invalidConfigResp
    86  	if got, want := GetS2AAddress(), testS2AAddr; got != want {
    87  		t.Errorf("cached config should still be valid, expected address: [%s], got [%s]", want, got)
    88  	}
    89  	time.Sleep(1 * time.Second)
    90  	if got, want := GetS2AAddress(), ""; got != want {
    91  		t.Errorf("config should be refreshed, expected address: [%s], got [%s]", want, got)
    92  	}
    93  	// Let the MTLS config expire before running other tests.
    94  	time.Sleep(1 * time.Second)
    95  }
    96  
    97  func TestIsGoogleS2AEnabled(t *testing.T) {
    98  	testCases := []struct {
    99  		name      string
   100  		useS2AEnv string
   101  		want      bool
   102  	}{
   103  		{
   104  			name:      "true",
   105  			useS2AEnv: "true",
   106  			want:      true,
   107  		},
   108  		{
   109  			name:      "false",
   110  			useS2AEnv: "false",
   111  			want:      false,
   112  		},
   113  		{
   114  			name:      "empty",
   115  			useS2AEnv: "",
   116  			want:      false,
   117  		},
   118  	}
   119  
   120  	for _, tc := range testCases {
   121  		t.Run(tc.name, func(t *testing.T) {
   122  			if tc.useS2AEnv != "" {
   123  				t.Setenv(googleAPIUseS2AEnv, tc.useS2AEnv)
   124  			}
   125  
   126  			if got := isGoogleS2AEnabled(); got != tc.want {
   127  				t.Errorf("got %t, want %t", got, tc.want)
   128  			}
   129  		})
   130  	}
   131  }
   132  

View as plain text