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
  
START_MARKER 
 
dnl MIME 
test_true([[objectp(MIME)]]) 
test_true([[programp(MIME.Message)]]) 
 
dnl Global functions 
 
dnl Bug 3171 
test_eq([[(MIME.parse_headers("\r\nBody\r\n\r\nMore Body\r\n")[1])]], 
          "Body\r\n\r\nMore Body\r\n") 
test_eq([[(MIME.parse_headers("\nBody\n\nMore Body\n")[1])]], 
          "Body\n\nMore Body\n") 
 
test_eq([[MIME.decode_base64("Zm9vCg==")]],"foo\n") 
test_eq([[MIME.decode_base64("YQ==YQ==")]],"a") 
test_eq([[MIME.encode_base64("bar")]],"YmFy") 
test_eq([[MIME.encode_base64("bar ")]],"YmFyIA==") 
test_eq([[MIME.encode_base64("bar  ")]],"YmFyICA=") 
test_eq([[MIME.encode_base64("bar   ")]],"YmFyICAg") 
test_eq([[MIME.encode_base64("How much wood could a woodchuck " 
        "chuck if a woodchuck could chuck wood?")]], 
        "SG93IG11Y2ggd29vZCBjb3VsZCBhIHdvb2RjaHVjayBjaHVjayBpZiBhI" 
        "Hdvb2RjaHVjayBjb3Vs\r\nZCBjaHVjayB3b29kPw==") 
test_eq([[MIME.encode_base64("How much wood could a woodchuck " 
        "chuck if a woodchuck could chuck wood?", 1)]], 
        "SG93IG11Y2ggd29vZCBjb3VsZCBhIHdvb2RjaHVjayBjaHVjayBpZiBhI" 
        "Hdvb2RjaHVjayBjb3VsZCBjaHVjayB3b29kPw==") 
test_any([[string data=Stdio.read_file("Makefile"); 
        return MIME.decode_base64(MIME.encode_base64(data))==data]],1) 
 
test_eq([[MIME.decode_qp("AbC=3a=4C=25z=\nxyz")]],"AbC:L%zxyz") 
test_eq([[MIME.encode_qp("åäöÅÄÖ=Ff")]],"=E5=E4=F6=C5=C4=D6=3DFf") 
test_eq([[MIME.encode_qp("How much wood could a woodchuck " 
        "chuck if a woodchuck could chuck wood?")]], 
        "How=20much=20wood=20could=20a=20woodchuck=20chuck=20if=20" 
        "a=20woodchuck=20=\r\ncould=20chuck=20wood?") 
test_eq([[MIME.encode_qp("How much wood could a woodchuck " 
        "chuck if a woodchuck could chuck wood?", 1)]], 
        "How=20much=20wood=20could=20a=20woodchuck=20chuck=20if=20" 
        "a=20woodchuck=20could=20chuck=20wood?") 
test_any([[string data=Stdio.read_file("Makefile"); 
        return MIME.decode_qp(MIME.encode_qp(data))==data]],1) 
 
test_eq([[MIME.decode_uue("begin 644 foo\n#8F%R\n`\nend\n")]], "bar") 
test_eq([[MIME.encode_uue("fie")]], 
                [["begin 644 attachment\r\n#9FEE\r\n`\r\nend\r\n"]]) 
test_eq([[MIME.encode_uue("fie ", "fum")]], 
                [["begin 644 fum\r\n$9FEE(```\r\n`\r\nend\r\n"]]) 
test_eq([[MIME.encode_uue("fie  ")]], 
                [["begin 644 attachment\r\n%9FEE(\"``\r\n`\r\nend\r\n"]]) 
test_eq([[MIME.encode_uue("fie   ")]], 
                [["begin 644 attachment\r\n&9FEE(\"`@\r\n`\r\nend\r\n"]]) 
test_eq([[MIME.encode_uue("fie    ")]], 
                [["begin 644 attachment\r\n'9FEE(\"`@(```\r\n`\r\nend\r\n"]]) 
