Signal-iOS/SignalServiceKit/tests/Storage/SSKSignedPreKeyStoreTest.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

91 lines
3.9 KiB
Swift

//
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import Foundation
import XCTest
@testable import SignalServiceKit
private extension SSKSignedPreKeyStore {
func loadSignedPreKey(_ id: Int32) -> SignedPreKeyRecord? {
return self.databaseStorage.read { transaction in
loadSignedPreKey(id, transaction: transaction)
}
}
var countSignedPreKeys: Int {
return self.databaseStorage.read { transaction in
loadSignedPreKeys(with: transaction).count
}
}
}
class SSKSignedPreKeyStoreTest: SSKBaseTestSwift {
func testPniStoreIsSeparate() {
let aciStore = signalProtocolStore(for: .aci).signedPreKeyStore
let pniStore = signalProtocolStore(for: .pni).signedPreKeyStore
XCTAssertEqual(0, aciStore.countSignedPreKeys)
XCTAssertEqual(0, pniStore.countSignedPreKeys)
let days: Int32 = 3
let lastPreKeyId = days
for i in 0...days { // 4 signed keys are generated, one per day from now until 3 days ago.
let secondsAgo = TimeInterval(i - days) * kDayInterval
assert(secondsAgo <= 0, "Time in past must be negative")
let generatedAt = Date(timeIntervalSinceNow: secondsAgo)
let record = SignedPreKeyRecord(id: i,
keyPair: Curve25519.generateKeyPair(),
signature: Data(),
generatedAt: generatedAt)
self.databaseStorage.write { transaction in
aciStore.storeSignedPreKey(i, signedPreKeyRecord: record, transaction: transaction)
}
}
XCTAssertEqual(4, aciStore.countSignedPreKeys)
XCTAssertNotNil(aciStore.loadSignedPreKey(lastPreKeyId))
for i in 0...days { // 4 signed keys are generated, one per day from now until 3 days ago.
let secondsAgo = TimeInterval(i - days) * kDayInterval
assert(secondsAgo <= 0, "Time in past must be negative")
let generatedAt = Date(timeIntervalSinceNow: secondsAgo)
let record = SignedPreKeyRecord(id: i,
keyPair: Curve25519.generateKeyPair(),
signature: Data(),
generatedAt: generatedAt)
self.databaseStorage.write { transaction in
pniStore.storeSignedPreKey(i, signedPreKeyRecord: record, transaction: transaction)
}
}
XCTAssertEqual(4, pniStore.countSignedPreKeys)
XCTAssertNotNil(pniStore.loadSignedPreKey(lastPreKeyId))
self.databaseStorage.write { transaction in
aciStore.removeSignedPreKey(lastPreKeyId, transaction: transaction)
}
XCTAssertNil(aciStore.loadSignedPreKey(lastPreKeyId))
XCTAssertNotNil(pniStore.loadSignedPreKey(lastPreKeyId))
}
func testGenerateWithCorrectSignature() {
let aciIdentity = identityManager.generateNewIdentityKey(for: .aci)
let aciStore = signalProtocolStore(for: .aci).signedPreKeyStore
let aciRecord = aciStore.generateRandomSignedRecord()
let aciPublicKey = aciIdentity.identityKeyPair.publicKey
XCTAssert(try! aciPublicKey.verifySignature(message: aciRecord.keyPair.identityKeyPair.publicKey.serialize(),
signature: aciRecord.signature))
let pniIdentity = identityManager.generateNewIdentityKey(for: .pni)
let pniStore = signalProtocolStore(for: .pni).signedPreKeyStore
let pniRecord = pniStore.generateRandomSignedRecord()
let pniPublicKey = pniIdentity.identityKeyPair.publicKey
XCTAssert(try! pniPublicKey.verifySignature(message: pniRecord.keyPair.identityKeyPair.publicKey.serialize(),
signature: pniRecord.signature))
}
}