RealmConfigurationTests.mm 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2015 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 "RLMTestCase.h"
  19. #import "RLMRealmConfiguration_Private.hpp"
  20. #import "RLMTestObjects.h"
  21. #import "RLMUtil.hpp"
  22. @interface RealmConfigurationTests : RLMTestCase
  23. @end
  24. @implementation RealmConfigurationTests
  25. #pragma mark - Setter Validation
  26. - (void)testSetPathAndInMemoryIdentifierAreMutuallyExclusive {
  27. RLMRealmConfiguration *configuration = [[RLMRealmConfiguration alloc] init];
  28. configuration.inMemoryIdentifier = @"identifier";
  29. XCTAssertNil(configuration.fileURL);
  30. XCTAssertEqualObjects(configuration.inMemoryIdentifier, @"identifier");
  31. configuration.fileURL = [NSURL fileURLWithPath:@"/dev/null"];
  32. XCTAssertNil(configuration.inMemoryIdentifier);
  33. XCTAssertEqualObjects(configuration.fileURL.path, @"/dev/null");
  34. }
  35. - (void)testFileURLValidation {
  36. RLMRealmConfiguration *configuration = [[RLMRealmConfiguration alloc] init];
  37. XCTAssertThrows(configuration.fileURL = nil);
  38. }
  39. - (void)testEncryptionKeyValidation {
  40. RLMRealmConfiguration *configuration = [[RLMRealmConfiguration alloc] init];
  41. XCTAssertNoThrow(configuration.encryptionKey = nil);
  42. RLMAssertThrowsWithReasonMatching(configuration.encryptionKey = [NSData data], @"Encryption key must be exactly 64 bytes long");
  43. NSData *key = RLMGenerateKey();
  44. configuration.encryptionKey = key;
  45. XCTAssertEqualObjects(configuration.encryptionKey, key);
  46. }
  47. - (void)testSchemaVersionValidation {
  48. RLMRealmConfiguration *configuration = [[RLMRealmConfiguration alloc] init];
  49. RLMAssertThrowsWithReasonMatching(configuration.schemaVersion = RLMNotVersioned, @"schema version.*RLMNotVersioned");
  50. configuration.schemaVersion = 1;
  51. XCTAssertEqual(configuration.schemaVersion, 1U);
  52. configuration.schemaVersion = std::numeric_limits<uint64_t>::max() - 1;
  53. XCTAssertEqual(configuration.schemaVersion, std::numeric_limits<uint64_t>::max() - 1);
  54. }
  55. - (void)testClassSubsetsValidateLinks {
  56. RLMRealmConfiguration *configuration = [[RLMRealmConfiguration alloc] init];
  57. XCTAssertThrows(configuration.objectClasses = @[LinkStringObject.class]);
  58. XCTAssertNoThrow(configuration.objectClasses = (@[LinkStringObject.class, StringObject.class]));
  59. XCTAssertThrows(configuration.objectClasses = @[CompanyObject.class]);
  60. XCTAssertNoThrow(configuration.objectClasses = (@[CompanyObject.class, EmployeeObject.class]));
  61. }
  62. - (void)testCannotSetMutuallyExclusiveProperties {
  63. RLMRealmConfiguration *configuration = [[RLMRealmConfiguration alloc] init];
  64. XCTAssertNoThrow(configuration.readOnly = YES);
  65. XCTAssertNoThrow(configuration.deleteRealmIfMigrationNeeded = NO);
  66. XCTAssertThrows(configuration.deleteRealmIfMigrationNeeded = YES);
  67. XCTAssertNoThrow(configuration.readOnly = NO);
  68. XCTAssertNoThrow(configuration.deleteRealmIfMigrationNeeded = YES);
  69. XCTAssertNoThrow(configuration.readOnly = NO);
  70. XCTAssertThrows(configuration.readOnly = YES);
  71. }
  72. #pragma mark - Default Configuration
  73. - (void)testDefaultConfiguration {
  74. RLMRealmConfiguration *defaultConfiguration = [RLMRealmConfiguration defaultConfiguration];
  75. XCTAssertEqualObjects(defaultConfiguration.fileURL, RLMDefaultRealmURL());
  76. XCTAssertNil(defaultConfiguration.inMemoryIdentifier);
  77. XCTAssertNil(defaultConfiguration.encryptionKey);
  78. XCTAssertFalse(defaultConfiguration.readOnly);
  79. XCTAssertEqual(defaultConfiguration.schemaVersion, 0U);
  80. XCTAssertNil(defaultConfiguration.migrationBlock);
  81. // private properties
  82. XCTAssertFalse(defaultConfiguration.dynamic);
  83. XCTAssertNil(defaultConfiguration.customSchema);
  84. }
  85. - (void)testSetDefaultConfiguration {
  86. RLMRealmConfiguration *configuration = [[RLMRealmConfiguration alloc] init];
  87. configuration.fileURL = [NSURL fileURLWithPath:@"/dev/null"];
  88. [RLMRealmConfiguration setDefaultConfiguration:configuration];
  89. XCTAssertEqualObjects(RLMRealmConfiguration.defaultConfiguration.fileURL.path, @"/dev/null");
  90. }
  91. - (void)testDefaultConfigurationUsesValueSemantics {
  92. RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
  93. config.fileURL = [NSURL fileURLWithPath:@"/dev/null"];
  94. XCTAssertNotEqualObjects(config.fileURL, RLMRealmConfiguration.defaultConfiguration.fileURL);
  95. [RLMRealmConfiguration setDefaultConfiguration:config];
  96. XCTAssertEqualObjects(config.fileURL, RLMRealmConfiguration.defaultConfiguration.fileURL);
  97. config.fileURL = [NSURL fileURLWithPath:@"/dev/null/foo"];
  98. XCTAssertNotEqualObjects(config.fileURL, RLMRealmConfiguration.defaultConfiguration.fileURL);
  99. }
  100. - (void)testDefaultRealmUsesDefaultConfiguration {
  101. RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
  102. @autoreleasepool { XCTAssertEqualObjects(RLMRealm.defaultRealm.configuration.fileURL, config.fileURL); }
  103. config.fileURL = RLMTestRealmURL();
  104. @autoreleasepool { XCTAssertNotEqualObjects(RLMRealm.defaultRealm.configuration.fileURL, config.fileURL); }
  105. RLMRealmConfiguration.defaultConfiguration = config;
  106. @autoreleasepool { XCTAssertEqualObjects(RLMRealm.defaultRealm.configuration.fileURL, config.fileURL); }
  107. config.inMemoryIdentifier = @"default";
  108. RLMRealmConfiguration.defaultConfiguration = config;
  109. @autoreleasepool {
  110. RLMRealm *realm = RLMRealm.defaultRealm;
  111. NSString *realmPath = @(realm.configuration.config.path.c_str());
  112. XCTAssertTrue([realmPath hasSuffix:@"/default"]);
  113. XCTAssertTrue([realmPath hasPrefix:NSTemporaryDirectory()]);
  114. }
  115. config.schemaVersion = 1;
  116. RLMRealmConfiguration.defaultConfiguration = config;
  117. @autoreleasepool {
  118. RLMRealm *realm = RLMRealm.defaultRealm;
  119. NSString *realmPath = @(realm.configuration.config.path.c_str());
  120. XCTAssertEqual(1U, [RLMRealm schemaVersionAtURL:[NSURL fileURLWithPath:realmPath] encryptionKey:nil error:nil]);
  121. }
  122. config.fileURL = RLMDefaultRealmURL();
  123. RLMRealmConfiguration.defaultConfiguration = config;
  124. config.encryptionKey = RLMGenerateKey();
  125. RLMRealmConfiguration.defaultConfiguration = config;
  126. @autoreleasepool {
  127. // Realm with no encryption key already exists from above
  128. XCTAssertThrows([RLMRealm defaultRealm]);
  129. }
  130. [self deleteRealmFileAtURL:config.fileURL];
  131. // Create and then re-open with same key
  132. @autoreleasepool { XCTAssertNoThrow([RLMRealm defaultRealm]); }
  133. @autoreleasepool { XCTAssertNoThrow([RLMRealm defaultRealm]); }
  134. // Fail to re-open with a different key
  135. config.encryptionKey = RLMGenerateKey();
  136. RLMRealmConfiguration.defaultConfiguration = config;
  137. @autoreleasepool { XCTAssertThrows([RLMRealm defaultRealm]); }
  138. // Verify that the default realm's migration block is used implicitly
  139. // when needed
  140. [self deleteRealmFileAtURL:config.fileURL];
  141. @autoreleasepool { XCTAssertNoThrow([RLMRealm defaultRealm]); }
  142. config.schemaVersion = 2;
  143. __block bool migrationCalled = false;
  144. config.migrationBlock = ^(RLMMigration *, uint64_t) {
  145. migrationCalled = true;
  146. };
  147. RLMRealmConfiguration.defaultConfiguration = config;
  148. XCTAssertFalse(migrationCalled);
  149. @autoreleasepool { XCTAssertNoThrow([RLMRealm defaultRealm]); }
  150. XCTAssertTrue(migrationCalled);
  151. }
  152. @end