test_eq([[MIME.encode_uue("How much wood could a woodchuck " 
        "chuck if a woodchuck could chuck wood?", "WOOD")]], 
        [["begin 644 WOOD\r\nM2&]W\050&UU8V@@=V]O9\"!C;W5L9\"!A\050'=O;V1C:'5C:" 
          "R!C:'5C:R!I9B!A\050'=O\r\n9;V1C:'5C:R!C;W5L9\"!C:'5C:R!W;V]D/P``" 
          "\r\n`\r\nend\r\n"]]) 
test_any([[string data=Stdio.read_file("Makefile"); 
        return MIME.decode_uue(MIME.encode_uue(data, "foo bar"))==data]],1) 
 
test_equal([[MIME.tokenize("foo&bar,fu.m:\"asdf(z\\\"\\\\)x\"afn(xy(z))ds")]], 
        ({"foo&bar",44,"fu.m",58,"asdf(z\"\\)x","afn","ds"})) 
test_equal([[MIME.tokenize("   asdf \t[foo\"bar\"ga(zonk)]  \"ghjk\"  ")]], 
        ({"asdf","[foo\"bar\"ga(zonk)]","ghjk"})) 
test_equal([[MIME.quote(({"asdf","ghij","a:b","foo",44,"bar",58,44,"åäö"}))]], 
        [["asdf ghij \"a:b\" foo,bar:,\"åäö\""]]) 
 
test_equal([[MIME.tokenize_labled("\":sysmail\"@  Some-Group. Some-Org, " 
        "Muhammed.(I am  the greatest) Ali @(t()\\)he)Vegas.[10.0.3.19]")]], 
        [[({({"word",":sysmail"}),({"special",'@'}),({"word","Some-Group."}), 
            ({"word","Some-Org"}),({"special",','}),({"word","Muhammed."}), 
            ({"comment","I am  the greatest"}),({"word","Ali"}), 
            ({"special",'@'}),({"comment","t())he"}),({"word","Vegas."}), 
            ({"domain-literal","[10.0.3.19]"})})]]) 
 
 
test_any([[int n; string t; return 2==sscanf(MIME.generate_boundary(), 
                "'ThIs-RaNdOm-StRiNg-/=_.%d:%s", n, t) && n>=0 && 
                n<=1000000000 && t==""]],1) 
 
test_eq([[MIME.decode("foo=21bar", 0)]], "foo=21bar") 
test_eq([[MIME.decode("foo=21bar", "Quoted-PrinTabLe")]], "foo!bar") 
test_eq([[MIME.decode("Zm9vYmFy", "BasE64")]], "foobar") 
test_eq([[MIME.decode("begin 644 foo\r\n#8F%Z\r\n`\r\nend", "X-uUe")]], "baz") 
test_eq([[MIME.decode("1=23", "7bIt")]], "1=23") 
test_eq([[MIME.decode("4=56", "8BiT")]], "4=56") 
test_eq([[MIME.decode("7=89", "biNaRy")]], "7=89") 
test_true([[catch(MIME.decode("foo", "bzot"))]]) 
test_eq([[MIME.encode("foo=21bar", 0)]], "foo=21bar") 
test_eq([[MIME.encode("foo=21bar", "quOteD-priNtaBle")]], "foo=3D21bar") 
test_eq([[MIME.encode("foo=21bar", "qUotEd-pRinTablE", "gek")]], "foo=3D21bar") 
test_eq([[MIME.encode("How much wood could a woodchuck " 
        "chuck if a woodchuck could chuck wood?", "quoted-PRINtable", 0, 1)]], 
        "How=20much=20wood=20could=20a=20woodchuck=20chuck=20if=20" 
        "a=20woodchuck=20could=20chuck=20wood?") 
