-
Notifications
You must be signed in to change notification settings - Fork 266
/
smtp_socket.erl
817 lines (762 loc) · 30.2 KB
/
smtp_socket.erl
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
%%% Copyright 2009 Jack Danger Canty <[email protected]>. All rights reserved.
%%%
%%% Permission is hereby granted, free of charge, to any person obtaining
%%% a copy of this software and associated documentation files (the
%%% "Software"), to deal in the Software without restriction, including
%%% without limitation the rights to use, copy, modify, merge, publish,
%%% distribute, sublicense, and/or sell copies of the Software, and to
%%% permit persons to whom the Software is furnished to do so, subject to
%%% the following conditions:
%%%
%%% The above copyright notice and this permission notice shall be
%%% included in all copies or substantial portions of the Software.
%%%
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
%%% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
%%% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
%%% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
%%% LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
%%% OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
%%% WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%% @doc Facilitates transparent gen_tcp/ssl socket handling
-module(smtp_socket).
-define(TCP_LISTEN_OPTIONS, [
{active, false},
{backlog, 30},
{ip, {0, 0, 0, 0}},
{keepalive, true},
{packet, line},
{reuseaddr, true}
]).
-define(TCP_CONNECT_OPTIONS, [
{active, false},
{packet, line},
{ip, {0, 0, 0, 0}},
{port, 0}
]).
-define(SSL_LISTEN_OPTIONS, [
{active, false},
{backlog, 30},
{certfile, "server.crt"},
{depth, 0},
{keepalive, true},
{keyfile, "server.key"},
{packet, line},
{reuse_sessions, false},
{reuseaddr, true}
]).
-define(SSL_CONNECT_OPTIONS, [
{active, false},
{depth, 0},
{packet, line},
{ip, {0, 0, 0, 0}},
{port, 0}
]).
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.
%% API
-export([connect/3, connect/4, connect/5]).
-export([listen/2, listen/3, accept/1, accept/2]).
-export([send/2, recv/2, recv/3]).
-export([controlling_process/2]).
-export([peername/1]).
-export([close/1, shutdown/2]).
-export([active_once/1]).
-export([setopts/2]).
-export([get_proto/1]).
-export([begin_inet_async/1]).
-export([handle_inet_async/1, handle_inet_async/2, handle_inet_async/3]).
-export([extract_port_from_socket/1]).
-export([to_ssl_server/1, to_ssl_server/2, to_ssl_server/3]).
-export([to_ssl_client/1, to_ssl_client/2, to_ssl_client/3]).
-export([type/1]).
-type protocol() :: 'tcp' | 'ssl'.
-type address() :: inet:ip_address() | string() | binary().
-type socket() :: ssl:sslsocket() | gen_tcp:socket().
-export_type([socket/0]).
%%%-----------------------------------------------------------------
%%% API
%%%-----------------------------------------------------------------
-spec connect(Protocol :: protocol(), Address :: address(), Port :: pos_integer()) ->
{ok, socket()} | {error, any()}.
connect(Protocol, Address, Port) ->
connect(Protocol, Address, Port, [], infinity).
-spec connect(
Protocol :: protocol(), Address :: address(), Port :: pos_integer(), Options :: list()
) -> {ok, socket()} | {error, any()}.
connect(Protocol, Address, Port, Opts) ->
connect(Protocol, Address, Port, Opts, infinity).
-spec connect(
Protocol :: protocol(),
Address :: address(),
Port :: pos_integer(),
Options :: list(),
Time :: non_neg_integer() | 'infinity'
) -> {ok, socket()} | {error, any()}.
connect(tcp, Address, Port, Opts, Time) ->
gen_tcp:connect(Address, Port, tcp_connect_options(Opts), Time);
connect(ssl, Address, Port, Opts, Time) ->
ssl:connect(Address, Port, ssl_connect_options(Opts), Time).
-spec listen(Protocol :: protocol(), Port :: pos_integer()) -> {ok, socket()} | {error, any()}.
listen(Protocol, Port) ->
listen(Protocol, Port, []).
-spec listen(Protocol :: protocol(), Port :: pos_integer(), Options :: list()) ->
{ok, socket()} | {error, any()}.
listen(ssl, Port, Options) ->
ssl:listen(Port, ssl_listen_options(Options));
listen(tcp, Port, Options) ->
gen_tcp:listen(Port, tcp_listen_options(Options)).
-spec accept(Socket :: socket()) -> {'ok', socket()} | {'error', any()}.
accept(Socket) ->
accept(Socket, infinity).
-spec accept(Socket :: socket(), Timeout :: pos_integer() | 'infinity') ->
{'ok', socket()} | {'error', any()}.
accept(Socket, Timeout) when is_port(Socket) ->
case gen_tcp:accept(Socket, Timeout) of
{ok, NewSocket} ->
{ok, Opts} = inet:getopts(Socket, [active, keepalive, packet, reuseaddr]),
inet:setopts(NewSocket, Opts),
{ok, NewSocket};
{error, _} = Error ->
Error
end;
accept(Socket, Timeout) ->
case ssl:transport_accept(Socket, Timeout) of
{ok, NewSocket} ->
ssl_handshake(NewSocket);
{error, _} = Error ->
Error
end.
-ifdef(OTP_RELEASE).
ssl_handshake(Socket) ->
ssl:handshake(Socket).
ssl_handshake(Socket, Options, Timeout) ->
ssl:handshake(Socket, Options, Timeout).
-else.
ssl_handshake(Socket) ->
case ssl:ssl_accept(Socket) of
ok -> {ok, Socket};
{error, _} = Error -> Error
end.
ssl_handshake(Socket, Options, Timeout) ->
case ssl:ssl_accept(Socket, Options, Timeout) of
{ok, _} = OK -> OK;
{error, _} = Error -> Error
end.
-endif.
-spec send(Socket :: socket(), Data :: binary() | string() | iolist()) -> 'ok' | {'error', any()}.
send(Socket, Data) when is_port(Socket) ->
gen_tcp:send(Socket, Data);
send(Socket, Data) ->
ssl:send(Socket, Data).
-spec recv(Socket :: socket(), Length :: non_neg_integer()) -> {'ok', any()} | {'error', any()}.
recv(Socket, Length) ->
recv(Socket, Length, infinity).
-spec recv(
Socket :: socket(), Length :: non_neg_integer(), Timeout :: non_neg_integer() | 'infinity'
) -> {'ok', any()} | {'error', any()}.
recv(Socket, Length, Timeout) when is_port(Socket) ->
gen_tcp:recv(Socket, Length, Timeout);
recv(Socket, Length, Timeout) ->
ssl:recv(Socket, Length, Timeout).
-spec controlling_process(Socket :: socket(), NewOwner :: pid()) -> 'ok' | {'error', any()}.
controlling_process(Socket, NewOwner) when is_port(Socket) ->
gen_tcp:controlling_process(Socket, NewOwner);
controlling_process(Socket, NewOwner) ->
ssl:controlling_process(Socket, NewOwner).
-spec peername(Socket :: socket()) ->
{ok, {inet:ip_address(), non_neg_integer()}} | {'error', any()}.
peername(Socket) when is_port(Socket) ->
inet:peername(Socket);
peername(Socket) ->
ssl:peername(Socket).
-spec close(Socket :: socket()) -> 'ok'.
close(Socket) when is_port(Socket) ->
gen_tcp:close(Socket);
close(Socket) ->
ssl:close(Socket).
-spec shutdown(Socket :: socket(), How :: 'read' | 'write' | 'read_write') ->
'ok' | {'error', any()}.
shutdown(Socket, How) when is_port(Socket) ->
gen_tcp:shutdown(Socket, How);
shutdown(Socket, How) ->
ssl:shutdown(Socket, How).
-spec active_once(Socket :: socket()) -> 'ok' | {'error', any()}.
active_once(Socket) when is_port(Socket) ->
inet:setopts(Socket, [{active, once}]);
active_once(Socket) ->
ssl:setopts(Socket, [{active, once}]).
-spec setopts(Socket :: socket(), Options :: list()) -> 'ok' | {'error', any()}.
setopts(Socket, Options) when is_port(Socket) ->
inet:setopts(Socket, Options);
setopts(Socket, Options) ->
ssl:setopts(Socket, Options).
-spec get_proto(Socket :: any()) -> 'tcp' | 'ssl'.
get_proto(Socket) when is_port(Socket) ->
tcp;
get_proto(_Socket) ->
ssl.
%% @doc {inet_async,...} will be sent to current process when a client connects
-spec begin_inet_async(Socket :: socket()) -> any().
begin_inet_async(Socket) when is_port(Socket) ->
prim_inet:async_accept(Socket, -1);
begin_inet_async(Socket) ->
Port = extract_port_from_socket(Socket),
begin_inet_async(Port).
%% @doc handle the {inet_async,...} message
-spec handle_inet_async(Message :: {'inet_async', socket(), any(), {'ok', socket()}}) ->
{'ok', socket()}.
handle_inet_async({inet_async, ListenSocket, _, {ok, ClientSocket}}) ->
handle_inet_async(ListenSocket, ClientSocket, []).
-spec handle_inet_async(ListenSocket :: socket(), ClientSocket :: socket()) -> {'ok', socket()}.
handle_inet_async(ListenObject, ClientSocket) ->
handle_inet_async(ListenObject, ClientSocket, []).
-spec handle_inet_async(ListenSocket :: socket(), ClientSocket :: socket(), Options :: list()) ->
{'ok', socket()}.
handle_inet_async(ListenObject, ClientSocket, Options) ->
ListenSocket = extract_port_from_socket(ListenObject),
case set_sockopt(ListenSocket, ClientSocket) of
ok -> ok;
Error -> erlang:error(set_sockopt, Error)
end,
%% Signal the network driver that we are ready to accept another connection
begin_inet_async(ListenSocket),
%% If the listening socket is SSL then negotiate the client socket
case is_port(ListenObject) of
true ->
{ok, ClientSocket};
false ->
{ok, UpgradedClientSocket} = to_ssl_server(ClientSocket, Options),
{ok, UpgradedClientSocket}
end.
%% @doc Upgrade a TCP connection to SSL
-spec to_ssl_server(Socket :: socket()) -> {'ok', ssl:sslsocket()} | {'error', any()}.
to_ssl_server(Socket) ->
to_ssl_server(Socket, []).
-spec to_ssl_server(Socket :: socket(), Options :: list()) ->
{'ok', ssl:sslsocket()} | {'error', any()}.
to_ssl_server(Socket, Options) ->
to_ssl_server(Socket, Options, infinity).
-spec to_ssl_server(
Socket :: socket(), Options :: list(), Timeout :: non_neg_integer() | 'infinity'
) -> {'ok', ssl:sslsocket()} | {'error', any()}.
to_ssl_server(Socket, Options, Timeout) when is_port(Socket) ->
ssl_handshake(Socket, ssl_listen_options(Options), Timeout);
to_ssl_server(_Socket, _Options, _Timeout) ->
{error, already_ssl}.
-spec to_ssl_client(Socket :: socket()) -> {'ok', ssl:sslsocket()} | {'error', 'already_ssl'}.
to_ssl_client(Socket) ->
to_ssl_client(Socket, []).
-spec to_ssl_client(Socket :: socket(), Options :: list()) ->
{'ok', ssl:sslsocket()} | {'error', 'already_ssl'}.
to_ssl_client(Socket, Options) ->
to_ssl_client(Socket, Options, infinity).
-spec to_ssl_client(
Socket :: socket(), Options :: list(), Timeout :: non_neg_integer() | 'infinity'
) -> {'ok', ssl:sslsocket()} | {'error', 'already_ssl'}.
to_ssl_client(Socket, Options, Timeout) when is_port(Socket) ->
ssl:connect(Socket, ssl_connect_options(Options), Timeout);
to_ssl_client(_Socket, _Options, _Timeout) ->
{error, already_ssl}.
-spec type(Socket :: socket()) -> protocol().
type(Socket) when is_port(Socket) ->
tcp;
type(_Socket) ->
ssl.
%%%-----------------------------------------------------------------
%%% Internal functions (OS_Mon configuration)
%%%-----------------------------------------------------------------
tcp_listen_options([Format | Options]) when Format =:= list; Format =:= binary ->
tcp_listen_options(Options, Format);
tcp_listen_options(Options) ->
tcp_listen_options(Options, list).
tcp_listen_options(Options, Format) ->
parse_address([Format | proplist_merge(Options, ?TCP_LISTEN_OPTIONS)]).
ssl_listen_options([Format | Options]) when Format =:= list; Format =:= binary ->
ssl_listen_options(Options, Format);
ssl_listen_options(Options) ->
ssl_listen_options(Options, list).
ssl_listen_options(Options, Format) ->
parse_address([Format | proplist_merge(Options, ?SSL_LISTEN_OPTIONS)]).
tcp_connect_options([Format | Options]) when Format =:= list; Format =:= binary ->
tcp_connect_options(Options, Format);
tcp_connect_options(Options) ->
tcp_connect_options(Options, list).
tcp_connect_options(Options, Format) ->
parse_address([Format | proplist_merge(Options, ?TCP_CONNECT_OPTIONS)]).
ssl_connect_options([Format | Options]) when Format =:= list; Format =:= binary ->
ssl_connect_options(Options, Format);
ssl_connect_options(Options) ->
ssl_connect_options(Options, list).
ssl_connect_options(Options, Format) ->
parse_address([Format | proplist_merge(Options, ?SSL_CONNECT_OPTIONS)]).
proplist_merge(PrimaryList, DefaultList) ->
{PrimaryTuples, PrimaryOther} = lists:partition(fun(X) -> is_tuple(X) end, PrimaryList),
{DefaultTuples, DefaultOther} = lists:partition(fun(X) -> is_tuple(X) end, DefaultList),
MergedTuples = lists:ukeymerge(
1,
lists:keysort(1, PrimaryTuples),
lists:keysort(1, DefaultTuples)
),
MergedOther = lists:umerge(lists:sort(PrimaryOther), lists:sort(DefaultOther)),
MergedTuples ++ MergedOther.
parse_address(Options) ->
case proplists:get_value(ip, Options) of
X when is_tuple(X) ->
Options;
X when is_list(X) ->
case inet_parse:address(X) of
{error, _} = Error ->
erlang:error(Error);
{ok, IP} ->
proplists:delete(ip, Options) ++ [{ip, IP}]
end;
_ ->
Options
end.
-spec extract_port_from_socket(Socket :: socket()) -> port().
extract_port_from_socket({sslsocket, _, {SSLPort, _}}) ->
SSLPort;
extract_port_from_socket(Socket) ->
Socket.
-spec set_sockopt(ListSock :: port(), CliSocket :: port()) -> 'ok' | any().
set_sockopt(ListenObject, ClientSocket) ->
ListenSocket = extract_port_from_socket(ListenObject),
true = inet_db:register_socket(ClientSocket, inet_tcp),
case prim_inet:getopts(ListenSocket, [active, nodelay, keepalive, delay_send, priority, tos]) of
{ok, Opts} ->
case prim_inet:setopts(ClientSocket, Opts) of
ok ->
ok;
Error ->
smtp_socket:close(ClientSocket),
Error
end;
Error ->
smtp_socket:close(ClientSocket),
Error
end.
-ifdef(TEST).
-define(TEST_PORT, 7586).
connect_test_() ->
[
{"listen and connect via tcp", fun() ->
Self = self(),
Port = ?TEST_PORT + 1,
Ref = make_ref(),
spawn(fun() ->
{ok, ListenSocket} = listen(tcp, Port),
?assert(is_port(ListenSocket)),
Self ! {Ref, listen},
{ok, ServerSocket} = accept(ListenSocket),
controlling_process(ServerSocket, Self),
Self ! {Ref, ListenSocket}
end),
receive
{Ref, listen} -> ok
end,
{ok, ClientSocket} = connect(tcp, "localhost", Port),
receive
{Ref, ListenSocket} when is_port(ListenSocket) -> ok
end,
?assert(is_port(ClientSocket)),
close(ListenSocket)
end},
{"listen and connect via ssl", fun() ->
Self = self(),
Port = ?TEST_PORT + 2,
Ref = make_ref(),
application:ensure_all_started(gen_smtp),
spawn(fun() ->
{ok, ListenSocket} = listen(ssl, Port, [
{keyfile, "test/fixtures/mx1.example.com-server.key"},
{certfile, "test/fixtures/mx1.example.com-server.crt"}
]),
?assertMatch([sslsocket | _], tuple_to_list(ListenSocket)),
Self ! {Ref, listen},
{ok, ServerSocket} = accept(ListenSocket),
controlling_process(ServerSocket, Self),
Self ! {Ref, ListenSocket}
end),
receive
{Ref, listen} -> ok
end,
{ok, ClientSocket} = connect(ssl, "localhost", Port, []),
receive
{Ref, {sslsocket, _, _} = ListenSocket} -> ok
end,
?assertMatch([sslsocket | _], tuple_to_list(ClientSocket)),
close(ListenSocket)
end}
].
evented_connections_test_() ->
[
{"current process receives connection to TCP listen sockets", fun() ->
Port = ?TEST_PORT + 3,
{ok, ListenSocket} = listen(tcp, Port),
begin_inet_async(ListenSocket),
spawn(fun() -> connect(tcp, "localhost", Port) end),
receive
{inet_async, ListenSocket, _, {ok, ServerSocket}} -> ok
end,
{ok, NewServerSocket} = handle_inet_async(ListenSocket, ServerSocket),
?assert(is_port(ServerSocket)),
%% only true for TCP
?assertEqual(ServerSocket, NewServerSocket),
?assert(is_port(ListenSocket)),
% Stop the async
spawn(fun() -> connect(tcp, "localhost", Port) end),
receive
_Ignored -> ok
end,
close(NewServerSocket),
close(ListenSocket)
end},
{"current process receives connection to SSL listen sockets", fun() ->
Port = ?TEST_PORT + 4,
application:ensure_all_started(gen_smtp),
{ok, ListenSocket} = listen(ssl, Port, [
{keyfile, "test/fixtures/mx1.example.com-server.key"},
{certfile, "test/fixtures/mx1.example.com-server.crt"}
]),
begin_inet_async(ListenSocket),
spawn(fun() -> connect(ssl, "localhost", Port) end),
receive
{inet_async, _ListenPort, _, {ok, ServerSocket}} -> ok
end,
{ok, NewServerSocket} = handle_inet_async(ListenSocket, ServerSocket, [
{keyfile, "test/fixtures/mx1.example.com-server.key"},
{certfile, "test/fixtures/mx1.example.com-server.crt"}
]),
?assert(is_port(ServerSocket)),
?assertMatch([sslsocket | _], tuple_to_list(NewServerSocket)),
?assertMatch([sslsocket | _], tuple_to_list(ListenSocket)),
%Stop the async
spawn(fun() -> connect(ssl, "localhost", Port) end),
receive
_Ignored -> ok
end,
close(ListenSocket),
close(NewServerSocket),
ok
end},
%% TODO: figure out if the following passes because
%% of an incomplete test case or if this really is
%% a magical feature where a single listener
%% can respond to either ssl or tcp connections.
{"current TCP listener receives SSL connection", fun() ->
Port = ?TEST_PORT + 5,
application:ensure_all_started(gen_smtp),
{ok, ListenSocket} = listen(tcp, Port),
begin_inet_async(ListenSocket),
spawn(fun() -> connect(ssl, "localhost", Port) end),
ServerSocket =
receive
{inet_async, _ListenPort, _, {ok, ServerSocket0}} -> ServerSocket0
end,
?assertMatch({ok, ServerSocket}, handle_inet_async(ListenSocket, ServerSocket)),
?assert(is_port(ListenSocket)),
?assert(is_port(ServerSocket)),
{ok, NewServerSocket} = to_ssl_server(ServerSocket, [
{certfile, "test/fixtures/mx1.example.com-server.crt"},
{keyfile, "test/fixtures/mx1.example.com-server.key"}
]),
?assertMatch([sslsocket | _], tuple_to_list(NewServerSocket)),
% Stop the async
spawn(fun() -> connect(ssl, "localhost", Port) end),
receive
_Ignored -> ok
end,
close(ListenSocket),
close(NewServerSocket)
end}
].
accept_test_() ->
[
{"Accept via tcp", fun() ->
Port = ?TEST_PORT + 6,
{ok, ListenSocket} = listen(tcp, Port, tcp_listen_options([])),
?assert(is_port(ListenSocket)),
spawn(fun() -> connect(ssl, "localhost", Port, tcp_connect_options([])) end),
{ok, ServerSocket} = accept(ListenSocket),
?assert(is_port(ListenSocket)),
close(ServerSocket),
close(ListenSocket)
end},
{"Accept via ssl", fun() ->
Port = ?TEST_PORT + 7,
application:ensure_all_started(gen_smtp),
{ok, ListenSocket} = listen(ssl, Port, [
{keyfile, "test/fixtures/mx1.example.com-server.key"},
{certfile, "test/fixtures/mx1.example.com-server.crt"}
]),
?assertMatch([sslsocket | _], tuple_to_list(ListenSocket)),
spawn(fun() -> connect(ssl, "localhost", Port) end),
accept(ListenSocket),
close(ListenSocket)
end}
].
type_test_() ->
[
{"a tcp socket returns 'tcp'", fun() ->
{ok, ListenSocket} = listen(tcp, ?TEST_PORT + 8),
?assertMatch(tcp, type(ListenSocket)),
close(ListenSocket)
end},
{"an ssl socket returns 'ssl'", fun() ->
application:ensure_all_started(gen_smtp),
{ok, ListenSocket} = listen(ssl, ?TEST_PORT + 9, [
{keyfile, "test/fixtures/mx1.example.com-server.key"},
{certfile, "test/fixtures/mx1.example.com-server.crt"}
]),
?assertMatch(ssl, type(ListenSocket)),
close(ListenSocket)
end}
].
active_once_test_() ->
[
{"socket is set to active:once on tcp", fun() ->
{ok, ListenSocket} = listen(tcp, ?TEST_PORT + 10, tcp_listen_options([])),
?assertEqual({ok, [{active, false}]}, inet:getopts(ListenSocket, [active])),
active_once(ListenSocket),
?assertEqual({ok, [{active, once}]}, inet:getopts(ListenSocket, [active])),
close(ListenSocket)
end},
{"socket is set to active:once on ssl", fun() ->
{ok, ListenSocket} = listen(
ssl,
?TEST_PORT + 11,
ssl_listen_options([
{keyfile, "test/fixtures/mx1.example.com-server.key"},
{certfile, "test/fixtures/mx1.example.com-server.crt"}
])
),
?assertEqual({ok, [{active, false}]}, ssl:getopts(ListenSocket, [active])),
active_once(ListenSocket),
?assertEqual({ok, [{active, once}]}, ssl:getopts(ListenSocket, [active])),
close(ListenSocket)
end}
].
option_test_() ->
[
{"tcp_listen_options has defaults", fun() ->
?assertEqual(
lists:sort([list | ?TCP_LISTEN_OPTIONS]), lists:sort(tcp_listen_options([]))
)
end},
{"tcp_connect_options has defaults", fun() ->
?assertEqual(
lists:sort([list | ?TCP_CONNECT_OPTIONS]), lists:sort(tcp_connect_options([]))
)
end},
{"ssl_listen_options has defaults", fun() ->
?assertEqual(
lists:sort([list | ?SSL_LISTEN_OPTIONS]), lists:sort(ssl_listen_options([]))
)
end},
{"ssl_connect_options has defaults", fun() ->
?assertEqual(
lists:sort([list | ?SSL_CONNECT_OPTIONS]), lists:sort(ssl_connect_options([]))
)
end},
{"tcp_listen_options defaults to list type", fun() ->
?assertEqual(
lists:sort([list | ?TCP_LISTEN_OPTIONS]),
lists:sort(tcp_listen_options([{active, false}]))
),
?assertEqual(
lists:sort([binary | ?TCP_LISTEN_OPTIONS]),
lists:sort(tcp_listen_options([binary, {active, false}]))
)
end},
{"tcp_connect_options defaults to list type", fun() ->
?assertEqual(
lists:sort([list | ?TCP_CONNECT_OPTIONS]),
lists:sort(tcp_connect_options([{active, false}]))
),
?assertEqual(
lists:sort([binary | ?TCP_CONNECT_OPTIONS]),
lists:sort(tcp_connect_options([binary, {active, false}]))
)
end},
{"ssl_listen_options defaults to list type", fun() ->
?assertEqual(
lists:sort([list | ?SSL_LISTEN_OPTIONS]),
lists:sort(ssl_listen_options([{active, false}]))
),
?assertEqual(
lists:sort([binary | ?SSL_LISTEN_OPTIONS]),
lists:sort(ssl_listen_options([binary, {active, false}]))
)
end},
{"ssl_connect_options defaults to list type", fun() ->
?assertEqual(
lists:sort([list | ?SSL_CONNECT_OPTIONS]),
lists:sort(ssl_connect_options([{active, false}]))
),
?assertEqual(
lists:sort([binary | ?SSL_CONNECT_OPTIONS]),
lists:sort(ssl_connect_options([binary, {active, false}]))
)
end},
{"tcp_listen_options merges provided proplist", fun() ->
?assertEqual(
[
list
| lists:keysort(1, [
{active, true},
{backlog, 30},
{ip, {0, 0, 0, 0}},
{keepalive, true},
{packet, 2},
{reuseaddr, true}
])
],
tcp_listen_options([{active, true}, {packet, 2}])
)
end},
{"tcp_connect_options merges provided proplist", fun() ->
?assertEqual(
lists:sort([
list,
{active, true},
{packet, 2},
{ip, {0, 0, 0, 0}},
{port, 0}
]),
lists:sort(tcp_connect_options([{active, true}, {packet, 2}]))
)
end},
{"ssl_listen_options merges provided proplist", fun() ->
?assertEqual(
[
list
| lists:keysort(1, [
{active, true},
{backlog, 30},
{certfile, "server.crt"},
{depth, 0},
{keepalive, true},
{keyfile, "server.key"},
{packet, 2},
{reuse_sessions, false},
{reuseaddr, true}
])
],
ssl_listen_options([{active, true}, {packet, 2}])
),
?assertEqual(
[
list
| lists:keysort(1, [
{active, false},
{backlog, 30},
{certfile, "../server.crt"},
{depth, 0},
{keepalive, true},
{keyfile, "../server.key"},
{packet, line},
{reuse_sessions, false},
{reuseaddr, true}
])
],
ssl_listen_options([{certfile, "../server.crt"}, {keyfile, "../server.key"}])
)
end},
{"ssl_connect_options merges provided proplist", fun() ->
?assertEqual(
lists:sort([
list,
{active, true},
{depth, 0},
{ip, {0, 0, 0, 0}},
{port, 0},
{packet, 2}
]),
lists:sort(ssl_connect_options([{active, true}, {packet, 2}]))
)
end}
].
ssl_upgrade_test_() ->
[
{"TCP connection can be upgraded to ssl", fun() ->
Self = self(),
Port = ?TEST_PORT + 12,
application:ensure_all_started(gen_smtp),
spawn(fun() ->
{ok, ListenSocket} = listen(tcp, Port),
Self ! listening,
{ok, ServerSocket} = accept(ListenSocket),
{ok, NewServerSocket} = smtp_socket:to_ssl_server(
ServerSocket,
[
{keyfile, "test/fixtures/mx1.example.com-server.key"},
{certfile, "test/fixtures/mx1.example.com-server.crt"}
]
),
Self ! {sock, NewServerSocket}
end),
receive
listening -> ok
end,
erlang:yield(),
{ok, ClientSocket} = connect(tcp, "localhost", Port),
?assert(is_port(ClientSocket)),
{ok, NewClientSocket} = to_ssl_client(ClientSocket),
?assertMatch([sslsocket | _], tuple_to_list(NewClientSocket)),
receive
{sock, NewServerSocket} -> ok
end,
?assertMatch({sslsocket, _, _}, NewServerSocket),
close(NewClientSocket),
close(NewServerSocket)
end},
{"SSL server connection can't be upgraded again", fun() ->
Self = self(),
Port = ?TEST_PORT + 13,
application:ensure_all_started(gen_smtp),
spawn(fun() ->
{ok, ListenSocket} = listen(ssl, Port, [
{keyfile, "test/fixtures/mx1.example.com-server.key"},
{certfile, "test/fixtures/mx1.example.com-server.crt"}
]),
Self ! listening,
{ok, ServerSocket} = accept(ListenSocket),
?assertMatch({error, already_ssl}, to_ssl_server(ServerSocket)),
close(ServerSocket)
end),
receive
listening -> ok
end,
erlang:yield(),
{ok, ClientSocket} = connect(ssl, "localhost", Port),
close(ClientSocket)
end},
{"SSL client connection can't be upgraded again", fun() ->
Self = self(),
Port = ?TEST_PORT + 14,
application:ensure_all_started(gen_smtp),
spawn(fun() ->
{ok, ListenSocket} = listen(ssl, Port, [
{keyfile, "test/fixtures/mx1.example.com-server.key"},
{certfile, "test/fixtures/mx1.example.com-server.crt"}
]),
Self ! listening,
{ok, ServerSocket} = accept(ListenSocket),
Self ! {sock, ServerSocket}
end),
receive
listening -> ok
end,
erlang:yield(),
{ok, ClientSocket} = connect(ssl, "localhost", Port),
receive
{sock, ServerSocket} -> ok
end,
?assertMatch({error, already_ssl}, to_ssl_client(ClientSocket)),
close(ClientSocket),
close(ServerSocket)
end}
].
-endif.