Skip to content

Commit e8c20f6

Browse files
committed
Integrate Latest @ 155531185
Changes to auth/testapp ... - Ensure current User is maintained on call failure. - Add additional checks of Future result values. - Add additional checks linking credentials. Changes to database/testapp ... - Clean up DatabaseReference to fix a testapp crash on exit. Changes to messaging/testapp ... - Added a check to confirm a message was received. Changes to remote_config/testapp ... - Add additional checks for timestamps. Changes to storage/testapp ... - Clean up StorageReference to fix a testapp crash on exit. - Demonstrate creating a blank valid Metadata and using it for PutFile. CL: 155531185
1 parent 8ffbea0 commit e8c20f6

File tree

5 files changed

+129
-28
lines changed

5 files changed

+129
-28
lines changed

auth/testapp/src/common_main.cc

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,6 @@ static bool WaitForSignInFuture(Future<User*> sign_in_future, const char* fn,
111111
static_cast<int>(reinterpret_cast<intptr_t>(auth_user)));
112112
}
113113

114-
const bool should_be_null = expected_error != kAuthErrorNone;
115-
const bool is_null = sign_in_user == nullptr;
116-
if (should_be_null != is_null) {
117-
LogMessage("ERROR: user pointer (%x) is incorrect",
118-
static_cast<int>(reinterpret_cast<intptr_t>(auth_user)));
119-
}
120114
return false;
121115
}
122116

