123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- #import "RLMTestCase.h"
- #import "RLMRealmConfiguration_Private.h"
- #import <Realm/RLMRealm_Private.h>
- #import <Realm/RLMSchema_Private.h>
- #import <Realm/RLMRealmConfiguration_Private.h>
- static NSString *parentProcessBundleIdentifier()
- {
- static BOOL hasInitializedIdentifier;
- static NSString *identifier;
- if (!hasInitializedIdentifier) {
- identifier = [NSProcessInfo processInfo].environment[@"RLMParentProcessBundleID"];
- hasInitializedIdentifier = YES;
- }
- return identifier;
- }
- NSURL *RLMDefaultRealmURL() {
- return [NSURL fileURLWithPath:RLMRealmPathForFileAndBundleIdentifier(@"default.realm", parentProcessBundleIdentifier())];
- }
- NSURL *RLMTestRealmURL() {
- return [NSURL fileURLWithPath:RLMRealmPathForFileAndBundleIdentifier(@"test.realm", parentProcessBundleIdentifier())];
- }
- static void deleteOrThrow(NSURL *fileURL) {
- NSError *error;
- if (![[NSFileManager defaultManager] removeItemAtURL:fileURL error:&error]) {
- if (error.code != NSFileNoSuchFileError) {
- @throw [NSException exceptionWithName:@"RLMTestException"
- reason:[@"Unable to delete realm: " stringByAppendingString:error.description]
- userInfo:nil];
- }
- }
- }
- NSData *RLMGenerateKey() {
- uint8_t buffer[64];
- (void)SecRandomCopyBytes(kSecRandomDefault, 64, buffer);
- return [[NSData alloc] initWithBytes:buffer length:sizeof(buffer)];
- }
- static BOOL encryptTests() {
- static BOOL encryptAll = NO;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- if (getenv("REALM_ENCRYPT_ALL")) {
- encryptAll = YES;
- }
- });
- return encryptAll;
- }
- @implementation RLMTestCase {
- dispatch_queue_t _bgQueue;
- }
- + (void)setUp {
- [super setUp];
- #if DEBUG || !TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
-
-
-
- RLMDisableSyncToDisk();
- #endif
- if (!getenv("RLMProcessIsChild")) {
- [self preinitializeSchema];
-
- [NSFileManager.defaultManager removeItemAtPath:RLMRealmPathForFile(@"") error:nil];
- }
-
-
- [NSFileManager.defaultManager createDirectoryAtURL:RLMDefaultRealmURL().URLByDeletingLastPathComponent
- withIntermediateDirectories:YES attributes:nil error:nil];
- }
- + (void)preinitializeSchema {
- [RLMSchema sharedSchema];
- }
- - (void)deleteFiles {
-
- [self resetRealmState];
-
- [self deleteRealmFileAtURL:RLMDefaultRealmURL()];
- [self deleteRealmFileAtURL:RLMTestRealmURL()];
- }
- - (void)resetRealmState {
- [RLMRealm resetRealmState];
- }
- - (void)deleteRealmFileAtURL:(NSURL *)fileURL
- {
- deleteOrThrow(fileURL);
- deleteOrThrow([fileURL URLByAppendingPathExtension:@"lock"]);
- deleteOrThrow([fileURL URLByAppendingPathExtension:@"note"]);
- }
- - (void)invokeTest {
- @autoreleasepool {
- [self deleteFiles];
- if (encryptTests()) {
- RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
- configuration.encryptionKey = RLMGenerateKey();
- }
- }
- @autoreleasepool {
- [super invokeTest];
- }
- @autoreleasepool {
- if (_bgQueue) {
- dispatch_sync(_bgQueue, ^{});
- _bgQueue = nil;
- }
- [self deleteFiles];
- }
- }
- - (RLMRealm *)realmWithTestPath
- {
- return [RLMRealm realmWithURL:RLMTestRealmURL()];
- }
- - (RLMRealm *)realmWithTestPathAndSchema:(RLMSchema *)schema {
- RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
- configuration.fileURL = RLMTestRealmURL();
- if (schema)
- configuration.customSchema = schema;
- else
- configuration.dynamic = true;
- return [RLMRealm realmWithConfiguration:configuration error:nil];
- }
- - (RLMRealm *)inMemoryRealmWithIdentifier:(NSString *)identifier {
- RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
- configuration.inMemoryIdentifier = identifier;
- return [RLMRealm realmWithConfiguration:configuration error:nil];
- }
- - (RLMRealm *)readOnlyRealmWithURL:(NSURL *)fileURL error:(NSError **)error {
- RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
- configuration.fileURL = fileURL;
- configuration.readOnly = true;
- return [RLMRealm realmWithConfiguration:configuration error:error];
- }
- - (void)waitForNotification:(NSString *)expectedNote realm:(RLMRealm *)realm block:(dispatch_block_t)block {
- XCTestExpectation *notificationFired = [self expectationWithDescription:@"notification fired"];
- __block RLMNotificationToken *token = [realm addNotificationBlock:^(NSString *note, RLMRealm *realm) {
- XCTAssertNotNil(note, @"Note should not be nil");
- XCTAssertNotNil(realm, @"Realm should not be nil");
- if (note == expectedNote) {
- [notificationFired fulfill];
- [token invalidate];
- }
- }];
- dispatch_queue_t queue = dispatch_queue_create("background", 0);
- dispatch_async(queue, ^{
- @autoreleasepool {
- block();
- }
- });
- [self waitForExpectationsWithTimeout:10.0 handler:nil];
-
- dispatch_sync(queue, ^{});
- }
- - (void)dispatchAsync:(dispatch_block_t)block {
- if (!_bgQueue) {
- _bgQueue = dispatch_queue_create("test background queue", 0);
- }
- dispatch_async(_bgQueue, ^{
- @autoreleasepool {
- block();
- }
- });
- }
- - (void)dispatchAsyncAndWait:(dispatch_block_t)block {
- [self dispatchAsync:block];
- dispatch_sync(_bgQueue, ^{});
- }
- - (id)nonLiteralNil
- {
- return nil;
- }
- @end
|