Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
junzhan.yzw committed Oct 22, 2015
1 parent 598f843 commit 00a0dcd
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 39 deletions.
4 changes: 3 additions & 1 deletion lib/stdlib/structs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ if wax.isArm64 == true then
wax.struct.create("UIEdgeInsets", "dddd", "top", "left", "bottom", "right")
wax.struct.create("CGRect", "dddd", "x", "y", "width", "height")

wax.struct.create("NSRange", "LL", "location", "length")
wax.struct.create("NSRange", "QQ", "location", "length")
wax.struct.create("_NSRange", "QQ", "location", "length")--typedef _NSRange to NSRange

wax.struct.create("CLLocationCoordinate2D", "dd", "latitude", "longitude")
wax.struct.create("MKCoordinateSpan", "dd", "latitudeDelta", "longitudeDelta")
Expand All @@ -18,6 +19,7 @@ else
wax.struct.create("CGRect", "ffff", "x", "y", "width", "height")

wax.struct.create("NSRange", "II", "location", "length")
wax.struct.create("_NSRange", "II", "location", "length")--typedef _NSRange to NSRange

wax.struct.create("CLLocationCoordinate2D", "dd", "latitude", "longitude")
wax.struct.create("MKCoordinateSpan", "dd", "latitudeDelta", "longitudeDelta")
Expand Down
2 changes: 1 addition & 1 deletion lib/wax_gc.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#import "wax_instance.h"
#import "wax_helpers.h"

static NSInteger WAX_GC_TIMEOUT = 1;//default 5 seconds
static NSInteger WAX_GC_TIMEOUT = 1;//default 1 seconds

@implementation wax_gc

Expand Down
32 changes: 18 additions & 14 deletions lib/wax_instance.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

static BOOL overrideMethod(lua_State *L, wax_instance_userdata *instanceUserdata);
static int pcallUserdata(lua_State *L, id self, SEL selector, va_list args);
static BOOL overrideMethodByInvocation(id klass, SEL selector, char *returnType);
static BOOL overrideMethodByInvocation(id klass, SEL selector, char *typeDescription, char *returnType);
static BOOL addMethodByInvocation(id klass, SEL selector, char * typeDescription) ;


