Skip to content

Commit 6d0f776

Browse files
committed
Add a FLEXHeapEnumerator test
Ensure we can spoof an object that is found by FLEXHeapEnumerator and also later prove it isn't a real object
1 parent 6c83ddc commit 6d0f776

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

FLEXTests/FLEXTests.m

+27
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#import <XCTest/XCTest.h>
1010
#import <objc/runtime.h>
1111
#import "NSObject+FLEX_Reflection.h"
12+
#import "FLEXObjcInternal.h"
13+
#import "FLEXHeapEnumerator.h"
14+
#import "FLEXObjectRef.h"
1215
#import "NSArray+FLEX.h"
1316
#import "FLEXPropertyAttributes.h"
1417
#import "FLEXProperty.h"
@@ -25,6 +28,12 @@ @interface Subclass : NSObject {
2528
@end
2629
@implementation Subclass @end
2730

31+
@interface NeverCreated : Subclass {
32+
NSInteger a, b, c;
33+
}
34+
@end
35+
@implementation NeverCreated @end
36+
2837
@interface FLEXTests : XCTestCase
2938
@property (nonatomic, setter=setMyFoo:) id foo;
3039
@end
@@ -140,4 +149,22 @@ - (void)testSafeGetClassName {
140149
XCTAssertEqualObjects(@"FLEXNewRootClass", className);
141150
}
142151

152+
- (void)testInvalidObjectFinding {
153+
// Create something that looks like an objc object
154+
uintptr_t *pointer = (uintptr_t *)calloc(1, sizeof(uintptr_t));
155+
object_setClass((__bridge id)(void *)pointer, [NeverCreated class]);
156+
157+
// Find that one object and assert that it is the object we just created
158+
NSArray<FLEXObjectRef *> *refs = [FLEXHeapEnumerator
159+
instancesOfClassWithName:@"NeverCreated" retained:NO
160+
];
161+
XCTAssertEqual(refs.count, 1);
162+
XCTAssertEqual((__bridge void *)refs.firstObject.object, pointer);
163+
164+
// Find that the object isn't really an object
165+
XCTAssertFalse(FLEXPointerIsValidObjcObject(pointer));
166+
167+
free(pointer);
168+
}
169+
143170
@end

0 commit comments

Comments
 (0)