3434import com .google .auth .oauth2 .OAuth2Credentials ;
3535import com .google .auth .oauth2 .OAuth2Credentials .CredentialsChangedListener ;
3636import com .google .common .base .Defaults ;
37+ import com .google .common .collect .ImmutableMap ;
3738import com .google .common .io .BaseEncoding ;
3839import com .google .firebase .FirebaseApp .TokenRefresher ;
3940import com .google .firebase .FirebaseOptions .Builder ;
4041import com .google .firebase .database .FirebaseDatabase ;
4142import com .google .firebase .testing .FirebaseAppRule ;
4243import com .google .firebase .testing .ServiceAccount ;
4344import com .google .firebase .testing .TestUtils ;
45+ import java .io .File ;
4446import java .io .IOException ;
4547import java .lang .reflect .InvocationTargetException ;
4648import java .lang .reflect .Method ;
5052import java .util .Collection ;
5153import java .util .Date ;
5254import java .util .List ;
55+ import java .util .Map ;
5356import java .util .UUID ;
5457import java .util .concurrent .Callable ;
5558import java .util .concurrent .ExecutionException ;
@@ -419,6 +422,114 @@ public void testTokenRefresherStateMachine() {
419422 assertEquals (0 , refresher .cancelCalls );
420423 }
421424
425+ @ Test (expected = IllegalArgumentException .class )
426+ public void testEmptyFirebaseConfigFile () {
427+ setFirebaseConfigEnvironmentVariable ("firebase_config_empty.json" );
428+ FirebaseApp .initializeApp ();
429+ }
430+
431+ @ Test
432+ public void testEmptyFirebaseConfigString () {
433+ setFirebaseConfigEnvironmentVariable ("" );
434+ FirebaseApp firebaseApp = FirebaseApp .initializeApp ();
435+ assertEquals (null , firebaseApp .getOptions ().getProjectId ());
436+ assertEquals (null , firebaseApp .getOptions ().getStorageBucket ());
437+ assertEquals (null , firebaseApp .getOptions ().getDatabaseUrl ());
438+ assertTrue (firebaseApp .getOptions ().getDatabaseAuthVariableOverride ().isEmpty ());
439+ }
440+
441+ @ Test
442+ public void testEmptyFirebaseConfigJSONObject () {
443+ setFirebaseConfigEnvironmentVariable ("{}" );
444+ FirebaseApp firebaseApp = FirebaseApp .initializeApp ();
445+ assertEquals (null , firebaseApp .getOptions ().getProjectId ());
446+ assertEquals (null , firebaseApp .getOptions ().getStorageBucket ());
447+ assertEquals (null , firebaseApp .getOptions ().getDatabaseUrl ());
448+ assertTrue (firebaseApp .getOptions ().getDatabaseAuthVariableOverride ().isEmpty ());
449+ }
450+
451+ @ Test (expected = IllegalStateException .class )
452+ public void testInvalidFirebaseConfigFile () {
453+ setFirebaseConfigEnvironmentVariable ("firebase_config_invalid.json" );
454+ FirebaseApp .initializeApp ();
455+ }
456+
457+ @ Test (expected = IllegalStateException .class )
458+ public void testInvalidFirebaseConfigString () {
459+ setFirebaseConfigEnvironmentVariable ("{,," );
460+ FirebaseApp .initializeApp ();
461+ }
462+
463+ @ Test (expected = IllegalStateException .class )
464+ public void testFirebaseConfigMissingFile () {
465+ setFirebaseConfigEnvironmentVariable ("no_such.json" );
466+ FirebaseApp .initializeApp ();
467+ }
468+
469+ @ Test
470+ public void testFirebaseConfigFileWithSomeKeysMissing () {
471+ setFirebaseConfigEnvironmentVariable ("firebase_config_partial.json" );
472+ FirebaseApp firebaseApp = FirebaseApp .initializeApp ();
473+ assertEquals ("hipster-chat-mock" , firebaseApp .getOptions ().getProjectId ());
474+ assertEquals ("https://hipster-chat.firebaseio.mock" , firebaseApp .getOptions ().getDatabaseUrl ());
475+ }
476+
477+ @ Test
478+ public void testValidFirebaseConfigFile () {
479+ setFirebaseConfigEnvironmentVariable ("firebase_config.json" );
480+ FirebaseApp firebaseApp = FirebaseApp .initializeApp ();
481+ assertEquals ("hipster-chat-mock" , firebaseApp .getOptions ().getProjectId ());
482+ assertEquals ("hipster-chat.appspot.mock" , firebaseApp .getOptions ().getStorageBucket ());
483+ assertEquals ("https://hipster-chat.firebaseio.mock" , firebaseApp .getOptions ().getDatabaseUrl ());
484+ assertEquals ("testuser" , firebaseApp .getOptions ().getDatabaseAuthVariableOverride ().get ("uid" ));
485+ }
486+
487+ @ Test
488+ public void testEnvironmentVariableIgnored () {
489+ setFirebaseConfigEnvironmentVariable ("firebase_config.json" );
490+ FirebaseApp firebaseApp = FirebaseApp .initializeApp (OPTIONS );
491+ assertEquals (null , firebaseApp .getOptions ().getProjectId ());
492+ assertEquals (null , firebaseApp .getOptions ().getStorageBucket ());
493+ assertEquals (null , firebaseApp .getOptions ().getDatabaseUrl ());
494+ assertTrue (firebaseApp .getOptions ().getDatabaseAuthVariableOverride ().isEmpty ());
495+ }
496+
497+ @ Test
498+ public void testValidFirebaseConfigString () {
499+ setFirebaseConfigEnvironmentVariable ("{"
500+ + "\" databaseAuthVariableOverride\" : {"
501+ + "\" uid\" :"
502+ + "\" testuser\" "
503+ + "},"
504+ + "\" databaseUrl\" : \" https://hipster-chat.firebaseio.mock\" ,"
505+ + "\" projectId\" : \" hipster-chat-mock\" ,"
506+ + "\" storageBucket\" : \" hipster-chat.appspot.mock\" "
507+ + "}" );
508+ FirebaseApp firebaseApp = FirebaseApp .initializeApp ();
509+ assertEquals ("hipster-chat-mock" , firebaseApp .getOptions ().getProjectId ());
510+ assertEquals ("hipster-chat.appspot.mock" , firebaseApp .getOptions ().getStorageBucket ());
511+ assertEquals ("https://hipster-chat.firebaseio.mock" , firebaseApp .getOptions ().getDatabaseUrl ());
512+ assertEquals ("testuser" ,
513+ firebaseApp .getOptions ().getDatabaseAuthVariableOverride ().get ("uid" ));
514+ }
515+
516+ @ Test
517+ public void testFirebaseConfigFileIgnoresInvalidKey () {
518+ setFirebaseConfigEnvironmentVariable ("firebase_config_invalid_key.json" );
519+ FirebaseApp firebaseApp = FirebaseApp .initializeApp ();
520+ assertEquals ("hipster-chat-mock" , firebaseApp .getOptions ().getProjectId ());
521+ }
522+
523+ @ Test
524+ public void testFirebaseConfigStringIgnoresInvalidKey () {
525+ setFirebaseConfigEnvironmentVariable ("{"
526+ + "\" databaseUareL\" : \" https://hipster-chat.firebaseio.mock\" ,"
527+ + "\" projectId\" : \" hipster-chat-mock\" "
528+ + "}" );
529+ FirebaseApp firebaseApp = FirebaseApp .initializeApp ();
530+ assertEquals ("hipster-chat-mock" , firebaseApp .getOptions ().getProjectId ());
531+ }
532+
422533 @ Test (expected = IllegalArgumentException .class )
423534 public void testFirebaseExceptionNullDetail () {
424535 new FirebaseException (null );
@@ -428,6 +539,20 @@ public void testFirebaseExceptionNullDetail() {
428539 public void testFirebaseExceptionEmptyDetail () {
429540 new FirebaseException ("" );
430541 }
542+
543+
544+ private void setFirebaseConfigEnvironmentVariable (String configJSON ) {
545+ String configValue = "" ;
546+ if (configJSON .isEmpty () || configJSON .startsWith ("{" )) {
547+ configValue = configJSON ;
548+ } else {
549+ configValue = new File ("src/test/resources/" + configJSON )
550+ .getAbsolutePath ();
551+ }
552+ Map <String , String > environmentVariables =
553+ ImmutableMap .of (FirebaseApp .FIREBASE_CONFIG_ENV_VAR , configValue );
554+ TestUtils .setEnvironmentVariables (environmentVariables );
555+ }
431556
432557 private static class MockTokenRefresher extends TokenRefresher {
433558
0 commit comments