aboutsummaryrefslogtreecommitdiff
path: root/src/mime/quotedprintable/writer_test.go
blob: d494c1e3dc35c8964b895b0daab19dd5983979ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package quotedprintable

import (
	"bytes"
	"io/ioutil"
	"strings"
	"testing"
)

func TestWriter(t *testing.T) {
	testWriter(t, false)
}

func TestWriterBinary(t *testing.T) {
	testWriter(t, true)
}

func testWriter(t *testing.T, binary bool) {
	tests := []struct {
		in, want, wantB string
	}{
		{in: "", want: ""},
		{in: "foo bar", want: "foo bar"},
		{in: "foo bar=", want: "foo bar=3D"},
		{in: "foo bar\r", want: "foo bar\r\n", wantB: "foo bar=0D"},
		{in: "foo bar\r\r", want: "foo bar\r\n\r\n", wantB: "foo bar=0D=0D"},
		{in: "foo bar\n", want: "foo bar\r\n", wantB: "foo bar=0A"},
		{in: "foo bar\r\n", want: "foo bar\r\n", wantB: "foo bar=0D=0A"},
		{in: "foo bar\r\r\n", want: "foo bar\r\n\r\n", wantB: "foo bar=0D=0D=0A"},
		{in: "foo bar ", want: "foo bar=20"},
		{in: "foo bar\t", want: "foo bar=09"},
		{in: "foo bar  ", want: "foo bar =20"},
		{in: "foo bar \n", want: "foo bar=20\r\n", wantB: "foo bar =0A"},
		{in: "foo bar \r", want: "foo bar=20\r\n", wantB: "foo bar =0D"},
		{in: "foo bar \r\n", want: "foo bar=20\r\n", wantB: "foo bar =0D=0A"},
		{in: "foo bar  \n", want: "foo bar =20\r\n", wantB: "foo bar  =0A"},
		{in: "foo bar  \n ", want: "foo bar =20\r\n=20", wantB: "foo bar  =0A=20"},
		{in: "¡Hola Señor!", want: "=C2=A1Hola Se=C3=B1or!"},
		{
			in:   "\t !\"#$%&'()*+,-./ :;<>?@[\\]^_`{|}~",
			want: "\t !\"#$%&'()*+,-./ :;<>?@[\\]^_`{|}~",
		},
		{
			in:   strings.Repeat("a", 75),
			want: strings.Repeat("a", 75),
		},
		{
			in:   strings.Repeat("a", 76),
			want: strings.Repeat("a", 75) + "=\r\na",
		},
		{
			in:   strings.Repeat("a", 72) + "=",
			want: strings.Repeat("a", 72) + "=3D",
		},
		{
			in:   strings.Repeat("a", 73) + "=",
			want: strings.Repeat("a", 73) + "=\r\n=3D",
		},
		{
			in:   strings.Repeat("a", 74) + "=",
			want: strings.Repeat("a", 74) + "=\r\n=3D",
		},
		{
			in:   strings.Repeat("a", 75) + "=",
			want: strings.Repeat("a", 75) + "=\r\n=3D",
		},
		{
			in:   strings.Repeat(" ", 73),
			want: strings.Repeat(" ", 72) + "=20",
		},
		{
			in:   strings.Repeat(" ", 74),
			want: strings.Repeat(" ", 73) + "=\r\n=20",
		},
		{
			in:   strings.Repeat(" ", 75),
			want: strings.Repeat(" ", 74) + "=\r\n=20",
		},
		{
			in:   strings.Repeat(" ", 76),
			want: strings.Repeat(" ", 75) + "=\r\n=20",
		},
		{
			in:   strings.Repeat(" ", 77),
			want: strings.Repeat(" ", 75) + "=\r\n =20",
		},
	}

	for _, tt := range tests {
		buf := new(bytes.Buffer)
		w := NewWriter(buf)

		want := tt.want
		if binary {
			w.Binary = true
			if tt.wantB != "" {
				want = tt.wantB
			}
		}

		if _, err := w.Write([]byte(tt.in)); err != nil {
			t.Errorf("Write(%q): %v", tt.in, err)
			continue
		}
		if err := w.Close(); err != nil {
			t.Errorf("Close(): %v", err)
			continue
		}
		got := buf.String()
		if got != want {
			t.Errorf("Write(%q), got:\n%q\nwant:\n%q", tt.in, got, want)
		}
	}
}

func TestRoundTrip(t *testing.T) {
	buf := new(bytes.Buffer)
	w := NewWriter(buf)
	if _, err := w.Write(testMsg); err != nil {
		t.Fatalf("Write: %v", err)
	}
	if err := w.Close(); err != nil {
		t.Fatalf("Close: %v", err)
	}

	r := NewReader(buf)
	gotBytes, err := ioutil.ReadAll(r)
	if err != nil {
		t.Fatalf("Error while reading from Reader: %v", err)
	}
	got := string(gotBytes)
	if got != string(testMsg) {
		t.Errorf("Encoding and decoding changed the message, got:\n%s", got)
	}
}

// From https://fr.wikipedia.org/wiki/Quoted-Printable
var testMsg = []byte("Quoted-Printable (QP) est un format d'encodage de données codées sur 8 bits, qui utilise exclusivement les caractères alphanumériques imprimables du code ASCII (7 bits).\r\n" +
	"\r\n" +
	"En effet, les différents codages comprennent de nombreux caractères qui ne sont pas représentables en ASCII (par exemple les caractères accentués), ainsi que des caractères dits « non-imprimables ».\r\n" +
	"\r\n" +
	"L'encodage Quoted-Printable permet de remédier à ce problème, en procédant de la manière suivante :\r\n" +
	"\r\n" +
	"Un octet correspondant à un caractère imprimable de l'ASCII sauf le signe égal (donc un caractère de code ASCII entre 33 et 60 ou entre 62 et 126) ou aux caractères de saut de ligne (codes ASCII 13 et 10) ou une suite de tabulations et espaces non situées en fin de ligne (de codes ASCII respectifs 9 et 32) est représenté tel quel.\r\n" +
	"Un octet qui ne correspond pas à la définition ci-dessus (caractère non imprimable de l'ASCII, tabulation ou espaces non suivies d'un caractère imprimable avant la fin de la ligne ou signe égal) est représenté par un signe égal, suivi de son numéro, exprimé en hexadécimal.\r\n" +
	"Enfin, un signe égal suivi par un saut de ligne (donc la suite des trois caractères de codes ASCII 61, 13 et 10) peut être inséré n'importe où, afin de limiter la taille des lignes produites si nécessaire. Une limite de 76 caractères par ligne est généralement respectée.\r\n")

func BenchmarkWriter(b *testing.B) {
	for i := 0; i < b.N; i++ {
		w := NewWriter(ioutil.Discard)
		w.Write(testMsg)
		w.Close()
	}
}