-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencdec_test.go
62 lines (56 loc) · 1.28 KB
/
encdec_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
package simplemaria
import (
"log"
"math/rand"
"testing"
"time"
)
func TestEncodeDecode(t *testing.T) {
hello := "hello"
original := hello
Encode(&hello)
Decode(&hello)
if hello != original {
t.Error("Unable to encode and decode: " + original)
}
}
func TestEncodeDecodeWithNewline(t *testing.T) {
newlinedrop := "\n!''' DROP TABLES EVERYWHERE"
original := newlinedrop
Encode(&newlinedrop)
Decode(&newlinedrop)
if newlinedrop != original {
t.Error("Unable to encode and decode: " + original)
}
}
func TestEncodeDecodeWithEOB(t *testing.T) {
weirdness := "\xbd\xb2\x3d\x17\xbc\x20\xe2\x8c\x98"
original := weirdness
Encode(&weirdness)
Decode(&weirdness)
if weirdness != original {
t.Error("Unable to encode and decode: " + original)
}
}
// Generate a random string of the given length.
func randomString(length int) string {
b := make([]byte, length)
for i := 0; i < length; i++ {
b[i] = byte(rand.Int63() & 0xff)
}
return string(b)
}
func TestRandom(t *testing.T) {
// Generate 10 random strings and check if they encode and decode correctly
rand.Seed(time.Now().UnixNano())
for i := 0; i < 10; i++ {
log.Printf("Random string %d\n", i)
s1 := randomString(100)
s2 := s1
Encode(&s2)
Decode(&s2)
if s1 != s2 {
t.Error(s1, "is different from", s2)
}
}
}