FSTSpecTests.mm 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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/SpecTests/FSTSpecTests.h"
  17. #import <FirebaseFirestore/FIRFirestoreErrors.h>
  18. #include <stddef.h>
  19. #include <algorithm>
  20. #include <limits>
  21. #include <map>
  22. #include <memory>
  23. #include <set>
  24. #include <sstream>
  25. #include <string>
  26. #include <unordered_map>
  27. #include <utility>
  28. #include <vector>
  29. #import "Firestore/Source/API/FSTUserDataReader.h"
  30. #import "Firestore/Example/Tests/SpecTests/FSTSyncEngineTestDriver.h"
  31. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  32. #include "Firestore/core/include/firebase/firestore/firestore_errors.h"
  33. #include "Firestore/core/src/api/load_bundle_task.h"
  34. #include "Firestore/core/src/bundle/bundle_reader.h"
  35. #include "Firestore/core/src/bundle/bundle_serializer.h"
  36. #include "Firestore/core/src/core/field_filter.h"
  37. #import "Firestore/core/src/core/listen_options.h"
  38. #include "Firestore/core/src/credentials/user.h"
  39. #include "Firestore/core/src/local/persistence.h"
  40. #include "Firestore/core/src/local/target_data.h"
  41. #include "Firestore/core/src/model/delete_mutation.h"
  42. #include "Firestore/core/src/model/document.h"
  43. #include "Firestore/core/src/model/document_key.h"
  44. #include "Firestore/core/src/model/document_key_set.h"
  45. #include "Firestore/core/src/model/mutable_document.h"
  46. #include "Firestore/core/src/model/patch_mutation.h"
  47. #include "Firestore/core/src/model/resource_path.h"
  48. #include "Firestore/core/src/model/set_mutation.h"
  49. #include "Firestore/core/src/model/snapshot_version.h"
  50. #include "Firestore/core/src/model/types.h"
  51. #include "Firestore/core/src/nanopb/message.h"
  52. #include "Firestore/core/src/nanopb/nanopb_util.h"
  53. #include "Firestore/core/src/remote/bloom_filter.h"
  54. #include "Firestore/core/src/remote/existence_filter.h"
  55. #include "Firestore/core/src/remote/serializer.h"
  56. #include "Firestore/core/src/remote/watch_change.h"
  57. #include "Firestore/core/src/util/async_queue.h"
  58. #include "Firestore/core/src/util/byte_stream_cpp.h"
  59. #include "Firestore/core/src/util/comparison.h"
  60. #include "Firestore/core/src/util/filesystem.h"
  61. #include "Firestore/core/src/util/hard_assert.h"
  62. #include "Firestore/core/src/util/log.h"
  63. #include "Firestore/core/src/util/path.h"
  64. #include "Firestore/core/src/util/status.h"
  65. #include "Firestore/core/src/util/string_apple.h"
  66. #include "Firestore/core/src/util/to_string.h"
  67. #include "Firestore/core/test/unit/testutil/testutil.h"
  68. #include "absl/memory/memory.h"
  69. #include "absl/strings/escaping.h"
  70. #include "absl/types/optional.h"
  71. namespace objc = firebase::firestore::objc;
  72. using firebase::firestore::Error;
  73. using firebase::firestore::google_firestore_v1_ArrayValue;
  74. using firebase::firestore::google_firestore_v1_Value;
  75. using firebase::firestore::api::ListenSource;
  76. using firebase::firestore::api::LoadBundleTask;
  77. using firebase::firestore::bundle::BundleReader;
  78. using firebase::firestore::bundle::BundleSerializer;
  79. using firebase::firestore::core::DocumentViewChange;
  80. using firebase::firestore::core::ListenOptions;
  81. using firebase::firestore::core::Query;
  82. using firebase::firestore::credentials::User;
  83. using firebase::firestore::local::Persistence;
  84. using firebase::firestore::local::QueryPurpose;
  85. using firebase::firestore::local::TargetData;
  86. using firebase::firestore::model::Document;
  87. using firebase::firestore::model::DocumentKey;
  88. using firebase::firestore::model::DocumentKeySet;
  89. using firebase::firestore::model::MutableDocument;
  90. using firebase::firestore::model::MutationResult;
  91. using firebase::firestore::model::ObjectValue;
  92. using firebase::firestore::model::ResourcePath;
  93. using firebase::firestore::model::SnapshotVersion;
  94. using firebase::firestore::model::TargetId;
  95. using firebase::firestore::nanopb::ByteString;
  96. using firebase::firestore::nanopb::MakeByteString;
  97. using firebase::firestore::nanopb::Message;
  98. using firebase::firestore::remote::BloomFilter;
  99. using firebase::firestore::remote::BloomFilterParameters;
  100. using firebase::firestore::remote::DocumentWatchChange;
  101. using firebase::firestore::remote::ExistenceFilter;
  102. using firebase::firestore::remote::ExistenceFilterWatchChange;
  103. using firebase::firestore::remote::WatchTargetChange;
  104. using firebase::firestore::remote::WatchTargetChangeState;
  105. using firebase::firestore::testutil::Doc;
  106. using firebase::firestore::testutil::Filter;
  107. using firebase::firestore::testutil::OrderBy;
  108. using firebase::firestore::testutil::Version;
  109. using firebase::firestore::util::ByteStreamCpp;
  110. using firebase::firestore::util::DirectoryIterator;
  111. using firebase::firestore::util::Executor;
  112. using firebase::firestore::util::MakeNSString;
  113. using firebase::firestore::util::MakeString;
  114. using firebase::firestore::util::MakeStringPtr;
  115. using firebase::firestore::util::Path;
  116. using firebase::firestore::util::Status;
  117. using firebase::firestore::util::StatusOr;
  118. using firebase::firestore::util::TimerId;
  119. using firebase::firestore::util::ToString;
  120. using firebase::firestore::util::WrapCompare;
  121. NS_ASSUME_NONNULL_BEGIN
  122. // Whether to run the benchmark spec tests.
  123. // TODO(mrschmidt): Make this configurable via the tests schema.
  124. static BOOL kRunBenchmarkTests = NO;
  125. // The name of an environment variable whose value is a filter that specifies which tests to
  126. // execute. The value of this environment variable is a regular expression that is matched against
  127. // the name of each test. Using this environment variable is an alternative to setting the
  128. // kExclusiveTag tag, which requires modifying the JSON file. When this environment variable is set
  129. // to a non-empty value, a test will be executed if and only if its name matches this regular
  130. // expression. In this context, a test's "name" is the result of appending its "itName" to its
  131. // "describeName", separated by a space character.
  132. static NSString *const kTestFilterEnvKey = @"SPEC_TEST_FILTER";
  133. // Disables all other tests; useful for debugging. Multiple tests can have this tag and they'll all
  134. // be run (but all others won't).
  135. static NSString *const kExclusiveTag = @"exclusive";
  136. // A tag for tests that should be excluded from execution (on iOS), useful to allow the platforms
  137. // to temporarily diverge.
  138. static NSString *const kNoIOSTag = @"no-ios";
  139. // A tag for tests that exercise the multi-client behavior of the Web client. These tests are
  140. // ignored on iOS.
  141. static NSString *const kMultiClientTag = @"multi-client";
  142. // A tag for tests that is assigned to the perf tests in "perf_spec.json". These tests are only run
  143. // if `kRunBenchmarkTests` is set to 'YES'.
  144. static NSString *const kBenchmarkTag = @"benchmark";
  145. // A tag for tests that should skip its pipeline run.
  146. static NSString *const kNoPipelineConversion = @"no-pipeline-conversion";
  147. NSString *const kEagerGC = @"eager-gc";
  148. NSString *const kDurablePersistence = @"durable-persistence";
  149. namespace {
  150. std::vector<TargetId> ConvertTargetsArray(NSArray<NSNumber *> *from) {
  151. std::vector<TargetId> result;
  152. for (NSNumber *targetID in from) {
  153. result.push_back(targetID.intValue);
  154. }
  155. return result;
  156. }
  157. ByteString MakeResumeToken(NSString *specString) {
  158. return MakeByteString([specString dataUsingEncoding:NSUTF8StringEncoding]);
  159. }
  160. NSString *ToDocumentListString(const std::set<DocumentKey> &keys) {
  161. std::vector<std::string> strings;
  162. strings.reserve(keys.size());
  163. for (const auto &key : keys) {
  164. strings.push_back(key.ToString());
  165. }
  166. std::sort(strings.begin(), strings.end());
  167. return MakeNSString(absl::StrJoin(strings, ", "));
  168. }
  169. NSString *ToDocumentListString(const std::map<DocumentKey, TargetId> &map) {
  170. std::set<DocumentKey> keys;
  171. for (const auto &kv : map) {
  172. keys.insert(kv.first);
  173. }
  174. return ToDocumentListString(keys);
  175. }
  176. NSString *ToTargetIdListString(const ActiveTargetMap &map) {
  177. std::vector<model::TargetId> targetIds;
  178. targetIds.reserve(map.size());
  179. for (const auto &kv : map) {
  180. targetIds.push_back(kv.first);
  181. }
  182. std::sort(targetIds.begin(), targetIds.end());
  183. return MakeNSString(absl::StrJoin(targetIds, ", "));
  184. }
  185. } // namespace
  186. @interface FSTSpecTests ()
  187. @property(nonatomic, strong, nullable) FSTSyncEngineTestDriver *driver;
  188. @end
  189. @implementation FSTSpecTests {
  190. BOOL _useEagerGCForMemory;
  191. size_t _maxConcurrentLimboResolutions;
  192. BOOL _networkEnabled;
  193. FSTUserDataReader *_reader;
  194. std::shared_ptr<Executor> user_executor_;
  195. }
  196. #define FSTAbstractMethodException() \
  197. [NSException exceptionWithName:NSInternalInconsistencyException \
  198. reason:[NSString stringWithFormat:@"You must override %s in a subclass", \
  199. __func__] \
  200. userInfo:nil];
  201. - (std::unique_ptr<Persistence>)persistenceWithEagerGCForMemory:(__unused BOOL)eagerGC {
  202. @throw FSTAbstractMethodException(); // NOLINT
  203. }
  204. - (BOOL)shouldRunWithTags:(NSArray<NSString *> *)tags {
  205. if ([tags containsObject:kNoIOSTag]) {
  206. return NO;
  207. } else if ([tags containsObject:kMultiClientTag]) {
  208. return NO;
  209. } else if (!kRunBenchmarkTests && [tags containsObject:kBenchmarkTag]) {
  210. return NO;
  211. } else if (self.usePipelineMode && [tags containsObject:kNoPipelineConversion]) {
  212. return NO;
  213. }
  214. return YES;
  215. }
  216. - (void)setUpForSpecWithConfig:(NSDictionary *)config {
  217. _convertToPipeline = [self usePipelineMode]; // Call new method
  218. _reader = FSTTestUserDataReader();
  219. std::unique_ptr<Executor> user_executor = Executor::CreateSerial("user executor");
  220. user_executor_ = absl::ShareUniquePtr(std::move(user_executor));
  221. // Store eagerGCForMemory so we can re-use it in doRestart.
  222. NSNumber *eagerGCForMemory = config[@"useEagerGCForMemory"];
  223. _useEagerGCForMemory = [eagerGCForMemory boolValue];
  224. NSNumber *maxConcurrentLimboResolutions = config[@"maxConcurrentLimboResolutions"];
  225. _maxConcurrentLimboResolutions = (maxConcurrentLimboResolutions == nil)
  226. ? std::numeric_limits<size_t>::max()
  227. : maxConcurrentLimboResolutions.unsignedIntValue;
  228. NSNumber *numClients = config[@"numClients"];
  229. if (numClients) {
  230. XCTAssertEqualObjects(numClients, @1, @"The iOS client does not support multi-client tests");
  231. }
  232. std::unique_ptr<Persistence> persistence =
  233. [self persistenceWithEagerGCForMemory:_useEagerGCForMemory];
  234. self.driver =
  235. [[FSTSyncEngineTestDriver alloc] initWithPersistence:std::move(persistence)
  236. eagerGC:_useEagerGCForMemory
  237. convertToPipeline:_convertToPipeline // Pass the flag
  238. initialUser:User::Unauthenticated()
  239. outstandingWrites:{}
  240. maxConcurrentLimboResolutions:_maxConcurrentLimboResolutions];
  241. [self.driver start];
  242. }
  243. - (void)tearDownForSpec {
  244. [self.driver shutdown];
  245. // Help ARC realize that everything here can be collected earlier.
  246. _driver = nil;
  247. }
  248. /**
  249. * Xcode will run tests from any class that extends XCTestCase, but this doesn't work for
  250. * FSTSpecTests since it is incomplete without the implementations supplied by its subclasses.
  251. */
  252. - (BOOL)isTestBaseClass {
  253. return [self class] == [FSTSpecTests class];
  254. }
  255. // Default implementation for pipeline mode. Subclasses can override.
  256. - (BOOL)usePipelineMode {
  257. return NO;
  258. }
  259. #pragma mark - Methods for constructing objects from specs.
  260. - (Query)parseQuery:(id)querySpec {
  261. if ([querySpec isKindOfClass:[NSString class]]) {
  262. return firebase::firestore::testutil::Query(MakeString((NSString *)querySpec));
  263. } else if ([querySpec isKindOfClass:[NSDictionary class]]) {
  264. NSDictionary *queryDict = (NSDictionary *)querySpec;
  265. NSString *path = queryDict[@"path"];
  266. ResourcePath resource_path = ResourcePath::FromString(MakeString(path));
  267. std::shared_ptr<const std::string> collectionGroup =
  268. MakeStringPtr(queryDict[@"collectionGroup"]);
  269. Query query(std::move(resource_path), std::move(collectionGroup));
  270. if (queryDict[@"limit"]) {
  271. NSNumber *limitNumber = queryDict[@"limit"];
  272. auto limit = static_cast<int32_t>(limitNumber.integerValue);
  273. NSString *limitType = queryDict[@"limitType"];
  274. if ([limitType isEqualToString:@"LimitToFirst"]) {
  275. query = query.WithLimitToFirst(limit);
  276. } else {
  277. query = query.WithLimitToLast(limit);
  278. }
  279. }
  280. if (queryDict[@"filters"]) {
  281. NSArray<NSArray<id> *> *filters = queryDict[@"filters"];
  282. for (NSArray<id> *filter in filters) {
  283. std::string key = MakeString(filter[0]);
  284. std::string op = MakeString(filter[1]);
  285. Message<google_firestore_v1_Value> value = [_reader parsedQueryValue:filter[2]];
  286. query = query.AddingFilter(Filter(key, op, std::move(value)));
  287. }
  288. }
  289. if (queryDict[@"orderBys"]) {
  290. NSArray *orderBys = queryDict[@"orderBys"];
  291. for (NSArray<NSString *> *orderBy in orderBys) {
  292. std::string field_path = MakeString(orderBy[0]);
  293. std::string direction = MakeString(orderBy[1]);
  294. query = query.AddingOrderBy(OrderBy(field_path, direction));
  295. }
  296. }
  297. return query;
  298. } else {
  299. XCTFail(@"Invalid query: %@", querySpec);
  300. return Query();
  301. }
  302. }
  303. - (SnapshotVersion)parseVersion:(NSNumber *_Nullable)version {
  304. return Version(version.longLongValue);
  305. }
  306. - (absl::optional<BloomFilterParameters>)parseBloomFilterParameter:
  307. (NSDictionary *_Nullable)bloomFilterProto {
  308. if (bloomFilterProto == nil) {
  309. return absl::nullopt;
  310. }
  311. NSDictionary *bitsData = bloomFilterProto[@"bits"];
  312. // Decode base64 string into uint8_t vector. If bitmap is not specified in proto, use default
  313. // empty string.
  314. NSString *bitmapEncoded = bitsData[@"bitmap"];
  315. std::string bitmapDecoded;
  316. absl::Base64Unescape([bitmapEncoded cStringUsingEncoding:NSASCIIStringEncoding], &bitmapDecoded);
  317. ByteString bitmap(bitmapDecoded);
  318. // If not specified in proto, default padding and hashCount to 0.
  319. int32_t padding = [bitsData[@"padding"] intValue];
  320. int32_t hashCount = [bloomFilterProto[@"hashCount"] intValue];
  321. return BloomFilterParameters{std::move(bitmap), padding, hashCount};
  322. }
  323. - (QueryPurpose)parseQueryPurpose:(NSString *)value {
  324. if ([value isEqualToString:@"TargetPurposeListen"]) {
  325. return QueryPurpose::Listen;
  326. }
  327. if ([value isEqualToString:@"TargetPurposeExistenceFilterMismatch"]) {
  328. return QueryPurpose::ExistenceFilterMismatch;
  329. }
  330. if ([value isEqualToString:@"TargetPurposeExistenceFilterMismatchBloom"]) {
  331. return QueryPurpose::ExistenceFilterMismatchBloom;
  332. }
  333. if ([value isEqualToString:@"TargetPurposeLimboResolution"]) {
  334. return QueryPurpose::LimboResolution;
  335. }
  336. XCTFail(@"unknown query purpose value: %@", value);
  337. return QueryPurpose::Listen;
  338. }
  339. - (DocumentViewChange)parseChange:(NSDictionary *)jsonDoc ofType:(DocumentViewChange::Type)type {
  340. NSNumber *version = jsonDoc[@"version"];
  341. NSDictionary *options = jsonDoc[@"options"];
  342. XCTAssert([jsonDoc[@"key"] isKindOfClass:[NSString class]]);
  343. Message<google_firestore_v1_Value> data = [_reader parsedQueryValue:jsonDoc[@"value"]];
  344. MutableDocument doc =
  345. Doc(MakeString((NSString *)jsonDoc[@"key"]), version.longLongValue, std::move(data));
  346. if ([options[@"hasLocalMutations"] boolValue] == YES) {
  347. doc.SetHasLocalMutations();
  348. } else if ([options[@"hasCommittedMutations"] boolValue] == YES) {
  349. doc.SetHasCommittedMutations();
  350. }
  351. return DocumentViewChange{std::move(doc), type};
  352. }
  353. - (ListenOptions)parseOptions:(NSDictionary *)optionsSpec {
  354. ListenOptions options = ListenOptions::FromIncludeMetadataChanges(true);
  355. if (optionsSpec != nil) {
  356. ListenSource source =
  357. [optionsSpec[@"source"] isEqual:@"cache"] ? ListenSource::Cache : ListenSource::Default;
  358. // include_metadata_changes are default to true in spec tests
  359. options = ListenOptions::FromOptions(true, source);
  360. }
  361. return options;
  362. }
  363. #pragma mark - Methods for doing the steps of the spec test.
  364. - (void)doListen:(NSDictionary *)listenSpec {
  365. Query query = [self parseQuery:listenSpec[@"query"]];
  366. ListenOptions options = [self parseOptions:listenSpec[@"options"]];
  367. TargetId actualID = [self.driver addUserListenerWithQuery:std::move(query) options:options];
  368. TargetId expectedID = [listenSpec[@"targetId"] intValue];
  369. XCTAssertEqual(actualID, expectedID, @"targetID assigned to listen");
  370. }
  371. - (void)doUnlisten:(NSArray *)unlistenSpec {
  372. Query query = [self parseQuery:unlistenSpec[1]];
  373. [self.driver removeUserListenerWithQuery:std::move(query)];
  374. }
  375. - (void)doLoadBundle:(NSString *)bundleJson {
  376. const auto &database_info = [self.driver databaseInfo];
  377. BundleSerializer bundle_serializer(remote::Serializer(database_info.database_id()));
  378. auto data = MakeString(bundleJson);
  379. auto bundle = absl::make_unique<ByteStreamCpp>(
  380. absl::make_unique<std::stringstream>(std::stringstream(data)));
  381. auto reader = std::make_shared<BundleReader>(std::move(bundle_serializer), std::move(bundle));
  382. auto task = std::make_shared<LoadBundleTask>(user_executor_);
  383. [self.driver loadBundleWithReader:std::move(reader) task:std::move(task)];
  384. }
  385. - (void)doSet:(NSArray *)setSpec {
  386. [self.driver writeUserMutation:FSTTestSetMutation(setSpec[0], setSpec[1])];
  387. }
  388. - (void)doPatch:(NSArray *)patchSpec {
  389. [self.driver writeUserMutation:FSTTestPatchMutation(patchSpec[0], patchSpec[1], {})];
  390. }
  391. - (void)doDelete:(NSString *)key {
  392. [self.driver writeUserMutation:FSTTestDeleteMutation(key)];
  393. }
  394. - (void)doWaitForPendingWrites {
  395. [self.driver waitForPendingWrites];
  396. }
  397. - (void)doAddSnapshotsInSyncListener {
  398. [self.driver addSnapshotsInSyncListener];
  399. }
  400. - (void)doRemoveSnapshotsInSyncListener {
  401. [self.driver removeSnapshotsInSyncListener];
  402. }
  403. - (void)doWatchAck:(NSArray<NSNumber *> *)ackedTargets {
  404. WatchTargetChange change{WatchTargetChangeState::Added, ConvertTargetsArray(ackedTargets)};
  405. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  406. }
  407. - (void)doWatchCurrent:(NSArray<id> *)currentSpec {
  408. NSArray<NSNumber *> *currentTargets = currentSpec[0];
  409. ByteString resumeToken = MakeResumeToken(currentSpec[1]);
  410. WatchTargetChange change{WatchTargetChangeState::Current, ConvertTargetsArray(currentTargets),
  411. resumeToken};
  412. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  413. }
  414. - (void)doWatchRemove:(NSDictionary *)watchRemoveSpec {
  415. Status error;
  416. NSDictionary *cause = watchRemoveSpec[@"cause"];
  417. if (cause) {
  418. int code = ((NSNumber *)cause[@"code"]).intValue;
  419. NSDictionary *userInfo = @{
  420. NSLocalizedDescriptionKey : @"Error from watchRemove.",
  421. };
  422. error = Status{static_cast<Error>(code), MakeString([userInfo description])};
  423. }
  424. WatchTargetChange change{WatchTargetChangeState::Removed,
  425. ConvertTargetsArray(watchRemoveSpec[@"targetIds"]), error};
  426. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  427. // Unlike web, the FSTMockDatastore detects a watch removal with cause and will remove active
  428. // targets
  429. }
  430. - (void)doWatchEntity:(NSDictionary *)watchEntity {
  431. if (watchEntity[@"docs"]) {
  432. HARD_ASSERT(!watchEntity[@"doc"], "Exactly one of |doc| or |docs| needs to be set.");
  433. NSArray *docs = watchEntity[@"docs"];
  434. for (NSDictionary *doc in docs) {
  435. NSMutableDictionary *watchSpec = [NSMutableDictionary dictionary];
  436. watchSpec[@"doc"] = doc;
  437. if (watchEntity[@"targets"]) {
  438. watchSpec[@"targets"] = watchEntity[@"targets"];
  439. }
  440. if (watchEntity[@"removedTargets"]) {
  441. watchSpec[@"removedTargets"] = watchEntity[@"removedTargets"];
  442. }
  443. [self doWatchEntity:watchSpec];
  444. }
  445. } else if (watchEntity[@"doc"]) {
  446. NSDictionary *docSpec = watchEntity[@"doc"];
  447. DocumentKey key = FSTTestDocKey(docSpec[@"key"]);
  448. absl::optional<ObjectValue> value = [docSpec[@"value"] isKindOfClass:[NSNull class]]
  449. ? absl::optional<ObjectValue>{}
  450. : FSTTestObjectValue(docSpec[@"value"]);
  451. SnapshotVersion version = [self parseVersion:docSpec[@"version"]];
  452. MutableDocument doc;
  453. if (value) {
  454. doc = MutableDocument::FoundDocument(key, version, *std::move(value));
  455. } else {
  456. doc = MutableDocument::NoDocument(key, version);
  457. }
  458. DocumentWatchChange change{ConvertTargetsArray(watchEntity[@"targets"]),
  459. ConvertTargetsArray(watchEntity[@"removedTargets"]), std::move(key),
  460. std::move(doc)};
  461. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  462. } else if (watchEntity[@"key"]) {
  463. DocumentKey docKey = FSTTestDocKey(watchEntity[@"key"]);
  464. DocumentWatchChange change{
  465. {}, ConvertTargetsArray(watchEntity[@"removedTargets"]), docKey, absl::nullopt};
  466. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  467. } else {
  468. HARD_FAIL("Either key, doc or docs must be set.");
  469. }
  470. }
  471. - (void)doWatchFilter:(NSDictionary *)watchFilter {
  472. NSArray<NSString *> *keys = watchFilter[@"keys"];
  473. NSArray<NSNumber *> *targets = watchFilter[@"targetIds"];
  474. HARD_ASSERT(targets.count == 1, "ExistenceFilters currently support exactly one target only.");
  475. absl::optional<BloomFilterParameters> bloomFilterParameters =
  476. [self parseBloomFilterParameter:watchFilter[@"bloomFilter"]];
  477. ExistenceFilter filter{static_cast<int>(keys.count), std::move(bloomFilterParameters)};
  478. ExistenceFilterWatchChange change{std::move(filter), targets[0].intValue};
  479. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  480. }
  481. - (void)doWatchReset:(NSArray<NSNumber *> *)watchReset {
  482. WatchTargetChange change{WatchTargetChangeState::Reset, ConvertTargetsArray(watchReset)};
  483. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  484. }
  485. - (void)doWatchSnapshot:(NSDictionary *)watchSnapshot {
  486. // The client will only respond to watchSnapshots if they are on a target change with an empty
  487. // set of target IDs.
  488. NSArray<NSNumber *> *targetIDs =
  489. watchSnapshot[@"targetIds"] ? watchSnapshot[@"targetIds"] : [NSArray array];
  490. ByteString resumeToken = MakeResumeToken(watchSnapshot[@"resumeToken"]);
  491. WatchTargetChange change{WatchTargetChangeState::NoChange, ConvertTargetsArray(targetIDs),
  492. resumeToken};
  493. [self.driver receiveWatchChange:change
  494. snapshotVersion:[self parseVersion:watchSnapshot[@"version"]]];
  495. }
  496. - (void)doWatchStreamClose:(NSDictionary *)closeSpec {
  497. NSDictionary *errorSpec = closeSpec[@"error"];
  498. int code = ((NSNumber *)(errorSpec[@"code"])).intValue;
  499. NSNumber *runBackoffTimer = closeSpec[@"runBackoffTimer"];
  500. // TODO(b/72313632): Incorporate backoff in iOS Spec Tests.
  501. HARD_ASSERT(runBackoffTimer.boolValue, "iOS Spec Tests don't support backoff.");
  502. [self.driver receiveWatchStreamError:code userInfo:errorSpec];
  503. }
  504. - (void)doWriteAck:(NSDictionary *)spec {
  505. SnapshotVersion version = [self parseVersion:spec[@"version"]];
  506. NSNumber *keepInQueue = spec[@"keepInQueue"];
  507. XCTAssertTrue(keepInQueue == nil || keepInQueue.boolValue == NO,
  508. @"'keepInQueue=true' is not supported on iOS and should only be set in "
  509. @"multi-client tests");
  510. MutationResult mutationResult(version, Message<google_firestore_v1_ArrayValue>{});
  511. std::vector<MutationResult> mutationResults;
  512. mutationResults.emplace_back(std::move(mutationResult));
  513. [self.driver receiveWriteAckWithVersion:version mutationResults:std::move(mutationResults)];
  514. }
  515. - (void)doFailWrite:(NSDictionary *)spec {
  516. NSDictionary *errorSpec = spec[@"error"];
  517. NSNumber *keepInQueue = spec[@"keepInQueue"];
  518. int code = ((NSNumber *)(errorSpec[@"code"])).intValue;
  519. [self.driver receiveWriteError:code userInfo:errorSpec keepInQueue:keepInQueue.boolValue];
  520. }
  521. - (void)doDrainQueue {
  522. [self.driver drainQueue];
  523. }
  524. - (void)doRunTimer:(NSString *)timer {
  525. TimerId timerID;
  526. if ([timer isEqualToString:@"all"]) {
  527. timerID = TimerId::All;
  528. } else if ([timer isEqualToString:@"listen_stream_idle"]) {
  529. timerID = TimerId::ListenStreamIdle;
  530. } else if ([timer isEqualToString:@"listen_stream_connection_backoff"]) {
  531. timerID = TimerId::ListenStreamConnectionBackoff;
  532. } else if ([timer isEqualToString:@"write_stream_idle"]) {
  533. timerID = TimerId::WriteStreamIdle;
  534. } else if ([timer isEqualToString:@"write_stream_connection_backoff"]) {
  535. timerID = TimerId::WriteStreamConnectionBackoff;
  536. } else if ([timer isEqualToString:@"online_state_timeout"]) {
  537. timerID = TimerId::OnlineStateTimeout;
  538. } else {
  539. HARD_FAIL("runTimer spec step specified unknown timer: %s", timer);
  540. }
  541. [self.driver runTimer:timerID];
  542. }
  543. - (void)doDisableNetwork {
  544. _networkEnabled = NO;
  545. [self.driver disableNetwork];
  546. }
  547. - (void)doEnableNetwork {
  548. _networkEnabled = YES;
  549. [self.driver enableNetwork];
  550. }
  551. - (void)doTriggerLruGC:(NSNumber *)threshold {
  552. [self.driver triggerLruGC:threshold];
  553. }
  554. - (void)doChangeUser:(nullable id)UID {
  555. if ([UID isEqual:[NSNull null]]) {
  556. UID = nil;
  557. }
  558. [self.driver changeUser:User::FromUid(UID)];
  559. }
  560. - (void)doRestart {
  561. // Any outstanding user writes should be automatically re-sent, so we want to preserve them
  562. // when re-creating the driver.
  563. FSTOutstandingWriteQueues outstandingWrites = self.driver.outstandingWrites;
  564. User currentUser = self.driver.currentUser;
  565. [self.driver shutdown];
  566. std::unique_ptr<Persistence> persistence =
  567. [self persistenceWithEagerGCForMemory:_useEagerGCForMemory];
  568. self.driver =
  569. [[FSTSyncEngineTestDriver alloc] initWithPersistence:std::move(persistence)
  570. eagerGC:_useEagerGCForMemory
  571. convertToPipeline:_convertToPipeline // Pass the flag
  572. initialUser:currentUser
  573. outstandingWrites:outstandingWrites
  574. maxConcurrentLimboResolutions:_maxConcurrentLimboResolutions];
  575. [self.driver start];
  576. }
  577. - (void)doStep:(NSDictionary *)step {
  578. NSNumber *clientIndex = step[@"clientIndex"];
  579. XCTAssertNil(clientIndex, @"The iOS client does not support switching clients");
  580. if (step[@"userListen"]) {
  581. [self doListen:step[@"userListen"]];
  582. } else if (step[@"userUnlisten"]) {
  583. [self doUnlisten:step[@"userUnlisten"]];
  584. } else if (step[@"userSet"]) {
  585. [self doSet:step[@"userSet"]];
  586. } else if (step[@"userPatch"]) {
  587. [self doPatch:step[@"userPatch"]];
  588. } else if (step[@"userDelete"]) {
  589. [self doDelete:step[@"userDelete"]];
  590. } else if (step[@"addSnapshotsInSyncListener"]) {
  591. [self doAddSnapshotsInSyncListener];
  592. } else if (step[@"removeSnapshotsInSyncListener"]) {
  593. [self doRemoveSnapshotsInSyncListener];
  594. } else if (step[@"drainQueue"]) {
  595. [self doDrainQueue];
  596. } else if (step[@"loadBundle"]) {
  597. [self doLoadBundle:step[@"loadBundle"]];
  598. } else if (step[@"watchAck"]) {
  599. [self doWatchAck:step[@"watchAck"]];
  600. } else if (step[@"watchCurrent"]) {
  601. [self doWatchCurrent:step[@"watchCurrent"]];
  602. } else if (step[@"watchRemove"]) {
  603. [self doWatchRemove:step[@"watchRemove"]];
  604. } else if (step[@"watchEntity"]) {
  605. [self doWatchEntity:step[@"watchEntity"]];
  606. } else if (step[@"watchFilter"]) {
  607. [self doWatchFilter:step[@"watchFilter"]];
  608. } else if (step[@"watchReset"]) {
  609. [self doWatchReset:step[@"watchReset"]];
  610. } else if (step[@"watchSnapshot"]) {
  611. [self doWatchSnapshot:step[@"watchSnapshot"]];
  612. } else if (step[@"watchStreamClose"]) {
  613. [self doWatchStreamClose:step[@"watchStreamClose"]];
  614. } else if (step[@"watchProto"]) {
  615. // watchProto isn't yet used, and it's unclear how to create arbitrary protos from JSON.
  616. HARD_FAIL("watchProto is not yet supported.");
  617. } else if (step[@"writeAck"]) {
  618. [self doWriteAck:step[@"writeAck"]];
  619. } else if (step[@"failWrite"]) {
  620. [self doFailWrite:step[@"failWrite"]];
  621. } else if (step[@"waitForPendingWrites"]) {
  622. [self doWaitForPendingWrites];
  623. } else if (step[@"runTimer"]) {
  624. [self doRunTimer:step[@"runTimer"]];
  625. } else if (step[@"enableNetwork"]) {
  626. if ([step[@"enableNetwork"] boolValue]) {
  627. [self doEnableNetwork];
  628. } else {
  629. [self doDisableNetwork];
  630. }
  631. } else if (step[@"changeUser"]) {
  632. [self doChangeUser:step[@"changeUser"]];
  633. } else if (step[@"triggerLruGC"]) {
  634. [self doTriggerLruGC:step[@"triggerLruGC"]];
  635. } else if (step[@"restart"]) {
  636. [self doRestart];
  637. } else if (step[@"applyClientState"]) {
  638. XCTFail(@"'applyClientState' is not supported on iOS and should only be used in multi-client "
  639. @"tests");
  640. } else {
  641. XCTFail(@"Unknown step: %@", step);
  642. }
  643. }
  644. - (void)validateEvent:(FSTQueryEvent *)actual matches:(NSDictionary *)expected {
  645. // The 'expected' query from JSON is always a standard Query.
  646. Query expectedJSONQuery = [self parseQuery:expected[@"query"]];
  647. core::QueryOrPipeline actualQueryOrPipeline = actual.queryOrPipeline;
  648. if (_convertToPipeline) {
  649. XCTAssertTrue(actualQueryOrPipeline.IsPipeline(),
  650. @"In pipeline mode, actual event query should be a pipeline. Actual: %@",
  651. MakeNSString(actualQueryOrPipeline.ToString()));
  652. // Convert the expected JSON Query to a RealtimePipeline for comparison.
  653. std::vector<std::shared_ptr<api::EvaluableStage>> expectedStages =
  654. core::ToPipelineStages(expectedJSONQuery);
  655. // TODO(specstest): Need access to the database_id for the serializer.
  656. // Assuming self.driver.databaseInfo is accessible and provides it.
  657. // This might require making databaseInfo public or providing a getter in
  658. // FSTSyncEngineTestDriver. For now, proceeding with the assumption it's available.
  659. auto serializer = absl::make_unique<remote::Serializer>(self.driver.databaseInfo.database_id());
  660. api::RealtimePipeline expectedPipeline(std::move(expectedStages), std::move(serializer));
  661. auto expectedQoPForComparison =
  662. core::QueryOrPipeline(expectedPipeline); // Wrap expected pipeline
  663. XCTAssertEqual(actualQueryOrPipeline.CanonicalId(), expectedQoPForComparison.CanonicalId(),
  664. @"Pipeline canonical IDs do not match. Actual: %@, Expected: %@",
  665. MakeNSString(actualQueryOrPipeline.CanonicalId()),
  666. MakeNSString(expectedQoPForComparison.CanonicalId()));
  667. } else {
  668. XCTAssertFalse(actualQueryOrPipeline.IsPipeline(),
  669. @"In non-pipeline mode, actual event query should be a Query. Actual: %@",
  670. MakeNSString(actualQueryOrPipeline.ToString()));
  671. XCTAssertTrue(actualQueryOrPipeline.query() == expectedJSONQuery,
  672. @"Queries do not match. Actual: %@, Expected: %@",
  673. MakeNSString(actualQueryOrPipeline.query().ToString()),
  674. MakeNSString(expectedJSONQuery.ToString()));
  675. }
  676. if ([expected[@"errorCode"] integerValue] != 0) {
  677. XCTAssertNotNil(actual.error);
  678. XCTAssertEqual(actual.error.code, [expected[@"errorCode"] integerValue]);
  679. } else {
  680. std::vector<DocumentViewChange> expectedChanges;
  681. NSMutableArray *removed = expected[@"removed"];
  682. for (NSDictionary *changeSpec in removed) {
  683. expectedChanges.push_back([self parseChange:changeSpec
  684. ofType:DocumentViewChange::Type::Removed]);
  685. }
  686. NSMutableArray *added = expected[@"added"];
  687. for (NSDictionary *changeSpec in added) {
  688. expectedChanges.push_back([self parseChange:changeSpec
  689. ofType:DocumentViewChange::Type::Added]);
  690. }
  691. NSMutableArray *modified = expected[@"modified"];
  692. for (NSDictionary *changeSpec in modified) {
  693. expectedChanges.push_back([self parseChange:changeSpec
  694. ofType:DocumentViewChange::Type::Modified]);
  695. }
  696. NSMutableArray *metadata = expected[@"metadata"];
  697. for (NSDictionary *changeSpec in metadata) {
  698. expectedChanges.push_back([self parseChange:changeSpec
  699. ofType:DocumentViewChange::Type::Metadata]);
  700. }
  701. XCTAssertEqual(actual.viewSnapshot.value().document_changes().size(), expectedChanges.size());
  702. auto comparator = [](const DocumentViewChange &lhs, const DocumentViewChange &rhs) {
  703. return lhs.document()->key() < rhs.document()->key();
  704. };
  705. std::vector<DocumentViewChange> expectedChangesSorted = expectedChanges;
  706. std::sort(expectedChangesSorted.begin(), expectedChangesSorted.end(), comparator);
  707. std::vector<DocumentViewChange> actualChangesSorted =
  708. actual.viewSnapshot.value().document_changes();
  709. std::sort(actualChangesSorted.begin(), actualChangesSorted.end(), comparator);
  710. for (size_t i = 0; i != expectedChangesSorted.size(); ++i) {
  711. XCTAssertTrue((actualChangesSorted[i] == expectedChangesSorted[i]));
  712. }
  713. BOOL expectedHasPendingWrites =
  714. expected[@"hasPendingWrites"] ? [expected[@"hasPendingWrites"] boolValue] : NO;
  715. BOOL expectedIsFromCache = expected[@"fromCache"] ? [expected[@"fromCache"] boolValue] : NO;
  716. XCTAssertEqual(actual.viewSnapshot.value().has_pending_writes(), expectedHasPendingWrites,
  717. @"hasPendingWrites");
  718. XCTAssertEqual(actual.viewSnapshot.value().from_cache(), expectedIsFromCache, @"isFromCache");
  719. }
  720. }
  721. - (void)validateExpectedSnapshotEvents:(NSArray *_Nullable)expectedEvents {
  722. NSArray<FSTQueryEvent *> *events = self.driver.capturedEventsSinceLastCall;
  723. if (!expectedEvents) {
  724. XCTAssertEqual(events.count, 0u);
  725. for (FSTQueryEvent *event in events) {
  726. XCTFail(@"Unexpected event: %@", event);
  727. }
  728. return;
  729. }
  730. XCTAssertEqual(events.count, expectedEvents.count);
  731. events =
  732. [events sortedArrayUsingComparator:^NSComparisonResult(FSTQueryEvent *q1, FSTQueryEvent *q2) {
  733. // Use QueryOrPipeline's CanonicalId for sorting
  734. return WrapCompare(q1.queryOrPipeline.CanonicalId(), q2.queryOrPipeline.CanonicalId());
  735. }];
  736. expectedEvents = [expectedEvents sortedArrayUsingComparator:^NSComparisonResult(
  737. NSDictionary *left, NSDictionary *right) {
  738. // Expected query from JSON is always a core::Query.
  739. // For sorting consistency with actual events (which might be pipelines),
  740. // we convert the expected query to QueryOrPipeline then get its CanonicalId.
  741. // If _convertToPipeline is true, this will effectively sort expected items
  742. // by their pipeline canonical ID.
  743. Query leftJSONQuery = [self parseQuery:left[@"query"]];
  744. core::QueryOrPipeline leftQoP;
  745. if (self->_convertToPipeline) {
  746. std::vector<std::shared_ptr<api::EvaluableStage>> stages =
  747. core::ToPipelineStages(leftJSONQuery);
  748. auto serializer =
  749. absl::make_unique<remote::Serializer>(self.driver.databaseInfo.database_id());
  750. leftQoP =
  751. core::QueryOrPipeline(api::RealtimePipeline(std::move(stages), std::move(serializer)));
  752. } else {
  753. leftQoP = core::QueryOrPipeline(leftJSONQuery);
  754. }
  755. Query rightJSONQuery = [self parseQuery:right[@"query"]];
  756. core::QueryOrPipeline rightQoP;
  757. if (self->_convertToPipeline) {
  758. std::vector<std::shared_ptr<api::EvaluableStage>> stages =
  759. core::ToPipelineStages(rightJSONQuery);
  760. auto serializer =
  761. absl::make_unique<remote::Serializer>(self.driver.databaseInfo.database_id());
  762. rightQoP =
  763. core::QueryOrPipeline(api::RealtimePipeline(std::move(stages), std::move(serializer)));
  764. } else {
  765. rightQoP = core::QueryOrPipeline(rightJSONQuery);
  766. }
  767. return WrapCompare(leftQoP.CanonicalId(), rightQoP.CanonicalId());
  768. }];
  769. NSUInteger i = 0;
  770. for (; i < expectedEvents.count && i < events.count; ++i) {
  771. [self validateEvent:events[i] matches:expectedEvents[i]];
  772. }
  773. for (; i < expectedEvents.count; ++i) {
  774. XCTFail(@"Missing event: %@", expectedEvents[i]);
  775. }
  776. for (; i < events.count; ++i) {
  777. XCTFail(@"Unexpected event: %@", events[i]);
  778. }
  779. }
  780. - (void)validateExpectedState:(nullable NSDictionary *)expectedState {
  781. if (expectedState) {
  782. if (expectedState[@"numOutstandingWrites"]) {
  783. XCTAssertEqual([self.driver sentWritesCount],
  784. [expectedState[@"numOutstandingWrites"] intValue]);
  785. }
  786. if (expectedState[@"writeStreamRequestCount"]) {
  787. XCTAssertEqual([self.driver writeStreamRequestCount],
  788. [expectedState[@"writeStreamRequestCount"] intValue]);
  789. }
  790. if (expectedState[@"watchStreamRequestCount"]) {
  791. XCTAssertEqual([self.driver watchStreamRequestCount],
  792. [expectedState[@"watchStreamRequestCount"] intValue]);
  793. }
  794. if (expectedState[@"activeLimboDocs"]) {
  795. DocumentKeySet expectedActiveLimboDocuments;
  796. NSArray *docNames = expectedState[@"activeLimboDocs"];
  797. for (NSString *name in docNames) {
  798. expectedActiveLimboDocuments = expectedActiveLimboDocuments.insert(FSTTestDocKey(name));
  799. }
  800. // Update the expected active limbo documents
  801. [self.driver setExpectedActiveLimboDocuments:std::move(expectedActiveLimboDocuments)];
  802. }
  803. if (expectedState[@"enqueuedLimboDocs"]) {
  804. DocumentKeySet expectedEnqueuedLimboDocuments;
  805. NSArray *docNames = expectedState[@"enqueuedLimboDocs"];
  806. for (NSString *name in docNames) {
  807. expectedEnqueuedLimboDocuments = expectedEnqueuedLimboDocuments.insert(FSTTestDocKey(name));
  808. }
  809. // Update the expected enqueued limbo documents
  810. [self.driver setExpectedEnqueuedLimboDocuments:std::move(expectedEnqueuedLimboDocuments)];
  811. }
  812. if (expectedState[@"activeTargets"]) {
  813. __block ActiveTargetMap expectedActiveTargets;
  814. [expectedState[@"activeTargets"]
  815. enumerateKeysAndObjectsUsingBlock:^(NSString *targetIDString, NSDictionary *queryData,
  816. BOOL *) {
  817. TargetId targetID = [targetIDString intValue];
  818. NSArray *queriesJson = queryData[@"queries"];
  819. std::vector<TargetData> queries;
  820. for (id queryJson in queriesJson) {
  821. QueryPurpose purpose = QueryPurpose::Listen;
  822. if ([queryData objectForKey:@"targetPurpose"] != nil) {
  823. purpose = [self parseQueryPurpose:queryData[@"targetPurpose"]];
  824. }
  825. core::TargetOrPipeline top;
  826. Query query = [self parseQuery:queryJson];
  827. if (self->_convertToPipeline &&
  828. purpose != firebase::firestore::local::QueryPurpose::LimboResolution) {
  829. std::vector<std::shared_ptr<api::EvaluableStage>> stages =
  830. core::ToPipelineStages(query);
  831. auto serializer =
  832. absl::make_unique<remote::Serializer>(self.driver.databaseInfo.database_id());
  833. top = core::TargetOrPipeline(
  834. api::RealtimePipeline(std::move(stages), std::move(serializer)));
  835. } else {
  836. top = core::TargetOrPipeline(query.ToTarget());
  837. }
  838. TargetData target_data(top, targetID, 0, purpose);
  839. if ([queryData objectForKey:@"resumeToken"] != nil) {
  840. target_data = target_data.WithResumeToken(
  841. MakeResumeToken(queryData[@"resumeToken"]), SnapshotVersion::None());
  842. } else {
  843. target_data = target_data.WithResumeToken(
  844. ByteString(), [self parseVersion:queryData[@"readTime"]]);
  845. }
  846. if ([queryData objectForKey:@"expectedCount"] != nil) {
  847. target_data = target_data.WithExpectedCount([queryData[@"expectedCount"] intValue]);
  848. }
  849. queries.push_back(std::move(target_data));
  850. }
  851. expectedActiveTargets[targetID] = std::move(queries);
  852. }];
  853. [self.driver setExpectedActiveTargets:std::move(expectedActiveTargets)];
  854. }
  855. }
  856. // Always validate the we received the expected number of callbacks.
  857. [self validateUserCallbacks:expectedState];
  858. // Always validate that the expected limbo docs match the actual limbo docs.
  859. [self validateActiveLimboDocuments];
  860. [self validateEnqueuedLimboDocuments];
  861. // Always validate that the expected active targets match the actual active targets.
  862. [self validateActiveTargets];
  863. }
  864. - (void)validateWaitForPendingWritesEvents:(int)expectedWaitForPendingWritesEvents {
  865. XCTAssertEqual(expectedWaitForPendingWritesEvents, [self.driver waitForPendingWritesEvents]);
  866. [self.driver resetWaitForPendingWritesEvents];
  867. }
  868. - (void)validateSnapshotsInSyncEvents:(int)expectedSnapshotInSyncEvents {
  869. XCTAssertEqual(expectedSnapshotInSyncEvents, [self.driver snapshotsInSyncEvents]);
  870. [self.driver resetSnapshotsInSyncEvents];
  871. }
  872. - (void)validateUserCallbacks:(nullable NSDictionary *)expected {
  873. NSDictionary *expectedCallbacks = expected[@"userCallbacks"];
  874. NSArray<NSString *> *actualAcknowledgedDocs =
  875. [self.driver capturedAcknowledgedWritesSinceLastCall];
  876. NSArray<NSString *> *actualRejectedDocs = [self.driver capturedRejectedWritesSinceLastCall];
  877. if (expectedCallbacks) {
  878. XCTAssertTrue([actualAcknowledgedDocs isEqualToArray:expectedCallbacks[@"acknowledgedDocs"]]);
  879. XCTAssertTrue([actualRejectedDocs isEqualToArray:expectedCallbacks[@"rejectedDocs"]]);
  880. } else {
  881. XCTAssertEqual([actualAcknowledgedDocs count], 0u);
  882. XCTAssertEqual([actualRejectedDocs count], 0u);
  883. }
  884. }
  885. - (void)validateActiveLimboDocuments {
  886. // Make a copy so it can modified while checking against the expected limbo docs.
  887. std::map<DocumentKey, TargetId> actualLimboDocs = self.driver.activeLimboDocumentResolutions;
  888. // Validate that each active limbo doc has an expected active target
  889. for (const auto &kv : actualLimboDocs) {
  890. const auto &expected = [self.driver expectedActiveTargets];
  891. XCTAssertTrue(expected.find(kv.second) != expected.end(),
  892. @"Found limbo doc %s, but its target ID %d was not in the "
  893. @"set of expected active target IDs %@",
  894. kv.first.ToString().c_str(), kv.second, ToTargetIdListString(expected));
  895. }
  896. for (const DocumentKey &expectedLimboDoc : self.driver.expectedActiveLimboDocuments) {
  897. XCTAssert(actualLimboDocs.find(expectedLimboDoc) != actualLimboDocs.end(),
  898. @"Expected doc to be in limbo, but was not: %s", expectedLimboDoc.ToString().c_str());
  899. actualLimboDocs.erase(expectedLimboDoc);
  900. }
  901. XCTAssertTrue(actualLimboDocs.empty(), @"Unexpected active docs in limbo: %@",
  902. ToDocumentListString(actualLimboDocs));
  903. }
  904. - (void)validateEnqueuedLimboDocuments {
  905. std::set<DocumentKey> actualLimboDocs;
  906. for (const auto &key : self.driver.enqueuedLimboDocumentResolutions) {
  907. actualLimboDocs.insert(key);
  908. }
  909. std::set<DocumentKey> expectedLimboDocs;
  910. for (const auto &key : self.driver.expectedEnqueuedLimboDocuments) {
  911. expectedLimboDocs.insert(key);
  912. }
  913. for (const auto &key : actualLimboDocs) {
  914. XCTAssertTrue(expectedLimboDocs.find(key) != expectedLimboDocs.end(),
  915. @"Found enqueued limbo doc %s, but it was not in the set of "
  916. @"expected enqueued limbo documents (%@)",
  917. key.ToString().c_str(), ToDocumentListString(expectedLimboDocs));
  918. }
  919. for (const auto &key : expectedLimboDocs) {
  920. XCTAssertTrue(actualLimboDocs.find(key) != actualLimboDocs.end(),
  921. @"Expected doc %s to be enqueued for limbo resolution, "
  922. @"but it was not in the queue (%@)",
  923. key.ToString().c_str(), ToDocumentListString(actualLimboDocs));
  924. }
  925. }
  926. - (void)validateActiveTargets {
  927. if (!_networkEnabled) {
  928. return;
  929. }
  930. // Create a copy so we can modify it below
  931. std::unordered_map<TargetId, TargetData> actualTargets = [self.driver activeTargets];
  932. for (const auto &kv : [self.driver expectedActiveTargets]) {
  933. TargetId targetID = kv.first;
  934. const std::vector<TargetData> &queries = kv.second;
  935. const TargetData &targetData = queries[0];
  936. auto found = actualTargets.find(targetID);
  937. XCTAssertNotEqual(found, actualTargets.end(), @"Expected active target not found: %s",
  938. targetData.ToString().c_str());
  939. // TODO(Mila): Replace the XCTAssertEqual() checks on the individual properties of TargetData
  940. // below with the single assertEquals on the TargetData objects themselves if the sequenceNumber
  941. // is ever made to be consistent.
  942. // XCTAssertEqualObjects(actualTargets[targetID], TargetData);
  943. const TargetData &actual = found->second;
  944. auto left = actual.target_or_pipeline();
  945. auto right = targetData.target_or_pipeline();
  946. auto left_p = left.IsPipeline();
  947. auto right_p = right.IsPipeline();
  948. XCTAssertEqual(left_p, right_p);
  949. XCTAssertEqual(left, right);
  950. XCTAssertEqual(actual.purpose(), targetData.purpose());
  951. XCTAssertEqual(actual.target_id(), targetData.target_id());
  952. XCTAssertEqual(actual.snapshot_version(), targetData.snapshot_version());
  953. XCTAssertEqual(actual.resume_token(), targetData.resume_token());
  954. if (targetData.expected_count().has_value()) {
  955. if (!actual.expected_count().has_value()) {
  956. XCTFail(@"Actual target data doesn't have an expected_count.");
  957. } else {
  958. XCTAssertEqual(actual.expected_count().value(), targetData.expected_count().value());
  959. }
  960. }
  961. actualTargets.erase(targetID);
  962. }
  963. XCTAssertTrue(actualTargets.empty(), "Unexpected active targets: %s",
  964. ToString(actualTargets).c_str());
  965. }
  966. - (void)runSpecTestSteps:(NSArray *)steps config:(NSDictionary *)config {
  967. @autoreleasepool {
  968. @try {
  969. [self setUpForSpecWithConfig:config];
  970. for (NSDictionary *step in steps) {
  971. LOG_DEBUG("Doing step %s", step);
  972. [self doStep:step];
  973. [self validateExpectedSnapshotEvents:step[@"expectedSnapshotEvents"]];
  974. [self validateExpectedState:step[@"expectedState"]];
  975. int expectedSnapshotsInSyncEvents = [step[@"expectedSnapshotsInSyncEvents"] intValue];
  976. [self validateSnapshotsInSyncEvents:expectedSnapshotsInSyncEvents];
  977. int expectedWaitForPendingWritesEvents =
  978. [step[@"expectedWaitForPendingWritesEvents"] intValue];
  979. [self validateWaitForPendingWritesEvents:expectedWaitForPendingWritesEvents];
  980. }
  981. [self.driver validateUsage];
  982. } @finally {
  983. // Ensure that the driver is torn down even if the test is failing due to a thrown exception
  984. // so that any resources held by the driver are released. This is important when the driver is
  985. // backed by LevelDB because LevelDB locks its database. If -tearDownForSpec were not called
  986. // after an exception then subsequent attempts to open the LevelDB will fail, making it harder
  987. // to zero in on the spec tests as a culprit.
  988. [self tearDownForSpec];
  989. }
  990. }
  991. }
  992. #pragma mark - The actual test methods.
  993. - (void)testSpecTests {
  994. if ([self isTestBaseClass]) return;
  995. // LogSetLevel(firebase::firestore::util::kLogLevelDebug);
  996. // Enumerate the .json files containing the spec tests.
  997. NSMutableArray<NSString *> *specFiles = [NSMutableArray array];
  998. NSMutableArray<NSDictionary *> *parsedSpecs = [NSMutableArray array];
  999. BOOL exclusiveMode = NO;
  1000. // TODO(wilhuff): Fix this when running spec tests using a real device
  1001. auto source_file = Path::FromUtf8(__FILE__);
  1002. Path json_ext = Path::FromUtf8(".json");
  1003. auto spec_dir = source_file.Dirname();
  1004. auto json_dir = spec_dir.AppendUtf8("json");
  1005. auto iter = DirectoryIterator::Create(json_dir);
  1006. for (; iter->Valid(); iter->Next()) {
  1007. Path entry = iter->file();
  1008. if (!entry.HasExtension(json_ext)) {
  1009. continue;
  1010. }
  1011. // Read and parse the JSON from the file.
  1012. NSString *path = entry.ToNSString();
  1013. NSData *json = [NSData dataWithContentsOfFile:path];
  1014. XCTAssertNotNil(json);
  1015. NSError *error = nil;
  1016. id _Nullable parsed = [NSJSONSerialization JSONObjectWithData:json options:0 error:&error];
  1017. XCTAssertNil(error, @"%@", error);
  1018. XCTAssertTrue([parsed isKindOfClass:[NSDictionary class]]);
  1019. NSDictionary *testDict = (NSDictionary *)parsed;
  1020. exclusiveMode = exclusiveMode || [self anyTestsAreMarkedExclusive:testDict];
  1021. [specFiles addObject:entry.Basename().ToNSString()];
  1022. [parsedSpecs addObject:testDict];
  1023. }
  1024. NSString *testNameFilterFromEnv = NSProcessInfo.processInfo.environment[kTestFilterEnvKey];
  1025. NSRegularExpression *testNameFilter;
  1026. if (testNameFilterFromEnv.length == 0) {
  1027. testNameFilter = nil;
  1028. } else {
  1029. exclusiveMode = YES;
  1030. NSError *error;
  1031. testNameFilter =
  1032. [NSRegularExpression regularExpressionWithPattern:testNameFilterFromEnv
  1033. options:NSRegularExpressionAnchorsMatchLines
  1034. error:&error];
  1035. XCTAssertNotNil(testNameFilter, @"Invalid regular expression: %@ (%@)", testNameFilterFromEnv,
  1036. error);
  1037. }
  1038. // Now iterate over them and run them.
  1039. __block int testPassCount = 0;
  1040. __block int testSkipCount = 0;
  1041. __block bool ranAtLeastOneTest = NO;
  1042. for (NSUInteger i = 0; i < specFiles.count; i++) {
  1043. NSLog(@"Spec test file: %@", specFiles[i]);
  1044. // Iterate over the tests in the file and run them.
  1045. [parsedSpecs[i] enumerateKeysAndObjectsUsingBlock:^(id, id obj, BOOL *) {
  1046. XCTAssertTrue([obj isKindOfClass:[NSDictionary class]]);
  1047. NSDictionary *testDescription = (NSDictionary *)obj;
  1048. NSString *describeName = testDescription[@"describeName"];
  1049. NSString *itName = testDescription[@"itName"];
  1050. NSString *name = [NSString stringWithFormat:@"%@ %@", describeName, itName];
  1051. NSDictionary *config = testDescription[@"config"];
  1052. NSArray *steps = testDescription[@"steps"];
  1053. NSArray<NSString *> *tags = testDescription[@"tags"];
  1054. BOOL runTest;
  1055. if (![self shouldRunWithTags:tags]) {
  1056. runTest = NO;
  1057. } else if (!exclusiveMode) {
  1058. runTest = YES;
  1059. } else if ([tags indexOfObject:kExclusiveTag] != NSNotFound) {
  1060. runTest = YES;
  1061. } else if (testNameFilter != nil) {
  1062. NSRange testNameFilterMatchRange =
  1063. [testNameFilter rangeOfFirstMatchInString:name
  1064. options:0
  1065. range:NSMakeRange(0, [name length])];
  1066. runTest = !NSEqualRanges(testNameFilterMatchRange, NSMakeRange(NSNotFound, 0));
  1067. } else {
  1068. runTest = NO;
  1069. }
  1070. if (runTest) {
  1071. NSLog(@" Spec test: %@", name);
  1072. [self runSpecTestSteps:steps config:config];
  1073. ranAtLeastOneTest = YES;
  1074. ++testPassCount;
  1075. } else {
  1076. ++testSkipCount;
  1077. // NSLog(@" [SKIPPED] Spec test: %@", name);
  1078. NSString *comment = testDescription[@"comment"];
  1079. if (comment) {
  1080. // NSLog(@" %@", comment);
  1081. }
  1082. }
  1083. }];
  1084. }
  1085. NSLog(@"%@ completed; pass=%d skip=%d", NSStringFromClass([self class]), testPassCount,
  1086. testSkipCount);
  1087. XCTAssertTrue(ranAtLeastOneTest);
  1088. }
  1089. - (BOOL)anyTestsAreMarkedExclusive:(NSDictionary *)tests {
  1090. __block BOOL found = NO;
  1091. [tests enumerateKeysAndObjectsUsingBlock:^(id, id obj, BOOL *stop) {
  1092. XCTAssertTrue([obj isKindOfClass:[NSDictionary class]]);
  1093. NSDictionary *testDescription = (NSDictionary *)obj;
  1094. NSArray<NSString *> *tags = testDescription[@"tags"];
  1095. if ([tags indexOfObject:kExclusiveTag] != NSNotFound) {
  1096. found = YES;
  1097. *stop = YES;
  1098. }
  1099. }];
  1100. return found;
  1101. }
  1102. @end
  1103. NS_ASSUME_NONNULL_END