FSTSpecTests.mm 46 KB

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