pEpObjCTests/PEPPassphraseCacheTest.m
author Dirk Zimmermann <dz@pep.security>
Fri, 26 Jun 2020 16:54:34 +0200
branchIOSAD-172
changeset 1474 a37a548a6b3b
parent 1473 066dcbeeb6a0
child 1480 b998c63a4cb6
permissions -rw-r--r--
IOSAD-172 Rename internal constructor
     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 = @[@"", passphrase1, passphrase2];
    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     XCTAssertEqual(self.cache.passphrases.count, passphrases.count + 1);
    65     NSMutableArray *expected = [NSMutableArray arrayWithArray:passphrases];
    66     [expected insertObject:@"" atIndex:0];
    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     [expectedPassphrases removeObjectAtIndex:0];
    81     [expectedPassphrases addObject:latestPassphrase];
    82     [self.cache addPassphrase:latestPassphrase];
    83 
    84     XCTAssertEqual(self.cache.passphrases.count, expectedPassphrases.count + 1);
    85     NSMutableArray *expected = [NSMutableArray arrayWithArray:expectedPassphrases];
    86     [expected insertObject:@"" atIndex:0];
    87     XCTAssertEqualObjects(self.cache.passphrases, expected);
    88 }
    89 
    90 - (void)testTimeout
    91 {
    92     NSTimeInterval timeout = 0.5;
    93     PEPPassphraseCache *ownCache = [[PEPPassphraseCache alloc]
    94                                     initWithPassphraseTimeout:timeout
    95                                     checkExpiryInterval:timeout];
    96 
    97     NSString *ownPassphrase = @"blah";
    98     [ownCache addPassphrase:ownPassphrase];
    99 
   100     NSArray *expectedBefore = @[@"", ownPassphrase];
   101     XCTAssertEqualObjects(ownCache.passphrases, expectedBefore);
   102 
   103     [NSThread sleepForTimeInterval:3*timeout];
   104     XCTAssertEqualObjects(ownCache.passphrases, @[@""]);
   105 }
   106 
   107 @end