FSTIntegrationTestCase.mm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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/FIRLogger.h>
  18. #import <FirebaseFirestore/FIRCollectionReference.h>
  19. #import <FirebaseFirestore/FIRDocumentChange.h>
  20. #import <FirebaseFirestore/FIRDocumentReference.h>
  21. #import <FirebaseFirestore/FIRDocumentSnapshot.h>
  22. #import <FirebaseFirestore/FIRFirestoreSettings.h>
  23. #import <FirebaseFirestore/FIRQuerySnapshot.h>
  24. #import <FirebaseFirestore/FIRSnapshotMetadata.h>
  25. #import <GRPCClient/GRPCCall+ChannelArg.h>
  26. #import <GRPCClient/GRPCCall+Tests.h>
  27. #include <memory>
  28. #include <string>
  29. #include <utility>
  30. #include "Firestore/core/src/firebase/firestore/auth/empty_credentials_provider.h"
  31. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  32. #include "Firestore/core/src/firebase/firestore/util/autoid.h"
  33. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  34. #include "absl/memory/memory.h"
  35. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  36. #import "Firestore/Source/Core/FSTFirestoreClient.h"
  37. #import "Firestore/Source/Local/FSTLevelDB.h"
  38. #import "Firestore/Source/Util/FSTDispatchQueue.h"
  39. #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
  40. namespace util = firebase::firestore::util;
  41. using firebase::firestore::auth::CredentialsProvider;
  42. using firebase::firestore::auth::EmptyCredentialsProvider;
  43. using firebase::firestore::model::DatabaseId;
  44. using firebase::firestore::util::CreateAutoId;
  45. NS_ASSUME_NONNULL_BEGIN
  46. @interface FIRFirestore (Testing)
  47. @property(nonatomic, strong) FSTDispatchQueue *workerDispatchQueue;
  48. @end
  49. @implementation FSTIntegrationTestCase {
  50. NSMutableArray<FIRFirestore *> *_firestores;
  51. }
  52. - (void)setUp {
  53. [super setUp];
  54. [self clearPersistence];
  55. _firestores = [NSMutableArray array];
  56. self.db = [self firestore];
  57. self.eventAccumulator = [FSTEventAccumulator accumulatorForTest:self];
  58. }
  59. - (void)tearDown {
  60. @try {
  61. for (FIRFirestore *firestore in _firestores) {
  62. [self shutdownFirestore:firestore];
  63. }
  64. } @finally {
  65. #pragma clang diagnostic push
  66. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  67. [GRPCCall closeOpenConnections];
  68. #pragma clang diagnostic pop
  69. _firestores = nil;
  70. [super tearDown];
  71. }
  72. }
  73. - (void)clearPersistence {
  74. NSString *levelDBDir = [FSTLevelDB documentsDirectory];
  75. NSError *error;
  76. if (![[NSFileManager defaultManager] removeItemAtPath:levelDBDir error:&error]) {
  77. // file not found is okay.
  78. XCTAssertTrue(
  79. [error.domain isEqualToString:NSCocoaErrorDomain] && error.code == NSFileNoSuchFileError,
  80. @"Failed to clear LevelDB Persistence: %@", error);
  81. }
  82. }
  83. - (FIRFirestore *)firestore {
  84. return [self firestoreWithProjectID:[FSTIntegrationTestCase projectID]];
  85. }
  86. + (NSString *)projectID {
  87. NSString *project = [[NSProcessInfo processInfo] environment][@"PROJECT_ID"];
  88. if (!project) {
  89. project = @"test-db";
  90. }
  91. return project;
  92. }
  93. + (FIRFirestoreSettings *)settings {
  94. FIRFirestoreSettings *settings = [[FIRFirestoreSettings alloc] init];
  95. NSString *host = [[NSProcessInfo processInfo] environment][@"DATASTORE_HOST"];
  96. settings.sslEnabled = YES;
  97. if (!host) {
  98. // If host is nil, there is no GoogleService-Info.plist. Check if a hexa integration test
  99. // configuration is configured. The first bundle location is used by bazel builds. The
  100. // second is used for github clones.
  101. host = @"localhost:8081";
  102. settings.sslEnabled = YES;
  103. NSString *certsPath =
  104. [[NSBundle mainBundle] pathForResource:@"PlugIns/IntegrationTests.xctest/CAcert"
  105. ofType:@"pem"];
  106. if (certsPath == nil) {
  107. certsPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"CAcert" ofType:@"pem"];
  108. }
  109. unsigned long long fileSize =
  110. [[[NSFileManager defaultManager] attributesOfItemAtPath:certsPath error:nil] fileSize];
  111. if (fileSize == 0) {
  112. NSLog(
  113. @"The cert is not properly configured. Make sure setup_integration_tests.py "
  114. "has been run.");
  115. }
  116. [GRPCCall useTestCertsPath:certsPath testName:@"test_cert_2" forHost:host];
  117. }
  118. settings.host = host;
  119. settings.persistenceEnabled = YES;
  120. settings.timestampsInSnapshotsEnabled = YES;
  121. NSLog(@"Configured integration test for %@ with SSL: %@", settings.host,
  122. settings.sslEnabled ? @"YES" : @"NO");
  123. return settings;
  124. }
  125. - (FIRFirestore *)firestoreWithProjectID:(NSString *)projectID {
  126. NSString *persistenceKey = [NSString stringWithFormat:@"db%lu", (unsigned long)_firestores.count];
  127. FSTDispatchQueue *workerDispatchQueue = [FSTDispatchQueue
  128. queueWith:dispatch_queue_create("com.google.firebase.firestore", DISPATCH_QUEUE_SERIAL)];
  129. FIRSetLoggerLevel(FIRLoggerLevelDebug);
  130. // HACK: FIRFirestore expects a non-nil app, but for tests we cheat.
  131. FIRApp *app = nil;
  132. std::unique_ptr<CredentialsProvider> credentials_provider =
  133. absl::make_unique<firebase::firestore::auth::EmptyCredentialsProvider>();
  134. FIRFirestore *firestore = [[FIRFirestore alloc] initWithProjectID:util::MakeStringView(projectID)
  135. database:DatabaseId::kDefault
  136. persistenceKey:persistenceKey
  137. credentialsProvider:std::move(credentials_provider)
  138. workerDispatchQueue:workerDispatchQueue
  139. firebaseApp:app];
  140. firestore.settings = [FSTIntegrationTestCase settings];
  141. [_firestores addObject:firestore];
  142. return firestore;
  143. }
  144. - (void)shutdownFirestore:(FIRFirestore *)firestore {
  145. [firestore shutdownWithCompletion:[self completionForExpectationWithName:@"shutdown"]];
  146. [self awaitExpectations];
  147. }
  148. - (NSString *)documentPath {
  149. std::string autoId = CreateAutoId();
  150. return [NSString stringWithFormat:@"test-collection/%s", autoId.c_str()];
  151. }
  152. - (FIRDocumentReference *)documentRef {
  153. return [self.db documentWithPath:[self documentPath]];
  154. }
  155. - (FIRCollectionReference *)collectionRef {
  156. std::string autoId = CreateAutoId();
  157. NSString *collectionName = [NSString stringWithFormat:@"test-collection-%s", autoId.c_str()];
  158. return [self.db collectionWithPath:collectionName];
  159. }
  160. - (FIRCollectionReference *)collectionRefWithDocuments:
  161. (NSDictionary<NSString *, NSDictionary<NSString *, id> *> *)documents {
  162. FIRCollectionReference *collection = [self collectionRef];
  163. // Use a different instance to write the documents
  164. [self writeAllDocuments:documents
  165. toCollection:[[self firestore] collectionWithPath:collection.path]];
  166. return collection;
  167. }
  168. - (void)writeAllDocuments:(NSDictionary<NSString *, NSDictionary<NSString *, id> *> *)documents
  169. toCollection:(FIRCollectionReference *)collection {
  170. [documents enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary<NSString *, id> *value,
  171. BOOL *stop) {
  172. FIRDocumentReference *ref = [collection documentWithPath:key];
  173. [self writeDocumentRef:ref data:value];
  174. }];
  175. }
  176. - (void)readerAndWriterOnDocumentRef:(void (^)(NSString *path,
  177. FIRDocumentReference *readerRef,
  178. FIRDocumentReference *writerRef))action {
  179. FIRFirestore *reader = self.db; // for clarity
  180. FIRFirestore *writer = [self firestore];
  181. NSString *path = [self documentPath];
  182. FIRDocumentReference *readerRef = [reader documentWithPath:path];
  183. FIRDocumentReference *writerRef = [writer documentWithPath:path];
  184. action(path, readerRef, writerRef);
  185. }
  186. - (FIRDocumentSnapshot *)readDocumentForRef:(FIRDocumentReference *)ref {
  187. return [self readDocumentForRef:ref source:FIRFirestoreSourceDefault];
  188. }
  189. - (FIRDocumentSnapshot *)readDocumentForRef:(FIRDocumentReference *)ref
  190. source:(FIRFirestoreSource)source {
  191. __block FIRDocumentSnapshot *result;
  192. XCTestExpectation *expectation = [self expectationWithDescription:@"getData"];
  193. [ref getDocumentWithSource:source
  194. completion:^(FIRDocumentSnapshot *doc, NSError *_Nullable error) {
  195. XCTAssertNil(error);
  196. result = doc;
  197. [expectation fulfill];
  198. }];
  199. [self awaitExpectations];
  200. return result;
  201. }
  202. - (FIRQuerySnapshot *)readDocumentSetForRef:(FIRQuery *)query {
  203. return [self readDocumentSetForRef:query source:FIRFirestoreSourceDefault];
  204. }
  205. - (FIRQuerySnapshot *)readDocumentSetForRef:(FIRQuery *)query source:(FIRFirestoreSource)source {
  206. __block FIRQuerySnapshot *result;
  207. XCTestExpectation *expectation = [self expectationWithDescription:@"getData"];
  208. [query getDocumentsWithSource:source
  209. completion:^(FIRQuerySnapshot *documentSet, NSError *error) {
  210. XCTAssertNil(error);
  211. result = documentSet;
  212. [expectation fulfill];
  213. }];
  214. [self awaitExpectations];
  215. return result;
  216. }
  217. - (FIRDocumentSnapshot *)readSnapshotForRef:(FIRDocumentReference *)ref
  218. requireOnline:(BOOL)requireOnline {
  219. __block FIRDocumentSnapshot *result;
  220. XCTestExpectation *expectation = [self expectationWithDescription:@"listener"];
  221. id<FIRListenerRegistration> listener = [ref
  222. addSnapshotListenerWithIncludeMetadataChanges:YES
  223. listener:^(FIRDocumentSnapshot *snapshot,
  224. NSError *error) {
  225. XCTAssertNil(error);
  226. if (!requireOnline || !snapshot.metadata.fromCache) {
  227. result = snapshot;
  228. [expectation fulfill];
  229. }
  230. }];
  231. [self awaitExpectations];
  232. [listener remove];
  233. return result;
  234. }
  235. - (void)writeDocumentRef:(FIRDocumentReference *)ref data:(NSDictionary<NSString *, id> *)data {
  236. [ref setData:data completion:[self completionForExpectationWithName:@"setData"]];
  237. [self awaitExpectations];
  238. }
  239. - (void)updateDocumentRef:(FIRDocumentReference *)ref data:(NSDictionary<id, id> *)data {
  240. [ref updateData:data completion:[self completionForExpectationWithName:@"updateData"]];
  241. [self awaitExpectations];
  242. }
  243. - (void)deleteDocumentRef:(FIRDocumentReference *)ref {
  244. [ref deleteDocumentWithCompletion:[self completionForExpectationWithName:@"deleteDocument"]];
  245. [self awaitExpectations];
  246. }
  247. - (void)mergeDocumentRef:(FIRDocumentReference *)ref data:(NSDictionary<NSString *, id> *)data {
  248. [ref setData:data
  249. merge:YES
  250. completion:[self completionForExpectationWithName:@"setDataWithMerge"]];
  251. [self awaitExpectations];
  252. }
  253. - (void)disableNetwork {
  254. [self.db.client
  255. disableNetworkWithCompletion:[self completionForExpectationWithName:@"Disable Network."]];
  256. [self awaitExpectations];
  257. }
  258. - (void)enableNetwork {
  259. [self.db.client
  260. enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable Network."]];
  261. [self awaitExpectations];
  262. }
  263. - (FSTDispatchQueue *)queueForFirestore:(FIRFirestore *)firestore {
  264. return firestore.workerDispatchQueue;
  265. }
  266. - (void)waitUntil:(BOOL (^)())predicate {
  267. NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
  268. double waitSeconds = [self defaultExpectationWaitSeconds];
  269. while (!predicate() && ([NSDate timeIntervalSinceReferenceDate] - start < waitSeconds)) {
  270. // This waits for the next event or until the 100ms timeout is reached
  271. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  272. beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
  273. }
  274. if (!predicate()) {
  275. XCTFail(@"Timeout");
  276. }
  277. }
  278. extern "C" NSArray<NSDictionary<NSString *, id> *> *FIRQuerySnapshotGetData(
  279. FIRQuerySnapshot *docs) {
  280. NSMutableArray<NSDictionary<NSString *, id> *> *result = [NSMutableArray array];
  281. for (FIRDocumentSnapshot *doc in docs.documents) {
  282. [result addObject:doc.data];
  283. }
  284. return result;
  285. }
  286. extern "C" NSArray<NSString *> *FIRQuerySnapshotGetIDs(FIRQuerySnapshot *docs) {
  287. NSMutableArray<NSString *> *result = [NSMutableArray array];
  288. for (FIRDocumentSnapshot *doc in docs.documents) {
  289. [result addObject:doc.documentID];
  290. }
  291. return result;
  292. }
  293. extern "C" NSArray<NSArray<id> *> *FIRQuerySnapshotGetDocChangesData(FIRQuerySnapshot *docs) {
  294. NSMutableArray<NSMutableArray<id> *> *result = [NSMutableArray array];
  295. for (FIRDocumentChange *docChange in docs.documentChanges) {
  296. NSMutableArray<id> *docChangeData = [NSMutableArray array];
  297. [docChangeData addObject:@(docChange.type)];
  298. [docChangeData addObject:docChange.document.documentID];
  299. [docChangeData addObject:docChange.document.data];
  300. [result addObject:docChangeData];
  301. }
  302. return result;
  303. }
  304. @end
  305. NS_ASSUME_NONNULL_END