9func TestUTF7(t *testing.T) {
10 check := func(input string, output string, expErr error) {
13 r, err := utf7decode(input)
15 t.Fatalf("got %q, expected %q (err %v), for input %q", r, output, err, input)
17 if (expErr == nil) != (err == nil) || err != nil && !errors.Is(err, expErr) {
18 t.Fatalf("got err %v, expected %v", err, expErr)
21 expInput := utf7encode(output)
22 if expInput != input {
23 t.Fatalf("encoding, got %s, expected %s", expInput, input)
28 check("plain", "plain", nil)
29 check("&Jjo-", "☺", nil)
30 check("test&Jjo-", "test☺", nil)
31 check("&Jjo-test&Jjo-", "☺test☺", nil)
32 check("&Jjo-test", "☺test", nil)
34 check("&Jjo", "", errUTF7UnfinishedShift) // missing closing -
35 check("&Jjo-&-", "", errUTF7SuperfluousShift) // shift just after unshift not allowed, should have been a single shift.
36 check("&AGE-", "", errUTF7UnneededShift) // Just 'a', does not need utf7.
37 check("&☺-", "", errUTF7Base64)
38 check("&YQ-", "", errUTF7OddSized) // Just a single byte 'a'
39 check("&2AHcNw-", "𐐷", nil)
40 check(fmt.Sprintf("&%s-", utf7encoding.EncodeToString([]byte{0xdc, 0x00, 0xd8, 0x00})), "", errUTF7BadSurrogate) // Low & high surrogate swapped.
41 check(fmt.Sprintf("&%s-", utf7encoding.EncodeToString([]byte{0, 1, 0xdc, 0x00})), "", errUTF7BadSurrogate) // ASCII + high surrogate.
42 check(fmt.Sprintf("&%s-", utf7encoding.EncodeToString([]byte{0, 1, 0xd8, 0x00})), "", errUTF7BadSurrogate) // ASCII + low surrogate.
43 check(fmt.Sprintf("&%s-", utf7encoding.EncodeToString([]byte{0xd8, 0x00, 0, 1})), "", errUTF7BadSurrogate) // low surrogate + ASCII.
44 check(fmt.Sprintf("&%s-", utf7encoding.EncodeToString([]byte{0xdc, 0x00, 0, 1})), "", errUTF7BadSurrogate) // high surrogate + ASCII.
47 check("~peter/mail/&U,BTFw-/&ZeVnLIqe-", "~peter/mail/台北/日本語", nil)
48 check("&U,BTFw-&ZeVnLIqe-", "", errUTF7SuperfluousShift)