Debian Codegraph @

internal/coding/headerext_test.go

raw
1 package coding
2
3 import (
4 "testing"
5 )
6
7 func TestDecodePlainPassthrough(t *testing.T) {
8 var ttable = []string{
9 "Test",
10 "Testing One two 3 4",
11 }
12
13 for _, in := range ttable {
14 t.Run(in, func(t *testing.T) {
15 got := DecodeExtHeader(in)
16 if got != in {
17 t.Errorf("DecodeExtHeader(%q) == %q, want: %q", in, got, in)
18 }
19 })
20 }
21 }
22
23 func TestDecodeFailurePassthrough(t *testing.T) {
24 var ttable = []struct {
25 label, in string
26 }{
27 {
28 label: "Newline detection & abort",
29 in: "=?US\nASCII?Q?Keith_Moore?=",
30 },
31 {
32 label: "Carriage return detection & abort",
33 in: "=?US-ASCII?\r?Keith_Moore?=",
34 },
35 {
36 label: "Invalid termination",
37 in: "=?US-ASCII?Q?Keith_Moore?!",
38 },
39 }
40
41 for _, tt := range ttable {
42 t.Run(tt.in, func(t *testing.T) {
43 got := DecodeExtHeader(tt.in)
44 if got != tt.in {
45 t.Errorf("DecodeExtHeader(%q) == %q, want: %q", tt.in, got, tt.in)
46 }
47 })
48 }
49 }
50
51 func TestDecodeAsciiB64(t *testing.T) {
52 var ttable = []struct {
53 in, want string
54 }{
55 // Simple ASCII quoted-printable encoded word
56 {"=?US-ASCII?B?SGVsbG8gV29ybGQ=?=", "Hello World"},
57 // Abutting a MIME header comment is legal
58 {"(=?US-ASCII?B?SGVsbG8gV29ybGQ=?=)", "(Hello World)"},
59 // The entire header does not need to be encoded
60 {"(Prefix =?US-ASCII?B?SGVsbG8gV29ybGQ=?=)", "(Prefix Hello World)"},
61 }
62
63 for _, tt := range ttable {
64 t.Run(tt.in, func(t *testing.T) {
65 got := DecodeExtHeader(tt.in)
66 if got != tt.want {
67 t.Errorf("DecodeExtHeader(%q) == %q, want: %q", tt.in, got, tt.want)
68 }
69 })
70 }
71 }
72
73 func TestDecodeAsciiQ(t *testing.T) {
74 var ttable = []struct {
75 in, want string
76 }{
77 // Simple ASCII QP encoded word
78 {"=?US-ASCII?Q?Keith_Moore?=", "Keith Moore"},
79 // Abutting a MIME header comment is legal
80 {"(=?US-ASCII?Q?Keith_Moore?=)", "(Keith Moore)"},
81 // The entire header does not need to be encoded
82 {"(Keith =?US-ASCII?Q?Moore?=)", "(Keith Moore)"},
83 }
84
85 for _, tt := range ttable {
86 t.Run(tt.in, func(t *testing.T) {
87 got := DecodeExtHeader(tt.in)
88 if got != tt.want {
89 t.Errorf("DecodeExtHeader(%q) == %q, want: %q", tt.in, got, tt.want)
90 }
91 })
92 }
93 }
94
95 // Spacing rules from RFC 2047
96 func TestDecodeSpacing(t *testing.T) {
97 var ttable = []struct {
98 in, want string
99 }{
100 {"(=?ISO-8859-1?Q?a?=)", "(a)"},
101 {"(=?ISO-8859-1?Q?a?= b)", "(a b)"},
102 {"(=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=)", "(ab)"},
103 {"(=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=)", "(ab)"},
104 {"(=?ISO-8859-1?Q?a?=\r\n =?ISO-8859-1?Q?b?=)", "(ab)"},
105 {"(=?ISO-8859-1?Q?a_b?=)", "(a b)"},
106 {"(=?ISO-8859-1?Q?a?= =?ISO-8859-2?Q?_b?=)", "(a b)"},
107 }
108
109 for _, tt := range ttable {
110 t.Run(tt.in, func(t *testing.T) {
111 got := DecodeExtHeader(tt.in)
112 if got != tt.want {
113 t.Errorf("DecodeExtHeader(%q) == %q, want: %q", tt.in, got, tt.want)
114 }
115 })
116 }
117 }
118
119 // Test some different character sets
120 func TestDecodeCharsets(t *testing.T) {
121 var ttable = []struct {
122 in, want string
123 }{
124 {"=?utf-8?q?abcABC_=24_=c2=a2_=e2=82=ac?=", "abcABC $ \u00a2 \u20ac"},
125 {"=?iso-8859-1?q?#=a3_c=a9_r=ae_u=b5?=", "#\u00a3 c\u00a9 r\u00ae u\u00b5"},
126 {"=?big5?q?=a1=5d_=a1=61_=a1=71?=", "\uff08 \uff5b \u3008"},
127 }
128
129 for _, tt := range ttable {
130 t.Run(tt.in, func(t *testing.T) {
131 got := DecodeExtHeader(tt.in)
132 if got != tt.want {
133 t.Errorf("DecodeExtHeader(%q) == %q, want: %q", tt.in, got, tt.want)
134 }
135 })
136 }
137 }
138
139 func TestRfc2047Decode(t *testing.T) {
140 var ttable = []struct {
141 label, in, want string
142 }{
143 {"pass through", "plain text", "plain text"},
144 {"quoted", "=?US-ASCII?q?Hello=20World?=", "Hello World"},
145 {"base64", "=?US-ASCII?b?SGVsbG8gV29ybGQ=?=", "Hello World"},
146 {"nested qp+b64", "=?utf-8?b?PT9VUy1BU0NJST9xP0hlbGxvPTIwV29ybGQ/PQ==?=", "Hello World"},
147 }
148
149 for _, tt := range ttable {
150 t.Run(tt.label, func(t *testing.T) {
151 got := RFC2047Decode(tt.in)
152 if got != tt.want {
153 t.Errorf("RFC2047Decode(%q) == %q, want: %q", tt.in, got, tt.want)
154 }
155 })
156 }
157 }