YYKeychainExample.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // YYKeychainExample.m
  3. // YYKitDemo
  4. //
  5. // Created by ibireme on 16/2/24.
  6. // Copyright 2016 ibireme. All rights reserved.
  7. //
  8. #import "YYKeychainExample.h"
  9. #import "YYKit.h"
  10. static NSString *const kServiceName = @"Facebook";
  11. static NSString *const kAccountName = @"ibireme";
  12. static NSString *const kPassword = @"123456";
  13. static NSString *const kLabel = @"Example";
  14. /*
  15. Some testcase copy from SSKeychain:
  16. https://github.com/soffes/sskeychain/blob/master/Tests/SSKeychainTests.m
  17. */
  18. @implementation YYKeychainExample
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. self.view.backgroundColor = [UIColor whiteColor];
  22. UILabel *label = [UILabel new];
  23. label.text = @"see YYKeychainExample.m";
  24. [label sizeToFit];
  25. label.centerX = self.view.width / 2;
  26. label.centerY = self.view.height / 2;
  27. [self.view addSubview:label];
  28. [self test];
  29. }
  30. - (void)test {
  31. [self testNewItem];
  32. [self testPasswordObject];
  33. [self testMissingInformation];
  34. [self testDeleteWithMissingInformation];
  35. [self testKeychain];
  36. }
  37. - (void)testNewItem {
  38. // New item
  39. YYKeychainItem *item = [[YYKeychainItem alloc] init];
  40. item.password = kPassword;
  41. item.service = kServiceName;
  42. item.account = kAccountName;
  43. item.label = kLabel;
  44. NSError *error = nil;
  45. NSAssert([YYKeychain insertItem:item error:&error], @"Unable to save item: %@", error);
  46. // Look up
  47. item = [[YYKeychainItem alloc] init];
  48. item.service = kServiceName;
  49. item.account = kAccountName;
  50. item.password = nil;
  51. NSAssert([YYKeychain selectOneItem:item error:&error], @"Unable to fetch keychain item: %@", error);
  52. NSAssert([[YYKeychain selectOneItem:item error:&error].password isEqualToString: kPassword], @"Passwords were not equal");
  53. // Search for all accounts
  54. item = [[YYKeychainItem alloc] init];
  55. NSArray *accounts = [YYKeychain selectItems:item error:&error];
  56. NSAssert(accounts, @"Unable to fetch accounts: %@", error);
  57. NSAssert([self _accounts:accounts containsAccountWithName:kAccountName], @"Matching account was not returned");
  58. // Check accounts for service
  59. item.service = kServiceName;
  60. accounts = [YYKeychain selectItems:item error:&error];
  61. NSAssert(accounts, @"Unable to fetch accounts: %@", error);
  62. NSAssert([self _accounts:accounts containsAccountWithName:kAccountName], @"Matching account was not returned");
  63. // Delete
  64. item = [[YYKeychainItem alloc] init];
  65. item.service = kServiceName;
  66. item.account = kAccountName;
  67. NSAssert([YYKeychain deleteItem:item error:&error], @"Unable to delete password: %@", error);
  68. }
  69. - (void)testPasswordObject {
  70. YYKeychainItem *item = [[YYKeychainItem alloc] init];
  71. item.service = kServiceName;
  72. item.account = kAccountName;
  73. NSDictionary *dictionary = @{@"number": @42, @"string": @"Hello World"};
  74. item.passwordObject = dictionary;
  75. __unused NSError *error = nil;
  76. NSAssert([YYKeychain insertItem:item error:&error], @"Unable to save item: %@", error);
  77. item = [[YYKeychainItem alloc] init];
  78. item.service = kServiceName;
  79. item.account = kAccountName;
  80. item.passwordObject = nil;
  81. NSAssert([YYKeychain selectOneItem:item error:&error], @"Unable to fetch keychain item: %@", error);
  82. NSAssert([((NSObject *)[YYKeychain selectOneItem:item error:&error].passwordObject) isEqual:dictionary], @"Passwords were not equal");
  83. }
  84. - (void)testMissingInformation {
  85. YYKeychainItem *item = [[YYKeychainItem alloc] init];
  86. item.service = kServiceName;
  87. item.account = kAccountName;
  88. __unused NSError *error = nil;
  89. NSAssert([YYKeychain insertItem:item error:&error] == NO, @"Function should return NO as not all needed information is provided: %@", error);
  90. item = [[YYKeychainItem alloc] init];
  91. item.password = kPassword;
  92. item.account = kAccountName;
  93. NSAssert([YYKeychain insertItem:item error:&error] == NO, @"Function should return NO as not all needed information is provided: %@", error);
  94. item = [[YYKeychainItem alloc] init];
  95. item.password = kPassword;
  96. item.service = kServiceName;
  97. NSAssert([YYKeychain insertItem:item error:&error] == NO, @"Function save should return NO if not all needed information is provided: %@", error);
  98. }
  99. - (void)testDeleteWithMissingInformation {
  100. YYKeychainItem *item = [[YYKeychainItem alloc] init];
  101. item.account = kAccountName;
  102. __unused NSError *error;
  103. NSAssert([YYKeychain deleteItem:item error:&error] == NO, @"Function deleteItem should return NO if not all needed information is provided: %@", error);
  104. item = [[YYKeychainItem alloc] init];
  105. item.service = kServiceName;
  106. NSAssert([YYKeychain deleteItem:item error:&error] == NO, @"Function deleteItem should return NO if not all needed information is provided: %@", error);
  107. // check if fetch handels missing information correctly
  108. item = [[YYKeychainItem alloc] init];
  109. item.account = kAccountName;
  110. NSAssert([YYKeychain selectOneItem:item error:&error] == nil, @"Function fetch should return NO if not all needed information is provided: %@", error);
  111. item = [[YYKeychainItem alloc] init];
  112. item.service = kServiceName;
  113. NSAssert([YYKeychain selectOneItem:item error:&error] == nil, @"Function fetch should return NO if not all needed information is provided: %@", error);
  114. item = [[YYKeychainItem alloc] init];
  115. item.service = kServiceName;
  116. NSAssert([YYKeychain selectOneItem:item error:NULL] == nil, @"Function fetch should return NO if not all needed information is provided and error is NULL");
  117. }
  118. - (void)testSynchronizable {
  119. YYKeychainItem *item = [[YYKeychainItem alloc] init];
  120. item.service = kServiceName;
  121. item.account = kAccountName;
  122. item.password = kPassword;
  123. item.synchronizable = YYKeychainQuerySynchronizationModeYes;
  124. __unused NSError *error;
  125. NSAssert([YYKeychain insertItem:item error:&error], @"Unable to save item: %@", error);
  126. item = [[YYKeychainItem alloc] init];
  127. item.service = kServiceName;
  128. item.account = kAccountName;
  129. item.password = nil;
  130. item.synchronizable = YYKeychainQuerySynchronizationModeNo;
  131. NSAssert([YYKeychain selectOneItem:item error:&error] == nil, @"Fetch should fail when trying to fetch an unsynced password that was saved as synced: %@", error);
  132. NSAssert([YYKeychain selectOneItem:item error:NULL] == nil, @"Fetch should fail when trying to fetch an unsynced password that was saved as synced. error == NULL");
  133. NSAssert([item.password isEqualToString:kPassword] == NO, @"Passwords should not be equal when trying to fetch an unsynced password that was saved as synced.");
  134. item = [[YYKeychainItem alloc] init];
  135. item.service = kServiceName;
  136. item.account = kAccountName;
  137. item.password = nil;
  138. item.synchronizable = YYKeychainQuerySynchronizationModeAny;
  139. NSAssert([YYKeychain selectOneItem:item error:&error], @"Unable to fetch keychain item: %@", error);
  140. NSAssert([[YYKeychain selectOneItem:item error:&error].password isEqualToString:kPassword], @"Passwords were not equal");
  141. [YYKeychain deleteItem:item error:NULL];
  142. }
  143. - (void)testKeychain {
  144. __unused NSError *error = nil;
  145. // create a new keychain item
  146. NSAssert([YYKeychain setPassword:kPassword forService:kServiceName account:kAccountName error:&error], @"Unable to save item: %@", error);
  147. [[YYKeychain getPasswordForService:kServiceName account:kAccountName error:NULL] isEqualToString:kPassword];
  148. // check password
  149. NSAssert([[YYKeychain getPasswordForService:kServiceName account:kAccountName error:NULL] isEqualToString:kPassword], @"Passwords were not equal");
  150. // delete password
  151. NSAssert([YYKeychain deletePasswordForService:kServiceName account:kAccountName error:&error], @"Unable to delete password: %@", error);
  152. }
  153. - (BOOL)_accounts:(NSArray *)items containsAccountWithName:(NSString *)name {
  154. for (YYKeychainItem *item in items) {
  155. if ([item.account isEqualToString:name]) {
  156. return YES;
  157. }
  158. }
  159. return NO;
  160. }
  161. @end