Signal-iOS/Signal/test/util/OWSOrphanDataCleanerTest.m
Evan Hahn 370ff654e7
Change license to AGPL
Change license to AGPL

This commit:

- Updates the `LICENSE` file

- Start every file with something like:

      // Copyright YEAR_FIRST_PUBLISHED Signal Messenger, LLC
      // SPDX-License-Identifier: AGPL-3.0-only

---

First, I removed existing license headers with this Ruby 3.1.2 script:

    require 'set'

    EXTENSIONS_TO_CHECK = Set['.h', '.hpp', '.cpp', '.m', '.mm', '.pch', '.swift']

    same = 0
    different = 0

    all_files = `git ls-files`.lines.map { |line| line.strip }
    all_files.each do |relative_path|
      if relative_path == 'Pods'
        next
      end

      unless EXTENSIONS_TO_CHECK.include? File.extname(relative_path)
        next
      end

      path = File.expand_path(relative_path)

      contents = File.read(path)
      new_contents = contents.sub(/\/\/\n\/\/  Copyright .*\n\/\/\n\n/, '')

      if contents == new_contents
        same += 1
      else
        different += 1
      end

      File.write(path, new_contents)
    end

    puts "updated #{different} file(s), left #{same} untouched"

I'm sure this script could be improved, but it worked well enough.

Then, I created `Scripts/lint/lint-license-headers` and ran it to auto-
fix a lot of files. This changed the mode of some files, but I think
that's actually desirable. For example,
`SignalServiceKit/src/Util/AppContext.m` previously had a mode of
`0755/-rwxr-xr-x`, and it's now `0644/-rw-r--r--`.

Then I fixed some stragglers and updated the precommit script.

See [a similar change in the Desktop app][0].

[0]: 8bfaf598af
2022-10-13 08:25:37 -05:00

240 lines
9.2 KiB
Objective-C

