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

99 lines
2.9 KiB
Objective-C

//
// Copyright 2019 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
#import "SDSDatabaseStorage+Objc.h"
#import "SSKBaseTestObjC.h"
#import "SSKPreKeyStore.h"
#import "SignedPrekeyRecord.h"
#import <SignalServiceKit/OWSIdentityManager.h>
#import <SignalServiceKit/SignalServiceKit-Swift.h>
@interface SSKPreKeyStoreTests : SSKBaseTestObjC
@end
#pragma mark -
@interface SSKPreKeyStore (Tests)
@end
#pragma mark -
@implementation SSKPreKeyStore (Tests)
- (void)storePreKeyRecords:(NSArray<PreKeyRecord *> *)preKeyRecords
{
DatabaseStorageWrite(self.databaseStorage,
^(SDSAnyWriteTransaction *transaction) { [self storePreKeyRecords:preKeyRecords transaction:transaction]; })
}
- (nullable PreKeyRecord *)loadPreKey:(int)preKeyId
{
__block PreKeyRecord *_Nullable result;
[self.databaseStorage readWithBlock:^(SDSAnyReadTransaction *transaction) {
result = [self loadPreKey:preKeyId transaction:transaction];
}];
return result;
}
@end
#pragma mark -
@implementation SSKPreKeyStoreTests
- (SSKPreKeyStore *)preKeyStore
{
return [self signalProtocolStoreForIdentity:OWSIdentityACI].preKeyStore;
}
- (void)testGeneratingAndStoringPreKeys
{
NSArray *generatedKeys = [self.preKeyStore generatePreKeyRecords];
XCTAssert([generatedKeys count] == 100, @"Not hundred keys generated");
[self.preKeyStore storePreKeyRecords:generatedKeys];
PreKeyRecord *lastPreKeyRecord = [generatedKeys lastObject];
PreKeyRecord *firstPreKeyRecord = [generatedKeys firstObject];
XCTAssert([[self.preKeyStore loadPreKey:lastPreKeyRecord.Id].keyPair.publicKey
isEqualToData:lastPreKeyRecord.keyPair.publicKey]);
XCTAssert([[self.preKeyStore loadPreKey:firstPreKeyRecord.Id].keyPair.publicKey
isEqualToData:firstPreKeyRecord.keyPair.publicKey]);
SSKPreKeyStore *pniStore = [self signalProtocolStoreForIdentity:OWSIdentityPNI].preKeyStore;
XCTAssertNil([pniStore loadPreKey:firstPreKeyRecord.Id]);
}
- (void)testRemovingPreKeys
{
NSArray *generatedKeys = [self.preKeyStore generatePreKeyRecords];
XCTAssert([generatedKeys count] == 100, @"Not hundred keys generated");
[self.preKeyStore storePreKeyRecords:generatedKeys];
PreKeyRecord *lastPreKeyRecord = [generatedKeys lastObject];
PreKeyRecord *firstPreKeyRecord = [generatedKeys firstObject];
SSKPreKeyStore *pniStore = [self signalProtocolStoreForIdentity:OWSIdentityPNI].preKeyStore;
[pniStore storePreKeyRecords:generatedKeys];
[self writeWithBlock:^(SDSAnyWriteTransaction *transaction) {
[self.preKeyStore removePreKey:lastPreKeyRecord.Id transaction:transaction];
}];
XCTAssertNil([self.preKeyStore loadPreKey:lastPreKeyRecord.Id]);
XCTAssertNotNil([self.preKeyStore loadPreKey:firstPreKeyRecord.Id]);
XCTAssertNotNil([pniStore loadPreKey:firstPreKeyRecord.Id]);
XCTAssertNotNil([pniStore loadPreKey:firstPreKeyRecord.Id]);
}
@end