package async_smtp

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file stress_test.ml

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
open Core
open Async
open Async_smtp

let log =
  Lazy.force Async.Log.Global.log |> Smtp_mail_log.adjust_log_levels ~remap_info_to:`Debug
;;

module Config = struct
  type t =
    { dir : string
    ; host : string
    ; port : int
    ; tls : bool
    ; send_n_messages : int
    ; client_allowed_ciphers : [ `Secure | `Openssl_default | `Only of string list ]
    ; server_allowed_ciphers : [ `Secure | `Openssl_default | `Only of string list ]
    ; key_type : [ `rsa of int | `ecdsa of string | `dsa of int ]
    }
  [@@deriving fields ~getters, sexp]

  let make_tls_certificates t =
    let openssl args = Async_shell.run "openssl" args in
    let%bind () =
      openssl
        [ "req"
        ; "-new"
        ; "-x509" (* generate the request and sign in one step *)
        ; "-newkey"
        ; "rsa:512" (* generate key *)
        ; "-nodes" (* don't encrypt the key *)
        ; "-batch" (* non interactive *)
        ; "-keyout"
        ; t.dir ^/ "ca.key"
        ; "-out"
        ; t.dir ^/ "ca.crt"
        ; "-days"
        ; "1" (* short shelf life is good for testing *)
        ; "-subj"
        ; "/CN=stress-test-CA/"
        ]
    in
    let%bind () =
      match t.key_type with
      | `rsa bits ->
        openssl [ "genrsa"; "-out"; t.dir ^/ "server.key"; Int.to_string bits ]
      | `dsa bits ->
        openssl
          [ "dsaparam"; "-genkey"; "-out"; t.dir ^/ "server.key"; Int.to_string bits ]
      | `ecdsa curve ->
        openssl [ "ecparam"; "-name"; curve; "-genkey"; "-out"; t.dir ^/ "server.key" ]
    in
    let%bind () =
      openssl
        [ "req"
        ; "-new"
        ; "-key"
        ; t.dir ^/ "server.key"
        ; "-nodes" (* don't encrypt the key *)
        ; "-batch" (* non interactive *)
        ; "-out"
        ; t.dir ^/ "server.csr"
        ; "-subj"
        ; sprintf "/CN=%s/" t.host
        ]
    in
    let%map () =
      openssl
        [ "x509"
        ; "-req"
        ; "-days"
        ; "1" (* short shelf life is good for testing *)
        ; "-CA"
        ; t.dir ^/ "ca.crt"
        ; "-CAkey"
        ; t.dir ^/ "ca.key"
        ; "-in"
        ; t.dir ^/ "server.csr"
        ; "-out"
        ; t.dir ^/ "server.crt"
        ; "-set_serial"
        ; "1"
        ]
    in
    let server =
      { Smtp_server.Config.Tls_options.version = None
      ; options = None
      ; name = None
      ; crt_file = t.dir ^/ "server.crt"
      ; key_file = t.dir ^/ "server.key"
      ; ca_file = Some (t.dir ^/ "ca.crt")
      ; ca_path = None
      ; allowed_ciphers = t.server_allowed_ciphers
      }
    in
    let client =
      [ ( Smtp_client.Config.Domain_suffix.of_string t.host
        , { Smtp_client.Config.Tls.version = None
          ; options = None
          ; name = None
          ; ca_file = Some (t.dir ^/ "ca.crt")
          ; ca_path = None
          ; mode = `Required
          ; certificate_mode = `Verify
          ; allowed_ciphers = t.client_allowed_ciphers
          } )
      ; ( Smtp_client.Config.Domain_suffix.of_string ""
        , { Smtp_client.Config.Tls.version = None
          ; options = None
          ; name = None
          ; ca_file = None
          ; ca_path = None
          ; mode = `Required
          ; certificate_mode =
              `Verify (* Causes the message to fail if we have to wrong host *)
          ; allowed_ciphers = t.client_allowed_ciphers
          } )
      ]
    in
    server, client
  ;;

  let server_and_client_config ~concurrent_receivers t =
    let%bind tls_options =
      if t.tls
      then (
        let%map tls_options = make_tls_certificates t in
        Some tls_options)
      else return None
    in
    let spool_dir = t.dir ^/ "spool-not-used" in
    let%map () = Deferred.all_unit [ Unix.mkdir ~p:() spool_dir ] in
    let client =
      { Smtp_client.Config.greeting = Some "stress-test"
      ; tls = Option.value_map ~f:snd tls_options ~default:[]
      ; send_receive_timeout = `This (Time_float.Span.of_sec 2.)
      ; final_ok_timeout = `This (Time_float.Span.of_sec 5.)
      }
    in
    let server =
      { Smtp_server.Config.where_to_listen = [ Localhost_on_port t.port ]
      ; max_concurrent_receive_jobs_per_port = concurrent_receivers
      ; timeouts = Smtp_server.Config.Timeouts.default
      ; rpc_port = 0 (* not used *)
      ; malformed_emails = `Reject
      ; max_message_size = Byte_units.of_megabytes 1.
      ; tls_options = Option.map ~f:fst tls_options
      ; tcp_options = None
      }
    in
    server, client
  ;;
