Source

ocaml-sip / tests / Test_sip_types.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
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
(*********************************************************************************************************************
 * Copyrights (C) 2012 by Pawel Wieczorek <wieczyk at gmail>
 *  http://bitbucket.org/wieczyk/ocaml-sip
 *)

open Testdata_messages
open Batteries
open OUnit
open Sip_types
open Testdata_messages.Util

(*********************************************************************************************************************
 * Helpers
 *)

let assert_str_equal msg a b =
    assert_equal ~msg:msg ~printer:String.quote a b


let prepare_valid_case  title input output = (title, input, output)
let prepare_valid_case' title output = (String.quote title, title, output)
let prepare_invalid_case title failmsg input = (title, failmsg, input)
let prepare_invalid_case' title failmsg = (String.quote title, failmsg, title)

let combine_invalid_cases' combine failmsg (title1, _, input1) (title2, _, input2) =
    ("combine (" ^ title1 ^ ") with " ^ "(" ^ title2 ^ ")", failmsg, combine input1 input2)

module type Test_for_type_converters = sig

    type t

    val converters : (string -> t) * (t -> string)

    val valid_data : (string * string * t) list

    val invalid_data : (string * string * string) list

end

(*********************************************************************************************************************
 * Some data
 *)

module SomeData = struct

    let list_int32 =
        [ Int32.of_int 454
        ; Int32.of_int 34
        ; Int32.of_int 12345
        ]


end

(*********************************************************************************************************************
 * SIP version
 *)

module Test_sip_version = struct

    type t = sip_version

    let converters =
        (sip_version_of_string, string_of_sip_version)

    let valid_data =
        [ prepare_valid_case'
            "SIP/1.0"
            SIP_1_0

        ; prepare_valid_case' 
            "SIP/1.1"
            (UnknownSipVersion "1.1")

        ; prepare_valid_case' 
            "SIP/2.0"
            SIP_2_0

        ; prepare_valid_case'
            "SIP/2.1"
            (UnknownSipVersion "2.1")

        ; prepare_valid_case'
            "SIP/3.0"
            (UnknownSipVersion "3.0")
        ]

    let invalid_data =
        let prepare title = prepare_invalid_case' title "sip_version_of_string" in
        [ prepare "HTTP/1.0"
        ; prepare "HTTP/1.1"
        ; prepare "HTTP/2.0"
        ; prepare "SIPS/2.0"
        ; prepare "SIPS"
        ; prepare "SIPS/"
        ; prepare "SIP/"
        ; prepare "ELO320"
        ; prepare "fdsfds fsd fsdf sdf s"
        ]

end

(*********************************************************************************************************************
 *)

module Test_sip_request_method = struct

    type t = sip_request_method

    let converters =
        (sip_request_method_of_string, string_of_sip_request_method)

    let valid_data =
        [ prepare_valid_case'
            "INVITE"
            INVITE

        ; prepare_valid_case'
            "ACK"
            ACK

        ; prepare_valid_case'
            "PRACK"
            PRACK

        ; prepare_valid_case'
            "MESSAGE"
            MESSAGE

        ; prepare_valid_case'
            "REGISTER"
            REGISTER

        ; prepare_valid_case'
            "CANCEL"
            CANCEL

        ; prepare_valid_case'
            "OPTIONS"
            OPTIONS

        ; prepare_valid_case'
            "OPTION"
            (UnknownRequestMethod "OPTION")

        ; prepare_valid_case'
            "BYE"
            BYE

        ; prepare_valid_case'
            "PUBLISH"
            PUBLISH

        ; prepare_valid_case'
            "SUBSCRIBE"
            SUBSCRIBE

        ; prepare_valid_case'
            "NOTIFY"
            NOTIFY

        ; prepare_valid_case'
            "INFO"
            INFO

        ; prepare_valid_case'
            "REFER"
            REFER

        ; prepare_valid_case'
            "UPDATE"
            UPDATE

        ]

    let invalid_data =
        let prepare title = prepare_invalid_case' title "sip_request_method_of_string" in
        [ prepare "INVITE a"
        ; prepare "a b c"
        ; prepare " INVITE"
        ; prepare " a b c"
        ; prepare "INVITE "
        ; prepare "17"
        ; prepare " 18"
        ; prepare "19 "
        ; prepare " 20 "
        ] 


end

(*********************************************************************************************************************
 *)

module Test_sip_cseq = struct

    type t = sip_cseq

    let converters =
        (sip_cseq_of_string, string_of_sip_cseq)

    let valid_data =
        [ prepare_valid_case'
            "1234 INVITE"
            (SipCseq (Int32.of_int 1234, INVITE))

        ; prepare_valid_case'
            "1235 INVITE"
            (SipCseq (Int32.of_int 1235, INVITE))

        ; prepare_valid_case'
            "1235 REGISTER"
            (SipCseq (Int32.of_int 1235, REGISTER))

        ; prepare_valid_case'
            "1235 BYE"
            (SipCseq (Int32.of_int 1235, BYE))

        ; prepare_valid_case'
            "42 OPTIONS"
            (SipCseq (Int32.of_int 42, OPTIONS))

        ; prepare_valid_case'
            "45 OPTION"
            (SipCseq (Int32.of_int 45, UnknownRequestMethod "OPTION"))
        ]

    let invalid_data =
        let prepare title = prepare_invalid_case' title "sip_cseq_of_string" in
        [ prepare "OPTION"
        ; prepare "OPTION 42"
        ; prepare "12 42"
        ] 

