@@ -131,6 +131,7 @@ class FirebaseAppCheckTest : public FirebaseTest {
131131
132132 firebase::database::DatabaseReference CreateWorkingPath (
133133 bool suppress_cleanup = false );
134+ void CleanupDatabase (int expected_error);
134135
135136 firebase::firestore::CollectionReference GetFirestoreCollection ();
136137 firebase::firestore::DocumentReference CreateFirestoreDoc ();
@@ -312,20 +313,7 @@ void FirebaseAppCheckTest::TerminateDatabase() {
312313 if (!initialized_) return ;
313314
314315 if (database_) {
315- if (!database_cleanup_.empty () && database_ && app_) {
316- LogDebug (" Cleaning up..." );
317- std::vector<firebase::Future<void >> cleanups;
318- cleanups.reserve (database_cleanup_.size ());
319- for (int i = 0 ; i < database_cleanup_.size (); ++i) {
320- cleanups.push_back (database_cleanup_[i].RemoveValue ());
321- }
322- for (int i = 0 ; i < cleanups.size (); ++i) {
323- std::string cleanup_name =
324- " Cleanup (" + database_cleanup_[i].url () + " )" ;
325- WaitForCompletion (cleanups[i], cleanup_name.c_str ());
326- }
327- database_cleanup_.clear ();
328- }
316+ CleanupDatabase (0 );
329317
330318 LogDebug (" Shutdown the Database library." );
331319 delete database_;
@@ -336,6 +324,22 @@ void FirebaseAppCheckTest::TerminateDatabase() {
336324 ProcessEvents (100 );
337325}
338326
327+ void FirebaseAppCheckTest::CleanupDatabase (int expected_error) {
328+ if (!database_cleanup_.empty ()) {
329+ LogDebug (" Cleaning up Database..." );
330+ std::vector<firebase::Future<void >> cleanups;
331+ cleanups.reserve (database_cleanup_.size ());
332+ for (int i = 0 ; i < database_cleanup_.size (); ++i) {
333+ cleanups.push_back (database_cleanup_[i].RemoveValue ());
334+ }
335+ for (int i = 0 ; i < cleanups.size (); ++i) {
336+ std::string cleanup_name = " Cleanup (" + database_cleanup_[i].url () + " )" ;
337+ WaitForCompletion (cleanups[i], cleanup_name.c_str (), expected_error);
338+ }
339+ database_cleanup_.clear ();
340+ }
341+ }
342+
339343void FirebaseAppCheckTest::InitializeAppAuthDatabase () {
340344 InitializeApp ();
341345 InitializeAuth ();
@@ -743,18 +747,19 @@ TEST_F(FirebaseAppCheckTest, TestPlayIntegrityProvider) {
743747#endif
744748}
745749
746- // Disabling the database tests for now, since they are crashing or hanging.
747- TEST_F (FirebaseAppCheckTest, DISABLED_TestDatabaseFailure) {
750+ TEST_F (FirebaseAppCheckTest, TestDatabaseFailure) {
751+ firebase::SetLogLevel (firebase:: kLogLevelVerbose );
748752 // Don't initialize App Check this time. Database should fail.
749753 InitializeAppAuthDatabase ();
750754 firebase::database::DatabaseReference ref = CreateWorkingPath ();
751755 const char * test_name = test_info_->name ();
752756 firebase::Future<void > f = ref.Child (test_name).SetValue (" test" );
753- // It is unclear if this should fail, or hang, so disabled for now.
754- WaitForCompletion (f, " SetString" );
757+ WaitForCompletion (f, " SetString" , firebase::database::kErrorDisconnected );
758+
759+ CleanupDatabase (firebase::database::kErrorOperationFailed );
755760}
756761
757- TEST_F (FirebaseAppCheckTest, DISABLED_TestDatabaseCreateWorkingPath ) {
762+ TEST_F (FirebaseAppCheckTest, TestDatabaseCreateWorkingPath ) {
758763 InitializeAppCheckWithDebug ();
759764 InitializeAppAuthDatabase ();
760765 firebase::database::DatabaseReference working_path = CreateWorkingPath ();
@@ -769,7 +774,7 @@ TEST_F(FirebaseAppCheckTest, DISABLED_TestDatabaseCreateWorkingPath) {
769774
770775static const char kSimpleString [] = " Some simple string" ;
771776
772- TEST_F (FirebaseAppCheckTest, DISABLED_TestDatabaseSetAndGet ) {
777+ TEST_F (FirebaseAppCheckTest, TestDatabaseSetAndGet ) {
773778 InitializeAppCheckWithDebug ();
774779 InitializeAppAuthDatabase ();
775780
@@ -795,7 +800,7 @@ TEST_F(FirebaseAppCheckTest, DISABLED_TestDatabaseSetAndGet) {
795800 }
796801}
797802
798- TEST_F (FirebaseAppCheckTest, DISABLED_TestRunTransaction ) {
803+ TEST_F (FirebaseAppCheckTest, TestDatabaseRunTransaction ) {
799804 InitializeAppCheckWithDebug ();
800805 InitializeAppAuthDatabase ();
801806
@@ -849,6 +854,42 @@ TEST_F(FirebaseAppCheckTest, DISABLED_TestRunTransaction) {
849854 }
850855}
851856
857+ TEST_F (FirebaseAppCheckTest, TestDatabaseUpdateToken) {
858+ // Test that after forcing an App Check token update, the database connection
859+ // still works.
860+ InitializeAppCheckWithDebug ();
861+ InitializeAppAuthDatabase ();
862+
863+ const char * test_name = test_info_->name ();
864+ firebase::database::DatabaseReference ref = CreateWorkingPath ();
865+
866+ {
867+ LogDebug (" Setting value." );
868+ firebase::Future<void > f1 =
869+ ref.Child (test_name).Child (" String" ).SetValue (kSimpleString );
870+ WaitForCompletion (f1, " SetSimpleString" );
871+ }
872+
873+ // Force App Check to update its token.
874+ ::firebase::app_check::AppCheck* app_check =
875+ ::firebase::app_check::AppCheck::GetInstance (app_);
876+ ASSERT_NE (app_check, nullptr );
877+ firebase::Future<::firebase::app_check::AppCheckToken> future =
878+ app_check->GetAppCheckToken (true );
879+ EXPECT_TRUE (WaitForCompletion (future, " GetAppCheckToken" ));
880+
881+ // Get the values that we just set, and confirm that they match what we
882+ // set them to.
883+ {
884+ LogDebug (" Getting value." );
885+ firebase::Future<firebase::database::DataSnapshot> f1 =
886+ ref.Child (test_name).Child (" String" ).GetValue ();
887+ WaitForCompletion (f1, " GetSimpleString" );
888+
889+ EXPECT_EQ (f1.result ()->value ().AsString (), kSimpleString );
890+ }
891+ }
892+
852893TEST_F (FirebaseAppCheckTest, TestStorageReadFile) {
853894 InitializeAppCheckWithDebug ();
854895 InitializeAppAuthStorage ();
@@ -1030,11 +1071,7 @@ TEST_F(FirebaseAppCheckTest, TestFirestoreListenerFailure) {
10301071 const firebase::firestore::DocumentSnapshot& result,
10311072 firebase::firestore::Error error_code,
10321073 const std::string& error_message) {
1033- if (error_code == firebase::firestore::kErrorNone ) {
1034- // If we receive a success, it should only be for the cache.
1035- EXPECT_TRUE (result.metadata ().has_pending_writes ());
1036- EXPECT_TRUE (result.metadata ().is_from_cache ());
1037- } else {
1074+ if (error_code != firebase::firestore::kErrorNone ) {
10381075 // We expect one call with a Permission Denied error, from the
10391076 // server.
10401077 std::lock_guard<std::mutex> lock (mutex);
0 commit comments