# # Copyright (C) 2006 SIPfoundry Inc. # Licensed by SIPfoundry under the LGPL license. # # Copyright (C) 2006 Pingtel Corp. # Licensed to SIPfoundry under a Contributor Agreement. # ############################################################################## # set up the load path $thisdir = File.dirname(__FILE__) $:.unshift($thisdir) $:.unshift(File.join($thisdir, "..")) $:.unshift(File.join($thisdir, "..", "..")) # application requires require 'test_helper' require 'utils' require 'exceptions' class UtilsTest < Test::Unit::TestCase def test_get_get_aor_from_header # test get_aor_from_header with a good input header = 'From: Alice ;tag=1928301774' assert_equal('From: Alice ', Utils.get_aor_from_header(header)) header = 'From: \"Alice:in;Wonder;land\" ;tag=1928301774' assert_equal('From: \"Alice:in;Wonder;land\" ', Utils.get_aor_from_header(header)) # another good input header = '' assert_equal('', Utils.get_aor_from_header(header)) # Test get_aor_from_header with a bad input: there is no tag. # Must raise an exception. assert_raise(BadSipHeaderException) do Utils.get_aor_from_header('From: Alice ') end # Test get_aor_from_header with a bad input: there is no tag. # Must not raise an exception because we tell it that a tag is not required. assert_nothing_thrown do Utils.get_aor_from_header('From: Alice ', false) end end def test_contact_host ip_contacts = ["\"Jorma; \\\"The sip: Man\\\" Kaukonen\";tag=\"1c32681\"", "\"Jorma:Kaukonen\"sip:187@10.1.1.170;tag=1c32681", "Joe ", "", "sip:187@10.1.1.170:1234", "sip:187@10.1.1.170", "sip:10.1.1.170" ] ip_contacts.each do |contact| assert_equal("10.1.1.170", Utils.contact_host(contact)) end name_contacts = ["\"Jorma%20Kaukonen\";tag=1c32681", "\"Jorma \\\"The Man\\\" Kaukonen\";tag=1c32681", "\"Jorma;:/,Kaukonen\"sip:187@test-example.com;tag=1c32681" ] name_contacts.each do |contact| assert_equal("test-example.com", Utils.contact_host(contact)) end assert_equal("test.exampl-1.exa2mple.com", Utils.contact_host("\"Joe\'s Place\" ")) end def test_remove_part_of_str_beginning_with_char str = "a.b.c" assert_equal("a", Utils.remove_part_of_str_beginning_with_char(str, ".")) assert_equal(str, Utils.remove_part_of_str_beginning_with_char(str, "^")) end def test_contact_without_params contact_with_no_params = '' assert_equal(contact_with_no_params, Utils.contact_without_params(contact_with_no_params)) contact_with_one_long_param = '' assert_equal('', Utils.contact_without_params(contact_with_one_long_param)) contact_with_lots_of_params = '' assert_equal('', Utils.contact_without_params(contact_with_lots_of_params)) contact_with_no_greater_than_sign_at_end = 'sip:alice@example.com;one;more;for;you;nineteen;for;me' assert_equal('sip:alice@example.com', Utils.contact_without_params(contact_with_no_greater_than_sign_at_end)) end end