2
0

torture_sip.c 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@ingroup sip_test @internal
  25. *
  26. * @CFILE torture_sip.c
  27. *
  28. * Unit-testing functions for SIP.
  29. *
  30. * @author Pekka Pessi <Pekka.Pessi@nokia.com>.
  31. *
  32. * @date Created: Tue Mar 6 18:33:42 2001 ppessi
  33. */
  34. #include "config.h"
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <stddef.h>
  38. #include <stdlib.h>
  39. /* Avoid casting sip_t to msg_pub_t and sip_header_t to msg_header_t */
  40. #define MSG_PUB_T struct sip_s
  41. #define MSG_HDR_T union sip_header_u
  42. #include <sofia-sip/su_types.h>
  43. #include <sofia-sip/su_tag.h>
  44. #include <sofia-sip/su_tag_class.h>
  45. #include <sofia-sip/su_tag_io.h>
  46. #include "sofia-sip/sip_parser.h"
  47. #include <sofia-sip/sip_util.h>
  48. #include <sofia-sip/sip_status.h>
  49. #include <sofia-sip/sip_tag.h>
  50. #include <sofia-sip/url_tag.h>
  51. #include <sofia-sip/msg_addr.h>
  52. #include <sofia-sip/msg_mclass.h>
  53. #include <sofia-sip/msg_mclass_hash.h>
  54. #include <sofia-sip/sip_extra.h>
  55. int tstflags;
  56. #define TSTFLAGS tstflags
  57. #include <sofia-sip/tstdef.h>
  58. char const *name = "torture_sip.c";
  59. msg_mclass_t *test_mclass = NULL;
  60. static msg_t *read_message(int flags, char const string[]);
  61. static int test_identity(void)
  62. {
  63. su_home_t *home;
  64. sip_alert_info_t *ai;
  65. sip_reply_to_t *rplyto;
  66. sip_remote_party_id_t *rpid;
  67. sip_p_asserted_identity_t *paid;
  68. sip_p_preferred_identity_t *ppid;
  69. msg_t *msg;
  70. sip_t *sip;
  71. BEGIN();
  72. msg_href_t const *href;
  73. msg_mclass_t const *def0, *def1, *ext;
  74. def0 = sip_default_mclass();
  75. /* Check that Refer-Sub has been added to our parser */
  76. TEST_1(href = msg_find_hclass(def0, "Refer-Sub", NULL));
  77. TEST_P(href->hr_class, sip_refer_sub_class);
  78. /* Check that Reply-To is not there */
  79. TEST_P(msg_find_hclass(def0, "Reply-To", NULL), def0->mc_unknown);
  80. TEST_1(ext = sip_extend_mclass(NULL));
  81. /* Update default parser */
  82. TEST_1(sip_update_default_mclass(ext) == 0);
  83. def1 = sip_default_mclass();
  84. TEST_1(def0 != def1);
  85. TEST_1(ext == def1);
  86. TEST_1(href = msg_find_hclass(def1, "Reply-To", NULL));
  87. TEST_P(href->hr_class, sip_reply_to_class);
  88. TEST_1(test_mclass = msg_mclass_clone(def0, 0, 0));
  89. msg = read_message(MSG_DO_EXTRACT_COPY,
  90. "BYE sip:foo@bar SIP/2.0\r\n"
  91. "To: <sip:foo@bar>;tag=deadbeef\r\n"
  92. "From: <sip:bar@foo>;\r\n"
  93. "Call-ID: 0ha0isndaksdj@10.1.2.3\r\n"
  94. "CSeq: 8 SUBSCRIBE\r\n"
  95. "Via: SIP/2.0/UDP 135.180.130.133\r\n"
  96. "Alert-Info: <http://test.com/tune>;walz, <http://test.com/buzz>\r\n"
  97. "Reply-To: Arska <sip:arska@gov.ca.us>;humppa\r\n"
  98. "P-Asserted-Identity: <sip:test@test.domain.com>\r\n"
  99. "P-Preferred-Identity: <sip:test@test.domain.com>, <tel:+358708008000>\r\n"
  100. "Remote-Party-ID: <sip:test2@test.domain.com>\r\n"
  101. "Content-Length: 0\r\n"
  102. "\r\n");
  103. sip = sip_object(msg);
  104. TEST_1(sip);
  105. TEST_1(!sip_alert_info(sip));
  106. TEST_1(!sip_reply_to(sip));
  107. TEST_1(!sip_p_asserted_identity(sip));
  108. TEST_1(!sip_p_preferred_identity(sip));
  109. TEST_1(!sip_remote_party_id(sip));
  110. msg_destroy(msg);
  111. TEST_1(msg_mclass_insert_header(test_mclass,
  112. sip_p_asserted_identity_class, 0) > 0);
  113. TEST_1(msg_mclass_insert_header(test_mclass,
  114. sip_p_preferred_identity_class, 0) > 0);
  115. msg = read_message(MSG_DO_EXTRACT_COPY,
  116. "BYE sip:foo@bar SIP/2.0\r\n"
  117. "To: <sip:foo@bar>;tag=deadbeef\r\n"
  118. "From: <sip:bar@foo>;\r\n"
  119. "Call-ID: 0ha0isndaksdj@10.1.2.3\r\n"
  120. "CSeq: 8 SUBSCRIBE\r\n"
  121. "Via: SIP/2.0/UDP 135.180.130.133\r\n"
  122. "Alert-Info: <http://test.com/tune>;walz, <http://test.com/buzz>\r\n"
  123. "Reply-To: Arska <sip:arska@gov.ca.us>;humppa\r\n"
  124. "P-Asserted-Identity: <sip:test@test.domain.com>\r\n"
  125. "P-Preferred-Identity: <sip:test@test.domain.com>, <tel:+358708008000>\r\n"
  126. "Remote-Party-ID: <sip:test2@test.domain.com>\r\n"
  127. "Content-Length: 0\r\n"
  128. "\r\n");
  129. sip = sip_object(msg);
  130. TEST_1(!sip_alert_info(sip));
  131. TEST_1(!sip_reply_to(sip));
  132. TEST_1(sip_p_asserted_identity(sip));
  133. TEST_1(sip_p_preferred_identity(sip));
  134. TEST_1(!sip_remote_party_id(sip));
  135. TEST_1(home = msg_home(msg));
  136. TEST_1((paid = sip_p_asserted_identity_make(home, "sip:joe@example.com")));
  137. TEST_1((paid = sip_p_asserted_identity_make
  138. (home, "Jaska <sip:joe@example.com>, Helmi <tel:+3587808000>")));
  139. TEST_1(paid->paid_next);
  140. TEST_1((ppid = sip_p_preferred_identity_make(home, "sip:joe@example.com")));
  141. TEST_1((ppid = sip_p_preferred_identity_make
  142. (home, "Jaska <sip:joe@example.com>, Helmi <tel:+3587808000>")));
  143. msg_destroy(msg);
  144. /* Now with extensions */
  145. TEST_1(test_mclass = msg_mclass_clone(def1, 0, 0));
  146. msg = read_message(MSG_DO_EXTRACT_COPY,
  147. "BYE sip:foo@bar SIP/2.0\r\n"
  148. "To: <sip:foo@bar>;tag=deadbeef\r\n"
  149. "From: <sip:bar@foo>;\r\n"
  150. "Call-ID: 0ha0isndaksdj@10.1.2.3\r\n"
  151. "CSeq: 8 SUBSCRIBE\r\n"
  152. "Via: SIP/2.0/UDP 135.180.130.133\r\n"
  153. "Alert-Info: <http://test.com/tune>;walz, <http://test.com/buzz>\r\n"
  154. "Reply-To: Arska <sip:arska@gov.ca.us>;humppa\r\n"
  155. "P-Asserted-Identity: <sip:test@test.domain.com>\r\n"
  156. "P-Preferred-Identity: <sip:test@test.domain.com>, <tel:+358708008000>\r\n"
  157. "Remote-Party-ID: <sip:test2@test.domain.com>\r\n"
  158. "Content-Length: 0\r\n"
  159. "\r\n");
  160. sip = sip_object(msg);
  161. TEST_1(ai = sip_alert_info(sip));
  162. TEST_S(ai->ai_url->url_host, "test.com");
  163. TEST_1(rplyto = sip_reply_to(sip));
  164. TEST_S(rplyto->rplyto_url->url_host, "gov.ca.us");
  165. TEST_1(paid = sip_p_asserted_identity(sip));
  166. TEST_1(ppid = sip_p_preferred_identity(sip));
  167. TEST_1(rpid = sip_remote_party_id(sip));
  168. TEST_S(rpid->rpid_url->url_host, "test.domain.com");
  169. msg_destroy(msg);
  170. {
  171. su_home_t *home = su_home_clone(NULL, sizeof *home);
  172. char *s;
  173. char const canonic[] =
  174. "\"Jaska Jokunen\" <sip:jaska.jokunen@example.com>;"
  175. "screen=yes;party=called;id-type=user;privacy=\"name,uri-network\"";
  176. char const canonic2[] =
  177. "Jaska Jokunen <sip:jaska.jokunen@example.com>;"
  178. "screen=yes;party=called;id-type=user;privacy=\"name,uri-network\"";
  179. sip_remote_party_id_t *rpid, *d;
  180. TEST_1(rpid = sip_remote_party_id_make(home, canonic));
  181. TEST_S(rpid->rpid_display, "\"Jaska Jokunen\"");
  182. TEST_S(rpid->rpid_url->url_user, "jaska.jokunen");
  183. TEST_S(rpid->rpid_params[0], "screen=yes");
  184. TEST_S(rpid->rpid_screen, "yes");
  185. TEST_S(rpid->rpid_party, "called");
  186. TEST_S(rpid->rpid_id_type, "user");
  187. TEST_S(rpid->rpid_privacy, "\"name,uri-network\"");
  188. TEST_1(s = sip_header_as_string(home, (void*)rpid));
  189. TEST_S(s, canonic);
  190. TEST_1(d = sip_remote_party_id_dup(home, rpid));
  191. TEST_S(d->rpid_display, rpid->rpid_display);
  192. TEST_S(d->rpid_params[0], rpid->rpid_params[0]);
  193. TEST_1(rpid = sip_remote_party_id_make(home, canonic2));
  194. TEST_S(rpid->rpid_display, "Jaska Jokunen");
  195. TEST_1(s = sip_header_as_string(home, (void*)rpid));
  196. TEST_S(s, canonic2);
  197. TEST_1(d = sip_remote_party_id_dup(home, rpid));
  198. TEST_S(d->rpid_display, rpid->rpid_display);
  199. su_home_check(home);
  200. su_home_zap(home);
  201. }
  202. END();
  203. }
  204. int test_url_headers(void)
  205. {
  206. BEGIN();
  207. su_home_t *home;
  208. char *s, *d;
  209. tagi_t *t;
  210. url_t *url;
  211. sip_from_t const *f;
  212. sip_accept_t const *ac;
  213. sip_payload_t const *body;
  214. sip_refer_sub_t rs[1];
  215. TEST_1(home = su_home_new(sizeof *home));
  216. sip_refer_sub_init(rs)->rs_value = "false";
  217. s = sip_headers_as_url_query
  218. (home,
  219. SIPTAG_SUBJECT_STR("kuik"),
  220. SIPTAG_REFER_SUB(rs),
  221. TAG_END());
  222. TEST_1(s);
  223. TEST_S(s, "subject=kuik&refer-sub=false");
  224. s = sip_headers_as_url_query
  225. (home,
  226. SIPTAG_TO_STR("\"Joe\" <sip:joe@example.com>;tag=foofaa"),
  227. SIPTAG_SUBJECT_STR("foo"),
  228. TAG_END());
  229. TEST_1(s);
  230. TEST_S(s, "to=%22Joe%22%20%3Csip%3Ajoe@example.com%3E%3Btag%3Dfoofaa"
  231. "&subject=foo");
  232. url = url_format(home, "sip:test@example.net?%s", s); TEST_1(url);
  233. TEST_S(url->url_headers, s);
  234. s = sip_headers_as_url_query
  235. (home,
  236. SIPTAG_FROM_STR("<sip:joe@example.com;user=ip>"),
  237. SIPTAG_ACCEPT_STR(""),
  238. SIPTAG_PAYLOAD_STR("hello"),
  239. SIPTAG_ACCEPT_STR(""),
  240. TAG_END());
  241. TEST_S(s, "from=%3Csip%3Ajoe@example.com%3Buser%3Dip%3E"
  242. "&accept="
  243. "&body=hello"
  244. "&accept=");
  245. d = url_query_as_header_string(home, s);
  246. TEST_S(d, "from:<sip:joe@example.com;user=ip>\n"
  247. "accept:\n"
  248. "accept:\n"
  249. "\n"
  250. "hello");
  251. t = sip_url_query_as_taglist(home, s, NULL); TEST_1(t);
  252. TEST_P(t[0].t_tag, siptag_from); TEST_1(f = (void *)t[0].t_value);
  253. TEST_P(t[1].t_tag, siptag_accept); TEST_1(ac = (void *)t[1].t_value);
  254. TEST_P(t[2].t_tag, siptag_payload); TEST_1(body = (void *)t[2].t_value);
  255. TEST_P(t[3].t_tag, siptag_accept);
  256. s = "xyzzy=foo";
  257. t = sip_url_query_as_taglist(home, s, NULL); TEST_1(t);
  258. TEST_P(t[0].t_tag, siptag_header_str);
  259. TEST_1(d = (void *)t[0].t_value);
  260. TEST_S(d, "foo");
  261. TEST_1(!sip_headers_as_url_query(home, SIPTAG_SEPARATOR_STR(""), TAG_END()));
  262. TEST_VOID(su_home_unref(home));
  263. END();
  264. }
  265. int test_manipulation(void)
  266. {
  267. BEGIN();
  268. sip_content_length_t *l;
  269. sip_payload_t *pl;
  270. msg_t *msg, *msg0;
  271. sip_t *sip;
  272. msg0 = read_message(MSG_DO_EXTRACT_COPY,
  273. "MESSAGE sip:foo@bar SIP/2.0\r\n"
  274. "To: Joe User <sip:foo@bar>\r\n"
  275. "From: \"Bar Owner\" <sip:bar@foo>;tag=foobar\r\n"
  276. "Call-ID: 0ha0isndaksdj@10.1.2.3\r\n"
  277. "CSeq: 8 MESSAGE\r\n"
  278. "Via: SIP/2.0/UDP 135.180.130.133\r\n"
  279. "Content-Length: 7\r\n"
  280. "Content-Type: text/plain\r\n"
  281. "\r\n"
  282. "Heippa!");
  283. TEST_1(msg0);
  284. TEST_1(msg = msg_copy(msg0));
  285. TEST_1(sip = sip_object(msg));
  286. TEST_1(l = sip_content_length_make(msg_home(msg), "6"));
  287. TEST_1(pl = sip_payload_make(msg_home(msg), "hello!"));
  288. TEST_1(msg_header_replace(msg, NULL,
  289. (void *)sip->sip_content_length,
  290. (void *)l) >= 0);
  291. TEST_1(msg_header_replace(msg, NULL,
  292. (void *)sip->sip_payload,
  293. (void *)pl) >= 0);
  294. TEST(msg_serialize(msg, NULL), 0);
  295. TEST_1(msg_prepare(msg) > 0);
  296. msg_destroy(msg);
  297. msg_destroy(msg0);
  298. END();
  299. }
  300. int test_methods(void)
  301. {
  302. int i;
  303. char name[32];
  304. BEGIN();
  305. for (i = 1; sip_method_names[i]; i++) {
  306. TEST_S(sip_method_names[i], sip_method_name(i, "foo"));
  307. }
  308. {
  309. char version[] = "protocol / version ";
  310. char *end = version + strlen(version);
  311. char *s = version;
  312. char const *result = NULL;
  313. TEST(sip_version_d(&s, &result), 0);
  314. TEST_P(s, end);
  315. TEST_S(result, "protocol/version");
  316. }
  317. {
  318. char udp[] = "SIP/ 2.0 / udp";
  319. char tcp[] = "SIP / 2.0 / tcp";
  320. char tls[] = "SIP / 2.0 / tls";
  321. char sctp[] = "SIP / 2.0 / scTp";
  322. char dtls[] = "SIP/2.0/TLS-UDP";
  323. char tls_sctp[] = "SIP/2.0/TLS-SCTP";
  324. char *s, *end;
  325. char const *result = NULL;
  326. s = udp; end = s + strlen(s);
  327. TEST_SIZE(sip_transport_d(&s, &result), 0); TEST_P(s, end);
  328. TEST_S(result, sip_transport_udp);
  329. s = tcp; end = s + strlen(s);
  330. TEST_SIZE(sip_transport_d(&s, &result), 0); TEST_P(s, end);
  331. TEST_S(result, sip_transport_tcp);
  332. s = tls; end = s + strlen(s);
  333. TEST_SIZE(sip_transport_d(&s, &result), 0); TEST_P(s, end);
  334. TEST_S(result, sip_transport_tls);
  335. s = sctp; end = s + strlen(s);
  336. TEST_SIZE(sip_transport_d(&s, &result), 0); TEST_P(s, end);
  337. TEST_S(result, sip_transport_sctp);
  338. s = dtls; end = s + strlen(s);
  339. TEST_SIZE(sip_transport_d(&s, &result), 0); TEST_P(s, end);
  340. TEST_S(result, "SIP/2.0/TLS-UDP");
  341. s = tls_sctp; end = s + strlen(s);
  342. TEST_SIZE(sip_transport_d(&s, &result), 0); TEST_P(s, end);
  343. TEST_S(result, "SIP/2.0/TLS-SCTP");
  344. }
  345. END();
  346. }
  347. /* Test <sip_basic.c> functions. */
  348. int test_basic(void)
  349. {
  350. su_home_t *home = su_home_new(sizeof *home);
  351. BEGIN();
  352. TEST_1(home);
  353. {
  354. sip_request_t *rq, *rq1;
  355. rq = sip_request_make(home, "INVITE sip:joe@example.com SIP/2.1");
  356. TEST_1(rq);
  357. TEST(rq->rq_method, sip_method_invite);
  358. TEST_S(rq->rq_method_name, "INVITE");
  359. TEST_1(rq1 = sip_request_dup(home, rq));
  360. su_free(home, rq);
  361. su_free(home, rq1);
  362. rq = sip_request_make(home, "invite sip:joe@example.com SIP/2.0");
  363. TEST_1(rq);
  364. TEST(rq->rq_method, sip_method_unknown);
  365. TEST_S(rq->rq_method_name, "invite");
  366. TEST_1(rq1 = sip_request_dup(home, rq));
  367. su_free(home, rq);
  368. su_free(home, rq1);
  369. TEST_1(!sip_request_create(home, sip_method_unknown, NULL,
  370. (void *)"sip:joe@example.com", NULL));
  371. TEST_1(rq = sip_request_create(home, sip_method_unknown, "invite",
  372. (void *)"sip:joe@example.com", NULL));
  373. TEST(rq->rq_method, sip_method_unknown);
  374. TEST_S(rq->rq_method_name, "invite");
  375. su_free(home, rq);
  376. TEST_1(rq = sip_request_create(home, sip_method_unknown, "INVITE",
  377. (void *)"sip:joe@example.com", NULL));
  378. TEST(rq->rq_method, sip_method_invite);
  379. TEST_S(rq->rq_method_name, "INVITE");
  380. su_free(home, rq);
  381. TEST_1(rq = sip_request_create(home, sip_method_invite, "foobar",
  382. (void *)"sip:joe@example.com", NULL));
  383. TEST(rq->rq_method, sip_method_invite);
  384. TEST_S(rq->rq_method_name, "INVITE");
  385. su_free(home, rq);
  386. }
  387. {
  388. sip_status_t *st;
  389. TEST_1(st = sip_status_make(home, "SIP/2.0 200 OK"));
  390. su_free(home, st);
  391. TEST_1(st = sip_status_make(home, "SIP/2.0 200"));
  392. su_free(home, st);
  393. TEST_1(!sip_status_make(home, "SIP2.0 200 OK"));
  394. TEST_1(!sip_status_create(home, 99, NULL, "SIP/2.1"));
  395. TEST_1(!sip_status_create(home, 700, NULL, "SIP/2.1"));
  396. TEST_1(st = sip_status_create(home, 200, "Ok", "SIP/2.2"));
  397. su_free(home, st);
  398. TEST_1(st = sip_status_create(home, 200, NULL, "SIP/2.0"));
  399. su_free(home, st);
  400. TEST_1(st = sip_status_create(home, 200, NULL, NULL));
  401. su_free(home, st);
  402. TEST_1(st = sip_status_create(home, 699, NULL, NULL));
  403. su_free(home, st);
  404. }
  405. {
  406. sip_payload_t *pl;
  407. TEST_1(pl = sip_payload_create(home, "foo", 3));
  408. su_free(home, pl);
  409. TEST_1(pl = sip_payload_create(home, NULL, 3));
  410. su_free(home, pl);
  411. }
  412. {
  413. sip_separator_t *sep;
  414. TEST_1(!sip_separator_make(home, "foo"));
  415. TEST_1(sep = sip_separator_create(home));
  416. su_free(home, sep);
  417. }
  418. /* Test name-addr things */
  419. {
  420. su_home_t home[1] = { SU_HOME_INIT(home) };
  421. char const *display;
  422. url_t url[1];
  423. msg_param_t const *params;
  424. char const *comment;
  425. char const na[] = "Raaka Arska <tel:+358501970>;param=1;humppa (test) ";
  426. char const na2[] = "tel:+358501970;param=1;humppa (test) ";
  427. char *s, buf[sizeof(na)], ebuf[sizeof(na) + 32];
  428. s = strcpy(buf, na);
  429. TEST_1(sip_name_addr_d(home, &s, &display, url, &params, &comment) >= 0);
  430. TEST_P(s, buf + strlen(na));
  431. TEST_1(display);
  432. TEST(url->url_type, url_tel);
  433. TEST_1(params);
  434. TEST_1(comment);
  435. TEST_SIZE(sip_name_addr_e(ebuf, sizeof(ebuf), 0, display, 0, url,
  436. params, comment),
  437. strlen(na) - 1);
  438. TEST_1(strncmp(na, ebuf, strlen(na) - 1) == 0);
  439. s = strcpy(buf, na2);
  440. TEST_1(sip_name_addr_d(home, &s, &display, url, &params, &comment) >= 0);
  441. TEST_S(s, "");
  442. TEST_P(s, buf + strlen(na2));
  443. TEST_1(!display);
  444. TEST(url->url_type, url_tel);
  445. TEST_1(params);
  446. TEST_1(comment);
  447. su_home_deinit(home);
  448. }
  449. {
  450. sip_from_t *f; sip_to_t *t, *t2;
  451. TEST_1(f = sip_from_create(home, (void *)"sip:joe@bar"));
  452. TEST_1(sip_from_add_param(home, f, NULL) == -1);
  453. TEST_1(sip_from_add_param(home, f, "tag=tagged") == 0);
  454. TEST_S(f->a_tag, "tagged");
  455. TEST_1(sip_from_tag(home, f, "jxahudsf") == -1);
  456. while (f->a_params && f->a_params[0])
  457. msg_header_remove_param(f->a_common, f->a_params[0]);
  458. TEST_P(f->a_tag, NULL);
  459. TEST_1(sip_from_add_param(home, f, "test=1") == 0);
  460. TEST_1(sip_from_tag(home, f, "jxahudsf") == 0);
  461. TEST_S(f->a_tag, "jxahudsf");
  462. su_free(home, f);
  463. TEST_1(!sip_from_create(home, (void *)"sip:joe@[baa"));
  464. TEST_1(!sip_from_make(home, (void *)"tester <>;tag=fasjfuios"));
  465. TEST_1(f = sip_from_make(home, (void *)"sip:joe@bar (foo)"));
  466. su_free(home, f);
  467. TEST_1(f = sip_from_make(home, (void *)"<sip:joe@bar;tag=bar> (joe)"));
  468. TEST_1(sip_from_tag(home, f, "tag=jxahudsf") == 0);
  469. su_free(home, f);
  470. TEST_1(f = sip_from_create(home, (void *)"<sip:joe@bar;tag=bar> (joe)"));
  471. TEST_1(sip_is_from((sip_header_t*)f));
  472. su_free(home, f);
  473. TEST_1(t = sip_to_create(home, (void *)"<sip:joe@bar;tag=bar> (joe)"));
  474. TEST_1(sip_is_to((sip_header_t*)t));
  475. TEST_1(sip_to_tag(home, t, "tag=jxahudsf") == 0);
  476. TEST_S(t->a_tag, "jxahudsf");
  477. TEST(msg_header_replace_param(home, t->a_common, "tag=bar"), 1);
  478. TEST_S(t->a_tag, "bar");
  479. TEST_1(t2 = sip_to_dup(home, t));
  480. TEST_S(t2->a_tag, "bar");
  481. TEST(msg_header_remove_param(t->a_common, "tag"), 1);
  482. TEST_P(t->a_tag, NULL);
  483. TEST_1(sip_to_add_param(home, t, "tst=1") == 0);
  484. TEST_P(t->a_tag, NULL);
  485. su_free(home, t);
  486. }
  487. {
  488. sip_call_id_t *i, *i0;
  489. TEST_1(i = sip_call_id_create(home, "example.com"));
  490. i->i_hash = 0;
  491. TEST_1(i0 = sip_call_id_dup(home, i));
  492. su_free(home, i);
  493. TEST_1(i = sip_call_id_make(home, i0->i_id));
  494. TEST(i->i_hash, i0->i_hash);
  495. su_free(home, i);
  496. su_free(home, i0);
  497. }
  498. {
  499. sip_cseq_t *cs, *cs0;
  500. TEST_1(cs = sip_cseq_create(home, 123456789, sip_method_invite, "1nvite"));
  501. TEST(cs->cs_seq, 123456789);
  502. TEST(cs->cs_method, sip_method_invite);
  503. TEST_S(cs->cs_method_name, "INVITE");
  504. su_free(home, cs);
  505. TEST_1(cs = sip_cseq_create(home, 123456789, sip_method_invite, NULL));
  506. TEST(cs->cs_seq, 123456789);
  507. TEST(cs->cs_method, sip_method_invite);
  508. TEST_S(cs->cs_method_name, "INVITE");
  509. TEST_1(cs0 = sip_cseq_dup(home, cs));
  510. su_free(home, cs);
  511. su_free(home, cs0);
  512. TEST_1(!sip_cseq_create(home, 123456789, sip_method_unknown, NULL));
  513. TEST_1(cs = sip_cseq_create(home, 123456789, sip_method_unknown,
  514. "invite"));
  515. TEST(cs->cs_seq, 123456789);
  516. TEST(cs->cs_method, sip_method_unknown);
  517. TEST_S(cs->cs_method_name, "invite");
  518. TEST_1(cs0 = sip_cseq_dup(home, cs));
  519. su_free(home, cs);
  520. su_free(home, cs0);
  521. }
  522. {
  523. sip_contact_t *m, *m0;
  524. TEST_1(!sip_contact_make(home, ",,"));
  525. TEST_1(m = sip_contact_create(home, (void *)"sip:joe@bar",
  526. "q=0.2",
  527. "+message",
  528. NULL));
  529. TEST_S(m->m_q, "0.2");
  530. TEST_1(m0 = sip_contact_dup(home, m));
  531. TEST_1(sip_contact_add_param(home, m, "q=0.5") >= 0);
  532. TEST_1(sip_contact_add_param(home, m, "video=FALSE") >= 0);
  533. TEST_1(sip_contact_add_param(home, m, NULL) == -1);
  534. TEST_1(sip_contact_add_param(home, NULL, "video=FALSE") == -1);
  535. TEST_1(sip_contact_add_param(home, m, "audio=FALSE") == 0);
  536. TEST_1(sip_contact_add_param(home, m, "expires=0") == 0);
  537. TEST_S(m->m_q, "0.5");
  538. TEST_S(m->m_expires, "0");
  539. TEST_1(!sip_contact_create(home, (void *)"sip:joe@[baa",
  540. "audio", "video", NULL));
  541. TEST_1(sip_header_format(home, sip_contact_class, "*"));
  542. su_free(home, m);
  543. su_free(home, m0);
  544. }
  545. {
  546. sip_via_t *v;
  547. char *s;
  548. v = sip_via_make(home, "SIP/2.0/UDP domain.invalid:5060"); TEST_1(v);
  549. s = sip_contact_string_from_via(home, v, NULL, v->v_protocol);
  550. TEST_S(s, "<sip:domain.invalid;transport=udp>");
  551. su_free(home, v), su_free(home, s);
  552. TEST_1(sip_transport_has_tls("SIP/2.0/TLS-SCTP"));
  553. TEST_1(sip_transport_has_tls("TLS-UDP"));
  554. v = sip_via_make(home, "SIP/2.0/TLS-SCTP domain.invalid"); TEST_1(v);
  555. s = sip_contact_string_from_via(home, v, NULL, v->v_protocol);
  556. TEST_S(s, "<sips:domain.invalid;transport=tls-sctp>");
  557. su_free(home, v), su_free(home, s);
  558. }
  559. {
  560. char *input;
  561. char const *output = NULL;
  562. char udp[] = "sip/2.0/udp";
  563. char tcp[] = "sip/2.0/tCp ";
  564. char sctp[] = "sip/2.0/sctp\t";
  565. char tls[] = "sip/2.0/tls\r";
  566. input = udp;
  567. TEST(sip_transport_d(&input, &output), 0);
  568. TEST_S(output, "SIP/2.0/UDP");
  569. input = tcp;
  570. TEST(sip_transport_d(&input, &output), 0);
  571. TEST_S(output, "SIP/2.0/TCP");
  572. input = sctp;
  573. TEST(sip_transport_d(&input, &output), 0);
  574. TEST_S(output, "SIP/2.0/SCTP");
  575. input = tls;
  576. TEST(sip_transport_d(&input, &output), 0);
  577. TEST_S(output, "SIP/2.0/TLS");
  578. }
  579. {
  580. sip_expires_t *ex;
  581. TEST_1(!sip_expires_make(home, "-12+1"));
  582. TEST_1(ex = sip_expires_make(home, "4294967297")); /* XXX */
  583. su_free(home, ex);
  584. TEST_1(ex = sip_expires_make(home, "Thu, 25 Mar 2004 14:49:29 GMT"));
  585. su_free(home, ex);
  586. TEST_1(ex = sip_expires_create(home, 3600));
  587. su_free(home, ex);
  588. }
  589. {
  590. sip_retry_after_t *ra;
  591. char const *s;
  592. TEST_1(!(ra = sip_retry_after_make(home, "50 (foo")));
  593. TEST_1(ra = sip_retry_after_make(home, "50 (foo) ; duration = 13"));
  594. TEST_S(ra->af_duration, "13");
  595. TEST_S(ra->af_comment, "foo");
  596. TEST(msg_header_remove_param(ra->af_common, "duration"), 1);
  597. TEST_P(ra->af_duration, NULL);
  598. s = sip_header_as_string(home, (void*)ra);
  599. TEST_S(s, "50 (foo)");
  600. TEST(msg_header_add_param(home, ra->af_common, "x=z"), 0);
  601. s = sip_header_as_string(home, (void*)ra);
  602. TEST_S(s, "50 (foo) ;x=z");
  603. su_free(home, ra);
  604. }
  605. {
  606. sip_date_t *d;
  607. TEST_1(!(d = sip_date_make(home, "Mon, 30 Feb 1896 23:59:59 GMT")));
  608. su_free(home, d);
  609. TEST_1(d = sip_date_create(home, (1<<30)));
  610. su_free(home, d);
  611. TEST_1(d = sip_date_create(home, 0));
  612. TEST_1(d->d_time != 0);
  613. su_free(home, d);
  614. }
  615. {
  616. sip_route_t *r, *r0;
  617. TEST_1(!sip_route_make(home, "<sip:foo@[bar:50>;lr"));
  618. TEST_1(r = sip_route_make(home, "<sip:foo@[baa::1]:5060>;lr"));
  619. TEST_1(r0 = sip_route_dup(home, r));
  620. TEST_1(sip_route_fix(r));
  621. TEST_1(url_has_param(r->r_url, "lr"));
  622. su_free(home, r);
  623. TEST_1(r = sip_route_create(home, r0->r_url, r0->r_url));
  624. su_free(home, r); su_free(home, r0);
  625. }
  626. {
  627. sip_record_route_t *r, *r0;
  628. TEST_1(!sip_record_route_make(home, "<sip:foo@[bar:50>;lr"));
  629. TEST_1(!sip_record_route_make(home, "<sip:foo@[baa::1]>;lr bar, sip:foo"));
  630. TEST_1(r = sip_record_route_make(home, "<sip:foo@[baa::1]:5060>;lr"));
  631. TEST_1(r0 = sip_record_route_dup(home, r));
  632. su_free(home, r);
  633. TEST_1(r = sip_route_create(home, r0->r_url, r0->r_url));
  634. su_free(home, r), su_free(home, r0);
  635. }
  636. {
  637. sip_via_t *v, *v0;
  638. TEST_1(!sip_via_make(home, ",,"));
  639. TEST_1(!sip_via_make(home, "SIP// host:5060 (foo),"));
  640. TEST_1(!sip_via_make(home, "SIP/2.0/TCP host:5060 (foo) bar,"));
  641. TEST_1(!sip_via_make(home, "SIP/2.0/TCP [3ffe::1:5060 (foo),"));
  642. TEST_1(v = sip_via_create(home, "bar.com",
  643. "50600",
  644. "SIP/2.0/UDP",
  645. "hidden",
  646. "rport=50601",
  647. "comp=sigcomp",
  648. "branch=1",
  649. "q=0.2",
  650. NULL));
  651. TEST_S(v->v_branch, "1");
  652. TEST_S(v->v_rport, "50601");
  653. TEST_S(v->v_comp, "sigcomp");
  654. TEST_1(v = sip_via_make(home, "SIP/2.0/UDP bar.com:50600"
  655. " ;hidden;rport=50601;comp=sigcomp;branch=1;ttl=15"
  656. " ; maddr=[::227.0.0.1]"
  657. " (This is a comment) "));
  658. TEST_S(v->v_ttl, "15");
  659. TEST_S(v->v_maddr, "[::227.0.0.1]");
  660. TEST_S(v->v_branch, "1");
  661. TEST_S(v->v_rport, "50601");
  662. TEST_S(v->v_comp, "sigcomp");
  663. TEST_1(v0 = sip_via_dup(home, v));
  664. TEST(msg_header_add_param(home, v->v_common, "rport"), 0);
  665. TEST_S(v->v_rport, "");
  666. TEST(msg_header_remove_param(v->v_common, "comp"), 1);
  667. TEST_P(v->v_comp, NULL);
  668. TEST(msg_header_remove_param(v->v_common, "ttl"), 1);
  669. TEST_P(v->v_ttl, NULL);
  670. TEST(msg_header_remove_param(v->v_common, "maddr"), 1);
  671. TEST_P(v->v_maddr, NULL);
  672. TEST(msg_header_remove_param(v->v_common, "rport"), 1);
  673. TEST_P(v->v_rport, NULL);
  674. TEST(msg_header_remove_param(v->v_common, "branch"), 1);
  675. TEST_P(v->v_branch, NULL);
  676. TEST_1(sip_via_add_param(home, v, "video=FALSE") == 0);
  677. TEST_1(sip_via_add_param(home, v, NULL) == -1);
  678. TEST_1(sip_via_add_param(home, NULL, "video=FALSE") == -1);
  679. TEST_1(sip_via_add_param(home, v, "audio=FALSE") == 0);
  680. TEST_1(sip_via_add_param(home, v, "branch=0") == 0);
  681. su_free(home, v);
  682. su_free(home, v0);
  683. TEST_1(v = sip_via_create(home, "bar.com",
  684. "50600",
  685. NULL,
  686. "rport=50601",
  687. "branch=1",
  688. "q=0.2",
  689. NULL));
  690. TEST_S(v->v_protocol, "SIP/2.0/UDP");
  691. su_free(home, v);
  692. }
  693. {
  694. sip_call_info_t *ci, *ci0;
  695. TEST_1(ci = sip_call_info_make(home,
  696. "<http://www.nokia.com>;purpose=info"));
  697. TEST_S(ci->ci_purpose, "info");
  698. TEST_1(ci0 = sip_call_info_dup(home, ci));
  699. TEST_S(ci0->ci_purpose, "info");
  700. TEST_1(ci->ci_purpose != ci0->ci_purpose);
  701. TEST(msg_header_remove_param(ci->ci_common, "purpose"), 1);
  702. TEST_P(ci->ci_purpose, NULL);
  703. su_free(home, ci);
  704. su_free(home, ci0);
  705. }
  706. {
  707. sip_alert_info_t *ai, *ai0;
  708. TEST_1(ai = sip_alert_info_make(home, "<http://www.nokia.com/ringtone.mp3>;x-format=mp3"));
  709. TEST_1(ai0 = sip_alert_info_dup(home, ai));
  710. TEST(msg_header_remove_param(ai->ai_common, "x-format"), 1);
  711. TEST(msg_header_remove_param(ai0->ai_common, "x-format"), 1);
  712. su_free(home, ai);
  713. su_free(home, ai0);
  714. }
  715. {
  716. sip_reply_to_t *rplyto, *rplyto0;
  717. TEST_1(rplyto = sip_reply_to_make(home, "sip:joe@bar"));
  718. TEST_1(msg_header_add_param(home, (msg_common_t *)rplyto, "x-extra=extra") == 0);
  719. while (rplyto->rplyto_params && rplyto->rplyto_params[0])
  720. msg_header_remove_param(rplyto->rplyto_common, rplyto->rplyto_params[0]);
  721. su_free(home, rplyto);
  722. TEST_1(!sip_reply_to_make(home, (void *)"sip:joe@[baa"));
  723. TEST_1(rplyto = sip_reply_to_make(home, (void *)"sip:joe@bar"));
  724. su_free(home, rplyto);
  725. TEST_1(rplyto = sip_reply_to_make(home, (void *)"Joe <sip:joe@bar;user=ip>;x-extra=extra"));
  726. TEST_1(rplyto0 = sip_reply_to_dup(home, rplyto));
  727. su_free(home, rplyto);
  728. su_free(home, rplyto0);
  729. }
  730. su_home_check(home);
  731. su_home_zap(home);
  732. END();
  733. }
  734. int test_sip_msg_class(msg_mclass_t const *mc)
  735. {
  736. int i, j, N;
  737. msg_hclass_t *hc;
  738. BEGIN();
  739. N = mc->mc_hash_size;
  740. /* check hashes */
  741. for (i = 0; i < N; i++) {
  742. if (!(hc = mc->mc_hash[i].hr_class))
  743. continue;
  744. for (j = i + 1; j < N; j++) {
  745. if (!mc->mc_hash[j].hr_class)
  746. continue;
  747. if (hc->hc_hash == mc->mc_hash[j].hr_class->hc_hash) {
  748. fprintf(stderr, "\t%s and %s have same hash\n",
  749. hc->hc_name, mc->mc_hash[j].hr_class->hc_name);
  750. return 1;
  751. }
  752. }
  753. }
  754. /* Check parser table sanity */
  755. for (i = 0; i < N; i++) {
  756. /* Verify each header entry */
  757. hc = mc->mc_hash[i].hr_class;
  758. if (hc == NULL)
  759. continue;
  760. /* Short form */
  761. if (hc->hc_short[0])
  762. TEST_P(mc->mc_short[hc->hc_short[0] - 'a'].hr_class, hc);
  763. /* Long form */
  764. j = msg_header_name_hash(hc->hc_name, NULL);
  765. TEST(j, hc->hc_hash);
  766. for (j = MC_HASH(hc->hc_name, N); j != i; j = (j + 1) % N)
  767. TEST_1(mc->mc_hash[j].hr_class);
  768. }
  769. END();
  770. }
  771. msg_t *read_message(int flags, char const buffer[])
  772. {
  773. size_t n;
  774. int m;
  775. msg_t *msg;
  776. msg_iovec_t iovec[2];
  777. n = strlen(buffer);
  778. if (n == 0)
  779. return NULL;
  780. msg = msg_create(test_mclass, flags);
  781. if (msg_recv_iovec(msg, iovec, 2, n, 1) < 0) {
  782. perror("msg_recv_iovec");
  783. }
  784. memcpy(iovec->mv_base, buffer, n);
  785. msg_recv_commit(msg, n, 1);
  786. m = msg_extract(msg);
  787. return msg;
  788. }
  789. static int test_encoding(void)
  790. {
  791. msg_header_t *h, *h1;
  792. msg_common_t *c;
  793. msg_t *msg;
  794. sip_t *sip;
  795. su_home_t *home;
  796. BEGIN();
  797. TEST_1(home = su_home_new(sizeof *home));
  798. msg = read_message(MSG_DO_EXTRACT_COPY,
  799. "SUBSCRIBE sip:foo@bar SIP/2.0\r\n"
  800. "To: Joe User <sip:foo@bar>\r\n"
  801. "From: \"Bar Owner\" <sip:bar@foo>;tag=foobar\r\n"
  802. "P-Asserted-Identity: <sip:bar@foo>\r\n"
  803. "P-Preferred-Identity: <sip:bar-owner@foo>\r\n"
  804. "Call-ID: 0ha0isndaksdj@10.1.2.3\r\n"
  805. "CSeq: 8 SUBSCRIBE\r\n"
  806. "Via: SIP/2.0/UDP 135.180.130.133\r\n"
  807. "Extension-Header: extended, more\r\n"
  808. "Reason: Q.850;cause=16;text=\"Terminated\"\r\n"
  809. "Contact: <sip:bar@pc.foo:5060>\r\n"
  810. "Date: Thu, 25 Mar 2004 14:49:29 GMT\r\n"
  811. "Max-Forwards: 80\r\n"
  812. "Min-Expires: 30\r\n"
  813. "Retry-After: 48 (this is a comment) ;duration=321\r\n"
  814. "Route: <sip:proxy.bar;maddr=172.21.40.40>\r\n"
  815. "Request-Disposition: proxy\r\n"
  816. "Accept-Contact: *;audio\r\n"
  817. "Reject-Contact: *;video\r\n"
  818. "Expires: 1200\r\n"
  819. "Event: presence;id=1\r\n"
  820. "In-Reply-To: {0h!a0i\"sndaksdj}@[kjsafi3], {0h!a0i\"snj}@[kjsfi3]\r\n"
  821. "Organization: Nuoret Banaani-Kotkat y.r.\r\n"
  822. "Priority: urgent\r\n"
  823. "Subject: ynk\r\n"
  824. "Timestamp: 3289129810.798259\r\n"
  825. "SIP-If-Match: foobar\r\n"
  826. "Proxy-Requires: prefs\r\n"
  827. "Supported: vnd.nokia\r\n"
  828. "User-Agent: Unknown Subscriber (1.0) Tonto (2.0)\r\n"
  829. "Accept: application/pidf+xml;version=1.0\r\n"
  830. "Accept-Encoding: gzip\r\n"
  831. /* Test loop below cannot encode multiple Accept-Language on one line */
  832. "Accept-Language: "/* "fi, "*/"en;q=0.2\r\n"
  833. "RAck: 421413 214214 INVITE\r\n"
  834. "Referred-By: <sips:bob@biloxi.example.com>\r\n"
  835. "Replaces: 12345601@atlanta.example.com;from-tag=314159;to-tag=1234567\r\n"
  836. "Authorization: Digest realm=\"foo\"\r\n"
  837. "Proxy-Authorization: Digest realm=\"foo\"\r\n"
  838. "Security-Client: tls\r\n"
  839. "Security-Verify: tls;q=0.2\r\n"
  840. "Privacy: none\r\n"
  841. "Content-Length: 7\r\n"
  842. "Content-Encoding: gzip, deflate, identity\r\n"
  843. "Content-Disposition: filter\r\n"
  844. "Content-Language: fi\r\n"
  845. "MIME-Version: 1.0\r\n"
  846. "Min-SE: 123\r\n"
  847. "Session-Expires: 1200\r\n"
  848. "Content-Type: text/plain\r\n"
  849. "Refer-Sub: true\r\n"
  850. "Suppress-Body-If-Match: humppa\r\n"
  851. "Suppress-Notify-If-Match: zumppa\r\n"
  852. "\r\n"
  853. "Heippa!");
  854. sip = sip_object(msg);
  855. TEST_1(msg); TEST_1(sip); TEST_1(!sip->sip_error);
  856. for (h = (msg_header_t *)sip->sip_request; h; h = h->sh_succ) {
  857. char b[80];
  858. size_t n;
  859. if (h == (msg_header_t*)sip->sip_payload)
  860. break;
  861. TEST_1(h1 = msg_header_dup(home, h));
  862. n = msg_header_e(b, sizeof b, h1, 0);
  863. TEST_SIZE(n, h->sh_len);
  864. TEST_M(b, h->sh_data, n);
  865. su_free(home, h1);
  866. }
  867. msg_destroy(msg), msg = NULL;
  868. /* Note: this should be canonic! */
  869. msg = read_message(MSG_DO_EXTRACT_COPY,
  870. "SIP/2.0 200 Ok\r\n"
  871. "To: Joe User <sip:foo@bar>;tag=deadbeef\r\n"
  872. "From: sip:bar@foo;tag=foobar\r\n"
  873. "Call-ID: {0h!a0i\"sndaksdj}@[kjsafi3]\r\n"
  874. "CSeq: 8912734 SUBSCRIBE\r\n"
  875. "Via: SIP/2.0/UDP 135.180.130.133\r\n"
  876. "Extension-Header: extended, more\r\n"
  877. "Reason: SIP;cause=400;text=\"Bad Message\"\r\n"
  878. "Contact: <sip:bar@pc.foo:5060>;audio\r\n"
  879. "Date: Thu, 25 Mar 2004 14:49:29 GMT\r\n"
  880. "Max-Forwards: 80\r\n"
  881. "Min-Expires: 30\r\n"
  882. "Expires: Thu, 25 Mar 2004 15:49:29 GMT\r\n"
  883. "Retry-After: 48;duration=321\r\n"
  884. "Record-Route: <sip:record-route@proxy.bar;maddr=172.21.40.40>\r\n"
  885. "Event: presence;id=1\r\n"
  886. "Allow-Events: presence, presence.winfo\r\n"
  887. "Subscription-State: active;expires=1800\r\n"
  888. "Call-Info: <http://www.bar.com/xcap/joe/>;purpose=xcap\r\n"
  889. "Error-Info: <http://www.bar.com/xcap/joe/errors>;param=xcap\r\n"
  890. "Server: None\r\n"
  891. "Timestamp: 3289129810.798259 0.084054\r\n"
  892. "SIP-ETag: foobar\r\n"
  893. "SIP-If-Match: foobar\r\n"
  894. "Requires: vnd.nokia\r\n"
  895. "Unsupported: vnd.nokia.pic\r\n"
  896. "Accept-Disposition: filter\r\n"
  897. "Warning: 399 presence.bar:5060 \"Unimplemented filter\"\r\n"
  898. "RSeq: 421414\r\n"
  899. "Refer-To: <sip:hsdf@cdwf.xcfw.com?Subject=test&Organization=Bar>\r\n"
  900. "Alert-Info: <http://alert.example.org/test.mp3>\r\n"
  901. "Reply-To: Bob <sip:bob@example.com>\r\n"
  902. "WWW-Authenticate: Digest realm=\"foo\"\r\n"
  903. "Proxy-Authenticate: Digest realm=\"foo\"\r\n"
  904. "Security-Server: tls;q=0.2\r\n"
  905. "Session-Expires: 1200;refresher=uac\r\n"
  906. "Content-Length: 7\r\n"
  907. "Content-Type: text/plain;charset=iso8859-1\r\n"
  908. "\r\n"
  909. "Heippa!");
  910. sip = sip_object(msg);
  911. TEST_1(msg); TEST_1(sip); TEST_1(!sip->sip_error);
  912. for (h = (msg_header_t *)sip->sip_status; h; h = h->sh_succ) {
  913. char b[80];
  914. size_t n;
  915. if (h == (sip_header_t*)sip->sip_payload)
  916. break;
  917. TEST_1(h1 = sip_header_dup(home, h));
  918. n = sip_header_e(b, sizeof b, h1, 0);
  919. TEST_SIZE(n, h->sh_len);
  920. TEST_M(b, h->sh_data, n);
  921. su_free(home, h1);
  922. }
  923. TEST_1(sip->sip_etag);
  924. TEST_S(sip->sip_etag->g_value, "foobar");
  925. TEST_1(sip->sip_if_match);
  926. msg_destroy(msg), msg = NULL;
  927. su_home_check(home);
  928. su_home_zap(home);
  929. msg = read_message(0,
  930. "SIP/2.0 200 Ok\r\n"
  931. "Via: SIP/2.0/UDP 135.180.130.133\r\n"
  932. "Via: SIP/2.0/UDP 135.180.130.130:5060\r\n"
  933. "To: Joe User <sip:foo@bar>;tag=deadbeef\r\n"
  934. "From: sip:bar@foo;tag=foobar\r\n"
  935. "Call-ID: {0h!a0i\"sndaksdj}@[kjsafi3]\r\n"
  936. "CSeq: 8912734 SUBSCRIBE\r\n"
  937. "Record-Route: <sip:135.180.130.133;lr>\r\n"
  938. "Record-Route: <sip:135.180.130.130;lr>\r\n"
  939. "Content-Length: 0\r\n"
  940. "\r\n");
  941. sip = sip_object(msg);
  942. TEST_1(msg); TEST_1(sip); TEST_1(!sip->sip_error);
  943. sip->sip_flags |= MSG_FLG_COMPACT;
  944. TEST_1(msg_prepare(msg) != 0);
  945. TEST_1(c = sip->sip_status->st_common);
  946. TEST_M(c->h_data, "SIP/2.0 200 Ok\r\n", c->h_len);
  947. TEST_1(c = sip->sip_to->a_common);
  948. TEST_M(c->h_data, "t:Joe User<sip:foo@bar>;tag=deadbeef\r\n", c->h_len);
  949. TEST_1(c = sip->sip_from->a_common);
  950. TEST_M(c->h_data, "f:sip:bar@foo;tag=foobar\r\n", c->h_len);
  951. TEST_1(c = sip->sip_call_id->i_common);
  952. TEST_M(c->h_data, "i:{0h!a0i\"sndaksdj}@[kjsafi3]\r\n", c->h_len);
  953. TEST_1(c = sip->sip_cseq->cs_common);
  954. TEST_M(c->h_data, "CSeq:8912734 SUBSCRIBE\r\n", c->h_len);
  955. TEST_1(c = sip->sip_via->v_common);
  956. TEST_M(c->h_data, "v:SIP/2.0/UDP 135.180.130.133,SIP/2.0/UDP 135.180.130.130:5060\r\n", c->h_len);
  957. TEST_1(c = sip->sip_via->v_next->v_common);
  958. TEST_SIZE(c->h_len, 0); TEST_1(c->h_data);
  959. TEST_1(c = sip->sip_record_route->r_common);
  960. TEST_M(c->h_data, "Record-Route:<sip:135.180.130.133;lr>,<sip:135.180.130.130;lr>\r\n", c->h_len);
  961. TEST_1(c = sip->sip_record_route->r_next->r_common);
  962. TEST_SIZE(c->h_len, 0); TEST_1(c->h_data);
  963. TEST_1(c = sip->sip_content_length->l_common);
  964. TEST_M(c->h_data, "l:0\r\n", c->h_len);
  965. END();
  966. }
  967. #define XTRA(xtra, h) SU_ALIGN(xtra) + sip_header_size((sip_header_t*)h)
  968. /** Test header filtering and duplicating */
  969. int tag_test(void)
  970. {
  971. su_home_t *home = su_home_new(sizeof(*home));
  972. sip_request_t *request =
  973. sip_request_make(home, "INVITE sip:joe@example.com SIP/2.0");
  974. sip_to_t *to = sip_to_make(home,
  975. "Joe User <sip:joe.user@example.com;param=1>"
  976. ";tag=12345678");
  977. sip_via_t *via = sip_via_make(home,
  978. "SIP/2.0/UDP sip.example.com"
  979. ";maddr=128.12.9.254"
  980. ";branch=289412978y641.321312");
  981. url_t *url = url_hdup(home,
  982. (url_t *)"sip:test:pass@example.com;baz=1?foo&bar");
  983. tagi_t *lst, *dup;
  984. size_t xtra;
  985. tag_value_t v;
  986. BEGIN();
  987. su_home_check(home);
  988. TEST_1(home && request && to && via);
  989. lst = tl_list(SIPTAG_REQUEST(request),
  990. SIPTAG_TO(to),
  991. SIPTAG_VIA(via),
  992. URLTAG_URL(url),
  993. TAG_NULL());
  994. xtra = 0;
  995. xtra += XTRA(xtra, request);
  996. xtra += XTRA(xtra, to);
  997. xtra += XTRA(xtra, via);
  998. xtra += SU_ALIGN(xtra) + sizeof(*url) + url_xtra(url);
  999. TEST_SIZE(tl_len(lst), 5 * sizeof(tagi_t));
  1000. TEST_SIZE(tl_xtra(lst, 0), xtra);
  1001. dup = tl_adup(NULL, lst);
  1002. TEST(dup != NULL, 1);
  1003. TEST_SIZE(tl_len(dup), 5 * sizeof(tagi_t));
  1004. TEST_SIZE(tl_xtra(dup, 0), xtra);
  1005. if (tstflags & tst_verbatim)
  1006. tl_print(stdout, "dup:\n", dup);
  1007. su_free(NULL, dup);
  1008. tl_vfree(lst);
  1009. TEST_1(t_scan(siptag_request, home, "INVITE sip:example.org SIP/2.0", &v));
  1010. TEST_1(request = (void *)v);
  1011. TEST_1(request->rq_common->h_class == sip_request_class);
  1012. TEST_S(request->rq_method_name, "INVITE");
  1013. TEST_S(request->rq_version, "SIP/2.0");
  1014. TEST_1(t_scan(siptag_to, home, "Example <sip:example.org>;tag=foo", &v));
  1015. TEST_1(to = (void *)v);
  1016. TEST_1(to->a_common->h_class == sip_to_class);
  1017. TEST_S(to->a_display, "Example");
  1018. TEST_S(to->a_tag, "foo");
  1019. su_home_check(home);
  1020. su_home_zap(home);
  1021. END();
  1022. }
  1023. /** Test advanced tag features */
  1024. static int parser_tag_test(void)
  1025. {
  1026. tagi_t *lst, *dup, *filter1, *filter2, *filter3, *filter4;
  1027. tagi_t *b1, *b2, *b3, *b4;
  1028. msg_t *msg;
  1029. sip_t *sip;
  1030. su_home_t *home;
  1031. size_t xtra;
  1032. BEGIN();
  1033. home = su_home_new(sizeof *home);
  1034. msg = read_message(MSG_DO_EXTRACT_COPY,
  1035. "SIP/2.0 401 Unauthorized\r\n"
  1036. "Via: SIP/2.0/UDP srlab.sr.ntc.nokia.com:5060;maddr=192.168.102.5\r\n"
  1037. "Via: SIP/2.0/UDP 172.21.9.155\r\n"
  1038. "Record-Route: <sip:garage.sr.ntc.nokia.com:5060;maddr=srlab.sr.ntc.nokia.com>\r\n"
  1039. "From: sip:digest@garage.sr.ntc.nokia.com\r\n"
  1040. "To: sip:digest@garage.sr.ntc.nokia.com\r\n"
  1041. "Call-ID: 982773899-reg@172.21.9.155\r\n"
  1042. "CSeq: 1 REGISTER\r\n"
  1043. "WWW-Authenticate: Digest realm=\"garage.sr.ntc.nokia.com\",\r\n"
  1044. " nonce=\"MjAwMS0wMS0yMSAxNTowODo1OA==\", algorithm=MD5, qop=\"auth\"\r\n"
  1045. "Proxy-Authenticate: Digest realm=\"IndigoSw\", domain=\"sip:indigosw.com\", "
  1046. "nonce=\"V2VkIEF1ZyAxNSAxODoxMzozMiBCU1QgMjAwMVtCQDJkYjE5ZA==\", "
  1047. "opaque=\"NzA3ZjJhYzU4MGY3MzU0MQ==\", stale=false, "
  1048. "algorithm=md5, algorithm=sha1, qop=\"auth\"\r\n"
  1049. /* , qop=\"auth, auth-int\"\r */
  1050. "\r\n");
  1051. sip = sip_object(msg);
  1052. TEST_1(home && msg && sip);
  1053. TEST_1(sip->sip_size >= sizeof *sip);
  1054. TEST_1(sip_is_status((sip_header_t *)sip->sip_status));
  1055. TEST_1(sip_is_via((sip_header_t *)sip->sip_via));
  1056. TEST_1(sip_is_via((sip_header_t *)sip->sip_via->v_next));
  1057. TEST_1(sip_is_record_route((sip_header_t *)sip->sip_record_route));
  1058. TEST_1(sip_is_from((sip_header_t *)sip->sip_from));
  1059. TEST_1(sip_is_to((sip_header_t *)sip->sip_to));
  1060. TEST_1(sip_is_call_id((sip_header_t *)sip->sip_call_id));
  1061. TEST_1(sip_is_cseq((sip_header_t *)sip->sip_cseq));
  1062. TEST_1(sip_is_www_authenticate(
  1063. (sip_header_t *)sip->sip_www_authenticate));
  1064. TEST_1(sip_complete_message(msg) == 0);
  1065. TEST_1(sip_is_content_length((sip_header_t *)sip->sip_content_length));
  1066. TEST_P(sip->sip_content_length->l_common->h_succ, sip->sip_separator);
  1067. lst = tl_list(SIPTAG_VIA(sip->sip_via),
  1068. SIPTAG_RECORD_ROUTE(sip->sip_record_route),
  1069. TAG_SKIP(2),
  1070. SIPTAG_CSEQ(sip->sip_cseq),
  1071. SIPTAG_PAYLOAD(sip->sip_payload),
  1072. TAG_NULL());
  1073. filter1 = tl_list(SIPTAG_VIA(0),
  1074. TAG_NULL());
  1075. filter2 = tl_list(SIPTAG_CALL_ID(0),
  1076. SIPTAG_FROM(0),
  1077. SIPTAG_ROUTE(0),
  1078. SIPTAG_CSEQ(0),
  1079. TAG_NULL());
  1080. filter3 = tl_list(SIPTAG_CSEQ(0),
  1081. SIPTAG_CONTENT_LENGTH(0),
  1082. TAG_NULL());
  1083. filter4 = tl_list(SIPTAG_STATUS(0),
  1084. SIPTAG_VIA(0),
  1085. SIPTAG_RECORD_ROUTE(0),
  1086. SIPTAG_FROM(0),
  1087. SIPTAG_TO(0),
  1088. SIPTAG_CALL_ID(0),
  1089. SIPTAG_CSEQ(0),
  1090. SIPTAG_WWW_AUTHENTICATE(0),
  1091. SIPTAG_PROXY_AUTHENTICATE(0),
  1092. SIPTAG_CONTENT_LENGTH(0),
  1093. TAG_NULL());
  1094. TEST_1(lst && filter1 && filter2 && filter3 && filter4);
  1095. b1 = tl_afilter(home, filter1, lst);
  1096. TEST_SIZE(tl_len(b1), 2 * sizeof(tagi_t));
  1097. TEST_1(((sip_via_t *)b1->t_value)->v_next);
  1098. xtra = sip_header_size((sip_header_t *)sip->sip_via);
  1099. xtra += SU_ALIGN(xtra);
  1100. xtra += sip_header_size((sip_header_t *)sip->sip_via->v_next);
  1101. TEST_SIZE(tl_xtra(b1, 0), xtra);
  1102. dup = tl_adup(home, lst);
  1103. TEST_SIZE(tl_len(dup), tl_len(lst));
  1104. TEST_SIZE(tl_xtra(dup, 0), tl_xtra(lst, 0));
  1105. tl_vfree(lst);
  1106. lst = tl_list(SIPTAG_SIP(sip), TAG_NULL());
  1107. b2 = tl_afilter(home, filter2, lst);
  1108. TEST_SIZE(tl_len(b2), 4 * sizeof(tagi_t));
  1109. xtra = 0;
  1110. xtra += XTRA(xtra, sip->sip_call_id);
  1111. xtra += XTRA(xtra, sip->sip_from);
  1112. xtra += XTRA(xtra, sip->sip_cseq);
  1113. TEST_SIZE(tl_xtra(b2, 0), xtra);
  1114. b3 = tl_afilter(home, filter3, lst);
  1115. TEST_SIZE(tl_len(b3), 3 * sizeof(tagi_t));
  1116. TEST_SIZE(tl_xtra(b3, 0),
  1117. sizeof(sip_content_length_t) + sizeof(sip_cseq_t));
  1118. b4 = tl_afilter(home, filter4, lst);
  1119. TEST_SIZE(tl_len(b4), 11 * sizeof(tagi_t));
  1120. xtra = 0;
  1121. xtra += XTRA(xtra, sip->sip_status);
  1122. xtra += XTRA(xtra, sip->sip_via);
  1123. xtra += XTRA(xtra, sip->sip_via->v_next);
  1124. xtra += XTRA(xtra, sip->sip_record_route);
  1125. xtra += XTRA(xtra, sip->sip_from);
  1126. xtra += XTRA(xtra, sip->sip_to);
  1127. xtra += XTRA(xtra, sip->sip_call_id);
  1128. xtra += XTRA(xtra, sip->sip_cseq);
  1129. xtra += XTRA(xtra, sip->sip_www_authenticate);
  1130. xtra += XTRA(xtra, sip->sip_proxy_authenticate);
  1131. xtra += XTRA(xtra, sip->sip_content_length);
  1132. TEST_SIZE(tl_xtra(b4, 0), xtra);
  1133. tl_vfree(filter1); tl_vfree(filter2); tl_vfree(filter3); tl_vfree(filter4);
  1134. tl_vfree(lst);
  1135. su_home_check(home);
  1136. su_free(home, b4);
  1137. su_free(home, b3);
  1138. su_free(home, b2);
  1139. su_free(home, dup);
  1140. su_free(home, b1);
  1141. su_home_check(home);
  1142. su_home_unref(home);
  1143. msg_destroy(msg);
  1144. END();
  1145. }
  1146. /** Test error messages */
  1147. static int response_phrase_test(void)
  1148. {
  1149. BEGIN();
  1150. {
  1151. struct { int status; char const *phrase; } const errors[] =
  1152. {
  1153. { SIP_100_TRYING },
  1154. { SIP_180_RINGING },
  1155. { SIP_181_CALL_IS_BEING_FORWARDED },
  1156. { SIP_182_QUEUED },
  1157. { SIP_183_SESSION_PROGRESS },
  1158. { SIP_200_OK },
  1159. { SIP_202_ACCEPTED },
  1160. { SIP_300_MULTIPLE_CHOICES },
  1161. { SIP_301_MOVED_PERMANENTLY },
  1162. { SIP_302_MOVED_TEMPORARILY },
  1163. { SIP_305_USE_PROXY },
  1164. { SIP_380_ALTERNATIVE_SERVICE },
  1165. { SIP_400_BAD_REQUEST },
  1166. { SIP_401_UNAUTHORIZED },
  1167. { SIP_402_PAYMENT_REQUIRED },
  1168. { SIP_403_FORBIDDEN },
  1169. { SIP_404_NOT_FOUND },
  1170. { SIP_405_METHOD_NOT_ALLOWED },
  1171. { SIP_406_NOT_ACCEPTABLE },
  1172. { SIP_407_PROXY_AUTH_REQUIRED },
  1173. { SIP_408_REQUEST_TIMEOUT },
  1174. { SIP_409_CONFLICT },
  1175. { SIP_410_GONE },
  1176. { SIP_411_LENGTH_REQUIRED },
  1177. { SIP_413_REQUEST_TOO_LARGE },
  1178. { SIP_414_REQUEST_URI_TOO_LONG },
  1179. { SIP_415_UNSUPPORTED_MEDIA },
  1180. { SIP_416_UNSUPPORTED_URI },
  1181. { SIP_420_BAD_EXTENSION },
  1182. { SIP_421_EXTENSION_REQUIRED },
  1183. { SIP_422_SESSION_TIMER_TOO_SMALL },
  1184. { SIP_423_INTERVAL_TOO_BRIEF },
  1185. { SIP_423_REGISTRATION_TOO_BRIEF },
  1186. { SIP_480_TEMPORARILY_UNAVAILABLE },
  1187. { SIP_481_NO_TRANSACTION },
  1188. { SIP_481_NO_CALL },
  1189. { SIP_482_LOOP_DETECTED },
  1190. { SIP_483_TOO_MANY_HOPS },
  1191. { SIP_484_ADDRESS_INCOMPLETE },
  1192. { SIP_485_AMBIGUOUS },
  1193. { SIP_486_BUSY_HERE },
  1194. { SIP_487_REQUEST_TERMINATED },
  1195. { SIP_487_REQUEST_CANCELLED },
  1196. { SIP_488_NOT_ACCEPTABLE },
  1197. { SIP_489_BAD_EVENT },
  1198. { SIP_491_REQUEST_PENDING },
  1199. { SIP_493_UNDECIPHERABLE },
  1200. { SIP_500_INTERNAL_SERVER_ERROR },
  1201. { SIP_501_NOT_IMPLEMENTED },
  1202. { SIP_502_BAD_GATEWAY },
  1203. { SIP_503_SERVICE_UNAVAILABLE },
  1204. { SIP_504_GATEWAY_TIME_OUT },
  1205. { SIP_505_VERSION_NOT_SUPPORTED },
  1206. { SIP_513_MESSAGE_TOO_LARGE },
  1207. { SIP_600_BUSY_EVERYWHERE },
  1208. { SIP_603_DECLINE },
  1209. { SIP_604_DOES_NOT_EXIST_ANYWHERE },
  1210. { SIP_606_NOT_ACCEPTABLE },
  1211. { SIP_607_UNWANTED },
  1212. { 0, NULL }
  1213. };
  1214. int i;
  1215. for (i = 0; errors[i].status; i++)
  1216. TEST_S(errors[i].phrase, sip_status_phrase(errors[i].status));
  1217. }
  1218. END();
  1219. }
  1220. /** Test parser and header manipulation */
  1221. static int parser_test(void)
  1222. {
  1223. msg_t *msg;
  1224. sip_t *sip;
  1225. su_home_t *home;
  1226. sip_route_t *r;
  1227. sip_request_t sip_request[1] = { SIP_REQUEST_INIT() };
  1228. sip_status_t sip_status[1] = { SIP_STATUS_INIT() };
  1229. sip_header_t sip_unknown[1] = { SIP_UNKNOWN_INIT() };
  1230. sip_separator_t sip_separator[1] = { SIP_SEPARATOR_INIT() };
  1231. sip_payload_t sip_payload[1] = { SIP_PAYLOAD_INIT() };
  1232. sip_via_t sip_via[1] = { SIP_VIA_INIT() };
  1233. sip_route_t sip_route[1] = { SIP_ROUTE_INIT() };
  1234. sip_record_route_t sip_record_route[1] = { SIP_RECORD_ROUTE_INIT() };
  1235. sip_max_forwards_t sip_max_forwards[1] = { SIP_MAX_FORWARDS_INIT() };
  1236. sip_from_t sip_from[1] = { SIP_FROM_INIT() };
  1237. sip_to_t sip_to[1] = { SIP_TO_INIT() };
  1238. sip_call_id_t sip_call_id[1] = { SIP_CALL_ID_INIT() };
  1239. sip_cseq_t sip_cseq[1] = { SIP_CSEQ_INIT() };
  1240. sip_contact_t sip_contact[1] = { SIP_CONTACT_INIT() };
  1241. sip_expires_t sip_expires[1] = { SIP_EXPIRES_INIT() };
  1242. sip_date_t sip_date[1] = { SIP_DATE_INIT() };
  1243. sip_retry_after_t sip_retry_after[1] = { SIP_RETRY_AFTER_INIT() };
  1244. sip_timestamp_t sip_timestamp[1] = { SIP_TIMESTAMP_INIT() };
  1245. sip_subject_t sip_subject[1] = { SIP_SUBJECT_INIT() };
  1246. sip_priority_t sip_priority[1] = { SIP_PRIORITY_INIT() };
  1247. sip_call_info_t sip_call_info[1] = { SIP_CALL_INFO_INIT() };
  1248. sip_organization_t sip_organization[1] = { SIP_ORGANIZATION_INIT() };
  1249. sip_server_t sip_server[1] = { SIP_SERVER_INIT() };
  1250. sip_user_agent_t sip_user_agent[1] = { SIP_USER_AGENT_INIT() };
  1251. sip_in_reply_to_t sip_in_reply_to[1] = { SIP_IN_REPLY_TO_INIT() };
  1252. sip_accept_t sip_accept[1] = { SIP_ACCEPT_INIT() };
  1253. sip_accept_encoding_t sip_accept_encoding[1] = { SIP_ACCEPT_ENCODING_INIT() };
  1254. sip_accept_language_t sip_accept_language[1] = { SIP_ACCEPT_LANGUAGE_INIT() };
  1255. sip_session_expires_t sip_session_expires[1] = { SIP_SESSION_EXPIRES_INIT() };
  1256. sip_min_se_t sip_min_se[1] = { SIP_MIN_SE_INIT() };
  1257. sip_allow_t sip_allow[1] = { SIP_ALLOW_INIT() };
  1258. sip_require_t sip_require[1] = { SIP_REQUIRE_INIT() };
  1259. sip_proxy_require_t sip_proxy_require[1] = { SIP_PROXY_REQUIRE_INIT() };
  1260. sip_supported_t sip_supported[1] = { SIP_SUPPORTED_INIT() };
  1261. sip_unsupported_t sip_unsupported[1] = { SIP_UNSUPPORTED_INIT() };
  1262. #if SIP_HAVE_ENCRYPTION
  1263. sip_encryption_t sip_encryption[1] = { SIP_ENCRYPTION_INIT() };
  1264. #endif
  1265. #if SIP_HAVE_RESPONSE_KEY
  1266. sip_response_key_t sip_response_key[1] = { SIP_RESPONSE_KEY_INIT() };
  1267. #endif
  1268. sip_proxy_authenticate_t sip_proxy_authenticate[1] = { SIP_PROXY_AUTHENTICATE_INIT() };
  1269. sip_proxy_authorization_t sip_proxy_authorization[1] = { SIP_PROXY_AUTHORIZATION_INIT() };
  1270. sip_authorization_t sip_authorization[1] = { SIP_AUTHORIZATION_INIT() };
  1271. sip_www_authenticate_t sip_www_authenticate[1] = { SIP_WWW_AUTHENTICATE_INIT() };
  1272. sip_error_info_t sip_error_info[1] = { SIP_ERROR_INFO_INIT() };
  1273. sip_warning_t sip_warning[1] = { SIP_WARNING_INIT() };
  1274. sip_mime_version_t sip_mime_version[1] = { SIP_MIME_VERSION_INIT() };
  1275. sip_content_type_t sip_content_type[1] = { SIP_CONTENT_TYPE_INIT() };
  1276. sip_content_encoding_t sip_content_encoding[1] = { SIP_CONTENT_ENCODING_INIT() };
  1277. sip_content_disposition_t sip_content_disposition[1] = { SIP_CONTENT_DISPOSITION_INIT() };
  1278. sip_content_length_t sip_content_length[1] = { SIP_CONTENT_LENGTH_INIT() };
  1279. BEGIN();
  1280. home = su_home_new(sizeof *home);
  1281. msg = read_message(MSG_DO_EXTRACT_COPY,
  1282. "INVITE sip:John_Smith@tct.hut.fi SIP/2.0\r\n"
  1283. "To: John Smith <sip:John_Smith@tct.hut.fi:5066;user=ip;maddr=131.228.16.2>\r\n"
  1284. " ; tag = deadbeef\r\n"
  1285. "From: http://www.cs.columbia.edu\r\n"
  1286. "Call-ID: 0ha0isndaksdj@10.1.2.3\r\n"
  1287. "CSeq : 8 INVITE\r\n"
  1288. "Via: SIP/2.0/UDP 135.180.130.133\r\n"
  1289. "Route: <sip:1@a;lr>, sip:2@b;lr=2, <sip:3@c;lr=3>\r\n"
  1290. "Route: <sip:1@d;lr=4>\r\n"
  1291. "Route: sip:2@e;lr=5, <sip:3@f;lr=6>\r\n"
  1292. "Route: <sip:1@g;lr=7>, <sip:2@h>;lr=8\r\n"
  1293. "Content-Type: application/sdp\r\n"
  1294. "Contact: Joe Bob Briggs <urn:ipaddr:122.1.2.3> ; bar=\"foo baa\", sip:kuik@foo.invalid\r\n"
  1295. "Via: SIP/2.0/UDP [aa:bb::1]:5061\r\n"
  1296. "\r\n"
  1297. "v=0\r\n"
  1298. "o=mhandley 29739 7272939 IN IP4 126.5.4.3\r\n"
  1299. "c=IN IP4 135.180.130.88\r\n"
  1300. "m=audio 492170 RTP/AVP 0 12\r\n"
  1301. "m=video 3227 RTP/AVP 31\r\n"
  1302. "a=rtpmap:31 LPC\r\n");
  1303. sip = sip_object(msg);
  1304. TEST_1(home && msg && sip);
  1305. TEST_1(sip_is_request((sip_header_t *)sip->sip_request));
  1306. TEST_1(sip->sip_via); TEST_1(sip->sip_via->v_next);
  1307. TEST_1(sip->sip_via->v_next->v_next == NULL);
  1308. TEST_1(sip_sanity_check(sip) == 0);
  1309. TEST_1(r = sip->sip_route); TEST_1(r->r_common->h_data);
  1310. TEST_1(r = r->r_next); TEST_1(r->r_common->h_data);
  1311. TEST_1(r = r->r_next); TEST_1(r->r_common->h_data);
  1312. TEST_1(r = r->r_next); TEST_1(r->r_common->h_data);
  1313. TEST_1(r = r->r_next); TEST_1(r->r_common->h_data);
  1314. TEST_1(r = r->r_next); TEST_1(r->r_common->h_data);
  1315. TEST_1(r = r->r_next); TEST_1(r->r_common->h_data);
  1316. TEST_1(r = r->r_next); TEST_1(r->r_common->h_data);
  1317. TEST_1(!r->r_next);
  1318. TEST_1(r = sip_route_fix(sip->sip_route)); TEST_1(!r->r_common->h_data);
  1319. TEST_1(r = r->r_next); TEST_1(!r->r_common->h_data);
  1320. TEST_1(r = r->r_next); TEST_1(!r->r_common->h_data);
  1321. TEST_1(r = r->r_next); TEST_1(r->r_common->h_data);
  1322. TEST_1(r = r->r_next); TEST_1(!r->r_common->h_data);
  1323. TEST_1(r = r->r_next); TEST_1(!r->r_common->h_data);
  1324. TEST_1(r = r->r_next); TEST_1(!r->r_common->h_data);
  1325. TEST_1(r = r->r_next); TEST_1(!r->r_common->h_data);
  1326. TEST_1(!r->r_next);
  1327. /* Quiet lots of warnings */
  1328. #define _msg_header_offset msg_header_offset
  1329. #define msg_header_offset(msg, sip, h) \
  1330. _msg_header_offset(msg, (msg_pub_t *)sip, (msg_header_t *)h)
  1331. TEST_P(msg_header_offset(msg, sip, sip_request), &sip->sip_request);
  1332. TEST_P(msg_header_offset(msg, sip, sip_status), &sip->sip_status);
  1333. TEST_P(msg_header_offset(msg, sip, sip_unknown), &sip->sip_unknown);
  1334. TEST_P(msg_header_offset(msg, sip, sip_separator), &sip->sip_separator);
  1335. TEST_P(msg_header_offset(msg, sip, sip_payload), &sip->sip_payload);
  1336. TEST_P(msg_header_offset(msg, sip, sip_via), &sip->sip_via);
  1337. TEST_P(msg_header_offset(msg, sip, sip_route), &sip->sip_route);
  1338. TEST_P(msg_header_offset(msg, sip, sip_record_route),
  1339. &sip->sip_record_route);
  1340. TEST_P(msg_header_offset(msg, sip, sip_max_forwards),
  1341. &sip->sip_max_forwards);
  1342. TEST_P(msg_header_offset(msg, sip, sip_from), &sip->sip_from);
  1343. TEST_P(msg_header_offset(msg, sip, sip_to), &sip->sip_to);
  1344. TEST_P(msg_header_offset(msg, sip, sip_call_id), &sip->sip_call_id);
  1345. TEST_P(msg_header_offset(msg, sip, sip_cseq), &sip->sip_cseq);
  1346. TEST_P(msg_header_offset(msg, sip, sip_contact), &sip->sip_contact);
  1347. TEST_P(msg_header_offset(msg, sip, sip_expires), &sip->sip_expires);
  1348. TEST_P(msg_header_offset(msg, sip, sip_date), &sip->sip_date);
  1349. TEST_P(msg_header_offset(msg, sip, sip_retry_after), &sip->sip_retry_after);
  1350. TEST_P(msg_header_offset(msg, sip, sip_timestamp), &sip->sip_timestamp);
  1351. TEST_P(msg_header_offset(msg, sip, sip_subject), &sip->sip_subject);
  1352. TEST_P(msg_header_offset(msg, sip, sip_priority), &sip->sip_priority);
  1353. TEST_P(msg_header_offset(msg, sip, sip_call_info), &sip->sip_call_info);
  1354. TEST_P(msg_header_offset(msg, sip, sip_organization),
  1355. &sip->sip_organization);
  1356. TEST_P(msg_header_offset(msg, sip, sip_server), &sip->sip_server);
  1357. TEST_P(msg_header_offset(msg, sip, sip_user_agent), &sip->sip_user_agent);
  1358. TEST_P(msg_header_offset(msg, sip, sip_in_reply_to), &sip->sip_in_reply_to);
  1359. TEST_P(msg_header_offset(msg, sip, sip_accept), &sip->sip_accept);
  1360. TEST_P(msg_header_offset(msg, sip, sip_accept_encoding),
  1361. &sip->sip_accept_encoding);
  1362. TEST_P(msg_header_offset(msg, sip, sip_accept_language),
  1363. &sip->sip_accept_language);
  1364. TEST_P(msg_header_offset(msg, sip, sip_session_expires),
  1365. &sip->sip_session_expires);
  1366. TEST_P(msg_header_offset(msg, sip, sip_min_se), &sip->sip_min_se);
  1367. TEST_P(msg_header_offset(msg, sip, sip_allow), &sip->sip_allow);
  1368. TEST_P(msg_header_offset(msg, sip, sip_require), &sip->sip_require);
  1369. TEST_P(msg_header_offset(msg, sip, sip_proxy_require),
  1370. &sip->sip_proxy_require);
  1371. TEST_P(msg_header_offset(msg, sip, sip_supported), &sip->sip_supported);
  1372. TEST_P(msg_header_offset(msg, sip, sip_unsupported), &sip->sip_unsupported);
  1373. #if SIP_HAVE_ENCRYPTION
  1374. TEST(msg_header_offset(msg, sip, sip_encryption), &sip->sip_encryption);
  1375. #endif
  1376. #if SIP_HAVE_RESPONSE_KEY
  1377. TEST(msg_header_offset(msg, sip, sip_response_key), &sip->sip_response_key);
  1378. #endif
  1379. TEST_P(msg_header_offset(msg, sip, sip_proxy_authenticate),
  1380. &sip->sip_proxy_authenticate);
  1381. TEST_P(msg_header_offset(msg, sip, sip_proxy_authorization),
  1382. &sip->sip_proxy_authorization);
  1383. TEST_P(msg_header_offset(msg, sip, sip_authorization),
  1384. &sip->sip_authorization);
  1385. TEST_P(msg_header_offset(msg, sip, sip_www_authenticate),
  1386. &sip->sip_www_authenticate);
  1387. TEST_P(msg_header_offset(msg, sip, sip_error_info), &sip->sip_error_info);
  1388. TEST_P(msg_header_offset(msg, sip, sip_warning), &sip->sip_warning);
  1389. TEST_P(msg_header_offset(msg, sip, sip_mime_version), &sip->sip_mime_version);
  1390. TEST_P(msg_header_offset(msg, sip, sip_content_type), &sip->sip_content_type);
  1391. TEST_P(msg_header_offset(msg, sip, sip_content_encoding),
  1392. &sip->sip_content_encoding);
  1393. TEST_P(msg_header_offset(msg, sip, sip_content_disposition),
  1394. &sip->sip_content_disposition);
  1395. TEST_P(msg_header_offset(msg, sip, sip_content_length),
  1396. &sip->sip_content_length);
  1397. TEST_SIZE(sip_request_class->hc_params, 0);
  1398. TEST_SIZE(sip_status_class->hc_params, 0);
  1399. TEST_SIZE(sip_unknown_class->hc_params, 0);
  1400. TEST_SIZE(sip_separator_class->hc_params, 0);
  1401. TEST_SIZE(sip_payload_class->hc_params, 0);
  1402. TEST_SIZE(sip_via_class->hc_params, offsetof(sip_via_t, v_params));
  1403. TEST_SIZE(sip_route_class->hc_params, offsetof(sip_route_t, r_params));
  1404. TEST_SIZE(sip_record_route_class->hc_params,
  1405. offsetof(sip_record_route_t, r_params));
  1406. TEST_SIZE(sip_max_forwards_class->hc_params, 0);
  1407. TEST_SIZE(sip_from_class->hc_params, offsetof(sip_from_t, a_params));
  1408. TEST_SIZE(sip_to_class->hc_params, offsetof(sip_to_t, a_params));
  1409. TEST_SIZE(sip_call_id_class->hc_params, 0);
  1410. TEST_SIZE(sip_cseq_class->hc_params, 0);
  1411. TEST_SIZE(sip_contact_class->hc_params, offsetof(sip_contact_t, m_params));
  1412. TEST_SIZE(sip_expires_class->hc_params, 0);
  1413. TEST_SIZE(sip_date_class->hc_params, 0);
  1414. TEST_SIZE(sip_retry_after_class->hc_params,
  1415. offsetof(sip_retry_after_t, af_params));
  1416. TEST_SIZE(sip_timestamp_class->hc_params, 0);
  1417. TEST_SIZE(sip_subject_class->hc_params, 0);
  1418. TEST_SIZE(sip_priority_class->hc_params, 0);
  1419. TEST_SIZE(sip_call_info_class->hc_params,
  1420. offsetof(sip_call_info_t, ci_params));
  1421. TEST_SIZE(sip_organization_class->hc_params, 0);
  1422. TEST_SIZE(sip_server_class->hc_params, 0);
  1423. TEST_SIZE(sip_user_agent_class->hc_params, 0);
  1424. TEST_SIZE(sip_in_reply_to_class->hc_params,
  1425. offsetof(sip_in_reply_to_t, k_items));
  1426. TEST_SIZE(sip_accept_class->hc_params, offsetof(sip_accept_t, ac_params));
  1427. TEST_SIZE(sip_accept_encoding_class->hc_params,
  1428. offsetof(sip_accept_encoding_t, aa_params));
  1429. TEST_SIZE(sip_accept_language_class->hc_params,
  1430. offsetof(sip_accept_language_t, aa_params));
  1431. TEST_SIZE(sip_session_expires_class->hc_params,
  1432. offsetof(sip_session_expires_t, x_params));
  1433. TEST_SIZE(sip_min_se_class->hc_params, offsetof(sip_min_se_t, min_params));
  1434. TEST_SIZE(sip_allow_class->hc_params, offsetof(sip_allow_t, k_items));
  1435. TEST_SIZE(sip_require_class->hc_params, offsetof(sip_require_t, k_items));
  1436. TEST_SIZE(sip_proxy_require_class->hc_params,
  1437. offsetof(sip_proxy_require_t, k_items));
  1438. TEST_SIZE(sip_supported_class->hc_params,
  1439. offsetof(sip_supported_t, k_items));
  1440. TEST_SIZE(sip_unsupported_class->hc_params,
  1441. offsetof(sip_unsupported_t, k_items));
  1442. #if SIP_HAVE_ENCRYPTION
  1443. TEST_SIZE(sip_encryption_class->hc_params,
  1444. offsetof(sip_encryption_t, au_params));
  1445. #endif
  1446. #if SIP_HAVE_RESPONSE_KEY
  1447. TEST_SIZE(sip_response_key_class->hc_params,
  1448. offsetof(sip_response_key_t, au_params));
  1449. #endif
  1450. TEST_SIZE(sip_proxy_authenticate_class->hc_params,
  1451. offsetof(sip_proxy_authenticate_t, au_params));
  1452. TEST_SIZE(sip_proxy_authorization_class->hc_params,
  1453. offsetof(sip_proxy_authorization_t, au_params));
  1454. TEST_SIZE(sip_authorization_class->hc_params,
  1455. offsetof(sip_authorization_t, au_params));
  1456. TEST_SIZE(sip_www_authenticate_class->hc_params,
  1457. offsetof(sip_www_authenticate_t, au_params));
  1458. TEST_SIZE(sip_error_info_class->hc_params,
  1459. offsetof(sip_error_info_t, ei_params));
  1460. TEST_SIZE(sip_alert_info_class->hc_params,
  1461. offsetof(sip_alert_info_t, ai_params));
  1462. TEST_SIZE(sip_reply_to_class->hc_params,
  1463. offsetof(sip_reply_to_t, rplyto_params));
  1464. TEST_SIZE(sip_warning_class->hc_params, 0);
  1465. TEST_SIZE(sip_mime_version_class->hc_params, 0);
  1466. TEST_SIZE(sip_content_type_class->hc_params,
  1467. offsetof(sip_content_type_t, c_params));
  1468. TEST_SIZE(sip_content_encoding_class->hc_params,
  1469. offsetof(sip_content_encoding_t, k_items));
  1470. TEST_SIZE(sip_content_disposition_class->hc_params,
  1471. offsetof(sip_content_disposition_t, cd_params));
  1472. TEST_SIZE(sip_content_length_class->hc_params, 0);
  1473. msg_destroy(msg);
  1474. su_home_unref(home);
  1475. END();
  1476. }
  1477. static int count(sip_common_t *h)
  1478. {
  1479. sip_header_t *sh = (sip_header_t *)h;
  1480. unsigned n;
  1481. for (n = 0; sh; sh = sh->sh_next)
  1482. n++;
  1483. return n;
  1484. }
  1485. static int len(sip_common_t *h)
  1486. {
  1487. sip_header_t *sh = (sip_header_t *)h;
  1488. unsigned n;
  1489. for (n = 0; sh; sh = sh->sh_next) {
  1490. if (n) n +=2;
  1491. n += sip_header_field_e(NULL, 0, sh, 0);
  1492. }
  1493. return n;
  1494. }
  1495. static int sip_header_test(void)
  1496. {
  1497. msg_t *msg;
  1498. sip_t *sip;
  1499. su_home_t *home;
  1500. void const *x;
  1501. sip_via_t *v, *v0;
  1502. tagi_t const *tl;
  1503. tagi_t *tl0;
  1504. BEGIN();
  1505. home = su_home_new(sizeof *home);
  1506. TEST_1(msg = read_message(MSG_DO_EXTRACT_COPY,
  1507. "MESSAGE sip:John_Smith@tct.hut.fi SIP/2.0\r\n"
  1508. "To: John Smith <sip:John_Smith@tct.hut.fi:5066;user=ip;maddr=131.228.16.2>\r\n"
  1509. " ; tag = deadbeef\r\n"
  1510. "From:h<http://www.cs.columbia.edu>\r\n"
  1511. "Call-ID: 0ha0isndaksdj@10.1.2.3\r\n"
  1512. "CSeq : 8 MESSAGE\r\n"
  1513. "Via: SIP/2.0/UDP 135.180.130.133;received=defa:daf::00:12\r\n"
  1514. "Via: SIP/2.0/TCP 135.180.130.131;branch=deadbeef.barf;ttl=3;hidden,,"
  1515. "SIP/2.0/UDP\r\n 135.180.130.131:5061;received=[defa::00:12]\r\n"
  1516. "Contact: Joe Bob Briggs <urn:ipaddr:122.1.2.3> ; bar=\042foo baa\042, <sip:kuik@foo.invalid>, sip:barf\r\n"
  1517. "Via: SIP/2.0/UDP [aa:bb::1]:5061\r\n"
  1518. "Record-Route: Test Element <sip:[defa::00:12]:5061>;param=12+1\r\n"
  1519. "Record-Route: sip:135.180.130.133,<sip:135.180.130.131;transport=tcp>,\r\n"
  1520. "\t,Test Element <sip:[defa::00:12]:5061>;param=12+1\r\n"
  1521. "Path: Test <sip:[defa::00:12]:5061>\r\n"
  1522. "Service-Route: Test <sip:[defa::00:12]:5061>\r\n"
  1523. "Route: ,\r\n"
  1524. "Unknown-Extension: hip\r\n"
  1525. "Hide: hop\r\n"
  1526. "Max-Forwards: 12\r\n"
  1527. "Min-Expires: 150\r\n"
  1528. "Timestamp: 10.010 0.000100\r\n"
  1529. "Suppress-Body-If-Match: humppa \t\r\n"
  1530. "Suppress-Notify-If-Match: zumppa\r\n"
  1531. " \r\n"
  1532. "Content-Type: application/sdp\r\n"
  1533. "\r\n"
  1534. "v=0\r\n"
  1535. "o=mhandley 29739 7272939 IN IP4 126.5.4.3\r\n"
  1536. "c=IN IP4 135.180.130.88\r\n"
  1537. "m=audio 492170 RTP/AVP 0 12\r\n"
  1538. "m=video 3227 RTP/AVP 31\r\n"
  1539. "a=rtpmap:31 LPC\r\n"));
  1540. TEST_1(sip = sip_object(msg));
  1541. TEST(count(sip->sip_request->rq_common), 1);
  1542. TEST(count(sip->sip_to->a_common), 1);
  1543. TEST(count(sip->sip_from->a_common), 1);
  1544. TEST(count(sip->sip_cseq->cs_common), 1);
  1545. TEST(count(sip->sip_call_id->i_common), 1);
  1546. TEST(count(sip->sip_via->v_common), 4);
  1547. TEST(count(sip->sip_contact->m_common), 3);
  1548. TEST(count(sip->sip_content_type->c_common), 1);
  1549. TEST(count(sip->sip_route->r_common), 0);
  1550. TEST(count(sip->sip_record_route->r_common), 4);
  1551. #if SU_HAVE_EXPERIMENTAL
  1552. TEST(count(sip->sip_unknown->un_common), 2);
  1553. #else
  1554. TEST(count(sip->sip_unknown->un_common), 4);
  1555. #endif
  1556. TEST(count(sip->sip_error->er_common), 1);
  1557. TEST(count(sip->sip_max_forwards->mf_common), 1);
  1558. TEST(count(sip->sip_min_expires->me_common), 1);
  1559. TEST(count(sip->sip_timestamp->ts_common), 1);
  1560. TEST_S(sip->sip_contact->m_display, "Joe Bob Briggs");
  1561. TEST_1(sip->sip_contact->m_next->m_display != NULL);
  1562. TEST_S(sip->sip_contact->m_next->m_display, "");
  1563. TEST_1(sip->sip_contact->m_next->m_next->m_display == NULL);
  1564. TEST(sip->sip_max_forwards->mf_count, 12);
  1565. TEST(sip->sip_min_expires->me_delta, 150);
  1566. #if SU_HAVE_EXPERIMENTAL
  1567. {
  1568. sip_suppress_body_if_match_t *sbim;
  1569. sip_suppress_notify_if_match_t *snim;
  1570. TEST_1(sbim = sip_suppress_body_if_match(sip));
  1571. TEST_S(sbim->sbim_tag, "humppa");
  1572. TEST_SIZE(offsetof(msg_generic_t, g_value),
  1573. offsetof(sip_suppress_body_if_match_t, sbim_tag));
  1574. TEST_1(snim = sip_suppress_notify_if_match(sip));
  1575. TEST_S(snim->snim_tag, "zumppa");
  1576. TEST_SIZE(offsetof(msg_generic_t, g_value),
  1577. offsetof(sip_suppress_notify_if_match_t, snim_tag));
  1578. }
  1579. #endif
  1580. TEST_1(sip->sip_from->a_display);
  1581. TEST_S(sip->sip_from->a_display, "h");
  1582. v0 = sip->sip_via;
  1583. TEST_1(v = sip_via_copy(home, v0));
  1584. TEST(len(v->v_common), len(v0->v_common));
  1585. for (; v && v0; v = v->v_next, v0 = v0->v_next) {
  1586. if (v->v_params)
  1587. TEST_1(v->v_params != v0->v_params);
  1588. if (v->v_branch)
  1589. TEST_1(v->v_branch == v0->v_branch);
  1590. }
  1591. TEST_1(v == NULL && v0 == NULL);
  1592. v0 = sip->sip_via;
  1593. TEST_1(v = sip_via_dup(home, v0));
  1594. TEST(len(v->v_common), len(v0->v_common));
  1595. for (; v && v0; v = v->v_next, v0 = v0->v_next) {
  1596. if (v->v_params)
  1597. TEST_1(v->v_params != v0->v_params);
  1598. if (v->v_branch)
  1599. TEST_1(v->v_branch != v0->v_branch);
  1600. }
  1601. TEST_1(v == NULL && v0 == NULL);
  1602. TEST(sip_add_dup(msg, sip, (sip_header_t *)sip->sip_max_forwards), 0);
  1603. /* Max-Forwards is last header? */
  1604. TEST_P(sip->sip_max_forwards, sip->sip_content_type->c_common->h_succ);
  1605. TEST(sip_to_tag(home, sip->sip_to, sip->sip_to->a_tag), 0);
  1606. TEST(sip_to_tag(home, sip->sip_to, "tag=deadbeef"), 0);
  1607. TEST(sip_to_tag(home, sip->sip_to, "foofaa"), -1);
  1608. msg_header_remove(msg, (msg_pub_t *)sip, (msg_header_t *)sip->sip_payload);
  1609. TEST(sip_add_tl(msg, sip,
  1610. SIPTAG_FROM(SIP_NONE),
  1611. SIPTAG_VIA(SIP_NONE),
  1612. SIPTAG_VIA_STR("SIP/2.0/SCTP foo.bar.com:5060;branch=foo"),
  1613. SIPTAG_TO_STR("<sip:foo@bar>"),
  1614. SIPTAG_HEADER_STR("Authorization: Basic foobar\n"
  1615. "Priority:\n urgent"),
  1616. SIPTAG_HEADER_STR("Accept: foo/bar\n"
  1617. "\n"
  1618. "test payload\n"),
  1619. SIPTAG_TIMESTAMP(sip->sip_timestamp),
  1620. SIPTAG_END(),
  1621. SIPTAG_REFER_TO_STR("<sip:foo@bar>"),
  1622. TAG_END()), 0);
  1623. TEST_1(sip->sip_from == NULL);
  1624. TEST_1(sip->sip_via); TEST_1(sip->sip_via->v_next == NULL);
  1625. TEST_S(sip->sip_via->v_protocol, "SIP/2.0/SCTP");
  1626. TEST_1(sip->sip_authorization);
  1627. TEST_1(sip->sip_priority);
  1628. TEST_1(sip->sip_payload);
  1629. TEST_S(sip->sip_payload->pl_data, "test payload\n");
  1630. TEST_1(sip->sip_timestamp);
  1631. TEST_S(sip->sip_timestamp->ts_stamp, "10.010");
  1632. TEST_S(sip->sip_timestamp->ts_delay, "0.000100");
  1633. TEST_1(!sip->sip_refer_to);
  1634. TEST_1(tl = tl0 = tl_list(SIPTAG_TO_STR("<sip:foo@bar>"),
  1635. SIPTAG_END(),
  1636. SIPTAG_REFER_TO_STR("<sip:foo@bar>"),
  1637. TAG_END()));
  1638. /* sip_add_tagis should stop after SIPTAG_END() */
  1639. TEST(sip_add_tagis(msg, sip, &tl), 0);
  1640. TEST_P(tl, tl0 + 2);
  1641. tl_free(tl0);
  1642. TEST_P(sip_timestamp_make(home, "+1"), NULL);
  1643. TEST_P(sip_timestamp_make(home, "1.0e6 13.0"), NULL);
  1644. TEST_1(sip_timestamp_make(home, "1.0 .001"));
  1645. TEST_P(sip_timestamp_make(home, ".0001 13.0"), NULL);
  1646. TEST_1(x = sip->sip_path);
  1647. TEST_1(sip_add_make(msg, sip, sip_path_class, "<sip:135.180.130.133>") == 0);
  1648. TEST_P(x, sip->sip_path->r_next);
  1649. TEST_1(x = sip->sip_service_route);
  1650. TEST_1(sip_add_make(msg, sip, sip_service_route_class,
  1651. "<sip:135.180.130.133>") == 0);
  1652. TEST_P(x, sip->sip_service_route);
  1653. TEST_1(sip->sip_service_route->r_next);
  1654. /* Detect parsing errors */
  1655. TEST_1(!sip_cseq_make(home, "21874624876976 INVITE"));
  1656. TEST_1(!sip_cseq_make(home, "218746INVITE"));
  1657. msg_destroy(msg), msg = NULL;
  1658. su_home_unref(home), home = NULL;
  1659. END();
  1660. }
  1661. static int test_bad_packet(void)
  1662. {
  1663. msg_t *msg;
  1664. sip_t *sip;
  1665. su_home_t *home;
  1666. BEGIN();
  1667. home = su_home_new(sizeof *home);
  1668. TEST_1(msg = read_message(MSG_DO_EXTRACT_COPY,
  1669. "MESSAGE <sip:John_Smith@tct.hut.fi> SIP/2.0\r\n"
  1670. "To: John Smith <sip:John_Smith@tct.hut.fi:5066;user=ip;maddr=131.228.16.2>\r\n"
  1671. " ; tag = deadbeef\r\n"
  1672. "From:h<http://www.cs.columbia.edu>\r\n"
  1673. "Call-ID: 0ha0isndaksdj@10.1.2.3\r\n"
  1674. "CSeq : 8 MESSAGE\r\n"
  1675. "Via: SIP/2.0/UDP 135.180.130.133;received=defa:daf::00:12\r\n"
  1676. "Via: SIP/2.0/TCP 135.180.130.131;branch=deadbeef.barf;ttl=3;hidden,,"
  1677. "SIP/2.0/UDP\r\n 135.180.130.131:5061;received=[defa::00:12]\r\n"
  1678. "Contact: Joe Bob Briggs <urn:ipaddr:122.1.2.3> ; bar=\042foo baa\042, sip:kuik@foo.invalid\r\n"
  1679. "Via: SIP/2.0/UDP [aa:bb::1]:5061\0\0"));
  1680. TEST_1(sip = sip_object(msg));
  1681. TEST(count(sip->sip_request->rq_common), 1);
  1682. TEST(count(sip->sip_to->a_common), 1);
  1683. TEST(count(sip->sip_from->a_common), 1);
  1684. TEST(count(sip->sip_cseq->cs_common), 1);
  1685. TEST(count(sip->sip_call_id->i_common), 1);
  1686. TEST(count(sip->sip_via->v_common), 4);
  1687. TEST(count(sip->sip_route->r_common), 0);
  1688. TEST(sip->sip_request->rq_url->url_type, url_invalid);
  1689. su_home_unref(home), home = NULL;
  1690. msg_destroy(msg), msg = NULL;
  1691. END();
  1692. }
  1693. static int test_sip_list_header(void)
  1694. {
  1695. msg_t *msg;
  1696. sip_t *sip;
  1697. su_home_t *home;
  1698. sip_allow_t *a;
  1699. BEGIN();
  1700. home = su_home_new(sizeof *home);
  1701. TEST_1(msg = read_message(0,
  1702. "MESSAGE sip:John_Smith@tct.hut.fi SIP/2.0\r\n"
  1703. "To: John Smith <sip:John_Smith@tct.hut.fi:5066;user=ip;maddr=131.228.16.2>\r\n"
  1704. "From: <sip:joe@doe.org>;tag=foobar\r\n"
  1705. "Call-ID: 0ha0isndaksdj@10.1.2.3\r\n"
  1706. "CSeq : 8 MESSAGE\r\n"
  1707. "Via: SIP/2.0/UDP 135.180.130.133;received=defa:daf::00:12\r\n"
  1708. "Via: SIP/2.0/TCP 135.180.130.131;branch=deadbeef.barf;ttl=3;hidden,,"
  1709. "SIP/2.0/UDP\r\n 135.180.130.131:5061;received=[defa::00:12]\r\n"
  1710. "Contact: Joe Bob Briggs <urn:ipaddr:122.1.2.3> ; bar=\042foo baa\042, <sip:kuik@foo.invalid>, sip:barf\r\n"
  1711. "Allow: INVITE\r\n"
  1712. "Allow: ACK\r\n"
  1713. "Allow: CANCEL\r\n"
  1714. "Allow: BYE\r\n"
  1715. "Allow: OPTIONS\r\n"
  1716. "Allow: MESSAGE\r\n"
  1717. "Allow: KUIK\r\n"
  1718. "Max-Forwards: 12\r\n"
  1719. "Content-Type: text/plain\r\n"
  1720. "\r\n"
  1721. "hello\r\n"));
  1722. TEST_1(sip = sip_object(msg));
  1723. TEST_1(a = sip->sip_allow);
  1724. TEST_1(a->k_items);
  1725. TEST_1(a->k_next == NULL);
  1726. TEST_1(sip_is_allowed(a, SIP_METHOD_INVITE));
  1727. TEST_1(!sip_is_allowed(a, SIP_METHOD_PUBLISH));
  1728. TEST_1(sip_is_allowed(a, SIP_METHOD(KUIK)));
  1729. TEST_1(!sip_is_allowed(a, SIP_METHOD(kuik)));
  1730. TEST_1(a = sip_allow_make(home, ""));
  1731. TEST_S(sip_header_as_string(home, (void *)a), "");
  1732. TEST_1(a = sip_allow_make(home, "INVITE, PUBLISH"));
  1733. TEST_1(sip_is_allowed(a, SIP_METHOD_INVITE));
  1734. /* Test with list header */
  1735. TEST_1(msg_header_add_dup(msg, NULL, (msg_header_t *)a) == 0);
  1736. TEST_1(a = sip_allow_make(home, "MESSAGE, SUBSCRIBE"));
  1737. TEST_1(msg_header_add_dup(msg, NULL, (msg_header_t *)a) == 0);
  1738. TEST_1(msg_header_add_make(msg, NULL, sip_allow_class, "kuik") == 0);
  1739. TEST_1(a = sip->sip_allow);
  1740. TEST_1(a->k_items);
  1741. TEST_S(a->k_items[0], "INVITE");
  1742. TEST_S(a->k_items[1], "ACK");
  1743. TEST_S(a->k_items[2], "CANCEL");
  1744. TEST_S(a->k_items[3], "BYE");
  1745. TEST_S(a->k_items[4], "OPTIONS");
  1746. TEST_S(a->k_items[5], "MESSAGE");
  1747. TEST_S(a->k_items[6], "KUIK");
  1748. TEST_S(a->k_items[7], "PUBLISH");
  1749. TEST_S(a->k_items[8], "SUBSCRIBE");
  1750. TEST_S(a->k_items[9], "kuik");
  1751. TEST_P(a->k_items[10], NULL);
  1752. msg_destroy(msg), msg = NULL;
  1753. su_home_unref(home), home = NULL;
  1754. END();
  1755. }
  1756. static int test_prack(void)
  1757. {
  1758. /* Test RAck and RSeq */
  1759. su_home_t *home;
  1760. sip_rack_t *rack, *rack0;
  1761. sip_rseq_t *rseq, *rseq0;
  1762. BEGIN();
  1763. TEST_1(home = su_home_create());
  1764. TEST_1(rack = sip_rack_make(home, "1 2 INVITE"));
  1765. TEST(rack->ra_response, 1);
  1766. TEST(rack->ra_cseq, 2);
  1767. TEST(rack->ra_method, sip_method_invite);
  1768. TEST_S(rack->ra_method_name, "INVITE");
  1769. TEST_1(rseq = sip_rseq_make(home, "3"));
  1770. TEST(rseq->rs_response, 3);
  1771. TEST_1(rack0 = sip_rack_dup(home, rack));
  1772. TEST_P(rack0->ra_method_name, rack->ra_method_name);
  1773. TEST_1(rseq0 = sip_rseq_dup(home, rseq));
  1774. TEST_1(rack = sip_rack_make(home, "4\r\n\t5\r\n\tEXTRA"));
  1775. TEST(rack->ra_response, 4);
  1776. TEST(rack->ra_cseq, 5);
  1777. TEST(rack->ra_method, sip_method_unknown);
  1778. TEST_S(rack->ra_method_name, "EXTRA");
  1779. TEST_1(rseq = sip_rseq_make(home, " 6 "));
  1780. TEST(rseq->rs_response, 6);
  1781. TEST_1(rack0 = sip_rack_dup(home, rack));
  1782. TEST_1(rack0->ra_method_name != rack->ra_method_name);
  1783. TEST_1(rseq0 = sip_rseq_dup(home, rseq));
  1784. su_home_unref(home);
  1785. END();
  1786. }
  1787. /* Test MIME headers */
  1788. static int test_accept(void)
  1789. {
  1790. /* Test Accept header */
  1791. sip_accept_t *ac, *ac0;
  1792. sip_accept_encoding_t *aa;
  1793. su_home_t *home;
  1794. BEGIN();
  1795. TEST_1(home = su_home_create());
  1796. TEST_1(ac = ac0 = sip_accept_make(home, "image / jpeg ; q = 0.6,, image/png, image/*, */* "));
  1797. TEST_S(ac->ac_type, "image/jpeg");
  1798. TEST_S(ac->ac_subtype, "jpeg");
  1799. TEST_1(ac->ac_params && ac->ac_params[0]);
  1800. TEST_S(ac->ac_params[0], "q=0.6");
  1801. TEST_S(ac->ac_q, "0.6");
  1802. TEST_1(ac = ac->ac_next);
  1803. TEST_S(ac->ac_type, "image/png");
  1804. TEST_S(ac->ac_subtype, "png");
  1805. TEST_1(ac = ac->ac_next);
  1806. TEST_S(ac->ac_type, "image/*");
  1807. TEST_S(ac->ac_subtype, "*");
  1808. TEST_1(aa = sip_accept_encoding_make(home, "gzip"));
  1809. TEST_1(aa = sip_accept_encoding_make(home, "gzip;q=1.0,deflate;q=1.0"));
  1810. TEST_S(aa->aa_value, "gzip"); TEST_S(aa->aa_q, "1.0");
  1811. TEST_1(aa->aa_next);
  1812. TEST_S(aa->aa_next->aa_value, "deflate");
  1813. TEST_1(aa = sip_accept_encoding_make(home, ","));
  1814. TEST_S(aa->aa_value, ""); TEST_1(!aa->aa_next);
  1815. TEST_1(aa = sip_accept_encoding_make(home, ""));
  1816. TEST_S(aa->aa_value, ""); TEST_1(!aa->aa_next);
  1817. TEST_1(aa = sip_accept_language_make(home, "fi"));
  1818. TEST_1(aa = sip_accept_language_make(home, "fi;q=1.0,sv;q=1.0"));
  1819. TEST_S(aa->aa_value, "fi"); TEST_S(aa->aa_q, "1.0");
  1820. TEST_1(aa->aa_next);
  1821. TEST_S(aa->aa_next->aa_value, "sv");
  1822. TEST_1(aa = sip_accept_language_make(home, ","));
  1823. TEST_S(aa->aa_value, ""); TEST_1(!aa->aa_next);
  1824. TEST_1(aa = sip_accept_language_make(home, ""));
  1825. TEST_S(aa->aa_value, ""); TEST_1(!aa->aa_next);
  1826. su_home_unref(home);
  1827. END();
  1828. }
  1829. static int test_content_disposition(void)
  1830. {
  1831. sip_content_disposition_t *cd, *cd0;
  1832. su_home_t *home;
  1833. BEGIN();
  1834. TEST_1(home = su_home_create());
  1835. TEST_1(cd = cd0 = sip_content_disposition_make(home, "sip-cgi ; action = store;handling=required "));
  1836. TEST_S(cd->cd_type, "sip-cgi");
  1837. TEST_1(cd->cd_params && cd->cd_params[0] && cd->cd_params[1] && !cd->cd_params[2]);
  1838. TEST_S(cd->cd_params[0], "action=store");
  1839. TEST_S(cd->cd_params[1], "handling=required");
  1840. TEST_S(cd->cd_handling, "required");
  1841. TEST_1(cd->cd_required);
  1842. TEST_1(!cd->cd_optional);
  1843. su_home_unref(home);
  1844. END();
  1845. }
  1846. static int test_content_type(void)
  1847. {
  1848. sip_content_type_t *c;
  1849. sip_content_type_t c0[1];
  1850. su_home_t *home;
  1851. BEGIN();
  1852. TEST_1(home = su_home_create());
  1853. TEST_1(c = sip_content_type_make(home, "application/sdp ; charset = utf-8"));
  1854. TEST_S(c->c_type, "application/sdp");
  1855. TEST_S(c->c_subtype, "sdp");
  1856. TEST_1(c->c_params && c->c_params[0] && !c->c_params[1]);
  1857. TEST_S(c->c_params[0], "charset=utf-8");
  1858. TEST_P(c->c_params[1], NULL);
  1859. sip_content_type_init(c0);
  1860. c = sip_content_type_dup(home, c0);
  1861. TEST_P(c->c_type, NULL);
  1862. TEST_P(c->c_subtype, NULL);
  1863. c0->c_type = "text";
  1864. c = sip_content_type_dup(home, c0);
  1865. TEST_S(c->c_type, "text");
  1866. TEST_P(c->c_subtype, NULL);
  1867. su_home_unref(home);
  1868. END();
  1869. }
  1870. static int test_www_authenticate(void)
  1871. {
  1872. sip_www_authenticate_t *www;
  1873. su_home_t *home;
  1874. char const *s;
  1875. msg_t *msg; sip_t *sip;
  1876. BEGIN();
  1877. TEST_1(home = su_home_create());
  1878. TEST_1(www = sip_www_authenticate_make
  1879. (home, "Digest realm=\"Registered_Subscribers\",\n"
  1880. "domain=\"sip:206.229.26.61\",\n"
  1881. "nonce=\"20dfb7e5a77abee7a02dbe53efe42cdd\", "
  1882. "opaque=\"423767123y723742376423762376423784623782a794e58\",\n"
  1883. "stale=FALSE,algorithm=MD5"));
  1884. TEST_S(www->au_scheme, "Digest");
  1885. TEST_1(www->au_params && www->au_params[0] && www->au_params[1] && www->au_params[2] &&
  1886. www->au_params[3] && www->au_params[4] && www->au_params[5] &&
  1887. !www->au_params[6]);
  1888. TEST_1(s = sip_header_as_string(home, (sip_header_t *)www));
  1889. TEST_1(strlen(s) >= 128);
  1890. su_home_unref(home);
  1891. TEST_1(
  1892. msg = read_message(
  1893. MSG_DO_EXTRACT_COPY,
  1894. "SIP/2.0 401 Unauthorized" "\r\n"
  1895. "Date: Wed, 07 Jan 2009 22:24:39 GMT" "\r\n"
  1896. "WWW-Authenticate: Kerberos realm=\"SIP Communications Service\", targetname=\"sip/OCS1.flux.local\", version=3" "\r\n"
  1897. "WWW-Authenticate: NTLM realm=\"SIP Communications Service\", targetname=\"OCS1.flux.local\", version=3" "\r\n"
  1898. "From: <sip:192.168.43.1:5069>;epid=1234567890;tag=48BXgr379e85j" "\r\n"
  1899. "To: <sip:1234@192.168.43.20:5061>;transport=tls;tag=B57737091022903031FF204696B79CC4" "\r\n"
  1900. "Call-ID: cf12f708-57ac-122c-ad90-6f23d7babf4f" "\r\n"
  1901. "CSeq: 109565202 REGISTER" "\r\n"
  1902. "Via: SIP/2.0/TLS 192.168.43.1:5069;branch=z9hG4bK47ZUrFK0v5eQa;received=192.168.43.1;ms-received-port=54059;ms-received-cid=E500" "\r\n"
  1903. "Content-Length: 0" "\r\n"));
  1904. TEST_1(sip = sip_object(msg));
  1905. TEST_1(www = sip->sip_www_authenticate);
  1906. TEST_S(www->au_scheme, "Kerberos");
  1907. TEST_1(www = www->au_next);
  1908. TEST_S(www->au_scheme, "NTLM");
  1909. msg_destroy(msg);
  1910. END();
  1911. }
  1912. int test_retry_after(void)
  1913. {
  1914. /* Test Session-Expires header */
  1915. sip_retry_after_t *af, *af0;
  1916. su_home_t *home;
  1917. char buf[64];
  1918. BEGIN();
  1919. TEST_1(home = su_home_create());
  1920. TEST_1(af = sip_retry_after_make(home, "1800"));
  1921. TEST(af->af_delta, 1800);
  1922. TEST_1(af = sip_retry_after_make(home, "1800(foo); duration = 3600"));
  1923. TEST_1(af->af_params && af->af_params[0]);
  1924. TEST_S(af->af_comment, "foo");
  1925. TEST_S(af->af_params[0], "duration=3600");
  1926. TEST_S(af->af_duration, "3600");
  1927. TEST_1(af0 = sip_retry_after_dup(home, af));
  1928. TEST_1(af0->af_params && af0->af_params[0]);
  1929. TEST_S(af0->af_comment, "foo");
  1930. TEST_S(af0->af_params[0], "duration=3600");
  1931. TEST_S(af0->af_duration, "3600");
  1932. TEST_1(sip_retry_after_e(buf, sizeof(buf), (sip_header_t *)af0, 0));
  1933. TEST_S(buf, "1800 (foo) ;duration=3600");
  1934. su_home_unref(home);
  1935. END();
  1936. }
  1937. int test_session_expires(void)
  1938. {
  1939. /* Test Session-Expires header */
  1940. sip_session_expires_t *x, *x0;
  1941. su_home_t *home;
  1942. BEGIN();
  1943. TEST_1(home = su_home_create());
  1944. TEST_1(x = x0 = sip_session_expires_make(home, "1800"));
  1945. TEST(x->x_delta, 1800);
  1946. TEST_1(x = x0 = sip_session_expires_make(home, "1800 ; refresher = uas"));
  1947. TEST_1(x->x_params && x->x_params[0]);
  1948. TEST_S(x->x_params[0], "refresher=uas");
  1949. TEST_S(x->x_refresher, "uas");
  1950. su_home_unref(home);
  1951. END();
  1952. }
  1953. int test_min_se(void)
  1954. {
  1955. /* Test Min-SE header */
  1956. sip_min_se_t *min, *min0;
  1957. su_home_t *home;
  1958. BEGIN();
  1959. TEST_1(home = su_home_create());
  1960. TEST_1(min = min0 = sip_min_se_make(home, "1800"));
  1961. TEST(min->min_delta, 1800);
  1962. TEST_1(min = sip_min_se_dup(home, min0));
  1963. TEST(min->min_delta, 1800);
  1964. TEST_1(min = sip_min_se_copy(home, min0));
  1965. TEST(min->min_delta, 1800);
  1966. TEST_1(min = sip_min_se_make(home, "1999 ; foo = bar"));
  1967. TEST(min->min_delta, 1999);
  1968. TEST_1(min->min_params);
  1969. TEST_S(min->min_params[0], "foo=bar");
  1970. TEST_1(min0 = sip_min_se_dup(home, min));
  1971. TEST(min0->min_delta, 1999);
  1972. TEST_1(min0->min_params);
  1973. TEST_S(min0->min_params[0], "foo=bar");
  1974. su_home_unref(home);
  1975. END();
  1976. }
  1977. int test_refer(void)
  1978. {
  1979. sip_refer_to_t *r, *r0;
  1980. sip_referred_by_t *b, *b0;
  1981. sip_replaces_t *rp, *rp0;
  1982. char const *s0;
  1983. su_home_t *home;
  1984. BEGIN();
  1985. char const m[] =
  1986. "REFER sip:10.3.3.104 SIP/2.0\r\n"
  1987. "Via: SIP/2.0/UDP 10.3.3.8;branch=z9hG4bKb8389b4c1BA8899\r\n"
  1988. "From: \"Anthony Minessale\" <sip:polycom500@10.3.3.104>;tag=5AA04E0-66CFC37F\r\n"
  1989. "To: <sip:3001@10.3.3.104>;user=phone;tag=j6Fg9y7t8KNrF\r\n"
  1990. "CSeq: 4 REFER\r\n"
  1991. "Call-ID: a14822a4-5932e3ea-d7f37191@10.3.3.8\r\n"
  1992. "Contact: <sip:polycom500@10.3.3.8>\r\n"
  1993. "User-Agent: PolycomSoundPointIP-SPIP_500-UA/1.4.1\r\n"
  1994. "Refer-To: <sip:2000@10.3.3.104?Replaces=7d84c014-321368da-efa90f41%40"
  1995. "10.3.3.8%3Bto-tag%3DpaNKgBB9vQe3D%3Bfrom-tag%3D93AC8D50-7CF6DAAF>\r\n"
  1996. "Referred-By: \"Anthony Minessale\" <sip:polycom500@10.3.3.104>\r\n"
  1997. "Refer-Sub: true\r\n"
  1998. "Max-Forwards: 70\r\n"
  1999. "Content-Length: 0\r\n"
  2000. "\r\n";
  2001. msg_t *msg;
  2002. sip_t *sip;
  2003. msg_iovec_t *iovec;
  2004. isize_t veclen, i, size;
  2005. char *back;
  2006. sip_refer_sub_t *rs;
  2007. TEST_1(home = su_home_create());
  2008. /* Check that Refer-Sub has now been added to our parser */
  2009. TEST_1(msg_mclass_insert_with_mask(test_mclass, sip_refer_sub_class,
  2010. 0, 0) == -1);
  2011. msg = read_message(0, m); TEST_1(msg); TEST_1(sip = sip_object(msg));
  2012. TEST_1(sip->sip_refer_to);
  2013. TEST_S(sip->sip_refer_to->r_url->url_headers,
  2014. "Replaces=7d84c014-321368da-efa90f41%40"
  2015. "10.3.3.8%3Bto-tag%3DpaNKgBB9vQe3D%3Bfrom-tag%3D93AC8D50-7CF6DAAF");
  2016. TEST_1(rs = sip_refer_sub(sip));
  2017. TEST_S(rs->rs_value, "true");
  2018. TEST_SIZE(msg_prepare(msg), strlen(m));
  2019. TEST_1(veclen = msg_iovec(msg, NULL, ISIZE_MAX));
  2020. TEST_1(iovec = su_zalloc(msg_home(home), veclen * (sizeof iovec[0])));
  2021. TEST_SIZE(msg_iovec(msg, iovec, veclen), veclen);
  2022. for (i = 0, size = 0; i < veclen; i++)
  2023. size += iovec[i].mv_len;
  2024. TEST_1(back = su_zalloc(msg_home(msg), size + 1));
  2025. for (i = 0, size = 0; i < veclen; i++) {
  2026. memcpy(back + size, iovec[i].mv_base, iovec[i].mv_len);
  2027. size += iovec[i].mv_len;
  2028. }
  2029. back[size] = '\0';
  2030. TEST_S(back, m);
  2031. TEST_1(r = r0 = sip_refer_to_make(home, "http://example.com;foo=bar"));
  2032. TEST(r->r_url->url_type, url_http);
  2033. TEST_1(r->r_params);
  2034. TEST_S(r->r_params[0], "foo=bar");
  2035. r = sip_refer_to_dup(home, r0);
  2036. TEST(r->r_url->url_type, url_http);
  2037. TEST_1(r->r_params);
  2038. TEST_S(r->r_params[0], "foo=bar");
  2039. TEST_1(r = r0 = sip_refer_to_make(home, s0 = "<http://example.com>"));
  2040. TEST_S(r->r_display, "");
  2041. TEST(r->r_url->url_type, url_http);
  2042. TEST_P(r->r_params, NULL);
  2043. r = sip_refer_to_dup(home, r0);
  2044. TEST_S(r->r_display, "");
  2045. TEST(r->r_url->url_type, url_http);
  2046. TEST_P(r->r_params, NULL);
  2047. TEST_S(sip_header_as_string(home, (sip_header_t*)r), s0);
  2048. TEST_1(r = r0 = sip_refer_to_make(home,
  2049. "Web Site <http://example.com>;foo=bar"));
  2050. TEST_S(r->r_display, "Web Site");
  2051. TEST(r->r_url->url_type, url_http);
  2052. TEST_1(r->r_params);
  2053. TEST_S(r->r_params[0], "foo=bar");
  2054. TEST_P(r->r_params[1], NULL);
  2055. r = sip_refer_to_dup(home, r0);
  2056. TEST(r->r_url->url_type, url_http);
  2057. TEST_1(r->r_params);
  2058. TEST_S(r->r_params[0], "foo=bar");
  2059. TEST_P(r->r_params[1], NULL);
  2060. /* Test bad replaces without <> */
  2061. {
  2062. char const s[] =
  2063. "sip:2000@10.3.3.104?Replaces=7d84c014-321368da-efa90f41%4010.3.3.8"
  2064. "%3Bto-tag%3DpaNKgBB9vQe3D%3Bfrom-tag%3D93AC8D50-7CF6DAAF" "\r\n";
  2065. char *str;
  2066. TEST_1(r = r0 = sip_refer_to_make(home, s));
  2067. msg_fragment_clear(r->r_common);
  2068. TEST_1(str = sip_header_as_string(home, (void *)r));
  2069. TEST_S(str,
  2070. "<"
  2071. "sip:2000@10.3.3.104?Replaces=7d84c014-321368da-efa90f41%4010.3.3.8"
  2072. "%3Bto-tag%3DpaNKgBB9vQe3D%3Bfrom-tag%3D93AC8D50-7CF6DAAF"
  2073. ">");
  2074. }
  2075. su_home_unref(home);
  2076. TEST_1(home = su_home_create());
  2077. TEST_1(b = b0 = sip_referred_by_make(home,
  2078. "sip:joe@example.edu;param=value"));
  2079. TEST_P(b->b_display, NULL);
  2080. TEST_1(b->b_params);
  2081. TEST_P(b->b_cid, NULL);
  2082. TEST_1(b = sip_referred_by_make(home,
  2083. "John Doe <sip:joe@example.edu>"
  2084. ";cid=\"foo@bar\""));
  2085. TEST_S(b->b_display, "John Doe");
  2086. TEST_1(b->b_params);
  2087. TEST_1(b->b_cid);
  2088. TEST_S(b->b_params[0] + 4, b->b_cid);
  2089. b = sip_referred_by_dup(home, b0 = b);
  2090. TEST_1(b);
  2091. TEST_S(b->b_display, "John Doe");
  2092. TEST_1(b->b_cid);
  2093. TEST_S(b->b_params[0] + 4, b->b_cid);
  2094. TEST_S(b->b_cid, b0->b_cid);
  2095. TEST(msg_header_replace_param(home, b->b_common, "cid=cid:8u432658725"), 1);
  2096. TEST_S(b->b_cid, "cid:8u432658725");
  2097. TEST(msg_header_remove_param(b->b_common, "cid"), 1);
  2098. TEST_P(b->b_cid, NULL);
  2099. /* XXX */
  2100. #define WORD ALPHA DIGIT "-.!%*_+`'~()<>:\\\"/[]?{}"
  2101. rp = sip_replaces_make(home, WORD "@" WORD ";to-tag=foo;from-tag=bar"
  2102. ";early-only = yes-please ");
  2103. TEST_1(rp);
  2104. TEST_S(rp->rp_call_id, WORD "@" WORD);
  2105. TEST_S(rp->rp_to_tag, "foo");
  2106. TEST_S(rp->rp_from_tag, "bar");
  2107. TEST(rp->rp_early_only, 1);
  2108. rp = sip_replaces_dup(home, rp0 = rp);
  2109. TEST_1(rp);
  2110. TEST_S(rp->rp_call_id, WORD "@" WORD);
  2111. TEST_S(rp->rp_to_tag, "foo");
  2112. TEST_S(rp->rp_from_tag, "bar");
  2113. TEST(rp->rp_early_only, 1);
  2114. TEST(msg_header_replace_param(home, rp->rp_common, "early-only"), 1);
  2115. TEST(rp->rp_early_only, 1);
  2116. TEST(msg_header_remove_param(rp->rp_common, "from-tag"), 1);
  2117. TEST_P(rp->rp_from_tag, NULL);
  2118. TEST(msg_header_remove_param(rp->rp_common, "to-tag"), 1);
  2119. TEST_P(rp->rp_to_tag, NULL);
  2120. su_home_unref(home);
  2121. END();
  2122. }
  2123. static int test_features(void)
  2124. {
  2125. /* Test Proxy-Required, Require, Supported, and Unsupported headers */
  2126. sip_proxy_require_t *pr;
  2127. sip_require_t *r;
  2128. sip_supported_t *s;
  2129. sip_unsupported_t *u, *u1;
  2130. su_home_t *home;
  2131. BEGIN();
  2132. TEST_1(home = su_home_create());
  2133. TEST_1(pr = sip_proxy_require_make(home, "foo, bar, baz, dig, dug"));
  2134. TEST_1(r = sip_require_make(home, "dig, dug"));
  2135. TEST_1(s = sip_supported_make(home, "foo, baz, dug"));
  2136. TEST_1(pr->k_items); TEST_S(pr->k_items[0], "foo");
  2137. TEST_1(r->k_items); TEST_S(r->k_items[0], "dig");
  2138. TEST_1(s->k_items); TEST_S(s->k_items[0], "foo");
  2139. TEST_1(u = sip_has_unsupported(home, s, pr));
  2140. TEST_1(u->k_items);
  2141. TEST_S(u->k_items[0], "bar");
  2142. TEST_S(u->k_items[1], "dig");
  2143. TEST_P(u->k_items[2], NULL);
  2144. TEST_1(u1 = sip_has_unsupported(home, s, r));
  2145. TEST_1(u1->k_items); TEST_S(u1->k_items[0], "dig"); TEST_1(!u1->k_items[1]);
  2146. TEST_1(sip_has_supported(s, "foo"));
  2147. TEST_1(sip_has_supported(s, "baz"));
  2148. TEST_1(sip_has_supported(s, "dug"));
  2149. TEST_1(!sip_has_supported(s, "dig"));
  2150. TEST_1(!sip_has_supported(s, "dag.2"));
  2151. TEST_1(sip_has_supported(s, NULL));
  2152. TEST_1(sip_has_supported(NULL, NULL));
  2153. TEST_1(!sip_has_supported(NULL, "foo"));
  2154. su_home_unref(home);
  2155. END();
  2156. }
  2157. #if 0
  2158. static int sip_time_test(void)
  2159. {
  2160. sip_contact_t *m;
  2161. sip_expires_t *ex;
  2162. sip_date_t *date = NULL;
  2163. sip_time_t default = 3600;
  2164. BEGIN();
  2165. sip_time_t sip_contact_expires(sip_contact_t const *m,
  2166. sip_expires_t const *ex,
  2167. sip_date_t const *date,
  2168. sip_time_t def,
  2169. sip_time_t now);
  2170. END();
  2171. }
  2172. #endif
  2173. static int test_events(void)
  2174. {
  2175. sip_event_t *o;
  2176. sip_allow_events_t *ae;
  2177. sip_subscription_state_t *ss;
  2178. su_home_t *home;
  2179. msg_t *msg;
  2180. sip_t *sip;
  2181. BEGIN();
  2182. TEST_1(home = su_home_create());
  2183. TEST_1((o = sip_event_make(home, "presence;id=1")));
  2184. TEST_S(o->o_type, "presence");
  2185. TEST_S(o->o_id, "1");
  2186. TEST(msg_header_remove_param(o->o_common, "ix=0"), 0);
  2187. TEST_S(o->o_id, "1");
  2188. TEST(msg_header_remove_param(o->o_common, "id"), 1);
  2189. TEST_P(o->o_id, NULL);
  2190. TEST(msg_header_replace_param(home, o->o_common, "id=32"), 0);
  2191. TEST_S(o->o_id, "32");
  2192. TEST_1((ae = sip_allow_events_make(home, "presence, presence.winfo, foo")));
  2193. TEST_1(ae->k_items);
  2194. TEST_S(ae->k_items[0], "presence");
  2195. TEST_S(ae->k_items[1], "presence.winfo");
  2196. TEST_S(ae->k_items[2], "foo");
  2197. TEST_P(ae->k_items[3], 0);
  2198. TEST(sip_allow_events_add(home, ae, "event3"), 0);
  2199. TEST_S(ae->k_items[3], "event3");
  2200. TEST(sip_allow_events_add(home, ae, "event4"), 0);
  2201. TEST_S(ae->k_items[4], "event4");
  2202. TEST(sip_allow_events_add(home, ae, "event5"), 0);
  2203. TEST_S(ae->k_items[5], "event5");
  2204. TEST(sip_allow_events_add(home, ae, "event6"), 0);
  2205. TEST_S(ae->k_items[6], "event6");
  2206. TEST(sip_allow_events_add(home, ae, "event7"), 0);
  2207. TEST_S(ae->k_items[7], "event7");
  2208. TEST(sip_allow_events_add(home, ae, "event8"), 0);
  2209. TEST_S(ae->k_items[8], "event8");
  2210. TEST_1((ss =
  2211. sip_subscription_state_make(home, "terminated ; reason=timeout")));
  2212. TEST_S(ss->ss_substate, "terminated");
  2213. TEST_S(ss->ss_reason, "timeout");
  2214. TEST(msg_header_replace_param(home, ss->ss_common, "reason=TimeOut"), 1);
  2215. TEST_S(ss->ss_reason, "TimeOut");
  2216. TEST(msg_header_remove_param(ss->ss_common, "reasom"), 0);
  2217. TEST_S(ss->ss_reason, "TimeOut");
  2218. TEST(msg_header_remove_param(ss->ss_common, "reason"), 1);
  2219. TEST_P(ss->ss_reason, NULL);
  2220. TEST(msg_header_replace_param(home, ss->ss_common, "expires=200"), 0);
  2221. TEST(msg_header_replace_param(home, ss->ss_common, "retry-after=10"), 0);
  2222. TEST_S(ss->ss_expires, "200");
  2223. TEST_S(ss->ss_retry_after, "10");
  2224. TEST_1((ss =
  2225. sip_subscription_state_make(home, "active;expires=2")));
  2226. TEST_S(ss->ss_substate, "active");
  2227. TEST_S(ss->ss_expires, "2");
  2228. TEST_1((ss =
  2229. sip_subscription_state_make(home, "terminated;retry-after=3600")));
  2230. TEST_S(ss->ss_substate, "terminated");
  2231. TEST_P(ss->ss_expires, NULL);
  2232. TEST_S(ss->ss_retry_after, "3600");
  2233. TEST_1((ss = sip_subscription_state_dup(home, ss)));
  2234. TEST_S(ss->ss_substate, "terminated");
  2235. TEST_P(ss->ss_expires, NULL);
  2236. TEST_S(ss->ss_retry_after, "3600");
  2237. msg = read_message(MSG_DO_EXTRACT_COPY,
  2238. "SIP/2.0 202 Accepted\r\n"
  2239. "To: <sip:foo@bar>;tag=deadbeef\r\n"
  2240. "From: <sip:bar@foo>;\r\n"
  2241. "Call-ID: 0ha0isndaksdj@10.1.2.3\r\n"
  2242. "CSeq: 8 SUBSCRIBE\r\n"
  2243. "Via: SIP/2.0/UDP 135.180.130.133\r\n"
  2244. "Event: foo;id=1\r\n"
  2245. "Allow-Events: bar, foo, zap\r\n"
  2246. "Subscription-State: terminated;reason=probation;retry-after=100000\r\n"
  2247. "Content-Length: 0\r\n"
  2248. "\r\n");
  2249. sip = sip_object(msg);
  2250. TEST_1(msg);
  2251. TEST_1(sip);
  2252. TEST_1(sip->sip_event);
  2253. TEST_1(sip->sip_allow_events);
  2254. TEST_1(sip->sip_event->o_type);
  2255. TEST_S(sip->sip_event->o_type, "foo");
  2256. TEST_1(sip->sip_event->o_id);
  2257. TEST_S(sip->sip_event->o_id, "1");
  2258. TEST_1(sip->sip_allow_events);
  2259. su_home_unref(home);
  2260. msg_destroy(msg), msg = NULL;
  2261. END();
  2262. }
  2263. static int test_route(void)
  2264. {
  2265. sip_record_route_t *r0, *r1;
  2266. sip_record_route_t *rr;
  2267. sip_path_t *p, *p0;
  2268. sip_service_route_t *sr, *sr0;
  2269. su_home_t *home;
  2270. BEGIN();
  2271. TEST_1(home = su_home_create());
  2272. TEST_1((rr = sip_record_route_make(home, "sip:foo.bar;lr")));
  2273. TEST_1(rr->r_params);
  2274. TEST_1((r0 = sip_record_route_make(home, "<sip:0@foo.bar:555;lr>")));
  2275. TEST_P(r0->r_params, NULL);
  2276. TEST_1(r0->r_url->url_params);
  2277. TEST_1((r1 = sip_record_route_make(home, "<sip:1@foo.bar:666"
  2278. ";maddr=127.0.0.1>")));
  2279. TEST_P(r1->r_params, NULL);
  2280. TEST_1(r1->r_url->url_params);
  2281. TEST_1((rr = sip_record_route_create(home, r0->r_url, r1->r_url)));
  2282. TEST_S(rr->r_url->url_user, "0");
  2283. TEST_S(rr->r_url->url_port, "666");
  2284. TEST_S(rr->r_url->url_params, "maddr=127.0.0.1");
  2285. TEST_1((rr = sip_record_route_create(home, r1->r_url, r0->r_url)));
  2286. TEST_S(rr->r_url->url_user, "1");
  2287. TEST_S(rr->r_url->url_port, "555");
  2288. TEST_S(rr->r_url->url_params, "lr;maddr=foo.bar");
  2289. TEST_1(!sip_path_make(home, "<sip:foo@[bar:50>;lr"));
  2290. TEST_1(p = sip_path_make(home, "<sip:foo@[baa::1]:5060>;lr"));
  2291. TEST_1(p0 = sip_path_dup(home, p));
  2292. su_free(home, p);
  2293. su_free(home, p0);
  2294. TEST_1(!sip_service_route_make(home, "<sip:foo@[bar:50>;lr"));
  2295. TEST_1(!sip_service_route_make(home,
  2296. "<sip:foo@[baa::1]>;lr bar, sip:foo"));
  2297. TEST_1(sr = sip_service_route_make(home, "<sip:foo@[baa::1]:5060>;lr"));
  2298. TEST_1(sr0 = sip_service_route_dup(home, sr));
  2299. su_free(home, sr);
  2300. TEST_1(sr = sip_service_route_make(home, "sip:foo@[baa::1]:5060;lr"));
  2301. su_free(home, sr);
  2302. su_free(home, sr0);
  2303. su_home_unref(home);
  2304. END();
  2305. }
  2306. /* Test Request-Disposition header */
  2307. int test_request_disposition(void)
  2308. {
  2309. sip_request_disposition_t *rd;
  2310. su_home_t *home;
  2311. BEGIN();
  2312. TEST_1(home = su_home_create());
  2313. TEST_1(rd = sip_request_disposition_make(home, "proxy, recurse, parallel"));
  2314. TEST_S(rd->rd_items[1], "recurse");
  2315. su_home_unref(home);
  2316. END();
  2317. }
  2318. #include <float.h>
  2319. #include <math.h>
  2320. int test_caller_prefs(void)
  2321. {
  2322. sip_accept_contact_t *ac;
  2323. sip_accept_contact_t *cp;
  2324. sip_reject_contact_t *rejc;
  2325. su_home_t *home;
  2326. char const *s;
  2327. int negate, error;
  2328. unsigned S, N;
  2329. union sip_pref sp[1], a[1];
  2330. sip_contact_t *m, *m0, *m1, *m2;
  2331. BEGIN();
  2332. TEST_1(home = su_home_new(sizeof *home));
  2333. TEST_1(!sip_is_callerpref("attendant"));
  2334. TEST_1(sip_is_callerpref("audio"));
  2335. TEST_1(sip_is_callerpref("automata"));
  2336. TEST_1(sip_is_callerpref("class"));
  2337. TEST_1(sip_is_callerpref("duplex"));
  2338. TEST_1(sip_is_callerpref("data"));
  2339. TEST_1(sip_is_callerpref("control"));
  2340. TEST_1(sip_is_callerpref("mobility"));
  2341. TEST_1(sip_is_callerpref("description"));
  2342. TEST_1(sip_is_callerpref("events"));
  2343. TEST_1(sip_is_callerpref("priority"));
  2344. TEST_1(sip_is_callerpref("methods"));
  2345. TEST_1(sip_is_callerpref("schemes"));
  2346. TEST_1(sip_is_callerpref("application"));
  2347. TEST_1(sip_is_callerpref("video"));
  2348. TEST_1(sip_is_callerpref("actor"));
  2349. TEST_1(!sip_is_callerpref("+actor"));
  2350. TEST_1(!sip_is_callerpref("msgserver"));
  2351. TEST_1(sip_is_callerpref("language"));
  2352. TEST_1(sip_is_callerpref("isfocus"));
  2353. TEST_1(sip_is_callerpref("type"));
  2354. TEST_1(!sip_is_callerpref("uri-user"));
  2355. TEST_1(!sip_is_callerpref("uri-domain"));
  2356. TEST_1(!sip_is_callerpref(NULL));
  2357. TEST_1(sip_is_callerpref("+"));
  2358. TEST_1(sip_is_callerpref("+foo"));
  2359. /* Booleans (treated as literals) */
  2360. s = "TRUE";
  2361. negate = 2; memset(sp, 0, sizeof sp);
  2362. TEST_1(sip_prefs_parse(sp, &s, &negate));
  2363. TEST(sp->sp_type, sp_literal);
  2364. TEST_S(sp->sp_literal.spl_value, "TRUE"); TEST_1(!negate);
  2365. TEST_1(sip_prefs_match(sp, sp));
  2366. TEST_1(!sip_prefs_parse(sp, &s, &negate));
  2367. TEST(sp->sp_type, sp_init);
  2368. s = "FALSE";
  2369. negate = 2; memset(sp, 0, sizeof sp);
  2370. TEST_1(sip_prefs_parse(sp, &s, &negate));
  2371. TEST(sp->sp_type, sp_literal);
  2372. TEST_S(sp->sp_literal.spl_value, "FALSE"); TEST_1(!negate);
  2373. TEST_1(sip_prefs_match(sp, sp));
  2374. TEST_1(!sip_prefs_parse(sp, &s, &negate));
  2375. TEST(sp->sp_type, sp_init);
  2376. s = "\"!TRUE,!FALSE\""; negate = 0;
  2377. TEST_1(sip_prefs_parse(sp, &s, &negate));
  2378. TEST(sp->sp_type, sp_literal); TEST_1(negate);
  2379. /* Literal */
  2380. s = "\" !oukki , doukki \""; negate = 0; memset(sp, 0, sizeof sp);
  2381. TEST_1(sip_prefs_parse(sp, &s, &negate));
  2382. TEST(sp->sp_type, sp_literal);
  2383. TEST_SIZE(sp->sp_literal.spl_length, 5);
  2384. TEST_M(sp->sp_literal.spl_value, "oukki", 5); TEST_1(negate);
  2385. TEST_1(sip_prefs_parse(sp, &s, &negate));
  2386. TEST(sp->sp_type, sp_literal);
  2387. TEST_SIZE(sp->sp_literal.spl_length, 6);
  2388. TEST_M(sp->sp_literal.spl_value, "doukki", 6); TEST_1(!negate);
  2389. TEST_1(!sip_prefs_parse(sp, &s, &negate));
  2390. TEST(sp->sp_type, sp_init);
  2391. /* Strings */
  2392. s = "\" !<oukki> , <douK\\\"ki >\"";
  2393. negate = 0; memset(sp, 0, sizeof sp);
  2394. TEST_1(sip_prefs_parse(sp, &s, &negate));
  2395. TEST(sp->sp_type, sp_string);
  2396. TEST_SIZE(sp->sp_string.sps_length, 5);
  2397. TEST_M(sp->sp_string.sps_value, "oukki", 5); TEST_1(negate);
  2398. TEST_1(sip_prefs_parse(sp, &s, &negate));
  2399. TEST(sp->sp_type, sp_string);
  2400. TEST_SIZE(sp->sp_string.sps_length, 10);
  2401. TEST_M(sp->sp_string.sps_value, "douK\\\"ki ", 10); TEST_1(!negate);
  2402. TEST_1(!sip_prefs_parse(sp, &s, &negate));
  2403. TEST(sp->sp_type, sp_init);
  2404. /* Numeric */
  2405. s = "\" !#=6, #<=3, #>=6, !#<=6, #1:6.5\"";
  2406. negate = 0; memset(sp, 0, sizeof sp);
  2407. TEST_1(sip_prefs_parse(sp, &s, &negate));
  2408. TEST(sp->sp_type, sp_range);
  2409. TEST_D(sp->sp_range.spr_lower, 6.0);
  2410. TEST_D(sp->sp_range.spr_upper, 6.0);
  2411. TEST_1(sip_prefs_match(sp, sp));
  2412. TEST_1(negate);
  2413. *a = *sp;
  2414. TEST_1(sip_prefs_parse(sp, &s, &negate));
  2415. TEST(sp->sp_type, sp_range);
  2416. TEST_D(sp->sp_range.spr_lower, -DBL_MAX);
  2417. TEST_D(sp->sp_range.spr_upper, 3.0);
  2418. TEST_1(sip_prefs_match(sp, sp));
  2419. TEST_1(!negate);
  2420. TEST_1(!sip_prefs_match(a, sp));
  2421. TEST_1(sip_prefs_parse(sp, &s, &negate));
  2422. TEST(sp->sp_type, sp_range);
  2423. TEST_D(sp->sp_range.spr_lower, 6.0);
  2424. TEST_D(sp->sp_range.spr_upper, DBL_MAX);
  2425. TEST_1(sip_prefs_match(sp, sp));
  2426. TEST_1(!negate);
  2427. TEST_1(sip_prefs_match(a, sp));
  2428. TEST_1(sip_prefs_parse(sp, &s, &negate));
  2429. TEST(sp->sp_type, sp_range);
  2430. TEST_D(sp->sp_range.spr_lower, -DBL_MAX);
  2431. TEST_D(sp->sp_range.spr_upper, 6.0);
  2432. TEST_1(sip_prefs_match(sp, sp));
  2433. TEST_1(negate);
  2434. TEST_1(sip_prefs_match(a, sp));
  2435. TEST_1(sip_prefs_parse(sp, &s, &negate));
  2436. TEST(sp->sp_type, sp_range);
  2437. TEST_D(sp->sp_range.spr_lower, 1.0);
  2438. TEST_D(sp->sp_range.spr_upper, 6.5);
  2439. TEST_1(sip_prefs_match(sp, sp));
  2440. TEST_1(!negate);
  2441. TEST_1(sip_prefs_match(a, sp));
  2442. TEST_1(!sip_prefs_parse(sp, &s, &negate));
  2443. TEST(sp->sp_type, sp_init);
  2444. /* Numeric */
  2445. s = "\" !#="
  2446. "1111111111111111111111111111111111111111"
  2447. "1111111111111111111111111111111111111111"
  2448. "1111111111111111111111111111111111111111"
  2449. "1111111111111111111111111111111111111111"
  2450. "1111111111111111111111111111111111111111"
  2451. "1111111111111111111111111111111111111111"
  2452. "1111111111111111111111111111111111111111"
  2453. "1111111111111111111111111111111111111111."
  2454. "1111111111111111111111111111111111111111"
  2455. "1111111111111111111111111111111111111111"
  2456. "1111111111111111111111111111111111111111"
  2457. "1111111111111111111111111111111111111111"
  2458. "1111111111111111111111111111111111111111"
  2459. "1111111111111111111111111111111111111111"
  2460. "1111111111111111111111111111111111111111"
  2461. "1111111111111111111111111111111111111111,"
  2462. " #<=-16"
  2463. "\"";
  2464. negate = 0; memset(sp, 0, sizeof sp);
  2465. TEST_1(sip_prefs_parse(sp, &s, &negate));
  2466. TEST(sp->sp_type, sp_range);
  2467. TEST_D(sp->sp_range.spr_lower, DBL_MAX);
  2468. TEST_D(sp->sp_range.spr_upper, DBL_MAX);
  2469. TEST_1(sip_prefs_match(sp, sp));
  2470. TEST_1(negate);
  2471. TEST_1(sip_prefs_parse(sp, &s, &negate));
  2472. TEST(sp->sp_type, sp_range);
  2473. TEST_D(sp->sp_range.spr_lower, -DBL_MAX);
  2474. TEST_D(sp->sp_range.spr_upper, -16.0);
  2475. TEST_1(sip_prefs_match(sp, sp));
  2476. TEST_1(!negate);
  2477. error = 12;
  2478. TEST_1(sip_prefs_matching("\"INVITE,MESSAGE,SUBSCRIBE\"",
  2479. "\"INVITE\"", &error));
  2480. TEST_1(!sip_prefs_matching("\"INVITE,MESSAGE,SUBSCRIBE\"",
  2481. "\"BYE\"", &error));
  2482. TEST(error, 12);
  2483. TEST_1(sip_prefs_matching("\"INVITE,MESSAGE,SUBSCRIBE\"",
  2484. "\"invite\"", &error));
  2485. TEST_1(sip_prefs_matching("\"!INVITE,MESSAGE,SUBSCRIBE\"",
  2486. "\"foo\"", &error));
  2487. TEST_1(sip_prefs_matching("TRUE", "", &error));
  2488. TEST_1(sip_prefs_matching("", "", &error));
  2489. TEST_1(!sip_prefs_matching("FALSE", "", &error));
  2490. TEST(error, 12);
  2491. TEST_1(sip_prefs_matching("FALSE", "FALSE", &error));
  2492. /* Lax when receiving... */
  2493. TEST_1(sip_prefs_matching("\"FALSE\"", "FALSE", &error)); /* XXX */
  2494. TEST_1(sip_prefs_matching("\"TRUE\"", "TRUE", &error)); /* XXX */
  2495. TEST_1(!sip_prefs_matching("\"!INVITE\"", "\"INVITE\"", &error));
  2496. TEST_1(!sip_prefs_matching("\"!INVITE\"", "\"invite\"", &error));
  2497. TEST_1(sip_prefs_matching("\"!<INVITE>\"", "\"<invite>\"", &error));
  2498. TEST_1(!sip_prefs_matching("\"INVITE\"", "\"!INVITE\"", &error));
  2499. TEST_1(sip_prefs_matching("\"!INVITE\"", "\"INVITE,MESSAGE\"", &error));
  2500. TEST_1(sip_prefs_matching("\"!INVITE,!MESSAGE\"",
  2501. "\"INVITE,MESSAGE\"", &error));
  2502. TEST_1(sip_prefs_matching("\"!MESSAGE\"", "\"INVITE,MESSAGE\"", &error));
  2503. TEST_1(!sip_prefs_matching("\"<foo>,<bar>\"",
  2504. "\"<FOO>,<BAR>\"", &error));
  2505. TEST_1(!sip_prefs_matching("\"<FOO>,<BAR>\"", "\"foo,bar\"", &error));
  2506. TEST_1(sip_prefs_matching("\"#=1\"", "\"#<=2\"", &error));
  2507. TEST_1(sip_prefs_matching("\"#1:2\"", "\"#<=2\"", &error));
  2508. TEST_1(!sip_prefs_matching("\"#1:2\"", "\"!#>=1,!#<=2\"", &error));
  2509. TEST_1(!sip_prefs_matching("\"#=0,#=1\"", "\"<FOO>,<BAR>\"", &error));
  2510. TEST(error, 12);
  2511. error = 12;
  2512. TEST_1(!sip_prefs_matching("\"<foo>,#=1\"", "\"<FOO>,<BAR>\"", &error));
  2513. TEST(error, -1);
  2514. error = 12;
  2515. TEST_1(!sip_prefs_matching("\"<FOO>,<BAR>\"", "\"<foo>,#=1\"", &error));
  2516. TEST(error, -1);
  2517. error = 12;
  2518. TEST_1(!sip_prefs_matching("\"<foo>,bar\"", "\"<FOO>,<BAR>\"", &error));
  2519. TEST(error, -1);
  2520. error = 12;
  2521. TEST_1(!sip_prefs_matching("\"<FOO>,<BAR>\"", "\"<foo>,#12:12\"", &error));
  2522. TEST(error, -1);
  2523. {
  2524. char const *params[] = {
  2525. "methods=\"INVITE,MESSAGE,SUBSCRIBE\"",
  2526. "events=\"presence,presence.winfo\"",
  2527. "description=\"<PC>\"",
  2528. "language=\"!en,de\"",
  2529. "schemes=\"sip\"",
  2530. "+res-x=\"#=640\"",
  2531. "+res-y=\"#=480\"",
  2532. NULL
  2533. };
  2534. TEST_1(sip_is_callerpref(params[0]));
  2535. TEST_1(sip_is_callerpref(params[1]));
  2536. TEST_1(sip_is_callerpref(params[2]));
  2537. TEST_1(sip_is_callerpref(params[3]));
  2538. TEST_1(sip_is_callerpref(params[4]));
  2539. TEST_1(sip_is_callerpref(params[5]));
  2540. TEST_1(sip_is_callerpref(params[6]));
  2541. TEST_1(!sip_is_callerpref(params[7])); /* NULL */
  2542. TEST_1(!sip_is_callerpref("method=\"foo\""));
  2543. TEST_1(!sip_is_callerpref("+methods=\"foo\""));
  2544. }
  2545. TEST_1(m = sip_contact_make(home,
  2546. "<sip:1@domain>;video;audio;type=\"<video/H263>\","
  2547. "<sip:2@domain>;video=FALSE;audio;text,"
  2548. "<sip:3@domain>;text;audio=FALSE"));
  2549. m0 = m, m1 = m->m_next, m2 = m->m_next->m_next;
  2550. TEST_1(ac = sip_accept_contact_make(home, "*;type=\"<video/H263>\";video"));
  2551. TEST_S(ac->cp_params[0], "type=\"<video/H263>\"");
  2552. TEST_S(ac->cp_params[1], "video");
  2553. TEST_P(ac->cp_params[2], NULL);
  2554. TEST_1(sip_contact_accept(m0, ac, &S, &N, &error)); TEST(S, 2); TEST(N, 2);
  2555. TEST_1(!sip_contact_accept(m1, ac, &S, &N, &error));
  2556. TEST_1(sip_contact_accept(m2, ac, &S, &N, &error)); TEST(S, 0); TEST(N, 2);
  2557. TEST_1(ac = sip_accept_contact_make(home, "sip:127.0.0.1:5060;video;audio"));
  2558. TEST_S(ac->cp_params[0], "video");
  2559. TEST_S(ac->cp_params[1], "audio");
  2560. TEST_P(ac->cp_params[2], NULL);
  2561. TEST_1(sip_contact_accept(m0, ac, &S, &N, &error)); TEST(S, 2); TEST(N, 2);
  2562. TEST_1(!sip_contact_accept(m1, ac, &S, &N, &error));
  2563. TEST_1(!sip_contact_accept(m2, ac, &S, &N, &error));
  2564. TEST_1(ac = sip_accept_contact_make(home,
  2565. "Joe the Luser "
  2566. "<sip:127.0.0.1:5060>;video;audio"));
  2567. TEST_S(ac->cp_params[0], "video");
  2568. TEST_S(ac->cp_params[1], "audio");
  2569. TEST_P(ac->cp_params[2], NULL);
  2570. TEST_1(ac = sip_accept_contact_make(home,
  2571. "video;audio;explicit"));
  2572. TEST_S(ac->cp_params[0], "video");
  2573. TEST_S(ac->cp_params[1], "audio");
  2574. TEST_S(ac->cp_params[2], "explicit");
  2575. TEST_P(ac->cp_params[3], NULL);
  2576. TEST_1(ac = sip_accept_contact_make(home,
  2577. "video = foo ;audio;explicit"));
  2578. TEST_S(ac->cp_params[0], "video=foo");
  2579. TEST_S(ac->cp_params[1], "audio");
  2580. TEST_S(ac->cp_params[2], "explicit");
  2581. TEST_P(ac->cp_params[3], NULL);
  2582. TEST_1(ac = sip_accept_contact_make(home,
  2583. "video = \"bar\" ;audio;explicit"));
  2584. TEST_S(ac->cp_params[0], "video=\"bar\"");
  2585. TEST_S(ac->cp_params[1], "audio");
  2586. TEST_S(ac->cp_params[2], "explicit");
  2587. TEST_P(ac->cp_params[3], NULL);
  2588. TEST_1(cp = sip_accept_contact_make(home,
  2589. "*;audio;video;require;explicit;q=1.0,"
  2590. "*;audio;require;q=0.8"
  2591. ));
  2592. TEST_1(ac = sip_accept_contact_dup(home, cp));
  2593. TEST_S(ac->cp_params[0], "audio");
  2594. TEST_S(ac->cp_params[1], "video");
  2595. TEST_S(ac->cp_params[2], "require");
  2596. TEST_S(ac->cp_params[3], "explicit");
  2597. TEST_S(ac->cp_params[4], "q=1.0");
  2598. TEST_P(ac->cp_params[5], NULL);
  2599. /* TEST_S(ac->cp_q, "1.0"); */
  2600. TEST(ac->cp_require, 1);
  2601. TEST(ac->cp_explicit, 1);
  2602. TEST_1(sip_contact_accept(m0, ac, &S, &N, &error)); TEST(S, 2); TEST(N, 2);
  2603. /* Explicit has short-circuit evaluation */
  2604. TEST_1(!sip_contact_accept(m1, ac, &S, &N, &error));
  2605. TEST_1(!sip_contact_accept(m2, ac, &S, &N, &error));
  2606. TEST_1(ac = ac->cp_next);
  2607. TEST_S(ac->cp_params[0], "audio");
  2608. TEST_S(ac->cp_params[1], "require");
  2609. TEST_S(ac->cp_params[2], "q=0.8");
  2610. TEST_P(ac->cp_params[3], NULL);
  2611. TEST(ac->cp_explicit, 0);
  2612. TEST_1(sip_contact_accept(m0, ac, &S, &N, &error)); TEST(S, 1); TEST(N, 1);
  2613. TEST_1(sip_contact_accept(m1, ac, &S, &N, &error)); TEST(S, 1); TEST(N, 1);
  2614. TEST_1(!sip_contact_accept(m2, ac, &S, &N, &error));
  2615. TEST_P(ac->cp_next, NULL);
  2616. TEST_1(rejc = sip_reject_contact_make(home,
  2617. "*;type=\"<video/H263>\";video=TRUE"));
  2618. TEST_S(rejc->cp_params[0], "type=\"<video/H263>\"");
  2619. TEST_S(rejc->cp_params[1], "video=TRUE");
  2620. TEST_1(sip_contact_reject(m0, rejc));
  2621. TEST_1(!sip_contact_reject(m1, rejc));
  2622. TEST_1(!sip_contact_reject(m2, rejc));
  2623. TEST_1(!sip_contact_immune(m0));
  2624. m0 = sip_contact_immunize(home, m0); TEST_1(m0);
  2625. TEST_1(sip_contact_immune(m0));
  2626. TEST_1(!sip_contact_immune(m1));
  2627. m1 = sip_contact_immunize(home, m1); TEST_1(m1);
  2628. TEST_1(sip_contact_immune(m1));
  2629. TEST_1(!sip_contact_immune(m2));
  2630. m2 = sip_contact_immunize(home, m2); TEST_1(m2);
  2631. TEST_1(sip_contact_immune(m2));
  2632. m = sip_contact_make(home, "<sip:test.domain>;+test;+audio");
  2633. TEST_1(!sip_contact_immune(m));
  2634. m1 = sip_contact_immunize(home, m); TEST_1(m1);
  2635. TEST_1(sip_contact_immune(m1));
  2636. TEST_S(m->m_params[0], "+test");
  2637. TEST_S(m->m_params[1], "+audio");
  2638. TEST_P(m->m_params[2], NULL);
  2639. TEST_S(m1->m_params[0], "+audio");
  2640. TEST_P(m1->m_params[1], NULL);
  2641. TEST_1(ac = sip_accept_contact_make(home, "*;q=0.9;require;explicit"));
  2642. /* TEST_S(ac->cp_q, "0.9"); */
  2643. TEST_1(ac->cp_require);
  2644. TEST_1(ac->cp_explicit);
  2645. TEST(msg_header_remove_param(ac->cp_common, "Q"), 1);
  2646. /* TEST(ac->cp_q, NULL); */
  2647. TEST(msg_header_remove_param(ac->cp_common, "require="), 1);
  2648. TEST(ac->cp_require, 0);
  2649. TEST(msg_header_remove_param(ac->cp_common, "require="), 0);
  2650. TEST(ac->cp_require, 0);
  2651. TEST(msg_header_remove_param(ac->cp_common, "explicit=true"), 1);
  2652. TEST(ac->cp_explicit, 0);
  2653. TEST(msg_header_replace_param(home, ac->cp_common, "explicit=true"), 0);
  2654. TEST(ac->cp_explicit, 1);
  2655. su_home_zap(home);
  2656. END();
  2657. }
  2658. static int test_callerpref_scoring(void)
  2659. {
  2660. sip_accept_contact_t *ac;
  2661. sip_reject_contact_t *rejc;
  2662. su_home_t *home;
  2663. int S;
  2664. sip_contact_t *m, *m1, *m2, *m3, *m4, *m5;
  2665. char const contact[] =
  2666. "sip:u1@h.example.com;audio;video;methods=\"INVITE,BYE\";q=0.2,"
  2667. "sip:u2@h.example.com;audio=\"FALSE\";"
  2668. "methods=\"INVITE\";actor=\"msg-taker\";q=0.2,"
  2669. "sip:u3@h.example.com;audio;actor=\"msg-taker\";"
  2670. "methods=\"INVITE\";video;q=0.3,"
  2671. "sip:u4@h.example.com;audio;methods=\"INVITE,OPTIONS\";q=0.2,"
  2672. "sip:u5@h.example.com;q=0.5";
  2673. char const reject[] =
  2674. "*;actor=\"msg-taker\";video";
  2675. char const accept[] =
  2676. "*;audio;require,"
  2677. "*;video;explicit,"
  2678. "*;methods=\"BYE\";class=\"business\";q=1.0";
  2679. BEGIN();
  2680. TEST_1(home = su_home_new(sizeof *home));
  2681. TEST_1(m = sip_contact_make(home, contact));
  2682. m1 = m, m2 = m->m_next, m3 = m->m_next->m_next, m4 = m->m_next->m_next->m_next,
  2683. m5 = m->m_next->m_next->m_next->m_next;
  2684. TEST_1(rejc = sip_reject_contact_make(home, reject));
  2685. TEST_1(ac = sip_accept_contact_make(home, accept));
  2686. S = sip_contact_score(m1, ac, rejc); TEST(S, 1000 * 5 / 6 + 1);
  2687. S = sip_contact_score(m2, ac, rejc); TEST(S, 0);
  2688. S = sip_contact_score(m3, ac, rejc); TEST_1(S < 0);
  2689. S = sip_contact_score(m4, ac, rejc); TEST(S, 1000 / 2);
  2690. S = sip_contact_score(m5, ac, rejc); TEST(S, 1000);
  2691. su_home_zap(home);
  2692. END();
  2693. }
  2694. static int test_reason(void)
  2695. {
  2696. sip_reason_t *re;
  2697. su_home_t *home;
  2698. msg_t *msg;
  2699. sip_t *sip;
  2700. BEGIN();
  2701. TEST_1(home = su_home_create());
  2702. TEST_1((re = sip_reason_make(home, "SIP;cause=200;text=\"Ok\"")));
  2703. TEST_S(re->re_protocol, "SIP");
  2704. TEST_S(re->re_cause, "200");
  2705. TEST_S(re->re_text, "\"Ok\"");
  2706. msg = read_message(MSG_DO_EXTRACT_COPY,
  2707. "BYE sip:foo@bar SIP/2.0\r\n"
  2708. "To: <sip:foo@bar>;tag=deadbeef\r\n"
  2709. "From: <sip:bar@foo>;\r\n"
  2710. "Call-ID: 0ha0isndaksdj@10.1.2.3\r\n"
  2711. "CSeq: 8 SUBSCRIBE\r\n"
  2712. "Via: SIP/2.0/UDP 135.180.130.133\r\n"
  2713. "Reason: SIP ;cause=200 ;text=\"Call completed elsewhere\"\r\n"
  2714. "Reason: Q.850 ;cause=16 ;text=\"Terminated\"\r\n ,,"
  2715. "SIP ; cause = 600 ;text\t\r\n = \"Busy Everywhere\" \t \r\n"
  2716. "Reason: SIP ;cause=580 ;text=\"Precondition Failure\"\r\n"
  2717. "Content-Length: 0\r\n"
  2718. "\r\n");
  2719. sip = sip_object(msg);
  2720. TEST_1(msg);
  2721. TEST_1(sip);
  2722. TEST_1(re = sip->sip_reason);
  2723. TEST_S(re->re_protocol, "SIP");
  2724. TEST_S(re->re_cause, "200");
  2725. TEST_S(re->re_text, "\"Call completed elsewhere\"");
  2726. TEST_1(re = re->re_next);
  2727. TEST_S(re->re_protocol, "Q.850");
  2728. TEST_S(re->re_cause, "16");
  2729. TEST_S(re->re_text, "\"Terminated\"");
  2730. TEST_1(re = re->re_next);
  2731. TEST_S(re->re_protocol, "SIP");
  2732. TEST_S(re->re_cause, "600");
  2733. TEST_S(re->re_text, "\"Busy Everywhere\"");
  2734. TEST_1(re = re->re_next);
  2735. TEST_S(re->re_protocol, "SIP");
  2736. TEST_S(re->re_cause, "580");
  2737. TEST_S(re->re_text, "\"Precondition Failure\"");
  2738. TEST_1(!(re = re->re_next));
  2739. TEST_1(re = sip->sip_reason);
  2740. TEST(msg_header_replace_param(home, re->re_common, "cause=202"), 1);
  2741. TEST_S(re->re_cause, "202");
  2742. TEST(msg_header_replace_param(home, re->re_common, "text=\"foo\""), 1);
  2743. TEST_S(re->re_text, "\"foo\"");
  2744. TEST(msg_header_remove_param(re->re_common, "cause=444"), 1);
  2745. TEST_P(re->re_cause, NULL);
  2746. TEST(msg_header_remove_param(re->re_common, "text=\"bar\""), 1);
  2747. TEST_P(re->re_text, NULL);
  2748. TEST(msg_header_remove_param(re->re_common, "cause=444"), 0);
  2749. TEST(msg_header_remove_param(re->re_common, "text=\"bar\""), 0);
  2750. /* Not a token */
  2751. TEST_1(!sip_reason_make(home, "\"nSIP\";cause=200;text=\"Ok\""));
  2752. /* Empty list */
  2753. TEST_1(!sip_reason_make(home, ","));
  2754. /* no protocol token */
  2755. TEST_1(!sip_reason_make(home, "cause=16;text=\"call cleared\""));
  2756. /* Empty parameter */
  2757. TEST_1(!sip_reason_make(home, "SIP;cause=200;;text=\"Ok\""));
  2758. /* no semicolon after token */
  2759. TEST_1(!sip_reason_make(home, "SIP cause=200;text=\"Ok\""));
  2760. /* extra semicolon after parameters */
  2761. TEST_1(!sip_reason_make(home, "SIP ;cause=200;text=\"Ok\";"));
  2762. su_home_unref(home);
  2763. msg_destroy(msg), msg = NULL;
  2764. END();
  2765. }
  2766. static int test_warning(void)
  2767. {
  2768. sip_warning_t *w;
  2769. su_home_t *home;
  2770. BEGIN();
  2771. TEST_1(home = su_home_create());
  2772. TEST_1((w = sip_warning_make(home,
  2773. "399 host:5060 \"Ok\", "
  2774. "399 [::1]:39999 \"foo\\\" bar\"")));
  2775. TEST(w->w_code, 399);
  2776. TEST_S(w->w_host, "host");
  2777. TEST_S(w->w_port, "5060");
  2778. TEST_S(w->w_text, "Ok");
  2779. TEST_1(w = w->w_next);
  2780. TEST(w->w_code, 399);
  2781. TEST_S(w->w_host, "[::1]");
  2782. TEST_S(w->w_port, "39999");
  2783. TEST_S(w->w_text, "foo\" bar");
  2784. TEST_1(w->w_next == NULL);
  2785. TEST_S(sip_header_as_string(home, (sip_header_t *)w),
  2786. "399 [::1]:39999 \"foo\\\" bar\"");
  2787. su_home_unref(home);
  2788. END();
  2789. }
  2790. static int test_sec_ext(void)
  2791. {
  2792. su_home_t *home;
  2793. sip_security_client_t *sac;
  2794. sip_security_server_t *sas;
  2795. sip_security_verify_t *sav;
  2796. sip_privacy_t *priv;
  2797. msg_t *msg;
  2798. sip_t *sip;
  2799. BEGIN();
  2800. msg = read_message(MSG_DO_EXTRACT_COPY,
  2801. "BYE sip:foo@bar SIP/2.0\r\n"
  2802. "To: <sip:foo@bar>;tag=deadbeef\r\n"
  2803. "From: <sip:bar@foo>;\r\n"
  2804. "Call-ID: 0ha0isndaksdj@10.1.2.3\r\n"
  2805. "CSeq: 8 SUBSCRIBE\r\n"
  2806. "Via: SIP/2.0/UDP 135.180.130.133\r\n"
  2807. "Content-Length: 0\r\n"
  2808. "\r\n");
  2809. sip = sip_object(msg);
  2810. TEST_1(home = msg_home(msg));
  2811. TEST_1(sac = sip_security_client_make(home, "digest;q=0.5,ipsec-3gpp"));
  2812. TEST_S(sac->sa_mec, "digest");
  2813. TEST_S(sac->sa_q, "0.5");
  2814. TEST_1(sac = sac->sa_next);
  2815. TEST_S(sac->sa_mec, "ipsec-3gpp");
  2816. TEST_1((sas = sip_security_server_make(home, "digest;q=0.5")));
  2817. TEST_S(sas->sa_mec, "digest");
  2818. TEST_S(sas->sa_q, "0.5");
  2819. TEST_1((sav = sip_security_verify_make(home, "digest;q=0.5")));
  2820. TEST_S(sav->sa_mec, "digest");
  2821. TEST_S(sav->sa_q, "0.5");
  2822. TEST_1((sav = sip_security_verify_make
  2823. (home, "digest;q=0.1;d-alg=SHA1;d-qop=auth;"
  2824. "d-ver=\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"")));
  2825. TEST_S(sav->sa_mec, "digest");
  2826. TEST_S(sav->sa_q, "0.1");
  2827. TEST_S(sav->sa_d_alg, "SHA1");
  2828. TEST_S(sav->sa_d_qop, "auth");
  2829. TEST_S(sav->sa_d_ver, "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"");
  2830. /* Test for accepting liberally.. */
  2831. TEST_1(priv = sip_privacy_make(home, "header,media"));
  2832. TEST_1(priv = sip_privacy_make(home, ";header;media"));
  2833. TEST_1(!(priv = sip_privacy_make(home, "none explicit")));
  2834. TEST_1((priv = sip_privacy_make(home, "header;media")));
  2835. TEST_1(priv->priv_values);
  2836. TEST_S(priv->priv_values[0], "header");
  2837. TEST_S(priv->priv_values[1], "media");
  2838. msg_destroy(msg);
  2839. END();
  2840. }
  2841. static int test_utils(void)
  2842. {
  2843. sip_from_t *f;
  2844. su_home_t *home;
  2845. sip_security_server_t *secs;
  2846. sip_security_verify_t *secv;
  2847. msg_param_t d_ver = NULL;
  2848. BEGIN();
  2849. TEST_1(home = su_home_new(sizeof *home));
  2850. TEST_1(f = sip_from_make(home, "<sip:u:p@h.com:5555"
  2851. ";ttl=1;user=IP;maddr=::1;lr=TRUE;transport=TCP"
  2852. ";test=1?accept-contact=*;audio;video;explicit>"));
  2853. TEST_1(sip_aor_strip(f->a_url) == 0);
  2854. TEST_1(f->a_url->url_port == NULL);
  2855. TEST_S(f->a_url->url_params, "user=IP;test=1");
  2856. TEST_1(f->a_url->url_headers == NULL);
  2857. TEST_1(f = sip_from_make(home, "<sip:u:p@h.com:5555;ttl=1>"));
  2858. TEST_1(sip_aor_strip(f->a_url) == 0);
  2859. TEST_1(f->a_url->url_params == NULL);
  2860. TEST_1(f = sip_from_make(home, "<sip:u:p@h.com:5555;test=1;ttl=1>"));
  2861. TEST_1(sip_aor_strip(f->a_url) == 0);
  2862. TEST_S(f->a_url->url_params, "test=1");
  2863. TEST_1(f = sip_from_make(home, "<sip:u:p@h.com:5555;;test=1;>"));
  2864. TEST_1(sip_aor_strip(f->a_url) == 0);
  2865. TEST_S(f->a_url->url_params, "test=1");
  2866. TEST_1(secs = sip_security_server_make(home, "Digest"));
  2867. TEST_1(secv =
  2868. sip_security_verify_make(home,
  2869. "Digest;"
  2870. "d-ver=\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\""));
  2871. TEST_1(sip_security_verify_compare(secs, secv, &d_ver) == 0);
  2872. TEST_S(d_ver, "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"");
  2873. TEST_1(secs =
  2874. sip_security_server_make(home,
  2875. "TLS;q=1,"
  2876. "Digest;q=0.1;"
  2877. "d-alg=MD5;d-qop=\"auth,auth-int\","
  2878. "Digest;d-alg=AKA-MD5;q=0.9"
  2879. ));
  2880. TEST_1(secv =
  2881. sip_security_verify_make(home,
  2882. "TLS;q=1,"
  2883. "Digest ; q = 0.1;"
  2884. "d-ver=\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"
  2885. "d-alg = MD5 ; d-qop = \"auth,auth-int\","
  2886. "Digest ; d-alg=AKA-MD5;q=0.9"));
  2887. TEST_1(sip_security_verify_compare(secs, secv, &d_ver) == 0);
  2888. TEST_S(d_ver, "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"");
  2889. TEST_1(sip_security_verify_compare(secs->sa_next, secv, &d_ver) != 0);
  2890. TEST_1(sip_security_verify_compare(secs, secv->sa_next, &d_ver) != 0);
  2891. d_ver = "kuik";
  2892. TEST_1(sip_security_verify_compare(secs->sa_next, secv->sa_next, &d_ver)
  2893. == 0);
  2894. TEST_S(d_ver, "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"");
  2895. d_ver = "kuik";
  2896. TEST_1(sip_security_verify_compare(secs->sa_next->sa_next,
  2897. secv->sa_next->sa_next, &d_ver)
  2898. == 0);
  2899. TEST_1(d_ver == NULL);
  2900. END();
  2901. }
  2902. void usage(int exitcode)
  2903. {
  2904. fprintf(stderr,
  2905. "usage: %s [-v] [-a]\n",
  2906. name);
  2907. exit(exitcode);
  2908. }
  2909. char *lastpart(char *path)
  2910. {
  2911. if (strchr(path, '/'))
  2912. return strrchr(path, '/') + 1;
  2913. else
  2914. return path;
  2915. }
  2916. int main(int argc, char *argv[])
  2917. {
  2918. int retval = 0;
  2919. int i;
  2920. name = lastpart(argv[0]); /* Set our name */
  2921. for (i = 1; argv[i]; i++) {
  2922. if (strcmp(argv[i], "-v") == 0)
  2923. tstflags |= tst_verbatim;
  2924. else if (strcmp(argv[i], "-a") == 0)
  2925. tstflags |= tst_abort;
  2926. else
  2927. usage(1);
  2928. }
  2929. #if HAVE_OPEN_C
  2930. tstflags |= tst_verbatim;
  2931. #endif
  2932. retval |= test_identity(); fflush(stdout);
  2933. if (test_mclass == NULL)
  2934. test_mclass = msg_mclass_clone(sip_default_mclass(), 0, 0);
  2935. retval |= parser_test(); fflush(stdout);
  2936. retval |= test_url_headers(); fflush(stdout);
  2937. retval |= test_manipulation(); fflush(stdout);
  2938. retval |= test_methods(); fflush(stdout);
  2939. retval |= test_basic(); fflush(stdout);
  2940. retval |= test_sip_msg_class(test_mclass); fflush(stdout);
  2941. retval |= test_encoding(); fflush(stdout);
  2942. retval |= test_events(); fflush(stdout);
  2943. retval |= test_reason(); fflush(stdout);
  2944. retval |= tag_test(); fflush(stdout);
  2945. retval |= parser_tag_test(); fflush(stdout);
  2946. retval |= response_phrase_test(); fflush(stdout);
  2947. retval |= sip_header_test(); fflush(stdout);
  2948. retval |= test_bad_packet(); fflush(stdout);
  2949. retval |= test_sip_list_header(); fflush(stdout);
  2950. retval |= test_prack(); fflush(stdout);
  2951. retval |= test_accept(); fflush(stdout);
  2952. retval |= test_content_disposition(); fflush(stdout);
  2953. retval |= test_features(); fflush(stdout);
  2954. retval |= test_retry_after(); fflush(stdout);
  2955. retval |= test_session_expires(); fflush(stdout);
  2956. retval |= test_min_se(); fflush(stdout);
  2957. retval |= test_refer(); fflush(stdout);
  2958. retval |= test_route(); fflush(stdout);
  2959. retval |= test_request_disposition(); fflush(stdout);
  2960. retval |= test_content_type(); fflush(stdout);
  2961. retval |= test_caller_prefs(); fflush(stdout);
  2962. retval |= test_callerpref_scoring(); fflush(stdout);
  2963. retval |= test_warning(); fflush(stdout);
  2964. retval |= test_www_authenticate(); fflush(stdout);
  2965. retval |= test_sec_ext(); fflush(stdout);
  2966. retval |= test_utils(); fflush(stdout);
  2967. #if HAVE_OPEN_C
  2968. sleep(5);
  2969. #endif
  2970. return retval;
  2971. }