test_eq([[MIME.encode("foobar", "bASe64")]], "Zm9vYmFy") 
test_eq([[MIME.encode("How much wood could a woodchuck " 
        "chuck if a woodchuck could chuck wood?", "base64", 0, 1)]], 
        "SG93IG11Y2ggd29vZCBjb3VsZCBhIHdvb2RjaHVjayBjaHVjayBpZiBhI" 
        "Hdvb2RjaHVjayBjb3VsZCBjaHVjayB3b29kPw==") 
test_eq([[MIME.encode("hepp", "x-Uue")]], 
                [["begin 644 attachment\r\n$:&5P<```\r\n`\r\nend\r\n"]]) 
test_eq([[MIME.encode("baz", "x-uuE", "foo")]], 
                [["begin 644 foo\r\n#8F%Z\r\n`\r\nend\r\n"]]) 
test_eq([[MIME.encode("1=23", "7biT")]], "1=23") 
test_eq([[MIME.encode("4=56", "8Bit")]], "4=56") 
test_eq([[MIME.encode("7=89", "BinArY")]], "7=89") 
test_true([[catch(MIME.encode("foo", "bzot"))]]) 
 
test_equal([[MIME.decode_word("foo")]], ({"foo",0})) 
test_equal([[MIME.decode_word("=?kOi8?Q?=2ax_y=5F?=")]], ({"*x y_","koi8"})) 
test_equal([[MIME.decode_word("=?iSo646?b?KnkgeF8=?=")]], ({"*y x_","iso646"})) 
test_true([[catch(MIME.decode_word("=?IsO10646?e?aa?="))]]) 
test_eq([[MIME.encode_word(({"foo", "cp037"}), 0)]], "foo") 
test_eq([[MIME.encode_word(({"foo", 0}),"bAsE64")]], "foo") 
test_eq([[MIME.encode_word(({"*x y_","koi8"}), "QUoTeD-prIntaBle")]], 
                "=?koi8?q?*x=20y=5F?=") 
test_eq([[MIME.encode_word(({"*y x_","iso646"}), "baSE64")]], 
                "=?iso646?b?KnkgeF8=?=") 
test_eq([[MIME.encode_word(({"foo","ksc5636"}), "B")]], "=?ksc5636?b?Zm9v?=") 
test_true([[catch(MIME.encode_word(({"foo","iso8859-1"}),"x-uue"))]]) 
 
test_eq([[MIME.guess_subtype("text")]], "plain") 
test_eq([[MIME.guess_subtype("message")]], "rfc822") 
test_eq([[MIME.guess_subtype("multipart")]], "mixed") 
test_eq([[MIME.guess_subtype("image")]], 0) 
 
dnl MIME.Message 
test_true([[objectp(MIME.Message())]]) 
 
test_eq([[MIME.Message("MIME-Version: 1.0\r\n\r\nfoo\r\n")->type]], "text") 
test_eq([[MIME.Message("MIME-Version: 1.0\r\n\r\nfoo\r\n")->subtype]], "plain") 
test_eq([[MIME.Message("MIME-Version: 1.0\r\n\r\nfoo\r\n")->charset]], "us-ascii") 
test_eq([[MIME.Message("MIME-Version: 1.0\r\n\r\nfoo\r\n")->getdata()]], "foo\r\n") 
test_eq([[MIME.Message("MIME-Version: 1.0\r\n\r\nfoo\r\n")->getencoded()]], "foo\r\n") 
test_equal([[MIME.Message((string)MIME.Message("foo\r\n", 
                (["mImE-veRsion":"1.0"])))->headers]], 
        (["mime-version":"1.0","content-length":"5"])) 
 
test_any([[ 
  string pf_in = "My-Pr3f1x(.0/9)"; 
  MIME.set_boundary_prefix(pf_in); 
  string pf_out = MIME.get_boundary_prefix(); 
  string boundary = MIME.generate_boundary(); 
  int nm = sscanf(boundary, pf_out + "%d%s", int n, string suffix); 
  return pf_in == pf_out && 
         boundary[0..sizeof(pf_out)-1] == pf_out && 
         nm == 2 && n <= 1000000000 && suffix == ""; 
]], 1) 
 
END_MARKER