FSTSpecTests.mm 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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 kEagerGC = @"eager-gc";
  66. NSString *const kDurablePersistence = @"durable-persistence";
  67. static NSString *Describe(NSData *data) {
  68. return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  69. }
  70. @interface FSTSpecTests ()
  71. @property(nonatomic, strong) FSTSyncEngineTestDriver *driver;
  72. @end
  73. @implementation FSTSpecTests {
  74. BOOL _gcEnabled;
  75. BOOL _networkEnabled;
  76. }
  77. - (id<FSTPersistence>)persistenceWithGCEnabled:(BOOL)GCEnabled {
  78. @throw FSTAbstractMethodException(); // NOLINT
  79. }
  80. - (BOOL)shouldRunWithTags:(NSArray<NSString *> *)tags {
  81. if ([tags containsObject:kNoIOSTag]) {
  82. return NO;
  83. } else if ([tags containsObject:kMultiClientTag]) {
  84. return NO;
  85. } else if (!kRunBenchmarkTests && [tags containsObject:kBenchmarkTag]) {
  86. return NO;
  87. }
  88. return YES;
  89. }
  90. - (void)setUpForSpecWithConfig:(NSDictionary *)config {
  91. // Store GCEnabled so we can re-use it in doRestart.
  92. NSNumber *GCEnabled = config[@"useGarbageCollection"];
  93. _gcEnabled = [GCEnabled boolValue];
  94. NSNumber *numClients = config[@"numClients"];
  95. if (numClients) {
  96. XCTAssertEqualObjects(numClients, @1, @"The iOS client does not support multi-client tests");
  97. }
  98. id<FSTPersistence> persistence = [self persistenceWithGCEnabled:_gcEnabled];
  99. self.driver = [[FSTSyncEngineTestDriver alloc] initWithPersistence:persistence];
  100. [self.driver start];
  101. }
  102. - (void)tearDownForSpec {
  103. [self.driver shutdown];
  104. }
  105. /**
  106. * Xcode will run tests from any class that extends XCTestCase, but this doesn't work for
  107. * FSTSpecTests since it is incomplete without the implementations supplied by its subclasses.
  108. */
  109. - (BOOL)isTestBaseClass {
  110. return [self class] == [FSTSpecTests class];
  111. }
  112. #pragma mark - Methods for constructing objects from specs.
  113. - (nullable FSTQuery *)parseQuery:(id)querySpec {
  114. if ([querySpec isKindOfClass:[NSString class]]) {
  115. return FSTTestQuery(util::MakeString((NSString *)querySpec));
  116. } else if ([querySpec isKindOfClass:[NSDictionary class]]) {
  117. NSDictionary *queryDict = (NSDictionary *)querySpec;
  118. NSString *path = queryDict[@"path"];
  119. __block FSTQuery *query = FSTTestQuery(util::MakeString(path));
  120. if (queryDict[@"limit"]) {
  121. NSNumber *limit = queryDict[@"limit"];
  122. query = [query queryBySettingLimit:limit.integerValue];
  123. }
  124. if (queryDict[@"filters"]) {
  125. NSArray *filters = queryDict[@"filters"];
  126. [filters enumerateObjectsUsingBlock:^(NSArray *_Nonnull filter, NSUInteger idx,
  127. BOOL *_Nonnull stop) {
  128. query = [query
  129. queryByAddingFilter:FSTTestFilter(util::MakeString(filter[0]), filter[1], filter[2])];
  130. }];
  131. }
  132. if (queryDict[@"orderBys"]) {
  133. NSArray *orderBys = queryDict[@"orderBys"];
  134. [orderBys enumerateObjectsUsingBlock:^(NSArray *_Nonnull orderBy, NSUInteger idx,
  135. BOOL *_Nonnull stop) {
  136. query =
  137. [query queryByAddingSortOrder:FSTTestOrderBy(util::MakeString(orderBy[0]), orderBy[1])];
  138. }];
  139. }
  140. return query;
  141. } else {
  142. XCTFail(@"Invalid query: %@", querySpec);
  143. return nil;
  144. }
  145. }
  146. - (SnapshotVersion)parseVersion:(NSNumber *_Nullable)version {
  147. return testutil::Version(version.longLongValue);
  148. }
  149. - (FSTDocumentViewChange *)parseChange:(NSArray *)change ofType:(FSTDocumentViewChangeType)type {
  150. BOOL hasMutations = NO;
  151. for (NSUInteger i = 3; i < change.count; ++i) {
  152. if ([change[i] isEqual:@"local"]) {
  153. hasMutations = YES;
  154. }
  155. }
  156. NSNumber *version = change[1];
  157. XCTAssert([change[0] isKindOfClass:[NSString class]]);
  158. FSTDocument *doc = FSTTestDoc(util::MakeString((NSString *)change[0]), version.longLongValue,
  159. change[2], hasMutations);
  160. return [FSTDocumentViewChange changeWithDocument:doc type:type];
  161. }
  162. #pragma mark - Methods for doing the steps of the spec test.
  163. - (void)doListen:(NSArray *)listenSpec {
  164. FSTQuery *query = [self parseQuery:listenSpec[1]];
  165. TargetId actualID = [self.driver addUserListenerWithQuery:query];
  166. TargetId expectedID = [listenSpec[0] intValue];
  167. XCTAssertEqual(actualID, expectedID, @"targetID assigned to listen");
  168. }
  169. - (void)doUnlisten:(NSArray *)unlistenSpec {
  170. FSTQuery *query = [self parseQuery:unlistenSpec[1]];
  171. [self.driver removeUserListenerWithQuery:query];
  172. }
  173. - (void)doSet:(NSArray *)setSpec {
  174. [self.driver writeUserMutation:FSTTestSetMutation(setSpec[0], setSpec[1])];
  175. }
  176. - (void)doPatch:(NSArray *)patchSpec {
  177. [self.driver
  178. writeUserMutation:FSTTestPatchMutation(util::MakeString(patchSpec[0]), patchSpec[1], {})];
  179. }
  180. - (void)doDelete:(NSString *)key {
  181. [self.driver writeUserMutation:FSTTestDeleteMutation(key)];
  182. }
  183. - (void)doWatchAck:(NSArray<NSNumber *> *)ackedTargets {
  184. FSTWatchTargetChange *change =
  185. [FSTWatchTargetChange changeWithState:FSTWatchTargetChangeStateAdded
  186. targetIDs:ackedTargets
  187. cause:nil];
  188. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  189. }
  190. - (void)doWatchCurrent:(NSArray<id> *)currentSpec {
  191. NSArray<NSNumber *> *currentTargets = currentSpec[0];
  192. NSData *resumeToken = [currentSpec[1] dataUsingEncoding:NSUTF8StringEncoding];
  193. FSTWatchTargetChange *change =
  194. [FSTWatchTargetChange changeWithState:FSTWatchTargetChangeStateCurrent
  195. targetIDs:currentTargets
  196. resumeToken:resumeToken];
  197. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  198. }
  199. - (void)doWatchRemove:(NSDictionary *)watchRemoveSpec {
  200. NSError *error = nil;
  201. NSDictionary *cause = watchRemoveSpec[@"cause"];
  202. if (cause) {
  203. int code = ((NSNumber *)cause[@"code"]).intValue;
  204. NSDictionary *userInfo = @{
  205. NSLocalizedDescriptionKey : @"Error from watchRemove.",
  206. };
  207. error = [NSError errorWithDomain:FIRFirestoreErrorDomain code:code userInfo:userInfo];
  208. }
  209. FSTWatchTargetChange *change =
  210. [FSTWatchTargetChange changeWithState:FSTWatchTargetChangeStateRemoved
  211. targetIDs:watchRemoveSpec[@"targetIds"]
  212. cause:error];
  213. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  214. // Unlike web, the FSTMockDatastore detects a watch removal with cause and will remove active
  215. // targets
  216. }
  217. - (void)doWatchEntity:(NSDictionary *)watchEntity {
  218. if (watchEntity[@"docs"]) {
  219. HARD_ASSERT(!watchEntity[@"doc"], "Exactly one of |doc| or |docs| needs to be set.");
  220. NSArray *docs = watchEntity[@"docs"];
  221. for (NSDictionary *doc in docs) {
  222. NSMutableDictionary *watchSpec = [NSMutableDictionary dictionary];
  223. watchSpec[@"doc"] = doc;
  224. if (watchEntity[@"targets"]) {
  225. watchSpec[@"targets"] = watchEntity[@"targets"];
  226. }
  227. if (watchEntity[@"removedTargets"]) {
  228. watchSpec[@"removedTargets"] = watchEntity[@"removedTargets"];
  229. }
  230. [self doWatchEntity:watchSpec];
  231. }
  232. } else if (watchEntity[@"doc"]) {
  233. NSArray *docSpec = watchEntity[@"doc"];
  234. FSTDocumentKey *key = FSTTestDocKey(docSpec[0]);
  235. FSTObjectValue *_Nullable value =
  236. [docSpec[2] isKindOfClass:[NSNull class]] ? nil : FSTTestObjectValue(docSpec[2]);
  237. SnapshotVersion version = [self parseVersion:docSpec[1]];
  238. FSTMaybeDocument *doc =
  239. value ? [FSTDocument documentWithData:value
  240. key:key
  241. version:std::move(version)
  242. hasLocalMutations:NO]
  243. : [FSTDeletedDocument documentWithKey:key version:std::move(version)];
  244. FSTWatchChange *change =
  245. [[FSTDocumentWatchChange alloc] initWithUpdatedTargetIDs:watchEntity[@"targets"]
  246. removedTargetIDs:watchEntity[@"removedTargets"]
  247. documentKey:doc.key
  248. document:doc];
  249. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  250. } else if (watchEntity[@"key"]) {
  251. FSTDocumentKey *docKey = FSTTestDocKey(watchEntity[@"key"]);
  252. FSTWatchChange *change =
  253. [[FSTDocumentWatchChange alloc] initWithUpdatedTargetIDs:@[]
  254. removedTargetIDs:watchEntity[@"removedTargets"]
  255. documentKey:docKey
  256. document:nil];
  257. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  258. } else {
  259. HARD_FAIL("Either key, doc or docs must be set.");
  260. }
  261. }
  262. - (void)doWatchFilter:(NSArray *)watchFilter {
  263. NSArray<NSNumber *> *targets = watchFilter[0];
  264. HARD_ASSERT(targets.count == 1, "ExistenceFilters currently support exactly one target only.");
  265. int keyCount = watchFilter.count == 0 ? 0 : (int)watchFilter.count - 1;
  266. // TODO(dimond): extend this with different existence filters over time.
  267. FSTExistenceFilter *filter = [FSTExistenceFilter filterWithCount:keyCount];
  268. FSTExistenceFilterWatchChange *change =
  269. [FSTExistenceFilterWatchChange changeWithFilter:filter targetID:targets[0].intValue];
  270. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  271. }
  272. - (void)doWatchReset:(NSArray<NSNumber *> *)watchReset {
  273. FSTWatchTargetChange *change =
  274. [FSTWatchTargetChange changeWithState:FSTWatchTargetChangeStateReset
  275. targetIDs:watchReset
  276. cause:nil];
  277. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  278. }
  279. - (void)doWatchSnapshot:(NSDictionary *)watchSnapshot {
  280. // The client will only respond to watchSnapshots if they are on a target change with an empty
  281. // set of target IDs.
  282. NSArray<NSNumber *> *targetIDs =
  283. watchSnapshot[@"targetIds"] ? watchSnapshot[@"targetIds"] : [NSArray array];
  284. NSData *resumeToken = [watchSnapshot[@"resumeToken"] dataUsingEncoding:NSUTF8StringEncoding];
  285. FSTWatchTargetChange *change =
  286. [FSTWatchTargetChange changeWithState:FSTWatchTargetChangeStateNoChange
  287. targetIDs:targetIDs
  288. resumeToken:resumeToken];
  289. [self.driver receiveWatchChange:change
  290. snapshotVersion:[self parseVersion:watchSnapshot[@"version"]]];
  291. }
  292. - (void)doWatchStreamClose:(NSDictionary *)closeSpec {
  293. NSDictionary *errorSpec = closeSpec[@"error"];
  294. int code = ((NSNumber *)(errorSpec[@"code"])).intValue;
  295. NSNumber *runBackoffTimer = closeSpec[@"runBackoffTimer"];
  296. // TODO(b/72313632): Incorporate backoff in iOS Spec Tests.
  297. HARD_ASSERT(runBackoffTimer.boolValue, "iOS Spec Tests don't support backoff.");
  298. [self.driver receiveWatchStreamError:code userInfo:errorSpec];
  299. }
  300. - (void)doWriteAck:(NSDictionary *)spec {
  301. SnapshotVersion version = [self parseVersion:spec[@"version"]];
  302. NSNumber *keepInQueue = spec[@"keepInQueue"];
  303. XCTAssertTrue(keepInQueue == nil || keepInQueue.boolValue == NO,
  304. @"'keepInQueue=true' is not supported on iOS and should only be set in "
  305. @"multi-client tests");
  306. FSTMutationResult *mutationResult =
  307. [[FSTMutationResult alloc] initWithVersion:version transformResults:nil];
  308. [self.driver receiveWriteAckWithVersion:version mutationResults:@[ mutationResult ]];
  309. }
  310. - (void)doFailWrite:(NSDictionary *)spec {
  311. NSDictionary *errorSpec = spec[@"error"];
  312. NSNumber *keepInQueue = spec[@"keepInQueue"];
  313. int code = ((NSNumber *)(errorSpec[@"code"])).intValue;
  314. [self.driver receiveWriteError:code userInfo:errorSpec keepInQueue:keepInQueue.boolValue];
  315. }
  316. - (void)doDrainQueue {
  317. [self.driver drainQueue];
  318. }
  319. - (void)doRunTimer:(NSString *)timer {
  320. FSTTimerID timerID;
  321. if ([timer isEqualToString:@"all"]) {
  322. timerID = FSTTimerIDAll;
  323. } else if ([timer isEqualToString:@"listen_stream_idle"]) {
  324. timerID = FSTTimerIDListenStreamIdle;
  325. } else if ([timer isEqualToString:@"listen_stream_connection_backoff"]) {
  326. timerID = FSTTimerIDListenStreamConnectionBackoff;
  327. } else if ([timer isEqualToString:@"write_stream_idle"]) {
  328. timerID = FSTTimerIDWriteStreamIdle;
  329. } else if ([timer isEqualToString:@"write_stream_connection_backoff"]) {
  330. timerID = FSTTimerIDWriteStreamConnectionBackoff;
  331. } else if ([timer isEqualToString:@"online_state_timeout"]) {
  332. timerID = FSTTimerIDOnlineStateTimeout;
  333. } else {
  334. HARD_FAIL("runTimer spec step specified unknown timer: %s", timer);
  335. }
  336. [self.driver runTimer:timerID];
  337. }
  338. - (void)doDisableNetwork {
  339. _networkEnabled = NO;
  340. [self.driver disableNetwork];
  341. }
  342. - (void)doEnableNetwork {
  343. _networkEnabled = YES;
  344. [self.driver enableNetwork];
  345. }
  346. - (void)doChangeUser:(nullable id)UID {
  347. if ([UID isEqual:[NSNull null]]) {
  348. UID = nil;
  349. }
  350. [self.driver changeUser:User::FromUid(UID)];
  351. }
  352. - (void)doRestart {
  353. // Any outstanding user writes should be automatically re-sent, so we want to preserve them
  354. // when re-creating the driver.
  355. FSTOutstandingWriteQueues outstandingWrites = self.driver.outstandingWrites;
  356. User currentUser = self.driver.currentUser;
  357. [self.driver shutdown];
  358. id<FSTPersistence> persistence = [self persistenceWithGCEnabled:_gcEnabled];
  359. self.driver = [[FSTSyncEngineTestDriver alloc] initWithPersistence:persistence
  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. if (!_networkEnabled) {
  573. return;
  574. }
  575. // Create a copy so we can modify it in tests
  576. NSMutableDictionary<FSTBoxedTargetID *, FSTQueryData *> *actualTargets =
  577. [NSMutableDictionary dictionaryWithDictionary:self.driver.activeTargets];
  578. [self.driver.expectedActiveTargets enumerateKeysAndObjectsUsingBlock:^(FSTBoxedTargetID *targetID,
  579. FSTQueryData *queryData,
  580. BOOL *stop) {
  581. XCTAssertNotNil(actualTargets[targetID], @"Expected active target not found: %@", queryData);
  582. // TODO(mcg): validate the purpose of the target once it's possible to encode that in the
  583. // spec tests. For now, only validate properties that can be validated.
  584. // XCTAssertEqualObjects(actualTargets[targetID], queryData);
  585. FSTQueryData *actual = actualTargets[targetID];
  586. XCTAssertNotNil(actual);
  587. if (actual) {
  588. XCTAssertEqualObjects(actual.query, queryData.query);
  589. XCTAssertEqual(actual.targetID, queryData.targetID);
  590. XCTAssertEqual(actual.snapshotVersion, queryData.snapshotVersion);
  591. XCTAssertEqualObjects(Describe(actual.resumeToken), Describe(queryData.resumeToken));
  592. }
  593. [actualTargets removeObjectForKey:targetID];
  594. }];
  595. XCTAssertTrue(actualTargets.count == 0, "Unexpected active targets: %@", actualTargets);
  596. }
  597. - (void)runSpecTestSteps:(NSArray *)steps config:(NSDictionary *)config {
  598. @try {
  599. [self setUpForSpecWithConfig:config];
  600. for (NSDictionary *step in steps) {
  601. LOG_DEBUG("Doing step %s", step);
  602. [self doStep:step];
  603. [self validateStepExpectations:step[@"expect"]];
  604. [self validateStateExpectations:step[@"stateExpect"]];
  605. }
  606. [self.driver validateUsage];
  607. } @finally {
  608. // Ensure that the driver is torn down even if the test is failing due to a thrown exception so
  609. // that any resources held by the driver are released. This is important when the driver is
  610. // backed by LevelDB because LevelDB locks its database. If -tearDownForSpec were not called
  611. // after an exception then subsequent attempts to open the LevelDB will fail, making it harder
  612. // to zero in on the spec tests as a culprit.
  613. [self tearDownForSpec];
  614. }
  615. }
  616. #pragma mark - The actual test methods.
  617. - (void)testSpecTests {
  618. if ([self isTestBaseClass]) return;
  619. // Enumerate the .json files containing the spec tests.
  620. NSMutableArray<NSString *> *specFiles = [NSMutableArray array];
  621. NSMutableArray<NSDictionary *> *parsedSpecs = [NSMutableArray array];
  622. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  623. NSFileManager *fs = [NSFileManager defaultManager];
  624. BOOL exclusiveMode = NO;
  625. for (NSString *file in [fs enumeratorAtPath:[bundle bundlePath]]) {
  626. if (![@"json" isEqual:[file pathExtension]]) {
  627. continue;
  628. }
  629. // Read and parse the JSON from the file.
  630. NSString *fileName = [file stringByDeletingPathExtension];
  631. NSString *path = [bundle pathForResource:fileName ofType:@"json"];
  632. NSData *json = [NSData dataWithContentsOfFile:path];
  633. XCTAssertNotNil(json);
  634. NSError *error = nil;
  635. id _Nullable parsed = [NSJSONSerialization JSONObjectWithData:json options:0 error:&error];
  636. XCTAssertNil(error, @"%@", error);
  637. XCTAssertTrue([parsed isKindOfClass:[NSDictionary class]]);
  638. NSDictionary *testDict = (NSDictionary *)parsed;
  639. exclusiveMode = exclusiveMode || [self anyTestsAreMarkedExclusive:testDict];
  640. [specFiles addObject:fileName];
  641. [parsedSpecs addObject:testDict];
  642. }
  643. // Now iterate over them and run them.
  644. __block bool ranAtLeastOneTest = NO;
  645. for (NSUInteger i = 0; i < specFiles.count; i++) {
  646. NSLog(@"Spec test file: %@", specFiles[i]);
  647. // Iterate over the tests in the file and run them.
  648. [parsedSpecs[i] enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  649. XCTAssertTrue([obj isKindOfClass:[NSDictionary class]]);
  650. NSDictionary *testDescription = (NSDictionary *)obj;
  651. NSString *describeName = testDescription[@"describeName"];
  652. NSString *itName = testDescription[@"itName"];
  653. NSString *name = [NSString stringWithFormat:@"%@ %@", describeName, itName];
  654. NSDictionary *config = testDescription[@"config"];
  655. NSArray *steps = testDescription[@"steps"];
  656. NSArray<NSString *> *tags = testDescription[@"tags"];
  657. BOOL runTest = !exclusiveMode || [tags indexOfObject:kExclusiveTag] != NSNotFound;
  658. if (runTest) {
  659. runTest = [self shouldRunWithTags:tags];
  660. }
  661. if (runTest) {
  662. NSLog(@" Spec test: %@", name);
  663. [self runSpecTestSteps:steps config:config];
  664. ranAtLeastOneTest = YES;
  665. } else {
  666. NSLog(@" [SKIPPED] Spec test: %@", name);
  667. NSString *comment = testDescription[@"comment"];
  668. if (comment) {
  669. NSLog(@" %@", comment);
  670. }
  671. }
  672. }];
  673. }
  674. XCTAssertTrue(ranAtLeastOneTest);
  675. }
  676. - (BOOL)anyTestsAreMarkedExclusive:(NSDictionary *)tests {
  677. __block BOOL found = NO;
  678. [tests enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  679. XCTAssertTrue([obj isKindOfClass:[NSDictionary class]]);
  680. NSDictionary *testDescription = (NSDictionary *)obj;
  681. NSArray<NSString *> *tags = testDescription[@"tags"];
  682. if ([tags indexOfObject:kExclusiveTag] != NSNotFound) {
  683. found = YES;
  684. *stop = YES;
  685. }
  686. }];
  687. return found;
  688. }
  689. @end
  690. NS_ASSUME_NONNULL_END