github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/mime/encodedword_test.go (about)

     1  // Copyright 2015 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 mime
     6  
     7  import (
     8  	"errors"
     9  	"io"
    10  	"io/ioutil"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  func TestEncodeWord(t *testing.T) {
    16  	utf8, iso88591 := "utf-8", "iso-8859-1"
    17  	tests := []struct {
    18  		enc      WordEncoder
    19  		charset  string
    20  		src, exp string
    21  	}{
    22  		{QEncoding, utf8, "François-Jérôme", "=?utf-8?q?Fran=C3=A7ois-J=C3=A9r=C3=B4me?="},
    23  		{BEncoding, utf8, "Café", "=?utf-8?b?Q2Fmw6k=?="},
    24  		{QEncoding, iso88591, "La Seleção", "=?iso-8859-1?q?La_Sele=C3=A7=C3=A3o?="},
    25  		{QEncoding, utf8, "", ""},
    26  		{QEncoding, utf8, "A", "A"},
    27  		{QEncoding, iso88591, "a", "a"},
    28  		{QEncoding, utf8, "123 456", "123 456"},
    29  		{QEncoding, utf8, "\t !\"#$%&'()*+,-./ :;<>?@[\\]^_`{|}~", "\t !\"#$%&'()*+,-./ :;<>?@[\\]^_`{|}~"},
    30  		{QEncoding, utf8, strings.Repeat("é", 10), "=?utf-8?q?" + strings.Repeat("=C3=A9", 10) + "?="},
    31  		{QEncoding, utf8, strings.Repeat("é", 11), "=?utf-8?q?" + strings.Repeat("=C3=A9", 10) + "?= =?utf-8?q?=C3=A9?="},
    32  		{QEncoding, iso88591, strings.Repeat("\xe9", 22), "=?iso-8859-1?q?" + strings.Repeat("=E9", 22) + "?="},
    33  		{QEncoding, utf8, strings.Repeat("\x80", 22), "=?utf-8?q?" + strings.Repeat("=80", 21) + "?= =?utf-8?q?=80?="},
    34  		{BEncoding, utf8, strings.Repeat("é", 24), "=?utf-8?b?" + strings.Repeat("w6nDqcOp", 8) + "?="},
    35  		{BEncoding, utf8, strings.Repeat("é", 27), "=?utf-8?b?" + strings.Repeat("w6nDqcOp", 8) + "?= =?utf-8?b?w6nDqcOp?="},
    36  		{BEncoding, iso88591, strings.Repeat("\xe9", 45), "=?iso-8859-1?b?" + strings.Repeat("6enp", 15) + "?="},
    37  		{BEncoding, utf8, strings.Repeat("\x80", 51), "=?utf-8?b?" + strings.Repeat("gICA", 16) + "?= =?utf-8?b?gICA?="},
    38  	}
    39  
    40  	for _, test := range tests {
    41  		if s := test.enc.Encode(test.charset, test.src); s != test.exp {
    42  			t.Errorf("Encode(%q) = %q, want %q", test.src, s, test.exp)
    43  		}
    44  	}
    45  }
    46  
    47  func TestDecodeWord(t *testing.T) {
    48  	tests := []struct {
    49  		src, exp string
    50  		hasErr   bool
    51  	}{
    52  		{"=?UTF-8?Q?=C2=A1Hola,_se=C3=B1or!?=", "¡Hola, señor!", false},
    53  		{"=?UTF-8?Q?Fran=C3=A7ois-J=C3=A9r=C3=B4me?=", "François-Jérôme", false},
    54  		{"=?UTF-8?q?ascii?=", "ascii", false},
    55  		{"=?utf-8?B?QW5kcsOp?=", "André", false},
    56  		{"=?ISO-8859-1?Q?Rapha=EBl_Dupont?=", "Raphaël Dupont", false},
    57  		{"=?utf-8?b?IkFudG9uaW8gSm9zw6kiIDxqb3NlQGV4YW1wbGUub3JnPg==?=", `"Antonio José" <jose@example.org>`, false},
    58  		{"=?UTF-8?A?Test?=", "", true},
    59  		{"=?UTF-8?Q?A=B?=", "", true},
    60  		{"=?UTF-8?Q?=A?=", "", true},
    61  		{"=?UTF-8?A?A?=", "", true},
    62  	}
    63  
    64  	for _, test := range tests {
    65  		dec := new(WordDecoder)
    66  		s, err := dec.Decode(test.src)
    67  		if test.hasErr && err == nil {
    68  			t.Errorf("Decode(%q) should return an error", test.src)
    69  			continue
    70  		}
    71  		if !test.hasErr && err != nil {
    72  			t.Errorf("Decode(%q): %v", test.src, err)
    73  			continue
    74  		}
    75  		if s != test.exp {
    76  			t.Errorf("Decode(%q) = %q, want %q", test.src, s, test.exp)
    77  		}
    78  	}
    79  }
    80  
    81  func TestDecodeHeader(t *testing.T) {
    82  	tests := []struct {
    83  		src, exp string
    84  	}{
    85  		{"=?UTF-8?Q?=C2=A1Hola,_se=C3=B1or!?=", "¡Hola, señor!"},
    86  		{"=?UTF-8?Q?Fran=C3=A7ois-J=C3=A9r=C3=B4me?=", "François-Jérôme"},
    87  		{"=?UTF-8?q?ascii?=", "ascii"},
    88  		{"=?utf-8?B?QW5kcsOp?=", "André"},
    89  		{"=?ISO-8859-1?Q?Rapha=EBl_Dupont?=", "Raphaël Dupont"},
    90  		{"Jean", "Jean"},
    91  		{"=?utf-8?b?IkFudG9uaW8gSm9zw6kiIDxqb3NlQGV4YW1wbGUub3JnPg==?=", `"Antonio José" <jose@example.org>`},
    92  		{"=?UTF-8?A?Test?=", "=?UTF-8?A?Test?="},
    93  		{"=?UTF-8?Q?A=B?=", "=?UTF-8?Q?A=B?="},
    94  		{"=?UTF-8?Q?=A?=", "=?UTF-8?Q?=A?="},
    95  		{"=?UTF-8?A?A?=", "=?UTF-8?A?A?="},
    96  		// Incomplete words
    97  		{"=?", "=?"},
    98  		{"=?UTF-8?", "=?UTF-8?"},
    99  		{"=?UTF-8?=", "=?UTF-8?="},
   100  		{"=?UTF-8?Q", "=?UTF-8?Q"},
   101  		{"=?UTF-8?Q?", "=?UTF-8?Q?"},
   102  		{"=?UTF-8?Q?=", "=?UTF-8?Q?="},
   103  		{"=?UTF-8?Q?A", "=?UTF-8?Q?A"},
   104  		{"=?UTF-8?Q?A?", "=?UTF-8?Q?A?"},
   105  		// Tests from RFC 2047
   106  		{"=?ISO-8859-1?Q?a?=", "a"},
   107  		{"=?ISO-8859-1?Q?a?= b", "a b"},
   108  		{"=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=", "ab"},
   109  		{"=?ISO-8859-1?Q?a?=  =?ISO-8859-1?Q?b?=", "ab"},
   110  		{"=?ISO-8859-1?Q?a?= \r\n\t =?ISO-8859-1?Q?b?=", "ab"},
   111  		{"=?ISO-8859-1?Q?a_b?=", "a b"},
   112  	}
   113  
   114  	for _, test := range tests {
   115  		dec := new(WordDecoder)
   116  		s, err := dec.DecodeHeader(test.src)
   117  		if err != nil {
   118  			t.Errorf("DecodeHeader(%q): %v", test.src, err)
   119  		}
   120  		if s != test.exp {
   121  			t.Errorf("DecodeHeader(%q) = %q, want %q", test.src, s, test.exp)
   122  		}
   123  	}
   124  }
   125  
   126  func TestCharsetDecoder(t *testing.T) {
   127  	tests := []struct {
   128  		src      string
   129  		want     string
   130  		charsets []string
   131  		content  []string
   132  	}{
   133  		{"=?utf-8?b?Q2Fmw6k=?=", "Café", nil, nil},
   134  		{"=?ISO-8859-1?Q?caf=E9?=", "café", nil, nil},
   135  		{"=?US-ASCII?Q?foo_bar?=", "foo bar", nil, nil},
   136  		{"=?utf-8?Q?=?=", "=?utf-8?Q?=?=", nil, nil},
   137  		{"=?utf-8?Q?=A?=", "=?utf-8?Q?=A?=", nil, nil},
   138  		{
   139  			"=?ISO-8859-15?Q?f=F5=F6?=  =?windows-1252?Q?b=E0r?=",
   140  			"f\xf5\xf6b\xe0r",
   141  			[]string{"iso-8859-15", "windows-1252"},
   142  			[]string{"f\xf5\xf6", "b\xe0r"},
   143  		},
   144  	}
   145  
   146  	for _, test := range tests {
   147  		i := 0
   148  		dec := &WordDecoder{
   149  			CharsetReader: func(charset string, input io.Reader) (io.Reader, error) {
   150  				if charset != test.charsets[i] {
   151  					t.Errorf("DecodeHeader(%q), got charset %q, want %q", test.src, charset, test.charsets[i])
   152  				}
   153  				content, err := ioutil.ReadAll(input)
   154  				if err != nil {
   155  					t.Errorf("DecodeHeader(%q), error in reader: %v", test.src, err)
   156  				}
   157  				got := string(content)
   158  				if got != test.content[i] {
   159  					t.Errorf("DecodeHeader(%q), got content %q, want %q", test.src, got, test.content[i])
   160  				}
   161  				i++
   162  
   163  				return strings.NewReader(got), nil
   164  			},
   165  		}
   166  		got, err := dec.DecodeHeader(test.src)
   167  		if err != nil {
   168  			t.Errorf("DecodeHeader(%q): %v", test.src, err)
   169  		}
   170  		if got != test.want {
   171  			t.Errorf("DecodeHeader(%q) = %q, want %q", test.src, got, test.want)
   172  		}
   173  	}
   174  }
   175  
   176  func TestCharsetDecoderError(t *testing.T) {
   177  	dec := &WordDecoder{
   178  		CharsetReader: func(charset string, input io.Reader) (io.Reader, error) {
   179  			return nil, errors.New("Test error")
   180  		},
   181  	}
   182  
   183  	if _, err := dec.DecodeHeader("=?charset?Q?foo?="); err == nil {
   184  		t.Error("DecodeHeader should return an error")
   185  	}
   186  }
   187  
   188  func BenchmarkQEncodeWord(b *testing.B) {
   189  	for i := 0; i < b.N; i++ {
   190  		QEncoding.Encode("UTF-8", "¡Hola, señor!")
   191  	}
   192  }
   193  
   194  func BenchmarkQDecodeWord(b *testing.B) {
   195  	dec := new(WordDecoder)
   196  
   197  	for i := 0; i < b.N; i++ {
   198  		dec.Decode("=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=")
   199  	}
   200  }
   201  
   202  func BenchmarkQDecodeHeader(b *testing.B) {
   203  	dec := new(WordDecoder)
   204  
   205  	for i := 0; i < b.N; i++ {
   206  		dec.Decode("=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=")
   207  	}
   208  }