-
Notifications
You must be signed in to change notification settings - Fork 10
/
patcher_test.go
144 lines (127 loc) · 2.49 KB
/
patcher_test.go
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
package mpatch
import (
"math/rand"
"reflect"
"testing"
)
//go:noinline
func methodA() int {
x := rand.Int() >> 48
y := rand.Int() >> 48
return x + y
}
//go:noinline
func methodB() int {
x := rand.Int() >> 48
y := rand.Int() >> 48
return -(x + y)
}
type myStruct struct {
}
//go:noinline
func (s *myStruct) Method() int {
return 1
}
//go:noinline
func (s myStruct) ValueMethod() int {
return 1
}
func TestPatcher(t *testing.T) {
patch, err := PatchMethod(methodA, methodB)
if err != nil {
t.Fatal(err)
}
if methodA() > 0 {
t.Fatal("The patch did not work")
}
err = patch.Unpatch()
if err != nil {
t.Fatal(err)
}
if methodA() < 0 {
t.Fatal("The unpatch did not work")
}
}
func TestPatcherUsingReflect(t *testing.T) {
reflectA := reflect.ValueOf(methodA)
patch, err := PatchMethodByReflectValue(reflectA, methodB)
if err != nil {
t.Fatal(err)
}
if methodA() > 0 {
t.Fatal("The patch did not work")
}
err = patch.Unpatch()
if err != nil {
t.Fatal(err)
}
if methodA() < 0 {
t.Fatal("The unpatch did not work")
}
}
func TestPatcherUsingMakeFunc(t *testing.T) {
reflectA := reflect.ValueOf(methodA)
patch, err := PatchMethodWithMakeFuncValue(reflectA,
func(args []reflect.Value) (results []reflect.Value) {
return []reflect.Value{reflect.ValueOf(42)}
})
if err != nil {
t.Fatal(err)
}
if methodA() != 42 {
t.Fatal("The patch did not work")
}
err = patch.Unpatch()
if err != nil {
t.Fatal(err)
}
if methodA() < 0 {
t.Fatal("The unpatch did not work")
}
}
func TestInstancePatcher(t *testing.T) {
mStruct := myStruct{}
var patch *Patch
var err error
patch, err = PatchInstanceMethodByName(reflect.TypeOf(mStruct), "Method", func(m *myStruct) int {
patch.Unpatch()
defer patch.Patch()
return 41 + m.Method()
})
if err != nil {
t.Fatal(err)
}
if mStruct.Method() != 42 {
t.Fatal("The patch did not work")
}
err = patch.Unpatch()
if err != nil {
t.Fatal(err)
}
if mStruct.Method() != 1 {
t.Fatal("The unpatch did not work")
}
}
func TestInstanceValuePatcher(t *testing.T) {
mStruct := myStruct{}
var patch *Patch
var err error
patch, err = PatchInstanceMethodByName(reflect.TypeOf(mStruct), "ValueMethod", func(m myStruct) int {
patch.Unpatch()
defer patch.Patch()
return 41 + m.Method()
})
if err != nil {
t.Fatal(err)
}
if mStruct.ValueMethod() != 42 {
t.Fatal("The patch did not work")
}
err = patch.Unpatch()
if err != nil {
t.Fatal(err)
}
if mStruct.ValueMethod() != 1 {
t.Fatal("The unpatch did not work")
}
}