FIRAppAttestKeyIDStorageTests.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2021 Google LLC
  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 "FBLPromise+Testing.h"
  18. #import "FirebaseAppCheck/Sources/AppAttestProvider/Storage/FIRAppAttestKeyIDStorage.h"
  19. #import "FirebaseAppCheck/Sources/Core/Errors/FIRAppCheckErrorUtil.h"
  20. @interface FIRAppAttestKeyIDStorageTests : XCTestCase
  21. @property(nonatomic) NSString *appName;
  22. @property(nonatomic) NSString *appID;
  23. @property(nonatomic) FIRAppAttestKeyIDStorage *storage;
  24. @end
  25. @implementation FIRAppAttestKeyIDStorageTests
  26. - (void)setUp {
  27. [super setUp];
  28. self.appName = @"FIRAppAttestKeyIDStorageTestsApp";
  29. self.appID = @"app_id";
  30. self.storage = [[FIRAppAttestKeyIDStorage alloc] initWithAppName:self.appName appID:self.appID];
  31. }
  32. - (void)tearDown {
  33. // Remove the app attest key ID from storage.
  34. [self.storage setAppAttestKeyID:nil];
  35. FBLWaitForPromisesWithTimeout(1.0);
  36. self.storage = nil;
  37. [super tearDown];
  38. }
  39. - (void)testInitWithApp {
  40. XCTAssertNotNil([[FIRAppAttestKeyIDStorage alloc] initWithAppName:self.appName appID:self.appID]);
  41. }
  42. - (void)testSetAndGetAppAttestKeyID {
  43. NSString *appAttestKeyID = @"app_attest_key_ID";
  44. FBLPromise *setPromise = [self.storage setAppAttestKeyID:appAttestKeyID];
  45. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  46. XCTAssertEqualObjects(setPromise.value, appAttestKeyID);
  47. XCTAssertNil(setPromise.error);
  48. __auto_type getPromise = [self.storage getAppAttestKeyID];
  49. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  50. XCTAssertEqualObjects(getPromise.value, appAttestKeyID);
  51. XCTAssertNil(getPromise.error);
  52. }
  53. - (void)testRemoveAppAttestKeyID {
  54. FBLPromise *setPromise = [self.storage setAppAttestKeyID:nil];
  55. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  56. XCTAssertEqualObjects(setPromise.value, nil);
  57. XCTAssertNil(setPromise.error);
  58. }
  59. - (void)testGetAppAttestKeyID_WhenAppAttestKeyIDNotFoundError {
  60. __auto_type getPromise = [self.storage getAppAttestKeyID];
  61. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  62. XCTAssertNotNil(getPromise.error);
  63. XCTAssertEqualObjects(getPromise.error, [FIRAppCheckErrorUtil appAttestKeyIDNotFound]);
  64. }
  65. - (void)testSetGetAppAttestKeyIDPerApp {
  66. // Assert storages for apps with the same name can independently set/get app attest key ID.
  67. [self assertIndependentSetGetForStoragesWithAppName1:self.appName
  68. appID1:@"app_id"
  69. appName2:self.appName
  70. appID2:@"app_id_2"];
  71. // Assert storages for apps with the same app ID can independently set/get app attest key ID.
  72. [self assertIndependentSetGetForStoragesWithAppName1:@"app_1"
  73. appID1:self.appID
  74. appName2:@"app_2"
  75. appID2:self.appID];
  76. // Assert storages for apps with different info can independently set/get app attest key ID.
  77. [self assertIndependentSetGetForStoragesWithAppName1:@"app_1"
  78. appID1:@"app_id_1"
  79. appName2:@"app_2"
  80. appID2:@"app_id_2"];
  81. }
  82. #pragma mark - Helpers
  83. - (void)assertIndependentSetGetForStoragesWithAppName1:(NSString *)appName1
  84. appID1:(NSString *)appID1
  85. appName2:(NSString *)appName2
  86. appID2:(NSString *)appID2 {
  87. // Create two storages.
  88. FIRAppAttestKeyIDStorage *storage1 = [[FIRAppAttestKeyIDStorage alloc] initWithAppName:appName1
  89. appID:appID1];
  90. FIRAppAttestKeyIDStorage *storage2 = [[FIRAppAttestKeyIDStorage alloc] initWithAppName:appName2
  91. appID:appID2];
  92. // 1. Independently set app attest key IDs for the two storages.
  93. NSString *appAttestKeyID1 = @"app_attest_key_ID1";
  94. FBLPromise *setPromise1 = [storage1 setAppAttestKeyID:appAttestKeyID1];
  95. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  96. XCTAssertEqualObjects(setPromise1.value, appAttestKeyID1);
  97. XCTAssertNil(setPromise1.error);
  98. NSString *appAttestKeyID2 = @"app_attest_key_ID2";
  99. __auto_type setPromise2 = [storage2 setAppAttestKeyID:appAttestKeyID2];
  100. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  101. XCTAssertEqualObjects(setPromise2.value, appAttestKeyID2);
  102. XCTAssertNil(setPromise2.error);
  103. // 2. Get app attest key IDs for the two storages.
  104. __auto_type getPromise1 = [storage1 getAppAttestKeyID];
  105. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  106. XCTAssertEqualObjects(getPromise1.value, appAttestKeyID1);
  107. XCTAssertNil(getPromise1.error);
  108. __auto_type getPromise2 = [storage2 getAppAttestKeyID];
  109. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  110. XCTAssertEqualObjects(getPromise2.value, appAttestKeyID2);
  111. XCTAssertNil(getPromise2.error);
  112. // 3. Assert that the app attest key IDs were set and retrieved independently of one another.
  113. XCTAssertNotEqualObjects(getPromise1.value, getPromise2.value);
  114. // Cleanup storages.
  115. [storage1 setAppAttestKeyID:nil];
  116. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  117. [storage2 setAppAttestKeyID:nil];
  118. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  119. }
  120. @end