GULSecureCodingTests.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2019 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <XCTest/XCTest.h>
  15. #import "GoogleUtilities/Environment/Private/GULSecureCoding.h"
  16. @interface SecureCodingIncompatibleObject : NSObject <NSCoding>
  17. @end
  18. @implementation SecureCodingIncompatibleObject
  19. - (void)encodeWithCoder:(nonnull NSCoder *)coder {
  20. }
  21. - (nullable instancetype)initWithCoder:(nonnull NSCoder *)coder {
  22. return [self init];
  23. }
  24. @end
  25. @interface GULSecureCodingTests : XCTestCase
  26. @end
  27. @implementation GULSecureCodingTests
  28. - (void)testArchiveUnarchiveSingleClass {
  29. NSDictionary *objectToArchive = @{@"key1" : @"value1", @"key2" : @(2)};
  30. NSError *error;
  31. NSData *archiveData = [GULSecureCoding archivedDataWithRootObject:objectToArchive error:&error];
  32. XCTAssertNil(error);
  33. XCTAssertNotNil(archiveData);
  34. NSDictionary *unarchivedObject = [GULSecureCoding unarchivedObjectOfClass:[NSDictionary class]
  35. fromData:archiveData
  36. error:&error];
  37. XCTAssertNil(error);
  38. XCTAssert([objectToArchive isEqualToDictionary:unarchivedObject]);
  39. }
  40. - (void)testArchiveUnarchiveMultipleClasses {
  41. NSDictionary *objectToArchive = @{@"key1" : [NSDate date], @"key2" : @(2)};
  42. NSError *error;
  43. NSData *archiveData = [GULSecureCoding archivedDataWithRootObject:objectToArchive error:&error];
  44. XCTAssertNil(error);
  45. XCTAssertNotNil(archiveData);
  46. NSDictionary *unarchivedObject = [GULSecureCoding
  47. unarchivedObjectOfClasses:[NSSet setWithArray:@[ NSDictionary.class, NSDate.class ]]
  48. fromData:archiveData
  49. error:&error];
  50. XCTAssertNil(error);
  51. XCTAssert([objectToArchive isEqualToDictionary:unarchivedObject]);
  52. }
  53. - (void)testArchivingIncompatibleObjectError {
  54. SecureCodingIncompatibleObject *objectToArchive = [[SecureCodingIncompatibleObject alloc] init];
  55. NSError *error;
  56. NSData *archiveData = [GULSecureCoding archivedDataWithRootObject:objectToArchive error:&error];
  57. XCTAssertNotNil(error);
  58. XCTAssertNil(archiveData);
  59. }
  60. - (void)testUnarchivingClassMismatchError {
  61. NSDictionary *objectToArchive = @{@"key1" : @"value1", @"key2" : @(2)};
  62. NSError *error;
  63. NSData *archiveData = [GULSecureCoding archivedDataWithRootObject:objectToArchive error:&error];
  64. XCTAssertNil(error);
  65. XCTAssertNotNil(archiveData);
  66. NSArray *unarchivedObject = [GULSecureCoding unarchivedObjectOfClass:[NSArray class]
  67. fromData:archiveData
  68. error:&error];
  69. XCTAssertNotNil(error);
  70. XCTAssertNil(unarchivedObject);
  71. }
  72. - (void)testUnarchivingCorruptedDataError {
  73. NSData *corruptedData = [@"abc" dataUsingEncoding:NSUTF8StringEncoding];
  74. NSError *error;
  75. NSString *unarchivedObject = [GULSecureCoding unarchivedObjectOfClass:[NSString class]
  76. fromData:corruptedData
  77. error:&error];
  78. XCTAssertNotNil(error);
  79. XCTAssertNil(unarchivedObject);
  80. }
  81. - (void)testArchiveUnarchiveWithNULLError {
  82. SecureCodingIncompatibleObject *objectToArchive = [[SecureCodingIncompatibleObject alloc] init];
  83. NSData *archiveData = [GULSecureCoding archivedDataWithRootObject:objectToArchive error:NULL];
  84. XCTAssertNil(archiveData);
  85. NSData *corruptedData = [@"abc" dataUsingEncoding:NSUTF8StringEncoding];
  86. NSDictionary *unarchivedObject = [GULSecureCoding unarchivedObjectOfClass:[NSDictionary class]
  87. fromData:corruptedData
  88. error:NULL];
  89. XCTAssertNil(unarchivedObject);
  90. }
  91. @end