Signal-iOS/SignalServiceKit/tests/Messages/Interactions/TSOutgoingMessageTest.swift
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

307 lines
16 KiB
Swift

//
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
@testable import SignalServiceKit
import XCTest
class TSOutgoingMessageTest: SSKBaseTestSwift {
override func setUp() {
super.setUp()
tsAccountManager.registerForTests(withLocalNumber: "+17775550101", uuid: UUID(), pni: UUID())
_ = identityManager.generateNewIdentityKey(for: .aci)
_ = identityManager.generateNewIdentityKey(for: .pni)
}
func testIsUrgent() {
write { transaction in
let otherAddress = SignalServiceAddress(phoneNumber: "+12223334444")
let thread = TSContactThread.getOrCreateThread(withContactAddress: otherAddress, transaction: transaction)
let messageBuilder = TSOutgoingMessageBuilder.outgoingMessageBuilder(thread: thread, messageBody: nil)
messageBuilder.timestamp = 100
let message = messageBuilder.build(transaction: transaction)
XCTAssertTrue(message.isUrgent)
}
}
func testShouldNotStartExpireTimerWithMessageThatDoesNotExpire() {
write { transaction in
let otherAddress = SignalServiceAddress(phoneNumber: "+12223334444")
let thread = TSContactThread.getOrCreateThread(withContactAddress: otherAddress, transaction: transaction)
let messageBuilder = TSOutgoingMessageBuilder.outgoingMessageBuilder(thread: thread, messageBody: nil)
messageBuilder.timestamp = 100
let message = messageBuilder.build(transaction: transaction)
XCTAssertFalse(message.shouldStartExpireTimer())
message.update(withSentRecipient: otherAddress, wasSentByUD: false, transaction: transaction)
XCTAssertFalse(message.shouldStartExpireTimer())
}
}
func testShouldStartExpireTimerWithSentMessage() {
write { transaction in
let otherAddress = SignalServiceAddress(phoneNumber: "+12223334444")
let thread = TSContactThread.getOrCreateThread(withContactAddress: otherAddress, transaction: transaction)
let messageBuilder = TSOutgoingMessageBuilder.outgoingMessageBuilder(thread: thread, messageBody: nil)
messageBuilder.timestamp = 100
messageBuilder.expiresInSeconds = 10
let message = messageBuilder.build(transaction: transaction)
XCTAssertFalse(message.shouldStartExpireTimer())
message.update(withSentRecipient: otherAddress, wasSentByUD: false, transaction: transaction)
XCTAssertTrue(message.shouldStartExpireTimer())
}
}
func testShouldNotStartExpireTimerWithAttemptingOutMessage() {
write { transaction in
let otherAddress = SignalServiceAddress(phoneNumber: "+12223334444")
let thread = TSContactThread.getOrCreateThread(withContactAddress: otherAddress, transaction: transaction)
let messageBuilder = TSOutgoingMessageBuilder.outgoingMessageBuilder(thread: thread, messageBody: nil)
messageBuilder.timestamp = 100
messageBuilder.expiresInSeconds = 10
let message = messageBuilder.build(transaction: transaction)
message.updateAllUnsentRecipientsAsSending(transaction: transaction)
XCTAssertFalse(message.shouldStartExpireTimer())
}
}
func testNoPniSignatureByDefault() {
write { transaction in
let otherAddress = SignalServiceAddress(uuid: UUID(), phoneNumber: "+12223334444")
let thread = TSContactThread.getOrCreateThread(withContactAddress: otherAddress, transaction: transaction)
let messageBuilder = TSOutgoingMessageBuilder.outgoingMessageBuilder(thread: thread, messageBody: nil)
messageBuilder.timestamp = 100
let message = messageBuilder.build(transaction: transaction)
let messageData = message.buildPlainTextData(thread, transaction: transaction)!
let content = try! SSKProtoContent(serializedData: messageData)
XCTAssertNil(content.pniSignatureMessage)
}
}
func testPniSignatureWhenNeeded() {
write { transaction in
let otherAddress = SignalServiceAddress(uuid: UUID(), phoneNumber: "+12223334444")
identityManager.setShouldSharePhoneNumber(with: otherAddress, transaction: transaction)
let thread = TSContactThread.getOrCreateThread(withContactAddress: otherAddress, transaction: transaction)
let messageBuilder = TSOutgoingMessageBuilder.outgoingMessageBuilder(thread: thread, messageBody: nil)
messageBuilder.timestamp = 100
let message = messageBuilder.build(transaction: transaction)
let messageData = message.buildPlainTextData(thread, transaction: transaction)!
let content = try! SSKProtoContent(serializedData: messageData)
let messagePni = content.pniSignatureMessage!.pni
XCTAssertEqual(messagePni, tsAccountManager.localPni!.data)
let aciKeyPair = identityManager.identityKeyPair(for: .aci, transaction: transaction)!.identityKeyPair
let pniKeyPair = identityManager.identityKeyPair(for: .pni, transaction: transaction)!.identityKeyPair
XCTAssert(try! pniKeyPair.identityKey.verifyAlternateIdentity(
aciKeyPair.identityKey,
signature: content.pniSignatureMessage!.signature!))
}
}
func testReceiptClearsSharePhoneNumber() {
write { transaction in
let otherAddress = SignalServiceAddress(uuid: UUID(), phoneNumber: "+12223334444")
identityManager.setShouldSharePhoneNumber(with: otherAddress, transaction: transaction)
let thread = TSContactThread.getOrCreateThread(withContactAddress: otherAddress, transaction: transaction)
let messageBuilder = TSOutgoingMessageBuilder.outgoingMessageBuilder(thread: thread, messageBody: nil)
messageBuilder.timestamp = Date.ows_millisecondTimestamp()
let message = messageBuilder.build(transaction: transaction)
let messageData = message.buildPlainTextData(thread, transaction: transaction)!
message.update(withSentRecipient: otherAddress, wasSentByUD: true, transaction: transaction)
let payloadId = MessageSendLog.recordPayload(messageData, forMessageBeingSent: message, transaction: transaction) as! Int64
MessageSendLog.recordPendingDelivery(payloadId: payloadId,
recipientUuid: otherAddress.uuid!,
recipientDeviceId: 1,
message: message,
transaction: transaction)
// Nothing changed yet...
XCTAssert(identityManager.shouldSharePhoneNumber(with: otherAddress, transaction: transaction))
message.update(withDeliveredRecipient: otherAddress,
recipientDeviceId: 1,
deliveryTimestamp: NSDate.ows_millisecondTimeStamp(),
context: PassthroughDeliveryReceiptContext(),
transaction: transaction)
XCTAssertFalse(identityManager.shouldSharePhoneNumber(with: otherAddress, transaction: transaction))
}
}
func testReceiptClearsSharePhoneNumberOnlyOnLastDevice() {
write { transaction in
let otherAddress = SignalServiceAddress(uuid: UUID(), phoneNumber: "+12223334444")
identityManager.setShouldSharePhoneNumber(with: otherAddress, transaction: transaction)
let thread = TSContactThread.getOrCreateThread(withContactAddress: otherAddress, transaction: transaction)
let messageBuilder = TSOutgoingMessageBuilder.outgoingMessageBuilder(thread: thread, messageBody: nil)
messageBuilder.timestamp = Date.ows_millisecondTimestamp()
let message = messageBuilder.build(transaction: transaction)
let messageData = message.buildPlainTextData(thread, transaction: transaction)!
message.update(withSentRecipient: otherAddress, wasSentByUD: true, transaction: transaction)
let payloadId = MessageSendLog.recordPayload(messageData, forMessageBeingSent: message, transaction: transaction) as! Int64
MessageSendLog.recordPendingDelivery(payloadId: payloadId,
recipientUuid: otherAddress.uuid!,
recipientDeviceId: 1,
message: message,
transaction: transaction)
MessageSendLog.recordPendingDelivery(payloadId: payloadId,
recipientUuid: otherAddress.uuid!,
recipientDeviceId: 2,
message: message,
transaction: transaction)
// Nothing changed yet...
XCTAssert(identityManager.shouldSharePhoneNumber(with: otherAddress, transaction: transaction))
message.update(withDeliveredRecipient: otherAddress,
recipientDeviceId: 1,
deliveryTimestamp: NSDate.ows_millisecondTimeStamp(),
context: PassthroughDeliveryReceiptContext(),
transaction: transaction)
// Still waiting on device #2!
XCTAssert(identityManager.shouldSharePhoneNumber(with: otherAddress, transaction: transaction))
message.update(withDeliveredRecipient: otherAddress,
recipientDeviceId: 2,
deliveryTimestamp: NSDate.ows_millisecondTimeStamp(),
context: PassthroughDeliveryReceiptContext(),
transaction: transaction)
// There we go.
XCTAssertFalse(identityManager.shouldSharePhoneNumber(with: otherAddress, transaction: transaction))
}
}
func testReceiptDoesNotClearSharePhoneNumberIfNotSealedSender() {
write { transaction in
let otherAddress = SignalServiceAddress(uuid: UUID(), phoneNumber: "+12223334444")
identityManager.setShouldSharePhoneNumber(with: otherAddress, transaction: transaction)
let thread = TSContactThread.getOrCreateThread(withContactAddress: otherAddress, transaction: transaction)
let messageBuilder = TSOutgoingMessageBuilder.outgoingMessageBuilder(thread: thread, messageBody: nil)
messageBuilder.timestamp = Date.ows_millisecondTimestamp()
let message = messageBuilder.build(transaction: transaction)
let messageData = message.buildPlainTextData(thread, transaction: transaction)!
message.update(withSentRecipient: otherAddress, wasSentByUD: false, transaction: transaction)
let payloadId = MessageSendLog.recordPayload(messageData, forMessageBeingSent: message, transaction: transaction) as! Int64
MessageSendLog.recordPendingDelivery(payloadId: payloadId,
recipientUuid: otherAddress.uuid!,
recipientDeviceId: 1,
message: message,
transaction: transaction)
// Nothing changed yet...
XCTAssert(identityManager.shouldSharePhoneNumber(with: otherAddress, transaction: transaction))
message.update(withDeliveredRecipient: otherAddress,
recipientDeviceId: 1,
deliveryTimestamp: NSDate.ows_millisecondTimeStamp(),
context: PassthroughDeliveryReceiptContext(),
transaction: transaction)
// Still not changed!
XCTAssert(identityManager.shouldSharePhoneNumber(with: otherAddress, transaction: transaction))
}
}
func testReceiptDoesNotClearSharePhoneNumberIfNoPniSignature() {
write { transaction in
let otherAddress = SignalServiceAddress(uuid: UUID(), phoneNumber: "+12223334444")
let thread = TSContactThread.getOrCreateThread(withContactAddress: otherAddress, transaction: transaction)
let messageBuilder = TSOutgoingMessageBuilder.outgoingMessageBuilder(thread: thread, messageBody: nil)
messageBuilder.timestamp = Date.ows_millisecondTimestamp()
let message = messageBuilder.build(transaction: transaction)
let messageData = message.buildPlainTextData(thread, transaction: transaction)!
message.update(withSentRecipient: otherAddress, wasSentByUD: true, transaction: transaction)
let payloadId = MessageSendLog.recordPayload(messageData, forMessageBeingSent: message, transaction: transaction) as! Int64
MessageSendLog.recordPendingDelivery(payloadId: payloadId,
recipientUuid: otherAddress.uuid!,
recipientDeviceId: 1,
message: message,
transaction: transaction)
// If we set it now...
XCTAssertFalse(identityManager.shouldSharePhoneNumber(with: otherAddress, transaction: transaction))
identityManager.setShouldSharePhoneNumber(with: otherAddress, transaction: transaction)
message.update(withDeliveredRecipient: otherAddress,
recipientDeviceId: 1,
deliveryTimestamp: NSDate.ows_millisecondTimeStamp(),
context: PassthroughDeliveryReceiptContext(),
transaction: transaction)
// ...it should stay active.
XCTAssert(identityManager.shouldSharePhoneNumber(with: otherAddress, transaction: transaction))
}
}
func testReceiptDoesNotClearSharePhoneNumberIfPniHasChanged() {
let otherAddress = SignalServiceAddress(uuid: UUID(), phoneNumber: "+12223334444")
var message: TSOutgoingMessage!
write { transaction in
identityManager.setShouldSharePhoneNumber(with: otherAddress, transaction: transaction)
let thread = TSContactThread.getOrCreateThread(withContactAddress: otherAddress, transaction: transaction)
let messageBuilder = TSOutgoingMessageBuilder.outgoingMessageBuilder(thread: thread, messageBody: nil)
messageBuilder.timestamp = Date.ows_millisecondTimestamp()
message = messageBuilder.build(transaction: transaction)
let messageData = message.buildPlainTextData(thread, transaction: transaction)!
message.update(withSentRecipient: otherAddress, wasSentByUD: true, transaction: transaction)
let payloadId = MessageSendLog.recordPayload(messageData, forMessageBeingSent: message, transaction: transaction) as! Int64
MessageSendLog.recordPendingDelivery(payloadId: payloadId,
recipientUuid: otherAddress.uuid!,
recipientDeviceId: 1,
message: message,
transaction: transaction)
}
// Change our PNI, using registerForTests(...) instead of updateLocalPhoneNumber(...) because the latter kicks
// off a request to check with the server.
tsAccountManager.registerForTests(withLocalNumber: "+17775550199",
uuid: tsAccountManager.localUuid!,
pni: UUID())
write { transaction in
// Changing your number resets this setting.
XCTAssertFalse(identityManager.shouldSharePhoneNumber(with: otherAddress, transaction: transaction))
identityManager.setShouldSharePhoneNumber(with: otherAddress, transaction: transaction)
message.update(withDeliveredRecipient: otherAddress,
recipientDeviceId: 1,
deliveryTimestamp: NSDate.ows_millisecondTimeStamp(),
context: PassthroughDeliveryReceiptContext(),
transaction: transaction)
// Still on, because our PNI changed!
XCTAssert(identityManager.shouldSharePhoneNumber(with: otherAddress, transaction: transaction))
}
}
}