pEpObjCTests/PEPPassphraseCacheTest.m
author Dirk Zimmermann <dz@pep.security>
Mon, 29 Jun 2020 16:39:43 +0200
branchIOSAD-172
changeset 1549 cf7b3b9c0e96
parent 1546 4a258d6b7b93
child 1572 f35650fadd3d
permissions -rw-r--r--
IOSAD-172 Fix testTwentyOnePassphrases
     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     [self.cache addPassphrase:latestPassphrase];
    82     [expectedPassphrases addObject:latestPassphrase];
    83 
    84     // Last added passphrase is the newest, so it comes first.
    85     expectedPassphrases = [NSMutableArray arrayWithArray:[self reversedArray:expectedPassphrases]];
    86 
    87     // There are 21 passphrases, so the oldest (last) is removed.
    88     [expectedPassphrases removeLastObject];
    89 
    90     [expectedPassphrases insertObject:@"" atIndex:0];
    91     XCTAssertEqualObjects(self.cache.passphrases, expectedPassphrases);
    92 }
    93 
    94 - (void)testTimeout
    95 {
    96     NSTimeInterval timeout = 0.2;
    97     PEPPassphraseCache *ownCache = [[PEPPassphraseCache alloc]
    98                                     initWithPassphraseTimeout:timeout
    99                                     checkExpiryInterval:timeout/2];
   100 
   101     NSString *ownPassphrase = @"blah";
   102     [ownCache addPassphrase:ownPassphrase];
   103 
   104     NSArray *expectedBefore = @[@"", ownPassphrase];
   105     XCTAssertEqualObjects(ownCache.passphrases, expectedBefore);
   106 
   107     [NSThread sleepForTimeInterval:2*timeout];
   108     XCTAssertEqualObjects(ownCache.passphrases, @[@""]);
   109 }
   110 
   111 - (void)testResetTimeout
   112 {
   113     NSString *passphrase1 = @"somepass1";
   114     NSString *passphrase2 = @"somepass2";
   115     NSString *passphrase3 = @"somepass3";
   116 
   117     [self.cache addPassphrase:passphrase1];
   118     [self.cache addPassphrase:passphrase2];
   119     [self.cache addPassphrase:passphrase3];
   120 
   121     [self.cache resetTimeoutForPassphrase:passphrase1];
   122 
   123     NSArray *expected1 = @[@"", passphrase1, passphrase3, passphrase2];
   124     XCTAssertEqualObjects(self.cache.passphrases, expected1);
   125 
   126     [self.cache resetTimeoutForPassphrase:passphrase3];
   127     NSArray *expected2 = @[@"", passphrase3, passphrase1, passphrase2];
   128     XCTAssertEqualObjects(self.cache.passphrases, expected2);
   129 }
   130 
   131 #pragma mark - Helpers
   132 
   133 - (NSArray *)reversedArray:(NSArray *)array
   134 {
   135     NSMutableArray *reversedArray = [NSMutableArray arrayWithCapacity:array.count];
   136     for (id obj in [array reverseObjectEnumerator]) {
   137         [reversedArray addObject:obj];
   138     }
   139     return [NSArray arrayWithArray:reversedArray];
   140 }
   141 
   142 @end