github.com/likebike/go--@v0.0.0-20190911215757-0bd925d16e96/go/src/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", err: "quotedprintable: invalid unescaped byte 0xff in body"}, 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 } 69 for _, tt := range tests { 70 var buf bytes.Buffer 71 _, err := io.Copy(&buf, NewReader(strings.NewReader(tt.in))) 72 if got := buf.String(); got != tt.want { 73 t.Errorf("for %q, got %q; want %q", tt.in, got, tt.want) 74 } 75 switch verr := tt.err.(type) { 76 case nil: 77 if err != nil { 78 t.Errorf("for %q, got unexpected error: %v", tt.in, err) 79 } 80 case string: 81 if got := fmt.Sprint(err); got != verr { 82 t.Errorf("for %q, got error %q; want %q", tt.in, got, verr) 83 } 84 case error: 85 if err != verr { 86 t.Errorf("for %q, got error %q; want %q", tt.in, err, verr) 87 } 88 } 89 } 90 91 } 92 93 func everySequence(base, alpha string, length int, fn func(string)) { 94 if len(base) == length { 95 fn(base) 96 return 97 } 98 for i := 0; i < len(alpha); i++ { 99 everySequence(base+alpha[i:i+1], alpha, length, fn) 100 } 101 } 102 103 var useQprint = flag.Bool("qprint", false, "Compare against the 'qprint' program.") 104 105 var badSoftRx = regexp.MustCompile(`=([^\r\n]+?\n)|([^\r\n]+$)|(\r$)|(\r[^\n]+\n)|( \r\n)`) 106 107 func TestExhaustive(t *testing.T) { 108 if *useQprint { 109 _, err := exec.LookPath("qprint") 110 if err != nil { 111 t.Fatalf("Error looking for qprint: %v", err) 112 } 113 } 114 115 var buf bytes.Buffer 116 res := make(map[string]int) 117 everySequence("", "0A \r\n=", 6, func(s string) { 118 if strings.HasSuffix(s, "=") || strings.Contains(s, "==") { 119 return 120 } 121 buf.Reset() 122 _, err := io.Copy(&buf, NewReader(strings.NewReader(s))) 123 if err != nil { 124 errStr := err.Error() 125 if strings.Contains(errStr, "invalid bytes after =:") { 126 errStr = "invalid bytes after =" 127 } 128 res[errStr]++ 129 if strings.Contains(errStr, "invalid hex byte ") { 130 if strings.HasSuffix(errStr, "0x20") && (strings.Contains(s, "=0 ") || strings.Contains(s, "=A ") || strings.Contains(s, "= ")) { 131 return 132 } 133 if strings.HasSuffix(errStr, "0x3d") && (strings.Contains(s, "=0=") || strings.Contains(s, "=A=")) { 134 return 135 } 136 if strings.HasSuffix(errStr, "0x0a") || strings.HasSuffix(errStr, "0x0d") { 137 // bunch of cases; since whitespace at the end of a line before \n is removed. 138 return 139 } 140 } 141 if strings.Contains(errStr, "unexpected EOF") { 142 return 143 } 144 if errStr == "invalid bytes after =" && badSoftRx.MatchString(s) { 145 return 146 } 147 t.Errorf("decode(%q) = %v", s, err) 148 return 149 } 150 if *useQprint { 151 cmd := exec.Command("qprint", "-d") 152 cmd.Stdin = strings.NewReader(s) 153 stderr, err := cmd.StderrPipe() 154 if err != nil { 155 panic(err) 156 } 157 qpres := make(chan interface{}, 2) 158 go func() { 159 br := bufio.NewReader(stderr) 160 s, _ := br.ReadString('\n') 161 if s != "" { 162 qpres <- errors.New(s) 163 if cmd.Process != nil { 164 // It can get stuck on invalid input, like: 165 // echo -n "0000= " | qprint -d 166 cmd.Process.Kill() 167 } 168 } 169 }() 170 go func() { 171 want, err := cmd.Output() 172 if err == nil { 173 qpres <- want 174 } 175 }() 176 select { 177 case got := <-qpres: 178 if want, ok := got.([]byte); ok { 179 if string(want) != buf.String() { 180 t.Errorf("go decode(%q) = %q; qprint = %q", s, want, buf.String()) 181 } 182 } else { 183 t.Logf("qprint -d(%q) = %v", s, got) 184 } 185 case <-time.After(5 * time.Second): 186 t.Logf("qprint timeout on %q", s) 187 } 188 } 189 res["OK"]++ 190 }) 191 var outcomes []string 192 for k, v := range res { 193 outcomes = append(outcomes, fmt.Sprintf("%v: %d", k, v)) 194 } 195 sort.Strings(outcomes) 196 got := strings.Join(outcomes, "\n") 197 want := `OK: 28934 198 invalid bytes after =: 3949 199 quotedprintable: invalid hex byte 0x0d: 2048 200 unexpected EOF: 194` 201 if got != want { 202 t.Errorf("Got:\n%s\nWant:\n%s", got, want) 203 } 204 }