pEpObjCTests/PEPPassphraseCacheTest.m
author Dirk Zimmermann <dz@pep.security>
Mon, 29 Jun 2020 16:39:43 +0200
branchIOSAD-172
changeset 1546 4a258d6b7b93
parent 1545 5ca27ed9f55b
child 1549 cf7b3b9c0e96
permissions -rw-r--r--
IOSAD-172 Test passphrase timeout reset
     1 //
     2 //  PEPPassphraseCacheTest.m
     3 //  pEpObjCAdapterTests
     4 //
     5 //  Created by Dirk Zimmermann on 25.06.20.
     6 //  Copyright © 2020 p≡p. All rights reserved.
     7 //
     8 
     9 #import <XCTest/XCTest.h>
    10 
    11 #import "PEPPassphraseCache.h"
    12 #import "PEPPassphraseCacheInternal.h"
    13 
    14 @interface PEPPassphraseCacheTest : XCTestCase
    15 
    16 @property (nonatomic) PEPPassphraseCache *cache;
    17 
    18 @end
    19 
    20 @implementation PEPPassphraseCacheTest
    21 
    22 - (void)setUp
    23 {
    24     self.cache = [[PEPPassphraseCache alloc] init];
    25 }
    26 
    27 - (void)testContainsEmptyPassphrase
    28 {
    29     XCTAssertEqual(self.cache.passphrases.count, 1);
    30     XCTAssertEqualObjects(self.cache.passphrases, @[@""]);
    31 }
    32 
    33 - (void)testContainsSetPassphrase
    34 {
    35     NSString *passphrase = @"somepass";
    36     [self.cache addPassphrase:passphrase];
    37     XCTAssertEqual(self.cache.passphrases.count, 2);
    38     NSArray *expected = @[@"", passphrase];
    39     XCTAssertEqualObjects(self.cache.passphrases, expected);
    40 }
    41 
    42 - (void)testContainsSetPassphrases
    43 {
    44     NSString *passphrase1 = @"somepass1";
    45     NSString *passphrase2 = @"somepass2";
    46 
    47     [self.cache addPassphrase:passphrase1];
    48     [self.cache addPassphrase:passphrase2];
    49 
    50     XCTAssertEqual(self.cache.passphrases.count, 3);
    51     NSArray *expected = @[@"", passphrase2, passphrase1];
    52     XCTAssertEqualObjects(self.cache.passphrases, expected);
    53 }
    54 
    55 - (void)testTwentyPassphrases
    56 {
    57     NSMutableArray *passphrases = [NSMutableArray arrayWithCapacity:20];
    58     for (NSUInteger i = 1; i <= 20; ++i) {
    59         NSString *newPhrase = [NSString stringWithFormat:@"passphrase_%lu", (unsigned long) i];
    60         [passphrases addObject:newPhrase];
    61         [self.cache addPassphrase:newPhrase];
    62     }
    63 
    64     NSMutableArray *expected = [NSMutableArray arrayWithArray:[self reversedArray:passphrases]];
    65 
    66     XCTAssertEqual(self.cache.passphrases.count, expected.count + 1);
    67     [expected insertObject:@"" atIndex:0];
    68     XCTAssertEqualObjects(self.cache.passphrases, expected);
    69 }
    70 
    71 - (void)testTwentyOnePassphrases
    72 {
    73     NSMutableArray *expectedPassphrases = [NSMutableArray arrayWithCapacity:20];
    74     for (NSUInteger i = 1; i <= 20; ++i) {
    75         NSString *newPhrase = [NSString stringWithFormat:@"passphrase_%lu", (unsigned long) i];
    76         [expectedPassphrases addObject:newPhrase];
    77         [self.cache addPassphrase:newPhrase];
    78     }
    79 
    80     NSString *latestPassphrase = @"theLatest";
    81 
    82     [self.cache addPassphrase:latestPassphrase];
    83 
    84     expectedPassphrases = [NSMutableArray arrayWithArray:[self reversedArray:expectedPassphrases]];
    85     [expectedPassphrases replaceObjectAtIndex:0 withObject:latestPassphrase];
    86 
    87     [expectedPassphrases insertObject:@"" atIndex:0];
    88     XCTAssertEqualObjects(self.cache.passphrases, expectedPassphrases);
    89 }
    90 
    91 - (void)testTimeout
    92 {
    93     NSTimeInterval timeout = 0.2;
    94     PEPPassphraseCache *ownCache = [[PEPPassphraseCache alloc]
    95                                     initWithPassphraseTimeout:timeout
    96                                     checkExpiryInterval:timeout/2];
    97 
    98     NSString *ownPassphrase = @"blah";
    99     [ownCache addPassphrase:ownPassphrase];
   100 
   101     NSArray *expectedBefore = @[@"", ownPassphrase];
   102     XCTAssertEqualObjects(ownCache.passphrases, expectedBefore);
   103 
   104     [NSThread sleepForTimeInterval:2*timeout];
   105     XCTAssertEqualObjects(ownCache.passphrases, @[@""]);
   106 }
   107 
   108 - (void)testResetTimeout
   109 {
   110     NSString *passphrase1 = @"somepass1";
   111     NSString *passphrase2 = @"somepass2";
   112     NSString *passphrase3 = @"somepass3";
   113 
   114     [self.cache addPassphrase:passphrase1];
   115     [self.cache addPassphrase:passphrase2];
   116     [self.cache addPassphrase:passphrase3];
   117 
   118     [self.cache resetTimeoutForPassphrase:passphrase1];
   119 
   120     NSArray *expected1 = @[@"", passphrase1, passphrase3, passphrase2];
   121     XCTAssertEqualObjects(self.cache.passphrases, expected1);
   122 
   123     [self.cache resetTimeoutForPassphrase:passphrase3];
   124     NSArray *expected2 = @[@"", passphrase3, passphrase1, passphrase2];
   125     XCTAssertEqualObjects(self.cache.passphrases, expected2);
   126 }
   127 
   128 #pragma mark - Helpers
   129 
   130 - (NSArray *)reversedArray:(NSArray *)array
   131 {
   132     NSMutableArray *reversedArray = [NSMutableArray arrayWithCapacity:array.count];
   133     for (id obj in [array reverseObjectEnumerator]) {
   134         [reversedArray addObject:obj];
   135     }
   136     return [NSArray arrayWithArray:reversedArray];
   137 }
   138 
   139 @end