FIRInstanceIDKeyPairTest.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 "Firebase/InstanceID/FIRInstanceIDKeyPair.h"
  18. #import "Firebase/InstanceID/FIRInstanceIDKeyPairStore.h"
  19. #import "Firebase/InstanceID/FIRInstanceIDKeyPairUtilities.h"
  20. #import "Firebase/InstanceID/FIRInstanceIDKeychain.h"
  21. static NSString *kKeyPairPrivateTag = @"iid-keypair-test-priv";
  22. static NSString *kKeyPairPublicTag = @"iid-keypair-test-public";
  23. @interface FIRInstanceIDKeyPairStore (ExposedForTest)
  24. + (void)deleteKeyPairWithPrivateTag:(NSString *)privateTag
  25. publicTag:(NSString *)publicTag
  26. handler:(void (^)(NSError *))handler;
  27. @end
  28. @interface FIRInstanceIDKeyPairTest : XCTestCase
  29. @end
  30. @implementation FIRInstanceIDKeyPairTest
  31. - (void)testInvalidKeychain {
  32. FIRInstanceIDKeyPair *keypair = [[FIRInstanceIDKeyPair alloc] initWithPrivateKey:nil
  33. publicKey:nil
  34. publicKeyData:nil
  35. privateKeyData:nil];
  36. XCTAssertNotNil(keypair);
  37. XCTAssertFalse([keypair isValid]);
  38. SecKeyRef publicKeyRef = [keypair publicKey];
  39. XCTAssertTrue(publicKeyRef == NULL);
  40. SecKeyRef privateKeyRef = [keypair privateKey];
  41. XCTAssertTrue(privateKeyRef == NULL);
  42. XCTAssertNil(keypair.publicKeyData);
  43. XCTAssertNil(keypair.privateKeyData);
  44. XCTAssertNil(FIRInstanceIDAppIdentity(keypair));
  45. }
  46. - (void)testValidKeychain {
  47. FIRInstanceIDKeyPair *keypair =
  48. [[FIRInstanceIDKeychain sharedInstance] generateKeyPairWithPrivateTag:kKeyPairPrivateTag
  49. publicTag:kKeyPairPublicTag];
  50. XCTAssertNotNil(keypair);
  51. XCTAssertTrue([keypair isValid]);
  52. SecKeyRef publicKeyRef = [keypair publicKey];
  53. XCTAssertFalse(publicKeyRef == NULL);
  54. SecKeyRef privateKeyRef = [keypair privateKey];
  55. XCTAssertFalse(privateKeyRef == NULL);
  56. XCTAssertNotNil(keypair.publicKeyData);
  57. XCTAssertNotNil(keypair.privateKeyData);
  58. XCTAssertNotNil(FIRInstanceIDAppIdentity(keypair));
  59. XCTestExpectation *keyPairDeleted = [self expectationWithDescription:@"keyPairDeleted"];
  60. [FIRInstanceIDKeyPairStore deleteKeyPairWithPrivateTag:kKeyPairPrivateTag
  61. publicTag:kKeyPairPublicTag
  62. handler:^(NSError *error) {
  63. [keyPairDeleted fulfill];
  64. }];
  65. [self waitForExpectations:@[ keyPairDeleted ] timeout:1.0];
  66. }
  67. @end