-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathBlockHookSampleTests.m
559 lines (474 loc) · 19.5 KB
/
BlockHookSampleTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//
// BlockHookSampleTests.m
// BlockHookSample iOSTests
//
// Created by 杨萧玉 on 2019/4/19.
// Copyright © 2019 杨萧玉. All rights reserved.
//
#import <XCTest/XCTest.h>
#import <BlockHook/BlockHook.h>
struct TestStruct {
int64_t a;
double b;
float c;
char d;
int *e;
CGRect *f;
uint64_t g;
};
@interface BlockHookSampleTests : XCTestCase
@end
@implementation BlockHookSampleTests
struct TestStruct _testRect;
- (void)setUp {
// Put setup code here. This method is called before the invocation of each test method in the class.
int e = 5;
_testRect = (struct TestStruct){1, 2.0, 3.0, 4, &e, NULL, 7};
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
- (void)foo:(void(^)(void))block {
if (block) {
block();
}
}
- (void)performBlock:(void(^)(void))block {
BHToken *tokenAfter = [block block_hookWithMode:BlockHookModeAfter usingBlock:^{
NSLog(@"hook stack block succeed!");
}];
__unused BHToken *tokenDead = [block block_hookWithMode:BlockHookModeDead usingBlock:^{
NSLog(@"stack block dead!");
}];
[self foo:block];
[tokenAfter remove];
}
struct TestStruct (^structReturnBlock)(int) = ^(int x) {
struct TestStruct result = _testRect;
return result;
};
- (void)testStructReturn {
[structReturnBlock block_hookWithMode:BlockHookModeAfter usingBlock:^(BHInvocation *invocation, int x){
struct TestStruct ret;
[invocation getReturnValue:&ret];
ret.a = 100;
[invocation setReturnValue:&ret];
XCTAssert(x == 8, @"Wrong arg!");
}];
__unused struct TestStruct result = structReturnBlock(8);
XCTAssert(result.a == 100, @"Modify return struct failed!");
}
struct TestStruct *(^structPointerReturnBlock)(void) = ^() {
struct TestStruct *result = &_testRect;
return result;
};
- (void)testStructPointerReturn {
[structPointerReturnBlock block_hookWithMode:BlockHookModeAfter usingBlock:^(BHInvocation *invocation){
struct TestStruct *ret;
[invocation getReturnValue:&ret];
ret->a = 100;
[invocation setReturnValue:&ret];
}];
__unused struct TestStruct *result = structPointerReturnBlock();
XCTAssert(result->a == 100, @"Modify return struct failed!");
}
- (void)testObjectArg {
NSObject *argOrig = [NSObject new];
NSObject *argFixed = [NSObject new];
void (^ObjectArgBlock)(NSObject *) = ^(NSObject *test)
{
XCTAssert(test == argFixed, @"Modify struct member failed!");
};
[ObjectArgBlock block_hookWithMode:BlockHookModeBefore usingBlock:^(BHInvocation *invocation, NSObject *test){
XCTAssert(test == argOrig, @"Wrong arg!");
// Hook 改参数
[invocation setArgument:(void *)&argFixed atIndex:1];
}];
ObjectArgBlock(argOrig);
}
void (^structArgBlock)(struct TestStruct) = ^(struct TestStruct test) {
XCTAssert(test.a == 100, @"Modify struct member failed!");
};
- (void)testStructArg {
[structArgBlock block_hookWithMode:BlockHookModeBefore usingBlock:^(BHInvocation *invocation, struct TestStruct test){
// Hook 改参数
test.a = 100;
[invocation setArgument:&test atIndex:1];
}];
structArgBlock(_testRect);
}
CGRect (^rectBlock)(CGRect) = ^(CGRect test) {
XCTAssert(test.origin.x == 100, @"Modify struct member failed!");
return test;
};
- (void)testCGRectArgAndRet {
[rectBlock block_hookWithMode:BlockHookModeBefore usingBlock:^(BHInvocation *invocation, CGRect test){
// Hook 改参数
test.origin.x = 100;
[invocation setArgument:&test atIndex:1];
}];
rectBlock((CGRect){1,2,3,4});
}
void (^structPointerArgBlock)(struct TestStruct *) = ^(struct TestStruct *test) {
XCTAssert(test->a == 100, @"Modify struct member failed!");
};
- (void)testStructPointerArg {
[structPointerArgBlock block_hookWithMode:BlockHookModeBefore usingBlock:^(BHInvocation *invocation, struct TestStruct *test){
// Hook 改参数
test->a = 100;
[invocation setArgument:&test atIndex:1];
}];
structPointerArgBlock(&_testRect);
}
- (void)testStackBlock {
NSObject *z = NSObject.new;
[self performBlock:^{
NSLog(@"stack block:%@", z);
}];
}
- (void)testGlobalBlock {
[self performBlock:^{
NSLog(@"global block");
}];
}
const char *(^protocolBlock)(id<CALayerDelegate>, int(^)(int, int)) = ^(id<CALayerDelegate> delegate, int(^block)(int, int)) {
if (block) {
block(1, 2);
}
return (const char *)"test protocol";
};
- (void)testProtocol {
int(^block)(int x, int y) = ^int(int x, int y) {
int result = x + y;
NSLog(@"%d + %d = %d", x, y, result);
return result;
};
const char *fakeResult = "lalalala";
[protocolBlock block_hookWithMode:BlockHookModeAfter usingBlock:^(BHInvocation *invocation, id<CALayerDelegate> delegate, int(^block)(int x, int y)){
[invocation setReturnValue:(void *)&fakeResult];
}];
id z = [NSObject new];
__unused const char *result = protocolBlock(z, block);
XCTAssert(strcmp(result, fakeResult) == 0, @"Change const char * result failed!");
}
- (void)testHookBlock {
NSObject *z = NSObject.new;
int(^block)(int x, int y) = ^int(int x, int y) {
int result = x + y;
NSLog(@"%d + %d = %d, z is a NSObject: %@", x, y, result, z);
return result;
};
__unused BHToken *tokenDead = [block block_hookWithMode:BlockHookModeDead usingBlock:^(BHInvocation *invocation){
// BHInvocation is the only arg.
NSLog(@"block dead! token:%@", invocation.token);
}];
BHToken *tokenInstead = [block block_hookWithMode:BlockHookModeInstead usingBlock:^(BHInvocation *invocation, int x, int y){
[invocation invokeOriginalBlock];
int ret = 0;
[invocation getReturnValue:&ret];
NSLog(@"let me see original result: %d", ret);
// change the block imp and result
ret = x * y;
[invocation setReturnValue:&ret];
NSLog(@"hook instead: '+' -> '*'");
}];
XCTAssert([tokenInstead.mangleName isEqualToString:@"__37-[BlockHookSampleTests testHookBlock]_block_invoke"], @"Wrong mangle name!");
__unused BHToken *tokenAfter = [block block_hookWithMode:BlockHookModeAfter usingBlock:^(BHInvocation *invocation, int x, int y){
// print args and result
int ret = 0;
[invocation getReturnValue:&ret];
NSLog(@"hook after block! %d * %d = %d", x, y, ret);
}];
__unused BHToken *tokenBefore = [block block_hookWithMode:BlockHookModeBefore usingBlock:^(BHInvocation *invocation){
// BHInvocation has to be the first arg.
NSLog(@"hook before block! invocation:%@", invocation);
}];
NSLog(@"hooked block");
int ret = block(3, 5);
XCTAssert(ret == 15, @"hook failed!");
NSLog(@"hooked result:%d", ret);
// remove token.
[tokenInstead remove];
NSLog(@"remove tokens, original block");
ret = block(3, 5);
XCTAssert(ret == 8, @"remove hook failed!");
NSLog(@"original result:%d", ret);
// [tokenDead remove];
}
- (void)testRemoveAll {
NSObject *z = NSObject.new;
int(^block)(int x, int y) = ^int(int x, int y) {
int result = x + y;
NSLog(@"%d + %d = %d, z is a NSObject: %@", x, y, result, z);
return result;
};
[block block_hookWithMode:BlockHookModeDead usingBlock:^(BHInvocation *invocation){
// BHInvocation is the only arg.
NSLog(@"block dead! token:%@", invocation.token);
}];
[block block_hookWithMode:BlockHookModeInstead usingBlock:^(BHInvocation *invocation, int x, int y){
[invocation invokeOriginalBlock];
int ret = 0;
[invocation getReturnValue:&ret];
NSLog(@"let me see original result: %d", ret);
// change the block imp and result
ret = x * y;
[invocation setReturnValue:&ret];
NSLog(@"hook instead: '+' -> '*'");
}];
[block block_hookWithMode:BlockHookModeAfter usingBlock:^(BHInvocation *invocation, int x, int y){
// print args and result
int ret = 0;
[invocation getReturnValue:&ret];
NSLog(@"hook after block! %d * %d = %d", x, y, ret);
}];
[block block_hookWithMode:BlockHookModeBefore usingBlock:^(BHInvocation *invocation){
// BHInvocation has to be the first arg.
NSLog(@"hook before block! invocation:%@", invocation);
}];
NSLog(@"hooked block");
int ret = block(3, 5);
XCTAssert(ret == 15, @"hook failed!");
NSLog(@"hooked result:%d", ret);
// remove all tokens when you don't need.
[block block_removeAllHook];
NSLog(@"remove tokens, original block");
ret = block(3, 5);
NSLog(@"original result:%d", ret);
XCTAssert(ret == 8, @"remove hook failed!");
XCTAssert([block block_currentHookToken] == nil, @"remove all hook failed!");
}
- (void)testOverstepArgs {
NSObject *z = NSObject.new;
int(^block)(int x, int y) = ^int(int x, int y) {
int result = x + y;
NSLog(@"%d + %d = %d, z is a NSObject: %@", x, y, result, z);
return result;
};
__unused BHToken *tokenDead = [block block_hookWithMode:BlockHookModeDead usingBlock:^(BHInvocation *invocation, int a){
// BHInvocation is the only arg.
NSLog(@"block dead! token:%@", invocation.token);
XCTAssert(a == 0, @"Overstep args for DeadMode not pass!.");
}];
XCTAssert(tokenDead != nil, @"Overstep args for DeadMode not pass!.");
__unused BHToken *tokenInstead = [block block_hookWithMode:BlockHookModeInstead usingBlock:^(BHInvocation *invocation, int x, int y, int a){
[invocation invokeOriginalBlock];
int ret = 0;
[invocation getReturnValue:&ret];
NSLog(@"let me see original result: %d", ret);
// change the block imp and result
ret = x * y;
[invocation setReturnValue:&ret];
NSLog(@"hook instead: '+' -> '*'");
XCTAssert(a == 0, @"Overstep args for DeadMode not pass!.");
}];
XCTAssert(tokenInstead != nil, @"Overstep args for InsteadMode not pass!.");
block(1, 2);
}
- (void)testDispatchBlockCreate {
XCTestExpectation *expectation = [[XCTestExpectation alloc] initWithDescription:@"Wait for block invoke."];
dispatch_queue_t queue = dispatch_queue_create("com.blockhook.test", DISPATCH_QUEUE_SERIAL);
dispatch_block_t block = dispatch_block_create(0, ^{
NSLog(@"I'm dispatch_block_t");
[expectation fulfill];
});
__unused BHToken *token = [block block_hookWithMode:BlockHookModeAfter
usingBlock:^(BHInvocation *invocation){
NSLog(@"dispatch_block_t: Hook After");
}];
XCTAssert(token != nil, @"Hook dispatch_block_create not pass!.");
dispatch_async(queue, block);
[self waitForExpectations:@[expectation] timeout:30];
dispatch_block_cancel(block);
}
- (void)testMultiModeHook {
NSObject *z = NSObject.new;
int(^block)(int x, int y) = ^int(int x, int y) {
int result = x + y;
NSLog(@"%d + %d = %d, z is a NSObject: %@", x, y, result, z);
return result;
};
BHToken *token = [block block_hookWithMode:BlockHookModeDead|BlockHookModeBefore|BlockHookModeInstead|BlockHookModeAfter
usingBlock:^(BHInvocation *invocation, int x, int y) {
int ret = 0;
[invocation getReturnValue:&ret];
switch (invocation.mode) {
case BlockHookModeBefore:
// BHInvocation has to be the first arg.
NSLog(@"hook before block! invocation:%@", invocation);
break;
case BlockHookModeInstead:
[invocation invokeOriginalBlock];
NSLog(@"let me see original result: %d", ret);
// change the block imp and result
ret = x * y;
[invocation setReturnValue:&ret];
NSLog(@"hook instead: '+' -> '*'");
break;
case BlockHookModeAfter:
// print args and result
NSLog(@"hook after block! %d * %d = %d", x, y, ret);
break;
case BlockHookModeDead:
// BHInvocation is the only arg.
NSLog(@"block dead! token:%@", invocation.token);
break;
default:
break;
}
}];
NSLog(@"hooked block");
int ret = block(3, 5);
XCTAssert(ret == 15, @"hook failed!");
NSLog(@"hooked result:%d", ret);
// remove token.
[token remove];
NSLog(@"remove tokens, original block");
ret = block(3, 5);
XCTAssert(ret == 8, @"remove hook failed!");
NSLog(@"original result:%d", ret);
}
- (void)testSyncInterceptor {
NSObject *ret1 = [NSObject new];
NSObject *testArg = [NSObject new];
NSObject *testArg1 = [NSObject new];
NSObject *(^testblock)(NSObject *) = ^(NSObject *a) {
XCTAssert(a == testArg1, @"Sync Interceptor change argument failed!");
return [NSObject new];
};
[testblock block_interceptor:^(BHInvocation *invocation, IntercepterCompletion _Nonnull completion) {
NSObject * __unsafe_unretained arg;
[invocation getArgument:&arg atIndex:1];
XCTAssert(arg == testArg, @"Sync Interceptor wrong argument!");
[invocation setArgument:(void *)&testArg1 atIndex:1];
completion();
[invocation setReturnValue:(void *)&ret1];
}];
NSObject *result = testblock(testArg);
XCTAssert(result == ret1, @"Sync Interceptor change return value failed!");
NSLog(@"result:%@", result);
}
- (void)testAsyncInterceptor {
NSObject *testArg = [NSObject new];
NSObject *testArg1 = [NSObject new];
XCTestExpectation *expectation = [[XCTestExpectation alloc] initWithDescription:@"Wait for block invoke."];
NSObject *(^testblock)(NSObject *) = ^(NSObject *a) {
XCTAssert(a == testArg1, @"Async Interceptor change argument failed!");
return [NSObject new];
};
[testblock block_interceptor:^(BHInvocation *invocation, IntercepterCompletion _Nonnull completion) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSObject * __unsafe_unretained arg;
[invocation getArgument:&arg atIndex:1];
XCTAssert(arg == testArg, @"Async Interceptor wrong argument!");
[invocation setArgument:(void *)&testArg1 atIndex:1];
completion();
NSObject *ret = [NSObject new];
[invocation setReturnValue:(void *)&ret];
[expectation fulfill];
});
}];
NSObject *result = testblock(testArg);
NSLog(@"result:%@", result);
[self waitForExpectations:@[expectation] timeout:30];
}
NSObject *(^testSyncCharArgBlock)(char *) = ^(char *a) {
XCTAssert(strcmp(a, "hooked") == 0, @"Sync Char Arg Interceptor change argument failed!");
return [NSObject new];
};
- (void)testSyncCharArgInterceptor {
NSObject *ret1 = [NSObject new];
char *origChar = (char *)malloc(sizeof(char) * 7);
strcpy(origChar, "origin");
[testSyncCharArgBlock block_interceptor:^(BHInvocation *invocation, IntercepterCompletion _Nonnull completion) {
char *arg;
[invocation getArgument:&arg atIndex:1];
XCTAssert(strcmp(arg, "origin") == 0, @"Sync Char Arg Interceptor wrong argument!");
char *hooked = "hooked";
[invocation setArgument:(void *)&hooked atIndex:1];
completion();
[invocation setReturnValue:(void *)&ret1];
}];
__unused NSObject *result = testSyncCharArgBlock(origChar);
origChar[1] = '1';
free(origChar);
XCTAssert(result == ret1, @"Sync Char Arg Interceptor change return value failed!");
}
NSObject *(^testAsyncCharArgBlock)(char *) = ^(char *a) {
XCTAssert(strcmp(a, "hooked") == 0, @"Async Char Arg Interceptor change argument failed!");
return [NSObject new];
};
- (void)testAsyncCharArgInterceptor {
XCTestExpectation *expectation = [[XCTestExpectation alloc] initWithDescription:@"Wait for block invoke."];
char *origChar = (char *)malloc(sizeof(char) * 7);
strcpy(origChar, "origin");
[testAsyncCharArgBlock block_interceptor:^(BHInvocation *invocation, IntercepterCompletion _Nonnull completion) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
char *arg;
[invocation getArgument:&arg atIndex:1];
XCTAssert(strcmp(arg, "origin") == 0, @"Async Char Arg Interceptor wrong argument!");
char *hooked = "hooked";
[invocation setArgument:(void *)&hooked atIndex:1];
completion();
NSObject *ret = [NSObject new];
[invocation setReturnValue:(void *)&ret];
[expectation fulfill];
});
}];
NSObject *result = testAsyncCharArgBlock(origChar);
origChar[1] = '1';
free(origChar);
NSLog(@"result:%@", result);
[self waitForExpectations:@[expectation] timeout:30];
}
- (void)testSyncStructReturnInterceptor {
NSObject *testArg = [NSObject new];
NSObject *testArg1 = [NSObject new];
struct TestStruct (^StructReturnBlock)(NSObject *) = ^(NSObject *a)
{
XCTAssert(a == testArg1, @"Sync Struct Return Interceptor change argument failed!");
struct TestStruct result = _testRect;
return result;
};
[StructReturnBlock block_interceptor:^(BHInvocation *invocation, IntercepterCompletion _Nonnull completion) {
NSObject * __unsafe_unretained arg;
[invocation getArgument:&arg atIndex:1];
XCTAssert(arg == testArg, @"Sync Interceptor wrong argument!");
[invocation setArgument:(void *)&testArg1 atIndex:1];
completion();
struct TestStruct ret;
[invocation getReturnValue:&ret];
ret.a = 100;
[invocation setReturnValue:&ret];
}];
__unused struct TestStruct result = StructReturnBlock(testArg);
XCTAssert(result.a == 100, @"Sync Interceptor change return value failed!");
}
- (void)testAsyncStructReturnInterceptor {
NSObject *testArg = [NSObject new];
NSObject *testArg1 = [NSObject new];
XCTestExpectation *expectation = [[XCTestExpectation alloc] initWithDescription:@"Wait for block invoke."];
struct TestStruct (^StructReturnBlock)(NSObject *) = ^(NSObject *a)
{
XCTAssert(a == testArg1, @"Sync Struct Return Interceptor change argument failed!");
struct TestStruct result = _testRect;
return result;
};
[StructReturnBlock block_interceptor:^(BHInvocation *invocation, IntercepterCompletion _Nonnull completion) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSObject * __unsafe_unretained arg;
[invocation getArgument:&arg atIndex:1];
XCTAssert(arg == testArg, @"Sync Interceptor wrong argument!");
[invocation setArgument:(void *)&testArg1 atIndex:1];
completion();
struct TestStruct ret;
[invocation getReturnValue:&ret];
ret.a = 100;
[invocation setReturnValue:&ret];
[expectation fulfill];
});
}];
__unused struct TestStruct result = StructReturnBlock(testArg);
[self waitForExpectations:@[expectation] timeout:30];
}
@end