-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathintegration_test.cc
More file actions
846 lines (712 loc) · 27.3 KB
/
integration_test.cc
File metadata and controls
846 lines (712 loc) · 27.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
// Copyright 2022 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <inttypes.h>
#include <algorithm>
#include <chrono>
#include <condition_variable>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <future>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include "app_framework.h" // NOLINT
#include "firebase/app.h"
#include "firebase/app_check.h"
#include "firebase/app_check/app_attest_provider.h"
#include "firebase/app_check/debug_provider.h"
#include "firebase/app_check/device_check_provider.h"
#include "firebase/app_check/play_integrity_provider.h"
#include "firebase/auth.h"
#include "firebase/database.h"
#include "firebase/functions.h"
#include "firebase/internal/platform.h"
#include "firebase/storage.h"
#include "firebase/util.h"
#include "firebase_test_framework.h" // NOLINT
// The TO_STRING macro is useful for command line defined strings as the quotes
// get stripped.
#define TO_STRING_EXPAND(X) #X
#define TO_STRING(X) TO_STRING_EXPAND(X)
// Path to the Firebase config file to load.
#ifdef FIREBASE_CONFIG
#define FIREBASE_CONFIG_STRING TO_STRING(FIREBASE_CONFIG)
#else
#define FIREBASE_CONFIG_STRING ""
#endif // FIREBASE_CONFIG
namespace firebase_testapp_automated {
// Your Firebase project's Debug token goes here.
// You can get this from Firebase Console, in the App Check settings.
const char kAppCheckDebugToken[] = "REPLACE_WITH_APP_CHECK_TOKEN";
using app_framework::LogDebug;
using app_framework::LogError;
using app_framework::LogInfo;
using app_framework::ProcessEvents;
using firebase_test_framework::FirebaseTest;
using testing::Pair;
using testing::UnorderedElementsAre;
const char kIntegrationTestRootPath[] = "integration_test_data";
const std::chrono::milliseconds kGetTokenTimeout =
std::chrono::milliseconds(5000);
class FirebaseAppCheckTest : public FirebaseTest {
public:
FirebaseAppCheckTest();
~FirebaseAppCheckTest() override;
// Called after each test.
void TearDown() override;
protected:
// Initialize App Check with the Debug provider.
void InitializeAppCheckWithDebug();
// Shut down App Check.
void TerminateAppCheck();
// Initialize Firebase App.
void InitializeApp();
// Shut down Firebase App.
void TerminateApp();
// Initialize Firebase Auth.
void InitializeAuth();
// Shut down Firebase Auth.
void TerminateAuth();
// Sign in an anonymous user.
void SignIn();
// Sign out the current user, if applicable.
// If this is an anonymous user, deletes the user instead,
// to avoid polluting the user list.
void SignOut();
// Initialize Firebase Database.
void InitializeDatabase();
// Shut down Firebase Database.
void TerminateDatabase();
// Initialize everything needed for Database tests.
void InitializeAppAuthDatabase();
// Initialize Firebase Storage.
void InitializeStorage();
// Shut down Firebase Storage.
void TerminateStorage();
// Initialize everything needed for Storage tests.
void InitializeAppAuthStorage();
// Initialize Firebase Functions.
void InitializeFunctions();
// Shut down Firebase Functions.
void TerminateFunctions();
firebase::database::DatabaseReference CreateWorkingPath(
bool suppress_cleanup = false);
void CleanupDatabase(int expected_error);
firebase::App* app_;
firebase::auth::Auth* auth_;
bool initialized_;
firebase::database::Database* database_;
std::vector<firebase::database::DatabaseReference> database_cleanup_;
firebase::storage::Storage* storage_;
firebase::functions::Functions* functions_;
};
// Listens for token changed notifications
class TestAppCheckListener : public firebase::app_check::AppCheckListener {
public:
TestAppCheckListener() : num_token_changes_(0) {}
~TestAppCheckListener() override {}
void OnAppCheckTokenChanged(
const firebase::app_check::AppCheckToken& token) override {
last_token_ = token;
num_token_changes_ += 1;
}
int num_token_changes_;
firebase::app_check::AppCheckToken last_token_;
};
// Initialization flow looks like this:
// - For each test:
// - Optionally initialize App Check.
// - Initialize App, and any additional products.
// - Run tests.
// - TearDown: Shutdowns down everything automatically.
void FirebaseAppCheckTest::InitializeAppCheckWithDebug() {
LogDebug("Initialize Firebase App Check with Debug Provider");
// Set the App Check Debug Token before providing the Factory.
firebase::app_check::DebugAppCheckProviderFactory::GetInstance()
->SetDebugToken(kAppCheckDebugToken);
firebase::app_check::AppCheck::SetAppCheckProviderFactory(
firebase::app_check::DebugAppCheckProviderFactory::GetInstance());
}
void FirebaseAppCheckTest::TerminateAppCheck() {
if (app_) {
::firebase::app_check::AppCheck* app_check =
::firebase::app_check::AppCheck::GetInstance(app_);
if (app_check) {
LogDebug("Shutdown App Check.");
delete app_check;
}
}
firebase::app_check::AppCheck::SetAppCheckProviderFactory(nullptr);
}
void FirebaseAppCheckTest::InitializeApp() {
LogDebug("Initialize Firebase App.");
FindFirebaseConfig(FIREBASE_CONFIG_STRING);
#if defined(__ANDROID__)
app_ = ::firebase::App::Create(app_framework::GetJniEnv(),
app_framework::GetActivity());
#else
app_ = ::firebase::App::Create();
#endif // defined(__ANDROID__)
ASSERT_NE(app_, nullptr);
firebase::SetLogLevel(firebase::kLogLevelVerbose);
}
void FirebaseAppCheckTest::TerminateApp() {
if (app_) {
LogDebug("Shutdown App.");
delete app_;
app_ = nullptr;
}
}
FirebaseAppCheckTest::FirebaseAppCheckTest()
: initialized_(false),
app_(nullptr),
auth_(nullptr),
database_(nullptr),
storage_(nullptr),
functions_(nullptr),
database_cleanup_() {
FindFirebaseConfig(FIREBASE_CONFIG_STRING);
}
FirebaseAppCheckTest::~FirebaseAppCheckTest() {
// Must be cleaned up on exit.
assert(app_ == nullptr);
}
void FirebaseAppCheckTest::TearDown() {
// Teardown all the products
TerminateDatabase();
TerminateStorage();
TerminateFunctions();
TerminateAuth();
TerminateAppCheck();
TerminateApp();
FirebaseTest::TearDown();
}
void FirebaseAppCheckTest::InitializeAuth() {
LogDebug("Initializing Auth.");
::firebase::ModuleInitializer initializer;
initializer.Initialize(app_, &auth_, [](::firebase::App* app, void* target) {
LogDebug("Attempting to initialize Firebase Auth.");
::firebase::InitResult result;
*reinterpret_cast<firebase::auth::Auth**>(target) =
::firebase::auth::Auth::GetAuth(app, &result);
return result;
});
WaitForCompletion(initializer.InitializeLastResult(), "InitializeAuth");
ASSERT_EQ(initializer.InitializeLastResult().error(), 0)
<< initializer.InitializeLastResult().error_message();
LogDebug("Successfully initialized Auth.");
ASSERT_NE(auth_, nullptr);
// Sign in anonymously.
SignIn();
}
void FirebaseAppCheckTest::TerminateAuth() {
if (auth_) {
LogDebug("Signing out.");
SignOut();
LogDebug("Shutdown Auth.");
delete auth_;
auth_ = nullptr;
}
}
void FirebaseAppCheckTest::InitializeDatabase() {
LogDebug("Initializing Firebase Database.");
::firebase::ModuleInitializer initializer;
initializer.Initialize(
app_, &database_, [](::firebase::App* app, void* target) {
LogDebug("Attempting to initialize Firebase Database.");
::firebase::InitResult result;
*reinterpret_cast<firebase::database::Database**>(target) =
firebase::database::Database::GetInstance(app, &result);
return result;
});
WaitForCompletion(initializer.InitializeLastResult(), "InitializeDatabase");
ASSERT_EQ(initializer.InitializeLastResult().error(), 0)
<< initializer.InitializeLastResult().error_message();
LogDebug("Successfully initialized Firebase Database.");
initialized_ = true;
}
void FirebaseAppCheckTest::TerminateDatabase() {
if (!initialized_) return;
if (database_) {
CleanupDatabase(0);
LogDebug("Shutdown the Database library.");
delete database_;
database_ = nullptr;
}
initialized_ = false;
ProcessEvents(100);
}
void FirebaseAppCheckTest::CleanupDatabase(int expected_error) {
if (!database_cleanup_.empty()) {
LogDebug("Cleaning up Database...");
std::vector<firebase::Future<void>> cleanups;
cleanups.reserve(database_cleanup_.size());
for (int i = 0; i < database_cleanup_.size(); ++i) {
cleanups.push_back(database_cleanup_[i].RemoveValue());
}
for (int i = 0; i < cleanups.size(); ++i) {
std::string cleanup_name = "Cleanup (" + database_cleanup_[i].url() + ")";
WaitForCompletion(cleanups[i], cleanup_name.c_str(), expected_error);
}
database_cleanup_.clear();
}
}
void FirebaseAppCheckTest::InitializeAppAuthDatabase() {
InitializeApp();
InitializeAuth();
InitializeDatabase();
}
void FirebaseAppCheckTest::InitializeStorage() {
LogDebug("Initializing Firebase Storage.");
::firebase::ModuleInitializer initializer;
initializer.Initialize(
app_, &storage_, [](::firebase::App* app, void* target) {
LogDebug("Attempting to initialize Firebase Storage.");
::firebase::InitResult result;
*reinterpret_cast<firebase::storage::Storage**>(target) =
firebase::storage::Storage::GetInstance(app, &result);
return result;
});
WaitForCompletion(initializer.InitializeLastResult(), "InitializeStorage");
ASSERT_EQ(initializer.InitializeLastResult().error(), 0)
<< initializer.InitializeLastResult().error_message();
LogDebug("Successfully initialized Firebase Storage.");
}
void FirebaseAppCheckTest::TerminateStorage() {
if (storage_) {
LogDebug("Shutdown the Storage library.");
delete storage_;
storage_ = nullptr;
}
ProcessEvents(100);
}
void FirebaseAppCheckTest::InitializeAppAuthStorage() {
InitializeApp();
InitializeAuth();
InitializeStorage();
}
void FirebaseAppCheckTest::InitializeFunctions() {
LogDebug("Initializing Firebase Functions.");
::firebase::ModuleInitializer initializer;
initializer.Initialize(
app_, &functions_, [](::firebase::App* app, void* target) {
LogDebug("Attempting to initialize Firebase Functions.");
::firebase::InitResult result;
*reinterpret_cast<firebase::functions::Functions**>(target) =
firebase::functions::Functions::GetInstance(app, &result);
return result;
});
WaitForCompletion(initializer.InitializeLastResult(), "InitializeFunctions");
ASSERT_EQ(initializer.InitializeLastResult().error(), 0)
<< initializer.InitializeLastResult().error_message();
LogDebug("Successfully initialized Firebase Functions.");
}
void FirebaseAppCheckTest::TerminateFunctions() {
if (functions_) {
LogDebug("Shutdown the Functions library.");
delete functions_;
functions_ = nullptr;
}
ProcessEvents(100);
}
void FirebaseAppCheckTest::SignIn() {
if (auth_->current_user().is_valid()) {
// Already signed in.
return;
}
LogDebug("Signing in.");
firebase::Future<firebase::auth::AuthResult> sign_in_future =
auth_->SignInAnonymously();
WaitForCompletion(sign_in_future, "SignInAnonymously");
if (sign_in_future.error() != 0) {
FAIL() << "Ensure your application has the Anonymous sign-in provider "
"enabled in Firebase Console.";
}
ProcessEvents(100);
}
void FirebaseAppCheckTest::SignOut() {
if (auth_ == nullptr) {
// Auth is not set up.
return;
}
if (!auth_->current_user().is_valid()) {
// Already signed out.
return;
}
if (auth_->current_user().is_anonymous()) {
// If signed in anonymously, delete the anonymous user.
WaitForCompletion(auth_->current_user().Delete(), "DeleteAnonymousUser");
// If there was a problem deleting the user, try to sign out at least.
if (auth_->current_user().is_valid()) {
auth_->SignOut();
}
} else {
// If not signed in anonymously (e.g. if the tests were modified to sign in
// as an actual user), just sign out normally.
auth_->SignOut();
// Wait for the sign-out to finish.
while (auth_->current_user().is_valid()) {
if (ProcessEvents(100)) break;
}
}
EXPECT_FALSE(auth_->current_user().is_valid());
}
firebase::database::DatabaseReference FirebaseAppCheckTest::CreateWorkingPath(
bool suppress_cleanup) {
auto ref = database_->GetReference(kIntegrationTestRootPath).PushChild();
if (!suppress_cleanup) {
database_cleanup_.push_back(ref);
}
return ref;
}
// Test cases below.
TEST_F(FirebaseAppCheckTest, TestInitializeAndTerminate) {
InitializeAppCheckWithDebug();
InitializeApp();
}
TEST_F(FirebaseAppCheckTest, TestGetTokenForcingRefresh) {
InitializeAppCheckWithDebug();
InitializeApp();
::firebase::app_check::AppCheck* app_check =
::firebase::app_check::AppCheck::GetInstance(app_);
ASSERT_NE(app_check, nullptr);
firebase::Future<::firebase::app_check::AppCheckToken> future =
app_check->GetAppCheckToken(true);
EXPECT_TRUE(WaitForCompletion(future, "GetToken #1"));
::firebase::app_check::AppCheckToken token = *future.result();
EXPECT_NE(token.token, "");
EXPECT_NE(token.expire_time_millis, 0);
// Wait a bit to make sure the expire time would be different
std::this_thread::sleep_for(std::chrono::milliseconds(500));
// GetToken with force_refresh=false will return the same token.
firebase::Future<::firebase::app_check::AppCheckToken> future2 =
app_check->GetAppCheckToken(false);
EXPECT_TRUE(WaitForCompletion(future2, "GetToken #2"));
EXPECT_EQ(future.result()->expire_time_millis,
future2.result()->expire_time_millis);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
// GetToken with force_refresh=true will return a new token.
firebase::Future<::firebase::app_check::AppCheckToken> future3 =
app_check->GetAppCheckToken(true);
EXPECT_TRUE(WaitForCompletion(future3, "GetToken #3"));
EXPECT_NE(future.result()->expire_time_millis,
future3.result()->expire_time_millis);
}
TEST_F(FirebaseAppCheckTest, TestGetTokenLastResult) {
InitializeAppCheckWithDebug();
InitializeApp();
::firebase::app_check::AppCheck* app_check =
::firebase::app_check::AppCheck::GetInstance(app_);
ASSERT_NE(app_check, nullptr);
firebase::Future<::firebase::app_check::AppCheckToken> future =
app_check->GetAppCheckToken(true);
EXPECT_TRUE(WaitForCompletion(future, "GetToken #1"));
firebase::Future<::firebase::app_check::AppCheckToken> future2 =
app_check->GetAppCheckTokenLastResult();
EXPECT_TRUE(WaitForCompletion(future2, "GetTokenLastResult"));
::firebase::app_check::AppCheckToken token = *future2.result();
EXPECT_EQ(future.result()->expire_time_millis,
future2.result()->expire_time_millis);
}
TEST_F(FirebaseAppCheckTest, TestGetLimitedUseAppCheckToken) {
InitializeAppCheckWithDebug();
InitializeApp();
::firebase::app_check::AppCheck* app_check =
::firebase::app_check::AppCheck::GetInstance(app_);
ASSERT_NE(app_check, nullptr);
firebase::Future<::firebase::app_check::AppCheckToken> future =
app_check->GetLimitedUseAppCheckToken();
EXPECT_TRUE(WaitForCompletion(future, "GetLimitedUseAppCheckToken"));
::firebase::app_check::AppCheckToken token = *future.result();
EXPECT_NE(token.token, "");
EXPECT_NE(token.expire_time_millis, 0);
firebase::Future<::firebase::app_check::AppCheckToken> future2 =
app_check->GetLimitedUseAppCheckTokenLastResult();
EXPECT_TRUE(
WaitForCompletion(future2, "GetLimitedUseAppCheckTokenLastResult"));
EXPECT_EQ(future.result()->expire_time_millis,
future2.result()->expire_time_millis);
}
TEST_F(FirebaseAppCheckTest, TestAddTokenChangedListener) {
InitializeAppCheckWithDebug();
InitializeApp();
::firebase::app_check::AppCheck* app_check =
::firebase::app_check::AppCheck::GetInstance(app_);
ASSERT_NE(app_check, nullptr);
// Create and add a token changed listener.
TestAppCheckListener token_changed_listener;
app_check->AddAppCheckListener(&token_changed_listener);
firebase::Future<::firebase::app_check::AppCheckToken> future =
app_check->GetAppCheckToken(true);
EXPECT_TRUE(WaitForCompletion(future, "GetToken"));
::firebase::app_check::AppCheckToken token = *future.result();
ASSERT_EQ(token_changed_listener.num_token_changes_, 1);
EXPECT_EQ(token_changed_listener.last_token_.token, token.token);
}
TEST_F(FirebaseAppCheckTest, TestRemoveTokenChangedListener) {
InitializeAppCheckWithDebug();
InitializeApp();
::firebase::app_check::AppCheck* app_check =
::firebase::app_check::AppCheck::GetInstance(app_);
ASSERT_NE(app_check, nullptr);
// Create, add, and immediately remove a token changed listener.
TestAppCheckListener token_changed_listener;
app_check->AddAppCheckListener(&token_changed_listener);
app_check->RemoveAppCheckListener(&token_changed_listener);
firebase::Future<::firebase::app_check::AppCheckToken> future =
app_check->GetAppCheckToken(true);
EXPECT_TRUE(WaitForCompletion(future, "GetToken"));
ASSERT_EQ(token_changed_listener.num_token_changes_, 0);
}
TEST_F(FirebaseAppCheckTest, TestSignIn) {
InitializeAppCheckWithDebug();
InitializeApp();
InitializeAuth();
EXPECT_TRUE(auth_->current_user().is_valid());
}
TEST_F(FirebaseAppCheckTest, TestDebugProviderValidToken) {
firebase::app_check::DebugAppCheckProviderFactory* factory =
firebase::app_check::DebugAppCheckProviderFactory::GetInstance();
ASSERT_NE(factory, nullptr);
InitializeAppCheckWithDebug();
InitializeApp();
firebase::app_check::AppCheckProvider* provider =
factory->CreateProvider(app_);
ASSERT_NE(provider, nullptr);
auto got_token_promise = std::make_shared<std::promise<void>>();
auto token_callback{
[got_token_promise](firebase::app_check::AppCheckToken token,
int error_code, const std::string& error_message) {
EXPECT_EQ(firebase::app_check::kAppCheckErrorNone, error_code);
EXPECT_EQ("", error_message);
EXPECT_NE(0, token.expire_time_millis);
EXPECT_NE("", token.token);
got_token_promise->set_value();
}};
provider->GetToken(token_callback);
auto got_token_future = got_token_promise->get_future();
ASSERT_EQ(std::future_status::ready,
got_token_future.wait_for(kGetTokenTimeout));
}
TEST_F(FirebaseAppCheckTest, TestDebugProviderValidLimitedUseToken) {
firebase::app_check::DebugAppCheckProviderFactory* factory =
firebase::app_check::DebugAppCheckProviderFactory::GetInstance();
ASSERT_NE(factory, nullptr);
InitializeAppCheckWithDebug();
InitializeApp();
firebase::app_check::AppCheckProvider* provider =
factory->CreateProvider(app_);
ASSERT_NE(provider, nullptr);
auto got_token_promise = std::make_shared<std::promise<void>>();
auto token_callback{
[got_token_promise](firebase::app_check::AppCheckToken token,
int error_code, const std::string& error_message) {
EXPECT_EQ(firebase::app_check::kAppCheckErrorNone, error_code);
EXPECT_EQ("", error_message);
EXPECT_NE(0, token.expire_time_millis);
EXPECT_NE("", token.token);
got_token_promise->set_value();
}};
provider->GetLimitedUseToken(token_callback);
auto got_token_future = got_token_promise->get_future();
ASSERT_EQ(std::future_status::ready,
got_token_future.wait_for(kGetTokenTimeout));
}
TEST_F(FirebaseAppCheckTest, TestAppAttestProvider) {
firebase::app_check::AppAttestProviderFactory* factory =
firebase::app_check::AppAttestProviderFactory::GetInstance();
#if FIREBASE_PLATFORM_IOS || FIREBASE_PLATFORM_TVOS
ASSERT_NE(factory, nullptr);
InitializeApp();
firebase::app_check::AppCheckProvider* provider =
factory->CreateProvider(app_);
ASSERT_NE(provider, nullptr);
#else
EXPECT_EQ(factory, nullptr);
#endif
}
TEST_F(FirebaseAppCheckTest, TestDeviceCheckProvider) {
firebase::app_check::DeviceCheckProviderFactory* factory =
firebase::app_check::DeviceCheckProviderFactory::GetInstance();
#if FIREBASE_PLATFORM_IOS || FIREBASE_PLATFORM_TVOS
ASSERT_NE(factory, nullptr);
InitializeApp();
firebase::app_check::AppCheckProvider* provider =
factory->CreateProvider(app_);
ASSERT_NE(provider, nullptr);
#else
EXPECT_EQ(factory, nullptr);
#endif
}
TEST_F(FirebaseAppCheckTest, TestPlayIntegrityProvider) {
firebase::app_check::PlayIntegrityProviderFactory* factory =
firebase::app_check::PlayIntegrityProviderFactory::GetInstance();
#if FIREBASE_PLATFORM_ANDROID
ASSERT_NE(factory, nullptr);
InitializeApp();
firebase::app_check::AppCheckProvider* provider =
factory->CreateProvider(app_);
EXPECT_NE(provider, nullptr);
#else
EXPECT_EQ(factory, nullptr);
#endif
}
TEST_F(FirebaseAppCheckTest, TestDatabaseCreateWorkingPath) {
InitializeAppCheckWithDebug();
InitializeAppAuthDatabase();
firebase::database::DatabaseReference working_path = CreateWorkingPath();
LogInfo("Database URL: %s", working_path.url().c_str());
EXPECT_TRUE(working_path.is_valid());
EXPECT_FALSE(working_path.url().empty());
EXPECT_EQ(working_path.url().find(database_->GetReference().url()), 0)
<< "Working path URL (" << working_path.url()
<< ") does not begin with root URL (" << database_->GetReference().url()
<< ")";
}
static const char kSimpleString[] = "Some simple string";
TEST_F(FirebaseAppCheckTest, TestDatabaseSetAndGet) {
InitializeAppCheckWithDebug();
InitializeAppAuthDatabase();
const char* test_name = test_info_->name();
firebase::database::DatabaseReference ref = CreateWorkingPath();
{
LogDebug("Setting value.");
firebase::Future<void> f1 =
ref.Child(test_name).Child("String").SetValue(kSimpleString);
WaitForCompletion(f1, "SetSimpleString");
}
// Get the values that we just set, and confirm that they match what we
// set them to.
{
LogDebug("Getting value.");
firebase::Future<firebase::database::DataSnapshot> f1 =
ref.Child(test_name).Child("String").GetValue();
WaitForCompletion(f1, "GetSimpleString");
EXPECT_EQ(f1.result()->value().AsString(), kSimpleString);
}
}
TEST_F(FirebaseAppCheckTest, TestDatabaseRunTransaction) {
InitializeAppCheckWithDebug();
InitializeAppAuthDatabase();
const char* test_name = test_info_->name();
firebase::database::DatabaseReference ref = CreateWorkingPath();
// Test running a transaction. This will call RunTransaction and set
// some values, including incrementing the player's score.
firebase::Future<firebase::database::DataSnapshot> transaction_future;
static const int kInitialScore = 500;
// Set an initial score of 500 points.
WaitForCompletion(
ref.Child(test_name).Child("player_score").SetValue(kInitialScore),
"SetInitialScoreValue");
// The transaction will set the player's item and class, and increment
// their score by 100 points.
int score_delta = 100;
transaction_future = ref.Child(test_name).RunTransaction(
[](firebase::database::MutableData* data, void* score_delta_void) {
LogDebug(" Transaction function executing.");
data->Child("player_item").set_value("Fire sword");
data->Child("player_class").set_value("Warrior");
// Increment the current score by 100.
int64_t score =
data->Child("player_score").value().AsInt64().int64_value();
data->Child("player_score")
.set_value(score + *reinterpret_cast<int*>(score_delta_void));
return firebase::database::kTransactionResultSuccess;
},
&score_delta);
WaitForCompletion(transaction_future, "RunTransaction");
// If the transaction succeeded, let's read back the values that were
// written to confirm they match.
if (transaction_future.error() == firebase::database::kErrorNone) {
firebase::Future<firebase::database::DataSnapshot> read_future =
ref.Child(test_name).GetValue();
WaitForCompletion(read_future, "ReadTransactionResults");
const firebase::database::DataSnapshot& read_result = *read_future.result();
EXPECT_EQ(read_result.children_count(), 3);
EXPECT_TRUE(read_result.HasChild("player_item"));
EXPECT_EQ(read_result.Child("player_item").value(), "Fire sword");
EXPECT_TRUE(read_result.HasChild("player_class"));
EXPECT_EQ(read_result.Child("player_class").value(), "Warrior");
EXPECT_TRUE(read_result.HasChild("player_score"));
EXPECT_EQ(read_result.Child("player_score").value().AsInt64(),
kInitialScore + score_delta);
EXPECT_EQ(read_result.value(), transaction_future.result()->value());
}
}
TEST_F(FirebaseAppCheckTest, TestDatabaseUpdateToken) {
// Test that after forcing an App Check token update, the database connection
// still works.
InitializeAppCheckWithDebug();
InitializeAppAuthDatabase();
const char* test_name = test_info_->name();
firebase::database::DatabaseReference ref = CreateWorkingPath();
{
LogDebug("Setting value.");
firebase::Future<void> f1 =
ref.Child(test_name).Child("String").SetValue(kSimpleString);
WaitForCompletion(f1, "SetSimpleString");
}
// Force App Check to update its token.
::firebase::app_check::AppCheck* app_check =
::firebase::app_check::AppCheck::GetInstance(app_);
ASSERT_NE(app_check, nullptr);
firebase::Future<::firebase::app_check::AppCheckToken> future =
app_check->GetAppCheckToken(true);
EXPECT_TRUE(WaitForCompletion(future, "GetAppCheckToken"));
// Get the values that we just set, and confirm that they match what we
// set them to.
{
LogDebug("Getting value.");
firebase::Future<firebase::database::DataSnapshot> f1 =
ref.Child(test_name).Child("String").GetValue();
WaitForCompletion(f1, "GetSimpleString");
EXPECT_EQ(f1.result()->value().AsString(), kSimpleString);
}
}
TEST_F(FirebaseAppCheckTest, TestStorageReadFile) {
InitializeAppCheckWithDebug();
InitializeAppAuthStorage();
firebase::storage::StorageReference ref = storage_->GetReference("test.txt");
EXPECT_TRUE(ref.is_valid());
const size_t kBufferSize = 128;
char buffer[kBufferSize];
memset(buffer, 0, sizeof(buffer));
firebase::Future<size_t> future = ref.GetBytes(buffer, kBufferSize);
WaitForCompletion(future, "GetBytes", firebase::storage::kErrorNone);
LogDebug(" buffer: %s", buffer);
}
TEST_F(FirebaseAppCheckTest, TestFunctionsSuccess) {
InitializeAppCheckWithDebug();
InitializeApp();
InitializeFunctions();
firebase::functions::HttpsCallableReference ref;
ref = functions_->GetHttpsCallable("addNumbers");
firebase::Variant data(firebase::Variant::EmptyMap());
data.map()["firstNumber"] = 5;
data.map()["secondNumber"] = 7;
firebase::Future<firebase::functions::HttpsCallableResult> future;
future = ref.Call(data);
WaitForCompletion(future, "CallFunction addnumbers",
firebase::functions::kErrorNone);
firebase::Variant result = future.result()->data();
EXPECT_TRUE(result.is_map());
if (result.is_map()) {
EXPECT_EQ(result.map()["operationResult"], 12);
}
}
} // namespace firebase_testapp_automated