File: headerext_test.go

package info (click to toggle)
golang-github-jhillyerd-enmime 0.9.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,156 kB
  • sloc: makefile: 25; sh: 16
file content (157 lines) | stat: -rw-r--r-- 3,926 bytes parent folder | download | duplicates (2)
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
package coding

import (
	"testing"
)

func TestDecodePlainPassthrough(t *testing.T) {
	var ttable = []string{
		"Test",
		"Testing One two 3 4",
	}

	for _, in := range ttable {
		t.Run(in, func(t *testing.T) {
			got := DecodeExtHeader(in)
			if got != in {
				t.Errorf("DecodeExtHeader(%q) == %q, want: %q", in, got, in)
			}
		})
	}
}

func TestDecodeFailurePassthrough(t *testing.T) {
	var ttable = []struct {
		label, in string
	}{
		{
			label: "Newline detection & abort",
			in:    "=?US\nASCII?Q?Keith_Moore?=",
		},
		{
			label: "Carriage return detection & abort",
			in:    "=?US-ASCII?\r?Keith_Moore?=",
		},
		{
			label: "Invalid termination",
			in:    "=?US-ASCII?Q?Keith_Moore?!",
		},
	}

	for _, tt := range ttable {
		t.Run(tt.in, func(t *testing.T) {
			got := DecodeExtHeader(tt.in)
			if got != tt.in {
				t.Errorf("DecodeExtHeader(%q) == %q, want: %q", tt.in, got, tt.in)
			}
		})
	}
}

func TestDecodeAsciiB64(t *testing.T) {
	var ttable = []struct {
		in, want string
	}{
		// Simple ASCII quoted-printable encoded word
		{"=?US-ASCII?B?SGVsbG8gV29ybGQ=?=", "Hello World"},
		// Abutting a MIME header comment is legal
		{"(=?US-ASCII?B?SGVsbG8gV29ybGQ=?=)", "(Hello World)"},
		// The entire header does not need to be encoded
		{"(Prefix =?US-ASCII?B?SGVsbG8gV29ybGQ=?=)", "(Prefix Hello World)"},
	}

	for _, tt := range ttable {
		t.Run(tt.in, func(t *testing.T) {
			got := DecodeExtHeader(tt.in)
			if got != tt.want {
				t.Errorf("DecodeExtHeader(%q) == %q, want: %q", tt.in, got, tt.want)
			}
		})
	}
}

func TestDecodeAsciiQ(t *testing.T) {
	var ttable = []struct {
		in, want string
	}{
		// Simple ASCII QP encoded word
		{"=?US-ASCII?Q?Keith_Moore?=", "Keith Moore"},
		// Abutting a MIME header comment is legal
		{"(=?US-ASCII?Q?Keith_Moore?=)", "(Keith Moore)"},
		// The entire header does not need to be encoded
		{"(Keith =?US-ASCII?Q?Moore?=)", "(Keith Moore)"},
	}

	for _, tt := range ttable {
		t.Run(tt.in, func(t *testing.T) {
			got := DecodeExtHeader(tt.in)
			if got != tt.want {
				t.Errorf("DecodeExtHeader(%q) == %q, want: %q", tt.in, got, tt.want)
			}
		})
	}
}

// Spacing rules from RFC 2047
func TestDecodeSpacing(t *testing.T) {
	var ttable = []struct {
		in, want string
	}{
		{"(=?ISO-8859-1?Q?a?=)", "(a)"},
		{"(=?ISO-8859-1?Q?a?= b)", "(a b)"},
		{"(=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=)", "(ab)"},
		{"(=?ISO-8859-1?Q?a?=  =?ISO-8859-1?Q?b?=)", "(ab)"},
		{"(=?ISO-8859-1?Q?a?=\r\n  =?ISO-8859-1?Q?b?=)", "(ab)"},
		{"(=?ISO-8859-1?Q?a_b?=)", "(a b)"},
		{"(=?ISO-8859-1?Q?a?= =?ISO-8859-2?Q?_b?=)", "(a b)"},
	}

	for _, tt := range ttable {
		t.Run(tt.in, func(t *testing.T) {
			got := DecodeExtHeader(tt.in)
			if got != tt.want {
				t.Errorf("DecodeExtHeader(%q) == %q, want: %q", tt.in, got, tt.want)
			}
		})
	}
}

// Test some different character sets
func TestDecodeCharsets(t *testing.T) {
	var ttable = []struct {
		in, want string
	}{
		{"=?utf-8?q?abcABC_=24_=c2=a2_=e2=82=ac?=", "abcABC $ \u00a2 \u20ac"},
		{"=?iso-8859-1?q?#=a3_c=a9_r=ae_u=b5?=", "#\u00a3 c\u00a9 r\u00ae u\u00b5"},
		{"=?big5?q?=a1=5d_=a1=61_=a1=71?=", "\uff08 \uff5b \u3008"},
	}

	for _, tt := range ttable {
		t.Run(tt.in, func(t *testing.T) {
			got := DecodeExtHeader(tt.in)
			if got != tt.want {
				t.Errorf("DecodeExtHeader(%q) == %q, want: %q", tt.in, got, tt.want)
			}
		})
	}
}

func TestRfc2047Decode(t *testing.T) {
	var ttable = []struct {
		label, in, want string
	}{
		{"pass through", "plain text", "plain text"},
		{"quoted", "=?US-ASCII?q?Hello=20World?=", "Hello World"},
		{"base64", "=?US-ASCII?b?SGVsbG8gV29ybGQ=?=", "Hello World"},
		{"nested qp+b64", "=?utf-8?b?PT9VUy1BU0NJST9xP0hlbGxvPTIwV29ybGQ/PQ==?=", "Hello World"},
	}

	for _, tt := range ttable {
		t.Run(tt.label, func(t *testing.T) {
			got := RFC2047Decode(tt.in)
			if got != tt.want {
				t.Errorf("RFC2047Decode(%q) == %q, want: %q", tt.in, got, tt.want)
			}
		})
	}
}