-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader_test.go
228 lines (213 loc) · 4.99 KB
/
reader_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
package mxt
import (
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestReadString(t *testing.T) {
cases := []struct {
inp string
exp Chunks
}{
{
inp: `//---------------------------------------------------------------- user.json -->
{
"user": "alucard",
"password": "C:SotN1997"
}
//--------------------------------------------------------------- connection.ini
// comment line that is not part of the ini file,
// comment lines will be joined with a space character
//
// empty comment lines will generate a newline character in the comment
//----------------------------------------------------------------------------->
request: GET
[url]
schema=http
host=localhost
port=8080
path=/db/add
// user.pgp --> XYZ
-----BEGIN PGP MESSAGE-----
hQEMA8p144+Gi+YpAQf/VeFG9Zb+8w9aldWll8n2g3jqpE613LKg2XAJgwXQmSQL
R4O+TlQakJ+Mz5vM4IxxubPgYCyt6cyL7qM3oJIuk7vsqMbl5t7c/dOfXjj7goIC
IskIX+9e5qrr8jRG/KZYSdBJtFEI9oNtZTLlnv3yeV3OWNTbUnjdTWrk/h1kavJE
D8nD4suo6ckVzYGJpknGSIAwaCFl//aqR/3SWO4wi6ibbfub8LA73V90Ll3/S/Ph
xU15HYmdCATnVX1sp1PWmyz972bMvl8txyIKMUueVw+w0C19ZTfWXjuFSguF7zt7
RY+I3to2lbyVJbcI9Dyz04GOJZ2vIhG9eq65FxeweAKDa7L+iH1NA5L2lYd9DEr1
ro/CU6vIqkOSNRUrNYDwqz1g3Z3eAQB/8t9Y4WsV4KL0M229rsFrtl26i7+quYfg
uuTd
=WxK9
-----END PGP MESSAGE-----
//XYZ hello-world.h -->
//---------------------------------------------------------- hello-world.c --> X
// this is part of hello-world.c
#include<stdio.h>
int main(void) {
printf("Hello World\n");
return 0;
}`,
exp: Chunks{
{
Name: "user.json",
Content: `{
"user": "alucard",
"password": "C:SotN1997"
}`,
},
{
Name: "connection.ini",
Content: `request: GET
[url]
schema=http
host=localhost
port=8080
path=/db/add`,
},
{
Name: "user.pgp",
Content: `-----BEGIN PGP MESSAGE-----
hQEMA8p144+Gi+YpAQf/VeFG9Zb+8w9aldWll8n2g3jqpE613LKg2XAJgwXQmSQL
R4O+TlQakJ+Mz5vM4IxxubPgYCyt6cyL7qM3oJIuk7vsqMbl5t7c/dOfXjj7goIC
IskIX+9e5qrr8jRG/KZYSdBJtFEI9oNtZTLlnv3yeV3OWNTbUnjdTWrk/h1kavJE
D8nD4suo6ckVzYGJpknGSIAwaCFl//aqR/3SWO4wi6ibbfub8LA73V90Ll3/S/Ph
xU15HYmdCATnVX1sp1PWmyz972bMvl8txyIKMUueVw+w0C19ZTfWXjuFSguF7zt7
RY+I3to2lbyVJbcI9Dyz04GOJZ2vIhG9eq65FxeweAKDa7L+iH1NA5L2lYd9DEr1
ro/CU6vIqkOSNRUrNYDwqz1g3Z3eAQB/8t9Y4WsV4KL0M229rsFrtl26i7+quYfg
uuTd
=WxK9
-----END PGP MESSAGE-----`,
},
{
Name: "hello-world.h",
Content: "",
},
{
Name: "hello-world.c",
Content: `// this is part of hello-world.c
#include<stdio.h>
int main(void) {
printf("Hello World\n");
return 0;
}`,
},
},
},
{
inp: `// base64 -->
hQEMA8p144+Gi+YpAQf/VeFG9Zb+8w9aldWll8n2g3jqpE613LKg2XAJgwXQmSQL
R4O+TlQakJ+//5vM4IxxubPgYCyt6cyL7qM3oJIuk7vsqMbl5t7c/dOfXjj7goIC
// last -->
tsal`,
exp: Chunks{
{
Name: "base64",
Content: `hQEMA8p144+Gi+YpAQf/VeFG9Zb+8w9aldWll8n2g3jqpE613LKg2XAJgwXQmSQL
R4O+TlQakJ+//5vM4IxxubPgYCyt6cyL7qM3oJIuk7vsqMbl5t7c/dOfXjj7goIC`,
},
{
Name: "last",
Content: "tsal",
},
},
},
}
for i, c := range cases {
chunks, err := ReadString(c.inp)
if err != nil {
t.Errorf("%v - unexpected error: %v", i, err)
continue
}
if len(chunks) != len(c.exp) {
t.Errorf("%v - unexpected number chunks: %v != %v", i, len(chunks), len(c.exp))
continue
}
for ci, cv := range chunks {
if diff := cmp.Diff(c.exp[ci].Name, cv.Name); diff != "" {
t.Errorf("%v - @%v - %v", i, ci, diff)
break
}
if diff := cmp.Diff(c.exp[ci].Content, cv.Content); diff != "" {
t.Errorf("%v - @%v - %v", i, ci, diff)
break
}
}
}
}
func TestValidHeaders(t *testing.T) {
var allTests = []struct {
input string
name string
comment string
}{
{
`// filename -->`,
"filename",
"",
},
{
`// file.name this is a comment -->`,
"file.name",
"this is a comment",
},
{
`// file/name
// this is a comment line -->`,
"file/name",
"this is a comment line",
},
{
`// file.name this is a comment line 1
// and line 2 -->`,
"file.name",
"this is a comment line 1 and line 2",
},
{
`// file-name.txt this is a comment line 1
// and line 2
//
// line 3
// -->`,
"file-name.txt",
"this is a comment line 1 and line 2\nline 3",
},
{
`//--------------------- file.name.txt
// ------------------------------------------------>`,
"file.name.txt",
"",
},
{
`// file@sys:cfg --> XYZ`,
"file@sys:cfg",
"",
},
}
for _, test := range allTests {
r := NewReader(strings.NewReader(test.input))
c, err := r.ReadChunk()
h := c.Header()
if h.Name != test.name {
t.Errorf("invalid name: %q != %q", h.Name, test.name)
}
if h.Comment != test.comment {
t.Errorf("invalid comment: %q != %q", h.Comment, test.comment)
}
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
}
func TestInvalidHeader(t *testing.T) {
var allTests = []struct {
input string
err error
}{}
for _, test := range allTests {
r := NewReader(strings.NewReader(test.input))
_, err := r.ReadChunk()
if err != test.err {
t.Errorf("invalid error: %v != %v", err, test.err)
}
}
}