FIRTestKeychain.m 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 <TargetConditionals.h>
  17. #if TARGET_OS_OSX
  18. #import "FirebaseInstallations/Source/Tests/Utils/FIRTestKeychain.h"
  19. #import <XCTest/XCTest.h>
  20. @implementation FIRTestKeychain
  21. - (nullable instancetype)init {
  22. self = [super init];
  23. if (self) {
  24. SecKeychainRef privateKeychain;
  25. NSString *keychainPath =
  26. [NSTemporaryDirectory() stringByAppendingPathComponent:@"FIRTestKeychain"];
  27. if ([[NSFileManager defaultManager] fileExistsAtPath:keychainPath]) {
  28. NSError *error;
  29. if (![[NSFileManager defaultManager] removeItemAtPath:keychainPath error:&error]) {
  30. NSLog(@"Failed to delete existing test keychain: %@", error);
  31. return nil;
  32. }
  33. }
  34. #pragma clang diagnostic push
  35. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  36. OSStatus result = SecKeychainCreate([keychainPath cStringUsingEncoding:NSUTF8StringEncoding], 0,
  37. "1", false, nil, &privateKeychain);
  38. #pragma clang diagnostic pop
  39. if (result != errSecSuccess) {
  40. NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:result userInfo:nil];
  41. NSLog(@"SecKeychainCreate error: %@", error);
  42. return nil;
  43. }
  44. _testKeychainRef = privateKeychain;
  45. }
  46. return self;
  47. }
  48. - (void)dealloc {
  49. if (self.testKeychainRef) {
  50. #pragma clang diagnostic push
  51. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  52. OSStatus result = SecKeychainDelete(self.testKeychainRef);
  53. #pragma clang diagnostic pop
  54. if (result != errSecSuccess) {
  55. NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:result userInfo:nil];
  56. NSLog(@"SecKeychainCreate error: %@", error);
  57. }
  58. CFRelease(self.testKeychainRef);
  59. }
  60. }
  61. @end
  62. #endif // TARGET_OSX