...

Source file src/google.golang.org/api/option/option_test.go

Documentation: google.golang.org/api/option

     1  // Copyright 2017 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 option
     6  
     7  import (
     8  	"testing"
     9  
    10  	"crypto/tls"
    11  	"math/big"
    12  
    13  	"github.com/google/go-cmp/cmp"
    14  	"github.com/google/go-cmp/cmp/cmpopts"
    15  	"golang.org/x/oauth2/google"
    16  	"google.golang.org/api/internal"
    17  	"google.golang.org/grpc"
    18  )
    19  
    20  // Below is a dummy certificate/key pair taken from
    21  // https://golang.org/src/crypto/tls/tls_test.go
    22  const certPEM = `-----BEGIN CERTIFICATE-----
    23  MIIB0zCCAX2gAwIBAgIJAI/M7BYjwB+uMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV
    24  BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
    25  aWRnaXRzIFB0eSBMdGQwHhcNMTIwOTEyMjE1MjAyWhcNMTUwOTEyMjE1MjAyWjBF
    26  MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50
    27  ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANLJ
    28  hPHhITqQbPklG3ibCVxwGMRfp/v4XqhfdQHdcVfHap6NQ5Wok/4xIA+ui35/MmNa
    29  rtNuC+BdZ1tMuVCPFZcCAwEAAaNQME4wHQYDVR0OBBYEFJvKs8RfJaXTH08W+SGv
    30  zQyKn0H8MB8GA1UdIwQYMBaAFJvKs8RfJaXTH08W+SGvzQyKn0H8MAwGA1UdEwQF
    31  MAMBAf8wDQYJKoZIhvcNAQEFBQADQQBJlffJHybjDGxRMqaRmDhX0+6v02TUKZsW
    32  r5QuVbpQhH6u+0UgcW0jp9QwpxoPTLTWGXEWBBBurxFwiCBhkQ+V
    33  -----END CERTIFICATE-----
    34  -----BEGIN PRIVATE KEY-----
    35  MIIBOwIBAAJBANLJhPHhITqQbPklG3ibCVxwGMRfp/v4XqhfdQHdcVfHap6NQ5Wo
    36  k/4xIA+ui35/MmNartNuC+BdZ1tMuVCPFZcCAwEAAQJAEJ2N+zsR0Xn8/Q6twa4G
    37  6OB1M1WO+k+ztnX/1SvNeWu8D6GImtupLTYgjZcHufykj09jiHmjHx8u8ZZB/o1N
    38  MQIhAPW+eyZo7ay3lMz1V01WVjNKK9QSn1MJlb06h/LuYv9FAiEA25WPedKgVyCW
    39  SmUwbPw8fnTcpqDWE3yTO3vKcebqMSsCIBF3UmVue8YU3jybC3NxuXq3wNm34R8T
    40  xVLHwDXh/6NJAiEAl2oHGGLz64BuAfjKrqwz7qMYr9HCLIe/YsoWq/olzScCIQDi
    41  D2lWusoe2/nEqfDVVWGWlyJ7yOmqaVm/iNUN9B2N2g==
    42  -----END PRIVATE KEY-----
    43  `
    44  
    45  // Check that the slice passed into WithScopes is copied.
    46  func TestCopyScopes(t *testing.T) {
    47  	o := &internal.DialSettings{}
    48  
    49  	scopes := []string{"a", "b"}
    50  	WithScopes(scopes...).Apply(o)
    51  
    52  	// Modify after using.
    53  	scopes[1] = "c"
    54  
    55  	if o.Scopes[0] != "a" || o.Scopes[1] != "b" {
    56  		t.Errorf("want ['a', 'b'], got %+v", o.Scopes)
    57  	}
    58  }
    59  
    60  func TestApply(t *testing.T) {
    61  	conn := &grpc.ClientConn{}
    62  	opts := []ClientOption{
    63  		WithEndpoint("https://example.com:443"),
    64  		WithScopes("a"), // the next WithScopes should overwrite this one
    65  		WithScopes("https://example.com/auth/helloworld", "https://example.com/auth/otherthing"),
    66  		WithGRPCConn(conn),
    67  		WithUserAgent("ua"),
    68  		WithCredentialsFile("service-account.json"),
    69  		WithCredentialsJSON([]byte(`{some: "json"}`)),
    70  		WithCredentials(&google.DefaultCredentials{ProjectID: "p"}),
    71  		WithAPIKey("api-key"),
    72  		WithAudiences("https://example.com/"),
    73  		WithQuotaProject("user-project"),
    74  		WithRequestReason("Request Reason"),
    75  		WithTelemetryDisabled(),
    76  		WithUniverseDomain("universe.com"),
    77  	}
    78  	var got internal.DialSettings
    79  	for _, opt := range opts {
    80  		opt.Apply(&got)
    81  	}
    82  	want := internal.DialSettings{
    83  		Scopes:            []string{"https://example.com/auth/helloworld", "https://example.com/auth/otherthing"},
    84  		UserAgent:         "ua",
    85  		Endpoint:          "https://example.com:443",
    86  		GRPCConn:          conn,
    87  		Credentials:       &google.DefaultCredentials{ProjectID: "p"},
    88  		CredentialsFile:   "service-account.json",
    89  		CredentialsJSON:   []byte(`{some: "json"}`),
    90  		APIKey:            "api-key",
    91  		Audiences:         []string{"https://example.com/"},
    92  		QuotaProject:      "user-project",
    93  		RequestReason:     "Request Reason",
    94  		TelemetryDisabled: true,
    95  		UniverseDomain:    "universe.com",
    96  	}
    97  	ignore := []cmp.Option{
    98  		cmpopts.IgnoreUnexported(grpc.ClientConn{}),
    99  		cmpopts.IgnoreFields(google.Credentials{}, "udMu", "universeDomain"),
   100  	}
   101  	if !cmp.Equal(got, want, ignore...) {
   102  		t.Errorf(cmp.Diff(got, want, ignore...))
   103  	}
   104  }
   105  
   106  func mockClientCertSource(info *tls.CertificateRequestInfo) (*tls.Certificate, error) {
   107  	cert, _ := tls.X509KeyPair([]byte(certPEM), []byte(certPEM))
   108  	return &cert, nil
   109  }
   110  
   111  func TestApplyClientCertSource(t *testing.T) {
   112  	opts := []ClientOption{
   113  		WithClientCertSource(mockClientCertSource),
   114  	}
   115  	var got internal.DialSettings
   116  	for _, opt := range opts {
   117  		opt.Apply(&got)
   118  	}
   119  	want := internal.DialSettings{
   120  		ClientCertSource: mockClientCertSource,
   121  	}
   122  
   123  	// Functions cannot be compared in Golang for equality, so we will compare the output of the functions instead.
   124  	certGot, err := got.ClientCertSource(nil)
   125  	if err != nil {
   126  		t.Error(err)
   127  	}
   128  	certWant, err := want.ClientCertSource(nil)
   129  	if err != nil {
   130  		t.Error(err)
   131  	}
   132  	if !cmp.Equal(certGot, certWant, cmpopts.IgnoreUnexported(big.Int{}), cmpopts.IgnoreFields(tls.Certificate{}, "Leaf")) {
   133  		t.Errorf(cmp.Diff(certGot, certWant, cmpopts.IgnoreUnexported(big.Int{}), cmpopts.IgnoreFields(tls.Certificate{}, "Leaf")))
   134  	}
   135  }
   136  

View as plain text