mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			206 lines
		
	
	
		
			8.7 KiB
		
	
	
	
		
			Matlab
		
	
		
		
			
		
	
	
			206 lines
		
	
	
		
			8.7 KiB
		
	
	
	
		
			Matlab
		
	
| 
											9 years ago
										 | // | ||
|  | //  Copyright (c) 2017 Open Whisper Systems. All rights reserved. | ||
|  | // | ||
| 
											9 years ago
										 | 
 | ||
| 
											9 years ago
										 | #import "OWSDevice.h" | ||
| 
											9 years ago
										 | #import "OWSOrphanedDataCleaner.h" | ||
|  | #import "TSAttachmentStream.h" | ||
|  | #import "TSContactThread.h" | ||
|  | #import "TSIncomingMessage.h" | ||
|  | #import "TSStorageManager.h" | ||
|  | #import <XCTest/XCTest.h> | ||
|  | 
 | ||
|  | @interface OWSOrphanedDataCleanerTest : XCTestCase | ||
|  | 
 | ||
|  | @end | ||
|  | 
 | ||
| 
											8 years ago
										 | #pragma mark - | ||
|  | 
 | ||
| 
											9 years ago
										 | @implementation OWSOrphanedDataCleanerTest | ||
|  | 
 | ||
|  | - (void)setUp | ||
|  | { | ||
|  |     [super setUp]; | ||
|  |     // Register views, etc. | ||
| 
											8 years ago
										 |     [[TSStorageManager sharedManager] setupDatabaseWithSafeBlockingMigrations:^{}]; | ||
| 
											9 years ago
										 | 
 | ||
|  |     // Set up initial conditions & Sanity check | ||
|  |     [TSAttachmentStream deleteAttachments]; | ||
| 
											8 years ago
										 |     XCTAssertEqual(0, [self numberOfItemsInAttachmentsFolder]); | ||
| 
											9 years ago
										 |     [TSAttachmentStream removeAllObjectsInCollection]; | ||
|  |     XCTAssertEqual(0, [TSAttachmentStream numberOfKeysInCollection]); | ||
|  |     [TSIncomingMessage removeAllObjectsInCollection]; | ||
|  |     XCTAssertEqual(0, [TSIncomingMessage numberOfKeysInCollection]); | ||
|  |     [TSThread removeAllObjectsInCollection]; | ||
|  |     XCTAssertEqual(0, [TSThread numberOfKeysInCollection]); | ||
|  | } | ||
|  | 
 | ||
|  | - (void)tearDown | ||
|  | { | ||
|  |     [super tearDown]; | ||
|  | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSUInteger)numberOfItemsInAttachmentsFolder | ||
|  | { | ||
|  |     return [OWSOrphanedDataCleaner filePathsInAttachmentsFolder].count; | ||
|  | } | ||
|  | 
 | ||
| 
											9 years ago
										 | - (void)testInteractionsWithoutThreadAreDeleted | ||
|  | { | ||
|  |     // This thread is intentionally not saved. It's meant to recreate a situation we've seen where interactions exist | ||
|  |     // that reference the id of a thread that no longer exists. Presumably this is the result of a deleted thread not | ||
|  |     // properly deleting it's interactions. | ||
|  |     TSContactThread *unsavedThread = [[TSContactThread alloc] initWithUniqueId:@"this-thread-does-not-exist"]; | ||
|  | 
 | ||
| 
											9 years ago
										 |     TSIncomingMessage *incomingMessage = [[TSIncomingMessage alloc] initWithTimestamp:1 | ||
|  |                                                                              inThread:unsavedThread | ||
|  |                                                                              authorId:@"fake-author-id" | ||
| 
											9 years ago
										 |                                                                        sourceDeviceId:OWSDevicePrimaryDeviceId | ||
| 
											9 years ago
										 |                                                                           messageBody:@"footch"]; | ||
| 
											9 years ago
										 |     [incomingMessage save]; | ||
|  |     XCTAssertEqual(1, [TSIncomingMessage numberOfKeysInCollection]); | ||
|  | 
 | ||
| 
											8 years ago
										 |     XCTestExpectation *expectation = [self expectationWithDescription:@"Cleanup"]; | ||
|  |     [OWSOrphanedDataCleaner auditAndCleanupAsync:^{ | ||
|  |         [expectation fulfill]; | ||
|  |     }]; | ||
|  |     [self waitForExpectationsWithTimeout:5.0 | ||
|  |                                  handler:^(NSError *error) { | ||
|  |                                      if (error) { | ||
|  |                                          XCTFail(@"Expectation Failed with error: %@", error); | ||
|  |                                      } | ||
|  |                                  }]; | ||
|  | 
 | ||
| 
											9 years ago
										 |     XCTAssertEqual(0, [TSIncomingMessage numberOfKeysInCollection]); | ||
|  | } | ||
|  | 
 | ||
