You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
4.1 KiB

  1. //
  2. // PEPPassphraseCacheTest.m
  3. // pEpObjCAdapterTests
  4. //
  5. // Created by Dirk Zimmermann on 25.06.20.
  6. // Copyright © 2020 pp. All rights reserved.
  7. //
  8. #import <XCTest/XCTest.h>
  9. #import "PEPPassphraseCache.h"
  10. #import "PEPPassphraseCacheInternal.h"
  11. @interface PEPPassphraseCacheTest : XCTestCase
  12. @property (nonatomic) PEPPassphraseCache *cache;
  13. @end
  14. @implementation PEPPassphraseCacheTest
  15. - (void)setUp
  16. {
  17. self.cache = [[PEPPassphraseCache alloc] init];
  18. }
  19. - (void)testContainsEmptyPassphrase
  20. {
  21. XCTAssertEqual(self.cache.passphrases.count, 0);
  22. XCTAssertEqualObjects(self.cache.passphrases, @[]);
  23. }
  24. - (void)testContainsSetPassphrase
  25. {
  26. NSString *passphrase = @"somepass";
  27. [self.cache addPassphrase:passphrase];
  28. XCTAssertEqual(self.cache.passphrases.count, 1);
  29. NSArray *expected = @[passphrase];
  30. XCTAssertEqualObjects(self.cache.passphrases, expected);
  31. }
  32. - (void)testContainsSetPassphrases
  33. {
  34. NSString *passphrase1 = @"somepass1";
  35. NSString *passphrase2 = @"somepass2";
  36. [self.cache addPassphrase:passphrase1];
  37. [self.cache addPassphrase:passphrase2];
  38. XCTAssertEqual(self.cache.passphrases.count, 2);
  39. NSArray *expected = @[passphrase2, passphrase1];
  40. XCTAssertEqualObjects(self.cache.passphrases, expected);
  41. }
  42. - (void)testTwentyPassphrases
  43. {
  44. NSMutableArray *passphrases = [NSMutableArray arrayWithCapacity:20];
  45. for (NSUInteger i = 1; i <= 20; ++i) {
  46. NSString *newPhrase = [NSString stringWithFormat:@"passphrase_%lu", (unsigned long) i];
  47. [passphrases addObject:newPhrase];
  48. [self.cache addPassphrase:newPhrase];
  49. }
  50. NSMutableArray *expected = [NSMutableArray arrayWithArray:[self reversedArray:passphrases]];
  51. XCTAssertEqual(self.cache.passphrases.count, expected.count);
  52. XCTAssertEqualObjects(self.cache.passphrases, expected);
  53. }
  54. - (void)testTwentyOnePassphrases
  55. {
  56. NSMutableArray *expectedPassphrases = [NSMutableArray arrayWithCapacity:20];
  57. for (NSUInteger i = 1; i <= 20; ++i) {
  58. NSString *newPhrase = [NSString stringWithFormat:@"passphrase_%lu", (unsigned long) i];
  59. [expectedPassphrases addObject:newPhrase];
  60. [self.cache addPassphrase:newPhrase];
  61. }
  62. NSString *latestPassphrase = @"theLatest";
  63. [self.cache addPassphrase:latestPassphrase];
  64. [expectedPassphrases addObject:latestPassphrase];
  65. // Last added passphrase is the newest, so it comes first.
  66. expectedPassphrases = [NSMutableArray arrayWithArray:[self reversedArray:expectedPassphrases]];
  67. // There are 21 passphrases, so the oldest (last) is removed.
  68. [expectedPassphrases removeLastObject];
  69. XCTAssertEqualObjects(self.cache.passphrases, expectedPassphrases);
  70. }
  71. - (void)testTimeout
  72. {
  73. NSTimeInterval timeout = 0.2;
  74. PEPPassphraseCache *ownCache = [[PEPPassphraseCache alloc]
  75. initWithPassphraseTimeout:timeout
  76. checkExpiryInterval:timeout/2];
  77. NSString *ownPassphrase = @"blah";
  78. [ownCache addPassphrase:ownPassphrase];
  79. NSArray *expectedBefore = @[ownPassphrase];
  80. XCTAssertEqualObjects(ownCache.passphrases, expectedBefore);
  81. [NSThread sleepForTimeInterval:2*timeout];
  82. XCTAssertEqualObjects(ownCache.passphrases, @[]);
  83. }
  84. - (void)testResetTimeout
  85. {
  86. NSString *passphrase1 = @"somepass1";
  87. NSString *passphrase2 = @"somepass2";
  88. NSString *passphrase3 = @"somepass3";
  89. [self.cache addPassphrase:passphrase1];
  90. [self.cache addPassphrase:passphrase2];
  91. [self.cache addPassphrase:passphrase3];
  92. [self.cache resetTimeoutForPassphrase:passphrase1];
  93. NSArray *expected1 = @[passphrase1, passphrase3, passphrase2];
  94. XCTAssertEqualObjects(self.cache.passphrases, expected1);
  95. [self.cache resetTimeoutForPassphrase:passphrase3];
  96. NSArray *expected2 = @[passphrase3, passphrase1, passphrase2];
  97. XCTAssertEqualObjects(self.cache.passphrases, expected2);
  98. }
  99. #pragma mark - Helpers
  100. - (NSArray *)reversedArray:(NSArray *)array
  101. {
  102. NSMutableArray *reversedArray = [NSMutableArray arrayWithCapacity:array.count];
  103. for (id obj in [array reverseObjectEnumerator]) {
  104. [reversedArray addObject:obj];
  105. }
  106. return [NSArray arrayWithArray:reversedArray];
  107. }
  108. @end