end

let counter = ref 0
let finished = Ivar.create ()
let throttle = ref (Throttle.create ~continue_on_error:true ~max_concurrent_jobs:1)

let send ~config ~client_config envelope =
  incr counter;
  let port = Config.port config in
  let host = Config.host config in
  don't_wait_for
    (Throttle.enqueue !throttle (fun () ->
       Deferred.Or_error.try_with_join ~run:`Schedule ~rest:`Log (fun () ->
         Smtp_client.Tcp.with_
           ~log:(Lazy.force Log.Global.log)
           (Host_and_port.create ~host ~port)
           ~config:client_config
           ~f:(fun client ->
           Smtp_client.send_envelope client ~log envelope
           >>|? Smtp_client.Envelope_status.ok_or_error ~allow_rejected_recipients:false
           >>| Or_error.join
           >>|? ignore)))
     >>| Result.iter_error ~f:(fun log_arg ->
           [%log.global.error_format !"buh???: %{Error#hum}" log_arg]))
;;

let main
  ~dir
  ~host
  ~port
  ~tls
  ~send_n_messages
  ~num_copies
  ~concurrent_senders
  ~concurrent_receivers
  ~message_from_stdin
  ~client_allowed_ciphers
  ~server_allowed_ciphers
  ~key_type
  ()
  =
  let config =
    { Config.dir
    ; host
    ; port
    ; tls
    ; send_n_messages
    ; client_allowed_ciphers
    ; server_allowed_ciphers
    ; key_type
    }
  in
  let%bind envelopes =
    if message_from_stdin
    then (
      let stdin = Lazy.force Reader.stdin in
      Smtp_server.read_bsmtp stdin |> Pipe.map ~f:Or_error.ok_exn |> Pipe.to_list)
    else (
      let recipients = [ Email_address.of_string_exn "test@example.com" ] in
      let email =
        Email.Simple.create
          ~from:(Async_smtp.Simplemail.local_address ())
          ~subject:"Stress test"
          ~to_:recipients
          (Email.Simple.Content.text_utf8 "Stress Test")
      in
      let sender = `Null in
      return [ Smtp_envelope.create ~sender ~recipients ~email () ])
  in
  let envelopes = List.init num_copies ~f:(fun _ -> envelopes) |> List.concat in
  throttle
    := Throttle.create ~continue_on_error:true ~max_concurrent_jobs:concurrent_senders;
  let%bind server_config, client_config =
    Config.server_and_client_config ~concurrent_receivers config
  in
  let module Server =
    Smtp_server.Make (struct
      open Smtp_monad.Let_syntax
      module State = Smtp_server.Plugin.Simple.State

      module Session = struct
        include Smtp_server.Plugin.Simple.Session

        let extensions ~state:_ _ =
          [ Smtp_server.Plugin.Extension.Start_tls
              (module struct
                type session = t

                let upgrade_to_tls ~log:_ t = return { t with tls = true }
              end : Smtp_server.Plugin.Start_tls
                with type session = t)
          ]
        ;;
      end

      module Envelope = struct
        include Smtp_server.Plugin.Simple.Envelope

        let process ~state:_ ~log:_ ~flows:_ _session t email =
          let envelope = smtp_envelope t email in
          if !counter >= Config.send_n_messages config
          then Ivar.fill_if_empty finished ()
          else send ~config ~client_config envelope;
          return (sprintf "stress-test:%d" !counter)
        ;;
      end

      let rpcs () = []
    end)
  in
  let%bind server =
    Server.start ~server_state:() ~log ~config:server_config >>| Or_error.ok_exn
  in
  List.iter envelopes ~f:(send ~config ~client_config);
  let%bind () = Ivar.read finished in
  (* Wait for all pending messages to clear *)
  let%bind () = Throttle.prior_jobs_done !throttle in
  let%bind () = Clock.after (sec 0.1) in
  match%bind Server.close server with
  | Error e -> Error.raise e
  | Ok () -> Deferred.return ()
;;

let cipher_list = Command.Arg_type.create (fun s -> `Only (String.split ~on:':' s))

