github.com/c12o16h1/go/src@v0.0.0-20200114212001-5a151c0f00ed/mime/quotedprintable/reader_test.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package quotedprintable 6 7 import ( 8 "bufio" 9 "bytes" 10 "errors" 11 "flag" 12 "fmt" 13 "io" 14 "os/exec" 15 "regexp" 16 "sort" 17 "strings" 18 "testing" 19 "time" 20 ) 21 22 func TestReader(t *testing.T) { 23 tests := []struct { 24 in, want string 25 err interface{} 26 }{ 27 {in: "", want: ""}, 28 {in: "foo bar", want: "foo bar"}, 29 {in: "foo bar=3D", want: "foo bar="}, 30 {in: "foo bar=3d", want: "foo bar="}, // lax. 31 {in: "foo bar=\n", want: "foo bar"}, 32 {in: "foo bar\n", want: "foo bar\n"}, // somewhat lax. 33 {in: "foo bar=0", want: "foo bar=0"}, // lax 34 {in: "foo bar=0D=0A", want: "foo bar\r\n"}, 35 {in: " A B \r\n C ", want: " A B\r\n C"}, 36 {in: " A B =\r\n C ", want: " A B C"}, 37 {in: " A B =\n C ", want: " A B C"}, // lax. treating LF as CRLF 38 {in: "foo=\nbar", want: "foobar"}, 39 {in: "foo\x00bar", want: "foo", err: "quotedprintable: invalid unescaped byte 0x00 in body"}, 40 {in: "foo bar\xff", want: "foo bar\xff"}, 41 42 // Equal sign. 43 {in: "=3D30\n", want: "=30\n"}, 44 {in: "=00=FF0=\n", want: "\x00\xff0"}, 45 46 // Trailing whitespace 47 {in: "foo \n", want: "foo\n"}, 48 {in: "foo \n\nfoo =\n\nfoo=20\n\n", want: "foo\n\nfoo \nfoo \n\n"}, 49 50 // Tests that we allow bare \n and \r through, despite it being strictly 51 // not permitted per RFC 2045, Section 6.7 Page 22 bullet (4). 52 {in: "foo\nbar", want: "foo\nbar"}, 53 {in: "foo\rbar", want: "foo\rbar"}, 54 {in: "foo\r\nbar", want: "foo\r\nbar"}, 55 56 // Different types of soft line-breaks. 57 {in: "foo=\r\nbar", want: "foobar"}, 58 {in: "foo=\nbar", want: "foobar"}, 59 {in: "foo=\rbar", want: "foo", err: "quotedprintable: invalid hex byte 0x0d"}, 60 {in: "foo=\r\r\r \nbar", want: "foo", err: `quotedprintable: invalid bytes after =: "\r\r\r \n"`}, 61 // Issue 15486, accept trailing soft line-break at end of input. 62 {in: "foo=", want: "foo"}, 63 {in: "=", want: "", err: `quotedprintable: invalid bytes after =: ""`}, 64 65 // Example from RFC 2045: 66 {in: "Now's the time =\n" + "for all folk to come=\n" + " to the aid of their country.", 67 want: "Now's the time for all folk to come to the aid of their country."}, 68 {in: "accept UTF-8 right quotation mark: ’", 69 want: "accept UTF-8 right quotation mark: ’"}, 70 } 71 for _, tt := range tests { 72 var buf bytes.Buffer 73 _, err := io.Copy(&buf, NewReader(strings.NewReader(tt.in))) 74 if got := buf.String(); got != tt.want { 75 t.Errorf("for %q, got %q; want %q", tt.in, got, tt.want) 76 } 77 switch verr := tt.err.(type) { 78 case nil: 79 if err != nil { 80 t.Errorf("for %q, got unexpected error: %v", tt.in, err) 81 } 82 case string: 83 if got := fmt.Sprint(err); got != verr { 84 t.Errorf("for %q, got error %q; want %q", tt.in, got, verr) 85 } 86 case error: 87 if err != verr { 88 t.Errorf("for %q, got error %q; want %q", tt.in, err, verr) 89 } 90 } 91 } 92 93 } 94 95 func everySequence(base, alpha string, length int, fn func(string)) { 96 if len(base) == length { 97 fn(base) 98 return 99 } 100 for i := 0; i < len(alpha); i++ { 101 everySequence(base+alpha[i:i+1], alpha, length, fn) 102 } 103 } 104 105 var useQprint = flag.Bool("qprint", false, "Compare against the 'qprint' program.") 106 107 var badSoftRx = regexp.MustCompile(`=([^\r\n]+?\n)|([^\r\n]+$)|(\r$)|(\r[^\n]+\n)|( \r\n)`) 108 109 func TestExhaustive(t *testing.T) { 110 if *useQprint { 111 _, err := exec.LookPath("qprint") 112 if err != nil { 113 t.Fatalf("Error looking for qprint: %v", err) 114 } 115 } 116 117 var buf bytes.Buffer 118 res := make(map[string]int) 119 n := 6 120 if testing.Short() { 121 n = 4 122 } 123 everySequence("", "0A \r\n=", n, func(s string) { 124 if strings.HasSuffix(s, "=") || strings.Contains(s, "==") { 125 return 126 } 127 buf.Reset() 128 _, err := io.Copy(&buf, NewReader(strings.NewReader(s))) 129 if err != nil { 130 errStr := err.Error() 131 if strings.Contains(errStr, "invalid bytes after =:") { 132 errStr = "invalid bytes after =" 133 } 134 res[errStr]++ 135 if strings.Contains(errStr, "invalid hex byte ") { 136 if strings.HasSuffix(errStr, "0x20") && (strings.Contains(s, "=0 ") || strings.Contains(s, "=A ") || strings.Contains(s, "= ")) { 137 return 138 } 139 if strings.HasSuffix(errStr, "0x3d") && (strings.Contains(s, "=0=") || strings.Contains(s, "=A=")) { 140 return 141 } 142 if strings.HasSuffix(errStr, "0x0a") || strings.HasSuffix(errStr, "0x0d") { 143 // bunch of cases; since whitespace at the end of a line before \n is removed. 144 return 145 } 146 } 147 if strings.Contains(errStr, "unexpected EOF") { 148 return 149 } 150 if errStr == "invalid bytes after =" && badSoftRx.MatchString(s) { 151 return 152 } 153 t.Errorf("decode(%q) = %v", s, err) 154 return 155 } 156 if *useQprint { 157 cmd := exec.Command("qprint", "-d") 158 cmd.Stdin = strings.NewReader(s) 159 stderr, err := cmd.StderrPipe() 160 if err != nil { 161 panic(err) 162 } 163 qpres := make(chan interface{}, 2) 164 go func() { 165 br := bufio.NewReader(stderr) 166 s, _ := br.ReadString('\n') 167 if s != "" { 168 qpres <- errors.New(s) 169 if cmd.Process != nil { 170 // It can get stuck on invalid input, like: 171 // echo -n "0000= " | qprint -d 172 cmd.Process.Kill() 173 } 174 } 175 }() 176 go func() { 177 want, err := cmd.Output() 178 if err == nil { 179 qpres <- want 180 } 181 }() 182 select { 183 case got := <-qpres: 184 if want, ok := got.([]byte); ok { 185 if string(want) != buf.String() { 186 t.Errorf("go decode(%q) = %q; qprint = %q", s, want, buf.String()) 187 } 188 } else { 189 t.Logf("qprint -d(%q) = %v", s, got) 190 } 191 case <-time.After(5 * time.Second): 192 t.Logf("qprint timeout on %q", s) 193 } 194 } 195 res["OK"]++ 196 }) 197 var outcomes []string 198 for k, v := range res { 199 outcomes = append(outcomes, fmt.Sprintf("%v: %d", k, v)) 200 } 201 sort.Strings(outcomes) 202 got := strings.Join(outcomes, "\n") 203 want := `OK: 28934 204 invalid bytes after =: 3949 205 quotedprintable: invalid hex byte 0x0d: 2048 206 unexpected EOF: 194` 207 if testing.Short() { 208 want = `OK: 896 209 invalid bytes after =: 100 210 quotedprintable: invalid hex byte 0x0d: 26 211 unexpected EOF: 3` 212 } 213 214 if got != want { 215 t.Errorf("Got:\n%s\nWant:\n%s", got, want) 216 } 217 }