| 1 |
;; |
| 2 |
;; testing precompiled rfc.* modules |
| 3 |
;; |
| 4 |
|
| 5 |
(use gauche.test) |
| 6 |
(use util.match) |
| 7 |
(use srfi.19) |
| 8 |
(use gauche.sequence) |
| 9 |
|
| 10 |
(test-start "precompiled rfc modules") |
| 11 |
|
| 12 |
;;-------------------------------------------------------------------- |
| 13 |
(test-section "rfc.822") |
| 14 |
(use rfc.822) |
| 15 |
(test-module 'rfc.822) |
| 16 |
|
| 17 |
(define rfc822-header1 |
| 18 |
"Received: by foo.bar.com id ZZZ55555; Thu, 31 May 2001 16:38:04 -1000 (HST) |
| 19 |
Received: from ooo.ooo.com (ooo.ooo.com [1.2.3.4]) |
| 20 |
\tby foo.bar.com (9.9.9+3.2W/3.7W-) with ESMTP id ZZZ55555 |
| 21 |
\tfor <[email protected]>; Thu, 31 May 2001 16:38:02 -1000 (HST) |
| 22 |
Received: from zzz ([1.2.3.5]) by ooo.ooo.com with Maccrosoft SMTPSVC(5.5.1877.197.19); |
| 23 |
\t Thu, 31 May 2001 22:33:16 -0400 |
| 24 |
Message-ID: <[email protected]> |
| 25 |
Subject: Bogus Tester |
| 26 |
From: Bogus Sender <[email protected]> |
| 27 |
To: You <[email protected]>, Another <[email protected]> |
| 28 |
Date: Fri, 01 Jun 2001 02:37:31 (GMT) |
| 29 |
Mime-Version: 1.0 |
| 30 |
Content-Type: text/html |
| 31 |
Content-Transfer-Encoding: quoted-printable |
| 32 |
X-MSMail-Priority: Normal |
| 33 |
X-mailer: FooMail 4.0 4.03 (SMT460B92F) |
| 34 |
Content-Length: 4349 |
| 35 |
|
| 36 |
") |
| 37 |
|
| 38 |
(define rfc822-header1-list |
| 39 |
'(("received" "by foo.bar.com id ZZZ55555; Thu, 31 May 2001 16:38:04 -1000 (HST)") |
| 40 |
("received" "from ooo.ooo.com (ooo.ooo.com [1.2.3.4])\tby foo.bar.com (9.9.9+3.2W/3.7W-) with ESMTP id ZZZ55555\tfor <[email protected]>; Thu, 31 May 2001 16:38:02 -1000 (HST)") |
| 41 |
("received" "from zzz ([1.2.3.5]) by ooo.ooo.com with Maccrosoft SMTPSVC(5.5.1877.197.19);\t Thu, 31 May 2001 22:33:16 -0400") |
| 42 |
("message-id" "<[email protected]>") |
| 43 |
("subject" "Bogus Tester") |
| 44 |
("from" "Bogus Sender <[email protected]>") |
| 45 |
("to" "You <[email protected]>, Another <[email protected]>") |
| 46 |
("date" "Fri, 01 Jun 2001 02:37:31 (GMT)") |
| 47 |
("mime-version" "1.0") |
| 48 |
("content-type" "text/html") |
| 49 |
("content-transfer-encoding" "quoted-printable") |
| 50 |
("x-msmail-priority" "Normal") |
| 51 |
("x-mailer" "FooMail 4.0 4.03 (SMT460B92F)") |
| 52 |
("content-length" "4349") |
| 53 |
)) |
| 54 |
|
| 55 |
(test* "rfc822-read-headers" #t |
| 56 |
(equal? rfc822-header1-list |
| 57 |
(rfc822-read-headers (open-input-string rfc822-header1)))) |
| 58 |
|
| 59 |
(test* "rfc822-header-ref" "Bogus Tester" |
| 60 |
(rfc822-header-ref rfc822-header1-list "subject")) |
| 61 |
(test* "rfc822-header-ref" "Missing" |
| 62 |
(rfc822-header-ref rfc822-header1-list "cc" "Missing")) |
| 63 |
(test* "rfc822-header-ref" |
| 64 |
"by foo.bar.com id ZZZ55555; Thu, 31 May 2001 16:38:04 -1000 (HST)" |
| 65 |
(rfc822-header-ref rfc822-header1-list "received")) |
| 66 |
(test* "rfc822-header-ref*" |
| 67 |
'("by foo.bar.com id ZZZ55555; Thu, 31 May 2001 16:38:04 -1000 (HST)" |
| 68 |
"from ooo.ooo.com (ooo.ooo.com [1.2.3.4])\tby foo.bar.com (9.9.9+3.2W/3.7W-) with ESMTP id ZZZ55555\tfor <[email protected]>; Thu, 31 May 2001 16:38:02 -1000 (HST)" |
| 69 |
"from zzz ([1.2.3.5]) by ooo.ooo.com with Maccrosoft SMTPSVC(5.5.1877.197.19);\t Thu, 31 May 2001 22:33:16 -0400") |
| 70 |
(rfc822-header-ref* rfc822-header1-list "received")) |
| 71 |
(test* "rfc822-header-put" '("Redacted") |
| 72 |
(rfc822-header-ref* |
| 73 |
(rfc822-header-put rfc822-header1-list "received" "Redacted") |
| 74 |
"received")) |
| 75 |
|
| 76 |
;; token parsers |
| 77 |
(test* "rfc822-field->tokens (basic)" |
| 78 |
'(("aa") ("bb") ("cc") ("dd") ("ee") (" a\"aa\\aa (a)")) |
| 79 |
(map rfc822-field->tokens |
| 80 |
'("aa" |
| 81 |
" bb " |
| 82 |
" (comment) cc(comment)" |
| 83 |
" (co\\mm$$*##&$%ent) dd(com (me) nt)" |
| 84 |
"\"ee\"" |
| 85 |
" \" a\\\"aa\\\\aa (a)\" (comment\\))"))) |
| 86 |
|
| 87 |
(test* "rfc822-field->tokens" |
| 88 |
'("from" "aaaaa.aaa.org" "by" "ggg.gggg.net" "with" "ESMTP" "id" "24D50175C8") |
| 89 |
(rfc822-field->tokens |
| 90 |
"from aaaaa.aaa.org (aaaaa.aaa.org [192.168.0.9]) by ggg.gggg.net (Postfix) with ESMTP id 24D50175C8")) |
| 91 |
|
| 92 |
|
| 93 |
(test* "rfc822-parse-date" '(2003 3 4 12 34 56 -3600 2) |
| 94 |
(receive r (rfc822-parse-date "Tue, 4 Mar 2003 12:34:56 -3600") r)) |
| 95 |
|
| 96 |
(test* "rfc822-parse-date" '(2003 3 4 12 34 56 0 2) |
| 97 |
(receive r (rfc822-parse-date "Tue, 4 Mar 2003 12:34:56 UT") r)) |
| 98 |
|
| 99 |
(test* "rfc822-parse-date (no weekday)" '(2003 3 4 12 34 56 -3600 #f) |
| 100 |
(receive r (rfc822-parse-date "4 Mar 2003 12:34:56 -3600") r)) |
| 101 |
|
| 102 |
(test* "rfc822-parse-date (no timezone)" '(2003 3 4 12 34 56 #f #f) |
| 103 |
(receive r (rfc822-parse-date "4 Mar 2003 12:34:56") r)) |
| 104 |
|
| 105 |
(test* "rfc822-parse-date (old tz)" '(2003 3 4 12 34 56 #f #f) |
| 106 |
(receive r (rfc822-parse-date "4 Mar 2003 12:34:56 jst") r)) |
| 107 |
|
| 108 |
(test* "rfc822-parse-date (no seconds)" '(2003 3 4 12 34 #f 900 #f) |
| 109 |
(receive r (rfc822-parse-date "4 Mar 2003 12:34 +0900") r)) |
| 110 |
|
| 111 |
(test* "rfc822-parse-date (no seconds)" '(2003 3 4 12 34 #f 900 2) |
| 112 |
(receive r (rfc822-parse-date "Tue, 04 Mar 2003 12:34 +0900") r)) |
| 113 |
|
| 114 |
(test* "rfc822-parse-date (2digit year)" '(2003 3 4 12 34 56 -3600 2) |
| 115 |
(receive r (rfc822-parse-date "Tue, 4 Mar 03 12:34:56 -3600") r)) |
| 116 |
|
| 117 |
(test* "rfc822-parse-date (2digit year)" '(1987 3 4 12 34 56 -3600 2) |
| 118 |
(receive r (rfc822-parse-date "Tue, 4 Mar 87 12:34:56 -3600") r)) |
| 119 |
|
| 120 |
(test* "rfc822-parse-date (Weekday, exhausive)" '(0 1 2 3 4 5 6 #f) |
| 121 |
(map-with-index |
| 122 |
(lambda (ind wday) |
| 123 |
(receive (y m d H M S tz wd) |
| 124 |
(rfc822-parse-date |
| 125 |
#"~|wday|, ~(+ 2 ind) Jan 2000 00:00:00 +0000") |
| 126 |
wd)) |
| 127 |
'("Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" "Znn"))) |
| 128 |
|
| 129 |
(test* "rfc822-parse-date (Months, exhausive)" |
| 130 |
'(1 2 3 4 5 6 7 8 9 10 11 12 #f) |
| 131 |
(map (lambda (mon) |
| 132 |
(receive (y m d H M S tz wd) |
| 133 |
(rfc822-parse-date |
| 134 |
#"1 ~mon 1999 00:00:00 +0000") |
| 135 |
m)) |
| 136 |
'("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" |
| 137 |
"Sep" "Oct" "Nov" "Dec" "Zzz"))) |
| 138 |
|
| 139 |
(test* "rfc822-parse-date (invalid)" '(#f #f #f #f #f #f #f #f) |
| 140 |
(receive r (rfc822-parse-date "Sun 2 Mar 2002") r)) |
| 141 |
|
| 142 |
(test* "date->rfc822-date" "Sun, 29 Nov 2009 01:23:45 +0000" |
| 143 |
(date->rfc822-date (make-date 0 45 23 01 29 11 2009 0))) |
| 144 |
(test* "date->rfc822-date" "Sun, 29 Nov 2009 01:23:45 +0900" |
| 145 |
(date->rfc822-date (make-date 0 45 23 01 29 11 2009 32400))) |
| 146 |
(test* "date->rfc822-date" "Sun, 29 Nov 2009 01:23:45 -0830" |
| 147 |
(date->rfc822-date (make-date 0 45 23 01 29 11 2009 -30600))) |
| 148 |
(test* "date->rfc822-date" "Sun, 29 Nov 2009 01:23:45 +0030" |
| 149 |
(date->rfc822-date (make-date 0 45 23 01 29 11 2009 1800))) |
| 150 |
|
| 151 |
|
| 152 |
(test* "rfc822-invalid-header-field" #f |
| 153 |
(rfc822-invalid-header-field "abcde")) |
| 154 |
(test* "rfc822-invalid-header-field" 'incomplete-string |
| 155 |
(rfc822-invalid-header-field #*"abcde")) |
| 156 |
;; unicode literal doesn't work with none encoding |
| 157 |
(unless (eq? (gauche-character-encoding) 'none) |
| 158 |
(test* "rfc822-invalid-header-field" 'bad-character |
| 159 |
(rfc822-invalid-header-field "abc\u3030 def")) |
| 160 |
) |
| 161 |
(test* "rfc822-invalid-header-field" 'bad-character |
| 162 |
(rfc822-invalid-header-field "abc\x00 def")) |
| 163 |
(test* "rfc822-invalid-header-field" 'line-too-long |
| 164 |
(rfc822-invalid-header-field (make-string 1000 #\a))) |
| 165 |
(test* "rfc822-invalid-header-field" 'line-too-long |
| 166 |
(rfc822-invalid-header-field |
| 167 |
(string-append (string-join (make-list 5 (make-string 78 #\a)) |
| 168 |
"\r\n ") |
| 169 |
(make-string 1000 #\a)))) |
| 170 |
(test* "rfc822-invalid-header-field" 'stray-crlf |
| 171 |
(rfc822-invalid-header-field |
| 172 |
(string-join (make-list 5 (make-string 78 #\a)) "\r\n"))) |
| 173 |
(test* "rfc822-invalid-header-field" 'stray-crlf |
| 174 |
(rfc822-invalid-header-field "abc\ndef")) |
| 175 |
(test* "rfc822-invalid-header-field" 'stray-crlf |
| 176 |
(rfc822-invalid-header-field "abc\rdef")) |
| 177 |
|
| 178 |
(test* "rfc822-write-headers" |
| 179 |
"name: Shiro Kawai\r\n\ |
| 180 |
address: 1234 Lambda St.\r\n \ |
| 181 |
Higher Order Functions, HI, 99899\r\n\ |
| 182 |
registration-date: 2007-12-10\r\n\r\n" |
| 183 |
(with-output-to-string |
| 184 |
(cut rfc822-write-headers |
| 185 |
'(("name" "Shiro Kawai") |
| 186 |
("address" "1234 Lambda St.\r\n Higher Order Functions, HI, 99899") |
| 187 |
("registration-date" "2007-12-10"))))) |
| 188 |
|
| 189 |
(test* "rfc822-write-headers (ignore error)" |
| 190 |
(make-list 2 "name: Shiro\x00Kawai\r\n\r\n") |
| 191 |
(map (lambda (x) |
| 192 |
(with-output-to-string |
| 193 |
(cut rfc822-write-headers '(("name" "Shiro\x00Kawai")) |
| 194 |
:check x))) |
| 195 |
'(#f :ignore))) |
| 196 |
|
| 197 |
(test* "rfc822-write-headers (continue)" |
| 198 |
"x: A\r\nx: B\r\nx: C\r\n\r\n" |
| 199 |
(call-with-output-string |
| 200 |
(lambda (p) |
| 201 |
(rfc822-write-headers '(("x" "A") ("x" "B")) :output p :continue #t) |
| 202 |
(rfc822-write-headers '(("x" "C")) :output p)))) |
| 203 |
|
| 204 |
(let-syntax ([test-reason |
| 205 |
(syntax-rules () |
| 206 |
[(_ expect hdrs) |
| 207 |
(begin |
| 208 |
(test* (format "rfc822-write-headers error (~a)" expect) |
| 209 |
expect |
| 210 |
(guard (e (else (rxmatch-case |
| 211 |
(condition-message e "") |
| 212 |
[#/\(([\w-]+)\)/ (_ m) m] |
| 213 |
[else e]))) |
| 214 |
(with-output-to-string |
| 215 |
(cut rfc822-write-headers hdrs)))) |
| 216 |
(test* (format "rfc822-write-headers handle (~a)" expect) |
| 217 |
(format "x-name: ~a\r\n\r\n" expect) |
| 218 |
(with-output-to-string |
| 219 |
(cut rfc822-write-headers hdrs |
| 220 |
:check (lambda (name body reason) |
| 221 |
(values #"x-~name" |
| 222 |
(x->string reason)))))) |
| 223 |
)])]) |
| 224 |
|
| 225 |
(test-reason "bad-character" '(("name" "Shiro\x00Kawai"))) |
| 226 |
(test-reason "incomplete-string" '(("name" #*"Shiro Kawai"))) |
| 227 |
(test-reason "stray-crlf" '(("name" "Shiro\nKawai"))) |
| 228 |
(test-reason "line-too-long" `(("name" ,(make-string 1000 #\a)))) |
| 229 |
) |
| 230 |
|
| 231 |
;;-------------------------------------------------------------------- |
| 232 |
(test-section "rfc.mime") |
| 233 |
(use rfc.mime) |
| 234 |
(test-module 'rfc.mime) |
| 235 |
(use rfc.mime-port) |
| 236 |
(test-module 'rfc.mime-port) |
| 237 |
|
| 238 |
(test* "mime-parse-version" '((1 0) (1 0) (1 0) (1 0) #f) |
| 239 |
(map mime-parse-version |
| 240 |
'(" 1.0" |
| 241 |
" 1.0 (produced by MetaSend Vx.x) " |
| 242 |
" (produced by MetaSend Vx.x) 1.0" |
| 243 |
" 1.(produced by MetaSend Vx.x (beta))0" |
| 244 |
" none "))) |
| 245 |
|
| 246 |
(test* "mime-parse-content-type" '("text" "plain") |
| 247 |
(mime-parse-content-type " text/plain (client: foo bar)")) |
| 248 |
(test* "mime-parse-content-type" '("text" "plain" ("charset" . "us-ascii")) |
| 249 |
(mime-parse-content-type " text/plain ;charset=\"us-ascii\"")) |
| 250 |
(test* "mime-parse-content-type" '("text" "plain" ("charset" . "us-ascii")) |
| 251 |
(mime-parse-content-type " text/plain; charset=us-ascii (Plain Text)")) |
| 252 |
(test* "mime-parse-content-type" '("text" "plain" ("charset" . "iso-2022-jp")) |
| 253 |
(mime-parse-content-type " text/(Plain Text)plain ; (Japanese) charset=iso-2022-jp")) |
| 254 |
|
| 255 |
(test* "mime-parse-content-type" |
| 256 |
'("text" "plain" ("zzz" . "yyy") ("xxx" . "www")) |
| 257 |
(mime-parse-content-type " text/plain ;zzz=\"yyy\"; xxx = www (AAA)")) |
| 258 |
(test* "mime-parse-content-type" |
| 259 |
'("multipart" "alternative" |
| 260 |
("boundary" . "=_alternative 006EBAA488256DF0_=")) |
| 261 |
(mime-parse-content-type |
| 262 |
"multipart/alternative; boundary=\"=_alternative 006EBAA488256DF0_=\"") |
| 263 |
) |
| 264 |
|
| 265 |
#| ; TODO: enable these after rfc2231 support |
| 266 |
(test* "mime-parse-content-type (rfc2231 encode)" |
| 267 |
'("application" "x-stuff" |
| 268 |
("x" . "y") |
| 269 |
("title" . "This is ***fun***") |
| 270 |
("url" . "http://www.example.com/")) |
| 271 |
(mime-parse-content-type |
| 272 |
"application/x-stuff; x=y; title*=us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A; url=\"http://www.example.com/\"")) |
| 273 |
|
| 274 |
(test* "mime-parse-content-type (rfc2231 concatenation)" |
| 275 |
'("message" "external-body" |
| 276 |
("access-type" . "URL") |
| 277 |
("url" . "ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar")) |
| 278 |
(mime-parse-content-type |
| 279 |
"Content-Type: message/external-body; access-type=URL; \ |
| 280 |
URL*0=\"ftp://\"; \ |
| 281 |
URL*1=\"cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar\"")) |
| 282 |
|
| 283 |
(test* "mime-parse-content-type (rfc2231 concatenation and encode)" |
| 284 |
'("application" "x-stuff" |
| 285 |
("title" . "This is even more ***fun*** isn't it!") |
| 286 |
("a" . "b")) |
| 287 |
(mime-parse-content-type |
| 288 |
"Content-Type: application/x-stuff; \ |
| 289 |
title*0*=us-ascii'en'This%20is%20even%20more%20 \ |
| 290 |
title*1*=%2A%2A%2Afun%2A%2A%2A%20 \ |
| 291 |
title*2=\"isn't it!\"; \ |
| 292 |
a=b")) |
| 293 |
|# |
| 294 |
|
| 295 |
(test* "mime-compose-parameters (simple)" |
| 296 |
"; ab=cd; ef=gh" |
| 297 |
(mime-compose-parameters '((ab . cd) (ef . gh)) #f)) |
| 298 |
(test* "mime-compose-parameters (quote)" |
| 299 |
"; ab=\"c d\"; ef=\"\\\"\\\\\"" |
| 300 |
(mime-compose-parameters '((ab . "c d") (ef . "\"\\")) #f)) |
| 301 |
(test* "mime-compose-parameters (long)" |
| 302 |
"; ab=cd;\r\n foo=012345678901234567890123456789012345679012345678901234567890123456789" |
| 303 |
(mime-compose-parameters |
| 304 |
'((ab . cd) (foo . "012345678901234567890123456789012345679012345678901234567890123456789")) |
| 305 |
#f)) |
| 306 |
|
| 307 |
(test* "mime-encode-text (pass-through)" "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\r\n tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim\r\n veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea\r\n commodo consequat. Duis aute irure dolor in reprehenderit in voluptate\r\n velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat\r\n cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id\r\n est laborum." |
| 308 |
(mime-encode-text "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")) |
| 309 |
(test* "mime-encode-text (pass-through, nonbreak)" "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." |
| 310 |
(mime-encode-text "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." |
| 311 |
:line-width #f)) |
| 312 |
(test* "mime-encode-text (pass-through, forced line break)" "Loremipsumdolorsitamet,consecteturadipisicingelit,seddoeiusmodtemporincididu\r\n ntutlaboreetdoloremagnaaliqua." |
| 313 |
(mime-encode-text "Loremipsumdolorsitamet,consecteturadipisicingelit,seddoeiusmodtemporincididuntutlaboreetdoloremagnaaliqua.")) |
| 314 |
|
| 315 |
;; unicode literal doesn't work with none encoding |
| 316 |
(unless (eq? (gauche-character-encoding) 'none) |
| 317 |
(test* "mime-encode-text" "=?utf-8?B?zrvjga7lroflrpnjgbjjgojjgYbjgZPjgZ0=?=\r\n =?utf-8?B?44CCV2VsY29tZSB0byDOuy1zcGFjZQ==?=" |
| 318 |
(mime-encode-text "\u03bb\u306e\u5b87\u5b99\u3078\u3088\u3046\u3053\u305d\u3002\u0057\u0065\u006c\u0063\u006f\u006d\u0065\u0020\u0074\u006f\u0020\u03bb\u002d\u0073\u0070\u0061\u0063\u0065" :line-width 50)) |
| 319 |
(test* "mime-encode-text (nobreak)" "=?utf-8?B?zrvjga7lroflrpnjgbjjgojjgYbjgZPjgZ3jgIJXZWxjb21lIHRvIM67LXNwYWNl?=" |
| 320 |
(mime-encode-text "\u03bb\u306e\u5b87\u5b99\u3078\u3088\u3046\u3053\u305d\u3002\u0057\u0065\u006c\u0063\u006f\u006d\u0065\u0020\u0074\u006f\u0020\u03bb\u002d\u0073\u0070\u0061\u0063\u0065" :line-width #f)) |
| 321 |
) |
| 322 |
|
| 323 |
(use gauche.charconv) |
| 324 |
|
| 325 |
(when (ces-conversion-supported? "iso-8859-1" #f) |
| 326 |
(test* "mime-decode-word" "this is some text" |
| 327 |
(mime-decode-word "=?iso-8859-1?q?this=20is=20some=20text?=")) |
| 328 |
(test* "mime-decode-text" "this is some text" |
| 329 |
(mime-decode-text |
| 330 |
"=?iso-8859-1?q?this=20is?= =?iso-8859-1?q?some=20text?=")) |
| 331 |
(test* "mime-decode-text" "this is some text" |
| 332 |
(mime-decode-text |
| 333 |
"=?iso-8859-1?q?this=20is?= some=?iso-8859-1?q?=20text?=")) |
| 334 |
|
| 335 |
(test* "mime-encode-word" "=?iso-8859-1?Q?this=20is=20some=20text?=" |
| 336 |
(mime-encode-word "this is some text" :charset 'iso-8859-1 |
| 337 |
:transfer-encoding 'quoted-printable)) |
| 338 |
(test* "mime-encode-text" "=?iso-8859-1?B?VGhlIHF1aWNr?=\r\n =?iso-8859-1?B?IGJyb3duIGZv?=\r\n =?iso-8859-1?B?eCBqdW1wcyBv?=\r\n =?iso-8859-1?B?dmVyIHRoZSBs?=\r\n =?iso-8859-1?B?YXp5IGRvZw==?=" |
| 339 |
(mime-encode-text "The quick brown fox jumps over the lazy dog" |
| 340 |
:charset 'iso-8859-1 :force #t |
| 341 |
:line-width 30)) |
| 342 |
(test* "mime-encode-text" "\r\n =?iso-8859-1?B?VGhlIHF1aWNrIGJyb3du?=\r\n =?iso-8859-1?B?IGZveCBqdW1wcyBvdmVy?=\r\n =?iso-8859-1?B?IHRoZSBsYXp5IGRvZw==?=" |
| 343 |
(mime-encode-text "The quick brown fox jumps over the lazy dog" |
| 344 |
:charset 'iso-8859-1 :force #t |
| 345 |
:line-width 40 |
| 346 |
:start-column 20)) |
| 347 |
) |
| 348 |
|
| 349 |
(when (ces-conversion-supported? "us-ascii" #f) |
| 350 |
(test* "mime-decode-word" "Keith_Moore" |
| 351 |
(mime-decode-word "=?US-ASCII?Q?Keith_Moore?=")) |
| 352 |
(test* "mime-decode-word" "Keith_Moore" |
| 353 |
(mime-decode-word "=?US-ASCII?B?S2VpdGhfTW9vcmU=?=")) |
| 354 |
(test* "mime-decode-text" "Keith/Moore" |
| 355 |
(mime-decode-text "=?US-ASCII?B?S2VpdGg=?=/=?US-ASCII?Q?Moore?=")) |
| 356 |
|
| 357 |
(test* "mime-encode-text" "=?us-ascii?B?VGhlIHF1aWNr?=\r\n =?us-ascii?B?IGJyb3duIGZv?=\r\n =?us-ascii?B?eCBqdW1wcyBv?=\r\n =?us-ascii?B?dmVyIHRoZSBs?=\r\n =?us-ascii?B?YXp5IGRvZw==?=" |
| 358 |
(mime-encode-text "The quick brown fox jumps over the lazy dog" |
| 359 |
:charset 'us-ascii :force #t |
| 360 |
:line-width 30)) |
| 361 |
) |
| 362 |
|
| 363 |
(when (ces-conversion-supported? "iso-2022-jp" #f) |
| 364 |
(test* "mime-decode-word" "\u5ddd\u5408 \u53f2\u6717" |
| 365 |
(mime-decode-word "=?ISO-2022-JP?B?GyRCQG45ZxsoQiAbJEI7S08vGyhC?=")) |
| 366 |
(test* "mime-decode-text" "(\u5ddd\u5408 \u53f2\u6717)" |
| 367 |
(mime-decode-text "(=?iso-2022-jp?b?GyRCQG45ZxsoQg==?= =?iso-2022-jp?b?GyRCO0tPLxsoQg==?=)")) |
| 368 |
(test* "mime-encode-word" "=?iso-2022-jp?B?GyRCQG45ZxsoQiAbJEI7S08vGyhC?=" |
| 369 |
(mime-encode-word "\u5ddd\u5408 \u53f2\u6717" |
| 370 |
:charset 'iso-2022-jp)) |
| 371 |
) |
| 372 |
|
| 373 |
;; these tests whether illegal input sequence is handled gracefully |
| 374 |
(test* "mime-decode-word" "=?ISO-2022-JP?B?GyRCJDkbKBsoQg==?=" |
| 375 |
(mime-decode-word "=?ISO-2022-JP?B?GyRCJDkbKBsoQg==?=")) |
| 376 |
(test* "mime-decode-text" "(=?ISO-2022-JP?B?GyRCJDkbKBsoQg==?=)" |
| 377 |
(mime-decode-text "(=?ISO-2022-JP?B?GyRCJDkbKBsoQg==?=)")) |
| 378 |
|
| 379 |
;; NB: this assumes the test is run in-place |
| 380 |
(define (mime-message-tester num headers) |
| 381 |
(let ((src #"../../test/data/rfc-mime-~|num|.txt") |
| 382 |
(res (call-with-input-file #"../../test/data/rfc-mime-~|num|.res.txt" |
| 383 |
read))) |
| 384 |
(call-with-input-file src |
| 385 |
(lambda (inp) |
| 386 |
(let* ((title (read-line inp)) ;; test title |
| 387 |
(expl (read-line inp)) ;; explanation (ignored) |
| 388 |
(headers (or headers (rfc822-read-headers inp)))) |
| 389 |
(test* #"mime-parse-message (~|num| - ~|title|)" |
| 390 |
res |
| 391 |
(and (equal? (mime-parse-version |
| 392 |
(rfc822-header-ref headers "mime-version")) |
| 393 |
'(1 0)) |
| 394 |
(mime-message-resolver |
| 395 |
(mime-parse-message inp headers |
| 396 |
(cut mime-body->string <> <>)) |
| 397 |
#f) |
| 398 |
))))) |
| 399 |
)) |
| 400 |
|
| 401 |
(define (mime-message-resolver mesg parent) |
| 402 |
(unless (eqv? (ref mesg 'parent) parent) (error "parent link broken")) |
| 403 |
(list* (string-append (ref mesg 'type) "/" (ref mesg 'subtype)) |
| 404 |
(ref mesg 'index) |
| 405 |
(if (string? (ref mesg 'content)) |
| 406 |
(list (ref mesg 'content)) |
| 407 |
(map (cut mime-message-resolver <> mesg) (ref mesg 'content))))) |
| 408 |
|
| 409 |
(dotimes (n 8) |
| 410 |
(mime-message-tester |
| 411 |
n |
| 412 |
(and (= n 6) |
| 413 |
'(("mime-version" " 1.0") |
| 414 |
("content-type" "multipart/form-data; boundary=\"---------------------------6578815652962098482130719379\""))))) |
| 415 |
|
| 416 |
(let1 b (mime-make-boundary) |
| 417 |
(test* "mime-compose-message (simple)" |
| 418 |
(string-append "\r\n--"b"\r\n" |
| 419 |
"Content-type: text/plain\r\n" |
| 420 |
"Content-transfer-encoding: 7bit\r\n\r\n" |
| 421 |
"This is a pen." |
| 422 |
"\r\n--"b"\r\n" |
| 423 |
"Content-type: text/html; charset=us-ascii\r\n\r\n" |
| 424 |
"<html><head></head><body></body></html>" |
| 425 |
"\r\n--"b"\r\n" |
| 426 |
"Content-type: application/octet-stream\r\n" |
| 427 |
"Content-transfer-encoding: base64\r\n\r\n" |
| 428 |
"YWJjZGVmZw==" |
| 429 |
"\r\n--"b"--\r\n") |
| 430 |
(receive (s bb) |
| 431 |
(mime-compose-message-string |
| 432 |
'((("text" "plain") |
| 433 |
(("content-transfer-encoding" "7bit")) |
| 434 |
"This is a pen.") |
| 435 |
(("text" "html" ("charset" . "us-ascii")) |
| 436 |
() |
| 437 |
"<html><head></head><body></body></html>") |
| 438 |
(("application" "octet-stream") |
| 439 |
(("content-transfer-encoding" "base64")) |
| 440 |
"abcdefg")) |
| 441 |
:boundary b) |
| 442 |
(and (equal? b bb) s)))) |
| 443 |
|
| 444 |
(define (mime-roundtrip-tester num) |
| 445 |
(define (gen-parts mesg) |
| 446 |
(match mesg |
| 447 |
[(ctype _ (? string? body)) |
| 448 |
`(,(mime-parse-content-type ctype) '() ,body)] |
| 449 |
[("message/rfc822" _ ("text/plain" _ body)) |
| 450 |
`(("message" "rfc822") '() |
| 451 |
,#"content-type: text/plain\r\n\r\n~body")] |
| 452 |
[(ctype _ children ...) |
| 453 |
`(,(mime-parse-content-type ctype) '() |
| 454 |
(subparts ,@(map gen-parts children)))])) |
| 455 |
(let1 src (call-with-input-file #"../../test/data/rfc-mime-~|num|.res.txt" read) |
| 456 |
(receive (composed boundary) |
| 457 |
(mime-compose-message-string (list (gen-parts src))) |
| 458 |
(test* #"mime-roundtrip (~num)" |
| 459 |
`("multipart/mixed" 0 ,src) |
| 460 |
(mime-message-resolver |
| 461 |
(call-with-input-string composed |
| 462 |
(cut mime-parse-message <> |
| 463 |
`(("mime-version" "1.0") |
| 464 |
("content-type" ,#"multipart/mixed; boundary=\"~|boundary|\"")) |
| 465 |
(cut mime-body->string <> <>))) |
| 466 |
#f))))) |
| 467 |
|
| 468 |
(dotimes (n 8) (mime-roundtrip-tester n)) |
| 469 |
|
| 470 |
(test-end) |