|  | - (void)testInteractionsWithThreadAreNotDeleted | ||
|  | { | ||
|  |     TSContactThread *savedThread = [[TSContactThread alloc] initWithUniqueId:@"this-thread-exists"]; | ||
|  |     [savedThread save]; | ||
|  | 
 | ||
| 
											9 years ago
										 |     TSIncomingMessage *incomingMessage = [[TSIncomingMessage alloc] initWithTimestamp:1 | ||
|  |                                                                              inThread:savedThread | ||
|  |                                                                              authorId:@"fake-author-id" | ||
| 
											9 years ago
										 |                                                                        sourceDeviceId:OWSDevicePrimaryDeviceId | ||
| 
											9 years ago
										 |                                                                           messageBody:@"footch"]; | ||
| 
											9 years ago
										 |     [incomingMessage save]; | ||
|  |     XCTAssertEqual(1, [TSIncomingMessage numberOfKeysInCollection]); | ||
|  | 
 | ||
| 
											8 years ago
										 |     XCTestExpectation *expectation = [self expectationWithDescription:@"Cleanup"]; | ||
|  |     [OWSOrphanedDataCleaner auditAndCleanupAsync:^{ | ||
|  |         [expectation fulfill]; | ||
|  |     }]; | ||
|  |     [self waitForExpectationsWithTimeout:5.0 | ||
|  |                                  handler:^(NSError *error) { | ||
|  |                                      if (error) { | ||
|  |                                          XCTFail(@"Expectation Failed with error: %@", error); | ||
|  |                                      } | ||
|  |                                  }]; | ||
|  | 
 | ||
| 
											9 years ago
										 |     XCTAssertEqual(1, [TSIncomingMessage numberOfKeysInCollection]); | ||
|  | } | ||
|  | 
 | ||
|  | - (void)testFilesWithoutInteractionsAreDeleted | ||
|  | { | ||
| 
											8 years ago
										 |     // sanity check | ||
| 
											8 years ago
										 |     XCTAssertEqual(0, [self numberOfItemsInAttachmentsFolder]); | ||
| 
											8 years ago
										 | 
 | ||
| 
											9 years ago
										 |     NSError *error; | ||
| 
											9 years ago
										 |     TSAttachmentStream *attachmentStream = [[TSAttachmentStream alloc] initWithContentType:@"image/jpeg" sourceFilename:nil]; | ||
| 
											9 years ago
										 |     [attachmentStream writeData:[NSData new] error:&error]; | ||
| 
											9 years ago
										 |     [attachmentStream save]; | ||
|  |     NSString *orphanedFilePath = [attachmentStream filePath]; | ||
|  |     BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:orphanedFilePath]; | ||
|  |     XCTAssert(fileExists); | ||
| 
											8 years ago
										 |     XCTAssertEqual(1, [self numberOfItemsInAttachmentsFolder]); | ||
|  | 
 | ||
|  |     // Do multiple cleanup passes. | ||
|  |     for (int i = 0; i < 2; i++) { | ||
|  |         XCTestExpectation *expectation = [self expectationWithDescription:@"Cleanup"]; | ||
|  |         [OWSOrphanedDataCleaner auditAndCleanupAsync:^{ | ||
|  |             [expectation fulfill]; | ||
|  |         }]; | ||
|  |         [self waitForExpectationsWithTimeout:5.0 | ||
|  |                                      handler:^(NSError *error) { | ||
|  |                                          if (error) { | ||
|  |                                              XCTFail(@"Expectation Failed with error: %@", error); | ||
|  |                                          } | ||
|  |                                      }]; | ||
|  |     } | ||
| 
											9 years ago
										 | 
 | ||
|  |     fileExists = [[NSFileManager defaultManager] fileExistsAtPath:orphanedFilePath]; | ||
|  |     XCTAssertFalse(fileExists); | ||
| 
											8 years ago
										 |     XCTAssertEqual(0, [self numberOfItemsInAttachmentsFolder]); | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
