-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintf.go
More file actions
42 lines (32 loc) · 839 Bytes
/
printf.go
File metadata and controls
42 lines (32 loc) · 839 Bytes
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
package EvilGo
import (
"fmt"
"github.com/MXuDong/test-utils-go/patch"
"os"
"time"
)
func init() {
}
const defaultReplaceWeight int = 10
var alreadyInjection bool = false
var printlnValue string = "Evil for go is inject ;-)"
func EvilPrintf(replaceWeight int, replaceValue string, unReplaceDuration time.Duration) {
if replaceWeight >= 100 || replaceWeight < 0 {
replaceWeight = defaultReplaceWeight
}
printlnValue = replaceValue
rv := randInstance.Int()
if rv%100 < replaceWeight && !alreadyInjection {
r := patch.Cover(fmt.Println, PrintlnReplacer)
alreadyInjection = true
// do reset with duration
go func() {
time.Sleep(unReplaceDuration)
r.Restore()
alreadyInjection = false
}()
}
}
func PrintlnReplacer(a ...interface{}) (n int, err error) {
return fmt.Fprintln(os.Stdout, printlnValue)
}