Signal-iOS/SignalServiceKit/tests/Util/TSMessageStorageTests.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

178 lines
7.8 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Copyright 2018 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
#import "SSKBaseTestObjC.h"
#import "TSAccountManager.h"
#import "TSContactThread.h"
#import "TSGroupThread.h"
#import "TSIncomingMessage.h"
#import "TSMessage.h"
#import "TSOutgoingMessage.h"
#import "TSThread.h"
#import <SignalCoreKit/Cryptography.h>
#import <SignalServiceKit/SignalServiceKit-Swift.h>
@interface TSMessageStorageTests : SSKBaseTestObjC
@property TSContactThread *thread;
@end
#pragma mark -
@implementation TSMessageStorageTests
- (SignalServiceAddress *)localAddress
{
return [[SignalServiceAddress alloc] initWithPhoneNumber:@"+13334445555"];
}
- (SignalServiceAddress *)otherAddress
{
return [[SignalServiceAddress alloc] initWithPhoneNumber:@"+12223334444"];
}
- (void)setUp
{
[super setUp];
// ensure local client has necessary "registered" state
NSString *localE164Identifier = @"+13235551234";
NSUUID *localUUID = NSUUID.UUID;
[self.tsAccountManager registerForTestsWithLocalNumber:localE164Identifier uuid:localUUID];
[self writeWithBlock:^(SDSAnyWriteTransaction *transaction) {
self.thread = [TSContactThread getOrCreateThreadWithContactAddress:self.otherAddress transaction:transaction];
}];
}
- (void)testStoreIncomingMessage
{
__block NSString *messageId;
uint64_t timestamp = 666;
NSString *body
= @"A child born today will grow up with no conception of privacy at all. Theyll never know what it means to "
@"have a private moment to themselves an unrecorded, unanalyzed thought. And thats a problem because "
@"privacy matters; privacy is what allows us to determine who we are and who we want to be.";
[self writeWithBlock:^(SDSAnyWriteTransaction *transaction) {
TSIncomingMessageBuilder *incomingMessageBuilder =
[TSIncomingMessageBuilder incomingMessageBuilderWithThread:self.thread messageBody:body];
incomingMessageBuilder.timestamp = timestamp;
incomingMessageBuilder.authorAddress = self.otherAddress;
incomingMessageBuilder.sourceDeviceId = 1;
TSIncomingMessage *newMessage = [incomingMessageBuilder build];
[newMessage anyInsertWithTransaction:transaction];
messageId = newMessage.uniqueId;
TSIncomingMessage *_Nullable fetchedMessage =
[TSIncomingMessage anyFetchIncomingMessageWithUniqueId:messageId transaction:transaction];
XCTAssertEqualObjects(body, fetchedMessage.body);
XCTAssertFalse(fetchedMessage.hasAttachments);
XCTAssertEqual(timestamp, fetchedMessage.timestamp);
XCTAssertFalse(fetchedMessage.wasRead);
XCTAssertEqualObjects(self.thread.uniqueId, fetchedMessage.uniqueThreadId);
}];
}
- (void)testMessagesDeletedOnThreadDeletion
{
NSString *body
= @"A child born today will grow up with no conception of privacy at all. Theyll never know what it means to "
@"have a private moment to themselves an unrecorded, unanalyzed thought. And thats a problem because "
@"privacy matters; privacy is what allows us to determine who we are and who we want to be.";
[self writeWithBlock:^(SDSAnyWriteTransaction *transaction) {
NSMutableArray<TSIncomingMessage *> *messages = [NSMutableArray new];
for (int i = 0; i < 10; i++) {
TSIncomingMessageBuilder *incomingMessageBuilder =
[TSIncomingMessageBuilder incomingMessageBuilderWithThread:self.thread messageBody:body];
incomingMessageBuilder.timestamp = i + 1;
incomingMessageBuilder.authorAddress = self.otherAddress;
incomingMessageBuilder.sourceDeviceId = 1;
TSIncomingMessage *newMessage = [incomingMessageBuilder build];
[messages addObject:newMessage];
[newMessage anyInsertWithTransaction:transaction];
}
for (TSIncomingMessage *message in messages) {
TSIncomingMessage *_Nullable fetchedMessage =
[TSIncomingMessage anyFetchIncomingMessageWithUniqueId:message.uniqueId transaction:transaction];
XCTAssertEqualObjects(fetchedMessage.body, body, @"Body of incoming message recovered");
XCTAssertEqual(0, fetchedMessage.attachmentIds.count, @"attachments are nil");
XCTAssertEqualObjects(fetchedMessage.uniqueId, message.uniqueId, @"Unique identifier is accurate");
XCTAssertFalse(fetchedMessage.wasRead, @"Message should originally be unread");
XCTAssertEqualObjects(
fetchedMessage.uniqueThreadId, self.thread.uniqueId, @"Isn't stored in the right thread!");
}
[self.thread anyRemoveWithTransaction:transaction];
for (TSIncomingMessage *message in messages) {
TSIncomingMessage *_Nullable fetchedMessage =
[TSIncomingMessage anyFetchIncomingMessageWithUniqueId:message.uniqueId transaction:transaction];
XCTAssertNil(fetchedMessage, @"Message should be deleted!");
}
}];
}
- (void)testGroupMessagesDeletedOnThreadDeletion
{
NSString *body
= @"A child born today will grow up with no conception of privacy at all. Theyll never know what it means to "
@"have a private moment to themselves an unrecorded, unanalyzed thought. And thats a problem because "
@"privacy matters; privacy is what allows us to determine who we are and who we want to be.";
[self writeWithBlock:^(SDSAnyWriteTransaction *transaction) {
TSGroupThread *thread = [GroupManager createGroupForTestsObjcWithMembers:@[
self.localAddress,
self.otherAddress,
]
name:@"fdsfsd"
avatarData:nil
transaction:transaction];
NSMutableArray<TSIncomingMessage *> *messages = [NSMutableArray new];
for (uint64_t i = 0; i < 10; i++) {
NSUInteger memberIdx = (i % thread.groupModel.groupMembers.count);
SignalServiceAddress *authorAddress = thread.groupModel.groupMembers[memberIdx];
TSIncomingMessageBuilder *incomingMessageBuilder =
[TSIncomingMessageBuilder incomingMessageBuilderWithThread:thread messageBody:body];
incomingMessageBuilder.timestamp = i + 1;
incomingMessageBuilder.authorAddress = authorAddress;
incomingMessageBuilder.sourceDeviceId = 1;
TSIncomingMessage *newMessage = [incomingMessageBuilder build];
[newMessage anyInsertWithTransaction:transaction];
[messages addObject:newMessage];
}
for (TSIncomingMessage *message in messages) {
TSIncomingMessage *_Nullable fetchedMessage =
[TSIncomingMessage anyFetchIncomingMessageWithUniqueId:message.uniqueId transaction:transaction];
XCTAssertNotNil(fetchedMessage);
XCTAssertEqualObjects(fetchedMessage.body, body, @"Body of incoming message recovered");
XCTAssertEqual(0, fetchedMessage.attachmentIds.count, @"attachments are empty");
XCTAssertEqualObjects(fetchedMessage.uniqueId, message.uniqueId, @"Unique identifier is accurate");
XCTAssertFalse(fetchedMessage.wasRead, @"Message should originally be unread");
XCTAssertEqualObjects(fetchedMessage.uniqueThreadId, thread.uniqueId, @"Isn't stored in the right thread!");
}
[thread anyRemoveWithTransaction:transaction];
for (TSIncomingMessage *message in messages) {
TSIncomingMessage *_Nullable fetchedMessage =
[TSIncomingMessage anyFetchIncomingMessageWithUniqueId:message.uniqueId transaction:transaction];
XCTAssertNil(fetchedMessage, @"Message should be deleted!");
}
}];
}
@end