FSTSpecTests.mm 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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 <map>
  19. #include <memory>
  20. #include <string>
  21. #include <unordered_map>
  22. #include <utility>
  23. #include <vector>
  24. #import "Firestore/Source/API/FSTUserDataConverter.h"
  25. #import "Firestore/Source/Core/FSTEventManager.h"
  26. #import "Firestore/Source/Core/FSTQuery.h"
  27. #import "Firestore/Source/Local/FSTPersistence.h"
  28. #import "Firestore/Source/Local/FSTQueryData.h"
  29. #import "Firestore/Source/Model/FSTDocument.h"
  30. #import "Firestore/Source/Model/FSTMutation.h"
  31. #import "Firestore/Source/Util/FSTClasses.h"
  32. #import "Firestore/Example/Tests/SpecTests/FSTSyncEngineTestDriver.h"
  33. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  34. #include "Firestore/core/include/firebase/firestore/firestore_errors.h"
  35. #include "Firestore/core/src/firebase/firestore/auth/user.h"
  36. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  37. #include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
  38. #include "Firestore/core/src/firebase/firestore/model/field_value.h"
  39. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  40. #include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
  41. #include "Firestore/core/src/firebase/firestore/model/types.h"
  42. #include "Firestore/core/src/firebase/firestore/objc/objc_compatibility.h"
  43. #include "Firestore/core/src/firebase/firestore/remote/existence_filter.h"
  44. #include "Firestore/core/src/firebase/firestore/remote/watch_change.h"
  45. #include "Firestore/core/src/firebase/firestore/util/async_queue.h"
  46. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  47. #include "Firestore/core/src/firebase/firestore/util/log.h"
  48. #include "Firestore/core/src/firebase/firestore/util/status.h"
  49. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  50. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  51. #include "absl/types/optional.h"
  52. namespace objc = firebase::firestore::objc;
  53. namespace testutil = firebase::firestore::testutil;
  54. namespace util = firebase::firestore::util;
  55. using firebase::firestore::FirestoreErrorCode;
  56. using firebase::firestore::auth::User;
  57. using firebase::firestore::core::DocumentViewChange;
  58. using firebase::firestore::model::DocumentKey;
  59. using firebase::firestore::model::DocumentKeySet;
  60. using firebase::firestore::model::DocumentState;
  61. using firebase::firestore::model::FieldValue;
  62. using firebase::firestore::model::ObjectValue;
  63. using firebase::firestore::model::ResourcePath;
  64. using firebase::firestore::model::SnapshotVersion;
  65. using firebase::firestore::model::TargetId;
  66. using firebase::firestore::remote::ExistenceFilter;
  67. using firebase::firestore::remote::DocumentWatchChange;
  68. using firebase::firestore::remote::ExistenceFilterWatchChange;
  69. using firebase::firestore::remote::WatchTargetChange;
  70. using firebase::firestore::remote::WatchTargetChangeState;
  71. using firebase::firestore::util::MakeString;
  72. using firebase::firestore::util::Status;
  73. using firebase::firestore::util::TimerId;
  74. using testutil::Filter;
  75. NS_ASSUME_NONNULL_BEGIN
  76. // Whether to run the benchmark spec tests.
  77. // TODO(mrschmidt): Make this configurable via the tests schema.
  78. static BOOL kRunBenchmarkTests = NO;
  79. // Disables all other tests; useful for debugging. Multiple tests can have this tag and they'll all
  80. // be run (but all others won't).
  81. static NSString *const kExclusiveTag = @"exclusive";
  82. // A tag for tests that should be excluded from execution (on iOS), useful to allow the platforms
  83. // to temporarily diverge.
  84. static NSString *const kNoIOSTag = @"no-ios";
  85. // A tag for tests that exercise the multi-client behavior of the Web client. These tests are
  86. // ignored on iOS.
  87. static NSString *const kMultiClientTag = @"multi-client";
  88. // A tag for tests that is assigned to the perf tests in "perf_spec.json". These tests are only run
  89. // if `kRunBenchmarkTests` is set to 'YES'.
  90. static NSString *const kBenchmarkTag = @"benchmark";
  91. NSString *const kEagerGC = @"eager-gc";
  92. NSString *const kDurablePersistence = @"durable-persistence";
  93. namespace {
  94. NSString *Describe(NSData *data) {
  95. return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  96. }
  97. std::vector<TargetId> ConvertTargetsArray(NSArray<NSNumber *> *from) {
  98. std::vector<TargetId> result;
  99. for (NSNumber *targetID in from) {
  100. result.push_back(targetID.intValue);
  101. }
  102. return result;
  103. }
  104. } // namespace
  105. @interface FSTSpecTests ()
  106. @property(nonatomic, strong) FSTSyncEngineTestDriver *driver;
  107. @end
  108. @implementation FSTSpecTests {
  109. BOOL _gcEnabled;
  110. BOOL _networkEnabled;
  111. }
  112. - (id<FSTPersistence>)persistenceWithGCEnabled:(BOOL)GCEnabled {
  113. @throw FSTAbstractMethodException(); // NOLINT
  114. }
  115. - (BOOL)shouldRunWithTags:(NSArray<NSString *> *)tags {
  116. if ([tags containsObject:kNoIOSTag]) {
  117. return NO;
  118. } else if ([tags containsObject:kMultiClientTag]) {
  119. return NO;
  120. } else if (!kRunBenchmarkTests && [tags containsObject:kBenchmarkTag]) {
  121. return NO;
  122. }
  123. return YES;
  124. }
  125. - (void)setUpForSpecWithConfig:(NSDictionary *)config {
  126. // Store GCEnabled so we can re-use it in doRestart.
  127. NSNumber *GCEnabled = config[@"useGarbageCollection"];
  128. _gcEnabled = [GCEnabled boolValue];
  129. NSNumber *numClients = config[@"numClients"];
  130. if (numClients) {
  131. XCTAssertEqualObjects(numClients, @1, @"The iOS client does not support multi-client tests");
  132. }
  133. id<FSTPersistence> persistence = [self persistenceWithGCEnabled:_gcEnabled];
  134. self.driver = [[FSTSyncEngineTestDriver alloc] initWithPersistence:persistence];
  135. [self.driver start];
  136. }
  137. - (void)tearDownForSpec {
  138. [self.driver shutdown];
  139. }
  140. /**
  141. * Xcode will run tests from any class that extends XCTestCase, but this doesn't work for
  142. * FSTSpecTests since it is incomplete without the implementations supplied by its subclasses.
  143. */
  144. - (BOOL)isTestBaseClass {
  145. return [self class] == [FSTSpecTests class];
  146. }
  147. #pragma mark - Methods for constructing objects from specs.
  148. - (nullable FSTQuery *)parseQuery:(id)querySpec {
  149. if ([querySpec isKindOfClass:[NSString class]]) {
  150. return FSTTestQuery(util::MakeString((NSString *)querySpec));
  151. } else if ([querySpec isKindOfClass:[NSDictionary class]]) {
  152. NSDictionary *queryDict = (NSDictionary *)querySpec;
  153. NSString *path = queryDict[@"path"];
  154. ResourcePath resource_path = ResourcePath::FromString(util::MakeString(path));
  155. std::shared_ptr<const std::string> collectionGroup =
  156. util::MakeStringPtr(queryDict[@"collectionGroup"]);
  157. __block FSTQuery *query = [FSTQuery queryWithPath:resource_path
  158. collectionGroup:collectionGroup];
  159. if (queryDict[@"limit"]) {
  160. NSNumber *limitNumber = queryDict[@"limit"];
  161. auto limit = static_cast<int32_t>(limitNumber.integerValue);
  162. query = [query queryBySettingLimit:limit];
  163. }
  164. if (queryDict[@"filters"]) {
  165. FSTUserDataConverter *converter = FSTTestUserDataConverter();
  166. NSArray *filters = queryDict[@"filters"];
  167. [filters enumerateObjectsUsingBlock:^(NSArray *_Nonnull filter, NSUInteger idx,
  168. BOOL *_Nonnull stop) {
  169. FieldValue value = [converter parsedQueryValue:filter[2]];
  170. query = [query queryByAddingFilter:Filter(util::MakeString(filter[0]),
  171. util::MakeString(filter[1]), value)];
  172. }];
  173. }
  174. if (queryDict[@"orderBys"]) {
  175. NSArray *orderBys = queryDict[@"orderBys"];
  176. [orderBys enumerateObjectsUsingBlock:^(NSArray *_Nonnull orderBy, NSUInteger idx,
  177. BOOL *_Nonnull stop) {
  178. query =
  179. [query queryByAddingSortOrder:FSTTestOrderBy(util::MakeString(orderBy[0]), orderBy[1])];
  180. }];
  181. }
  182. return query;
  183. } else {
  184. XCTFail(@"Invalid query: %@", querySpec);
  185. return nil;
  186. }
  187. }
  188. - (SnapshotVersion)parseVersion:(NSNumber *_Nullable)version {
  189. return testutil::Version(version.longLongValue);
  190. }
  191. - (DocumentViewChange)parseChange:(NSDictionary *)jsonDoc ofType:(DocumentViewChange::Type)type {
  192. NSNumber *version = jsonDoc[@"version"];
  193. NSDictionary *options = jsonDoc[@"options"];
  194. DocumentState documentState = [options[@"hasLocalMutations"] isEqualToNumber:@YES]
  195. ? DocumentState::kLocalMutations
  196. : ([options[@"hasCommittedMutations"] isEqualToNumber:@YES]
  197. ? DocumentState::kCommittedMutations
  198. : DocumentState::kSynced);
  199. XCTAssert([jsonDoc[@"key"] isKindOfClass:[NSString class]]);
  200. FSTDocument *doc = FSTTestDoc(util::MakeString((NSString *)jsonDoc[@"key"]),
  201. version.longLongValue, jsonDoc[@"value"], documentState);
  202. return DocumentViewChange{doc, type};
  203. }
  204. #pragma mark - Methods for doing the steps of the spec test.
  205. - (void)doListen:(NSArray *)listenSpec {
  206. FSTQuery *query = [self parseQuery:listenSpec[1]];
  207. TargetId actualID = [self.driver addUserListenerWithQuery:query];
  208. TargetId expectedID = [listenSpec[0] intValue];
  209. XCTAssertEqual(actualID, expectedID, @"targetID assigned to listen");
  210. }
  211. - (void)doUnlisten:(NSArray *)unlistenSpec {
  212. FSTQuery *query = [self parseQuery:unlistenSpec[1]];
  213. [self.driver removeUserListenerWithQuery:query];
  214. }
  215. - (void)doSet:(NSArray *)setSpec {
  216. [self.driver writeUserMutation:FSTTestSetMutation(setSpec[0], setSpec[1])];
  217. }
  218. - (void)doPatch:(NSArray *)patchSpec {
  219. [self.driver
  220. writeUserMutation:FSTTestPatchMutation(util::MakeString(patchSpec[0]), patchSpec[1], {})];
  221. }
  222. - (void)doDelete:(NSString *)key {
  223. [self.driver writeUserMutation:FSTTestDeleteMutation(key)];
  224. }
  225. - (void)doWatchAck:(NSArray<NSNumber *> *)ackedTargets {
  226. WatchTargetChange change{WatchTargetChangeState::Added, ConvertTargetsArray(ackedTargets)};
  227. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  228. }
  229. - (void)doWatchCurrent:(NSArray<id> *)currentSpec {
  230. NSArray<NSNumber *> *currentTargets = currentSpec[0];
  231. NSData *resumeToken = [currentSpec[1] dataUsingEncoding:NSUTF8StringEncoding];
  232. WatchTargetChange change{WatchTargetChangeState::Current, ConvertTargetsArray(currentTargets),
  233. resumeToken};
  234. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  235. }
  236. - (void)doWatchRemove:(NSDictionary *)watchRemoveSpec {
  237. Status error;
  238. NSDictionary *cause = watchRemoveSpec[@"cause"];
  239. if (cause) {
  240. int code = ((NSNumber *)cause[@"code"]).intValue;
  241. NSDictionary *userInfo = @{
  242. NSLocalizedDescriptionKey : @"Error from watchRemove.",
  243. };
  244. error = Status{static_cast<FirestoreErrorCode>(code), MakeString([userInfo description])};
  245. }
  246. WatchTargetChange change{WatchTargetChangeState::Removed,
  247. ConvertTargetsArray(watchRemoveSpec[@"targetIds"]), error};
  248. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  249. // Unlike web, the FSTMockDatastore detects a watch removal with cause and will remove active
  250. // targets
  251. }
  252. - (void)doWatchEntity:(NSDictionary *)watchEntity {
  253. if (watchEntity[@"docs"]) {
  254. HARD_ASSERT(!watchEntity[@"doc"], "Exactly one of |doc| or |docs| needs to be set.");
  255. NSArray *docs = watchEntity[@"docs"];
  256. for (NSDictionary *doc in docs) {
  257. NSMutableDictionary *watchSpec = [NSMutableDictionary dictionary];
  258. watchSpec[@"doc"] = doc;
  259. if (watchEntity[@"targets"]) {
  260. watchSpec[@"targets"] = watchEntity[@"targets"];
  261. }
  262. if (watchEntity[@"removedTargets"]) {
  263. watchSpec[@"removedTargets"] = watchEntity[@"removedTargets"];
  264. }
  265. [self doWatchEntity:watchSpec];
  266. }
  267. } else if (watchEntity[@"doc"]) {
  268. NSDictionary *docSpec = watchEntity[@"doc"];
  269. DocumentKey key = FSTTestDocKey(docSpec[@"key"]);
  270. absl::optional<ObjectValue> value = [docSpec[@"value"] isKindOfClass:[NSNull class]]
  271. ? absl::optional<ObjectValue>{}
  272. : FSTTestObjectValue(docSpec[@"value"]);
  273. SnapshotVersion version = [self parseVersion:docSpec[@"version"]];
  274. FSTMaybeDocument *doc = value ? [FSTDocument documentWithData:*value
  275. key:key
  276. version:std::move(version)
  277. state:DocumentState::kSynced]
  278. : [FSTDeletedDocument documentWithKey:key
  279. version:std::move(version)
  280. hasCommittedMutations:NO];
  281. DocumentWatchChange change{ConvertTargetsArray(watchEntity[@"targets"]),
  282. ConvertTargetsArray(watchEntity[@"removedTargets"]), doc.key, doc};
  283. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  284. } else if (watchEntity[@"key"]) {
  285. DocumentKey docKey = FSTTestDocKey(watchEntity[@"key"]);
  286. DocumentWatchChange change{
  287. {}, ConvertTargetsArray(watchEntity[@"removedTargets"]), docKey, nil};
  288. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  289. } else {
  290. HARD_FAIL("Either key, doc or docs must be set.");
  291. }
  292. }
  293. - (void)doWatchFilter:(NSArray *)watchFilter {
  294. NSArray<NSNumber *> *targets = watchFilter[0];
  295. HARD_ASSERT(targets.count == 1, "ExistenceFilters currently support exactly one target only.");
  296. int keyCount = watchFilter.count == 0 ? 0 : (int)watchFilter.count - 1;
  297. ExistenceFilter filter{keyCount};
  298. ExistenceFilterWatchChange change{filter, targets[0].intValue};
  299. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  300. }
  301. - (void)doWatchReset:(NSArray<NSNumber *> *)watchReset {
  302. WatchTargetChange change{WatchTargetChangeState::Reset, ConvertTargetsArray(watchReset)};
  303. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  304. }
  305. - (void)doWatchSnapshot:(NSDictionary *)watchSnapshot {
  306. // The client will only respond to watchSnapshots if they are on a target change with an empty
  307. // set of target IDs.
  308. NSArray<NSNumber *> *targetIDs =
  309. watchSnapshot[@"targetIds"] ? watchSnapshot[@"targetIds"] : [NSArray array];
  310. NSData *resumeToken = [watchSnapshot[@"resumeToken"] dataUsingEncoding:NSUTF8StringEncoding];
  311. WatchTargetChange change{WatchTargetChangeState::NoChange, ConvertTargetsArray(targetIDs),
  312. resumeToken};
  313. [self.driver receiveWatchChange:change
  314. snapshotVersion:[self parseVersion:watchSnapshot[@"version"]]];
  315. }
  316. - (void)doWatchStreamClose:(NSDictionary *)closeSpec {
  317. NSDictionary *errorSpec = closeSpec[@"error"];
  318. int code = ((NSNumber *)(errorSpec[@"code"])).intValue;
  319. NSNumber *runBackoffTimer = closeSpec[@"runBackoffTimer"];
  320. // TODO(b/72313632): Incorporate backoff in iOS Spec Tests.
  321. HARD_ASSERT(runBackoffTimer.boolValue, "iOS Spec Tests don't support backoff.");
  322. [self.driver receiveWatchStreamError:code userInfo:errorSpec];
  323. }
  324. - (void)doWriteAck:(NSDictionary *)spec {
  325. SnapshotVersion version = [self parseVersion:spec[@"version"]];
  326. NSNumber *keepInQueue = spec[@"keepInQueue"];
  327. XCTAssertTrue(keepInQueue == nil || keepInQueue.boolValue == NO,
  328. @"'keepInQueue=true' is not supported on iOS and should only be set in "
  329. @"multi-client tests");
  330. FSTMutationResult *mutationResult = [[FSTMutationResult alloc] initWithVersion:version
  331. transformResults:absl::nullopt];
  332. [self.driver receiveWriteAckWithVersion:version mutationResults:{mutationResult}];
  333. }
  334. - (void)doFailWrite:(NSDictionary *)spec {
  335. NSDictionary *errorSpec = spec[@"error"];
  336. NSNumber *keepInQueue = spec[@"keepInQueue"];
  337. int code = ((NSNumber *)(errorSpec[@"code"])).intValue;
  338. [self.driver receiveWriteError:code userInfo:errorSpec keepInQueue:keepInQueue.boolValue];
  339. }
  340. - (void)doDrainQueue {
  341. [self.driver drainQueue];
  342. }
  343. - (void)doRunTimer:(NSString *)timer {
  344. TimerId timerID;
  345. if ([timer isEqualToString:@"all"]) {
  346. timerID = TimerId::All;
  347. } else if ([timer isEqualToString:@"listen_stream_idle"]) {
  348. timerID = TimerId::ListenStreamIdle;
  349. } else if ([timer isEqualToString:@"listen_stream_connection_backoff"]) {
  350. timerID = TimerId::ListenStreamConnectionBackoff;
  351. } else if ([timer isEqualToString:@"write_stream_idle"]) {
  352. timerID = TimerId::WriteStreamIdle;
  353. } else if ([timer isEqualToString:@"write_stream_connection_backoff"]) {
  354. timerID = TimerId::WriteStreamConnectionBackoff;
  355. } else if ([timer isEqualToString:@"online_state_timeout"]) {
  356. timerID = TimerId::OnlineStateTimeout;
  357. } else {
  358. HARD_FAIL("runTimer spec step specified unknown timer: %s", timer);
  359. }
  360. [self.driver runTimer:timerID];
  361. }
  362. - (void)doDisableNetwork {
  363. _networkEnabled = NO;
  364. [self.driver disableNetwork];
  365. }
  366. - (void)doEnableNetwork {
  367. _networkEnabled = YES;
  368. [self.driver enableNetwork];
  369. }
  370. - (void)doChangeUser:(nullable id)UID {
  371. if ([UID isEqual:[NSNull null]]) {
  372. UID = nil;
  373. }
  374. [self.driver changeUser:User::FromUid(UID)];
  375. }
  376. - (void)doRestart {
  377. // Any outstanding user writes should be automatically re-sent, so we want to preserve them
  378. // when re-creating the driver.
  379. FSTOutstandingWriteQueues outstandingWrites = self.driver.outstandingWrites;
  380. User currentUser = self.driver.currentUser;
  381. [self.driver shutdown];
  382. id<FSTPersistence> persistence = [self persistenceWithGCEnabled:_gcEnabled];
  383. self.driver = [[FSTSyncEngineTestDriver alloc] initWithPersistence:persistence
  384. initialUser:currentUser
  385. outstandingWrites:outstandingWrites];
  386. [self.driver start];
  387. }
  388. - (void)doStep:(NSDictionary *)step {
  389. NSNumber *clientIndex = step[@"clientIndex"];
  390. XCTAssertNil(clientIndex, @"The iOS client does not support switching clients");
  391. if (step[@"userListen"]) {
  392. [self doListen:step[@"userListen"]];
  393. } else if (step[@"userUnlisten"]) {
  394. [self doUnlisten:step[@"userUnlisten"]];
  395. } else if (step[@"userSet"]) {
  396. [self doSet:step[@"userSet"]];
  397. } else if (step[@"userPatch"]) {
  398. [self doPatch:step[@"userPatch"]];
  399. } else if (step[@"userDelete"]) {
  400. [self doDelete:step[@"userDelete"]];
  401. } else if (step[@"drainQueue"]) {
  402. [self doDrainQueue];
  403. } else if (step[@"watchAck"]) {
  404. [self doWatchAck:step[@"watchAck"]];
  405. } else if (step[@"watchCurrent"]) {
  406. [self doWatchCurrent:step[@"watchCurrent"]];
  407. } else if (step[@"watchRemove"]) {
  408. [self doWatchRemove:step[@"watchRemove"]];
  409. } else if (step[@"watchEntity"]) {
  410. [self doWatchEntity:step[@"watchEntity"]];
  411. } else if (step[@"watchFilter"]) {
  412. [self doWatchFilter:step[@"watchFilter"]];
  413. } else if (step[@"watchReset"]) {
  414. [self doWatchReset:step[@"watchReset"]];
  415. } else if (step[@"watchSnapshot"]) {
  416. [self doWatchSnapshot:step[@"watchSnapshot"]];
  417. } else if (step[@"watchStreamClose"]) {
  418. [self doWatchStreamClose:step[@"watchStreamClose"]];
  419. } else if (step[@"watchProto"]) {
  420. // watchProto isn't yet used, and it's unclear how to create arbitrary protos from JSON.
  421. HARD_FAIL("watchProto is not yet supported.");
  422. } else if (step[@"writeAck"]) {
  423. [self doWriteAck:step[@"writeAck"]];
  424. } else if (step[@"failWrite"]) {
  425. [self doFailWrite:step[@"failWrite"]];
  426. } else if (step[@"runTimer"]) {
  427. [self doRunTimer:step[@"runTimer"]];
  428. } else if (step[@"enableNetwork"]) {
  429. if ([step[@"enableNetwork"] boolValue]) {
  430. [self doEnableNetwork];
  431. } else {
  432. [self doDisableNetwork];
  433. }
  434. } else if (step[@"changeUser"]) {
  435. [self doChangeUser:step[@"changeUser"]];
  436. } else if (step[@"restart"]) {
  437. [self doRestart];
  438. } else if (step[@"applyClientState"]) {
  439. XCTFail(@"'applyClientState' is not supported on iOS and should only be used in multi-client "
  440. @"tests");
  441. } else {
  442. XCTFail(@"Unknown step: %@", step);
  443. }
  444. }
  445. - (void)validateEvent:(FSTQueryEvent *)actual matches:(NSDictionary *)expected {
  446. FSTQuery *expectedQuery = [self parseQuery:expected[@"query"]];
  447. XCTAssertEqualObjects(actual.query, expectedQuery);
  448. if ([expected[@"errorCode"] integerValue] != 0) {
  449. XCTAssertNotNil(actual.error);
  450. XCTAssertEqual(actual.error.code, [expected[@"errorCode"] integerValue]);
  451. } else {
  452. std::vector<DocumentViewChange> expectedChanges;
  453. NSMutableArray *removed = expected[@"removed"];
  454. for (NSDictionary *changeSpec in removed) {
  455. expectedChanges.push_back([self parseChange:changeSpec
  456. ofType:DocumentViewChange::Type::kRemoved]);
  457. }
  458. NSMutableArray *added = expected[@"added"];
  459. for (NSDictionary *changeSpec in added) {
  460. expectedChanges.push_back([self parseChange:changeSpec
  461. ofType:DocumentViewChange::Type::kAdded]);
  462. }
  463. NSMutableArray *modified = expected[@"modified"];
  464. for (NSDictionary *changeSpec in modified) {
  465. expectedChanges.push_back([self parseChange:changeSpec
  466. ofType:DocumentViewChange::Type::kModified]);
  467. }
  468. NSMutableArray *metadata = expected[@"metadata"];
  469. for (NSDictionary *changeSpec in metadata) {
  470. expectedChanges.push_back([self parseChange:changeSpec
  471. ofType:DocumentViewChange::Type::kMetadata]);
  472. }
  473. XCTAssertEqual(actual.viewSnapshot.value().document_changes().size(), expectedChanges.size());
  474. for (size_t i = 0; i != expectedChanges.size(); ++i) {
  475. XCTAssertTrue((actual.viewSnapshot.value().document_changes()[i] == expectedChanges[i]));
  476. }
  477. BOOL expectedHasPendingWrites =
  478. expected[@"hasPendingWrites"] ? [expected[@"hasPendingWrites"] boolValue] : NO;
  479. BOOL expectedIsFromCache = expected[@"fromCache"] ? [expected[@"fromCache"] boolValue] : NO;
  480. XCTAssertEqual(actual.viewSnapshot.value().has_pending_writes(), expectedHasPendingWrites,
  481. @"hasPendingWrites");
  482. XCTAssertEqual(actual.viewSnapshot.value().from_cache(), expectedIsFromCache, @"isFromCache");
  483. }
  484. }
  485. - (void)validateStepExpectations:(NSArray *_Nullable)stepExpectations {
  486. NSArray<FSTQueryEvent *> *events = self.driver.capturedEventsSinceLastCall;
  487. if (!stepExpectations) {
  488. XCTAssertEqual(events.count, 0);
  489. for (FSTQueryEvent *event in events) {
  490. XCTFail(@"Unexpected event: %@", event);
  491. }
  492. return;
  493. }
  494. XCTAssertEqual(events.count, stepExpectations.count);
  495. events =
  496. [events sortedArrayUsingComparator:^NSComparisonResult(FSTQueryEvent *q1, FSTQueryEvent *q2) {
  497. return [q1.query.canonicalID compare:q2.query.canonicalID];
  498. }];
  499. stepExpectations = [stepExpectations
  500. sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *left, NSDictionary *right) {
  501. FSTQuery *leftQuery = [self parseQuery:left[@"query"]];
  502. FSTQuery *rightQuery = [self parseQuery:right[@"query"]];
  503. return [leftQuery.canonicalID compare:rightQuery.canonicalID];
  504. }];
  505. NSUInteger i = 0;
  506. for (; i < stepExpectations.count && i < events.count; ++i) {
  507. [self validateEvent:events[i] matches:stepExpectations[i]];
  508. }
  509. for (; i < stepExpectations.count; ++i) {
  510. XCTFail(@"Missing event: %@", stepExpectations[i]);
  511. }
  512. for (; i < events.count; ++i) {
  513. XCTFail(@"Unexpected event: %@", events[i]);
  514. }
  515. }
  516. - (void)validateStateExpectations:(nullable NSDictionary *)expected {
  517. if (expected) {
  518. if (expected[@"numOutstandingWrites"]) {
  519. XCTAssertEqual([self.driver sentWritesCount], [expected[@"numOutstandingWrites"] intValue]);
  520. }
  521. if (expected[@"writeStreamRequestCount"]) {
  522. XCTAssertEqual([self.driver writeStreamRequestCount],
  523. [expected[@"writeStreamRequestCount"] intValue]);
  524. }
  525. if (expected[@"watchStreamRequestCount"]) {
  526. XCTAssertEqual([self.driver watchStreamRequestCount],
  527. [expected[@"watchStreamRequestCount"] intValue]);
  528. }
  529. if (expected[@"limboDocs"]) {
  530. DocumentKeySet expectedLimboDocuments;
  531. NSArray *docNames = expected[@"limboDocs"];
  532. for (NSString *name in docNames) {
  533. expectedLimboDocuments = expectedLimboDocuments.insert(FSTTestDocKey(name));
  534. }
  535. // Update the expected limbo documents
  536. [self.driver setExpectedLimboDocuments:std::move(expectedLimboDocuments)];
  537. }
  538. if (expected[@"activeTargets"]) {
  539. __block std::unordered_map<TargetId, FSTQueryData *> expectedActiveTargets;
  540. [expected[@"activeTargets"] enumerateKeysAndObjectsUsingBlock:^(NSString *targetIDString,
  541. NSDictionary *queryData,
  542. BOOL *stop) {
  543. TargetId targetID = [targetIDString intValue];
  544. FSTQuery *query = [self parseQuery:queryData[@"query"]];
  545. NSData *resumeToken = [queryData[@"resumeToken"] dataUsingEncoding:NSUTF8StringEncoding];
  546. // TODO(mcg): populate the purpose of the target once it's possible to encode that in the
  547. // spec tests. For now, hard-code that it's a listen despite the fact that it's not always
  548. // the right value.
  549. expectedActiveTargets[targetID] =
  550. [[FSTQueryData alloc] initWithQuery:query
  551. targetID:targetID
  552. listenSequenceNumber:0
  553. purpose:FSTQueryPurposeListen
  554. snapshotVersion:SnapshotVersion::None()
  555. resumeToken:resumeToken];
  556. }];
  557. [self.driver setExpectedActiveTargets:expectedActiveTargets];
  558. }
  559. }
  560. // Always validate the we received the expected number of callbacks.
  561. [self validateUserCallbacks:expected];
  562. // Always validate that the expected limbo docs match the actual limbo docs.
  563. [self validateLimboDocuments];
  564. // Always validate that the expected active targets match the actual active targets.
  565. [self validateActiveTargets];
  566. }
  567. - (void)validateUserCallbacks:(nullable NSDictionary *)expected {
  568. NSDictionary *expectedCallbacks = expected[@"userCallbacks"];
  569. NSArray<NSString *> *actualAcknowledgedDocs =
  570. [self.driver capturedAcknowledgedWritesSinceLastCall];
  571. NSArray<NSString *> *actualRejectedDocs = [self.driver capturedRejectedWritesSinceLastCall];
  572. if (expectedCallbacks) {
  573. XCTAssertTrue([actualAcknowledgedDocs isEqualToArray:expectedCallbacks[@"acknowledgedDocs"]]);
  574. XCTAssertTrue([actualRejectedDocs isEqualToArray:expectedCallbacks[@"rejectedDocs"]]);
  575. } else {
  576. XCTAssertEqual([actualAcknowledgedDocs count], 0);
  577. XCTAssertEqual([actualRejectedDocs count], 0);
  578. }
  579. }
  580. - (void)validateLimboDocuments {
  581. // Make a copy so it can modified while checking against the expected limbo docs.
  582. std::map<DocumentKey, TargetId> actualLimboDocs = self.driver.currentLimboDocuments;
  583. // Validate that each limbo doc has an expected active target
  584. for (const auto &kv : actualLimboDocs) {
  585. const auto &expected = [self.driver expectedActiveTargets];
  586. XCTAssertTrue(expected.find(kv.second) != expected.end(),
  587. @"Found limbo doc without an expected active target");
  588. }
  589. for (const DocumentKey &expectedLimboDoc : self.driver.expectedLimboDocuments) {
  590. XCTAssert(actualLimboDocs.find(expectedLimboDoc) != actualLimboDocs.end(),
  591. @"Expected doc to be in limbo, but was not: %s", expectedLimboDoc.ToString().c_str());
  592. actualLimboDocs.erase(expectedLimboDoc);
  593. }
  594. XCTAssertTrue(actualLimboDocs.empty(), "%lu Unexpected docs in limbo, the first one is <%s, %d>",
  595. actualLimboDocs.size(), actualLimboDocs.begin()->first.ToString().c_str(),
  596. actualLimboDocs.begin()->second);
  597. }
  598. - (void)validateActiveTargets {
  599. if (!_networkEnabled) {
  600. return;
  601. }
  602. // Create a copy so we can modify it in tests
  603. std::unordered_map<TargetId, FSTQueryData *> actualTargets = [self.driver activeTargets];
  604. for (const auto &kv : [self.driver activeTargets]) {
  605. TargetId targetID = kv.first;
  606. FSTQueryData *queryData = kv.second;
  607. XCTAssertNotNil(actualTargets[targetID], @"Expected active target not found: %@", queryData);
  608. // TODO(mcg): validate the purpose of the target once it's possible to encode that in the
  609. // spec tests. For now, only validate properties that can be validated.
  610. // XCTAssertEqualObjects(actualTargets[targetID], queryData);
  611. FSTQueryData *actual = actualTargets[targetID];
  612. XCTAssertNotNil(actual);
  613. if (actual) {
  614. XCTAssertEqualObjects(actual.query, queryData.query);
  615. XCTAssertEqual(actual.targetID, queryData.targetID);
  616. XCTAssertEqual(actual.snapshotVersion, queryData.snapshotVersion);
  617. XCTAssertEqualObjects(Describe(actual.resumeToken), Describe(queryData.resumeToken));
  618. }
  619. actualTargets.erase(targetID);
  620. }
  621. XCTAssertTrue(actualTargets.empty(), "Unexpected active targets: %@",
  622. objc::Description(actualTargets));
  623. }
  624. - (void)runSpecTestSteps:(NSArray *)steps config:(NSDictionary *)config {
  625. @try {
  626. [self setUpForSpecWithConfig:config];
  627. for (NSDictionary *step in steps) {
  628. LOG_DEBUG("Doing step %s", step);
  629. [self doStep:step];
  630. [self validateStepExpectations:step[@"expect"]];
  631. [self validateStateExpectations:step[@"stateExpect"]];
  632. }
  633. [self.driver validateUsage];
  634. } @finally {
  635. // Ensure that the driver is torn down even if the test is failing due to a thrown exception so
  636. // that any resources held by the driver are released. This is important when the driver is
  637. // backed by LevelDB because LevelDB locks its database. If -tearDownForSpec were not called
  638. // after an exception then subsequent attempts to open the LevelDB will fail, making it harder
  639. // to zero in on the spec tests as a culprit.
  640. [self tearDownForSpec];
  641. }
  642. }
  643. #pragma mark - The actual test methods.
  644. - (void)testSpecTests {
  645. if ([self isTestBaseClass]) return;
  646. // Enumerate the .json files containing the spec tests.
  647. NSMutableArray<NSString *> *specFiles = [NSMutableArray array];
  648. NSMutableArray<NSDictionary *> *parsedSpecs = [NSMutableArray array];
  649. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  650. NSFileManager *fs = [NSFileManager defaultManager];
  651. BOOL exclusiveMode = NO;
  652. for (NSString *file in [fs enumeratorAtPath:[bundle resourcePath]]) {
  653. if (![@"json" isEqual:[file pathExtension]]) {
  654. continue;
  655. }
  656. // Read and parse the JSON from the file.
  657. NSString *fileName = [file stringByDeletingPathExtension];
  658. NSString *path = [bundle pathForResource:fileName ofType:@"json"];
  659. NSData *json = [NSData dataWithContentsOfFile:path];
  660. XCTAssertNotNil(json);
  661. NSError *error = nil;
  662. id _Nullable parsed = [NSJSONSerialization JSONObjectWithData:json options:0 error:&error];
  663. XCTAssertNil(error, @"%@", error);
  664. XCTAssertTrue([parsed isKindOfClass:[NSDictionary class]]);
  665. NSDictionary *testDict = (NSDictionary *)parsed;
  666. exclusiveMode = exclusiveMode || [self anyTestsAreMarkedExclusive:testDict];
  667. [specFiles addObject:fileName];
  668. [parsedSpecs addObject:testDict];
  669. }
  670. // Now iterate over them and run them.
  671. __block bool ranAtLeastOneTest = NO;
  672. for (NSUInteger i = 0; i < specFiles.count; i++) {
  673. NSLog(@"Spec test file: %@", specFiles[i]);
  674. // Iterate over the tests in the file and run them.
  675. [parsedSpecs[i] enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  676. XCTAssertTrue([obj isKindOfClass:[NSDictionary class]]);
  677. NSDictionary *testDescription = (NSDictionary *)obj;
  678. NSString *describeName = testDescription[@"describeName"];
  679. NSString *itName = testDescription[@"itName"];
  680. NSString *name = [NSString stringWithFormat:@"%@ %@", describeName, itName];
  681. NSDictionary *config = testDescription[@"config"];
  682. NSArray *steps = testDescription[@"steps"];
  683. NSArray<NSString *> *tags = testDescription[@"tags"];
  684. BOOL runTest = !exclusiveMode || [tags indexOfObject:kExclusiveTag] != NSNotFound;
  685. if (runTest) {
  686. runTest = [self shouldRunWithTags:tags];
  687. }
  688. if (runTest) {
  689. NSLog(@" Spec test: %@", name);
  690. [self runSpecTestSteps:steps config:config];
  691. ranAtLeastOneTest = YES;
  692. } else {
  693. NSLog(@" [SKIPPED] Spec test: %@", name);
  694. NSString *comment = testDescription[@"comment"];
  695. if (comment) {
  696. NSLog(@" %@", comment);
  697. }
  698. }
  699. }];
  700. }
  701. XCTAssertTrue(ranAtLeastOneTest);
  702. }
  703. - (BOOL)anyTestsAreMarkedExclusive:(NSDictionary *)tests {
  704. __block BOOL found = NO;
  705. [tests enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  706. XCTAssertTrue([obj isKindOfClass:[NSDictionary class]]);
  707. NSDictionary *testDescription = (NSDictionary *)obj;
  708. NSArray<NSString *> *tags = testDescription[@"tags"];
  709. if ([tags indexOfObject:kExclusiveTag] != NSNotFound) {
  710. found = YES;
  711. *stop = YES;
  712. }
  713. }];
  714. return found;
  715. }
  716. @end
  717. NS_ASSUME_NONNULL_END