FIRInstallationsIIDStoreTests.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. #import <FirebaseInstanceID/FirebaseInstanceID.h>
  18. #import "FBLPromise+Testing.h"
  19. #import "FirebaseInstallations/Source/Tests/Utils/FIRTestKeychain.h"
  20. #import "FirebaseInstallations/Source/Library/IIDMigration/FIRInstallationsIIDStore.h"
  21. @interface FIRInstanceID (Tests)
  22. + (FIRInstanceID *)instanceIDForTests;
  23. @end
  24. @interface FIRInstallationsIIDStoreTests : XCTestCase
  25. @property(nonatomic) FIRInstanceID *instanceID;
  26. @property(nonatomic) FIRInstallationsIIDStore *IIDStore;
  27. #if TARGET_OS_OSX
  28. @property(nonatomic) FIRTestKeychain *privateKeychain;
  29. #endif // TARGET_OSX
  30. @end
  31. @implementation FIRInstallationsIIDStoreTests
  32. - (void)setUp {
  33. self.instanceID = [FIRInstanceID instanceIDForTests];
  34. self.IIDStore = [[FIRInstallationsIIDStore alloc] init];
  35. #if TARGET_OS_OSX
  36. self.privateKeychain = [[FIRTestKeychain alloc] init];
  37. self.IIDStore.keychainRef = self.privateKeychain.testKeychainRef;
  38. #endif // TARGET_OSX
  39. }
  40. - (void)tearDown {
  41. self.instanceID = nil;
  42. #if TARGET_OS_OSX
  43. self.privateKeychain = nil;
  44. #endif // TARGET_OSX
  45. }
  46. // TODO: Configure the tests to run on macOS without requesting the keychain password.
  47. #if !TARGET_OS_OSX
  48. - (void)testExistingIIDSuccess {
  49. NSString *existingIID = [self readExistingIID];
  50. FBLPromise<NSString *> *IIDPromise = [self.IIDStore existingIID];
  51. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  52. XCTAssertNil(IIDPromise.error);
  53. XCTAssertEqualObjects(IIDPromise.value, existingIID);
  54. NSLog(@"Existing IID: %@", IIDPromise.value);
  55. }
  56. - (void)testDeleteExistingIID {
  57. // 1. Generate IID.
  58. NSString *existingIID1 = [self readExistingIID];
  59. // 2. Delete IID.
  60. FBLPromise<NSNull *> *deletePromise = [self.IIDStore deleteExistingIID];
  61. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  62. XCTAssertNil(deletePromise.error);
  63. XCTAssertTrue(deletePromise.isFulfilled);
  64. // 3. Check there is no IID.
  65. FBLPromise<NSString *> *IIDPromise = [self.IIDStore existingIID];
  66. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  67. XCTAssertNotNil(IIDPromise.error);
  68. XCTAssertTrue(IIDPromise.isRejected);
  69. // 4. Re-instantiate IID instance to reset its in-memory cache.
  70. self.instanceID = [FIRInstanceID instanceIDForTests];
  71. // 5. Generate a new IID and check it is different.
  72. NSString *existingIID2 = [self readExistingIID];
  73. XCTAssertNotEqualObjects(existingIID1, existingIID2);
  74. }
  75. #endif // !TARGET_OSX
  76. #pragma mark - Helpers
  77. - (NSString *)readExistingIID {
  78. __block NSString *existingIID;
  79. XCTestExpectation *IIDExpectation = [self expectationWithDescription:@"IIDExpectation"];
  80. [self.instanceID getIDWithHandler:^(NSString *_Nullable identity, NSError *_Nullable error) {
  81. XCTAssertNil(error);
  82. XCTAssertNotNil(identity);
  83. existingIID = identity;
  84. [IIDExpectation fulfill];
  85. }];
  86. [self waitForExpectations:@[ IIDExpectation ] timeout:20];
  87. return existingIID;
  88. }
  89. @end