FSTSpecTests.mm 32 KB

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