@@ -119,17 +119,18 @@ describe('grpc', () => {
119119 } ) ;
120120 } ) ;
121121
122+ class DummyStub {
123+ constructor (
124+ public address : { } ,
125+ public creds : { } ,
126+ public options : { [ index : string ] : string | number | Function }
127+ ) { }
128+ }
129+
122130 describe ( 'createStub' , ( ) => {
123- class DummyStub {
124- constructor (
125- public address : { } ,
126- public creds : { } ,
127- public options : { [ index : string ] : string | number | Function }
128- ) { }
129- }
130131 let grpcClient : GrpcClient ;
131132 const dummyChannelCreds = { channelCreds : 'dummyChannelCreds' } ;
132- const stubAuth = { getClient : sinon . stub ( ) } ;
133+ const stubAuth = { getClient : sinon . stub ( ) , getUniverseDomain : sinon . stub ( ) } ;
133134 const stubGrpc = {
134135 credentials : {
135136 createSsl : sinon . stub ( ) ,
@@ -148,6 +149,7 @@ describe('grpc', () => {
148149 stubGrpc . credentials . createFromGoogleCredential . reset ( ) ;
149150
150151 stubAuth . getClient . resolves ( dummyAuth ) ;
152+ stubAuth . getUniverseDomain . resolves ( 'googleapis.com' ) ;
151153 stubGrpc . credentials . createSsl . returns ( dummySslCreds ) ;
152154 stubGrpc . credentials . createFromGoogleCredential
153155 . withArgs ( dummyAuth )
@@ -176,6 +178,30 @@ describe('grpc', () => {
176178 } ) ;
177179 } ) ;
178180
181+ it ( 'validates universe domain if set' , async ( ) => {
182+ const opts = {
183+ servicePath : 'foo.example.com' ,
184+ port : 443 ,
185+ universeDomain : 'example.com' ,
186+ } ;
187+ assert . rejects (
188+ // @ts -ignore
189+ grpcClient . createStub ( DummyStub , opts ) ,
190+ / c o n f i g u r e d u n i v e r s e d o m a i n /
191+ ) ;
192+ } ) ;
193+
194+ it ( 'validates universe domain if unset' , async ( ) => {
195+ const opts = { servicePath : 'foo.example.com' , port : 443 } ;
196+ stubAuth . getUniverseDomain . reset ( ) ;
197+ stubAuth . getUniverseDomain . resolves ( 'example.com' ) ;
198+ assert . rejects (
199+ // @ts -ignore
200+ grpcClient . createStub ( DummyStub , opts ) ,
201+ / c o n f i g u r e d u n i v e r s e d o m a i n /
202+ ) ;
203+ } ) ;
204+
179205 it ( 'supports optional parameters' , ( ) => {
180206 const opts = {
181207 servicePath : 'foo.example.com' ,
@@ -659,5 +685,24 @@ dvorak
659685 assert . ok ( key . includes ( 'dvorak' ) ) ;
660686 rimrafSync ( tmpFolder ) ; // Cleanup.
661687 } ) ;
688+ it ( 'throws if attempted to use mTLS in non-default universe' , async ( ) => {
689+ // Pretend that "tmp-secure-context" in the current folder is the
690+ // home directory, so that we can test logic for loading
691+ // context_aware_metadata.json from well known location:
692+ const tmpdir = path . join ( tmpFolder , '.secureConnect' ) ;
693+ mkdirSync ( tmpdir , { recursive : true } ) ;
694+ const metadataFile = path . join ( tmpdir , 'context_aware_metadata.json' ) ;
695+ writeFileSync ( metadataFile , JSON . stringify ( metadataFileContents ) , 'utf8' ) ;
696+ sandbox . stub ( os , 'homedir' ) . returns ( tmpFolder ) ;
697+ // Create a client and test the certificate detection flow:
698+ process . env . GOOGLE_API_USE_CLIENT_CERTIFICATE = 'true' ;
699+ const client = gaxGrpc ( ) ;
700+ assert . rejects (
701+ // @ts -ignore
702+ client . createStub ( DummyStub , { universeDomain : 'example.com' } ) ,
703+ / c o n f i g u r e d u n i v e r s e d o m a i n /
704+ ) ;
705+ rimrafSync ( tmpFolder ) ; // Cleanup.
706+ } ) ;
662707 } ) ;
663708} ) ;
0 commit comments