Skip to content

Commit

Permalink
Merge branch 'main' into workflow/auto-update-deps-199-20240209-231054
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsimantov authored Feb 12, 2024
2 parents 856a71f + 1c76fd3 commit a011584
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 58 deletions.
1 change: 1 addition & 0 deletions analytics/integration_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ else()
# Platform abstraction layer for the desktop integration test.
set(FIREBASE_APP_FRAMEWORK_DESKTOP_SRCS
src/desktop/desktop_app_framework.cc
src/desktop/desktop_firebase_test_framework.cc
)

set(integration_test_target_name "integration_test")
Expand Down
1 change: 1 addition & 0 deletions app/integration_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ else()
# Platform abstraction layer for the desktop integration test.
set(FIREBASE_APP_FRAMEWORK_DESKTOP_SRCS
src/desktop/desktop_app_framework.cc
src/desktop/desktop_firebase_test_framework.cc
)

set(integration_test_target_name "integration_test")
Expand Down
1 change: 1 addition & 0 deletions app_check/integration_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ else()
# Platform abstraction layer for the desktop integration test.
set(FIREBASE_APP_FRAMEWORK_DESKTOP_SRCS
src/desktop/desktop_app_framework.cc
src/desktop/desktop_firebase_test_framework.cc
)

set(integration_test_target_name "integration_test")
Expand Down
1 change: 1 addition & 0 deletions auth/integration_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ else()
# Platform abstraction layer for the desktop integration test.
set(FIREBASE_APP_FRAMEWORK_DESKTOP_SRCS
src/desktop/desktop_app_framework.cc
src/desktop/desktop_firebase_test_framework.cc
)

set(integration_test_target_name "integration_test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
D62CCBC022F367140099BE9F /* gmock-all.cc in Sources */ = {isa = PBXBuildFile; fileRef = D62CCBBF22F367140099BE9F /* gmock-all.cc */; };
D66B16871CE46E8900E5638A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D66B16861CE46E8900E5638A /* LaunchScreen.storyboard */; };
D67D355822BABD2200292C1D /* gtest-all.cc in Sources */ = {isa = PBXBuildFile; fileRef = D67D355622BABD2100292C1D /* gtest-all.cc */; };
D6A908EC2B786EDF00EF5160 /* ios_firebase_test_framework.mm in Sources */ = {isa = PBXBuildFile; fileRef = D6C179E822CB322900C2651A /* ios_firebase_test_framework.mm */; };
D6C179E922CB322900C2651A /* ios_app_framework.mm in Sources */ = {isa = PBXBuildFile; fileRef = D6C179E722CB322900C2651A /* ios_app_framework.mm */; };
D6C179EA22CB322900C2651A /* ios_firebase_test_framework.mm in Sources */ = {isa = PBXBuildFile; fileRef = D6C179E822CB322900C2651A /* ios_firebase_test_framework.mm */; };
D6C179EE22CB323300C2651A /* firebase_test_framework.cc in Sources */ = {isa = PBXBuildFile; fileRef = D6C179EC22CB323300C2651A /* firebase_test_framework.cc */; };
Expand Down Expand Up @@ -368,6 +369,7 @@
9F3A09032669792100E1D69F /* app_framework.cc in Sources */,
9F3A09022669791E00E1D69F /* gtest-all.cc in Sources */,
9F3A09042669792400E1D69F /* firebase_test_framework.cc in Sources */,
D6A908EC2B786EDF00EF5160 /* ios_firebase_test_framework.mm in Sources */,
9F5DD0AC266A7AD30058C144 /* integration_test.cc in Sources */,
9F5DD0AB266A7ACE0058C144 /* ios_app_framework.mm in Sources */,
);
Expand Down
1 change: 1 addition & 0 deletions database/integration_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ else()
# Platform abstraction layer for the desktop integration test.
set(FIREBASE_APP_FRAMEWORK_DESKTOP_SRCS
src/desktop/desktop_app_framework.cc
src/desktop/desktop_firebase_test_framework.cc
)

