Source

ocaml-sip / tests / Test_parser.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 assert_strs_equal msg a b =
    assert_equal ~msg:msg ~printer:(fun x -> "[" ^ String.concat "," (List.map String.quote x) ^ "]") a b

let _validate_field printer actual request revconv field extracted expected =
    assert_equal
        ~msg:("mime field " ^ field)
        ~printer:printer
        expected actual;

    assert_equal
        ~msg:("extracted field " ^ field)
        ~printer:printer
        expected extracted

let validate_extracted_field request conv revconv field =
    let actual_mime      = conv (request#header (String.lowercase field)) in
    let printer = String.quote -| revconv in
    _validate_field printer actual_mime request revconv field 

let validate_extracted_field_option request conv revconv field =
    let actual_mime      = Option.map conv (request#header_option (String.lowercase field)) in
    let printer          = Option.map_default (String.quote -| revconv) "None" in
    _validate_field printer actual_mime request revconv field 

let validate_extracted_field_all request conv revconv field =
    let actual_mime      = List.map conv (request#header_all (String.lowercase field)) in
    let printer          = String.concat "," -| List.map (String.quote -| revconv) in
    _validate_field printer actual_mime request revconv field 

(*********************************************************************************************************************
 * Checking message against description
 *)

let _validate_message message parsed_message = function
    | DescrMimeField (field, expected) ->
        begin try
            let actual = message#header (String.lowercase field) in
            assert_str_equal ("mime field " ^ field) expected actual
        with Not_found ->
            failwith ("Cannot find header " ^ field)
        end

    | DescrMimeFields (field, expected) ->
        let actual = message#header_all (String.lowercase field) in
        assert_strs_equal ("mime fields " ^ field) expected actual

    | DescrContentLength expected ->
        let extracted = message#content_length in
        validate_extracted_field message
            int_of_string string_of_int
            "content-length" extracted expected

    | DescrMaxForwards expected ->
        begin match parsed_message with
            | Sip_parser.Request request ->
                let extracted = request#max_forwards in
                validate_extracted_field message
                    int_of_string string_of_int
                    "max-forwards" extracted expected

            | _ ->
                failwith "Message is not REQUEST"

        end


    | DescrSipVersion expected ->
        let extracted = message#sip_version in
        assert_equal
            ~msg:"incorrect sip version"
            ~printer:string_of_sip_version
            expected extracted

    | DescrSipMethod expected ->
        begin match parsed_message with
            | Sip_parser.Request request ->
                let extracted = request#sip_method in
                assert_equal
                    ~msg:"incorrect sip method"
                    ~printer:string_of_sip_request_method
                    expected extracted

            | _ ->
                failwith "Message is not REQUEST"

        end

    | DescrUri expected ->
        begin match parsed_message with
            | Sip_parser.Request request ->
                let extracted = request#uri in
                assert_equal
                    ~msg:"incorrect reques uri"
                    ~printer:string_of_sip_uri
                    expected extracted

            | _ ->
                failwith "Message is not REQUEST"
        end

    | DescrCseq expected ->
        let extracted = message#cseq in
        validate_extracted_field message
            sip_cseq_of_string string_of_sip_cseq
            "cseq" extracted expected

    | DescrCallId expected ->
        let extracted = message#call_id in
        validate_extracted_field message
            sip_call_id_of_string string_of_sip_call_id
            "call-id" extracted expected

    | DescrTo expected ->
        let extracted = message#addr_to in
        validate_extracted_field message
            sip_address_of_string string_of_sip_address
            "to" extracted expected

    | DescrFrom expected ->
        let extracted = message#addr_from in
        validate_extracted_field message
            sip_address_of_string string_of_sip_address
            "from" extracted expected

    | DescrVia expected ->
        let extracted = message#vias in
        validate_extracted_field_all message
            sip_via_of_string string_of_sip_via
            "via" extracted expected

    | DescrContact expected ->
        let extracted = message#contact in
        validate_extracted_field_option message
            sip_address_of_string string_of_sip_address
            "contact" extracted expected

    | _ ->
        ()

let validate_message parsed_message =
    _validate_message (Sip_parser.sip_message_from_parsed parsed_message) parsed_message

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

let valid_message_parse_test (title, buffer, descr) = 
    "valid message -- " ^ title >::
    begin fun () ->
        let parsed_message = Sip_parser.parse_message buffer in
        List.iter (validate_message parsed_message) descr
    end

let invalid_message_parse_test (title, buffer) =
    "invalid message -- " ^ title >::
    begin fun () ->
        assert_raises Sip_parser.Invalid_sip_message
            (fun () -> Sip_parser.parse_message buffer)
    end

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

let test_suite = "Parser" >::: List.concat
    [ List.map valid_message_parse_test   Valid_messages.messages
    ; List.map invalid_message_parse_test Invalid_messages.messages
    ]
;;
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.