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