Merge branch 'master' of git://factorcode.org/git/factor
[factor/jcg.git] / basis / smtp / smtp-tests.factor
blob8a9107b905ff5a65cd85005fb17a3e531bd2d7ed
1 USING: smtp tools.test io.streams.string io.sockets
2 io.sockets.secure threads smtp.server kernel sequences
3 namespaces logging accessors assocs sorting smtp.private
4 concurrency.promises system ;
5 IN: smtp.tests
7 \ send-email must-infer
9 { 0 0 } [ [ ] with-smtp-connection ] must-infer-as
11 [ "hello\nworld" validate-address ] must-fail
13 [ "slava@factorcode.org" ]
14 [ "slava@factorcode.org" validate-address ] unit-test
16 [ { "hello" "." "world" } validate-message ] must-fail
18 [ "aGVsbG8Kd29ybGQ=\r\n.\r\n" ] [
19     "hello\nworld" [ send-body ] with-string-writer
20 ] unit-test
22 [ { "500 syntax error" } <response> check-response ]
23 [ smtp-error? ] must-fail-with
25 [ ] [ { "220 success" } <response> check-response ] unit-test
27 [ T{ response f 220 { "220 success" } } ] [
28     "220 success" [ receive-response ] with-string-reader
29 ] unit-test
32     T{ response f 220 {
33         "220-a multiline response"
34         "250-another line"
35         "220 the end"
36     } }
37 ] [
38     "220-a multiline response\r\n250-another line\r\n220 the end"
39     [ receive-response ] with-string-reader
40 ] unit-test
42 [ ] [
43     "220-a multiline response\r\n250-another line\r\n220 the end"
44     [ get-ok ] with-string-reader
45 ] unit-test
48     "Subject:\r\nsecurity hole" validate-header
49 ] must-fail
52     {
53         { "Content-Transfer-Encoding" "base64" }
54         { "Content-Type" "Text/plain; charset=utf-8" }
55         { "From" "Doug <erg@factorcode.org>" }
56         { "MIME-Version" "1.0" }
57         { "Subject" "Factor rules" }
58         { "To" "Slava <slava@factorcode.org>, Ed <dharmatech@factorcode.org>" }
59     }
60     { "slava@factorcode.org" "dharmatech@factorcode.org" }
61     "erg@factorcode.org"
62 ] [
63     <email>
64         "Factor rules" >>subject
65         {
66             "Slava <slava@factorcode.org>"
67             "Ed <dharmatech@factorcode.org>"
68         } >>to
69         "Doug <erg@factorcode.org>" >>from
70     [
71         email>headers sort-keys [
72             drop { "Date" "Message-Id" } member? not
73         ] assoc-filter
74     ]
75     [ to>> [ extract-email ] map ]
76     [ from>> extract-email ] tri
77 ] unit-test
79 <promise> "p" set
81 [ ] [ "p" get mock-smtp-server ] unit-test
83 [ ] [
84     <secure-config> f >>verify [
85         "localhost" "p" get ?promise <inet> smtp-server set
86         no-auth smtp-auth set
87         os unix? [ smtp-tls? on ] when
89         <email>
90             "Hi guys\nBye guys" >>body
91             "Factor rules" >>subject
92             {
93                 "Slava <slava@factorcode.org>"
94                 "Ed <dharmatech@factorcode.org>"
95             } >>to
96             "Doug <erg@factorcode.org>" >>from
97         send-email
98     ] with-secure-context
99 ] unit-test