FIRMessagingAPNSInfoTest.m 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "FirebaseMessaging/Sources/FIRMessagingConstants.h"
  18. #import "FirebaseMessaging/Sources/Token/FIRMessagingAPNSInfo.h"
  19. @interface FIRMessagingAPNSInfoTest : XCTestCase
  20. @end
  21. @implementation FIRMessagingAPNSInfoTest
  22. - (void)testAPNSInfoCreationWithValidDictionary {
  23. NSDictionary *validDictionary = @{
  24. kFIRMessagingTokenOptionsAPNSKey : [@"tokenData" dataUsingEncoding:NSUTF8StringEncoding],
  25. kFIRMessagingTokenOptionsAPNSIsSandboxKey : @(YES)
  26. };
  27. FIRMessagingAPNSInfo *info =
  28. [[FIRMessagingAPNSInfo alloc] initWithTokenOptionsDictionary:validDictionary];
  29. XCTAssertNotNil(info);
  30. XCTAssertEqualObjects(info.deviceToken, validDictionary[kFIRMessagingTokenOptionsAPNSKey]);
  31. XCTAssertEqual(info.sandbox,
  32. [validDictionary[kFIRMessagingTokenOptionsAPNSIsSandboxKey] boolValue]);
  33. }
  34. - (void)testAPNSInfoCreationWithInvalidDictionary {
  35. NSDictionary *validDictionary = @{};
  36. FIRMessagingAPNSInfo *info =
  37. [[FIRMessagingAPNSInfo alloc] initWithTokenOptionsDictionary:validDictionary];
  38. XCTAssertNil(info);
  39. }
  40. - (void)testAPNSInfoCreationWithInvalidTokenFormat {
  41. // Token data stored as NSString instead of NSData
  42. NSDictionary *badDictionary = @{
  43. kFIRMessagingTokenOptionsAPNSKey : @"tokenDataAsString",
  44. kFIRMessagingTokenOptionsAPNSIsSandboxKey : @(YES)
  45. };
  46. FIRMessagingAPNSInfo *info =
  47. [[FIRMessagingAPNSInfo alloc] initWithTokenOptionsDictionary:badDictionary];
  48. XCTAssertNil(info);
  49. }
  50. - (void)testAPNSInfoCreationWithInvalidSandboxFormat {
  51. // Sandbox key stored as NSString instead of NSNumber (bool)
  52. NSDictionary *validDictionary = @{
  53. kFIRMessagingTokenOptionsAPNSKey : [@"tokenData" dataUsingEncoding:NSUTF8StringEncoding],
  54. kFIRMessagingTokenOptionsAPNSIsSandboxKey : @"sandboxValueAsString"
  55. };
  56. FIRMessagingAPNSInfo *info =
  57. [[FIRMessagingAPNSInfo alloc] initWithTokenOptionsDictionary:validDictionary];
  58. XCTAssertNil(info);
  59. }
  60. // Test that archiving a FIRMessagingAPNSInfo object and restoring it from the archive
  61. // yields the same values for all the fields.
  62. - (void)testAPNSInfoEncodingAndDecoding {
  63. NSDictionary *validDictionary = @{
  64. kFIRMessagingTokenOptionsAPNSKey : [@"tokenData" dataUsingEncoding:NSUTF8StringEncoding],
  65. kFIRMessagingTokenOptionsAPNSIsSandboxKey : @"sandboxValueAsString"
  66. };
  67. FIRMessagingAPNSInfo *info =
  68. [[FIRMessagingAPNSInfo alloc] initWithTokenOptionsDictionary:validDictionary];
  69. #pragma clang diagnostic push
  70. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  71. NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:info];
  72. FIRMessagingAPNSInfo *restoredInfo = [NSKeyedUnarchiver unarchiveObjectWithData:archive];
  73. #pragma clang diagnostic pop
  74. XCTAssertEqualObjects(info.deviceToken, restoredInfo.deviceToken);
  75. XCTAssertEqual(info.sandbox, restoredInfo.sandbox);
  76. }
  77. @end