forked from liule/golang_escape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_escape_test.go
65 lines (59 loc) · 1.4 KB
/
mysql_escape_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
package mysql_escape
import (
"bytes"
. "github.com/smartystreets/goconvey/convey"
"io/ioutil"
"testing"
)
var phpCode string = `// use z to seperate, so no z in data
$src = array(
"normal",
"aaa\ro", // \r
"iam\0ooo", // \0
"\"shou\x08ldbe", // " and \b
"+123",
"'youyou'", // '
"\n gogogo",
"\026haha",
"tab \t iam tab",
"yoyo\\ yoyo",
"heros never die",
"heros%",
"heros_",
"heros$(&&^*&$##%^(*(*&$##&^^&(*)",
"??>><<>><?<:||)(&___*((*&%@%@$@#GJGJGFDOIUO*&*JKJKL137813"
);
$dest ="";
foreach ($src as $s){
$dest = $dest."z".$s."z".mysql_escape_string($s);
}
file_put_contents("sql_util_test.dat", $dest);
`
// the test data is generated by php, using mysql_escape_string. // data are sperated by 'z'
// phpCode are pasted above.
func TestMysqlEscapeString(t *testing.T) {
Convey("testMysqlEscape", t, func() {
data, err := ioutil.ReadFile("test.dat")
So(err, ShouldBeNil)
pairs := bytes.Split(data, []byte("z"))
pairs = pairs[1:]
for i := 0; i < len(pairs); i += 2 {
originalData := pairs[i]
destData := pairs[i+1]
escaped := MysqlEscapeString(string(originalData))
byteEqual(t, []byte(escaped), destData)
}
})
}
func byteEqual(t *testing.T, src, dest []byte) {
t.Log(src)
t.Log(dest)
if len(src) != len(dest) {
t.Error("len check fail")
}
for i, v := range src {
if dest[i] != v {
t.Error("fail in index %v", i)
}
}
}