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 internal supports the options and transport packages. 6package internal 7 8import ( 9 "crypto/tls" 10 "net/http" 11 "testing" 12 13 "google.golang.org/api/internal/impersonate" 14 "google.golang.org/grpc" 15 16 "golang.org/x/oauth2" 17 "golang.org/x/oauth2/google" 18) 19 20func TestSettingsValidate(t *testing.T) { 21 dummyGetClientCertificate := func(info *tls.CertificateRequestInfo) (*tls.Certificate, error) { return nil, nil } 22 23 // Valid. 24 for _, ds := range []DialSettings{ 25 {}, 26 {APIKey: "x"}, 27 {Scopes: []string{"s"}}, 28 {CredentialsFile: "f"}, 29 {TokenSource: dummyTS{}}, 30 {CredentialsFile: "f", TokenSource: dummyTS{}}, // keep for backwards compatibility 31 {CredentialsJSON: []byte("json")}, 32 {HTTPClient: &http.Client{}}, 33 {GRPCConn: &grpc.ClientConn{}}, 34 // Although NoAuth and Scopes are technically incompatible, too many 35 // cloud clients add WithScopes to user-provided options to make 36 // the check feasible. 37 {NoAuth: true, Scopes: []string{"s"}}, 38 {ClientCertSource: dummyGetClientCertificate}, 39 {ImpersonationConfig: &impersonate.Config{Scopes: []string{"x"}}}, 40 {ImpersonationConfig: &impersonate.Config{}, Scopes: []string{"x"}}, 41 } { 42 err := ds.Validate() 43 if err != nil { 44 t.Errorf("%+v: got %v, want nil", ds, err) 45 } 46 } 47 48 // Invalid. 49 for _, ds := range []DialSettings{ 50 {NoAuth: true, APIKey: "x"}, 51 {NoAuth: true, CredentialsFile: "f"}, 52 {NoAuth: true, TokenSource: dummyTS{}}, 53 {NoAuth: true, Credentials: &google.DefaultCredentials{}}, 54 {Credentials: &google.DefaultCredentials{}, CredentialsFile: "f"}, 55 {Credentials: &google.DefaultCredentials{}, TokenSource: dummyTS{}}, 56 {Credentials: &google.DefaultCredentials{}, CredentialsJSON: []byte("json")}, 57 {CredentialsFile: "f", CredentialsJSON: []byte("json")}, 58 {CredentialsJSON: []byte("json"), TokenSource: dummyTS{}}, 59 {HTTPClient: &http.Client{}, GRPCConn: &grpc.ClientConn{}}, 60 {HTTPClient: &http.Client{}, GRPCDialOpts: []grpc.DialOption{grpc.WithInsecure()}}, 61 {Audiences: []string{"foo"}, Scopes: []string{"foo"}}, 62 {HTTPClient: &http.Client{}, QuotaProject: "foo"}, 63 {HTTPClient: &http.Client{}, RequestReason: "foo"}, 64 {HTTPClient: &http.Client{}, ClientCertSource: dummyGetClientCertificate}, 65 {ClientCertSource: dummyGetClientCertificate, GRPCConn: &grpc.ClientConn{}}, 66 {ClientCertSource: dummyGetClientCertificate, GRPCConnPool: struct{ ConnPool }{}}, 67 {ClientCertSource: dummyGetClientCertificate, GRPCDialOpts: []grpc.DialOption{grpc.WithInsecure()}}, 68 {ClientCertSource: dummyGetClientCertificate, GRPCConnPoolSize: 1}, 69 {ImpersonationConfig: &impersonate.Config{}}, 70 } { 71 err := ds.Validate() 72 if err == nil { 73 t.Errorf("%+v: got nil, want error", ds) 74 } 75 } 76 77} 78 79type dummyTS struct{} 80 81func (dummyTS) Token() (*oauth2.Token, error) { return nil, nil } 82