end

(*********************************************************************************************************************
 *)

module Test_sip_call_id = struct

    type t = sip_call_id

    let converters =
        (sip_call_id_of_string, string_of_sip_call_id)

    let valid_data =
        [ prepare_valid_case'
            "f81d4fae-7dec-11d0-a765-00a0c91e6bf6@biloxi.com"
            ( SipCallId("f81d4fae-7dec-11d0-a765-00a0c91e6bf6@biloxi.com") )

        ; prepare_valid_case'
            "this is sparta"
            ( SipCallId("this is sparta") )
        ]

    let invalid_data =
        [
        ]

end

(*********************************************************************************************************************
 *)

module Test_sip_via = struct

    type t = sip_via

    let converters =
        (sip_via_of_string, string_of_sip_via)

    let valid_data =
        [
        ]

    let invalid_data =
        [
        ]

end

(*********************************************************************************************************************
 *)

module Test_sip_uri_scheme = struct

    type t = sip_uri_scheme

    let converters =
        (sip_uri_scheme_of_string, string_of_sip_uri_scheme)

    let valid_data =
        [ prepare_valid_case'
            "sip"
            URI_SCHEME_SIP

        ; prepare_valid_case'
            "sips"
            URI_SCHEME_SIPS

        ; prepare_valid_case'
            "tel"
            URI_SCHEME_TEL

        ; prepare_valid_case'
            "http"
            (UnknownUriScheme "http")

        ; prepare_valid_case'
            "https"
            (UnknownUriScheme "https")
        ]

    let invalid_data =
        let prepare title = prepare_invalid_case' title "sip_uri_scheme_of_string" in
        [ prepare "sip:"
        ; prepare "sips:"
        ; prepare "tel:"
        ; prepare "http:"
        ; prepare "https:"
        ; prepare "sip "
        ; prepare "si+p"
        ; prepare "s@ip "
        ; prepare "s*ip "
        ; prepare "sip a"
        ]

end

(*********************************************************************************************************************
 *)

module Test_sip_uri = struct

    type t = sip_uri

    let converters =
        (sip_uri_of_string, string_of_sip_uri)

    let valid_data =
        [
        ]

    let invalid_data =
        [
        ]

end

(*********************************************************************************************************************
 *)

module Test_sip_address = struct

    type t = sip_address

    let converters =
        (sip_address_of_string, string_of_sip_address)

    let valid_data =
        [
        ]

    let invalid_data =
        [
        ]

end

(*********************************************************************************************************************
 *)


(*********************************************************************************************************************
 * Test generators
 *)

let valid_conversion_test (input_conv, output_conv) (title, input, output) = 
    "valid conversion -- " ^ title >::
    begin fun () ->
        let input_converted  = input_conv input in
        let output_converted = output_conv output in
        assert_equal
            ~msg:"checking conversion from string"
            output input_converted;
        assert_equal
            ~msg:"checking conversion to string"
            ~printer:String.quote
            input  output_converted
    end

let invalid_conversion_test (input_conv, _)  (title, failmsg, input) =
    "invalid conversion -- " ^ title >::
     begin fun () ->
        let exn = Failure failmsg in
        assert_raises 
            exn (fun () -> input_conv input)
     end

let generate_valid_conversion_tests converters tests =
    List.map (valid_conversion_test converters) tests

let generate_invalid_conversion_tests converters tests =
    List.map (invalid_conversion_test converters) tests

let generate_tests_from_module m = 
    let module M = (val m : Test_for_type_converters) in
    List.concat
        [ generate_valid_conversion_tests M.converters M.valid_data
        ; generate_invalid_conversion_tests M.converters M.invalid_data
        ]

(*********************************************************************************************************************
 * Test suite
 *)

let test_suite = "Types" >::: List.concat
    [ generate_tests_from_module (module Test_sip_version        : Test_for_type_converters )
    ; generate_tests_from_module (module Test_sip_request_method : Test_for_type_converters )
    ; generate_tests_from_module (module Test_sip_cseq           : Test_for_type_converters )
    ; generate_tests_from_module (module Test_sip_call_id        : Test_for_type_converters )
    ; generate_tests_from_module (module Test_sip_via            : Test_for_type_converters )
    ; generate_tests_from_module (module Test_sip_uri_scheme     : Test_for_type_converters )
    ; generate_tests_from_module (module Test_sip_uri            : Test_for_type_converters )
    ; generate_tests_from_module (module Test_sip_address        : Test_for_type_converters )
    ]