-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_test.go
117 lines (103 loc) · 2.18 KB
/
service_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
package main
import (
"bytes"
"crypto/rand"
"github.com/dchest/uniuri"
"github.com/fdelbos/fe/rw"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"io"
)
var _ = Describe("Service", func() {
testBin := make([]byte, 1<<16)
rand.Read(testBin)
data := rw.NewData()
id := uniuri.New()
cat := &rw.Shell{
Cmd: "cat",
Name: "cat",
}
cat.Init()
It("run a simple pipeline", func() {
out := new(bytes.Buffer)
r, w := io.Pipe()
go func() {
w.Write(testBin)
w.Close()
}()
p := rw.NewEncoding(
[]rw.Encoder{cat},
r,
data)
Ω(p.Exec(out)).To(BeNil())
Ω(bytes.Equal(out.Bytes(), testBin)).To(BeTrue())
})
file := &rw.File{
Dir: "/tmp/" + uniuri.New(),
Name: "file",
}
file.Init()
service := &Service{
Url: "/test",
EncodingPipe: &rw.EncodingPipeline{
Output: file,
},
}
It("should encode", func() {
r, w := io.Pipe()
go func() {
w.Write(testBin)
w.Close()
}()
Ω(service.Encode(id, r, data)).To(BeNil())
})
service.DecodingPipe = &rw.DecodingPipeline{
Input: file,
}
It("should decode", func() {
out := new(bytes.Buffer)
r, w := io.Pipe()
go func() {
io.Copy(out, r)
}()
Ω(service.Decode(id, w, data)).To(BeNil())
Ω(bytes.Equal(out.Bytes(), testBin)).To(BeTrue())
})
aes := &rw.AES256{
Base64String: "ETl5QyPnHfi+vF4HrZfFvO2Julv4LVL7HNB1N7vkLGU=",
Name: "aes",
}
gzip := &rw.Gzip{
Algo: "speed",
}
It("should encode with Encoders", func() {
Ω(aes.Init()).To(BeNil())
Ω(gzip.Init()).To(BeNil())
service.EncodingPipe.Encoders = []rw.Encoder{gzip, aes}
r, w := io.Pipe()
go func() {
w.Write(testBin)
w.Close()
}()
Ω(service.Encode(id, r, data)).To(BeNil())
})
It("should decode", func() {
service.DecodingPipe.Decoders = []rw.Decoder{aes, gzip}
out := new(bytes.Buffer)
r, w := io.Pipe()
go func() {
io.Copy(out, r)
}()
Ω(service.Decode(id, w, data)).To(BeNil())
Ω(bytes.Equal(out.Bytes(), testBin)).To(BeTrue())
})
It("should not decode", func() {
service.DecodingPipe.Decoders = []rw.Decoder{aes, gzip}
out := new(bytes.Buffer)
r, w := io.Pipe()
go func() {
io.Copy(out, r)
}()
Ω(service.Decode("wrong", w, data)).ToNot(BeNil())
})
})