FSTIntegrationTestCase.mm 20 KB

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