forked from ostrovok-tech/pgdump-obfuscator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
270 lines (242 loc) · 7.7 KB
/
main_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
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
package main
import (
"bufio"
"bytes"
"strings"
"testing"
)
var testConfig01 = &Configuration{
Obfuscations: []TargetedObfuscation{
TargetedObfuscation{
Target{Table: "auth_user", Column: "email"},
ScrambleEmail,
},
TargetedObfuscation{
Target{Table: "auth_user", Column: "password"},
GenScrambleBytes(7),
},
TargetedObfuscation{
Target{Table: "accounts_profile", Column: "phone"},
ScrambleDigits,
},
},
}
const testInput01 = `
--
SELECT pg_catalog.setval('auth_user_id_seq', 123111, true);
--
-- Data for Name: auth_user; Type: TABLE DATA; Schema: public; Owner: postgres
--
COPY auth_user (id, username, first_name, last_name, email, password, is_staff, is_active, is_superuser, last_login, date_joined) FROM stdin;
123123111 499964777.sdsad testname testname \N ! f t f 2011-02-07 12:08:30+00 2010-11-22 19:27:12.31832+00
333114441 [email protected] [email protected] ! f t f 2011-06-08 12:57:36+00 2011-06-08 12:50:25.206298+00
515131311 whoisthere [email protected] pbkdf2_sha256$10000$qweqweqweqwe$cThxOHE4 f t f 2012-11-16 18:27:43.673889+00 2012-11-16 18:27:43.229281+00
\.
COPY accounts_profile (id, user_id, opted_in, next_break, status, phone, last_visited, come_from, cs_letter, city_id, budget_range_id, prefs_opt) FROM stdin;
6161 12113 f \N 0 +74991002000 2011-07-04 12:28:33.895325+00 \N f \N \N \N
1223 1321 f \N 0 666666666 2011-09-28 09:37:20.83051+00 \N f \N \N \N
4423 55512 f \N 0 \N \N f \N \N \N
\.
`
func assertScramble(t *testing.T, scramble func([]byte) []byte, s, expected string) {
assertString(t, string(scramble([]byte(s))), expected)
}
func assertString(t *testing.T, s, expected string) {
if s != expected {
t.Fatalf("Expected: '%s'(%d) received: '%s'(%d)", expected, len(expected), s, len(s))
}
}
func TestProcess01(t *testing.T) {
input := bufio.NewReader(bytes.NewBufferString(testInput01))
output := new(bytes.Buffer)
process(testConfig01, input, output)
outString := output.String()
if outString == testInput01 {
t.Fatal("Outputs are equal")
}
if !strings.Contains(outString, "COPY auth_user (id, username, first_name, last_name, email, password, is_staff, is_active, is_superuser, last_login, date_joined) FROM stdin;") ||
!strings.Contains(outString, "COPY accounts_profile (id, user_id, opted_in, next_break, status, phone, last_visited, come_from, cs_letter, city_id, budget_range_id, prefs_opt) FROM stdin;") {
t.Fatal("Changed SQL")
}
if strings.Contains(outString, "pbkdf2_sha256$10000$qweqweqweqwe$cThxOHE4") ||
strings.Contains(outString, "+3801445223001") {
t.Fatal("Did not scramble sensitive data")
}
if !strings.Contains(outString, "515131311 whoisthere") ||
!strings.Contains(outString, ` 2011-07-04 12:28:33.895325+00 \N f \N \N \N`) ||
!strings.Contains(outString, `1223 1321 f \N 0 `) {
t.Fatal("Changed other data")
}
lines := strings.Split(outString, "\n")
if !strings.HasPrefix(lines[11], "123123111") {
t.Fatal("Line 12 invalid:", lines[11])
}
fields := strings.Split(lines[11], "\t")
assertString(t, fields[4], "\\N")
}
func TestProcess02(t *testing.T) {
testConfig02 := &Configuration{
Obfuscations: []TargetedObfuscation{
TargetedObfuscation{
Target{Table: "with_emails", Column: "id"},
ScrambleDigits,
},
TargetedObfuscation{
Target{Table: "with_emails", Column: "emails_list"},
ScrambleEmail,
},
},
}
const testInput02 = `COPY with_emails (id, emails_list) FROM stdin;
\.
`
const expected = `COPY with_emails (id, emails_list) FROM stdin;
\.
`
input := bufio.NewReader(bytes.NewBufferString(testInput02))
output := new(bytes.Buffer)
Salt = []byte("test-salt")
process(testConfig02, input, output)
assertString(t, output.String(), expected)
}
func TestScrambleBytes(t *testing.T) {
Salt = []byte("test-salt")
assertScramble(t, ScrambleBytes, "everyone lies", "oSE0Sm0yioFSJ")
assertScramble(t, ScrambleBytes, "very long line very long line very long line very",
"4ce6EsWcmziuUzpEtV0rGiZAOtiHprwB0wWWWuOYrHkqHQtAN")
assertScramble(t, ScrambleBytes, "{item1,\"item space 2\"}",
"{yho3y,rEZwPM7FVuVf1S}")
}
func TestScrambleBytesUtf8(t *testing.T) {
Salt = []byte("test-salt")
// Output must be of same length as input
assertScramble(t, ScrambleBytes, "also русский and 你好",
"emEY0UP-gkC2kV+J6pK")
assertScramble(t, ScrambleBytes, "{\"array z\",руки,你好}",
"{LoKXy6kRZ,uefS,G1}")
}
func TestScrambleDigits(t *testing.T) {
Salt = []byte("test-salt")
assertScramble(t, ScrambleDigits, "+7(876) 123-0011 или 99999999999;",
"+1(584) 047-9250 или 22280031035;")
assertScramble(t, ScrambleDigits, "5", "7")
}
func TestScrambleEmail(t *testing.T) {
Salt = []byte("test-salt")
assertScramble(t, ScrambleEmail, "[email protected]",
assertScramble(t, ScrambleEmail, "{[email protected],[email protected]}",
assertScramble(t, ScrambleEmail, "унеун@mail.ru", "[email protected]")
assertScramble(t, ScrambleEmail, "[email protected],[email protected]",
}
func TestScrambleInet(t *testing.T) {
Salt = []byte("test-salt")
assertScramble(t, ScrambleInet, "142.34.56.78", "56.42.246.77")
assertScramble(t, ScrambleInet, "97.34.0.18", "e4ed:d550:209d:9f10:f690:953:5d4f:c0d6")
}
func BenchmarkProcessShort(b *testing.B) {
b.StopTimer()
config := &Configuration{
Obfuscations: []TargetedObfuscation{
TargetedObfuscation{
Target{Table: "simple", Column: "id"},
ScrambleDigits,
},
TargetedObfuscation{
Target{Table: "simple", Column: "email"},
ScrambleEmail,
},
TargetedObfuscation{
Target{Table: "simple", Column: "password"},
ScrambleBytes,
},
},
}
const s = `--
-- Useless comments
--
select 'and other statements';
create table simple (
id serial not null,
email text not null,
password text not null
);
COPY simple (id, email, password) FROM stdin;
13 [email protected] 12345
27 [email protected] password
28 [email protected] strongPassw0rd
33 [email protected] allyourbase
41 [email protected] belongto
42 [email protected] usususus
43 [email protected] 12345
44 [email protected] password
48 [email protected] strongPassw0rd
49 [email protected] allyourbase
50 [email protected] belongto
121 [email protected] usususus
122 [email protected] 12345
123 [email protected] password
124 [email protected] strongPassw0rd
125 [email protected] allyourbase
126 [email protected] belongto
127 [email protected] usususus
\.
`
var input *bufio.Reader
var output *bytes.Buffer
for i := 0; i < b.N; i++ {
input = bufio.NewReader(bytes.NewBufferString(s))
output = new(bytes.Buffer)
b.StartTimer()
process(config, input, output)
b.StopTimer()
}
}
func BenchmarkScrambleBytes(b *testing.B) {
Salt = []byte("test-salt")
s := []byte("everybody lies many times")
for i := 0; i < b.N; i++ {
ScrambleBytes(s)
}
}
func BenchmarkScrambleBytesArray(b *testing.B) {
Salt = []byte("test-salt")
s := []byte("{everybody,lies,\"many many many\",times}")
for i := 0; i < b.N; i++ {
ScrambleBytes(s)
}
}
func BenchmarkScrambleDigits(b *testing.B) {
Salt = []byte("test-salt")
digits := "+7(876) 123-0011"
for i := 0; i < b.N; i++ {
ScrambleDigits([]byte(digits))
}
}
func BenchmarkScrambleEmail(b *testing.B) {
Salt = []byte("test-salt")
email := "[email protected]"
for i := 0; i < b.N; i++ {
ScrambleEmail([]byte(email))
}
}
func BenchmarkScrambleEmailArray(b *testing.B) {
Salt = []byte("test-salt")
for i := 0; i < b.N; i++ {
ScrambleEmail([]byte(email))
}
}
func BenchmarkScrambleInet(b *testing.B) {
Salt = []byte("test-salt")
s := []byte("23.11.1.239")
for i := 0; i < b.N; i++ {
ScrambleInet(s)
}
}