Skip to content

Commit

Permalink
Merge pull request #716 from bamx23/secure-coding
Browse files Browse the repository at this point in the history
Fix supportsSecureCoding for intercepted classes
  • Loading branch information
ecaselles authored Jan 31, 2020
2 parents c5e5c2b + 75cc4e6 commit ff3f34c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Classes/Stubbing/KWIntercept.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
void KWInterceptedDealloc(id anObject, SEL aSelector);
Class KWInterceptedClass(id anObject, SEL aSelector);
Class KWInterceptedSuperclass(id anObject, SEL aSelector);
BOOL KWInterceptedSupportsSecureCodingTrue(id anObject, SEL aSelector);
BOOL KWInterceptedSupportsSecureCodingFalse(id anObject, SEL aSelector);

#pragma mark - Getting Forwarding Implementations

Expand Down Expand Up @@ -120,6 +122,18 @@ Class KWInterceptClassForCanonicalClass(Class canonicalClass) {
Class interceptMetaClass = object_getClass(interceptClass);
class_addMethod(interceptMetaClass, @selector(forwardInvocation:), (IMP)KWInterceptedForwardInvocation, "v@:@");

SEL supportsSecureCodingSelector = @selector(supportsSecureCoding);
Method supportsSecureCodingMethod = class_getClassMethod(canonicalClass, supportsSecureCodingSelector);
if (supportsSecureCodingMethod != NULL) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
BOOL support = (BOOL)[(id)canonicalClass performSelector:supportsSecureCodingSelector];
#pragma clang diagnostic pop
IMP supportsSecureCodingIMP = support ? (IMP)KWInterceptedSupportsSecureCodingTrue
: (IMP)KWInterceptedSupportsSecureCodingFalse;
class_addMethod(interceptMetaClass, supportsSecureCodingSelector, supportsSecureCodingIMP, "B@:");
}

return interceptClass;
}

Expand Down Expand Up @@ -223,6 +237,14 @@ Class KWInterceptedSuperclass(id anObject, SEL aSelector) {
return originalSuperclass;
}

BOOL KWInterceptedSupportsSecureCodingTrue(id anObject, SEL aSelector) {
return YES;
}

BOOL KWInterceptedSupportsSecureCodingFalse(id anObject, SEL aSelector) {
return NO;
}

#pragma mark - Managing Objects Stubs

void KWAssociateObjectStub(id anObject, KWStub *aStub, BOOL overrideExisting) {
Expand Down
9 changes: 9 additions & 0 deletions Tests/KWRealObjectStubTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ - (void)testItShouldStubWithBlock {
XCTAssertEqual([cruiser classification], @"Enterprise", @"expected method to be stubbed with block");
}

- (void)testStubSecureCodingOfDateClass {
NSDate *date = [NSDate date];
[NSDate stub:@selector(date) andReturn:date];
if (@available(macOS 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *)) {
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:date requiringSecureCoding:YES error:NULL];
XCTAssertNotNil(data, @"expected stubbed class to be able to use secure coding");
}
}

@end

#endif // #if KW_TESTS_ENABLED

0 comments on commit ff3f34c

Please sign in to comment.