...
Run Format

Source file src/mime/quotedprintable/reader_test.go

Documentation: mime/quotedprintable

     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  	everySequence("", "0A \r\n=", 6, func(s string) {
   120  		if strings.HasSuffix(s, "=") || strings.Contains(s, "==") {
   121  			return
   122  		}
   123  		buf.Reset()
   124  		_, err := io.Copy(&buf, NewReader(strings.NewReader(s)))
   125  		if err != nil {
   126  			errStr := err.Error()
   127  			if strings.Contains(errStr, "invalid bytes after =:") {
   128  				errStr = "invalid bytes after ="
   129  			}
   130  			res[errStr]++
   131  			if strings.Contains(errStr, "invalid hex byte ") {
   132  				if strings.HasSuffix(errStr, "0x20") && (strings.Contains(s, "=0 ") || strings.Contains(s, "=A ") || strings.Contains(s, "= ")) {
   133  					return
   134  				}
   135  				if strings.HasSuffix(errStr, "0x3d") && (strings.Contains(s, "=0=") || strings.Contains(s, "=A=")) {
   136  					return
   137  				}
   138  				if strings.HasSuffix(errStr, "0x0a") || strings.HasSuffix(errStr, "0x0d") {
   139  					// bunch of cases; since whitespace at the end of a line before \n is removed.
   140  					return
   141  				}
   142  			}
   143  			if strings.Contains(errStr, "unexpected EOF") {
   144  				return
   145  			}
   146  			if errStr == "invalid bytes after =" && badSoftRx.MatchString(s) {
   147  				return
   148  			}
   149  			t.Errorf("decode(%q) = %v", s, err)
   150  			return
   151  		}
   152  		if *useQprint {
   153  			cmd := exec.Command("qprint", "-d")
   154  			cmd.Stdin = strings.NewReader(s)
   155  			stderr, err := cmd.StderrPipe()
   156  			if err != nil {
   157  				panic(err)
   158  			}
   159  			qpres := make(chan interface{}, 2)
   160  			go func() {
   161  				br := bufio.NewReader(stderr)
   162  				s, _ := br.ReadString('\n')
   163  				if s != "" {
   164  					qpres <- errors.New(s)
   165  					if cmd.Process != nil {
   166  						// It can get stuck on invalid input, like:
   167  						// echo -n "0000= " | qprint -d
   168  						cmd.Process.Kill()
   169  					}
   170  				}
   171  			}()
   172  			go func() {
   173  				want, err := cmd.Output()
   174  				if err == nil {
   175  					qpres <- want
   176  				}
   177  			}()
   178  			select {
   179  			case got := <-qpres:
   180  				if want, ok := got.([]byte); ok {
   181  					if string(want) != buf.String() {
   182  						t.Errorf("go decode(%q) = %q; qprint = %q", s, want, buf.String())
   183  					}
   184  				} else {
   185  					t.Logf("qprint -d(%q) = %v", s, got)
   186  				}
   187  			case <-time.After(5 * time.Second):
   188  				t.Logf("qprint timeout on %q", s)
   189  			}
   190  		}
   191  		res["OK"]++
   192  	})
   193  	var outcomes []string
   194  	for k, v := range res {
   195  		outcomes = append(outcomes, fmt.Sprintf("%v: %d", k, v))
   196  	}
   197  	sort.Strings(outcomes)
   198  	got := strings.Join(outcomes, "\n")
   199  	want := `OK: 28934
   200  invalid bytes after =: 3949
   201  quotedprintable: invalid hex byte 0x0d: 2048
   202  unexpected EOF: 194`
   203  	if got != want {
   204  		t.Errorf("Got:\n%s\nWant:\n%s", got, want)
   205  	}
   206  }
   207  

View as plain text