-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodifier_test.go
154 lines (141 loc) · 3.76 KB
/
modifier_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
145
146
147
148
149
150
151
152
153
154
package interpol
import (
"fmt"
"strings"
"testing"
"unicode"
"unicode/utf8"
)
func TestSimpleModifier(t *testing.T) {
var modifierTestData = []struct {
modifier string
indata string
outdata string
}{
{"reverse", "", ""},
{"reverse", "ABc", "cBA"},
{"reverse", "頭いってる", "るてっい頭"},
{"tolower", "Anders Jonas Ångström", "anders jonas ångström"},
{"tolower", "Anders Jonas Ångström", "anders jonas ångström"},
{"toupper", "Per Martin-Löf", "PER MARTIN-LÖF"},
{"toupper", "Lars Bergström", "LARS BERGSTRÖM"},
{"capitalize", "gordon GEKKO", "Gordon Gekko"},
{"empty", "bla bla bla", ""},
{"empty", "", ""},
{"len", "five", "4"},
{"base64", "", ""},
{"base64", "Fermat said he had a proof", "RmVybWF0IHNhaWQgaGUgaGFkIGEgcHJvb2Y="},
{"base64", "ずるい :)", "44Ga44KL44GEIDop"},
{"trim", "", ""},
{"trim", " AAA BB C \t\n", "AAA BB C"},
}
for _, test := range modifierTestData {
cmd := fmt.Sprintf("{{set data='%s' sep='%s' modifier=%s}}",
test.indata, "$$$", test.modifier)
ip := New()
str, err := ip.Add(cmd)
if err != nil {
t.Errorf("%s: failed to parse, %v", cmd, err)
} else {
if str.String() != test.outdata {
t.Errorf("expected %s got %s", str, test.outdata)
}
}
}
}
// since Leet is random we cant test it the normal way,
// instead we are looking at average of N runs
func TestLeetModifier(t *testing.T) {
N := 1000
indata := "I do my taxes in emacs..."
counters := make([]int, utf8.RuneCountInString(indata))
for i := 0; i < N; i++ {
ip := New()
cmd := fmt.Sprintf("{{set data='%s' sep=$$ modifier=1337}}", indata)
str, err := ip.Add(cmd)
if err != nil {
t.Errorf("%s: failed to parse, %v", cmd, err)
} else {
str2 := str.String()
if strings.ToLower(str2) != strings.ToLower(indata) {
t.Errorf("'%s' is not leet speak for '%s'", str, indata)
}
for i, c := range str2 {
if unicode.IsUpper(c) {
counters[i]++
}
}
}
}
// really bad statistical test:
for i, c := range indata {
if unicode.IsLetter(c) {
n := counters[i]
if 100*n < (15*N) || 100*n > (85*N) {
t.Errorf("Leet modifier charcter %d upper rate was %d%%", i, 100*n/N)
}
}
}
}
func TestBitflip(t *testing.T) {
N := 1000
indata := "Emacs is written in Lisp, which is the only computer language that is beautiful."
for i := 0; i < N; i++ {
ip := New()
cmd := fmt.Sprintf("{{set data='%s' sep=$$ modifier=bitflip}}", indata)
str, err := ip.Add(cmd)
if err != nil {
t.Errorf("%s: failed to parse, %v", cmd, err)
} else {
str2 := str.String()
if len(str2) != len(indata) {
t.Errorf("Bad size in bitflip")
} else {
cnt, diff := 0, 0
for i := range indata {
if indata[i] != str2[i] {
cnt++
diff = int(indata[i]) ^ int(str2[i])
}
}
if cnt != 1 {
t.Errorf("Not bytes were modified")
}
if (diff & (diff - 1)) != 0 { // diff is not a power of two?
t.Errorf("Exactly one bit should have been changed")
}
}
}
}
}
func TestByteswap(t *testing.T) {
N := 50
indata := "0123456789" // no repeating data
for i := 0; i < N; i++ {
ip := New()
cmd := fmt.Sprintf("{{set data='%s' sep=$$ modifier=byteswap}}", indata)
str, err := ip.Add(cmd)
if err != nil {
t.Errorf("%s: failed to parse, %v", cmd, err)
} else {
str2 := str.String()
if len(str2) != len(indata) {
t.Errorf("Bad size in byteswap")
} else {
cnt, mask := 0, 0
for i := range indata {
if indata[i] != str2[i] {
cnt++
}
// mask is a simple way to see if all bytes are still present
if str2[i] >= '0' && str2[i] <= '9' {
mask |= (1 << uint(str2[i]-'0'))
}
}
if cnt != 2 || mask != (1<<10)-1 {
t.Errorf("Exactly one byte should have been swapped")
}
}
}
}
}