@@ -131,6 +131,7 @@ class FirebaseAppCheckTest : public FirebaseTest {
131
131
132
132
firebase::database::DatabaseReference CreateWorkingPath (
133
133
bool suppress_cleanup = false );
134
+ void CleanupDatabase (int expected_error);
134
135
135
136
firebase::firestore::CollectionReference GetFirestoreCollection ();
136
137
firebase::firestore::DocumentReference CreateFirestoreDoc ();
@@ -312,20 +313,7 @@ void FirebaseAppCheckTest::TerminateDatabase() {
312
313
if (!initialized_) return ;
313
314
314
315
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 );
329
317
330
318
LogDebug (" Shutdown the Database library." );
331
319
delete database_;
@@ -336,6 +324,22 @@ void FirebaseAppCheckTest::TerminateDatabase() {
336
324
ProcessEvents (100 );
337
325
}
338
326
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
+
339
343
void FirebaseAppCheckTest::InitializeAppAuthDatabase () {
340
344
InitializeApp ();
341
345
InitializeAuth ();
@@ -743,18 +747,19 @@ TEST_F(FirebaseAppCheckTest, TestPlayIntegrityProvider) {
743
747
#endif
744
748
}
745
749
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 );
748
752
// Don't initialize App Check this time. Database should fail.
749
753
InitializeAppAuthDatabase ();
750
754
firebase::database::DatabaseReference ref = CreateWorkingPath ();
751
755
const char * test_name = test_info_->name ();
752
756
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 );
755
760
}
756
761
757
- TEST_F (FirebaseAppCheckTest, DISABLED_TestDatabaseCreateWorkingPath ) {
762
+ TEST_F (FirebaseAppCheckTest, TestDatabaseCreateWorkingPath ) {
758
763
InitializeAppCheckWithDebug ();
759
764
InitializeAppAuthDatabase ();
760
765
firebase::database::DatabaseReference working_path = CreateWorkingPath ();
@@ -769,7 +774,7 @@ TEST_F(FirebaseAppCheckTest, DISABLED_TestDatabaseCreateWorkingPath) {
769
774
770
775
static const char kSimpleString [] = " Some simple string" ;
771
776
772
- TEST_F (FirebaseAppCheckTest, DISABLED_TestDatabaseSetAndGet ) {
777
+ TEST_F (FirebaseAppCheckTest, TestDatabaseSetAndGet ) {
773
778
InitializeAppCheckWithDebug ();
774
779
InitializeAppAuthDatabase ();
775
780
@@ -795,7 +800,7 @@ TEST_F(FirebaseAppCheckTest, DISABLED_TestDatabaseSetAndGet) {
795
800
}
796
801
}
797
802
798
- TEST_F (FirebaseAppCheckTest, DISABLED_TestRunTransaction ) {
803
+ TEST_F (FirebaseAppCheckTest, TestDatabaseRunTransaction ) {
799
804
InitializeAppCheckWithDebug ();
800
805
InitializeAppAuthDatabase ();
801
806
@@ -849,6 +854,42 @@ TEST_F(FirebaseAppCheckTest, DISABLED_TestRunTransaction) {
849
854
}
850
855
}
851
856
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
+
852
893
TEST_F (FirebaseAppCheckTest, TestStorageReadFile) {
853
894
InitializeAppCheckWithDebug ();
854
895
InitializeAppAuthStorage ();
@@ -1030,11 +1071,7 @@ TEST_F(FirebaseAppCheckTest, TestFirestoreListenerFailure) {
1030
1071
const firebase::firestore::DocumentSnapshot& result,
1031
1072
firebase::firestore::Error error_code,
1032
1073
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 ) {
1038
1075
// We expect one call with a Permission Denied error, from the
1039
1076
// server.
1040
1077
std::lock_guard<std::mutex> lock (mutex);
0 commit comments