...

Source file src/google.golang.org/api/internal/s2a_test.go

Documentation: google.golang.org/api/internal

     1  // Copyright 2023 Google LLC.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package internal
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  const testS2AAddr = "testS2AAddress:port"
    15  
    16  var validConfigResp = func() (string, error) {
    17  	validConfig := mtlsConfig{
    18  		S2A: &s2aAddresses{
    19  			PlaintextAddress: testS2AAddr,
    20  			MTLSAddress:      "",
    21  		},
    22  	}
    23  	configStr, err := json.Marshal(validConfig)
    24  	if err != nil {
    25  		return "", err
    26  	}
    27  	return string(configStr), nil
    28  }
    29  
    30  var errorConfigResp = func() (string, error) {
    31  	return "", fmt.Errorf("error getting config")
    32  }
    33  
    34  var invalidConfigResp = func() (string, error) {
    35  	return "{}", nil
    36  }
    37  
    38  var invalidJSONResp = func() (string, error) {
    39  	return "test", nil
    40  }
    41  
    42  func TestGetS2AAddress(t *testing.T) {
    43  	testCases := []struct {
    44  		Desc     string
    45  		RespFunc func() (string, error)
    46  		Want     string
    47  	}{
    48  		{
    49  			Desc:     "test valid config",
    50  			RespFunc: validConfigResp,
    51  			Want:     testS2AAddr,
    52  		},
    53  		{
    54  			Desc:     "test error when getting config",
    55  			RespFunc: errorConfigResp,
    56  			Want:     "",
    57  		},
    58  		{
    59  			Desc:     "test invalid config",
    60  			RespFunc: invalidConfigResp,
    61  			Want:     "",
    62  		},
    63  		{
    64  			Desc:     "test invalid JSON response",
    65  			RespFunc: invalidJSONResp,
    66  			Want:     "",
    67  		},
    68  	}
    69  
    70  	oldHTTPGet := httpGetMetadataMTLSConfig
    71  	oldExpiry := configExpiry
    72  	configExpiry = time.Millisecond
    73  	defer func() {
    74  		httpGetMetadataMTLSConfig = oldHTTPGet
    75  		configExpiry = oldExpiry
    76  	}()
    77  	for _, tc := range testCases {
    78  		httpGetMetadataMTLSConfig = tc.RespFunc
    79  		if want, got := tc.Want, GetS2AAddress(); got != want {
    80  			t.Errorf("%s: want address [%s], got address [%s]", tc.Desc, want, got)
    81  		}
    82  		// Let the MTLS config expire at the end of each test case.
    83  		time.Sleep(2 * time.Millisecond)
    84  	}
    85  }
    86  
    87  func TestMTLSConfigExpiry(t *testing.T) {
    88  	oldHTTPGet := httpGetMetadataMTLSConfig
    89  	oldExpiry := configExpiry
    90  	configExpiry = 1 * time.Second
    91  	defer func() {
    92  		httpGetMetadataMTLSConfig = oldHTTPGet
    93  		configExpiry = oldExpiry
    94  	}()
    95  	httpGetMetadataMTLSConfig = validConfigResp
    96  	if got, want := GetS2AAddress(), testS2AAddr; got != want {
    97  		t.Errorf("expected address: [%s], got [%s]", want, got)
    98  	}
    99  	httpGetMetadataMTLSConfig = invalidConfigResp
   100  	if got, want := GetS2AAddress(), testS2AAddr; got != want {
   101  		t.Errorf("cached config should still be valid, expected address: [%s], got [%s]", want, got)
   102  	}
   103  	time.Sleep(1 * time.Second)
   104  	if got, want := GetS2AAddress(), ""; got != want {
   105  		t.Errorf("config should be refreshed, expected address: [%s], got [%s]", want, got)
   106  	}
   107  	// Let the MTLS config expire before running other tests.
   108  	time.Sleep(1 * time.Second)
   109  }
   110  

View as plain text