Signal-iOS/SignalServiceKit/tests/Contacts/PhoneNumberTest.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

113 lines
5.2 KiB
Swift

//
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import XCTest
import SignalServiceKit
class PhoneNumberTestSwift: SSKBaseTestSwift {
func testTryParsePhoneNumberTextOnly() {
let testCases: [String: String?] = [
// Phone numbers with explicit region codes
"+1 (902) 555-0123": "+19025550123",
"1 (902) 555-0123": "+19025550123",
"1-902-555-0123": "+19025550123",
"1 902 555 0123": "+19025550123",
"1.902.555.0123": "+19025550123",
"+33 1 70 39 38 00": "+33170393800",
// Phone numbers missing a calling code. Assumes local region
"9025550123": "+19025550123",
"902-555-0123": "+19025550123",
"902.555.0123": "+19025550123",
"902 555 0123": "+19025550123",
"(902) 555-0123": "+19025550123",
// Phone numbers outside your region without a plus.
// You must include a plus when dialing outside of your locale.
// This might not be desired, but documents existing behavior.
"33 1 70 39 38 00": nil,
// Phone numbers with a calling code but without a plus
"19025550123": "+19025550123",
// Empty input
"": nil
]
for (input, expected) in testCases {
let actual = PhoneNumber.tryParsePhoneNumber(fromUserSpecifiedText: input)?.toE164()
XCTAssertEqual(actual, expected, input)
}
}
func testTryParsePhoneNumberWithCallingCode() {
XCTAssertEqual(
PhoneNumber.tryParsePhoneNumber(fromUserSpecifiedText: "18085550101", callingCode: "1")?.toE164(),
"+18085550101"
)
XCTAssertEqual(
PhoneNumber.tryParsePhoneNumber(fromUserSpecifiedText: "61255504321", callingCode: "61")?.toE164(),
"+61255504321"
)
XCTAssertEqual(
PhoneNumber.tryParsePhoneNumber(fromUserSpecifiedText: "493083050", callingCode: "49")?.toE164(),
"+493083050"
)
}
func test_mx_transition() {
// MX recently removed the mobile 1. So 521xxx numbers can now be dialed on PTSN as 52xxx
// But legacy users registered using the 521xxx format, and their signal account/sessions are matched to that number
// so we need to be permissive about matching *either* format until we have a way to migrate client phone numbers.
let expectedCandidates: Set<String> = ["+528341639157", "+5218341639157"]
Assert(parsingRawText: "528341639157", localE164: "+13213214321", includesCandidates: expectedCandidates)
Assert(parsingRawText: "5218341639157", localE164: "+13213214321", includesCandidates: expectedCandidates)
// Local MX number infers mexican country when parsing numbers, includes candidates with and without mobile 1.
Assert(parsingRawText: "8341639157", localE164: "+528341635555", includesCandidates: expectedCandidates)
Assert(parsingRawText: "18341639157", localE164: "+528341635555", includesCandidates: expectedCandidates)
Assert(parsingRawText: "8341639157", localE164: "+5218341634444", includesCandidates: expectedCandidates)
Assert(parsingRawText: "18341639157", localE164: "+5218341634444", includesCandidates: expectedCandidates)
Assert(parsingRawText: "528341639157", localE164: "+5218341634444", includesCandidates: expectedCandidates)
Assert(parsingRawText: "5218341639157", localE164: "+5218341634444", includesCandidates: expectedCandidates)
}
func test_resemblesE164() {
XCTAssertTrue(PhoneNumber.resemblesE164("+5218341639157"))
XCTAssertFalse(PhoneNumber.resemblesE164("5218341639157"))
XCTAssertTrue(PhoneNumber.resemblesE164("+18018108311"))
XCTAssertFalse(PhoneNumber.resemblesE164("18018108311"))
// E164 must have leading +.
XCTAssertFalse(PhoneNumber.resemblesE164("a18018108311"))
// E164 must have exactly 1 leading +.
XCTAssertFalse(PhoneNumber.resemblesE164("++18018108311"))
// E164 must only contains 0-9 arabic digits.
XCTAssertFalse(PhoneNumber.resemblesE164("+18018108311a"))
// E164 must have at least 10 digits.
XCTAssertTrue(PhoneNumber.resemblesE164("+1234567890"))
XCTAssertFalse(PhoneNumber.resemblesE164("+123456789"))
// E164 must have no more than 15 digits.
XCTAssertTrue(PhoneNumber.resemblesE164("+123456789012345"))
XCTAssertFalse(PhoneNumber.resemblesE164("+1234567890123456"))
XCTAssertFalse(PhoneNumber.resemblesE164("+12345678901234567890"))
}
}
func Assert(parsingRawText rawText: String,
localE164: String,
includesCandidates expectedCandidates: Set<String>,
file: StaticString = #file,
line: UInt = #line) {
let contactFactory = ContactFactory()
contactFactory.localClientPhonenumber = localE164
contactFactory.userTextPhoneNumberAndLabelBuilder = {
return [(rawText, "Main")]
}
let contact = try! contactFactory.build()
let actual = contact.e164sForIntersection
let missingNumbers = expectedCandidates.subtracting(actual)
XCTAssert(missingNumbers == [], "missing candidates: \(missingNumbers)", file: file, line: line)
}