FSTSpecTests.mm 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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 <algorithm>
  19. #include <map>
  20. #include <memory>
  21. #include <string>
  22. #include <unordered_map>
  23. #include <utility>
  24. #include <vector>
  25. #import "Firestore/Source/API/FSTUserDataConverter.h"
  26. #import "Firestore/Example/Tests/SpecTests/FSTSyncEngineTestDriver.h"
  27. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  28. #include "Firestore/core/include/firebase/firestore/firestore_errors.h"
  29. #include "Firestore/core/src/firebase/firestore/auth/user.h"
  30. #include "Firestore/core/src/firebase/firestore/core/field_filter.h"
  31. #include "Firestore/core/src/firebase/firestore/local/persistence.h"
  32. #include "Firestore/core/src/firebase/firestore/local/target_data.h"
  33. #include "Firestore/core/src/firebase/firestore/model/delete_mutation.h"
  34. #include "Firestore/core/src/firebase/firestore/model/document.h"
  35. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  36. #include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
  37. #include "Firestore/core/src/firebase/firestore/model/field_value.h"
  38. #include "Firestore/core/src/firebase/firestore/model/maybe_document.h"
  39. #include "Firestore/core/src/firebase/firestore/model/no_document.h"
  40. #include "Firestore/core/src/firebase/firestore/model/patch_mutation.h"
  41. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  42. #include "Firestore/core/src/firebase/firestore/model/set_mutation.h"
  43. #include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
  44. #include "Firestore/core/src/firebase/firestore/model/types.h"
  45. #include "Firestore/core/src/firebase/firestore/nanopb/nanopb_util.h"
  46. #include "Firestore/core/src/firebase/firestore/remote/existence_filter.h"
  47. #include "Firestore/core/src/firebase/firestore/remote/serializer.h"
  48. #include "Firestore/core/src/firebase/firestore/remote/watch_change.h"
  49. #include "Firestore/core/src/firebase/firestore/util/async_queue.h"
  50. #include "Firestore/core/src/firebase/firestore/util/comparison.h"
  51. #include "Firestore/core/src/firebase/firestore/util/filesystem.h"
  52. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  53. #include "Firestore/core/src/firebase/firestore/util/log.h"
  54. #include "Firestore/core/src/firebase/firestore/util/path.h"
  55. #include "Firestore/core/src/firebase/firestore/util/status.h"
  56. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  57. #include "Firestore/core/src/firebase/firestore/util/to_string.h"
  58. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  59. #include "absl/types/optional.h"
  60. namespace objc = firebase::firestore::objc;
  61. namespace testutil = firebase::firestore::testutil;
  62. namespace util = firebase::firestore::util;
  63. using firebase::firestore::Error;
  64. using firebase::firestore::auth::User;
  65. using firebase::firestore::core::DocumentViewChange;
  66. using firebase::firestore::core::Query;
  67. using firebase::firestore::local::Persistence;
  68. using firebase::firestore::local::TargetData;
  69. using firebase::firestore::local::QueryPurpose;
  70. using firebase::firestore::model::Document;
  71. using firebase::firestore::model::DocumentKey;
  72. using firebase::firestore::model::DocumentKeySet;
  73. using firebase::firestore::model::DocumentState;
  74. using firebase::firestore::model::FieldValue;
  75. using firebase::firestore::model::MaybeDocument;
  76. using firebase::firestore::model::MutationResult;
  77. using firebase::firestore::model::NoDocument;
  78. using firebase::firestore::model::ObjectValue;
  79. using firebase::firestore::model::ResourcePath;
  80. using firebase::firestore::model::SnapshotVersion;
  81. using firebase::firestore::model::TargetId;
  82. using firebase::firestore::nanopb::ByteString;
  83. using firebase::firestore::nanopb::MakeByteString;
  84. using firebase::firestore::remote::ExistenceFilter;
  85. using firebase::firestore::remote::DocumentWatchChange;
  86. using firebase::firestore::remote::ExistenceFilterWatchChange;
  87. using firebase::firestore::remote::WatchTargetChange;
  88. using firebase::firestore::remote::WatchTargetChangeState;
  89. using firebase::firestore::util::MakeNSString;
  90. using firebase::firestore::util::MakeString;
  91. using firebase::firestore::util::Path;
  92. using firebase::firestore::util::Status;
  93. using firebase::firestore::util::TimerId;
  94. using testutil::Doc;
  95. using testutil::Filter;
  96. using testutil::OrderBy;
  97. NS_ASSUME_NONNULL_BEGIN
  98. // Whether to run the benchmark spec tests.
  99. // TODO(mrschmidt): Make this configurable via the tests schema.
  100. static BOOL kRunBenchmarkTests = NO;
  101. // The name of an environment variable whose value is a filter that specifies which tests to
  102. // execute. The value of this environment variable is a regular expression that is matched against
  103. // the name of each test. Using this environment variable is an alternative to setting the
  104. // kExclusiveTag tag, which requires modifying the JSON file. When this environment variable is set
  105. // to a non-empty value, a test will be executed if and only if its name matches this regular
  106. // expression. In this context, a test's "name" is the result of appending its "itName" to its
  107. // "describeName", separated by a space character.
  108. static NSString *const kTestFilterEnvKey = @"SPEC_TEST_FILTER";
  109. // Disables all other tests; useful for debugging. Multiple tests can have this tag and they'll all
  110. // be run (but all others won't).
  111. static NSString *const kExclusiveTag = @"exclusive";
  112. // A tag for tests that should be excluded from execution (on iOS), useful to allow the platforms
  113. // to temporarily diverge.
  114. static NSString *const kNoIOSTag = @"no-ios";
  115. // A tag for tests that exercise the multi-client behavior of the Web client. These tests are
  116. // ignored on iOS.
  117. static NSString *const kMultiClientTag = @"multi-client";
  118. // A tag for tests that is assigned to the perf tests in "perf_spec.json". These tests are only run
  119. // if `kRunBenchmarkTests` is set to 'YES'.
  120. static NSString *const kBenchmarkTag = @"benchmark";
  121. NSString *const kEagerGC = @"eager-gc";
  122. NSString *const kDurablePersistence = @"durable-persistence";
  123. namespace {
  124. std::vector<TargetId> ConvertTargetsArray(NSArray<NSNumber *> *from) {
  125. std::vector<TargetId> result;
  126. for (NSNumber *targetID in from) {
  127. result.push_back(targetID.intValue);
  128. }
  129. return result;
  130. }
  131. ByteString MakeResumeToken(NSString *specString) {
  132. return MakeByteString([specString dataUsingEncoding:NSUTF8StringEncoding]);
  133. }
  134. NSString *ToDocumentListString(const std::map<DocumentKey, TargetId> &map) {
  135. std::vector<std::string> strings;
  136. strings.reserve(map.size());
  137. for (const auto &kv : map) {
  138. strings.push_back(kv.first.ToString());
  139. }
  140. std::sort(strings.begin(), strings.end());
  141. return MakeNSString(absl::StrJoin(strings, ", "));
  142. }
  143. NSString *ToTargetIdListString(const ActiveTargetMap &map) {
  144. std::vector<model::TargetId> targetIds;
  145. targetIds.reserve(map.size());
  146. for (const auto &kv : map) {
  147. targetIds.push_back(kv.first);
  148. }
  149. std::sort(targetIds.begin(), targetIds.end());
  150. return MakeNSString(absl::StrJoin(targetIds, ", "));
  151. }
  152. } // namespace
  153. @interface FSTSpecTests ()
  154. @property(nonatomic, strong, nullable) FSTSyncEngineTestDriver *driver;
  155. @end
  156. @implementation FSTSpecTests {
  157. BOOL _gcEnabled;
  158. BOOL _networkEnabled;
  159. FSTUserDataConverter *_converter;
  160. }
  161. #define FSTAbstractMethodException() \
  162. [NSException exceptionWithName:NSInternalInconsistencyException \
  163. reason:[NSString stringWithFormat:@"You must override %s in a subclass", \
  164. __func__] \
  165. userInfo:nil];
  166. - (std::unique_ptr<Persistence>)persistenceWithGCEnabled:(BOOL)GCEnabled {
  167. @throw FSTAbstractMethodException(); // NOLINT
  168. }
  169. - (BOOL)shouldRunWithTags:(NSArray<NSString *> *)tags {
  170. if ([tags containsObject:kNoIOSTag]) {
  171. return NO;
  172. } else if ([tags containsObject:kMultiClientTag]) {
  173. return NO;
  174. } else if (!kRunBenchmarkTests && [tags containsObject:kBenchmarkTag]) {
  175. return NO;
  176. }
  177. return YES;
  178. }
  179. - (void)setUpForSpecWithConfig:(NSDictionary *)config {
  180. _converter = FSTTestUserDataConverter();
  181. // Store GCEnabled so we can re-use it in doRestart.
  182. NSNumber *GCEnabled = config[@"useGarbageCollection"];
  183. _gcEnabled = [GCEnabled boolValue];
  184. NSNumber *numClients = config[@"numClients"];
  185. if (numClients) {
  186. XCTAssertEqualObjects(numClients, @1, @"The iOS client does not support multi-client tests");
  187. }
  188. std::unique_ptr<Persistence> persistence = [self persistenceWithGCEnabled:_gcEnabled];
  189. self.driver = [[FSTSyncEngineTestDriver alloc] initWithPersistence:std::move(persistence)];
  190. [self.driver start];
  191. }
  192. - (void)tearDownForSpec {
  193. [self.driver shutdown];
  194. // Help ARC realize that everything here can be collected earlier.
  195. _driver = nil;
  196. }
  197. /**
  198. * Xcode will run tests from any class that extends XCTestCase, but this doesn't work for
  199. * FSTSpecTests since it is incomplete without the implementations supplied by its subclasses.
  200. */
  201. - (BOOL)isTestBaseClass {
  202. return [self class] == [FSTSpecTests class];
  203. }
  204. #pragma mark - Methods for constructing objects from specs.
  205. - (Query)parseQuery:(id)querySpec {
  206. if ([querySpec isKindOfClass:[NSString class]]) {
  207. return testutil::Query(util::MakeString((NSString *)querySpec));
  208. } else if ([querySpec isKindOfClass:[NSDictionary class]]) {
  209. NSDictionary *queryDict = (NSDictionary *)querySpec;
  210. NSString *path = queryDict[@"path"];
  211. ResourcePath resource_path = ResourcePath::FromString(util::MakeString(path));
  212. std::shared_ptr<const std::string> collectionGroup =
  213. util::MakeStringPtr(queryDict[@"collectionGroup"]);
  214. Query query(std::move(resource_path), std::move(collectionGroup));
  215. if (queryDict[@"limit"]) {
  216. NSNumber *limitNumber = queryDict[@"limit"];
  217. auto limit = static_cast<int32_t>(limitNumber.integerValue);
  218. NSString *limitType = queryDict[@"limitType"];
  219. if ([limitType isEqualToString:@"LimitToFirst"]) {
  220. query = query.WithLimitToFirst(limit);
  221. } else {
  222. query = query.WithLimitToLast(limit);
  223. }
  224. }
  225. if (queryDict[@"filters"]) {
  226. NSArray<NSArray<id> *> *filters = queryDict[@"filters"];
  227. for (NSArray<id> *filter in filters) {
  228. std::string key = util::MakeString(filter[0]);
  229. std::string op = util::MakeString(filter[1]);
  230. FieldValue value = [_converter parsedQueryValue:filter[2]];
  231. query = query.AddingFilter(Filter(key, op, value));
  232. }
  233. }
  234. if (queryDict[@"orderBys"]) {
  235. NSArray *orderBys = queryDict[@"orderBys"];
  236. for (NSArray<NSString *> *orderBy in orderBys) {
  237. std::string field_path = util::MakeString(orderBy[0]);
  238. std::string direction = util::MakeString(orderBy[1]);
  239. query = query.AddingOrderBy(OrderBy(field_path, direction));
  240. }
  241. }
  242. return query;
  243. } else {
  244. XCTFail(@"Invalid query: %@", querySpec);
  245. return Query();
  246. }
  247. }
  248. - (SnapshotVersion)parseVersion:(NSNumber *_Nullable)version {
  249. return testutil::Version(version.longLongValue);
  250. }
  251. - (DocumentViewChange)parseChange:(NSDictionary *)jsonDoc ofType:(DocumentViewChange::Type)type {
  252. NSNumber *version = jsonDoc[@"version"];
  253. NSDictionary *options = jsonDoc[@"options"];
  254. DocumentState documentState = [options[@"hasLocalMutations"] isEqualToNumber:@YES]
  255. ? DocumentState::kLocalMutations
  256. : ([options[@"hasCommittedMutations"] isEqualToNumber:@YES]
  257. ? DocumentState::kCommittedMutations
  258. : DocumentState::kSynced);
  259. XCTAssert([jsonDoc[@"key"] isKindOfClass:[NSString class]]);
  260. FieldValue data = [_converter parsedQueryValue:jsonDoc[@"value"]];
  261. Document doc = Doc(util::MakeString((NSString *)jsonDoc[@"key"]), version.longLongValue, data,
  262. documentState);
  263. return DocumentViewChange{doc, type};
  264. }
  265. #pragma mark - Methods for doing the steps of the spec test.
  266. - (void)doListen:(NSArray *)listenSpec {
  267. Query query = [self parseQuery:listenSpec[1]];
  268. TargetId actualID = [self.driver addUserListenerWithQuery:std::move(query)];
  269. TargetId expectedID = [listenSpec[0] intValue];
  270. XCTAssertEqual(actualID, expectedID, @"targetID assigned to listen");
  271. }
  272. - (void)doUnlisten:(NSArray *)unlistenSpec {
  273. Query query = [self parseQuery:unlistenSpec[1]];
  274. [self.driver removeUserListenerWithQuery:std::move(query)];
  275. }
  276. - (void)doSet:(NSArray *)setSpec {
  277. [self.driver writeUserMutation:FSTTestSetMutation(setSpec[0], setSpec[1])];
  278. }
  279. - (void)doPatch:(NSArray *)patchSpec {
  280. [self.driver
  281. writeUserMutation:FSTTestPatchMutation(util::MakeString(patchSpec[0]), patchSpec[1], {})];
  282. }
  283. - (void)doDelete:(NSString *)key {
  284. [self.driver writeUserMutation:FSTTestDeleteMutation(key)];
  285. }
  286. - (void)doAddSnapshotsInSyncListener {
  287. [self.driver addSnapshotsInSyncListener];
  288. }
  289. - (void)doRemoveSnapshotsInSyncListener {
  290. [self.driver removeSnapshotsInSyncListener];
  291. }
  292. - (void)doWatchAck:(NSArray<NSNumber *> *)ackedTargets {
  293. WatchTargetChange change{WatchTargetChangeState::Added, ConvertTargetsArray(ackedTargets)};
  294. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  295. }
  296. - (void)doWatchCurrent:(NSArray<id> *)currentSpec {
  297. NSArray<NSNumber *> *currentTargets = currentSpec[0];
  298. ByteString resumeToken = MakeResumeToken(currentSpec[1]);
  299. WatchTargetChange change{WatchTargetChangeState::Current, ConvertTargetsArray(currentTargets),
  300. resumeToken};
  301. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  302. }
  303. - (void)doWatchRemove:(NSDictionary *)watchRemoveSpec {
  304. Status error;
  305. NSDictionary *cause = watchRemoveSpec[@"cause"];
  306. if (cause) {
  307. int code = ((NSNumber *)cause[@"code"]).intValue;
  308. NSDictionary *userInfo = @{
  309. NSLocalizedDescriptionKey : @"Error from watchRemove.",
  310. };
  311. error = Status{static_cast<Error>(code), MakeString([userInfo description])};
  312. }
  313. WatchTargetChange change{WatchTargetChangeState::Removed,
  314. ConvertTargetsArray(watchRemoveSpec[@"targetIds"]), error};
  315. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  316. // Unlike web, the FSTMockDatastore detects a watch removal with cause and will remove active
  317. // targets
  318. }
  319. - (void)doWatchEntity:(NSDictionary *)watchEntity {
  320. if (watchEntity[@"docs"]) {
  321. HARD_ASSERT(!watchEntity[@"doc"], "Exactly one of |doc| or |docs| needs to be set.");
  322. NSArray *docs = watchEntity[@"docs"];
  323. for (NSDictionary *doc in docs) {
  324. NSMutableDictionary *watchSpec = [NSMutableDictionary dictionary];
  325. watchSpec[@"doc"] = doc;
  326. if (watchEntity[@"targets"]) {
  327. watchSpec[@"targets"] = watchEntity[@"targets"];
  328. }
  329. if (watchEntity[@"removedTargets"]) {
  330. watchSpec[@"removedTargets"] = watchEntity[@"removedTargets"];
  331. }
  332. [self doWatchEntity:watchSpec];
  333. }
  334. } else if (watchEntity[@"doc"]) {
  335. NSDictionary *docSpec = watchEntity[@"doc"];
  336. DocumentKey key = FSTTestDocKey(docSpec[@"key"]);
  337. absl::optional<ObjectValue> value = [docSpec[@"value"] isKindOfClass:[NSNull class]]
  338. ? absl::optional<ObjectValue>{}
  339. : FSTTestObjectValue(docSpec[@"value"]);
  340. SnapshotVersion version = [self parseVersion:docSpec[@"version"]];
  341. MaybeDocument doc;
  342. if (value) {
  343. doc = Document(*std::move(value), key, version, DocumentState::kSynced);
  344. } else {
  345. doc = NoDocument(key, version, /* has_committed_mutations= */ false);
  346. }
  347. DocumentWatchChange change{ConvertTargetsArray(watchEntity[@"targets"]),
  348. ConvertTargetsArray(watchEntity[@"removedTargets"]), std::move(key),
  349. std::move(doc)};
  350. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  351. } else if (watchEntity[@"key"]) {
  352. DocumentKey docKey = FSTTestDocKey(watchEntity[@"key"]);
  353. DocumentWatchChange change{
  354. {}, ConvertTargetsArray(watchEntity[@"removedTargets"]), docKey, absl::nullopt};
  355. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  356. } else {
  357. HARD_FAIL("Either key, doc or docs must be set.");
  358. }
  359. }
  360. - (void)doWatchFilter:(NSArray *)watchFilter {
  361. NSArray<NSNumber *> *targets = watchFilter[0];
  362. HARD_ASSERT(targets.count == 1, "ExistenceFilters currently support exactly one target only.");
  363. int keyCount = watchFilter.count == 0 ? 0 : (int)watchFilter.count - 1;
  364. ExistenceFilter filter{keyCount};
  365. ExistenceFilterWatchChange change{filter, targets[0].intValue};
  366. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  367. }
  368. - (void)doWatchReset:(NSArray<NSNumber *> *)watchReset {
  369. WatchTargetChange change{WatchTargetChangeState::Reset, ConvertTargetsArray(watchReset)};
  370. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  371. }
  372. - (void)doWatchSnapshot:(NSDictionary *)watchSnapshot {
  373. // The client will only respond to watchSnapshots if they are on a target change with an empty
  374. // set of target IDs.
  375. NSArray<NSNumber *> *targetIDs =
  376. watchSnapshot[@"targetIds"] ? watchSnapshot[@"targetIds"] : [NSArray array];
  377. ByteString resumeToken = MakeResumeToken(watchSnapshot[@"resumeToken"]);
  378. WatchTargetChange change{WatchTargetChangeState::NoChange, ConvertTargetsArray(targetIDs),
  379. resumeToken};
  380. [self.driver receiveWatchChange:change
  381. snapshotVersion:[self parseVersion:watchSnapshot[@"version"]]];
  382. }
  383. - (void)doWatchStreamClose:(NSDictionary *)closeSpec {
  384. NSDictionary *errorSpec = closeSpec[@"error"];
  385. int code = ((NSNumber *)(errorSpec[@"code"])).intValue;
  386. NSNumber *runBackoffTimer = closeSpec[@"runBackoffTimer"];
  387. // TODO(b/72313632): Incorporate backoff in iOS Spec Tests.
  388. HARD_ASSERT(runBackoffTimer.boolValue, "iOS Spec Tests don't support backoff.");
  389. [self.driver receiveWatchStreamError:code userInfo:errorSpec];
  390. }
  391. - (void)doWriteAck:(NSDictionary *)spec {
  392. SnapshotVersion version = [self parseVersion:spec[@"version"]];
  393. NSNumber *keepInQueue = spec[@"keepInQueue"];
  394. XCTAssertTrue(keepInQueue == nil || keepInQueue.boolValue == NO,
  395. @"'keepInQueue=true' is not supported on iOS and should only be set in "
  396. @"multi-client tests");
  397. MutationResult mutationResult(version, absl::nullopt);
  398. [self.driver receiveWriteAckWithVersion:version mutationResults:{mutationResult}];
  399. }
  400. - (void)doFailWrite:(NSDictionary *)spec {
  401. NSDictionary *errorSpec = spec[@"error"];
  402. NSNumber *keepInQueue = spec[@"keepInQueue"];
  403. int code = ((NSNumber *)(errorSpec[@"code"])).intValue;
  404. [self.driver receiveWriteError:code userInfo:errorSpec keepInQueue:keepInQueue.boolValue];
  405. }
  406. - (void)doDrainQueue {
  407. [self.driver drainQueue];
  408. }
  409. - (void)doRunTimer:(NSString *)timer {
  410. TimerId timerID;
  411. if ([timer isEqualToString:@"all"]) {
  412. timerID = TimerId::All;
  413. } else if ([timer isEqualToString:@"listen_stream_idle"]) {
  414. timerID = TimerId::ListenStreamIdle;
  415. } else if ([timer isEqualToString:@"listen_stream_connection_backoff"]) {
  416. timerID = TimerId::ListenStreamConnectionBackoff;
  417. } else if ([timer isEqualToString:@"write_stream_idle"]) {
  418. timerID = TimerId::WriteStreamIdle;
  419. } else if ([timer isEqualToString:@"write_stream_connection_backoff"]) {
  420. timerID = TimerId::WriteStreamConnectionBackoff;
  421. } else if ([timer isEqualToString:@"online_state_timeout"]) {
  422. timerID = TimerId::OnlineStateTimeout;
  423. } else {
  424. HARD_FAIL("runTimer spec step specified unknown timer: %s", timer);
  425. }
  426. [self.driver runTimer:timerID];
  427. }
  428. - (void)doDisableNetwork {
  429. _networkEnabled = NO;
  430. [self.driver disableNetwork];
  431. }
  432. - (void)doEnableNetwork {
  433. _networkEnabled = YES;
  434. [self.driver enableNetwork];
  435. }
  436. - (void)doChangeUser:(nullable id)UID {
  437. if ([UID isEqual:[NSNull null]]) {
  438. UID = nil;
  439. }
  440. [self.driver changeUser:User::FromUid(UID)];
  441. }
  442. - (void)doRestart {
  443. // Any outstanding user writes should be automatically re-sent, so we want to preserve them
  444. // when re-creating the driver.
  445. FSTOutstandingWriteQueues outstandingWrites = self.driver.outstandingWrites;
  446. User currentUser = self.driver.currentUser;
  447. [self.driver shutdown];
  448. std::unique_ptr<Persistence> persistence = [self persistenceWithGCEnabled:_gcEnabled];
  449. self.driver = [[FSTSyncEngineTestDriver alloc] initWithPersistence:std::move(persistence)
  450. initialUser:currentUser
  451. outstandingWrites:outstandingWrites];
  452. [self.driver start];
  453. }
  454. - (void)doStep:(NSDictionary *)step {
  455. NSNumber *clientIndex = step[@"clientIndex"];
  456. XCTAssertNil(clientIndex, @"The iOS client does not support switching clients");
  457. if (step[@"userListen"]) {
  458. [self doListen:step[@"userListen"]];
  459. } else if (step[@"userUnlisten"]) {
  460. [self doUnlisten:step[@"userUnlisten"]];
  461. } else if (step[@"userSet"]) {
  462. [self doSet:step[@"userSet"]];
  463. } else if (step[@"userPatch"]) {
  464. [self doPatch:step[@"userPatch"]];
  465. } else if (step[@"userDelete"]) {
  466. [self doDelete:step[@"userDelete"]];
  467. } else if (step[@"addSnapshotsInSyncListener"]) {
  468. [self doAddSnapshotsInSyncListener];
  469. } else if (step[@"removeSnapshotsInSyncListener"]) {
  470. [self doRemoveSnapshotsInSyncListener];
  471. } else if (step[@"drainQueue"]) {
  472. [self doDrainQueue];
  473. } else if (step[@"watchAck"]) {
  474. [self doWatchAck:step[@"watchAck"]];
  475. } else if (step[@"watchCurrent"]) {
  476. [self doWatchCurrent:step[@"watchCurrent"]];
  477. } else if (step[@"watchRemove"]) {
  478. [self doWatchRemove:step[@"watchRemove"]];
  479. } else if (step[@"watchEntity"]) {
  480. [self doWatchEntity:step[@"watchEntity"]];
  481. } else if (step[@"watchFilter"]) {
  482. [self doWatchFilter:step[@"watchFilter"]];
  483. } else if (step[@"watchReset"]) {
  484. [self doWatchReset:step[@"watchReset"]];
  485. } else if (step[@"watchSnapshot"]) {
  486. [self doWatchSnapshot:step[@"watchSnapshot"]];
  487. } else if (step[@"watchStreamClose"]) {
  488. [self doWatchStreamClose:step[@"watchStreamClose"]];
  489. } else if (step[@"watchProto"]) {
  490. // watchProto isn't yet used, and it's unclear how to create arbitrary protos from JSON.
  491. HARD_FAIL("watchProto is not yet supported.");
  492. } else if (step[@"writeAck"]) {
  493. [self doWriteAck:step[@"writeAck"]];
  494. } else if (step[@"failWrite"]) {
  495. [self doFailWrite:step[@"failWrite"]];
  496. } else if (step[@"runTimer"]) {
  497. [self doRunTimer:step[@"runTimer"]];
  498. } else if (step[@"enableNetwork"]) {
  499. if ([step[@"enableNetwork"] boolValue]) {
  500. [self doEnableNetwork];
  501. } else {
  502. [self doDisableNetwork];
  503. }
  504. } else if (step[@"changeUser"]) {
  505. [self doChangeUser:step[@"changeUser"]];
  506. } else if (step[@"restart"]) {
  507. [self doRestart];
  508. } else if (step[@"applyClientState"]) {
  509. XCTFail(@"'applyClientState' is not supported on iOS and should only be used in multi-client "
  510. @"tests");
  511. } else {
  512. XCTFail(@"Unknown step: %@", step);
  513. }
  514. }
  515. - (void)validateEvent:(FSTQueryEvent *)actual matches:(NSDictionary *)expected {
  516. Query expectedQuery = [self parseQuery:expected[@"query"]];
  517. XCTAssertEqual(actual.query, expectedQuery);
  518. if ([expected[@"errorCode"] integerValue] != 0) {
  519. XCTAssertNotNil(actual.error);
  520. XCTAssertEqual(actual.error.code, [expected[@"errorCode"] integerValue]);
  521. } else {
  522. std::vector<DocumentViewChange> expectedChanges;
  523. NSMutableArray *removed = expected[@"removed"];
  524. for (NSDictionary *changeSpec in removed) {
  525. expectedChanges.push_back([self parseChange:changeSpec
  526. ofType:DocumentViewChange::Type::Removed]);
  527. }
  528. NSMutableArray *added = expected[@"added"];
  529. for (NSDictionary *changeSpec in added) {
  530. expectedChanges.push_back([self parseChange:changeSpec
  531. ofType:DocumentViewChange::Type::Added]);
  532. }
  533. NSMutableArray *modified = expected[@"modified"];
  534. for (NSDictionary *changeSpec in modified) {
  535. expectedChanges.push_back([self parseChange:changeSpec
  536. ofType:DocumentViewChange::Type::Modified]);
  537. }
  538. NSMutableArray *metadata = expected[@"metadata"];
  539. for (NSDictionary *changeSpec in metadata) {
  540. expectedChanges.push_back([self parseChange:changeSpec
  541. ofType:DocumentViewChange::Type::Metadata]);
  542. }
  543. XCTAssertEqual(actual.viewSnapshot.value().document_changes().size(), expectedChanges.size());
  544. for (size_t i = 0; i != expectedChanges.size(); ++i) {
  545. XCTAssertTrue((actual.viewSnapshot.value().document_changes()[i] == expectedChanges[i]));
  546. }
  547. BOOL expectedHasPendingWrites =
  548. expected[@"hasPendingWrites"] ? [expected[@"hasPendingWrites"] boolValue] : NO;
  549. BOOL expectedIsFromCache = expected[@"fromCache"] ? [expected[@"fromCache"] boolValue] : NO;
  550. XCTAssertEqual(actual.viewSnapshot.value().has_pending_writes(), expectedHasPendingWrites,
  551. @"hasPendingWrites");
  552. XCTAssertEqual(actual.viewSnapshot.value().from_cache(), expectedIsFromCache, @"isFromCache");
  553. }
  554. }
  555. - (void)validateExpectedSnapshotEvents:(NSArray *_Nullable)expectedEvents {
  556. NSArray<FSTQueryEvent *> *events = self.driver.capturedEventsSinceLastCall;
  557. if (!expectedEvents) {
  558. XCTAssertEqual(events.count, 0u);
  559. for (FSTQueryEvent *event in events) {
  560. XCTFail(@"Unexpected event: %@", event);
  561. }
  562. return;
  563. }
  564. XCTAssertEqual(events.count, expectedEvents.count);
  565. events =
  566. [events sortedArrayUsingComparator:^NSComparisonResult(FSTQueryEvent *q1, FSTQueryEvent *q2) {
  567. return util::WrapCompare(q1.query.CanonicalId(), q2.query.CanonicalId());
  568. }];
  569. expectedEvents = [expectedEvents
  570. sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *left, NSDictionary *right) {
  571. Query leftQuery = [self parseQuery:left[@"query"]];
  572. Query rightQuery = [self parseQuery:right[@"query"]];
  573. return util::WrapCompare(leftQuery.CanonicalId(), rightQuery.CanonicalId());
  574. }];
  575. NSUInteger i = 0;
  576. for (; i < expectedEvents.count && i < events.count; ++i) {
  577. [self validateEvent:events[i] matches:expectedEvents[i]];
  578. }
  579. for (; i < expectedEvents.count; ++i) {
  580. XCTFail(@"Missing event: %@", expectedEvents[i]);
  581. }
  582. for (; i < events.count; ++i) {
  583. XCTFail(@"Unexpected event: %@", events[i]);
  584. }
  585. }
  586. - (void)validateExpectedState:(nullable NSDictionary *)expectedState {
  587. if (expectedState) {
  588. if (expectedState[@"numOutstandingWrites"]) {
  589. XCTAssertEqual([self.driver sentWritesCount],
  590. [expectedState[@"numOutstandingWrites"] intValue]);
  591. }
  592. if (expectedState[@"writeStreamRequestCount"]) {
  593. XCTAssertEqual([self.driver writeStreamRequestCount],
  594. [expectedState[@"writeStreamRequestCount"] intValue]);
  595. }
  596. if (expectedState[@"watchStreamRequestCount"]) {
  597. XCTAssertEqual([self.driver watchStreamRequestCount],
  598. [expectedState[@"watchStreamRequestCount"] intValue]);
  599. }
  600. if (expectedState[@"activeLimboDocs"]) {
  601. DocumentKeySet expectedActiveLimboDocuments;
  602. NSArray *docNames = expectedState[@"activeLimboDocs"];
  603. for (NSString *name in docNames) {
  604. expectedActiveLimboDocuments = expectedActiveLimboDocuments.insert(FSTTestDocKey(name));
  605. }
  606. // Update the expected limbo documents
  607. [self.driver setExpectedActiveLimboDocuments:std::move(expectedActiveLimboDocuments)];
  608. }
  609. if (expectedState[@"activeTargets"]) {
  610. __block ActiveTargetMap expectedActiveTargets;
  611. [expectedState[@"activeTargets"]
  612. enumerateKeysAndObjectsUsingBlock:^(NSString *targetIDString, NSDictionary *queryData,
  613. BOOL *stop) {
  614. TargetId targetID = [targetIDString intValue];
  615. ByteString resumeToken = MakeResumeToken(queryData[@"resumeToken"]);
  616. NSArray *queriesJson = queryData[@"queries"];
  617. std::vector<TargetData> queries;
  618. for (id queryJson in queriesJson) {
  619. Query query = [self parseQuery:queryJson];
  620. // TODO(mcg): populate the purpose of the target once it's possible to encode that in
  621. // the spec tests. For now, hard-code that it's a listen despite the fact that it's
  622. // not always the right value.
  623. queries.push_back(TargetData(query.ToTarget(), targetID, 0, QueryPurpose::Listen,
  624. SnapshotVersion::None(), SnapshotVersion::None(),
  625. std::move(resumeToken)));
  626. }
  627. expectedActiveTargets[targetID] = std::make_pair(std::move(queries), resumeToken);
  628. }];
  629. [self.driver setExpectedActiveTargets:std::move(expectedActiveTargets)];
  630. }
  631. }
  632. // Always validate the we received the expected number of callbacks.
  633. [self validateUserCallbacks:expectedState];
  634. // Always validate that the expected limbo docs match the actual limbo docs.
  635. [self validateLimboDocuments];
  636. // Always validate that the expected active targets match the actual active targets.
  637. [self validateActiveTargets];
  638. }
  639. - (void)validateSnapshotsInSyncEvents:(int)expectedSnapshotInSyncEvents {
  640. XCTAssertEqual(expectedSnapshotInSyncEvents, [self.driver snapshotsInSyncEvents]);
  641. [self.driver resetSnapshotsInSyncEvents];
  642. }
  643. - (void)validateUserCallbacks:(nullable NSDictionary *)expected {
  644. NSDictionary *expectedCallbacks = expected[@"userCallbacks"];
  645. NSArray<NSString *> *actualAcknowledgedDocs =
  646. [self.driver capturedAcknowledgedWritesSinceLastCall];
  647. NSArray<NSString *> *actualRejectedDocs = [self.driver capturedRejectedWritesSinceLastCall];
  648. if (expectedCallbacks) {
  649. XCTAssertTrue([actualAcknowledgedDocs isEqualToArray:expectedCallbacks[@"acknowledgedDocs"]]);
  650. XCTAssertTrue([actualRejectedDocs isEqualToArray:expectedCallbacks[@"rejectedDocs"]]);
  651. } else {
  652. XCTAssertEqual([actualAcknowledgedDocs count], 0u);
  653. XCTAssertEqual([actualRejectedDocs count], 0u);
  654. }
  655. }
  656. - (void)validateLimboDocuments {
  657. // Make a copy so it can modified while checking against the expected limbo docs.
  658. std::map<DocumentKey, TargetId> actualLimboDocs = self.driver.currentLimboDocuments;
  659. // Validate that each active limbo doc has an expected active target
  660. for (const auto &kv : actualLimboDocs) {
  661. const auto &expected = [self.driver expectedActiveTargets];
  662. XCTAssertTrue(expected.find(kv.second) != expected.end(),
  663. @"Found limbo doc %s, but its target ID %d was not in the "
  664. @"set of expected active target IDs %@",
  665. kv.first.ToString().c_str(), kv.second, ToTargetIdListString(expected));
  666. }
  667. for (const DocumentKey &expectedLimboDoc : self.driver.expectedActiveLimboDocuments) {
  668. XCTAssert(actualLimboDocs.find(expectedLimboDoc) != actualLimboDocs.end(),
  669. @"Expected doc to be in limbo, but was not: %s", expectedLimboDoc.ToString().c_str());
  670. actualLimboDocs.erase(expectedLimboDoc);
  671. }
  672. XCTAssertTrue(actualLimboDocs.empty(), @"Unexpected active docs in limbo: %@",
  673. ToDocumentListString(actualLimboDocs));
  674. }
  675. - (void)validateActiveTargets {
  676. if (!_networkEnabled) {
  677. return;
  678. }
  679. // Create a copy so we can modify it below
  680. std::unordered_map<TargetId, TargetData> actualTargets = [self.driver activeTargets];
  681. for (const auto &kv : [self.driver expectedActiveTargets]) {
  682. TargetId targetID = kv.first;
  683. const std::pair<std::vector<TargetData>, ByteString> &queries = kv.second;
  684. const TargetData &targetData = queries.first[0];
  685. auto found = actualTargets.find(targetID);
  686. XCTAssertNotEqual(found, actualTargets.end(), @"Expected active target not found: %s",
  687. targetData.ToString().c_str());
  688. // TODO(mcg): validate the purpose of the target once it's possible to encode that in the
  689. // spec tests. For now, only validate properties that can be validated.
  690. // XCTAssertEqualObjects(actualTargets[targetID], TargetData);
  691. const TargetData &actual = found->second;
  692. XCTAssertEqual(actual.target(), targetData.target());
  693. XCTAssertEqual(actual.target_id(), targetData.target_id());
  694. XCTAssertEqual(actual.snapshot_version(), targetData.snapshot_version());
  695. XCTAssertEqual(actual.resume_token(), targetData.resume_token());
  696. actualTargets.erase(targetID);
  697. }
  698. XCTAssertTrue(actualTargets.empty(), "Unexpected active targets: %s",
  699. util::ToString(actualTargets).c_str());
  700. }
  701. - (void)runSpecTestSteps:(NSArray *)steps config:(NSDictionary *)config {
  702. @autoreleasepool {
  703. @try {
  704. [self setUpForSpecWithConfig:config];
  705. for (NSDictionary *step in steps) {
  706. LOG_DEBUG("Doing step %s", step);
  707. [self doStep:step];
  708. [self validateExpectedSnapshotEvents:step[@"expectedSnapshotEvents"]];
  709. [self validateExpectedState:step[@"expectedState"]];
  710. int expectedSnapshotsInSyncEvents = [step[@"expectedSnapshotsInSyncEvents"] intValue];
  711. [self validateSnapshotsInSyncEvents:expectedSnapshotsInSyncEvents];
  712. }
  713. [self.driver validateUsage];
  714. } @finally {
  715. // Ensure that the driver is torn down even if the test is failing due to a thrown exception
  716. // so that any resources held by the driver are released. This is important when the driver is
  717. // backed by LevelDB because LevelDB locks its database. If -tearDownForSpec were not called
  718. // after an exception then subsequent attempts to open the LevelDB will fail, making it harder
  719. // to zero in on the spec tests as a culprit.
  720. [self tearDownForSpec];
  721. }
  722. }
  723. }
  724. #pragma mark - The actual test methods.
  725. - (void)testSpecTests {
  726. if ([self isTestBaseClass]) return;
  727. // Enumerate the .json files containing the spec tests.
  728. NSMutableArray<NSString *> *specFiles = [NSMutableArray array];
  729. NSMutableArray<NSDictionary *> *parsedSpecs = [NSMutableArray array];
  730. BOOL exclusiveMode = NO;
  731. // TODO(wilhuff): Fix this when running spec tests using a real device
  732. auto source_file = Path::FromUtf8(__FILE__);
  733. Path json_ext = Path::FromUtf8(".json");
  734. auto spec_dir = source_file.Dirname();
  735. auto json_dir = spec_dir.AppendUtf8("json");
  736. auto iter = util::DirectoryIterator::Create(json_dir);
  737. for (; iter->Valid(); iter->Next()) {
  738. Path entry = iter->file();
  739. if (!entry.HasExtension(json_ext)) {
  740. continue;
  741. }
  742. // Read and parse the JSON from the file.
  743. NSString *path = entry.ToNSString();
  744. NSData *json = [NSData dataWithContentsOfFile:path];
  745. XCTAssertNotNil(json);
  746. NSError *error = nil;
  747. id _Nullable parsed = [NSJSONSerialization JSONObjectWithData:json options:0 error:&error];
  748. XCTAssertNil(error, @"%@", error);
  749. XCTAssertTrue([parsed isKindOfClass:[NSDictionary class]]);
  750. NSDictionary *testDict = (NSDictionary *)parsed;
  751. exclusiveMode = exclusiveMode || [self anyTestsAreMarkedExclusive:testDict];
  752. [specFiles addObject:entry.Basename().ToNSString()];
  753. [parsedSpecs addObject:testDict];
  754. }
  755. NSString *testNameFilterFromEnv = NSProcessInfo.processInfo.environment[kTestFilterEnvKey];
  756. NSRegularExpression *testNameFilter;
  757. if (testNameFilterFromEnv.length == 0) {
  758. testNameFilter = nil;
  759. } else {
  760. exclusiveMode = YES;
  761. NSError *error;
  762. testNameFilter =
  763. [NSRegularExpression regularExpressionWithPattern:testNameFilterFromEnv
  764. options:NSRegularExpressionAnchorsMatchLines
  765. error:&error];
  766. XCTAssertNotNil(testNameFilter, @"Invalid regular expression: %@ (%@)", testNameFilterFromEnv,
  767. error);
  768. }
  769. // Now iterate over them and run them.
  770. __block int testPassCount = 0;
  771. __block int testSkipCount = 0;
  772. __block bool ranAtLeastOneTest = NO;
  773. for (NSUInteger i = 0; i < specFiles.count; i++) {
  774. NSLog(@"Spec test file: %@", specFiles[i]);
  775. // Iterate over the tests in the file and run them.
  776. [parsedSpecs[i] enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  777. XCTAssertTrue([obj isKindOfClass:[NSDictionary class]]);
  778. NSDictionary *testDescription = (NSDictionary *)obj;
  779. NSString *describeName = testDescription[@"describeName"];
  780. NSString *itName = testDescription[@"itName"];
  781. NSString *name = [NSString stringWithFormat:@"%@ %@", describeName, itName];
  782. NSDictionary *config = testDescription[@"config"];
  783. NSArray *steps = testDescription[@"steps"];
  784. NSArray<NSString *> *tags = testDescription[@"tags"];
  785. BOOL runTest;
  786. if (![self shouldRunWithTags:tags]) {
  787. runTest = NO;
  788. } else if (!exclusiveMode) {
  789. runTest = YES;
  790. } else if ([tags indexOfObject:kExclusiveTag] != NSNotFound) {
  791. runTest = YES;
  792. } else if (testNameFilter != nil) {
  793. NSRange testNameFilterMatchRange =
  794. [testNameFilter rangeOfFirstMatchInString:name
  795. options:0
  796. range:NSMakeRange(0, [name length])];
  797. runTest = !NSEqualRanges(testNameFilterMatchRange, NSMakeRange(NSNotFound, 0));
  798. } else {
  799. runTest = NO;
  800. }
  801. if (runTest) {
  802. NSLog(@" Spec test: %@", name);
  803. [self runSpecTestSteps:steps config:config];
  804. ranAtLeastOneTest = YES;
  805. ++testPassCount;
  806. } else {
  807. ++testSkipCount;
  808. NSLog(@" [SKIPPED] Spec test: %@", name);
  809. NSString *comment = testDescription[@"comment"];
  810. if (comment) {
  811. NSLog(@" %@", comment);
  812. }
  813. }
  814. }];
  815. }
  816. NSLog(@"%@ completed; pass=%d skip=%d", NSStringFromClass([self class]), testPassCount,
  817. testSkipCount);
  818. XCTAssertTrue(ranAtLeastOneTest);
  819. }
  820. - (BOOL)anyTestsAreMarkedExclusive:(NSDictionary *)tests {
  821. __block BOOL found = NO;
  822. [tests enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  823. XCTAssertTrue([obj isKindOfClass:[NSDictionary class]]);
  824. NSDictionary *testDescription = (NSDictionary *)obj;
  825. NSArray<NSString *> *tags = testDescription[@"tags"];
  826. if ([tags indexOfObject:kExclusiveTag] != NSNotFound) {
  827. found = YES;
  828. *stop = YES;
  829. }
  830. }];
  831. return found;
  832. }
  833. @end
  834. NS_ASSUME_NONNULL_END