set(integration_test_target_name "integration_test")
Expand Down
40 changes: 11 additions & 29 deletions database/integration_test/src/integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ class FirebaseDatabaseTest : public FirebaseTest {
// Shut down Firebase Database.
void TerminateDatabase();

int64_t GetRemoteTimeInMilliseconds();

firebase::database::DatabaseReference CreateWorkingPath(
bool suppress_cleanup = false);

Expand Down Expand Up @@ -368,30 +366,6 @@ firebase::database::DatabaseReference FirebaseDatabaseTest::CreateWorkingPath(
return ref;
}

int64_t FirebaseDatabaseTest::GetRemoteTimeInMilliseconds() {
firebase::database::DatabaseReference ref =
CreateWorkingPath().Child("timestamp");
WaitForCompletionAnyResult(
ref.SetValue(firebase::database::ServerTimestamp()),
"GetRemoteTime_SetValue");
firebase::Future<firebase::database::DataSnapshot> future = ref.GetValue();
WaitForCompletionAnyResult(future, "GetRemoteTime_GetValue");
int64_t timestamp = 0;
if (future.error() == 0 && future.result() &&
future.result()->value().is_int64()) {
timestamp = future.result()->value().int64_value();
}
if (timestamp > 0) {
LogDebug("Got server timestamp: %lld", timestamp);
LogDebug(" Local timestamp: %lld",
app_framework::GetCurrentTimeInMicroseconds() / 1000L);
return timestamp;
} else {
LogWarning("Couldn't get remote timestamp, using local time");
return app_framework::GetCurrentTimeInMicroseconds() / 1000L;
}
}

// Test cases below.
TEST_F(FirebaseDatabaseTest, TestInitializeAndTerminate) {
// Already tested via SetUp() and TearDown().
Expand Down Expand Up @@ -487,7 +461,11 @@ TEST_F(FirebaseDatabaseTest, TestSetAndGetSimpleValues) {
WaitForCompletion(f7, "GetLongDouble");

// Get the current time to compare to the Timestamp.
int64_t current_time_milliseconds = GetRemoteTimeInMilliseconds();
int64_t current_time_milliseconds =
GetCurrentTimeInSecondsSinceEpoch() * 1000LL;
LogDebug("Comparing current time %" PRId64 " with timestamp %" PRId64,
current_time_milliseconds,
f5.result()->value().AsInt64().int64_value());

EXPECT_EQ(f1.result()->value().AsString(), kSimpleString);
EXPECT_EQ(f2.result()->value().AsInt64(), kSimpleInt);
Expand Down Expand Up @@ -698,8 +676,12 @@ TEST_F(FirebaseDatabaseTest, TestUpdateChildren) {
WaitForCompletion(update_future, "UpdateChildren");
read_future = ref.Child(test_name).GetValue();
WaitForCompletion(read_future, "GetValue 2");
int64_t current_time_milliseconds = GetRemoteTimeInMilliseconds();

int64_t current_time_milliseconds =
GetCurrentTimeInSecondsSinceEpoch() * 1000L;
LogDebug(
"Comparing current time %" PRId64 " with timestamp %" PRId64,
current_time_milliseconds,
read_future.result()->value().map()["timestamp"].AsInt64().int64_value());
EXPECT_THAT(
read_future.result()->value().map(),
UnorderedElementsAre(
Expand Down
1 change: 1 addition & 0 deletions firestore/integration_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ else()
# Platform abstraction layer for the desktop integration test.
set(FIREBASE_APP_FRAMEWORK_DESKTOP_SRCS
src/desktop/desktop_app_framework.cc
src/desktop/desktop_firebase_test_framework.cc
)

set(integration_test_target_name "integration_test")
Expand Down
1 change: 1 addition & 0 deletions firestore/integration_test_internal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ else()
# Platform abstraction layer for the desktop integration test.
set(FIREBASE_APP_FRAMEWORK_DESKTOP_SRCS
src/desktop/desktop_app_framework.cc
src/desktop/desktop_firebase_test_framework.cc
)

set(integration_test_target_name "integration_test")
Expand Down
1 change: 1 addition & 0 deletions functions/integration_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ else()
# Platform abstraction layer for the desktop integration test.
set(FIREBASE_APP_FRAMEWORK_DESKTOP_SRCS
src/desktop/desktop_app_framework.cc
src/desktop/desktop_firebase_test_framework.cc
)

set(integration_test_target_name "integration_test")
Expand Down
1 change: 1 addition & 0 deletions installations/integration_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ else()
# Platform abstraction layer for the desktop integration test.
set(FIREBASE_APP_FRAMEWORK_DESKTOP_SRCS
src/desktop/desktop_app_framework.cc
src/desktop/desktop_firebase_test_framework.cc
)

set(integration_test_target_name "integration_test")
Expand Down
1 change: 1 addition & 0 deletions remote_config/integration_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ else()
# Platform abstraction layer for the desktop integration test.
set(FIREBASE_APP_FRAMEWORK_DESKTOP_SRCS
src/desktop/desktop_app_framework.cc
src/desktop/desktop_firebase_test_framework.cc
)

set(integration_test_target_name "integration_test")
Expand Down
1 change: 1 addition & 0 deletions storage/integration_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ else()
# Platform abstraction layer for the desktop integration test.
set(FIREBASE_APP_FRAMEWORK_DESKTOP_SRCS
src/desktop/desktop_app_framework.cc
src/desktop/desktop_firebase_test_framework.cc
)

set(integration_test_target_name "integration_test")
Expand Down
28 changes: 1 addition & 27 deletions storage/integration_test/src/integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ class FirebaseStorageTest : public FirebaseTest {
// Create a unique working folder and return a reference to it.
firebase::storage::StorageReference CreateFolder();

int64_t GetRemoteTimeInMilliseconds();

static firebase::App* shared_app_;
static firebase::auth::Auth* shared_auth_;

Expand Down Expand Up @@ -326,30 +324,6 @@ firebase::storage::StorageReference FirebaseStorageTest::CreateFolder() {
return storage_->GetReference(kRootNodeName).Child(saved_url_);
}

int64_t FirebaseStorageTest::GetRemoteTimeInMilliseconds() {
SignIn();

firebase::storage::StorageReference ref =
CreateFolder().Child("timestamp.txt");
firebase::Future<firebase::storage::Metadata> future =
RunWithRetry<firebase::storage::Metadata>(
[&]() { return ref.PutBytes("TS00", 4); });
WaitForCompletionAnyResult(future, "GetRemoteTime_PutBytes");
if (future.error() == 0 && future.result() != nullptr &&
future.result()->creation_time() > 0) {
int64_t timestamp = future.result()->creation_time();
WaitForCompletionAnyResult(RunWithRetry([&]() { return ref.Delete(); }),
"GetRemoteTime_Delete");
LogDebug("Got server timestamp: %lld", timestamp);
LogDebug(" Local timestamp: %lld",
app_framework::GetCurrentTimeInMicroseconds() / 1000L);
return timestamp;
} else {
LogWarning("Couldn't get remote timestamp, using local time");
return app_framework::GetCurrentTimeInMicroseconds() / 1000L;
}
}

// Test cases below.

TEST_F(FirebaseStorageTest, TestInitializeAndTerminate) {
Expand Down Expand Up @@ -523,7 +497,7 @@ TEST_F(FirebaseStorageTest, TestWriteAndReadFileWithCustomMetadata) {
ASSERT_NE(metadata, nullptr);

// Get the current time to compare to the Timestamp.
int64_t current_time_seconds = GetRemoteTimeInMilliseconds() / 1000L;
int64_t current_time_seconds = GetCurrentTimeInSecondsSinceEpoch();
int64_t updated_time_milliseconds = metadata->updated_time();
int64_t updated_time_seconds = updated_time_milliseconds / 1000L;
int64_t time_difference_seconds =
Expand Down
6 changes: 4 additions & 2 deletions testing/sample_framework/src/desktop/desktop_app_framework.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,11 @@ int64_t GetCurrentTimeInMicroseconds() {
now.LowPart = file_time.dwLowDateTime;
now.HighPart = file_time.dwHighDateTime;

// Windows file time has a different offset, year 1601 instead of 1970.
now.QuadPart -= 116444736000000000ULL;
// Windows file time is expressed in 100s of nanoseconds.
// To convert to microseconds, multiply x10.
return now.QuadPart * 10LL;
// To convert to microseconds, divide by 10.
return now.QuadPart / 10LL;
}
#endif // defined(_WIN32)

Expand Down
65 changes: 65 additions & 0 deletions testing/test_framework/src/firebase_test_framework.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@

#include "firebase_test_framework.h" // NOLINT

#include <inttypes.h>

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <vector>

#include "firebase/future.h"

using app_framework::LogDebug;

namespace firebase {
namespace internal {
// Borrow Firebase's internal Base64 encoder and decoder.
Expand Down Expand Up @@ -293,6 +298,66 @@ bool FirebaseTest::Base64Decode(const std::string& input, std::string* output) {
return ::firebase::internal::Base64Decode(input, output);
}

int64_t FirebaseTest::GetCurrentTimeInSecondsSinceEpoch() {
#if defined(ANDROID) || (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
// Quick and dirty function to retrieve GMT time from worldtimeapi.org
// and parse the very simple JSON response to obtain the "unixtime" value.
// If any step fails, it will return the local time instead.
std::map<std::string, std::string> empty_headers;
int response_code = 0;
std::string response_body;
bool success =
SendHttpGetRequest("https://worldtimeapi.org/api/timezone/GMT",
empty_headers, &response_code, &response_body);
if (!success || response_code != 200 || response_body.empty()) {
LogDebug("GetCurrentTimeInSecondsSinceEpoch: HTTP request failed");
return (app_framework::GetCurrentTimeInMicroseconds() / 1000000LL);
}

const char kJsonTag[] = "\"unixtime\":";
size_t begin = response_body.find(kJsonTag);
if (begin < 0) {
LogDebug(
"GetCurrentTimeInSecondsSinceEpoch: Can't find unixtime JSON field in "
"response: %s",
response_body.c_str());
return (app_framework::GetCurrentTimeInMicroseconds() / 1000000LL);
}
begin += strlen(kJsonTag);
if (response_body[begin] == ' ') {
// Advance past a single space, if present.
begin++;
}

size_t end = response_body.find(",", begin);
if (end < 0) end = response_body.find("}", begin);
if (end < 0) {
LogDebug(
"GetCurrentTimeInSecondsSinceEpoch: Can't extract unixtime JSON field "
"from response: %s",
response_body.c_str());
return (app_framework::GetCurrentTimeInMicroseconds() / 1000000LL);
}
std::string time_str = response_body.substr(begin, end - begin);
int64_t timestamp = std::stoll(time_str);
if (timestamp <= 0) {
LogDebug(
"GetCurrentTimeInSecondsSinceEpoch: Can't parse unixtime JSON value %s",
time_str.c_str());
return (app_framework::GetCurrentTimeInMicroseconds() / 1000000LL);
}
LogDebug("Got remote timestamp: %" PRId64, timestamp);
return timestamp;
#else
// On desktop, just return the local time since SendHttpGetRequest is not
// implemented.
int64_t time_in_microseconds =
app_framework::GetCurrentTimeInMicroseconds() / 1000000LL;
LogDebug("Got local time: %" PRId64, time_in_microseconds);
return (time_in_microseconds);
#endif
}

class LogTestEventListener : public testing::EmptyTestEventListener {
public:
void OnTestPartResult(
Expand Down
5 changes: 5 additions & 0 deletions testing/test_framework/src/firebase_test_framework.h
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,11 @@ class FirebaseTest : public testing::Test {

static std::string GetDebugDeviceId();

// Get the current time in seconds since epoch. On desktop, this will use the
// local machine's time. On mobile, it will retrieve UTC time from a server if
// possible, otherwise use the local machine's time.
int64_t GetCurrentTimeInSecondsSinceEpoch();

firebase::App* app_;
static int argc_;
static char** argv_;
Expand Down

0 comments on commit a011584

Please sign in to comment.