|
25 | 25 | #import "FirebaseDatabase/Sources/Snapshot/FSnapshotUtilities.h" |
26 | 26 | #import "FirebaseDatabase/Tests/Helpers/FTestHelpers.h" |
27 | 27 |
|
28 | | -@interface FLevelDBStorageEngineTests : XCTestCase |
| 28 | +@interface FLevelDBStorageEngine (Tests) |
| 29 | ++ (void)ensureDir:(NSString *)path markAsDoNotBackup:(BOOL)markAsDoNotBackup; |
| 30 | +@end |
29 | 31 |
|
| 32 | +@interface FLevelDBStorageEngineTests : XCTestCase |
30 | 33 | @end |
31 | 34 |
|
32 | 35 | @implementation FLevelDBStorageEngineTests |
@@ -685,4 +688,49 @@ - (void)testRemoveTrackedQueryRemovesTrackedQueryKeys { |
685 | 688 | ([NSSet setWithArray:@[ @"b", @"c" ]])); |
686 | 689 | } |
687 | 690 |
|
| 691 | +- (void)testEnsureDirSetsCorrectFileProtection { |
| 692 | + NSString *testDirName = |
| 693 | + [NSString stringWithFormat:@"fdb_persistence_test_%lu", (unsigned long)arc4random()]; |
| 694 | + NSString *testPath = [NSTemporaryDirectory() stringByAppendingPathComponent:testDirName]; |
| 695 | + NSFileManager *fileManager = [NSFileManager defaultManager]; |
| 696 | + |
| 697 | + // --- Test creation --- |
| 698 | + [fileManager removeItemAtPath:testPath error:nil]; |
| 699 | + [FLevelDBStorageEngine ensureDir:testPath markAsDoNotBackup:NO]; |
| 700 | + |
| 701 | + NSError *error = nil; |
| 702 | + NSDictionary<NSFileAttributeKey, id> *attributes = [fileManager attributesOfItemAtPath:testPath |
| 703 | + error:&error]; |
| 704 | + XCTAssertNil(error, @"Failed to get attributes of directory: %@", error); |
| 705 | + |
| 706 | +#if !TARGET_OS_SIMULATOR |
| 707 | + // On a physical device, file protection should be set. |
| 708 | + XCTAssertEqualObjects(attributes[NSFileProtectionKey], |
| 709 | + NSFileProtectionCompleteUntilFirstUserAuthentication); |
| 710 | +#else |
| 711 | + XCTAssertNil(attributes[NSFileProtectionKey]); |
| 712 | +#endif |
| 713 | + |
| 714 | + // --- Test update on existing directory --- |
| 715 | +#if !TARGET_OS_SIMULATOR |
| 716 | + // This part of the test is only relevant on devices where file protection is supported. |
| 717 | + [fileManager removeItemAtPath:testPath error:nil]; |
| 718 | + NSDictionary *initialAttributes = @{NSFileProtectionKey : NSFileProtectionNone}; |
| 719 | + XCTAssertTrue([fileManager createDirectoryAtPath:testPath |
| 720 | + withIntermediateDirectories:YES |
| 721 | + attributes:initialAttributes |
| 722 | + error:&error], |
| 723 | + @"Failed to create directory for update test: %@", error); |
| 724 | + |
| 725 | + [FLevelDBStorageEngine ensureDir:testPath markAsDoNotBackup:NO]; |
| 726 | + |
| 727 | + attributes = [fileManager attributesOfItemAtPath:testPath error:&error]; |
| 728 | + XCTAssertNil(error, @"Failed to get attributes after update: %@", error); |
| 729 | + XCTAssertEqualObjects(attributes[NSFileProtectionKey], |
| 730 | + NSFileProtectionCompleteUntilFirstUserAuthentication); |
| 731 | +#endif // !TARGET_OS_SIMULATOR |
| 732 | + |
| 733 | + // Clean up |
| 734 | + [fileManager removeItemAtPath:testPath error:nil]; |
| 735 | +} |
688 | 736 | @end |
0 commit comments