pEpObjCTests/PEPPassphraseCacheTest.m
author Dirk Zimmermann <dz@pep.security>
Mon, 29 Jun 2020 16:39:43 +0200
branchIOSAD-172
changeset 1542 c68371dfc71e
parent 1480 b998c63a4cb6
child 1543 674307ef48b9
permissions -rw-r--r--
IOSAD-172 Fix testContainsSetPassphrases
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@1455
    64
    XCTAssertEqual(self.cache.passphrases.count, passphrases.count + 1);
dz@1455
    65
    NSMutableArray *expected = [NSMutableArray arrayWithArray:passphrases];
dz@1455
    66
    [expected insertObject:@"" atIndex:0];
dz@1455
    67
    XCTAssertEqualObjects(self.cache.passphrases, expected);
dz@1455
    68
}
dz@1455
    69
dz@1455
    70
- (void)testTwentyOnePassphrases
dz@1455
    71
{
dz@1458
    72
    NSMutableArray *expectedPassphrases = [NSMutableArray arrayWithCapacity:20];
dz@1462
    73
    for (NSUInteger i = 1; i <= 20; ++i) {
dz@1455
    74
        NSString *newPhrase = [NSString stringWithFormat:@"passphrase_%lu", (unsigned long) i];
dz@1458
    75
        [expectedPassphrases addObject:newPhrase];
dz@1455
    76
        [self.cache addPassphrase:newPhrase];
dz@1455
    77
    }
dz@1455
    78
dz@1455
    79
    NSString *latestPassphrase = @"theLatest";
dz@1458
    80
    [expectedPassphrases removeObjectAtIndex:0];
dz@1458
    81
    [expectedPassphrases addObject:latestPassphrase];
dz@1462
    82
    [self.cache addPassphrase:latestPassphrase];
dz@1455
    83
dz@1458
    84
    XCTAssertEqual(self.cache.passphrases.count, expectedPassphrases.count + 1);
dz@1458
    85
    NSMutableArray *expected = [NSMutableArray arrayWithArray:expectedPassphrases];
dz@1455
    86
    [expected insertObject:@"" atIndex:0];
dz@1455
    87
    XCTAssertEqualObjects(self.cache.passphrases, expected);
dz@1455
    88
}
dz@1455
    89
dz@1471
    90
- (void)testTimeout
dz@1471
    91
{
dz@1480
    92
    NSTimeInterval timeout = 0.2;
dz@1473
    93
    PEPPassphraseCache *ownCache = [[PEPPassphraseCache alloc]
dz@1474
    94
                                    initWithPassphraseTimeout:timeout
dz@1480
    95
                                    checkExpiryInterval:timeout/2];
dz@1471
    96
dz@1471
    97
    NSString *ownPassphrase = @"blah";
dz@1471
    98
    [ownCache addPassphrase:ownPassphrase];
dz@1471
    99
dz@1471
   100
    NSArray *expectedBefore = @[@"", ownPassphrase];
dz@1471
   101
    XCTAssertEqualObjects(ownCache.passphrases, expectedBefore);
dz@1471
   102
dz@1480
   103
    [NSThread sleepForTimeInterval:2*timeout];
dz@1471
   104
    XCTAssertEqualObjects(ownCache.passphrases, @[@""]);
dz@1471
   105
}
dz@1471
   106
dz@1449
   107
@end