Skip to content

Commit

Permalink
replacing Auth by C++ auth implementation (#802)
Browse files Browse the repository at this point in the history
* lazy replacing FST(Firebase)CredentialsProvider by (Firebase)CredentialsProvider

* lazy replacing FSTUser by User

* adding error-code parameter to TokenListener

* actually use const user& instead of pointer; also add an error util

* add HashUser and pass into the unordered_map

* use User in test

* use c++ CredentialsProvider and subclass in test

* fix unit test

* use explicit capture in lambda instead of capture all by reference

* cache currentUser explicitly when reset sync engineer test driver

* objc object should be captured by value in lambda

* replacing Auth/FSTUser by C++ auth implementation

* address changes

* replacing FSTGetTokenResult by C++ Token implementation

* address changes

* fix unintentional change in merging

* patch the change in objc Auth up-stream

* somehow, the lambda-version of set-user-change-listener does not work... fallback to block

* address changes

* fix another const& v.s. dispatch bug

* fix more const& v.s. dispatch bug  zxu123 committed

* fix a bad sync line

* address changes

* address change

* address change

* fix upstream change from merge

* fix upstream changes

* Suggested fixes for cpp/port_auth (#846)

* Get rid of MockDatastore factory

This avoids the need to statically allocate (and leak) a credentials
provider

* Use absl::make_unique

std::make_unique technically does not exist until C++14.

* #include <utility> for std::move

* Use std::future for the initial user

* fix style
  • Loading branch information
zxu123 committed Feb 27, 2018
1 parent 13aeb61 commit 3e7c062
Show file tree
Hide file tree
Showing 26 changed files with 360 additions and 470 deletions.
9 changes: 4 additions & 5 deletions Firestore/Example/Tests/Integration/FSTDatastoreTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

#import "Firestore/Source/API/FIRDocumentReference+Internal.h"
#import "Firestore/Source/API/FSTUserDataConverter.h"
#import "Firestore/Source/Auth/FSTEmptyCredentialsProvider.h"
#import "Firestore/Source/Core/FSTFirestoreClient.h"
#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Core/FSTSnapshotVersion.h"
Expand All @@ -41,11 +40,13 @@

#import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"

#include "Firestore/core/src/firebase/firestore/auth/empty_credentials_provider.h"
#include "Firestore/core/src/firebase/firestore/core/database_info.h"
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"

namespace util = firebase::firestore::util;
using firebase::firestore::auth::EmptyCredentialsProvider;
using firebase::firestore::core::DatabaseInfo;
using firebase::firestore::model::DatabaseId;

Expand Down Expand Up @@ -141,7 +142,7 @@ @interface FSTDatastoreTests : XCTestCase
@implementation FSTDatastoreTests {
FSTDispatchQueue *_testWorkerQueue;
FSTLocalStore *_localStore;
id<FSTCredentialsProvider> _credentials;
EmptyCredentialsProvider _credentials;

DatabaseInfo _databaseInfo;
FSTDatastore *_datastore;
Expand Down Expand Up @@ -170,11 +171,9 @@ - (void)setUp {
queueWith:dispatch_queue_create("com.google.firestore.FSTDatastoreTestsWorkerQueue",
DISPATCH_QUEUE_SERIAL)];

_credentials = [[FSTEmptyCredentialsProvider alloc] init];

_datastore = [FSTDatastore datastoreWithDatabase:&_databaseInfo
workerDispatchQueue:_testWorkerQueue
credentials:_credentials];
credentials:&_credentials];

_remoteStore = [FSTRemoteStore remoteStoreWithLocalStore:_localStore datastore:_datastore];

Expand Down
10 changes: 5 additions & 5 deletions Firestore/Example/Tests/Integration/FSTStreamTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@

#import "Firestore/Example/Tests/Util/FSTHelpers.h"
#import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
#import "Firestore/Source/Auth/FSTEmptyCredentialsProvider.h"
#import "Firestore/Source/Remote/FSTDatastore.h"
#import "Firestore/Source/Remote/FSTStream.h"
#import "Firestore/Source/Util/FSTAssert.h"

#include "Firestore/core/src/firebase/firestore/auth/empty_credentials_provider.h"
#include "Firestore/core/src/firebase/firestore/core/database_info.h"
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"

namespace util = firebase::firestore::util;
using firebase::firestore::auth::EmptyCredentialsProvider;
using firebase::firestore::core::DatabaseInfo;
using firebase::firestore::model::DatabaseId;

Expand Down Expand Up @@ -136,7 +137,7 @@ @implementation FSTStreamTests {
dispatch_queue_t _testQueue;
FSTDispatchQueue *_workerDispatchQueue;
DatabaseInfo _databaseInfo;
FSTEmptyCredentialsProvider *_credentials;
EmptyCredentialsProvider _credentials;
FSTStreamStatusDelegate *_delegate;

/** Single mutation to send to the write stream. */
Expand All @@ -155,7 +156,6 @@ DatabaseId database_id(util::MakeStringView([FSTIntegrationTestCase projectID]),

_databaseInfo = DatabaseInfo(database_id, "test-key", util::MakeStringView(settings.host),
settings.sslEnabled);
_credentials = [[FSTEmptyCredentialsProvider alloc] init];

_delegate = [[FSTStreamStatusDelegate alloc] initWithTestCase:self queue:_workerDispatchQueue];

Expand All @@ -165,14 +165,14 @@ DatabaseId database_id(util::MakeStringView([FSTIntegrationTestCase projectID]),
- (FSTWriteStream *)setUpWriteStream {
FSTDatastore *datastore = [[FSTDatastore alloc] initWithDatabaseInfo:&_databaseInfo
workerDispatchQueue:_workerDispatchQueue
credentials:_credentials];
credentials:&_credentials];
return [datastore createWriteStream];
}

- (FSTWatchStream *)setUpWatchStream {
FSTDatastore *datastore = [[FSTDatastore alloc] initWithDatabaseInfo:&_databaseInfo
workerDispatchQueue:_workerDispatchQueue
credentials:_credentials];
credentials:&_credentials];
return [datastore createWatchStream];
}

Expand Down
2 changes: 0 additions & 2 deletions Firestore/Example/Tests/SpecTests/FSTMockDatastore.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property(nonatomic) int writeStreamRequestCount;

+ (instancetype)mockDatastoreWithWorkerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue;

#pragma mark - Watch Stream manipulation.

/** Injects an Added WatchChange containing the given targetIDs. */
Expand Down
35 changes: 13 additions & 22 deletions Firestore/Example/Tests/SpecTests/FSTMockDatastore.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#import "Firestore/Example/Tests/SpecTests/FSTMockDatastore.h"

#import "Firestore/Source/Auth/FSTEmptyCredentialsProvider.h"
#import "Firestore/Source/Core/FSTSnapshotVersion.h"
#import "Firestore/Source/Local/FSTQueryData.h"
#import "Firestore/Source/Model/FSTMutation.h"
Expand All @@ -27,10 +26,14 @@

#import "Firestore/Example/Tests/Remote/FSTWatchChange+Testing.h"

#include "Firestore/core/src/firebase/firestore/auth/credentials_provider.h"
#include "Firestore/core/src/firebase/firestore/auth/empty_credentials_provider.h"
#include "Firestore/core/src/firebase/firestore/core/database_info.h"
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"

using firebase::firestore::auth::CredentialsProvider;
using firebase::firestore::auth::EmptyCredentialsProvider;
using firebase::firestore::core::DatabaseInfo;
using firebase::firestore::model::DatabaseId;

Expand All @@ -44,17 +47,17 @@ @interface FSTMockWatchStream : FSTWatchStream

- (instancetype)initWithDatastore:(FSTMockDatastore *)datastore
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
credentials:(id<FSTCredentialsProvider>)credentials
credentials:(CredentialsProvider *)credentials
serializer:(FSTSerializerBeta *)serializer NS_DESIGNATED_INITIALIZER;

- (instancetype)initWithDatabase:(const DatabaseInfo *)database
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
credentials:(id<FSTCredentialsProvider>)credentials
credentials:(CredentialsProvider *)credentials
serializer:(FSTSerializerBeta *)serializer NS_UNAVAILABLE;

- (instancetype)initWithDatabase:(const DatabaseInfo *)database
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
credentials:(id<FSTCredentialsProvider>)credentials
credentials:(CredentialsProvider *)credentials
responseMessageClass:(Class)responseMessageClass NS_UNAVAILABLE;

@property(nonatomic, assign) BOOL open;
Expand All @@ -69,7 +72,7 @@ @implementation FSTMockWatchStream

- (instancetype)initWithDatastore:(FSTMockDatastore *)datastore
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
credentials:(id<FSTCredentialsProvider>)credentials
credentials:(CredentialsProvider *)credentials
serializer:(FSTSerializerBeta *)serializer {
self = [super initWithDatabase:datastore.databaseInfo
workerDispatchQueue:workerDispatchQueue
Expand Down Expand Up @@ -170,17 +173,17 @@ @interface FSTMockWriteStream : FSTWriteStream

- (instancetype)initWithDatastore:(FSTMockDatastore *)datastore
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
credentials:(id<FSTCredentialsProvider>)credentials
credentials:(CredentialsProvider *)credentials
serializer:(FSTSerializerBeta *)serializer NS_DESIGNATED_INITIALIZER;

- (instancetype)initWithDatabase:(const DatabaseInfo *)database
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
credentials:(id<FSTCredentialsProvider>)credentials
credentials:(CredentialsProvider *)credentials
serializer:(FSTSerializerBeta *)serializer NS_UNAVAILABLE;

- (instancetype)initWithDatabase:(const DatabaseInfo *)database
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
credentials:(id<FSTCredentialsProvider>)credentials
credentials:(CredentialsProvider *)credentials
responseMessageClass:(Class)responseMessageClass NS_UNAVAILABLE;

@property(nonatomic, strong, readonly) FSTMockDatastore *datastore;
Expand All @@ -193,7 +196,7 @@ @implementation FSTMockWriteStream

- (instancetype)initWithDatastore:(FSTMockDatastore *)datastore
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
credentials:(id<FSTCredentialsProvider>)credentials
credentials:(CredentialsProvider *)credentials
serializer:(FSTSerializerBeta *)serializer {
self = [super initWithDatabase:datastore.databaseInfo
workerDispatchQueue:workerDispatchQueue
Expand Down Expand Up @@ -279,24 +282,12 @@ @interface FSTMockDatastore ()

/** Properties implemented in FSTDatastore that are nonpublic. */
@property(nonatomic, strong, readonly) FSTDispatchQueue *workerDispatchQueue;
@property(nonatomic, strong, readonly) id<FSTCredentialsProvider> credentials;
@property(nonatomic, assign, readonly) CredentialsProvider *credentials;

@end

@implementation FSTMockDatastore

+ (instancetype)mockDatastoreWithWorkerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue {
// This owns the DatabaseInfos since we do not have FirestoreClient instance to own them.
static DatabaseInfo database_info{DatabaseId{"project", "database"}, "persistence", "host",
false};

FSTEmptyCredentialsProvider *credentials = [[FSTEmptyCredentialsProvider alloc] init];

return [[FSTMockDatastore alloc] initWithDatabaseInfo:&database_info
workerDispatchQueue:workerDispatchQueue
credentials:credentials];
}

#pragma mark - Overridden FSTDatastore methods.

- (FSTWatchStream *)createWatchStream {
Expand Down
14 changes: 13 additions & 1 deletion Firestore/Example/Tests/SpecTests/FSTSyncEngineTestDriver.mm
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@
#import "Firestore/Example/Tests/Core/FSTSyncEngine+Testing.h"
#import "Firestore/Example/Tests/SpecTests/FSTMockDatastore.h"

#include "Firestore/core/src/firebase/firestore/auth/empty_credentials_provider.h"
#include "Firestore/core/src/firebase/firestore/auth/user.h"
#include "Firestore/core/src/firebase/firestore/core/database_info.h"
#include "Firestore/core/src/firebase/firestore/model/database_id.h"

using firebase::firestore::auth::EmptyCredentialsProvider;
using firebase::firestore::auth::HashUser;
using firebase::firestore::auth::User;
using firebase::firestore::core::DatabaseInfo;
using firebase::firestore::model::DatabaseId;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -83,7 +89,9 @@ @implementation FSTSyncEngineTestDriver {
// ivar is declared as mutable.
std::unordered_map<User, NSMutableArray<FSTOutstandingWrite *> *, HashUser> _outstandingWrites;

DatabaseInfo _databaseInfo;
User _currentUser;
EmptyCredentialsProvider _credentialProvider;
}

- (instancetype)initWithPersistence:(id<FSTPersistence>)persistence
Expand All @@ -106,13 +114,17 @@ - (instancetype)initWithPersistence:(id<FSTPersistence>)persistence

_events = [NSMutableArray array];

_databaseInfo = {DatabaseId{"project", "database"}, "persistence", "host", false};

// Set up the sync engine and various stores.
dispatch_queue_t mainQueue = dispatch_get_main_queue();
FSTDispatchQueue *dispatchQueue = [FSTDispatchQueue queueWith:mainQueue];
_localStore = [[FSTLocalStore alloc] initWithPersistence:persistence
garbageCollector:garbageCollector
initialUser:initialUser];
_datastore = [FSTMockDatastore mockDatastoreWithWorkerDispatchQueue:dispatchQueue];
_datastore = [[FSTMockDatastore alloc] initWithDatabaseInfo:&_databaseInfo
workerDispatchQueue:dispatchQueue
credentials:&_credentialProvider];

_remoteStore = [FSTRemoteStore remoteStoreWithLocalStore:_localStore datastore:_datastore];

Expand Down
20 changes: 13 additions & 7 deletions Firestore/Example/Tests/Util/FSTIntegrationTestCase.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,30 @@

#import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"

#include <memory>
#include <utility>

#import <FirebaseCore/FIRLogger.h>
#import <FirebaseFirestore/FirebaseFirestore-umbrella.h>
#import <GRPCClient/GRPCCall+ChannelArg.h>
#import <GRPCClient/GRPCCall+Tests.h>

#include "Firestore/core/src/firebase/firestore/auth/empty_credentials_provider.h"
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
#include "Firestore/core/src/firebase/firestore/util/autoid.h"
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
#include "absl/memory/memory.h"

#import "Firestore/Source/API/FIRFirestore+Internal.h"
#import "Firestore/Source/Auth/FSTEmptyCredentialsProvider.h"
#import "Firestore/Source/Core/FSTFirestoreClient.h"
#import "Firestore/Source/Local/FSTLevelDB.h"
#import "Firestore/Source/Util/FSTDispatchQueue.h"

#import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"

#include "Firestore/core/src/firebase/firestore/model/database_id.h"
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"

namespace util = firebase::firestore::util;
using firebase::firestore::auth::CredentialsProvider;
using firebase::firestore::auth::EmptyCredentialsProvider;
using firebase::firestore::model::DatabaseId;
using firebase::firestore::util::CreateAutoId;

Expand Down Expand Up @@ -135,15 +140,16 @@ - (FIRFirestore *)firestoreWithProjectID:(NSString *)projectID {
FSTDispatchQueue *workerDispatchQueue = [FSTDispatchQueue
queueWith:dispatch_queue_create("com.google.firebase.firestore", DISPATCH_QUEUE_SERIAL)];

FSTEmptyCredentialsProvider *credentialsProvider = [[FSTEmptyCredentialsProvider alloc] init];

FIRSetLoggerLevel(FIRLoggerLevelDebug);
// HACK: FIRFirestore expects a non-nil app, but for tests we cheat.
FIRApp *app = nil;
std::unique_ptr<CredentialsProvider> credentials_provider =
absl::make_unique<firebase::firestore::auth::EmptyCredentialsProvider>();

FIRFirestore *firestore = [[FIRFirestore alloc] initWithProjectID:util::MakeStringView(projectID)
database:DatabaseId::kDefault
persistenceKey:persistenceKey
credentialsProvider:credentialsProvider
credentialsProvider:std::move(credentials_provider)
workerDispatchQueue:workerDispatchQueue
firebaseApp:app];

Expand Down
7 changes: 5 additions & 2 deletions Firestore/Source/API/FIRFirestore+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

#import "FIRFirestore.h"

#include <memory>

#include "Firestore/core/src/firebase/firestore/auth/credentials_provider.h"
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
#include "absl/strings/string_view.h"

Expand All @@ -24,7 +27,6 @@ NS_ASSUME_NONNULL_BEGIN
@class FSTDispatchQueue;
@class FSTFirestoreClient;
@class FSTUserDataConverter;
@protocol FSTCredentialsProvider;

@interface FIRFirestore (/* Init */)

Expand All @@ -35,7 +37,8 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithProjectID:(const absl::string_view)projectID
database:(const absl::string_view)database
persistenceKey:(NSString *)persistenceKey
credentialsProvider:(id<FSTCredentialsProvider>)credentialsProvider
credentialsProvider:(std::unique_ptr<firebase::firestore::auth::CredentialsProvider>)
credentialsProvider
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
firebaseApp:(FIRApp *)app;

Expand Down
Loading

0 comments on commit 3e7c062

Please sign in to comment.