FIRMessagingBackupExcludedPlistTest.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <OCMock/OCMock.h>
  18. #import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
  19. #import "FirebaseMessaging/Sources/Token/FIRMessagingAuthKeychain.h"
  20. #import "FirebaseMessaging/Sources/Token/FIRMessagingBackupExcludedPlist.h"
  21. #import "FirebaseMessaging/Sources/Token/FIRMessagingCheckinStore.h"
  22. static NSString *const kSubDirectoryName = @"FirebaseInstanceIDBackupPlistTest";
  23. static NSString *const kTestPlistFileName = @"com.google.test.IIDBackupExcludedPlist";
  24. @interface FIRMessaging (ExposedForTest)
  25. + (BOOL)createSubDirectory:(NSString *)subDirectoryName;
  26. @end
  27. @interface FIRMessagingBackupExcludedPlist ()
  28. - (BOOL)moveToApplicationSupportSubDirectory:(NSString *)subDirectoryName;
  29. @end
  30. @interface FIRMessagingBackupExcludedPlistTest : XCTestCase
  31. @property(nonatomic, readwrite, strong) FIRMessagingBackupExcludedPlist *plist;
  32. @end
  33. @implementation FIRMessagingBackupExcludedPlistTest
  34. - (void)setUp {
  35. [super setUp];
  36. [FIRMessaging createSubDirectory:kSubDirectoryName];
  37. self.plist = [[FIRMessagingBackupExcludedPlist alloc] initWithFileName:kTestPlistFileName
  38. subDirectory:kSubDirectoryName];
  39. }
  40. - (void)tearDown {
  41. [self.plist deleteFile:nil];
  42. [super tearDown];
  43. }
  44. - (void)testWriteToPlistInDocumentsFolder {
  45. XCTAssertNil([self.plist contentAsDictionary]);
  46. NSDictionary *plistContents = @{@"hello" : @"world", @"id" : @123};
  47. [self.plist writeDictionary:plistContents error:nil];
  48. XCTAssertEqualObjects(plistContents, [self.plist contentAsDictionary]);
  49. }
  50. - (void)testDeleteFileInDocumentsFolder {
  51. NSDictionary *plistContents = @{@"hello" : @"world", @"id" : @123};
  52. [self.plist writeDictionary:plistContents error:nil];
  53. XCTAssertEqualObjects(plistContents, [self.plist contentAsDictionary]);
  54. // Delete file
  55. XCTAssertTrue([self.plist doesFileExist]);
  56. XCTAssertTrue([self.plist deleteFile:nil]);
  57. XCTAssertFalse([self.plist doesFileExist]);
  58. }
  59. - (void)testWriteToPlistInApplicationSupportFolder {
  60. XCTAssertNil([self.plist contentAsDictionary]);
  61. NSDictionary *plistContents = @{@"hello" : @"world", @"id" : @123};
  62. [self.plist writeDictionary:plistContents error:nil];
  63. XCTAssertTrue([self.plist doesFileExist]);
  64. XCTAssertEqualObjects(plistContents, [self.plist contentAsDictionary]);
  65. XCTAssertTrue([self doesPlistFileExist]);
  66. }
  67. #pragma mark - Private Helpers
  68. - (BOOL)doesPlistFileExist {
  69. #if TARGET_OS_TV
  70. NSArray *directoryPaths =
  71. NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  72. #else
  73. NSArray *directoryPaths =
  74. NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  75. #endif
  76. NSString *dirPath = directoryPaths.lastObject;
  77. NSArray *components =
  78. @[ dirPath, kSubDirectoryName, [NSString stringWithFormat:@"%@.plist", kTestPlistFileName] ];
  79. NSString *plistPath = [NSString pathWithComponents:components];
  80. return [[NSFileManager defaultManager] fileExistsAtPath:plistPath];
  81. }
  82. @end