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
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
START_MARKER 
 
// Global functions 
 
// 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") 
 
define(test_base64,[[ 
  test_eq(MIME.encode_base64($1),$2) 
  test_eq(MIME.decode_base64($2),$1) 
]]) 
define(test_base64url,[[ 
  test_eq(MIME.encode_base64url($1),$2) 
  test_eq(MIME.decode_base64url($2),$1) 
]]) 
define(test_base32,[[ 
  test_eq(MIME.encode_base32($1), $2) 
  test_eq(MIME.decode_base32($2), $1) 
  test_eq(MIME.decode_base32(lower_case($2)), $1) 
]]) 
define(test_base32hex,[[ 
  test_eq(MIME.encode_base32hex($1), $2) 
  test_eq(MIME.decode_base32hex($2), $1) 
  test_eq(MIME.decode_base32hex(lower_case($2)), $1) 
  test_eq(MIME.encode_base32hex($1, UNDEFINED,  1), $2 - "=") 
  test_eq(MIME.decode_base32hex($2 - "="), $1) 
  test_eq(MIME.decode_base32hex(lower_case($2) - "="), $1) 
]]) 
 
// Test vectors from RFC 4648 
test_base64("","") 
test_base64("f","Zg==") 
test_base64("fo","Zm8=") 
test_base64("foo","Zm9v") 
test_base64("foob","Zm9vYg==") 
test_base64("fooba","Zm9vYmE=") 
test_base64("foobar","Zm9vYmFy") 
test_base64url("","") 
test_base64url("f","Zg") 
test_base64url("fo","Zm8") 
test_base64url("foo","Zm9v") 
test_base64url("foob","Zm9vYg") 
test_base64url("fooba","Zm9vYmE") 
test_base64url("foobar","Zm9vYmFy") 
test_base32("","") 
test_base32("f","MY======") 
test_base32("fo","MZXQ====") 
test_base32("foo","MZXW6===") 
test_base32("foob","MZXW6YQ=") 
test_base32("fooba","MZXW6YTB") 
test_base32("foobar","MZXW6YTBOI======") 
test_base32("foobarg","MZXW6YTBOJTQ====") 
test_base32("foobarga","MZXW6YTBOJTWC===") 
test_base32("foobargaz","MZXW6YTBOJTWC6Q=") 
test_base32("foobargazo","MZXW6YTBOJTWC6TP") 
test_base32hex("","") 
test_base32hex("f","CO======") 
test_base32hex("fo","CPNG====") 
test_base32hex("foo","CPNMU===") 
test_base32hex("foob","CPNMUOG=") 
test_base32hex("fooba","CPNMUOJ1") 
test_base32hex("foobar","CPNMUOJ1E8======") 
test_base32hex("foobarg","CPNMUOJ1E9JG====") 
test_base32hex("foobarga","CPNMUOJ1E9JM2===") 
test_base32hex("foobargaz","CPNMUOJ1E9JM2UG=") 
test_base32hex("foobargazo","CPNMUOJ1E9JM2UJF") 
 
test_base64("\0\0\0", "AAAA") 
test_base64("\0\377", "AP8=") 
test_base64url("\0\0\0", "AAAA") 
test_base64url("\0\377", "AP8") 
test_eval_error(MIME.encode_base64("\400")) 
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==") 
 
define(test_buffer_handling, [[ 
  test_any([[ 
    // Test all lengths 0-127 to check that there are 
    // no buffering errors. 
    for (int i = 0; i < 128; i++) { 
      string(8bit) vector = "A"*i; 
      string(7bit) enc = MIME.encode_$1(vector); 
      string(8bit) dec = MIME.decode_$1(enc); 
      if (dec != vector) return ({ vector, enc, dec }); 
    } 
    return 0; 
  ]], 0) 
]]) 
 
test_buffer_handling(base64) 
test_buffer_handling(base64url) 
test_buffer_handling(base32) 
test_buffer_handling(base32hex) 
test_buffer_handling(qp) 
test_buffer_handling(uue) 
 
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_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_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) 
 
// 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