1package imapserver
2
3import (
4 "errors"
5 "fmt"
6 "testing"
7)
8
9func TestUTF7(t *testing.T) {
10 check := func(input string, output string, expErr error) {
11 t.Helper()
12
13 r, err := utf7decode(input)
14 if r != output {
15 t.Fatalf("got %q, expected %q (err %v), for input %q", r, output, err, input)
16 }
17 if (expErr == nil) != (err == nil) || err != nil && !errors.Is(err, expErr) {
18 t.Fatalf("got err %v, expected %v", err, expErr)
19 }
20 if expErr == nil {
21 expInput := utf7encode(output)
22 if expInput != input {
23 t.Fatalf("encoding, got %s, expected %s", expInput, input)
24 }
25 }
26 }
27
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)
33 check("&-", "&", 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.
45
46 // ../rfc/9051:7967
47 check("~peter/mail/&U,BTFw-/&ZeVnLIqe-", "~peter/mail/台北/日本語", nil)
48 check("&U,BTFw-&ZeVnLIqe-", "", errUTF7SuperfluousShift)
49}
50