]> gitweb.factorcode.org Git - factor.git/blob - basis/smtp/smtp-tests.factor
Merge branch 'master' of git://factorcode.org/git/factor into clean-linux-x86-32
[factor.git] / basis / smtp / smtp-tests.factor
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
6
7 \ send-email must-infer
8
9 { 0 0 } [ [ ] with-smtp-connection ] must-infer-as
10
11 [ "hello\nworld" validate-address ] must-fail
12
13 [ "slava@factorcode.org" ]
14 [ "slava@factorcode.org" validate-address ] unit-test
15
16 [ { "hello" "." "world" } validate-message ] must-fail
17
18 [ "aGVsbG8Kd29ybGQ=\r\n.\r\n" ] [
19     T{ email { body "hello\nworld" } } [ send-body ] with-string-writer
20 ] unit-test
21
22 [ { "500 syntax error" } <response> check-response ]
23 [ smtp-error? ] must-fail-with
24
25 [ ] [ { "220 success" } <response> check-response ] unit-test
26
27 [ T{ response f 220 { "220 success" } } ] [
28     "220 success" [ receive-response ] with-string-reader
29 ] unit-test
30
31 [
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
41
42 [ ] [
43     "220-a multiline response\r\n250-another line\r\n220 the end"
44     [ get-ok ] with-string-reader
45 ] unit-test
46
47 [
48     "Subject:\r\nsecurity hole" validate-header
49 ] must-fail
50
51 [
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
78
79 <promise> "p" set
80
81 [ ] [ "p" get mock-smtp-server ] unit-test
82
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
88
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