GULSecureCodingTests.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/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)testArchiveUnarchive {
  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)testArchivingIncompatibleObjectError {
  41. SecureCodingIncompatibleObject *objectToArchive = [[SecureCodingIncompatibleObject alloc] init];
  42. NSError *error;
  43. NSData *archiveData = [GULSecureCoding archivedDataWithRootObject:objectToArchive error:&error];
  44. XCTAssertNotNil(error);
  45. XCTAssertNil(archiveData);
  46. }
  47. - (void)testUnarchivingClassMismatchError {
  48. NSDictionary *objectToArchive = @{@"key1" : @"value1", @"key2" : @(2)};
  49. NSError *error;
  50. NSData *archiveData = [GULSecureCoding archivedDataWithRootObject:objectToArchive error:&error];
  51. XCTAssertNil(error);
  52. XCTAssertNotNil(archiveData);
  53. NSArray *unarchivedObject = [GULSecureCoding unarchivedObjectOfClass:[NSArray class]
  54. fromData:archiveData
  55. error:&error];
  56. XCTAssertNotNil(error);
  57. XCTAssertNil(unarchivedObject);
  58. }
  59. - (void)testUnarchivingCorruptedDataError {
  60. NSData *corruptedData = [@"abc" dataUsingEncoding:NSUTF8StringEncoding];
  61. NSError *error;
  62. NSString *unarchivedObject = [GULSecureCoding unarchivedObjectOfClass:[NSString class]
  63. fromData:corruptedData
  64. error:&error];
  65. XCTAssertNotNil(error);
  66. XCTAssertNil(unarchivedObject);
  67. }
  68. - (void)testArchiveUnarchiveWithNULLError {
  69. SecureCodingIncompatibleObject *objectToArchive = [[SecureCodingIncompatibleObject alloc] init];
  70. NSData *archiveData = [GULSecureCoding archivedDataWithRootObject:objectToArchive error:NULL];
  71. XCTAssertNil(archiveData);
  72. NSData *corruptedData = [@"abc" dataUsingEncoding:NSUTF8StringEncoding];
  73. NSDictionary *unarchivedObject = [GULSecureCoding unarchivedObjectOfClass:[NSDictionary class]
  74. fromData:corruptedData
  75. error:NULL];
  76. XCTAssertNil(unarchivedObject);
  77. }
  78. @end