FSTSpecTests.mm 46 KB

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