]> git.saurik.com Git - apple/objc4.git/blob - test/getImageNameHook.m
objc4-818.2.tar.gz
[apple/objc4.git] / test / getImageNameHook.m
1 // TEST_CONFIG
2
3 #include "test.h"
4 #include "testroot.i"
5
6 @interface One : TestRoot @end
7 @implementation One @end
8
9 @interface Two : TestRoot @end
10 @implementation Two @end
11
12 @interface Both : TestRoot @end
13 @implementation Both @end
14
15 @interface None : TestRoot @end
16 @implementation None @end
17
18
19 objc_hook_getImageName OnePreviousHook;
20 BOOL GetImageNameHookOne(Class cls, const char **outName)
21 {
22 if (0 == strcmp(class_getName(cls), "One")) {
23 *outName = "Image One";
24 return YES;
25 } else if (0 == strcmp(class_getName(cls), "Both")) {
26 *outName = "Image Both via One";
27 return YES;
28 } else {
29 return OnePreviousHook(cls, outName);
30 }
31 }
32
33 objc_hook_getImageName TwoPreviousHook;
34 BOOL GetImageNameHookTwo(Class cls, const char **outName)
35 {
36 if (0 == strcmp(class_getName(cls), "Two")) {
37 *outName = "Image Two";
38 return YES;
39 } else if (0 == strcmp(class_getName(cls), "Both")) {
40 *outName = "Image Both via Two";
41 return YES;
42 } else {
43 return TwoPreviousHook(cls, outName);
44 }
45 }
46
47 int main()
48 {
49
50 // before hooks: main executable is the image name for four classes
51 testassert(strstr(class_getImageName([One class]), "getImageNameHook"));
52 testassert(strstr(class_getImageName([Two class]), "getImageNameHook"));
53 testassert(strstr(class_getImageName([Both class]), "getImageNameHook"));
54 testassert(strstr(class_getImageName([None class]), "getImageNameHook"));
55 testassert(strstr(class_getImageName([NSObject class]), "libobjc"));
56
57 // install hook One
58 objc_setHook_getImageName(GetImageNameHookOne, &OnePreviousHook);
59
60 // two classes are in Image One with hook One in place
61 testassert(strstr(class_getImageName([One class]), "Image One"));
62 testassert(strstr(class_getImageName([Two class]), "getImageNameHook"));
63 testassert(strstr(class_getImageName([Both class]), "Image Both via One"));
64 testassert(strstr(class_getImageName([None class]), "getImageNameHook"));
65 testassert(strstr(class_getImageName([NSObject class]), "libobjc"));
66
67 // install hook Two which chains to One
68 objc_setHook_getImageName(GetImageNameHookTwo, &TwoPreviousHook);
69
70 // two classes are in Image Two and one in One with both hooks in place
71 testassert(strstr(class_getImageName([One class]), "Image One"));
72 testassert(strstr(class_getImageName([Two class]), "Image Two"));
73 testassert(strstr(class_getImageName([Both class]), "Image Both via Two"));
74 testassert(strstr(class_getImageName([None class]), "getImageNameHook"));
75 testassert(strstr(class_getImageName([NSObject class]), "libobjc"));
76
77 succeed(__FILE__);
78 }