FSTIntegrationTestCase.mm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  17. #import <FirebaseCore/FIRAppInternal.h>
  18. #import <FirebaseCore/FIRLogger.h>
  19. #import <FirebaseCore/FIROptions.h>
  20. #import <FirebaseFirestore/FIRCollectionReference.h>
  21. #import <FirebaseFirestore/FIRDocumentChange.h>
  22. #import <FirebaseFirestore/FIRDocumentReference.h>
  23. #import <FirebaseFirestore/FIRDocumentSnapshot.h>
  24. #import <FirebaseFirestore/FIRFirestore.h>
  25. #import <FirebaseFirestore/FIRFirestoreSettings.h>
  26. #import <FirebaseFirestore/FIRQuerySnapshot.h>
  27. #import <FirebaseFirestore/FIRSnapshotMetadata.h>
  28. #import <FirebaseFirestore/FIRTransaction.h>
  29. #include <memory>
  30. #include <string>
  31. #include <utility>
  32. #include "Firestore/core/src/firebase/firestore/auth/credentials_provider.h"
  33. #include "Firestore/core/src/firebase/firestore/auth/empty_credentials_provider.h"
  34. #include "Firestore/core/src/firebase/firestore/auth/user.h"
  35. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  36. #include "Firestore/core/src/firebase/firestore/remote/grpc_connection.h"
  37. #include "Firestore/core/src/firebase/firestore/util/autoid.h"
  38. #include "Firestore/core/src/firebase/firestore/util/filesystem.h"
  39. #include "Firestore/core/src/firebase/firestore/util/path.h"
  40. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  41. #include "Firestore/core/test/firebase/firestore/testutil/app_testing.h"
  42. #include "Firestore/core/test/firebase/firestore/util/status_testing.h"
  43. #include "absl/memory/memory.h"
  44. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  45. #import "Firestore/Source/Local/FSTLevelDB.h"
  46. #import "Firestore/Example/Tests/Util/FIRFirestore+Testing.h"
  47. #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
  48. #include "Firestore/core/src/firebase/firestore/util/async_queue.h"
  49. #include "Firestore/core/src/firebase/firestore/util/executor_libdispatch.h"
  50. namespace util = firebase::firestore::util;
  51. using firebase::firestore::auth::CredentialChangeListener;
  52. using firebase::firestore::auth::CredentialsProvider;
  53. using firebase::firestore::auth::EmptyCredentialsProvider;
  54. using firebase::firestore::auth::User;
  55. using firebase::firestore::model::DatabaseId;
  56. using firebase::firestore::testutil::AppForUnitTesting;
  57. using firebase::firestore::remote::GrpcConnection;
  58. using firebase::firestore::util::AsyncQueue;
  59. using firebase::firestore::util::CreateAutoId;
  60. using firebase::firestore::util::Path;
  61. using firebase::firestore::util::Status;
  62. using firebase::firestore::util::ExecutorLibdispatch;
  63. NS_ASSUME_NONNULL_BEGIN
  64. /**
  65. * Firestore databases can be subject to a ~30s "cold start" delay if they have not been used
  66. * recently, so before any tests run we "prime" the backend.
  67. */
  68. static const double kPrimingTimeout = 45.0;
  69. static NSString *defaultProjectId;
  70. static FIRFirestoreSettings *defaultSettings;
  71. static bool runningAgainstEmulator = false;
  72. // Behaves the same as `EmptyCredentialsProvider` except it can also trigger a user
  73. // change.
  74. class FakeCredentialsProvider : public EmptyCredentialsProvider {
  75. public:
  76. void SetCredentialChangeListener(CredentialChangeListener changeListener) override {
  77. if (changeListener) {
  78. listener_ = std::move(changeListener);
  79. listener_(User::Unauthenticated());
  80. }
  81. }
  82. void ChangeUser(NSString *new_id) {
  83. if (listener_) {
  84. listener_(firebase::firestore::auth::User::FromUid(new_id));
  85. }
  86. }
  87. private:
  88. CredentialChangeListener listener_;
  89. };
  90. @implementation FSTIntegrationTestCase {
  91. NSMutableArray<FIRFirestore *> *_firestores;
  92. std::shared_ptr<FakeCredentialsProvider> _fakeCredentialsProvider;
  93. }
  94. - (void)setUp {
  95. [super setUp];
  96. _fakeCredentialsProvider = std::make_shared<FakeCredentialsProvider>();
  97. [self clearPersistenceOnce];
  98. [self primeBackend];
  99. _firestores = [NSMutableArray array];
  100. self.db = [self firestore];
  101. self.eventAccumulator = [FSTEventAccumulator accumulatorForTest:self];
  102. }
  103. - (void)tearDown {
  104. @try {
  105. for (FIRFirestore *firestore in _firestores) {
  106. [self terminateFirestore:firestore];
  107. }
  108. } @finally {
  109. _firestores = nil;
  110. [super tearDown];
  111. }
  112. }
  113. /**
  114. * Clears persistence, but only the first time. This ensures that each test
  115. * run is isolated from the last test run, but doesn't allow tests to interfere
  116. * with each other.
  117. */
  118. - (void)clearPersistenceOnce {
  119. static bool clearedPersistence = false;
  120. @synchronized([FSTIntegrationTestCase class]) {
  121. if (clearedPersistence) return;
  122. Path levelDBDir = [FSTLevelDB documentsDirectory];
  123. Status status = util::RecursivelyDelete(levelDBDir);
  124. ASSERT_OK(status);
  125. clearedPersistence = true;
  126. }
  127. }
  128. - (FIRFirestore *)firestore {
  129. return [self firestoreWithProjectID:[FSTIntegrationTestCase projectID]];
  130. }
  131. /**
  132. * Figures out what kind of testing environment we're using, and sets up testing defaults to make
  133. * that work.
  134. *
  135. * Several configurations are supported:
  136. * * Mobile Harness, running periocally against prod and nightly, using live SSL certs
  137. * * Hexa built from google3, running on a companion gLinux machine, using self-signed test SSL
  138. * certs
  139. * * Firestore emulator, running on localhost, with SSL disabled
  140. *
  141. * See Firestore/README.md for detailed setup instructions or comments below for which specific
  142. * values trigger which configurations.
  143. */
  144. + (void)setUpDefaults {
  145. defaultSettings = [[FIRFirestoreSettings alloc] init];
  146. defaultSettings.persistenceEnabled = YES;
  147. // Check for a MobileHarness configuration, running against nightly or prod, which have live
  148. // SSL certs.
  149. NSString *project = [[NSProcessInfo processInfo] environment][@"PROJECT_ID"];
  150. NSString *host = [[NSProcessInfo processInfo] environment][@"DATASTORE_HOST"];
  151. if (project && host) {
  152. defaultProjectId = project;
  153. defaultSettings.host = host;
  154. return;
  155. }
  156. // Check for configuration of a prod project via GoogleServices-Info.plist.
  157. FIROptions *options = [FIROptions defaultOptions];
  158. if (options && ![options.projectID isEqualToString:@"abc-xyz-123"]) {
  159. defaultProjectId = options.projectID;
  160. if (host) {
  161. // Allow access to nightly or other hosts via this mechanism too.
  162. defaultSettings.host = host;
  163. }
  164. return;
  165. }
  166. // Otherwise fall back on assuming the emulator or Hexa on localhost.
  167. defaultProjectId = @"test-db";
  168. // Hexa uses a self-signed cert: the first bundle location is used by bazel builds. The second is
  169. // used for github clones.
  170. NSString *certsPath =
  171. [[NSBundle mainBundle] pathForResource:@"PlugIns/IntegrationTests.xctest/CAcert"
  172. ofType:@"pem"];
  173. if (certsPath == nil) {
  174. certsPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"CAcert" ofType:@"pem"];
  175. }
  176. unsigned long long fileSize =
  177. [[[NSFileManager defaultManager] attributesOfItemAtPath:certsPath error:nil] fileSize];
  178. if (fileSize != 0) {
  179. defaultSettings.host = @"localhost:8081";
  180. GrpcConnection::UseTestCertificate(util::MakeString(defaultSettings.host),
  181. Path::FromNSString(certsPath), "test_cert_2");
  182. } else {
  183. // If no cert is set up, configure for the Firestore emulator.
  184. defaultSettings.host = @"localhost:8080";
  185. defaultSettings.sslEnabled = false;
  186. runningAgainstEmulator = true;
  187. // Also issue a warning because the Firestore emulator doesn't completely work yet.
  188. NSLog(@"Please set up a GoogleServices-Info.plist for Firestore in Firestore/Example/App using "
  189. "instructions at <https://github.com/firebase/firebase-ios-sdk#running-sample-apps>. "
  190. "Alternatively, if you're a Googler with a Hexa preproduction environment, run "
  191. "setup_integration_tests.py to properly configure testing SSL certificates.");
  192. }
  193. }
  194. + (NSString *)projectID {
  195. if (!defaultProjectId) {
  196. [self setUpDefaults];
  197. }
  198. return defaultProjectId;
  199. }
  200. + (bool)isRunningAgainstEmulator {
  201. // The only way to determine whether or not we're running against the emulator is to figure out
  202. // which testing environment we're using. Essentially `setUpDefaults` determines
  203. // `runningAgainstEmulator` as a side effect.
  204. if (!defaultProjectId) {
  205. [self setUpDefaults];
  206. }
  207. return runningAgainstEmulator;
  208. }
  209. + (FIRFirestoreSettings *)settings {
  210. if (!defaultSettings) {
  211. [self setUpDefaults];
  212. }
  213. return defaultSettings;
  214. }
  215. - (FIRFirestore *)firestoreWithProjectID:(NSString *)projectID {
  216. FIRApp *app = AppForUnitTesting(util::MakeString(projectID));
  217. return [self firestoreWithApp:app];
  218. }
  219. - (FIRFirestore *)firestoreWithApp:(FIRApp *)app {
  220. NSString *persistenceKey = [NSString stringWithFormat:@"db%lu", (unsigned long)_firestores.count];
  221. dispatch_queue_t queue =
  222. dispatch_queue_create("com.google.firebase.firestore", DISPATCH_QUEUE_SERIAL);
  223. std::unique_ptr<AsyncQueue> workerQueue =
  224. absl::make_unique<AsyncQueue>(absl::make_unique<ExecutorLibdispatch>(queue));
  225. FIRSetLoggerLevel(FIRLoggerLevelDebug);
  226. std::string projectID = util::MakeString(app.options.projectID);
  227. FIRFirestore *firestore =
  228. [[FIRFirestore alloc] initWithDatabaseID:DatabaseId(projectID)
  229. persistenceKey:util::MakeString(persistenceKey)
  230. credentialsProvider:_fakeCredentialsProvider
  231. workerQueue:std::move(workerQueue)
  232. firebaseApp:app
  233. instanceRegistry:nil];
  234. firestore.settings = [FSTIntegrationTestCase settings];
  235. [_firestores addObject:firestore];
  236. return firestore;
  237. }
  238. - (void)triggerUserChangeWithUid:(NSString *)uid {
  239. _fakeCredentialsProvider->ChangeUser(uid);
  240. }
  241. - (void)primeBackend {
  242. static dispatch_once_t onceToken;
  243. dispatch_once(&onceToken, ^{
  244. FIRFirestore *db = [self firestore];
  245. XCTestExpectation *watchInitialized =
  246. [self expectationWithDescription:@"Prime backend: Watch initialized"];
  247. __block XCTestExpectation *watchUpdateReceived;
  248. FIRDocumentReference *docRef = [db documentWithPath:[self documentPath]];
  249. id<FIRListenerRegistration> listenerRegistration =
  250. [docRef addSnapshotListener:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  251. if ([snapshot[@"value"] isEqual:@"done"]) {
  252. [watchUpdateReceived fulfill];
  253. } else {
  254. [watchInitialized fulfill];
  255. }
  256. }];
  257. // Wait for watch to initialize and deliver first event.
  258. [self awaitExpectations];
  259. watchUpdateReceived = [self expectationWithDescription:@"Prime backend: Watch update received"];
  260. // Use a transaction to perform a write without triggering any local events.
  261. [docRef.firestore
  262. runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **pError) {
  263. [transaction setData:@{@"value" : @"done"} forDocument:docRef];
  264. return nil;
  265. }
  266. completion:^(id result, NSError *error){
  267. }];
  268. // Wait to see the write on the watch stream.
  269. [self waitForExpectationsWithTimeout:kPrimingTimeout
  270. handler:^(NSError *_Nullable expectationError) {
  271. if (expectationError) {
  272. XCTFail(@"Error waiting for prime backend: %@",
  273. expectationError);
  274. }
  275. }];
  276. [listenerRegistration remove];
  277. [self terminateFirestore:db];
  278. });
  279. }
  280. - (void)terminateFirestore:(FIRFirestore *)firestore {
  281. [firestore terminateWithCompletion:[self completionForExpectationWithName:@"shutdown"]];
  282. [self awaitExpectations];
  283. }
  284. - (void)deleteApp:(FIRApp *)app {
  285. XCTestExpectation *expectation = [self expectationWithDescription:@"Delete app"];
  286. [app deleteApp:^(BOOL completion) {
  287. XCTAssertTrue(completion);
  288. [expectation fulfill];
  289. }];
  290. [self awaitExpectations];
  291. }
  292. - (NSString *)documentPath {
  293. std::string autoId = CreateAutoId();
  294. return [NSString stringWithFormat:@"test-collection/%s", autoId.c_str()];
  295. }
  296. - (FIRDocumentReference *)documentRef {
  297. return [self.db documentWithPath:[self documentPath]];
  298. }
  299. - (FIRCollectionReference *)collectionRef {
  300. std::string autoId = CreateAutoId();
  301. NSString *collectionName = [NSString stringWithFormat:@"test-collection-%s", autoId.c_str()];
  302. return [self.db collectionWithPath:collectionName];
  303. }
  304. - (FIRCollectionReference *)collectionRefWithDocuments:
  305. (NSDictionary<NSString *, NSDictionary<NSString *, id> *> *)documents {
  306. FIRCollectionReference *collection = [self collectionRef];
  307. // Use a different instance to write the documents
  308. [self writeAllDocuments:documents
  309. toCollection:[[self firestore] collectionWithPath:collection.path]];
  310. return collection;
  311. }
  312. - (void)writeAllDocuments:(NSDictionary<NSString *, NSDictionary<NSString *, id> *> *)documents
  313. toCollection:(FIRCollectionReference *)collection {
  314. [documents enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary<NSString *, id> *value,
  315. BOOL *stop) {
  316. FIRDocumentReference *ref = [collection documentWithPath:key];
  317. [self writeDocumentRef:ref data:value];
  318. }];
  319. }
  320. - (void)readerAndWriterOnDocumentRef:(void (^)(NSString *path,
  321. FIRDocumentReference *readerRef,
  322. FIRDocumentReference *writerRef))action {
  323. FIRFirestore *reader = self.db; // for clarity
  324. FIRFirestore *writer = [self firestore];
  325. NSString *path = [self documentPath];
  326. FIRDocumentReference *readerRef = [reader documentWithPath:path];
  327. FIRDocumentReference *writerRef = [writer documentWithPath:path];
  328. action(path, readerRef, writerRef);
  329. }
  330. - (FIRDocumentSnapshot *)readDocumentForRef:(FIRDocumentReference *)ref {
  331. return [self readDocumentForRef:ref source:FIRFirestoreSourceDefault];
  332. }
  333. - (FIRDocumentSnapshot *)readDocumentForRef:(FIRDocumentReference *)ref
  334. source:(FIRFirestoreSource)source {
  335. __block FIRDocumentSnapshot *result;
  336. XCTestExpectation *expectation = [self expectationWithDescription:@"getData"];
  337. [ref getDocumentWithSource:source
  338. completion:^(FIRDocumentSnapshot *doc, NSError *_Nullable error) {
  339. XCTAssertNil(error);
  340. result = doc;
  341. [expectation fulfill];
  342. }];
  343. [self awaitExpectations];
  344. return result;
  345. }
  346. - (FIRQuerySnapshot *)readDocumentSetForRef:(FIRQuery *)query {
  347. return [self readDocumentSetForRef:query source:FIRFirestoreSourceDefault];
  348. }
  349. - (FIRQuerySnapshot *)readDocumentSetForRef:(FIRQuery *)query source:(FIRFirestoreSource)source {
  350. __block FIRQuerySnapshot *result;
  351. XCTestExpectation *expectation = [self expectationWithDescription:@"getData"];
  352. [query getDocumentsWithSource:source
  353. completion:^(FIRQuerySnapshot *documentSet, NSError *error) {
  354. XCTAssertNil(error);
  355. result = documentSet;
  356. [expectation fulfill];
  357. }];
  358. [self awaitExpectations];
  359. return result;
  360. }
  361. - (FIRDocumentSnapshot *)readSnapshotForRef:(FIRDocumentReference *)ref
  362. requireOnline:(BOOL)requireOnline {
  363. __block FIRDocumentSnapshot *result;
  364. XCTestExpectation *expectation = [self expectationWithDescription:@"listener"];
  365. id<FIRListenerRegistration> listener = [ref
  366. addSnapshotListenerWithIncludeMetadataChanges:YES
  367. listener:^(FIRDocumentSnapshot *snapshot,
  368. NSError *error) {
  369. XCTAssertNil(error);
  370. if (!requireOnline || !snapshot.metadata.fromCache) {
  371. result = snapshot;
  372. [expectation fulfill];
  373. }
  374. }];
  375. [self awaitExpectations];
  376. [listener remove];
  377. return result;
  378. }
  379. - (void)writeDocumentRef:(FIRDocumentReference *)ref data:(NSDictionary<NSString *, id> *)data {
  380. [ref setData:data completion:[self completionForExpectationWithName:@"setData"]];
  381. [self awaitExpectations];
  382. }
  383. - (void)updateDocumentRef:(FIRDocumentReference *)ref data:(NSDictionary<id, id> *)data {
  384. [ref updateData:data completion:[self completionForExpectationWithName:@"updateData"]];
  385. [self awaitExpectations];
  386. }
  387. - (void)deleteDocumentRef:(FIRDocumentReference *)ref {
  388. [ref deleteDocumentWithCompletion:[self completionForExpectationWithName:@"deleteDocument"]];
  389. [self awaitExpectations];
  390. }
  391. - (void)mergeDocumentRef:(FIRDocumentReference *)ref data:(NSDictionary<NSString *, id> *)data {
  392. [ref setData:data
  393. merge:YES
  394. completion:[self completionForExpectationWithName:@"setDataWithMerge"]];
  395. [self awaitExpectations];
  396. }
  397. - (void)mergeDocumentRef:(FIRDocumentReference *)ref
  398. data:(NSDictionary<NSString *, id> *)data
  399. fields:(NSArray<id> *)fields {
  400. [ref setData:data
  401. mergeFields:fields
  402. completion:[self completionForExpectationWithName:@"setDataWithMerge"]];
  403. [self awaitExpectations];
  404. }
  405. - (void)disableNetwork {
  406. [self.db
  407. disableNetworkWithCompletion:[self completionForExpectationWithName:@"Disable Network."]];
  408. [self awaitExpectations];
  409. }
  410. - (void)enableNetwork {
  411. [self.db enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable Network."]];
  412. [self awaitExpectations];
  413. }
  414. - (const std::shared_ptr<util::AsyncQueue> &)queueForFirestore:(FIRFirestore *)firestore {
  415. return [firestore workerQueue];
  416. }
  417. - (void)waitUntil:(BOOL (^)())predicate {
  418. NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
  419. double waitSeconds = [self defaultExpectationWaitSeconds];
  420. while (!predicate() && ([NSDate timeIntervalSinceReferenceDate] - start < waitSeconds)) {
  421. // This waits for the next event or until the 100ms timeout is reached
  422. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  423. beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
  424. }
  425. if (!predicate()) {
  426. XCTFail(@"Timeout");
  427. }
  428. }
  429. extern "C" NSArray<NSDictionary<NSString *, id> *> *FIRQuerySnapshotGetData(
  430. FIRQuerySnapshot *docs) {
  431. NSMutableArray<NSDictionary<NSString *, id> *> *result = [NSMutableArray array];
  432. for (FIRDocumentSnapshot *doc in docs.documents) {
  433. [result addObject:doc.data];
  434. }
  435. return result;
  436. }
  437. extern "C" NSArray<NSString *> *FIRQuerySnapshotGetIDs(FIRQuerySnapshot *docs) {
  438. NSMutableArray<NSString *> *result = [NSMutableArray array];
  439. for (FIRDocumentSnapshot *doc in docs.documents) {
  440. [result addObject:doc.documentID];
  441. }
  442. return result;
  443. }
  444. extern "C" NSArray<NSArray<id> *> *FIRQuerySnapshotGetDocChangesData(FIRQuerySnapshot *docs) {
  445. NSMutableArray<NSMutableArray<id> *> *result = [NSMutableArray array];
  446. for (FIRDocumentChange *docChange in docs.documentChanges) {
  447. NSMutableArray<id> *docChangeData = [NSMutableArray array];
  448. [docChangeData addObject:@(docChange.type)];
  449. [docChangeData addObject:docChange.document.documentID];
  450. [docChangeData addObject:docChange.document.data];
  451. [result addObject:docChangeData];
  452. }
  453. return result;
  454. }
  455. @end
  456. NS_ASSUME_NONNULL_END