1 package quotedprintable
2
3 import (
4 "bufio"
5 "bytes"
6 "errors"
7 "flag"
8 "fmt"
9 "io"
10 "os/exec"
11 "regexp"
12 "sort"
13 "strings"
14 "testing"
15 "time"
16 )
17
18 func TestReader(t *testing.T) {
19 tests := []struct {
20 in, want string
21 err interface{}
22 }{
23 {in: "", want: ""},
24 {in: "foo bar", want: "foo bar"},
25 {in: "foo bar=3D", want: "foo bar="},
26 {in: "foo bar=3d", want: "foo bar="},
27 {in: "foo bar=\n", want: "foo bar"},
28 {in: "foo bar\n", want: "foo bar\n"},
29 {in: "foo bar=0", want: "foo bar", err: io.ErrUnexpectedEOF},
30 {in: "foo bar=0D=0A", want: "foo bar\r\n"},
31 {in: " A B \r\n C ", want: " A B\r\n C"},
32 {in: " A B =\r\n C ", want: " A B C"},
33 {in: " A B =\n C ", want: " A B C"},
34 {in: "foo=\nbar", want: "foobar"},
35 {in: "foo\x00bar", want: "foo", err: "quotedprintable: invalid unescaped byte 0x00 in body"},
36 {in: "foo bar\xff", want: "foo bar", err: "quotedprintable: invalid unescaped byte 0xff in body"},
37
38
39 {in: "=3D30\n", want: "=30\n"},
40 {in: "=00=FF0=\n", want: "\x00\xff0"},
41
42
43 {in: "foo \n", want: "foo\n"},
44 {in: "foo \n\nfoo =\n\nfoo=20\n\n", want: "foo\n\nfoo \nfoo \n\n"},
45
46
47
48 {in: "foo\nbar", want: "foo\nbar"},
49 {in: "foo\rbar", want: "foo\rbar"},
50 {in: "foo\r\nbar", want: "foo\r\nbar"},
51
52
53 {in: "foo=\r\nbar", want: "foobar"},
54 {in: "foo=\nbar", want: "foobar"},
55 {in: "foo=\rbar", want: "foo", err: "quotedprintable: invalid hex byte 0x0d"},
56 {in: "foo=\r\r\r \nbar", want: "foo", err: `quotedprintable: invalid bytes after =: "\r\r\r \n"`},
57
58
59 {in: "Now's the time =\n" + "for all folk to come=\n" + " to the aid of their country.",
60 want: "Now's the time for all folk to come to the aid of their country."},
61 }
62 for _, tt := range tests {
63 var buf bytes.Buffer
64 _, err := io.Copy(&buf, NewReader(strings.NewReader(tt.in)))
65 if got := buf.String(); got != tt.want {
66 t.Errorf("for %q, got %q; want %q", tt.in, got, tt.want)
67 }
68 switch verr := tt.err.(type) {
69 case nil:
70 if err != nil {
71 t.Errorf("for %q, got unexpected error: %v", tt.in, err)
72 }
73 case string:
74 if got := fmt.Sprint(err); got != verr {
75 t.Errorf("for %q, got error %q; want %q", tt.in, got, verr)
76 }
77 case error:
78 if err != verr {
79 t.Errorf("for %q, got error %q; want %q", tt.in, err, verr)
80 }
81 }
82 }
83
84 }
85
86 func everySequence(base, alpha string, length int, fn func(string)) {
87 if len(base) == length {
88 fn(base)
89 return
90 }
91 for i := 0; i < len(alpha); i++ {
92 everySequence(base+alpha[i:i+1], alpha, length, fn)
93 }
94 }
95
96 var useQprint = flag.Bool("qprint", false, "Compare against the 'qprint' program.")
97
98 var badSoftRx = regexp.MustCompile(`=([^\r\n]+?\n)|([^\r\n]+$)|(\r$)|(\r[^\n]+\n)|( \r\n)`)
99
100 func TestExhaustive(t *testing.T) {
101 if *useQprint {
102 _, err := exec.LookPath("qprint")
103 if err != nil {
104 t.Fatalf("Error looking for qprint: %v", err)
105 }
106 }
107
108 var buf bytes.Buffer
109 res := make(map[string]int)
110 everySequence("", "0A \r\n=", 6, func(s string) {
111 if strings.HasSuffix(s, "=") || strings.Contains(s, "==") {
112 return
113 }
114 buf.Reset()
115 _, err := io.Copy(&buf, NewReader(strings.NewReader(s)))
116 if err != nil {
117 errStr := err.Error()
118 if strings.Contains(errStr, "invalid bytes after =:") {
119 errStr = "invalid bytes after ="
120 }
121 res[errStr]++
122 if strings.Contains(errStr, "invalid hex byte ") {
123 if strings.HasSuffix(errStr, "0x20") && (strings.Contains(s, "=0 ") || strings.Contains(s, "=A ") || strings.Contains(s, "= ")) {
124 return
125 }
126 if strings.HasSuffix(errStr, "0x3d") && (strings.Contains(s, "=0=") || strings.Contains(s, "=A=")) {
127 return
128 }
129 if strings.HasSuffix(errStr, "0x0a") || strings.HasSuffix(errStr, "0x0d") {
130
131 return
132 }
133 }
134 if strings.Contains(errStr, "unexpected EOF") {
135 return
136 }
137 if errStr == "invalid bytes after =" && badSoftRx.MatchString(s) {
138 return
139 }
140 t.Errorf("decode(%q) = %v", s, err)
141 return
142 }
143 if *useQprint {
144 cmd := exec.Command("qprint", "-d")
145 cmd.Stdin = strings.NewReader(s)
146 stderr, err := cmd.StderrPipe()
147 if err != nil {
148 panic(err)
149 }
150 qpres := make(chan interface{}, 2)
151 go func() {
152 br := bufio.NewReader(stderr)
153 s, _ := br.ReadString('\n')
154 if s != "" {
155 qpres <- errors.New(s)
156 if cmd.Process != nil {
157
158
159 cmd.Process.Kill()
160 }
161 }
162 }()
163 go func() {
164 want, err := cmd.Output()
165 if err == nil {
166 qpres <- want
167 }
168 }()
169 select {
170 case got := <-qpres:
171 if want, ok := got.([]byte); ok {
172 if string(want) != buf.String() {
173 t.Errorf("go decode(%q) = %q; qprint = %q", s, want, buf.String())
174 }
175 } else {
176 t.Logf("qprint -d(%q) = %v", s, got)
177 }
178 case <-time.After(5 * time.Second):
179 t.Logf("qprint timeout on %q", s)
180 }
181 }
182 res["OK"]++
183 })
184 var outcomes []string
185 for k, v := range res {
186 outcomes = append(outcomes, fmt.Sprintf("%v: %d", k, v))
187 }
188 sort.Strings(outcomes)
189 got := strings.Join(outcomes, "\n")
190 want := `OK: 21576
191 invalid bytes after =: 3397
192 quotedprintable: invalid hex byte 0x0a: 1400
193 quotedprintable: invalid hex byte 0x0d: 2700
194 quotedprintable: invalid hex byte 0x20: 2490
195 quotedprintable: invalid hex byte 0x3d: 440
196 unexpected EOF: 3122`
197 if got != want {
198 t.Errorf("Got:\n%s\nWant:\n%s", got, want)
199 }
200 }
201
View as plain text