|  | - (void)testFilesWithInteractionsAreNotDeleted | ||
|  | { | ||
|  |     TSContactThread *savedThread = [[TSContactThread alloc] initWithUniqueId:@"this-thread-exists"]; | ||
|  |     [savedThread save]; | ||
|  | 
 | ||
| 
											9 years ago
										 |     NSError *error; | ||
| 
											9 years ago
										 |     TSAttachmentStream *attachmentStream = [[TSAttachmentStream alloc] initWithContentType:@"image/jpeg" sourceFilename:nil]; | ||
| 
											9 years ago
										 |     [attachmentStream writeData:[NSData new] error:&error]; | ||
| 
											9 years ago
										 |     [attachmentStream save]; | ||
|  | 
 | ||
|  |     TSIncomingMessage *incomingMessage = [[TSIncomingMessage alloc] initWithTimestamp:1 | ||
|  |                                                                              inThread:savedThread | ||
| 
											9 years ago
										 |                                                                              authorId:@"fake-author-id" | ||
| 
											9 years ago
										 |                                                                        sourceDeviceId:OWSDevicePrimaryDeviceId | ||
| 
											9 years ago
										 |                                                                           messageBody:@"footch" | ||
| 
											9 years ago
										 |                                                                         attachmentIds:@[ attachmentStream.uniqueId ] | ||
|  |                                                                      expiresInSeconds:0]; | ||
| 
											9 years ago
										 |     [incomingMessage save]; | ||
|  | 
 | ||
|  |     NSString *attachmentFilePath = [attachmentStream filePath]; | ||
|  |     BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:attachmentFilePath]; | ||
|  |     XCTAssert(fileExists); | ||
| 
											8 years ago
										 |     XCTAssertEqual(1, [self numberOfItemsInAttachmentsFolder]); | ||
|  | 
 | ||
|  |     XCTestExpectation *expectation = [self expectationWithDescription:@"Cleanup"]; | ||
|  |     [OWSOrphanedDataCleaner auditAndCleanupAsync:^{ | ||
|  |         [expectation fulfill]; | ||
|  |     }]; | ||
|  |     [self waitForExpectationsWithTimeout:5.0 | ||
|  |                                  handler:^(NSError *error) { | ||
|  |                                      if (error) { | ||
|  |                                          XCTFail(@"Expectation Failed with error: %@", error); | ||
|  |                                      } | ||
|  |                                  }]; | ||
| 
											9 years ago
										 | 
 | ||
|  |     fileExists = [[NSFileManager defaultManager] fileExistsAtPath:attachmentFilePath]; | ||
|  |     XCTAssert(fileExists); | ||
| 
											8 years ago
										 |     XCTAssertEqual(1, [self numberOfItemsInAttachmentsFolder]); | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
|  | - (void)testFilesWithoutAttachmentStreamsAreDeleted | ||
|  | { | ||
| 
											9 years ago
										 |     NSError *error; | ||
| 
											9 years ago
										 |     TSAttachmentStream *attachmentStream = [[TSAttachmentStream alloc] initWithContentType:@"image/jpeg" sourceFilename:nil]; | ||
| 
											9 years ago
										 |     [attachmentStream writeData:[NSData new] error:&error]; | ||
| 
											9 years ago
										 |     // Intentionally not saved, because we want a lingering file. | ||
| 
											9 years ago
										 | 
 | ||
| 
											9 years ago
										 |     NSString *orphanedFilePath = [attachmentStream filePath]; | ||
|  |     BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:orphanedFilePath]; | ||
|  |     XCTAssert(fileExists); | ||
| 
											8 years ago
										 |     XCTAssertEqual(1, [self numberOfItemsInAttachmentsFolder]); | ||
|  | 
 | ||
|  |     XCTestExpectation *expectation = [self expectationWithDescription:@"Cleanup"]; | ||
|  |     [OWSOrphanedDataCleaner auditAndCleanupAsync:^{ | ||
|  |         [expectation fulfill]; | ||
|  |     }]; | ||
|  |     [self waitForExpectationsWithTimeout:5.0 | ||
|  |                                  handler:^(NSError *error) { | ||
|  |                                      if (error) { | ||
|  |                                          XCTFail(@"Expectation Failed with error: %@", error); | ||
|  |                                      } | ||
|  |                                  }]; | ||
| 
											9 years ago
										 | 
 | ||
|  |     fileExists = [[NSFileManager defaultManager] fileExistsAtPath:orphanedFilePath]; | ||
|  |     XCTAssertFalse(fileExists); | ||
| 
											8 years ago
										 |     XCTAssertEqual(0, [self numberOfItemsInAttachmentsFolder]); | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
|  | @end |