RLMSyncTestCase.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "RLMMultiProcessTestCase.h"
  19. #import "RLMSyncConfiguration_Private.h"
  20. typedef void(^RLMSyncBasicErrorReportingBlock)(NSError * _Nullable);
  21. NS_ASSUME_NONNULL_BEGIN
  22. @interface RLMSyncManager ()
  23. - (void)setSessionCompletionNotifier:(nullable RLMSyncBasicErrorReportingBlock)sessionCompletionNotifier;
  24. @end
  25. @interface SyncObject : RLMObject
  26. @property NSString *stringProp;
  27. @end
  28. @interface HugeSyncObject : RLMObject
  29. @property NSData *dataProp;
  30. + (instancetype)object;
  31. @end
  32. @interface RLMSyncTestCase : RLMMultiProcessTestCase
  33. + (NSURL *)authServerURL;
  34. + (NSURL *)secureAuthServerURL;
  35. + (RLMSyncCredentials *)basicCredentialsWithName:(NSString *)name register:(BOOL)shouldRegister;
  36. + (NSURL *)onDiskPathForSyncedRealm:(RLMRealm *)realm;
  37. /// Retrieve the administrator token.
  38. - (NSString *)adminToken;
  39. /// Read and delete the last email sent by ROS to the given address.
  40. /// Returns nil if none has been sent to that address.
  41. - (nullable NSString *)emailForAddress:(NSString *)email;
  42. /// Synchronously open a synced Realm and wait until the binding process has completed or failed.
  43. - (RLMRealm *)openRealmForURL:(NSURL *)url user:(RLMSyncUser *)user;
  44. /// Synchronously open a synced Realm and wait until the binding process has completed or failed.
  45. - (RLMRealm *)openRealmWithConfiguration:(RLMRealmConfiguration *)configuration;
  46. /// Synchronously open a synced Realm via asyncOpen and return the Realm.
  47. - (RLMRealm *)asyncOpenRealmWithConfiguration:(RLMRealmConfiguration *)configuration;
  48. /// Synchronously open a synced Realm via asyncOpen and return the expected error.
  49. - (NSError *)asyncOpenErrorWithConfiguration:(RLMRealmConfiguration *)configuration;
  50. /// Synchronously open a synced Realm. Also run a block right after the Realm is created.
  51. - (RLMRealm *)openRealmForURL:(NSURL *)url
  52. user:(RLMSyncUser *)user
  53. immediatelyBlock:(nullable void(^)(void))block;
  54. /// Synchronously open a synced Realm with encryption key and stop policy.
  55. /// Also run a block right after the Realm is created.
  56. - (RLMRealm *)openRealmForURL:(NSURL *)url
  57. user:(RLMSyncUser *)user
  58. encryptionKey:(nullable NSData *)encryptionKey
  59. stopPolicy:(RLMSyncStopPolicy)stopPolicy
  60. immediatelyBlock:(nullable void(^)(void))block;
  61. /// Synchronously open a synced Realm and wait until the binding process has completed or failed.
  62. /// Also run a block right after the Realm is created.
  63. - (RLMRealm *)openRealmWithConfiguration:(RLMRealmConfiguration *)configuration
  64. immediatelyBlock:(nullable void(^)(void))block;
  65. ;
  66. /// Immediately open a synced Realm.
  67. - (RLMRealm *)immediatelyOpenRealmForURL:(NSURL *)url user:(RLMSyncUser *)user;
  68. /// Immediately open a synced Realm with encryption key and stop policy.
  69. - (RLMRealm *)immediatelyOpenRealmForURL:(NSURL *)url
  70. user:(RLMSyncUser *)user
  71. encryptionKey:(nullable NSData *)encryptionKey
  72. stopPolicy:(RLMSyncStopPolicy)stopPolicy;
  73. /// Synchronously create, log in, and return a user.
  74. - (RLMSyncUser *)logInUserForCredentials:(RLMSyncCredentials *)credentials
  75. server:(NSURL *)url;
  76. /// Create and log in an admin user.
  77. - (RLMSyncUser *)createAdminUserForURL:(NSURL *)url username:(NSString *)username;
  78. /// Add a number of objects to a Realm.
  79. - (void)addSyncObjectsToRealm:(RLMRealm *)realm descriptions:(NSArray<NSString *> *)descriptions;
  80. /// Synchronously wait for downloads to complete for any number of Realms, and then check their `SyncObject` counts.
  81. - (void)waitForDownloadsForUser:(RLMSyncUser *)user
  82. realms:(NSArray<RLMRealm *> *)realms
  83. realmURLs:(NSArray<NSURL *> *)realmURLs
  84. expectedCounts:(NSArray<NSNumber *> *)counts;
  85. /// "Prime" the sync manager to signal the given semaphore the next time a session is bound. This method should be
  86. /// called right before a Realm is opened if that Realm's session is the one to be monitored.
  87. - (void)primeSyncManagerWithSemaphore:(nullable dispatch_semaphore_t)semaphore;
  88. /// Wait for downloads to complete; drop any error.
  89. - (void)waitForDownloadsForRealm:(RLMRealm *)realm;
  90. - (void)waitForDownloadsForRealm:(RLMRealm *)realm error:(NSError **)error;
  91. /// Wait for uploads to complete; drop any error.
  92. - (void)waitForUploadsForRealm:(RLMRealm *)realm;
  93. - (void)waitForUploadsForRealm:(RLMRealm *)realm error:(NSError **)error;
  94. /// Wait for downloads to complete while spinning the runloop. This method uses expectations.
  95. - (void)waitForDownloadsForUser:(RLMSyncUser *)user
  96. url:(NSURL *)url
  97. expectation:(nullable XCTestExpectation *)expectation
  98. error:(NSError **)error;
  99. /// Manually set the refresh token for a user. Used for testing invalid token conditions.
  100. - (void)manuallySetRefreshTokenForUser:(RLMSyncUser *)user value:(NSString *)tokenValue;
  101. - (void)setupSyncManager;
  102. - (void)resetSyncManager;
  103. @end
  104. NS_ASSUME_NONNULL_END
  105. #define WAIT_FOR_SEMAPHORE(macro_semaphore, macro_timeout) \
  106. { \
  107. int64_t delay_in_ns = (int64_t)(macro_timeout * NSEC_PER_SEC); \
  108. BOOL sema_success = dispatch_semaphore_wait(macro_semaphore, dispatch_time(DISPATCH_TIME_NOW, delay_in_ns)) == 0; \
  109. XCTAssertTrue(sema_success, @"Semaphore timed out."); \
  110. }
  111. #define CHECK_COUNT(d_count, macro_object_type, macro_realm) \
  112. { \
  113. [macro_realm refresh]; \
  114. NSInteger c = [macro_object_type allObjectsInRealm:macro_realm].count; \
  115. NSString *w = self.isParent ? @"parent" : @"child"; \
  116. XCTAssert(d_count == c, @"Expected %@ items, but actually got %@ (%@)", @(d_count), @(c), w); \
  117. }