//
// Copyright 2018 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
#import "SignalBaseTest.h"
#import <SignalMessaging/OWSOrphanDataCleaner.h>
#import <SignalServiceKit/OWSDevice.h>
#import <SignalServiceKit/TSAttachmentStream.h>
#import <SignalServiceKit/TSContactThread.h>
#import <SignalServiceKit/TSIncomingMessage.h>
@interface OWSOrphanDataCleaner (Test)
+ (nullable NSSet<NSString *> *)filePathsInDirectorySafe:(NSString *)dirPath;
@end
#pragma mark -
@interface OWSOrphanDataCleanerTest : SignalBaseTest
@end
#pragma mark -
@implementation OWSOrphanDataCleanerTest
#ifdef BROKEN_TESTS
- (void)setUp
{
[super setUp];
// Set up initial conditions & Sanity check
XCTAssertEqual(0, [self numberOfItemsInAttachmentsFolder]);
XCTAssertEqual(0, [TSAttachmentStream numberOfKeysInCollection]);
XCTAssertEqual(0, [TSIncomingMessage numberOfKeysInCollection]);
XCTAssertEqual(0, [TSThread numberOfKeysInCollection]);
}
- (int)numberOfItemsInAttachmentsFolder
{
NSString *legacyAttachmentsDirPath = TSAttachmentStream.legacyAttachmentsDirPath;
NSSet<NSString *> *_Nullable legacyAttachmentFilePaths =
[OWSOrphanDataCleaner filePathsInDirectorySafe:legacyAttachmentsDirPath];
NSString *sharedDataAttachmentsDirPath = TSAttachmentStream.sharedDataAttachmentsDirPath;
NSSet<NSString *> *_Nullable sharedDataAttachmentFilePaths =
[OWSOrphanDataCleaner filePathsInDirectorySafe:sharedDataAttachmentsDirPath];
NSMutableSet<NSString *> *attachmentFilePaths = [NSMutableSet new];
if (legacyAttachmentFilePaths) {
[attachmentFilePaths unionSet:legacyAttachmentFilePaths];
}
if (sharedDataAttachmentFilePaths) {
[attachmentFilePaths unionSet:sharedDataAttachmentFilePaths];
}
return (int)attachmentFilePaths.count;
}
- (TSIncomingMessage *)createIncomingMessageWithThread:(TSThread *)thread
attachmentIds:(NSArray<NSString *> *)attachmentIds
{
TSIncomingMessageBuilder *incomingMessageBuilder =
[TSIncomingMessageBuilder incomingMessageBuilderWithThread:thread messageBody:@"Test body"];
incomingMessageBuilder.authorAddress = [[SignalServiceAddress alloc] initWithPhoneNumber:@"fake-author-id"];
incomingMessageBuilder.timestamp = 1;
incomingMessageBuilder.attachmentIds = attachmentIds;
TSIncomingMessage *incomingMessage = [incomingMessageBuilder build];
[incomingMessage save];
return incomingMessage;
}
- (TSAttachmentStream *)createAttachmentStream
{
NSError *error;
TSAttachmentStream *attachmentStream =
[[TSAttachmentStream alloc] initWithContentType:OWSMimeTypeImageJpeg byteCount:12 sourceFilename:nil];
[attachmentStream writeData:[NSData new] error:&error];
XCTAssertNil(error);
[attachmentStream save];
return attachmentStream;
}
- (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"];
__unused TSIncomingMessage *incomingMessage =
[self createIncomingMessageWithThread:unsavedThread attachmentIds:@[]];
XCTAssertEqual(1, [TSIncomingMessage numberOfKeysInCollection]);
XCTestExpectation *expectation = [self expectationWithDescription:@"Cleanup"];
[OWSOrphanDataCleaner auditAndCleanup:YES
completion:^{
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5.0
handler:^(NSError *error) {
if (error) {
XCTFail(@"Expectation Failed with error: %@", error);
}
}];
XCTAssertEqual(0, [TSIncomingMessage numberOfKeysInCollection]);
}
- (void)testInteractionsWithThreadAreNotDeleted
{
TSContactThread *savedThread = [[TSContactThread alloc] initWithUniqueId:@"this-thread-exists"];
[savedThread save];
__unused TSIncomingMessage *incomingMessage = [self createIncomingMessageWithThread:savedThread attachmentIds:@[]];
XCTAssertEqual(1, [TSIncomingMessage numberOfKeysInCollection]);
XCTestExpectation *expectation = [self expectationWithDescription:@"Cleanup"];
[OWSOrphanDataCleaner auditAndCleanup:YES
completion:^{
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5.0
handler:^(NSError *error) {
if (error) {
XCTFail(@"Expectation Failed with error: %@", error);
}
}];
XCTAssertEqual(1, [TSIncomingMessage numberOfKeysInCollection]);
}
- (void)testFilesWithoutInteractionsAreDeleted
{
// sanity check
XCTAssertEqual(0, [self numberOfItemsInAttachmentsFolder]);
TSAttachmentStream *attachmentStream = [self createAttachmentStream];
NSString *orphanFilePath = [attachmentStream originalFilePath];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:orphanFilePath];
XCTAssert(fileExists);
XCTAssertEqual(1, [self numberOfItemsInAttachmentsFolder]);
// Do multiple cleanup passes.
for (int i = 0; i < 2; i++) {
XCTestExpectation *expectation = [self expectationWithDescription:@"Cleanup"];
[OWSOrphanDataCleaner auditAndCleanup:YES
completion:^{
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5.0
handler:^(NSError *error) {
if (error) {
XCTFail(@"Expectation Failed with error: %@", error);
}
}];
}
fileExists = [[NSFileManager defaultManager] fileExistsAtPath:orphanFilePath];
XCTAssertFalse(fileExists);
XCTAssertEqual(0, [self numberOfItemsInAttachmentsFolder]);
}
- (void)testFilesWithInteractionsAreNotDeleted
{
TSContactThread *savedThread = [[TSContactThread alloc] initWithUniqueId:@"this-thread-exists"];
[savedThread save];
TSAttachmentStream *attachmentStream = [self createAttachmentStream];
__unused TSIncomingMessage *incomingMessage =
[self createIncomingMessageWithThread:savedThread attachmentIds:@[ attachmentStream.uniqueId ]];
NSString *attachmentFilePath = [attachmentStream originalFilePath];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:attachmentFilePath];
XCTAssert(fileExists);
XCTAssertEqual(1, [self numberOfItemsInAttachmentsFolder]);
XCTestExpectation *expectation = [self expectationWithDescription:@"Cleanup"];
[OWSOrphanDataCleaner auditAndCleanup:YES
completion:^{
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5.0
handler:^(NSError *error) {
if (error) {
XCTFail(@"Expectation Failed with error: %@", error);
}
}];
fileExists = [[NSFileManager defaultManager] fileExistsAtPath:attachmentFilePath];
XCTAssert(fileExists);
XCTAssertEqual(1, [self numberOfItemsInAttachmentsFolder]);
}
- (void)testFilesWithoutAttachmentStreamsAreDeleted
{
NSError *error;
TSAttachmentStream *attachmentStream =
[[TSAttachmentStream alloc] initWithContentType:OWSMimeTypeImageJpeg byteCount:0 sourceFilename:nil];
[attachmentStream writeData:[NSData new] error:&error];
// Intentionally not saved, because we want a lingering file.
NSString *orphanFilePath = [attachmentStream originalFilePath];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:orphanFilePath];
XCTAssert(fileExists);
XCTAssertEqual(1, [self numberOfItemsInAttachmentsFolder]);
XCTestExpectation *expectation = [self expectationWithDescription:@"Cleanup"];
[OWSOrphanDataCleaner auditAndCleanup:YES
completion:^{
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:5.0
handler:^(NSError *error) {
if (error) {
XCTFail(@"Expectation Failed with error: %@", error);
}
}];
fileExists = [[NSFileManager defaultManager] fileExistsAtPath:orphanFilePath];
XCTAssertFalse(fileExists);
XCTAssertEqual(0, [self numberOfItemsInAttachmentsFolder]);
}
#endif
@end