pEpObjCTests/PEPPassphraseCacheTest.m
author Dirk Zimmermann <dz@pep.security>
Tue, 30 Jun 2020 17:00:20 +0200
branchIOSAD-172
changeset 1572 f35650fadd3d
parent 1549 cf7b3b9c0e96
permissions -rw-r--r--
IOSAD-172 Passphrase cache itself doesn't deal with empty password
     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, 0);
    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, 1);
    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, 2);
    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);
    67     XCTAssertEqualObjects(self.cache.passphrases, expected);
    68 }
    69 
    70 - (void)testTwentyOnePassphrases
    71 {
    72     NSMutableArray *expectedPassphrases = [NSMutableArray arrayWithCapacity:20];
    73     for (NSUInteger i = 1; i <= 20; ++i) {
    74         NSString *newPhrase = [NSString stringWithFormat:@"passphrase_%lu", (unsigned long) i];
    75         [expectedPassphrases addObject:newPhrase];
    76         [self.cache addPassphrase:newPhrase];
    77     }
    78 
    79     NSString *latestPassphrase = @"theLatest";
    80     [self.cache addPassphrase:latestPassphrase];
    81     [expectedPassphrases addObject:latestPassphrase];
    82 
    83     // Last added passphrase is the newest, so it comes first.
    84     expectedPassphrases = [NSMutableArray arrayWithArray:[self reversedArray:expectedPassphrases]];
    85 
    86     // There are 21 passphrases, so the oldest (last) is removed.
    87     [expectedPassphrases removeLastObject];
    88 
    89     XCTAssertEqualObjects(self.cache.passphrases, expectedPassphrases);
    90 }
    91 
    92 - (void)testTimeout
    93 {
    94     NSTimeInterval timeout = 0.2;
    95     PEPPassphraseCache *ownCache = [[PEPPassphraseCache alloc]
    96                                     initWithPassphraseTimeout:timeout
    97                                     checkExpiryInterval:timeout/2];
    98 
    99     NSString *ownPassphrase = @"blah";
   100     [ownCache addPassphrase:ownPassphrase];
   101 
   102     NSArray *expectedBefore = @[ownPassphrase];
   103     XCTAssertEqualObjects(ownCache.passphrases, expectedBefore);
   104 
   105     [NSThread sleepForTimeInterval:2*timeout];
   106     XCTAssertEqualObjects(ownCache.passphrases, @[]);
   107 }
   108 
   109 - (void)testResetTimeout
   110 {
   111     NSString *passphrase1 = @"somepass1";
   112     NSString *passphrase2 = @"somepass2";
   113     NSString *passphrase3 = @"somepass3";
   114 
   115     [self.cache addPassphrase:passphrase1];
   116     [self.cache addPassphrase:passphrase2];
   117     [self.cache addPassphrase:passphrase3];
   118 
   119     [self.cache resetTimeoutForPassphrase:passphrase1];
   120 
   121     NSArray *expected1 = @[passphrase1, passphrase3, passphrase2];
   122     XCTAssertEqualObjects(self.cache.passphrases, expected1);
   123 
   124     [self.cache resetTimeoutForPassphrase:passphrase3];
   125     NSArray *expected2 = @[passphrase3, passphrase1, passphrase2];
   126     XCTAssertEqualObjects(self.cache.passphrases, expected2);
   127 }
   128 
   129 #pragma mark - Helpers
   130 
   131 - (NSArray *)reversedArray:(NSArray *)array
   132 {
   133     NSMutableArray *reversedArray = [NSMutableArray arrayWithCapacity:array.count];
   134     for (id obj in [array reverseObjectEnumerator]) {
   135         [reversedArray addObject:obj];
   136     }
   137     return [NSArray arrayWithArray:reversedArray];
   138 }
   139 
   140 @end