let key_type =
  Command.Arg_type.create (fun s ->
    match String.split ~on:':' s with
    | [ "rsa"; bits ] -> `rsa (Int.of_string bits)
    | [ "dsa"; bits ] -> `dsa (Int.of_string bits)
    | [ "ecdsa"; curve ] -> `ecdsa curve
    | _ -> failwith "not a recognized key type. Supported rsa:BITS, dsa:BITS, ecdsa:CURVE")
;;

let command =
  let open Command.Let_syntax in
  Command.async
    ~summary:
      "Stress-test an smtp server by repeatedly sending and receiving a message read \
       from stdin"
    [%map_open
      let dir =
        flag "-dir" (optional string) ~doc:" Working dir"
        |> map ~f:(function
             | Some dir -> dir
             | None -> Core_unix.mkdtemp "/tmp/stress-test-")
      and host =
        flag
          "-host"
          (optional_with_default "localhost" string)
          ~doc:" Hostname to listen on"
      and port = flag "-port" (optional_with_default 2525 int) ~doc:" Port to listen on"
      and tls = flag "-tls" no_arg ~doc:" Run the stress test with TLS enabled"
      and send_n_messages =
        flag
          "-send-n-messages"
          ~aliases:[ "-n" ]
          (optional_with_default 1000 int)
          ~doc:" Number of messages to send"
      and num_copies =
        flag
          "-num-copies"
          (optional_with_default 1 int)
          ~doc:" Number of copies of each (the) message to have in circulation"
      and concurrent_senders =
        flag
          "-concurrent-senders"
          (optional_with_default 1 int)
          ~doc:" Number of concurrent senders"
      and concurrent_receivers =
        flag
          "-concurrent-receivers"
          (optional_with_default 1 int)
          ~doc:" Number of concurrent receivers"
      and message_from_stdin =
        flag
          "-message-from-stdin"
          no_arg
          ~doc:" Read the message from stdin, otherwise generate a simple message"
      and () =
        flag "-log-level" (optional Log.Level.arg) ~doc:" Log level"
        |> map ~f:(Option.iter ~f:Log.Global.set_level)
      and client_allowed_ciphers =
        flag
          "-client-allowed-ciphers"
          (optional_with_default `Secure cipher_list)
          ~doc:" Restrict client side SSL ciphers"
      and server_allowed_ciphers =
        flag
          "-server-allowed-ciphers"
          (optional_with_default `Secure cipher_list)
          ~doc:" Restrict server side SSL ciphers"
      and key_type =
        flag
          "-key-type"
          (optional_with_default (`rsa 2048) key_type)
          ~doc:" TLS Key type to use/generate"
      in
      fun () ->
        main
          ~dir
          ~host
          ~port
          ~tls
          ~send_n_messages
          ~num_copies
          ~concurrent_senders
          ~concurrent_receivers
          ~message_from_stdin
          ~client_allowed_ciphers
          ~server_allowed_ciphers
          ~key_type
          ()]
    ~behave_nicely_in_pipeline:false
;;
OCaml

Innovation. Community. Security.