@@ -326,7 +320,7 @@ extern "C" int common_main(int argc, const char* argv[]) {
326320
auth->SignOut();
327321
if (auth->current_user() != nullptr) {
328322
LogMessage(
329-
"ERROR: current_user() returning %x instead of NULL after "
323+
"ERROR: current_user() returning %x instead of nullptr after "
330324
"SignOut()",
331325
auth->current_user());
332326
}
@@ -373,12 +367,15 @@ extern "C" int common_main(int argc, const char* argv[]) {
373367
Future<User*> sign_in_future = auth->SignInAnonymously();
374368
WaitForSignInFuture(sign_in_future, "Auth::SignInAnonymously()",
375369
kAuthErrorNone, auth);
370+
ExpectTrue("SignInAnonymouslyLastResult matches returned Future",
371+
sign_in_future == auth->SignInAnonymouslyLastResult());
372+
376373
// Test SignOut() after signed in anonymously.
377374
if (sign_in_future.status() == ::firebase::kFutureStatusComplete) {
378375
auth->SignOut();
379376
if (auth->current_user() != nullptr) {
380377
LogMessage(
381-
"ERROR: current_user() returning %x instead of NULL after "
378+
"ERROR: current_user() returning %x instead of nullptr after "
382379
"SignOut()",
383380
auth->current_user());
384381
}
@@ -391,6 +388,10 @@ extern "C" int common_main(int argc, const char* argv[]) {
391388
auth->FetchProvidersForEmail(user_login.email());
392389
WaitForFuture(providers_future, "Auth::FetchProvidersForEmail()",
393390
kAuthErrorNone);
391+
ExpectTrue(
392+
"FetchProvidersForEmailLastResult matches returned Future",
393+
providers_future == auth->FetchProvidersForEmailLastResult());
394+
394395
const Auth::FetchProvidersResult* pro = providers_future.result();
395396
if (pro) {
396397
LogMessage(" email %s, num providers %d", user_login.email(),
@@ -411,12 +412,16 @@ extern "C" int common_main(int argc, const char* argv[]) {
411412
sign_in_future,
412413
"Auth::SignInWithEmailAndPassword() existing email and password",
413414
kAuthErrorNone, auth);
415+
ExpectTrue(
416+
"SignInWithEmailAndPasswordLastResult matches returned Future",
417+
sign_in_future == auth->SignInWithEmailAndPasswordLastResult());
418+
414419
// Test SignOut() after signed in with email and password.
415420
if (sign_in_future.status() == ::firebase::kFutureStatusComplete) {
416421
auth->SignOut();
417422
if (auth->current_user() != nullptr) {
418423
LogMessage(
419-
"ERROR: current_user() returning %x instead of NULL after "
424+
"ERROR: current_user() returning %x instead of nullptr after "
420425
"SignOut()",
421426
auth->current_user());
422427
}
@@ -450,6 +455,10 @@ extern "C" int common_main(int argc, const char* argv[]) {
450455
create_future_bad,
451456
"Auth::CreateUserWithEmailAndPassword() existing email",
452457
kAuthErrorFailure, auth);
458+
ExpectTrue(
459+
"CreateUserWithEmailAndPasswordLastResult matches returned Future",
460+
create_future_bad ==
461+
auth->CreateUserWithEmailAndPasswordLastResult());
453462
}
454463

455464
// Test Auth::SignInWithCredential() using email&password.
@@ -462,6 +471,8 @@ extern "C" int common_main(int argc, const char* argv[]) {
462471
WaitForSignInFuture(sign_in_cred_ok,
463472
"Auth::SignInWithCredential() existing email",
464473
kAuthErrorNone, auth);
474+
ExpectTrue("SignInWithCredentialLastResult matches returned Future",
475+
sign_in_cred_ok == auth->SignInWithCredentialLastResult());
465476
}
466477

467478
// Use bad Facebook credentials. Should fail.
@@ -516,6 +527,9 @@ extern "C" int common_main(int argc, const char* argv[]) {
516527
WaitForFuture(send_password_reset_ok,
517528
"Auth::SendPasswordResetEmail() existing email",
518529
kAuthErrorNone);
530+
ExpectTrue(
531+
"SendPasswordResetEmailLastResult matches returned Future",
532+
send_password_reset_ok == auth->SendPasswordResetEmailLastResult());
519533
}
520534

521535
// Use bad email. Should fail.
@@ -554,14 +568,42 @@ extern "C" int common_main(int argc, const char* argv[]) {
554568
ExpectFalse("Email email is_email_verified()",
555569
anonymous_user->is_email_verified());
556570

557-
// Test User::LinkWithCredential().
571+
// Test User::LinkWithCredential(), linking with email & password.
558572
const std::string newer_email = CreateNewEmail();
559573
Credential user_cred = EmailAuthProvider::GetCredential(
560574
newer_email.c_str(), kTestPassword);
561-
Future<User*> link_future =
562-
anonymous_user->LinkWithCredential(user_cred);
563-
WaitForSignInFuture(link_future, "User::LinkWithCredential()",
564-
kAuthErrorNone, auth);
575+
{
576+
Future<User*> link_future =
577+
anonymous_user->LinkWithCredential(user_cred);
578+
WaitForSignInFuture(link_future, "User::LinkWithCredential()",
579+
kAuthErrorNone, auth);
580+
}
581+
582+
// Test User::LinkWithCredential(), linking with same email & password.
583+
{
584+
Future<User*> link_future =
585+
anonymous_user->LinkWithCredential(user_cred);
586+
WaitForSignInFuture(link_future, "User::LinkWithCredential() again",
587+
kAuthErrorNone, auth);
588+
}
589+
590+
// Test User::LinkWithCredential(), linking with bad credential.
591+
// Call should fail and Auth's current user should be maintained.
592+
{
593+
const User* pre_link_user = auth->current_user();
594+
ExpectTrue("Test precondition requires active user",
595+
pre_link_user != nullptr);
596+
597+
Credential twitter_cred_bad = TwitterAuthProvider::GetCredential(
598+
kTestIdTokenBad, kTestAccessTokenBad);
599+
Future<User*> link_bad_future =
600+
anonymous_user->LinkWithCredential(twitter_cred_bad);
601+
WaitForFuture(link_bad_future,
602+
"User::LinkWithCredential() with bad credential",
603+
kAuthErrorFailure);
604+
ExpectTrue("Linking maintains user",
605+
auth->current_user() == pre_link_user);
606+
}
565607

566608
UserLogin user_login(auth);
567609
user_login.Register();

database/testapp/src/common_main.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ extern "C" int common_main(int argc, const char* argv[]) {
214214
std::string saved_url; // persists across connections
215215

216216
// Create a unique child in the database that we can run our tests in.
217-
firebase::database::DatabaseReference ref =
218-
database->GetReference("test_app_data").PushChild();
217+
firebase::database::DatabaseReference ref;
218+
ref = database->GetReference("test_app_data").PushChild();
219219

220220
saved_url = ref.url();
221221
LogMessage("URL: %s", saved_url.c_str());
@@ -901,6 +901,9 @@ extern "C" int common_main(int argc, const char* argv[]) {
901901
future.error(), future.error_message());
902902
}
903903

904+
// Clean up the DatabaseReference before deleting the Database instance.
905+
ref = firebase::database::DatabaseReference();
906+
904907
LogMessage("Shutdown the Database library.");
905908
delete database;
906909
database = nullptr;

messaging/testapp/src/android/java/com/google/firebase/example/TestappNativeActivity.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import android.app.NativeActivity;
66
import android.content.Intent;
7+
import android.os.Bundle;
78
import com.google.firebase.messaging.MessageForwardingService;
89

910
/**
@@ -15,6 +16,18 @@
1516
* background.
1617
*/
1718
public class TestappNativeActivity extends NativeActivity {
19+
// The key in the intent's extras that maps to the incoming message's message ID. Only sent by
20+
// the server, GmsCore sends EXTRA_MESSAGE_ID_KEY below. Server can't send that as it would get
21+
// stripped by the client.
22+
private static final String EXTRA_MESSAGE_ID_KEY_SERVER = "message_id";
23+
24+
// An alternate key value in the intent's extras that also maps to the incoming message's message
25+
// ID. Used by upstream, and set by GmsCore.
26+
private static final String EXTRA_MESSAGE_ID_KEY = "google.message_id";
27+
28+
// The key in the intent's extras that maps to the incoming message's sender value.
29+
private static final String EXTRA_FROM = "google.message_id";
30+
1831
/**
1932
* Workaround for when a message is sent containing both a Data and Notification payload.
2033
*
@@ -28,10 +41,20 @@ public class TestappNativeActivity extends NativeActivity {
2841
*/
2942
@Override
3043
protected void onNewIntent(Intent intent) {
31-
Intent message = new Intent(this, MessageForwardingService.class);
32-
message.setAction(MessageForwardingService.ACTION_REMOTE_INTENT);
33-
message.putExtras(intent);
34-
startService(message);
44+
// If we do not have a 'from' field this intent was not a message and should not be handled. It
45+
// probably means this intent was fired by tapping on the app icon.
46+
Bundle extras = intent.getExtras();
47+
String from = extras.getString(EXTRA_FROM);
48+
String messageId = extras.getString(EXTRA_MESSAGE_ID_KEY);
49+
if (messageId == null) {
50+
messageId = extras.getString(EXTRA_MESSAGE_ID_KEY_SERVER);
51+
}
52+
if (from != null && messageId != null) {
53+
Intent message = new Intent(this, MessageForwardingService.class);
54+
message.setAction(MessageForwardingService.ACTION_REMOTE_INTENT);
55+
message.putExtras(intent);
56+
startService(message);
57+
}
3558
setIntent(intent);
3659
}
3760
}

remote_config/testapp/src/common_main.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,14 @@ extern "C" int common_main(int argc, const char* argv[]) {
116116
LogMessage("ActivateFetched %s", activate_result ? "succeeded" : "failed");
117117

118118
const remote_config::ConfigInfo& info = remote_config::GetInfo();
119-
LogMessage("Info last_fetch_time_ms=%d fetch_status=%d failure_reason=%d",
120-
static_cast<int>(info.fetch_time), info.last_fetch_status,
121-
info.last_fetch_failure_reason);
119+
LogMessage(
120+
"Info last_fetch_time_ms=%d (year=%.2f) fetch_status=%d "
121+
"failure_reason=%d throttled_end_time=%d",
122+
static_cast<int>(info.fetch_time),
123+
1970.0f + static_cast<float>(info.fetch_time) /
124+
(1000.0f * 60.0f * 60.0f * 24.0f * 365.0f),
125+
info.last_fetch_status, info.last_fetch_failure_reason,
126+
info.throttled_end_time);
122127

123128
// Print out the new values, which may be updated from the Fetch.
124129
{

storage/testapp/src/common_main.cc

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ extern "C" int common_main(int argc, const char* argv[]) {
158158
std::string saved_url = buffer;
159159

160160
// Create a unique child in the storage that we can run our tests in.
161-
firebase::storage::StorageReference ref =
162-
storage->GetReference("test_app_data").Child(saved_url);
161+
firebase::storage::StorageReference ref;
162+
ref = storage->GetReference("test_app_data").Child(saved_url);
163163

164164
LogMessage("Storage URL: gs://%s/%s", ref.bucket().c_str(),
165165
ref.full_path().c_str());
@@ -212,12 +212,18 @@ extern "C" int common_main(int argc, const char* argv[]) {
212212
std::fwrite(kSimpleTestFile.c_str(), 1, kSimpleTestFile.size(), file);
213213
fclose(file);
214214

215+
firebase::storage::Metadata new_metadata;
216+
new_metadata.set_content_type("text/html");
217+
new_metadata.custom_metadata()->insert(std::make_pair("hello", "world"));
218+
215219
firebase::Future<firebase::storage::Metadata> future =
216-
ref.Child("TestFile").Child("File2.txt").PutFile(file_path.c_str());
220+
ref.Child("TestFile")
221+
.Child("File2.txt")
222+
.PutFile(file_path.c_str(), new_metadata);
217223
WaitForCompletion(future, "Write File");
218224
if (future.error() != firebase::storage::kErrorNone) {
219225
LogMessage("ERROR: Write file failed.");
220-
LogMessage(" File1.txt: Error %d: %s", future.error(),
226+
LogMessage(" File2.txt: Error %d: %s", future.error(),
221227
future.error_message());
222228
} else {
223229
LogMessage("SUCCESS: Wrote file with PutFile.");
@@ -229,6 +235,25 @@ extern "C" int common_main(int argc, const char* argv[]) {
229235
LogMessage(" Got %i bytes, expected %i bytes.",
230236
metadata->size_bytes(), kSimpleTestFile.size());
231237
}
238+
if (strcmp(metadata->content_type(), new_metadata.content_type()) ==
239+
0) {
240+
LogMessage(
241+
"SUCCESS: Metadata has correct content type set at upload.");
242+
} else {
243+
LogMessage(
244+
"ERROR: Metadata has incorrect content type set at upload.");
245+
LogMessage(" Got %s, expected %s.", metadata->content_type(),
246+
new_metadata.content_type());
247+
}
248+
auto pair1 = metadata->custom_metadata()->find("hello");
249+
if (pair1 != metadata->custom_metadata()->end() &&
250+
pair1->second == "world") {
251+
LogMessage(
252+
"SUCCESS: Metadata has correct custom metadata set at upload.");
253+
} else {
254+
LogMessage(
255+
"SUCCESS: Metadata has incorrect custom metadata set at upload.");
256+
}
232257
}
233258
}
234259

@@ -342,7 +367,7 @@ extern "C" int common_main(int argc, const char* argv[]) {
342367
custom_metadata->insert(std::make_pair("Key", "Value"));
343368
custom_metadata->insert(std::make_pair("Foo", "Bar"));
344369
firebase::Future<firebase::storage::Metadata> custom_metadata_future =
345-
ref.Child("TestFile").Child("File1.txt").UpdateMetadata(metadata);
370+
ref.Child("TestFile").Child("File1.txt").UpdateMetadata(*metadata);
346371
WaitForCompletion(custom_metadata_future, "UpdateMetadata");
347372
if (future.error() != firebase::storage::kErrorNone) {
348373
LogMessage("ERROR: UpdateMetadata failed.");
@@ -562,6 +587,9 @@ extern "C" int common_main(int argc, const char* argv[]) {
562587
}
563588
}
564589

590+
// Clean up the StorageReference before deleting the Storage instance.
591+
ref = firebase::storage::StorageReference();
592+
565593
LogMessage("Shutdown the Storage library.");
566594
delete storage;
567595
storage = nullptr;

0 commit comments

Comments
 (0)