Expand Down Expand Up @@ -617,8 +617,10 @@ static int pcallUserdataARM64Invocation(lua_State *L, id self, SEL selector, NSI
for (NSUInteger i = 2; i < [signature numberOfArguments]; i++) { // start at 2 because to skip the automatic self and _cmd arugments
const char *type = [signature getArgumentTypeAtIndex:i];
const char *typeDescription = wax_removeProtocolEncodings(type);

int size = wax_sizeOfTypeDescription(typeDescription);
// int size = wax_sizeOfTypeDescription(typeDescription);//should use NSGetSizeAndAlignment ?
NSUInteger size = 0;
NSGetSizeAndAlignment(type, &size, NULL);

void *buffer = malloc(size);
[anInvocation getArgument:buffer atIndex:i];

Expand Down Expand Up @@ -711,7 +713,7 @@ static BOOL overrideMethod(lua_State *L, wax_instance_userdata *instanceUserdata
Method method = class_getInstanceMethod(klass, selector);

if (method) { // Is method defined in the superclass?
typeDescription = (char *)method_getTypeEncoding(method);
typeDescription = (char *)method_getTypeEncoding(method);
returnType = method_copyReturnType(method);
}
else { // Is this method implementing a protocol?
Expand Down Expand Up @@ -753,7 +755,7 @@ static BOOL overrideMethod(lua_State *L, wax_instance_userdata *instanceUserdata
luaL_error(L, "Trying to override method '%s' on an instance. You can only override classes", methodName);
}

success = overrideMethodByInvocation(klass, selector, returnType);
success = overrideMethodByInvocation(klass, selector, typeDescription,returnType);
}
else {
SEL possibleSelectors[2];
Expand All @@ -779,7 +781,7 @@ static BOOL overrideMethod(lua_State *L, wax_instance_userdata *instanceUserdata
typeDescription = (char *)method_getTypeEncoding(method);
returnType = method_copyReturnType(method);

success = overrideMethodByInvocation(metaclass, selector, returnType);
success = overrideMethodByInvocation(metaclass, selector, typeDescription, returnType);
if(returnType) free(returnType);
} else {//no method, then add it both

Expand Down Expand Up @@ -868,16 +870,18 @@ static void hookForwardInvocation(id self, SEL sel, NSInvocation *anInvocation){
};
}

static BOOL overrideMethodByInvocation(id klass, SEL selector, char *returnType) {
static BOOL overrideMethodByInvocation(id klass, SEL selector, char *typeDescription, char *returnType) {
IMP forwardImp = _objc_msgForward;
#if !defined(__arm64__)
if(strlen(returnType) > 0 && returnType[0] == '{'){//return struct
#if WAX_IS_ARM_64 == 1
replaceMethodAndGenerateORIG(klass, selector, (IMP)_objc_msgForward);//trigger forwardInvocation
#else
replaceMethodAndGenerateORIG(klass, selector, (IMP)_objc_msgForward_stret);//trigger forwardInvocation
#endif
}else{
replaceMethodAndGenerateORIG(klass, selector, _objc_msgForward);//trigger forwardInvocation
NSMethodSignature *methodSignature = [NSMethodSignature signatureWithObjCTypes:typeDescription];
if ([methodSignature.debugDescription rangeOfString:@"is special struct return? YES"].location != NSNotFound) {
forwardImp = (IMP)_objc_msgForward_stret;
}
}
#endif

replaceMethodAndGenerateORIG(klass, selector, forwardImp);//trigger forwardInvocation

if(!isMethodReplacedByInvocation(klass, @selector(forwardInvocation:))){//just replace once

Expand Down
6 changes: 3 additions & 3 deletions lib/wax_stdlib.h

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion tools/AutoTest/AutoTest/AutoTest-Prefix.pch
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,11 @@

#define TEST_VALUE_FLOAT 123.456f
#define TEST_VALUE_CGFLOAT (CGFloat)1234.5678
#define TEST_VALUE_DOUBLE (double)12345.6789
#define TEST_VALUE_DOUBLE (double)12345.6789
#define TEST_VALUE_STRING @"abcdefg"


#define TEST_VALUE_CGRECT CGRectMake(100, 200, 300, 400)
#define TEST_VALUE_CGPOINT CGPointMake(100, 200)
#define TEST_VALUE_CGSIZE CGSizeMake(100, 200)
#define TEST_VALUE_NSRANGE NSMakeRange(100, 200)
155 changes: 152 additions & 3 deletions tools/AutoTest/AutoTest/AutoTestClass/AutoTestHookMethod.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// AutoTest64MethodHookVC.m
//
// TBHotpatchSDKTest
//
// Created by junzhan on 15-1-4.
// Copyright (c) 2015年 junzhan. All rights reserved.
Expand All @@ -18,9 +18,30 @@ - (void)autoTestStart{
[self testHookMethod];
[self testAddMethod];
[self testMutiThreadSafe];
//
[[self class] testHookClassMethod];
[self testUnderlinePrefixMethod];
[self testStructMethod];
// [self testTimeConsuming];
}

#pragma mark hook

- (void)testTimeConsuming{
int n = 1000;
NSTimeInterval start = CFAbsoluteTimeGetCurrent();

NSLog(@"F:%s L:%d", __PRETTY_FUNCTION__, __LINE__);

for(int i = 0; i < n; ++i){
[self testHookMethod];
}
NSTimeInterval end = CFAbsoluteTimeGetCurrent();
NSLog(@"F:%s L:%d", __PRETTY_FUNCTION__, __LINE__);
NSLog(@"start=%f, end=%f", start, end);
NSLog(@"total cost=%.0f\n\n", (end-start)*1000.0);
}

- (void)testHookMethod{
[self testHookReturnVoid];
NSAssert([self testHookReturnId] == self, @"FUN:%s LINE:%d", __PRETTY_FUNCTION__, __LINE__);
Expand Down Expand Up @@ -121,7 +142,7 @@ - (void)testAddMethod{
}

//- (void)testAddMethodWithVoid{
//
//
//}
//
//- (id)testAddMethodWithaId:(NSString *)aId{
Expand All @@ -140,7 +161,7 @@ - (void)testMutiThreadSafe{
NSString *name = [NSString stringWithFormat:@"oc.queue.%d", i%10];
dispatch_queue_t queue = dispatch_queue_create([name UTF8String], DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:rand()%10/1000.0];
[NSThread sleepForTimeInterval:rand()%100/1000.0];
if(i&1){
[self testHookMethod];
}else{
Expand All @@ -151,4 +172,132 @@ - (void)testMutiThreadSafe{
}
});
}

#pragma mark test hook Class method

+ (void)testHookClassMethod{
[self testHookClassaId:self];
NSAssert([self testHookClassReturnIdWithaId:self] == self, @"FUN:%s LINE:%d", __PRETTY_FUNCTION__, __LINE__);
}

+ (void)testHookClassaId:(id)aId{
NSLog(@"OC TEST SUCCESS: %s", __PRETTY_FUNCTION__);
NSLog(@"aId=%@", aId);
}

+ (id)testHookClassReturnIdWithaId:(id)aId{
NSLog(@"OC TEST SUCCESS: %s", __PRETTY_FUNCTION__);
NSLog(@"aId=%@", aId);
return aId;
}

#pragma mark test underline prefix method

- (void)testUnderlinePrefixMethod{
[self _prefixMethod:@"abc"];
[self _prefixMethodA:@"abc" B:@"efg"];
[self _];
[self _prefixA:@"abc" _b:@"efg"];
[self __aa_bb_:@"abc" _cc__dd_:@"efg" ___ee___f___:@"hij"];
[[self class] ___aa_bb_:@"abc" _cc__dd_:@"efg" ___ee___f___:@"hij"];
}

- (id)_{
NSLog(@"F:%s L:%d", __PRETTY_FUNCTION__, __LINE__);
return self;
}

- (void)_prefixMethod:(NSString *)str{
NSAssert([str isEqualToString:TEST_VALUE_STRING], @"FUN:%s LINE:%d", __PRETTY_FUNCTION__, __LINE__);
NSLog(@"F:%s L:%d", __PRETTY_FUNCTION__, __LINE__);
}

- (id)_prefixMethodA:(NSString *)str B:(NSString *)b{
NSAssert([str isEqualToString:TEST_VALUE_STRING], @"FUN:%s LINE:%d", __PRETTY_FUNCTION__, __LINE__);
NSAssert([b isEqualToString:TEST_VALUE_STRING], @"FUN:%s LINE:%d", __PRETTY_FUNCTION__, __LINE__);
NSLog(@"F:%s L:%d", __PRETTY_FUNCTION__, __LINE__);
return str;
}

- (void)_prefixA:(NSString *)a _b:(NSString *)b{
NSAssert([a isEqualToString:TEST_VALUE_STRING], @"FUN:%s LINE:%d", __PRETTY_FUNCTION__, __LINE__);
NSAssert([b isEqualToString:TEST_VALUE_STRING], @"FUN:%s LINE:%d", __PRETTY_FUNCTION__, __LINE__);
NSLog(@"F:%s L:%d", __PRETTY_FUNCTION__, __LINE__);
}

- (id)__aa_bb_:(NSString *)v1 _cc__dd_ :(NSString *)v2 ___ee___f___:(NSString *)v3{
NSAssert([v1 isEqualToString:TEST_VALUE_STRING], @"FUN:%s LINE:%d", __PRETTY_FUNCTION__, __LINE__);
NSAssert([v2 isEqualToString:TEST_VALUE_STRING], @"FUN:%s LINE:%d", __PRETTY_FUNCTION__, __LINE__);
NSAssert([v3 isEqualToString:TEST_VALUE_STRING], @"FUN:%s LINE:%d", __PRETTY_FUNCTION__, __LINE__);
NSLog(@"F:%s L:%d", __PRETTY_FUNCTION__, __LINE__);
return v1;
}

+ (id)___aa_bb_:(NSString *)v1 _cc__dd_:(NSString *)v2 ___ee___f___:(NSString *)v3{
NSAssert([v1 isEqualToString:TEST_VALUE_STRING], @"FUN:%s LINE:%d", __PRETTY_FUNCTION__, __LINE__);
NSAssert([v2 isEqualToString:TEST_VALUE_STRING], @"FUN:%s LINE:%d", __PRETTY_FUNCTION__, __LINE__);
NSAssert([v3 isEqualToString:TEST_VALUE_STRING], @"FUN:%s LINE:%d", __PRETTY_FUNCTION__, __LINE__);
NSLog(@"F:%s L:%d", __PRETTY_FUNCTION__, __LINE__);
return v1;
}

#pragma mark test hook struct method

- (void)testStructMethod{
id x = [self initWithFrame:TEST_VALUE_CGRECT];
CGRect rect = [self testReturnCGRectWithCGRect:TEST_VALUE_CGRECT];

NSAssert(CGRectEqualToRect(rect, CGRectMake(TEST_VALUE_CGRECT.origin.x+10*2, TEST_VALUE_CGRECT.origin.y+20*2, TEST_VALUE_CGRECT.size.width+30*2, TEST_VALUE_CGRECT.size.height+40*2)), @"testReturnCGRectWithCGRect fail");

CGRect rect2 = [self testReturnCGRectWithaId:self aCGRect:TEST_VALUE_CGRECT];
NSAssert(CGRectEqualToRect(rect2, CGRectMake(TEST_VALUE_CGRECT.origin.x+10*2, TEST_VALUE_CGRECT.origin.y+20*2, TEST_VALUE_CGRECT.size.width+30*2, TEST_VALUE_CGRECT.size.height+40*2)) , @"testReturnCGRectWithaId fail");

CGPoint point = [self testReturnCGPointWithCGPoint:TEST_VALUE_CGPOINT];
NSAssert(CGPointEqualToPoint(point, CGPointMake(TEST_VALUE_CGPOINT.x+10*2, TEST_VALUE_CGPOINT.y+20*2)), @"testReturnCGPointWithCGPoint fail");
CGFloat aCGFloat = [[self class] testReturnCGFloatWithaPoint:TEST_VALUE_CGPOINT bPoint:TEST_VALUE_CGPOINT];
NSAssert([AutoTestUtil isDoubleEqual:aCGFloat aDouble:(TEST_VALUE_CGPOINT.x+10+TEST_VALUE_CGPOINT.y+20)*2], @"testReturnCGFloatWithaPoint fail");

// CGSize size = [self testReturnCGSizeWithCGSize:TEST_VALUE_CGSize];
// NSAssert(CGSizeEqualToSize(size, CGSizeMake(TEST_VALUE_CGSize.width+10*2, TEST_VALUE_CGSize.height+20*2)), @"testReturnCGSizeWithCGSize fail");

NSRange range = [self testReturnNSRangeWithNSRange:TEST_VALUE_NSRANGE];
NSAssert(range.location == TEST_VALUE_NSRANGE.location+10*2 && range.length == TEST_VALUE_NSRANGE.length+20*2, @"testReturnNSRangeWithNSRange fail");
range = [self testReturnNSRangeWithaId:self aNSRange:TEST_VALUE_NSRANGE];
NSAssert(range.location == TEST_VALUE_NSRANGE.location+10*2 && range.length == TEST_VALUE_NSRANGE.length+20*2, @"testReturnNSRangeWithaId fail");
}


- (id)initWithFrame:(CGRect)aCGRect{
NSLog(@"aCGRect=%@", NSStringFromCGRect(aCGRect));
self = [super init];
return self;
}

- (CGRect)testReturnCGRectWithCGRect:(CGRect)aCGRect{
return CGRectMake(aCGRect.origin.x+10, aCGRect.origin.y+20, aCGRect.size.width+30, aCGRect.size.height+40);
}

- (CGRect)testReturnCGRectWithaId:(id)aId aCGRect:(CGRect)aCGRect{
return CGRectMake(aCGRect.origin.x+10, aCGRect.origin.y+20, aCGRect.size.width+30, aCGRect.size.height+40);
}

- (CGSize)testReturnCGSizeWithCGSize:(CGSize)aCGSize{
return CGSizeMake(aCGSize.width+10, aCGSize.height+20);
}

- (CGPoint)testReturnCGPointWithCGPoint:(CGPoint)aCGPoint{
return CGPointMake(aCGPoint.x+10, aCGPoint.y+20);
}
//a distance case
+ (CGFloat)testReturnCGFloatWithaPoint:(CGPoint)aPoint bPoint:(CGPoint)bPoint{
return aPoint.x+aPoint.y+bPoint.x+bPoint.y;
}

- (NSRange)testReturnNSRangeWithNSRange:(NSRange)aNSRange{
return NSMakeRange(aNSRange.location+10, aNSRange.length+20);
}

- (NSRange)testReturnNSRangeWithaId:(id)aId aNSRange:(NSRange)aNSRange{
return NSMakeRange(aNSRange.location+10, aNSRange.length+20);
}
@end
3 changes: 1 addition & 2 deletions tools/AutoTest/AutoTest/AutoTestClass/AutoTestManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ @implementation AutoTestManager
+ (void)autoTestStart{
[AutoTestUtil runLuaFileInBundle:@"AutoTestConfig"];

NSArray *autoTestClassArray = @[@"AutoTestHookMethod", @"AutoTestBlockTransfer", @"AutoTestBlockCall"];
// NSArray *autoTestClassArray = @[@"AutoTestRuntime", @"AutoTestGCD", @"AutoTestUIKitFunction"];
NSArray *autoTestClassArray = @[@"AutoTestHookMethod", @"AutoTestBlockTransfer", @"AutoTestBlockCall", @"AutoTestRuntime", @"AutoTestGCD", @"AutoTestUIKitFunction"];
for (NSString *className in autoTestClassArray){
[AutoTestUtil runLuaFileInBundle:className];

Expand Down
2 changes: 1 addition & 1 deletion tools/AutoTest/AutoTest/AutoTestScript/AutoTestConfig.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ TEST_VALUE_bool = false
TEST_VALUE_FLOAT = 123.456
TEST_VALUE_CGFLOAT = 1234.5678
TEST_VALUE_DOUBLE = 12345.6789

TEST_VALUE_STRING = "abcdefg"


function isDoubleEqual(a, b)
Expand Down
4 changes: 3 additions & 1 deletion tools/AutoTest/AutoTest/AutoTestScript/AutoTestGCD.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ print("vb=", vb)
toblock(
function( )
print("dispatch_async back to main");
print(string.format("data.length=%d", data:length()));
if data ~= nil then
print(string.format("data.length=%d", data:length()));
end
end)
)
end)
Expand Down
Loading

0 comments on commit 00a0dcd

Please sign in to comment.