SwiftObjectServerTests.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. import XCTest
  19. import RealmSwift
  20. // Used by testOfflineClientReset
  21. // The naming here is nonstandard as the sync-1.x.realm test file comes from the .NET unit tests.
  22. // swiftlint:disable identifier_name
  23. @objc(Person)
  24. class Person: Object {
  25. @objc dynamic var FirstName: String?
  26. @objc dynamic var LastName: String?
  27. override class func shouldIncludeInDefaultSchema() -> Bool { return false }
  28. }
  29. class SwiftObjectServerTests: SwiftSyncTestCase {
  30. /// It should be possible to successfully open a Realm configured for sync.
  31. func testBasicSwiftSync() {
  32. let url = URL(string: "realm://127.0.0.1:9080/~/testBasicSync")!
  33. do {
  34. let user = try synchronouslyLogInUser(for: basicCredentials(register: true), server: authURL)
  35. let realm = try synchronouslyOpenRealm(url: url, user: user)
  36. XCTAssert(realm.isEmpty, "Freshly synced Realm was not empty...")
  37. } catch {
  38. XCTFail("Got an error: \(error)")
  39. }
  40. }
  41. /// If client B adds objects to a Realm, client A should see those new objects.
  42. func testSwiftAddObjects() {
  43. do {
  44. let user = try synchronouslyLogInUser(for: basicCredentials(register: isParent), server: authURL)
  45. let realm = try synchronouslyOpenRealm(url: realmURL, user: user)
  46. if isParent {
  47. waitForDownloads(for: realm)
  48. checkCount(expected: 0, realm, SwiftSyncObject.self)
  49. executeChild()
  50. waitForDownloads(for: realm)
  51. checkCount(expected: 3, realm, SwiftSyncObject.self)
  52. } else {
  53. // Add objects
  54. try realm.write {
  55. realm.add(SwiftSyncObject(value: ["child-1"]))
  56. realm.add(SwiftSyncObject(value: ["child-2"]))
  57. realm.add(SwiftSyncObject(value: ["child-3"]))
  58. }
  59. waitForUploads(for: realm)
  60. checkCount(expected: 3, realm, SwiftSyncObject.self)
  61. }
  62. } catch {
  63. XCTFail("Got an error: \(error) (process: \(isParent ? "parent" : "child"))")
  64. }
  65. }
  66. /// If client B removes objects from a Realm, client A should see those changes.
  67. func testSwiftDeleteObjects() {
  68. do {
  69. let user = try synchronouslyLogInUser(for: basicCredentials(register: isParent), server: authURL)
  70. let realm = try synchronouslyOpenRealm(url: realmURL, user: user)
  71. if isParent {
  72. try realm.write {
  73. realm.add(SwiftSyncObject(value: ["child-1"]))
  74. realm.add(SwiftSyncObject(value: ["child-2"]))
  75. realm.add(SwiftSyncObject(value: ["child-3"]))
  76. }
  77. waitForUploads(for: realm)
  78. checkCount(expected: 3, realm, SwiftSyncObject.self)
  79. executeChild()
  80. waitForDownloads(for: realm)
  81. checkCount(expected: 0, realm, SwiftSyncObject.self)
  82. } else {
  83. try realm.write {
  84. realm.deleteAll()
  85. }
  86. waitForUploads(for: realm)
  87. checkCount(expected: 0, realm, SwiftSyncObject.self)
  88. }
  89. } catch {
  90. XCTFail("Got an error: \(error) (process: \(isParent ? "parent" : "child"))")
  91. }
  92. }
  93. func testConnectionState() {
  94. let user = try! synchronouslyLogInUser(for: basicCredentials(register: true), server: authURL)
  95. let realm = try! synchronouslyOpenRealm(url: realmURL, user: user)
  96. let session = realm.syncSession!
  97. func wait(forState desiredState: SyncSession.ConnectionState) {
  98. let ex = expectation(description: "Wait for connection state: \(desiredState)")
  99. let token = session.observe(\SyncSession.connectionState, options: .initial) { s, _ in
  100. if s.connectionState == desiredState {
  101. ex.fulfill()
  102. }
  103. }
  104. waitForExpectations(timeout: 2.0)
  105. token.invalidate()
  106. }
  107. wait(forState: .connected)
  108. session.suspend()
  109. wait(forState: .disconnected)
  110. session.resume()
  111. wait(forState: .connecting)
  112. wait(forState: .connected)
  113. }
  114. // MARK: - Client reset
  115. func testClientReset() {
  116. do {
  117. let user = try synchronouslyLogInUser(for: basicCredentials(register: isParent), server: authURL)
  118. let realm = try synchronouslyOpenRealm(url: realmURL, user: user)
  119. var theError: SyncError?
  120. let ex = expectation(description: "Waiting for error handler to be called...")
  121. SyncManager.shared.errorHandler = { (error, session) in
  122. if let error = error as? SyncError {
  123. theError = error
  124. } else {
  125. XCTFail("Error \(error) was not a sync error. Something is wrong.")
  126. }
  127. ex.fulfill()
  128. }
  129. user.simulateClientResetError(forSession: realmURL)
  130. waitForExpectations(timeout: 10, handler: nil)
  131. XCTAssertNotNil(theError)
  132. XCTAssertTrue(theError!.code == SyncError.Code.clientResetError)
  133. let resetInfo = theError!.clientResetInfo()
  134. XCTAssertNotNil(resetInfo)
  135. XCTAssertTrue(resetInfo!.0.contains("io.realm.object-server-recovered-realms/recovered_realm"))
  136. XCTAssertNotNil(realm)
  137. } catch {
  138. XCTFail("Got an error: \(error) (process: \(isParent ? "parent" : "child"))")
  139. }
  140. }
  141. func testClientResetManualInitiation() {
  142. do {
  143. let user = try synchronouslyLogInUser(for: basicCredentials(register: isParent), server: authURL)
  144. var theError: SyncError?
  145. try autoreleasepool {
  146. let realm = try synchronouslyOpenRealm(url: realmURL, user: user)
  147. let ex = expectation(description: "Waiting for error handler to be called...")
  148. SyncManager.shared.errorHandler = { (error, session) in
  149. if let error = error as? SyncError {
  150. theError = error
  151. } else {
  152. XCTFail("Error \(error) was not a sync error. Something is wrong.")
  153. }
  154. ex.fulfill()
  155. }
  156. user.simulateClientResetError(forSession: realmURL)
  157. waitForExpectations(timeout: 10, handler: nil)
  158. XCTAssertNotNil(theError)
  159. XCTAssertNotNil(realm)
  160. }
  161. let (path, errorToken) = theError!.clientResetInfo()!
  162. XCTAssertFalse(FileManager.default.fileExists(atPath: path))
  163. SyncSession.immediatelyHandleError(errorToken)
  164. XCTAssertTrue(FileManager.default.fileExists(atPath: path))
  165. } catch {
  166. XCTFail("Got an error: \(error) (process: \(isParent ? "parent" : "child"))")
  167. }
  168. }
  169. // MARK: - Progress notifiers
  170. let bigObjectCount = 2
  171. func populateRealm(user: SyncUser, url: URL) {
  172. let realm = try! synchronouslyOpenRealm(url: realmURL, user: user)
  173. try! realm.write {
  174. for _ in 0..<bigObjectCount {
  175. realm.add(SwiftHugeSyncObject())
  176. }
  177. }
  178. waitForUploads(for: realm)
  179. checkCount(expected: bigObjectCount, realm, SwiftHugeSyncObject.self)
  180. }
  181. func testStreamingDownloadNotifier() {
  182. let user = try! synchronouslyLogInUser(for: basicCredentials(register: isParent), server: authURL)
  183. if !isParent {
  184. populateRealm(user: user, url: realmURL)
  185. return
  186. }
  187. var callCount = 0
  188. var transferred = 0
  189. var transferrable = 0
  190. let realm = try! synchronouslyOpenRealm(url: realmURL, user: user)
  191. let session = realm.syncSession
  192. XCTAssertNotNil(session)
  193. let ex = expectation(description: "streaming-downloads-expectation")
  194. var hasBeenFulfilled = false
  195. let token = session!.addProgressNotification(for: .download, mode: .reportIndefinitely) { p in
  196. callCount += 1
  197. XCTAssert(p.transferredBytes >= transferred)
  198. XCTAssert(p.transferrableBytes >= transferrable)
  199. transferred = p.transferredBytes
  200. transferrable = p.transferrableBytes
  201. if p.transferredBytes > 0 && p.isTransferComplete && !hasBeenFulfilled {
  202. ex.fulfill()
  203. hasBeenFulfilled = true
  204. }
  205. }
  206. // Wait for the child process to upload all the data.
  207. executeChild()
  208. waitForExpectations(timeout: 10.0, handler: nil)
  209. token!.invalidate()
  210. XCTAssert(callCount > 1)
  211. XCTAssert(transferred >= transferrable)
  212. }
  213. func testStreamingUploadNotifier() {
  214. do {
  215. var transferred = 0
  216. var transferrable = 0
  217. let user = try synchronouslyLogInUser(for: basicCredentials(register: isParent), server: authURL)
  218. let realm = try synchronouslyOpenRealm(url: realmURL, user: user)
  219. let session = realm.syncSession
  220. XCTAssertNotNil(session)
  221. var ex = expectation(description: "initial upload")
  222. let token = session!.addProgressNotification(for: .upload, mode: .reportIndefinitely) { p in
  223. XCTAssert(p.transferredBytes >= transferred)
  224. XCTAssert(p.transferrableBytes >= transferrable)
  225. transferred = p.transferredBytes
  226. transferrable = p.transferrableBytes
  227. if p.transferredBytes > 0 && p.isTransferComplete {
  228. ex.fulfill()
  229. }
  230. }
  231. waitForExpectations(timeout: 10.0, handler: nil)
  232. ex = expectation(description: "write transaction upload")
  233. try realm.write {
  234. for _ in 0..<bigObjectCount {
  235. realm.add(SwiftHugeSyncObject())
  236. }
  237. }
  238. waitForExpectations(timeout: 10.0, handler: nil)
  239. token!.invalidate()
  240. XCTAssert(transferred >= transferrable)
  241. } catch {
  242. XCTFail("Got an error: \(error) (process: \(isParent ? "parent" : "child"))")
  243. }
  244. }
  245. // MARK: - Download Realm
  246. func testDownloadRealm() {
  247. let user = try! synchronouslyLogInUser(for: basicCredentials(register: isParent), server: authURL)
  248. if !isParent {
  249. populateRealm(user: user, url: realmURL)
  250. return
  251. }
  252. // Wait for the child process to upload everything.
  253. executeChild()
  254. let ex = expectation(description: "download-realm")
  255. let config = user.configuration(realmURL: realmURL, fullSynchronization: true)
  256. let pathOnDisk = ObjectiveCSupport.convert(object: config).pathOnDisk
  257. XCTAssertFalse(FileManager.default.fileExists(atPath: pathOnDisk))
  258. Realm.asyncOpen(configuration: config) { realm, error in
  259. XCTAssertNil(error)
  260. self.checkCount(expected: self.bigObjectCount, realm!, SwiftHugeSyncObject.self)
  261. ex.fulfill()
  262. }
  263. func fileSize(path: String) -> Int {
  264. if let attr = try? FileManager.default.attributesOfItem(atPath: path) {
  265. return attr[.size] as! Int
  266. }
  267. return 0
  268. }
  269. XCTAssertFalse(RLMHasCachedRealmForPath(pathOnDisk))
  270. waitForExpectations(timeout: 10.0, handler: nil)
  271. XCTAssertGreaterThan(fileSize(path: pathOnDisk), 0)
  272. XCTAssertFalse(RLMHasCachedRealmForPath(pathOnDisk))
  273. }
  274. func testCancelDownloadRealm() {
  275. let user = try! synchronouslyLogInUser(for: basicCredentials(register: isParent), server: authURL)
  276. if !isParent {
  277. populateRealm(user: user, url: realmURL)
  278. return
  279. }
  280. // Wait for the child process to upload everything.
  281. executeChild()
  282. // Use a serial queue for asyncOpen to ensure that the first one adds
  283. // the completion block before the second one cancels it
  284. RLMSetAsyncOpenQueue(DispatchQueue(label: "io.realm.asyncOpen"))
  285. let ex = expectation(description: "async open")
  286. let config = user.configuration(realmURL: realmURL, fullSynchronization: true)
  287. Realm.asyncOpen(configuration: config) { _, error in
  288. XCTAssertNotNil(error)
  289. ex.fulfill()
  290. }
  291. let task = Realm.asyncOpen(configuration: config) { _, _ in
  292. XCTFail("Cancelled completion handler was called")
  293. }
  294. task.cancel()
  295. waitForExpectations(timeout: 10.0, handler: nil)
  296. }
  297. func testAsyncOpenProgress() {
  298. let user = try! synchronouslyLogInUser(for: basicCredentials(register: isParent), server: authURL)
  299. if !isParent {
  300. populateRealm(user: user, url: realmURL)
  301. return
  302. }
  303. // Wait for the child process to upload everything.
  304. executeChild()
  305. let ex1 = expectation(description: "async open")
  306. let ex2 = expectation(description: "download progress")
  307. let config = user.configuration(realmURL: realmURL, fullSynchronization: true)
  308. let task = Realm.asyncOpen(configuration: config) { _, error in
  309. XCTAssertNil(error)
  310. ex1.fulfill()
  311. }
  312. task.addProgressNotification { progress in
  313. if progress.isTransferComplete {
  314. ex2.fulfill()
  315. }
  316. }
  317. waitForExpectations(timeout: 10.0, handler: nil)
  318. }
  319. func testAsyncOpenTimeout() {
  320. let syncTimeoutOptions = SyncTimeoutOptions()
  321. syncTimeoutOptions.connectTimeout = 3000
  322. SyncManager.shared.timeoutOptions = syncTimeoutOptions
  323. // The server proxy adds a 2 second delay, so a 3 second timeout should succeed
  324. autoreleasepool {
  325. let user = try! synchronouslyLogInUser(for: basicCredentials(register: true), server: slowConnectAuthURL)
  326. let config = user.configuration(cancelAsyncOpenOnNonFatalErrors: true)
  327. let ex = expectation(description: "async open")
  328. Realm.asyncOpen(configuration: config) { _, error in
  329. XCTAssertNil(error)
  330. ex.fulfill()
  331. }
  332. waitForExpectations(timeout: 10.0, handler: nil)
  333. user.logOut()
  334. }
  335. self.resetSyncManager()
  336. self.setupSyncManager()
  337. // and a 1 second timeout should fail
  338. autoreleasepool {
  339. let user = try! synchronouslyLogInUser(for: basicCredentials(register: true), server: slowConnectAuthURL)
  340. let config = user.configuration(cancelAsyncOpenOnNonFatalErrors: true)
  341. syncTimeoutOptions.connectTimeout = 1000
  342. SyncManager.shared.timeoutOptions = syncTimeoutOptions
  343. let ex = expectation(description: "async open")
  344. Realm.asyncOpen(configuration: config) { _, error in
  345. XCTAssertNotNil(error)
  346. if let error = error as NSError? {
  347. XCTAssertEqual(error.code, Int(ETIMEDOUT))
  348. XCTAssertEqual(error.domain, NSPOSIXErrorDomain)
  349. }
  350. ex.fulfill()
  351. }
  352. waitForExpectations(timeout: 4.0, handler: nil)
  353. }
  354. }
  355. // MARK: - Administration
  356. func testRetrieveUserInfo() {
  357. let adminUsername = "jyaku.swift"
  358. let nonAdminUsername = "meela.swift@realm.example.org"
  359. let password = "p"
  360. let server = SwiftObjectServerTests.authServerURL()
  361. // Create a non-admin user.
  362. _ = logInUser(for: .init(username: nonAdminUsername, password: password, register: true),
  363. server: server)
  364. // Create an admin user.
  365. let adminUser = createAdminUser(for: server, username: adminUsername)
  366. // Look up information about the non-admin user from the admin user.
  367. let ex = expectation(description: "Should be able to look up user information")
  368. adminUser.retrieveInfo(forUser: nonAdminUsername, identityProvider: .usernamePassword) { (userInfo, err) in
  369. XCTAssertNil(err)
  370. XCTAssertNotNil(userInfo)
  371. guard let userInfo = userInfo else {
  372. return
  373. }
  374. let account = userInfo.accounts.first!
  375. XCTAssertEqual(account.providerUserIdentity, nonAdminUsername)
  376. XCTAssertEqual(account.provider, Provider.usernamePassword)
  377. XCTAssertFalse(userInfo.isAdmin)
  378. ex.fulfill()
  379. }
  380. waitForExpectations(timeout: 10.0, handler: nil)
  381. }
  382. // MARK: - Authentication
  383. func testInvalidCredentials() {
  384. do {
  385. let username = "testInvalidCredentialsUsername"
  386. let credentials = SyncCredentials.usernamePassword(username: username,
  387. password: "THIS_IS_A_PASSWORD",
  388. register: true)
  389. _ = try synchronouslyLogInUser(for: credentials, server: authURL)
  390. // Now log in the same user, but with a bad password.
  391. let ex = expectation(description: "wait for user login")
  392. let credentials2 = SyncCredentials.usernamePassword(username: username, password: "NOT_A_VALID_PASSWORD")
  393. SyncUser.logIn(with: credentials2, server: authURL) { user, error in
  394. XCTAssertNil(user)
  395. XCTAssertTrue(error is SyncAuthError)
  396. let castError = error as! SyncAuthError
  397. XCTAssertEqual(castError.code, SyncAuthError.invalidCredential)
  398. ex.fulfill()
  399. }
  400. waitForExpectations(timeout: 2.0, handler: nil)
  401. } catch {
  402. XCTFail("Got an error: \(error) (process: \(isParent ? "parent" : "child"))")
  403. }
  404. }
  405. // MARK: - User-specific functionality
  406. func testUserExpirationCallback() {
  407. do {
  408. let user = try synchronouslyLogInUser(for: basicCredentials(), server: authURL)
  409. // Set a callback on the user
  410. var blockCalled = false
  411. let ex = expectation(description: "Error callback should fire upon receiving an error")
  412. user.errorHandler = { (u, error) in
  413. XCTAssertEqual(u.identity, user.identity)
  414. XCTAssertEqual(error.code, .accessDeniedOrInvalidPath)
  415. blockCalled = true
  416. ex.fulfill()
  417. }
  418. // Screw up the token on the user.
  419. manuallySetRefreshToken(for: user, value: "not-a-real-token")
  420. // Try to open a Realm with the user; this will cause our errorHandler block defined above to be fired.
  421. XCTAssertFalse(blockCalled)
  422. _ = try immediatelyOpenRealm(url: realmURL, user: user)
  423. waitForExpectations(timeout: 10.0, handler: nil)
  424. XCTAssertEqual(user.state, .loggedOut)
  425. } catch {
  426. XCTFail("Got an error: \(error) (process: \(isParent ? "parent" : "child"))")
  427. }
  428. }
  429. // MARK: - Offline client reset
  430. func testOfflineClientReset() {
  431. let user = try! synchronouslyLogInUser(for: basicCredentials(), server: authURL)
  432. let sourceFileURL = Bundle(for: type(of: self)).url(forResource: "sync-1.x", withExtension: "realm")!
  433. let fileName = "\(UUID()).realm"
  434. let fileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)
  435. try! FileManager.default.copyItem(at: sourceFileURL, to: fileURL)
  436. let syncConfig = ObjectiveCSupport.convert(object: user.configuration(realmURL: realmURL, fullSynchronization: true).syncConfiguration!)
  437. syncConfig.customFileURL = fileURL
  438. let config = Realm.Configuration(syncConfiguration: ObjectiveCSupport.convert(object: syncConfig))
  439. do {
  440. _ = try Realm(configuration: config)
  441. } catch let e as Realm.Error where e.code == .incompatibleSyncedFile {
  442. var backupConfiguration = e.backupConfiguration
  443. XCTAssertNotNil(backupConfiguration)
  444. // Open the backup Realm with a schema subset since it was created using the schema from .NET's unit tests.
  445. backupConfiguration!.objectTypes = [Person.self]
  446. let backupRealm = try! Realm(configuration: backupConfiguration!)
  447. let people = backupRealm.objects(Person.self)
  448. XCTAssertEqual(people.count, 1)
  449. XCTAssertEqual(people[0].FirstName, "John")
  450. XCTAssertEqual(people[0].LastName, "Smith")
  451. // Verify that we can now successfully open the original synced Realm.
  452. _ = try! Realm(configuration: config)
  453. } catch {
  454. fatalError("Unexpected error: \(error)")
  455. }
  456. }
  457. // MARK: - Certificate Pinning
  458. func testSecureConnectionToLocalhostWithDefaultSecurity() {
  459. let user = try! synchronouslyLogInUser(for: basicCredentials(), server: authURL)
  460. let config = user.configuration(realmURL: URL(string: "realms://localhost:9443/~/default"),
  461. serverValidationPolicy: .system)
  462. let ex = expectation(description: "Waiting for error handler to be called")
  463. SyncManager.shared.errorHandler = { (error, session) in
  464. ex.fulfill()
  465. }
  466. _ = try! Realm(configuration: config)
  467. self.waitForExpectations(timeout: 4.0)
  468. }
  469. func testSecureConnectionToLocalhostWithValidationDisabled() {
  470. let user = try! synchronouslyLogInUser(for: basicCredentials(), server: authURL)
  471. let config = user.configuration(realmURL: URL(string: "realms://localhost:9443/~/default"),
  472. serverValidationPolicy: .none)
  473. SyncManager.shared.errorHandler = { (error, session) in
  474. XCTFail("Unexpected connection failure: \(error)")
  475. }
  476. let realm = try! Realm(configuration: config)
  477. self.waitForUploads(for: realm)
  478. }
  479. func testSecureConnectionToLocalhostWithPinnedCertificate() {
  480. let user = try! synchronouslyLogInUser(for: basicCredentials(), server: authURL)
  481. let certURL = URL(string: #file)!
  482. .deletingLastPathComponent()
  483. .appendingPathComponent("certificates")
  484. .appendingPathComponent("localhost.cer")
  485. let config = user.configuration(realmURL: URL(string: "realms://localhost:9443/~/default"),
  486. serverValidationPolicy: .pinCertificate(path: certURL))
  487. SyncManager.shared.errorHandler = { (error, session) in
  488. XCTFail("Unexpected connection failure: \(error)")
  489. }
  490. let realm = try! Realm(configuration: config)
  491. self.waitForUploads(for: realm)
  492. }
  493. func testSecureConnectionToLocalhostWithIncorrectPinnedCertificate() {
  494. let user = try! synchronouslyLogInUser(for: basicCredentials(), server: authURL)
  495. let certURL = URL(string: #file)!
  496. .deletingLastPathComponent()
  497. .appendingPathComponent("certificates")
  498. .appendingPathComponent("localhost-other.cer")
  499. let config = user.configuration(realmURL: URL(string: "realms://localhost:9443/~/default"),
  500. serverValidationPolicy: .pinCertificate(path: certURL))
  501. let ex = expectation(description: "Waiting for error handler to be called")
  502. SyncManager.shared.errorHandler = { (error, session) in
  503. ex.fulfill()
  504. }
  505. _ = try! Realm(configuration: config)
  506. self.waitForExpectations(timeout: 4.0)
  507. }
  508. }