Source file src/mime/quotedprintable/reader_test.go

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

View as plain text