ocaml-sip / tests / Test_sip_types.ml

(*********************************************************************************************************************
 * 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 =
        [
        ]

    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 = 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            : Test_for_type_converters )
    ; generate_tests_from_module (module Test_sip_address        : Test_for_type_converters )
    ]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.