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.
222 lines
8.4 KiB
Matlab
222 lines
8.4 KiB
Matlab
8 years ago
|
//
|
||
7 years ago
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||
8 years ago
|
//
|
||
9 years ago
|
|
||
7 years ago
|
#import "OWSDevice.h"
|
||
7 years ago
|
#import "OWSOrphanDataCleaner.h"
|
||
7 years ago
|
#import "OWSPrimaryStorage.h"
|
||
7 years ago
|
#import "SSKBaseTest.h"
|
||
9 years ago
|
#import "TSAttachmentStream.h"
|
||
|
#import "TSContactThread.h"
|
||
|
#import "TSIncomingMessage.h"
|
||
|
|
||
7 years ago
|
@interface OWSOrphanDataCleanerTest : SSKBaseTest
|
||
9 years ago
|
|
||
|
@end
|
||
|
|
||
8 years ago
|
#pragma mark -
|
||
|
|
||
7 years ago
|
@implementation OWSOrphanDataCleanerTest
|
||
9 years ago
|
|
||
|
- (void)setUp
|
||
|
{
|
||
|
[super setUp];
|
||
|
// Register views, etc.
|
||
7 years ago
|
[OWSPrimaryStorage registerExtensionsWithMigrationBlock:^{
|
||
|
}];
|
||
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
|
||
|
{
|
||
7 years ago
|
return [OWSOrphanDataCleaner filePathsInAttachmentsFolder].count;
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
- (TSIncomingMessage *)createIncomingMessageWithThread:(TSThread *)thread
|
||
|
attachmentIds:(NSArray<NSString *> *)attachmentIds
|
||
|
{
|
||
|
TSIncomingMessage *incomingMessage =
|
||
|
[[TSIncomingMessage alloc] initIncomingMessageWithTimestamp:1
|
||
|
inThread:thread
|
||
|
authorId:@"fake-author-id"
|
||
|
sourceDeviceId:OWSDevicePrimaryDeviceId
|
||
|
messageBody:@"footch"
|
||
|
attachmentIds:attachmentIds
|
||
|
expiresInSeconds:0
|
||
|
quotedMessage:nil
|
||
|
contactShare:nil];
|
||
|
[incomingMessage save];
|
||
|
|
||
|
return incomingMessage;
|
||
|
}
|
||
|
|
||
|
- (TSAttachmentStream *)createAttachmentStream
|
||
|
{
|
||
|
NSError *error;
|
||
|
TSAttachmentStream *attachmentStream =
|
||
|
[[TSAttachmentStream alloc] initWithContentType:@"image/jpeg" byteCount:12 sourceFilename:nil];
|
||
|
[attachmentStream writeData:[NSData new] error:&error];
|
||
|
|
||
|
XCTAssertNil(error);
|
||
|
|
||
|
[attachmentStream save];
|
||
|
|
||
|
return attachmentStream;
|
||
|
}
|
||
|
|
||
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"];
|
||
|
|
||
7 years ago
|
__unused TSIncomingMessage *incomingMessage =
|
||
|
[self createIncomingMessageWithThread:unsavedThread attachmentIds:@[]];
|
||
|
|
||
9 years ago
|
XCTAssertEqual(1, [TSIncomingMessage numberOfKeysInCollection]);
|
||
|
|
||
8 years ago
|
XCTestExpectation *expectation = [self expectationWithDescription:@"Cleanup"];
|
||
7 years ago
|
[OWSOrphanDataCleaner auditAndCleanupAsync:^{
|
||
8 years ago
|
[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];
|
||
|
|
||
7 years ago
|
__unused TSIncomingMessage *incomingMessage = [self createIncomingMessageWithThread:savedThread attachmentIds:@[]];
|
||
|
|
||
9 years ago
|
XCTAssertEqual(1, [TSIncomingMessage numberOfKeysInCollection]);
|
||
|
|
||
8 years ago
|
XCTestExpectation *expectation = [self expectationWithDescription:@"Cleanup"];
|
||
7 years ago
|
[OWSOrphanDataCleaner auditAndCleanupAsync:^{
|
||
8 years ago
|
[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
|
|
||
7 years ago
|
TSAttachmentStream *attachmentStream = [self createAttachmentStream];
|
||
|
|
||
9 years ago
|
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"];
|
||
7 years ago
|
[OWSOrphanDataCleaner auditAndCleanupAsync:^{
|
||
8 years ago
|
[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];
|
||
|
|
||
7 years ago
|
TSAttachmentStream *attachmentStream = [self createAttachmentStream];
|
||
9 years ago
|
|
||
7 years ago
|
__unused TSIncomingMessage *incomingMessage =
|
||
|
[self createIncomingMessageWithThread:savedThread attachmentIds:@[ attachmentStream.uniqueId ]];
|
||
9 years ago
|
|
||
|
NSString *attachmentFilePath = [attachmentStream filePath];
|
||
|
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:attachmentFilePath];
|
||
|
XCTAssert(fileExists);
|
||
8 years ago
|
XCTAssertEqual(1, [self numberOfItemsInAttachmentsFolder]);
|
||
|
|
||
|
XCTestExpectation *expectation = [self expectationWithDescription:@"Cleanup"];
|
||
7 years ago
|
[OWSOrphanDataCleaner auditAndCleanupAsync:^{
|
||
8 years ago
|
[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;
|
||
7 years ago
|
TSAttachmentStream *attachmentStream =
|
||
|
[[TSAttachmentStream alloc] initWithContentType:@"image/jpeg" byteCount:0 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"];
|
||
7 years ago
|
[OWSOrphanDataCleaner auditAndCleanupAsync:^{
|
||
8 years ago
|
[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
|