public_server.c 150 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386
  1. /* Copyright (c) 2015-2020 the Civetweb developers
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. #ifdef _MSC_VER
  22. #ifndef _CRT_SECURE_NO_WARNINGS
  23. #define _CRT_SECURE_NO_WARNINGS
  24. #endif
  25. #endif
  26. #include <stdarg.h>
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <sys/stat.h>
  32. #include <sys/types.h>
  33. #include <time.h>
  34. #include "public_server.h"
  35. #include <civetweb.h>
  36. #if defined(_WIN32)
  37. #include <windows.h>
  38. #define test_sleep(x) (Sleep((x)*1000))
  39. #else
  40. #include <arpa/inet.h>
  41. #include <netdb.h>
  42. #include <netinet/in.h>
  43. #include <sys/socket.h>
  44. #include <unistd.h>
  45. #define test_sleep(x) (sleep(x))
  46. #endif
  47. /* Some environment configuration for the unit test */
  48. #define SLEEP_BEFORE_MG_START (1)
  49. #define SLEEP_AFTER_MG_START (3)
  50. #define SLEEP_BEFORE_MG_STOP (1)
  51. #define SLEEP_AFTER_MG_STOP (5)
  52. /* Try to communicate with an external http server. */
  53. static const char *
  54. get_external_server_ip(void)
  55. {
  56. #define no_of_testhosts (4)
  57. const char *testhost[no_of_testhosts] = {"github.com",
  58. "google.com",
  59. "sourceforge.net",
  60. "microsoft.com"};
  61. static char external_ip[64] = {0};
  62. int testhostidx;
  63. if (external_ip[0]) {
  64. /* Get IP of external server only once, then reuse it */
  65. return external_ip;
  66. }
  67. mark_point();
  68. for (testhostidx = 0; testhostidx < no_of_testhosts; testhostidx++) {
  69. struct hostent *hostentry;
  70. struct in_addr **in_addr_list;
  71. const char *ip;
  72. hostentry = gethostbyname(testhost[testhostidx]);
  73. if (hostentry != NULL) {
  74. in_addr_list = (struct in_addr **)hostentry->h_addr_list;
  75. if (in_addr_list[0] != NULL) {
  76. ip = inet_ntoa(*in_addr_list[0]);
  77. if (ip != NULL) {
  78. mark_point();
  79. strcpy(external_ip, ip);
  80. return external_ip;
  81. }
  82. }
  83. }
  84. }
  85. ck_abort_msg("Could not determine IP of any external server");
  86. return "0.0.0.0";
  87. }
  88. /* This unit test file uses the excellent Check unit testing library.
  89. * The API documentation is available here:
  90. * http://check.sourceforge.net/doc/check_html/index.html
  91. */
  92. #if defined(__MINGW32__) || defined(__GNUC__)
  93. /* Return codes of the tested functions are evaluated. Checking all other
  94. * functions, used only to prepare the test environment seems redundant.
  95. * If they fail, the test fails anyway. */
  96. #pragma GCC diagnostic ignored "-Wunused-result"
  97. #endif
  98. static const char *
  99. locate_path(const char *a_path)
  100. {
  101. static char r_path[256];
  102. #ifdef _WIN32
  103. #ifdef LOCAL_TEST
  104. sprintf(r_path, "%s\\", a_path);
  105. #else
  106. /* Appveyor */
  107. sprintf(r_path, "..\\..\\..\\%s\\", a_path);
  108. /* TODO: the different paths
  109. * used in the different test
  110. * system is an unsolved
  111. * problem. */
  112. #endif
  113. #else
  114. #ifdef LOCAL_TEST
  115. sprintf(r_path, "%s/", a_path);
  116. #else
  117. /* Travis */
  118. sprintf(r_path,
  119. "../../%s/",
  120. a_path); // TODO: fix path in CI test environment
  121. #endif
  122. #endif
  123. return r_path;
  124. }
  125. #define locate_resources() locate_path("resources")
  126. #define locate_test_exes() locate_path("output")
  127. static const char *
  128. locate_ssl_cert(void)
  129. {
  130. static char cert_path[256];
  131. const char *res = locate_resources();
  132. size_t l;
  133. ck_assert(res != NULL);
  134. l = strlen(res);
  135. ck_assert_uint_gt(l, 0);
  136. ck_assert_uint_lt(l, 100); /* assume there is enough space left in our
  137. typical 255 character string buffers */
  138. strcpy(cert_path, res);
  139. strcat(cert_path, "ssl_cert.pem");
  140. return cert_path;
  141. }
  142. static int
  143. wait_not_null(void *volatile *data)
  144. {
  145. int i;
  146. for (i = 0; i < 100; i++) {
  147. mark_point();
  148. test_sleep(1);
  149. if (*data != NULL) {
  150. mark_point();
  151. return 1;
  152. }
  153. }
  154. #if defined(__MINGW32__) || defined(__GNUC__)
  155. #pragma GCC diagnostic push
  156. #pragma GCC diagnostic ignored "-Wunreachable-code"
  157. #pragma GCC diagnostic ignored "-Wunreachable-code-return"
  158. #endif
  159. ck_abort_msg("wait_not_null failed (%i sec)", i);
  160. return 0;
  161. #if defined(__MINGW32__) || defined(__GNUC__)
  162. #pragma GCC diagnostic pop
  163. #endif
  164. }
  165. START_TEST(test_the_test_environment)
  166. {
  167. char wd[300];
  168. char buf[500];
  169. FILE *f;
  170. const char *ssl_cert = locate_ssl_cert();
  171. memset(wd, 0, sizeof(wd));
  172. memset(buf, 0, sizeof(buf));
  173. /* Get the current working directory */
  174. #ifdef _WIN32
  175. (void)GetCurrentDirectoryA(sizeof(wd), wd);
  176. wd[sizeof(wd) - 1] = 0;
  177. #else
  178. (void)getcwd(wd, sizeof(wd));
  179. wd[sizeof(wd) - 1] = 0;
  180. #endif
  181. /* Check the pem file */
  182. #ifdef _WIN32
  183. strcpy(buf, wd);
  184. strcat(buf, "\\");
  185. strcat(buf, ssl_cert);
  186. f = fopen(buf, "rb");
  187. #else
  188. strcpy(buf, wd);
  189. strcat(buf, "/");
  190. strcat(buf, ssl_cert);
  191. f = fopen(buf, "r");
  192. #endif
  193. if (f) {
  194. fclose(f);
  195. } else {
  196. fprintf(stderr, "Certificate %s not found\n", buf);
  197. exit(1); /* some path is not correct --> test will not work */
  198. }
  199. #ifdef _WIN32
  200. /* Try to copy the files required for AppVeyor */
  201. #if defined(_WIN64) || defined(__MINGW64__)
  202. (void)system("cmd /c copy C:\\OpenSSL-Win64\\libeay32.dll libeay32.dll");
  203. (void)system("cmd /c copy C:\\OpenSSL-Win64\\libssl32.dll libssl32.dll");
  204. (void)system("cmd /c copy C:\\OpenSSL-Win64\\ssleay32.dll ssleay32.dll");
  205. (void)system("cmd /c copy C:\\OpenSSL-Win64\\libeay32.dll libeay64.dll");
  206. (void)system("cmd /c copy C:\\OpenSSL-Win64\\libssl32.dll libssl64.dll");
  207. (void)system("cmd /c copy C:\\OpenSSL-Win64\\ssleay32.dll ssleay64.dll");
  208. #else
  209. (void)system("cmd /c copy C:\\OpenSSL-Win32\\libeay32.dll libeay32.dll");
  210. (void)system("cmd /c copy C:\\OpenSSL-Win32\\libssl32.dll libssl32.dll");
  211. (void)system("cmd /c copy C:\\OpenSSL-Win32\\ssleay32.dll ssleay32.dll");
  212. #endif
  213. #endif
  214. }
  215. END_TEST
  216. static void *threading_data = 0;
  217. static void *
  218. test_thread_func_t(void *param)
  219. {
  220. ck_assert_ptr_eq(param, &threading_data);
  221. ck_assert_ptr_eq(threading_data, NULL);
  222. threading_data = &threading_data;
  223. return NULL;
  224. }
  225. START_TEST(test_threading)
  226. {
  227. int ok;
  228. threading_data = NULL;
  229. mark_point();
  230. ok = mg_start_thread(test_thread_func_t, &threading_data);
  231. ck_assert_int_eq(ok, 0);
  232. wait_not_null(&threading_data);
  233. ck_assert_ptr_eq(threading_data, &threading_data);
  234. }
  235. END_TEST
  236. static const char *lastMessage = NULL;
  237. static int
  238. log_msg_func(const struct mg_connection *conn, const char *message)
  239. {
  240. struct mg_context *ctx;
  241. char *ud;
  242. ck_assert(conn != NULL);
  243. ctx = mg_get_context(conn);
  244. ck_assert(ctx != NULL);
  245. ud = (char *)mg_get_user_data(ctx);
  246. strncpy(ud, message, 255);
  247. ud[255] = 0;
  248. mark_point();
  249. printf("LOG_MSG_FUNC: %s\n", message);
  250. mark_point();
  251. lastMessage = message;
  252. return 1; /* Return 1 means "already handled" */
  253. }
  254. static int
  255. test_log_message(const struct mg_connection *conn, const char *message)
  256. {
  257. (void)conn;
  258. printf("LOG_MESSAGE: %s\n", message);
  259. mark_point();
  260. lastMessage = message;
  261. return 0; /* Return 0 means "not yet handled" */
  262. }
  263. static struct mg_context *
  264. test_mg_start(const struct mg_callbacks *callbacks,
  265. void *user_data,
  266. const char **configuration_options,
  267. unsigned line)
  268. {
  269. struct mg_context *ctx;
  270. struct mg_callbacks cb;
  271. struct mg_init_data init;
  272. struct mg_error_data error;
  273. char error_buffer[256];
  274. memset(&init, 0, sizeof(init));
  275. memset(&error, 0, sizeof(error));
  276. if (callbacks) {
  277. memcpy(&cb, callbacks, sizeof(cb));
  278. } else {
  279. memset(&cb, 0, sizeof(cb));
  280. }
  281. if (cb.log_message == NULL) {
  282. cb.log_message = test_log_message;
  283. }
  284. init.callbacks = &cb;
  285. init.user_data = user_data;
  286. init.configuration_options = configuration_options;
  287. error.text = error_buffer;
  288. error.text_buffer_size = sizeof(error_buffer);
  289. mark_point();
  290. test_sleep(SLEEP_BEFORE_MG_START);
  291. mark_point();
  292. ctx = mg_start2(&init, &error);
  293. mark_point();
  294. if (ctx) {
  295. /* Give the server some time to start in the test VM. */
  296. /* Don't need to do this if mg_start failed. */
  297. test_sleep(SLEEP_AFTER_MG_START);
  298. ck_assert_uint_eq(error.code, 0);
  299. ck_assert_str_eq(error.text, "");
  300. } else if (line > 0) {
  301. /* mg_start is not supposed to fail anywhere, except for
  302. * special tests (for them, line is 0). */
  303. ck_abort_msg(
  304. "mg_start failed in line %u\nerror %u: %s\nlast message %s\n",
  305. line,
  306. error.code,
  307. error.text,
  308. (lastMessage ? lastMessage : "<NULL>"));
  309. }
  310. mark_point();
  311. return ctx;
  312. }
  313. static void
  314. test_mg_stop(struct mg_context *ctx, unsigned line)
  315. {
  316. (void)line;
  317. #if defined(__MACH__) && defined(__APPLE__)
  318. /* For unknown reasons, there are sporadic hangs
  319. * for OSX if mark_point is called here */
  320. test_sleep(SLEEP_BEFORE_MG_STOP);
  321. mg_stop(ctx);
  322. test_sleep(SLEEP_AFTER_MG_STOP);
  323. #else
  324. mark_point();
  325. test_sleep(SLEEP_BEFORE_MG_STOP);
  326. mark_point();
  327. mg_stop(ctx);
  328. mark_point();
  329. test_sleep(SLEEP_AFTER_MG_STOP);
  330. mark_point();
  331. #endif
  332. }
  333. static void
  334. test_mg_start_stop_http_server_impl(int ipv6, int bound)
  335. {
  336. struct mg_context *ctx;
  337. const char *OPTIONS[16];
  338. int optcnt = 0;
  339. const char *localhost_name = ((ipv6) ? "[::1]" : "127.0.0.1");
  340. struct mg_callbacks callbacks;
  341. char errmsg[256];
  342. struct mg_connection *client_conn;
  343. char client_err[256];
  344. const struct mg_response_info *client_ri;
  345. int client_res, ret;
  346. struct mg_server_port portinfo[8];
  347. mark_point();
  348. #if !defined(NO_FILES)
  349. OPTIONS[optcnt++] = "document_root";
  350. OPTIONS[optcnt++] = ".";
  351. #endif
  352. OPTIONS[optcnt++] = "listening_ports";
  353. if (bound) {
  354. OPTIONS[optcnt++] = ((ipv6) ? "[::1]:+8080" : "127.0.0.1:8080");
  355. } else {
  356. OPTIONS[optcnt++] = ((ipv6) ? "+8080" : "8080");
  357. /* Test also tcp_nodelay - this option is not related
  358. * to interface binding, it's just tested here in this
  359. * combination to keep the number of tests smaller and
  360. * the test duration shorter.
  361. */
  362. OPTIONS[optcnt++] = "tcp_nodelay";
  363. OPTIONS[optcnt++] = "1";
  364. }
  365. OPTIONS[optcnt] = 0;
  366. memset(portinfo, 0, sizeof(portinfo));
  367. memset(&callbacks, 0, sizeof(callbacks));
  368. memset(errmsg, 0, sizeof(errmsg));
  369. callbacks.log_message = log_msg_func;
  370. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, __LINE__);
  371. ck_assert_str_eq(errmsg, "");
  372. ck_assert(ctx != NULL);
  373. ret = mg_get_server_ports(ctx, 0, portinfo);
  374. ck_assert_int_lt(ret, 0);
  375. ck_assert_int_eq(portinfo[0].protocol, 0);
  376. ck_assert_int_eq(portinfo[0].port, 0);
  377. ck_assert_int_eq(portinfo[0].is_ssl, 0);
  378. ck_assert_int_eq(portinfo[0].is_redirect, 0);
  379. ck_assert_int_eq(portinfo[1].protocol, 0);
  380. ck_assert_int_eq(portinfo[1].port, 0);
  381. ck_assert_int_eq(portinfo[1].is_ssl, 0);
  382. ck_assert_int_eq(portinfo[1].is_redirect, 0);
  383. ret = mg_get_server_ports(ctx, 4, portinfo);
  384. ck_assert_int_eq(ret, 1);
  385. if (ipv6) {
  386. ck_assert_int_eq(portinfo[0].protocol, 3);
  387. } else {
  388. ck_assert_int_eq(portinfo[0].protocol, 1);
  389. }
  390. ck_assert_int_eq(portinfo[0].port, 8080);
  391. ck_assert_int_eq(portinfo[0].is_ssl, 0);
  392. ck_assert_int_eq(portinfo[0].is_redirect, 0);
  393. ck_assert_int_eq(portinfo[1].protocol, 0);
  394. ck_assert_int_eq(portinfo[1].port, 0);
  395. ck_assert_int_eq(portinfo[1].is_ssl, 0);
  396. ck_assert_int_eq(portinfo[1].is_redirect, 0);
  397. test_sleep(1);
  398. /* HTTP 1.0 GET request */
  399. memset(client_err, 0, sizeof(client_err));
  400. client_conn = mg_connect_client(
  401. localhost_name, 8080, 0, client_err, sizeof(client_err));
  402. ck_assert_str_eq(client_err, "");
  403. ck_assert(client_conn != NULL);
  404. mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
  405. client_res =
  406. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  407. ck_assert_int_ge(client_res, 0);
  408. ck_assert_str_eq(client_err, "");
  409. client_ri = mg_get_response_info(client_conn);
  410. ck_assert(client_ri != NULL);
  411. #if defined(NO_FILES)
  412. ck_assert_int_eq(client_ri->status_code, 404);
  413. #else
  414. ck_assert_int_eq(client_ri->status_code, 200);
  415. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  416. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  417. ck_assert_int_gt(client_res, 0);
  418. ck_assert_int_le(client_res, sizeof(client_err));
  419. #endif
  420. mg_close_connection(client_conn);
  421. test_sleep(1);
  422. /* HTTP 1.1 GET request */
  423. memset(client_err, 0, sizeof(client_err));
  424. client_conn = mg_connect_client(
  425. localhost_name, 8080, 0, client_err, sizeof(client_err));
  426. ck_assert_str_eq(client_err, "");
  427. ck_assert(client_conn != NULL);
  428. mg_printf(client_conn, "GET / HTTP/1.1\r\n");
  429. mg_printf(client_conn, "Host: localhost:8080\r\n");
  430. mg_printf(client_conn, "Connection: close\r\n\r\n");
  431. client_res =
  432. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  433. ck_assert_int_ge(client_res, 0);
  434. ck_assert_str_eq(client_err, "");
  435. client_ri = mg_get_response_info(client_conn);
  436. ck_assert(client_ri != NULL);
  437. #if defined(NO_FILES)
  438. ck_assert_int_eq(client_ri->status_code, 404);
  439. #else
  440. ck_assert_int_eq(client_ri->status_code, 200);
  441. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  442. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  443. ck_assert_int_gt(client_res, 0);
  444. ck_assert_int_le(client_res, sizeof(client_err));
  445. #endif
  446. mg_close_connection(client_conn);
  447. test_sleep(1);
  448. /* HTTP 1.7 GET request - this HTTP version does not exist */
  449. memset(client_err, 0, sizeof(client_err));
  450. client_conn = mg_connect_client(
  451. localhost_name, 8080, 0, client_err, sizeof(client_err));
  452. ck_assert_str_eq(client_err, "");
  453. ck_assert(client_conn != NULL);
  454. mg_printf(client_conn, "GET / HTTP/1.7\r\n");
  455. mg_printf(client_conn, "Host: localhost:8080\r\n");
  456. mg_printf(client_conn, "Connection: close\r\n\r\n");
  457. client_res =
  458. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  459. ck_assert_int_ge(client_res, 0);
  460. ck_assert_str_eq(client_err, "");
  461. client_ri = mg_get_response_info(client_conn);
  462. ck_assert(client_ri != NULL);
  463. /* Response must be 505 HTTP Version not supported */
  464. ck_assert_int_eq(client_ri->status_code, 505);
  465. mg_close_connection(client_conn);
  466. test_sleep(1);
  467. /* HTTP request with multiline header.
  468. * Multiline header are obsolete with RFC 7230 section-3.2.4
  469. * and must return "400 Bad Request" */
  470. memset(client_err, 0, sizeof(client_err));
  471. client_conn = mg_connect_client(
  472. localhost_name, 8080, 0, client_err, sizeof(client_err));
  473. ck_assert_str_eq(client_err, "");
  474. ck_assert(client_conn != NULL);
  475. mg_printf(client_conn, "GET / HTTP/1.1\r\n");
  476. mg_printf(client_conn, "Host: localhost:8080\r\n");
  477. mg_printf(client_conn, "X-Obsolete-Header: something\r\nfor nothing\r\n");
  478. mg_printf(client_conn, "Connection: close\r\n\r\n");
  479. client_res =
  480. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  481. ck_assert_int_ge(client_res, 0);
  482. ck_assert_str_eq(client_err, "");
  483. client_ri = mg_get_response_info(client_conn);
  484. ck_assert(client_ri != NULL);
  485. /* Response must be 400 Bad Request */
  486. ck_assert_int_eq(client_ri->status_code, 400);
  487. mg_close_connection(client_conn);
  488. test_sleep(1);
  489. /* End test */
  490. test_mg_stop(ctx, __LINE__);
  491. mark_point();
  492. }
  493. START_TEST(test_mg_start_stop_http_server)
  494. {
  495. mark_point();
  496. test_mg_start_stop_http_server_impl(0, 0);
  497. mark_point();
  498. test_mg_start_stop_http_server_impl(0, 1);
  499. mark_point();
  500. }
  501. END_TEST
  502. START_TEST(test_mg_start_stop_http_server_ipv6)
  503. {
  504. mark_point();
  505. #if defined(USE_IPV6)
  506. test_mg_start_stop_http_server_impl(1, 0);
  507. mark_point();
  508. test_mg_start_stop_http_server_impl(1, 1);
  509. #endif
  510. mark_point();
  511. }
  512. END_TEST
  513. START_TEST(test_mg_start_stop_https_server)
  514. {
  515. #ifndef NO_SSL
  516. struct mg_context *ctx;
  517. struct mg_callbacks callbacks;
  518. char errmsg[256];
  519. const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
  520. int opt_idx = 0;
  521. const char *ssl_cert = locate_ssl_cert();
  522. struct mg_connection *client_conn;
  523. char client_err[256];
  524. const struct mg_response_info *client_ri;
  525. int client_res, ret;
  526. struct mg_server_port portinfo[8];
  527. ck_assert(ssl_cert != NULL);
  528. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  529. #if !defined(NO_FILES)
  530. OPTIONS[opt_idx++] = "document_root";
  531. OPTIONS[opt_idx++] = ".";
  532. #endif
  533. OPTIONS[opt_idx++] = "listening_ports";
  534. OPTIONS[opt_idx++] = "8080r,8443s";
  535. OPTIONS[opt_idx++] = "ssl_certificate";
  536. OPTIONS[opt_idx++] = ssl_cert;
  537. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  538. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  539. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  540. memset(portinfo, 0, sizeof(portinfo));
  541. memset(&callbacks, 0, sizeof(callbacks));
  542. memset(errmsg, 0, sizeof(errmsg));
  543. callbacks.log_message = log_msg_func;
  544. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, __LINE__);
  545. ck_assert_str_eq(errmsg, "");
  546. ck_assert(ctx != NULL);
  547. ret = mg_get_server_ports(ctx, 0, portinfo);
  548. ck_assert_int_lt(ret, 0);
  549. ck_assert_int_eq(portinfo[0].protocol, 0);
  550. ck_assert_int_eq(portinfo[0].port, 0);
  551. ck_assert_int_eq(portinfo[0].is_ssl, 0);
  552. ck_assert_int_eq(portinfo[0].is_redirect, 0);
  553. ck_assert_int_eq(portinfo[1].protocol, 0);
  554. ck_assert_int_eq(portinfo[1].port, 0);
  555. ck_assert_int_eq(portinfo[1].is_ssl, 0);
  556. ck_assert_int_eq(portinfo[1].is_redirect, 0);
  557. ret = mg_get_server_ports(ctx, 4, portinfo);
  558. ck_assert_int_eq(ret, 2);
  559. ck_assert_int_eq(portinfo[0].protocol, 1);
  560. ck_assert_int_eq(portinfo[0].port, 8080);
  561. ck_assert_int_eq(portinfo[0].is_ssl, 0);
  562. ck_assert_int_eq(portinfo[0].is_redirect, 1);
  563. ck_assert_int_eq(portinfo[1].protocol, 1);
  564. ck_assert_int_eq(portinfo[1].port, 8443);
  565. ck_assert_int_eq(portinfo[1].is_ssl, 1);
  566. ck_assert_int_eq(portinfo[1].is_redirect, 0);
  567. ck_assert_int_eq(portinfo[2].protocol, 0);
  568. ck_assert_int_eq(portinfo[2].port, 0);
  569. ck_assert_int_eq(portinfo[2].is_ssl, 0);
  570. ck_assert_int_eq(portinfo[2].is_redirect, 0);
  571. test_sleep(1);
  572. memset(client_err, 0, sizeof(client_err));
  573. client_conn =
  574. mg_connect_client("127.0.0.1", 8443, 1, client_err, sizeof(client_err));
  575. ck_assert_str_eq(client_err, "");
  576. ck_assert(client_conn != NULL);
  577. mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
  578. client_res =
  579. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  580. ck_assert_int_ge(client_res, 0);
  581. ck_assert_str_eq(client_err, "");
  582. client_ri = mg_get_response_info(client_conn);
  583. ck_assert(client_ri != NULL);
  584. #if defined(NO_FILES)
  585. ck_assert_int_eq(client_ri->status_code, 404);
  586. #else
  587. ck_assert_int_eq(client_ri->status_code, 200);
  588. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  589. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  590. ck_assert_int_gt(client_res, 0);
  591. ck_assert_int_le(client_res, sizeof(client_err));
  592. #endif
  593. mg_close_connection(client_conn);
  594. test_sleep(1);
  595. test_mg_stop(ctx, __LINE__);
  596. mark_point();
  597. #endif
  598. }
  599. END_TEST
  600. START_TEST(test_mg_server_and_client_tls)
  601. {
  602. #ifndef NO_SSL
  603. struct mg_context *ctx;
  604. int ports_cnt;
  605. struct mg_server_port ports[16];
  606. struct mg_callbacks callbacks;
  607. char errmsg[256];
  608. struct mg_connection *client_conn;
  609. char client_err[256];
  610. const struct mg_response_info *client_ri;
  611. int client_res;
  612. struct mg_client_options client_options;
  613. const char *OPTIONS[32]; /* initializer list here is rejected by CI test */
  614. int opt_idx = 0;
  615. char server_cert[256];
  616. char client_cert[256];
  617. const char *res_dir = locate_resources();
  618. ck_assert(res_dir != NULL);
  619. strcpy(server_cert, res_dir);
  620. strcpy(client_cert, res_dir);
  621. #ifdef _WIN32
  622. strcat(server_cert, "cert\\server.pem");
  623. strcat(client_cert, "cert\\client.pem");
  624. #else
  625. strcat(server_cert, "cert/server.pem");
  626. strcat(client_cert, "cert/client.pem");
  627. #endif
  628. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  629. #if !defined(NO_FILES)
  630. OPTIONS[opt_idx++] = "document_root";
  631. OPTIONS[opt_idx++] = ".";
  632. #endif
  633. OPTIONS[opt_idx++] = "listening_ports";
  634. OPTIONS[opt_idx++] = "8080r,8443s";
  635. OPTIONS[opt_idx++] = "ssl_certificate";
  636. OPTIONS[opt_idx++] = server_cert;
  637. OPTIONS[opt_idx++] = "ssl_verify_peer";
  638. OPTIONS[opt_idx++] = "yes";
  639. OPTIONS[opt_idx++] = "ssl_ca_file";
  640. OPTIONS[opt_idx++] = client_cert;
  641. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  642. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  643. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  644. memset(ports, 0, sizeof(ports));
  645. memset(&callbacks, 0, sizeof(callbacks));
  646. memset(errmsg, 0, sizeof(errmsg));
  647. callbacks.log_message = log_msg_func;
  648. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, __LINE__);
  649. ck_assert_str_eq(errmsg, "");
  650. ck_assert(ctx != NULL);
  651. ports_cnt = mg_get_server_ports(ctx, 16, ports);
  652. ck_assert_int_eq(ports_cnt, 2);
  653. ck_assert_int_eq(ports[0].protocol, 1);
  654. ck_assert_int_eq(ports[0].port, 8080);
  655. ck_assert_int_eq(ports[0].is_ssl, 0);
  656. ck_assert_int_eq(ports[0].is_redirect, 1);
  657. ck_assert_int_eq(ports[1].protocol, 1);
  658. ck_assert_int_eq(ports[1].port, 8443);
  659. ck_assert_int_eq(ports[1].is_ssl, 1);
  660. ck_assert_int_eq(ports[1].is_redirect, 0);
  661. ck_assert_int_eq(ports[2].protocol, 0);
  662. ck_assert_int_eq(ports[2].port, 0);
  663. ck_assert_int_eq(ports[2].is_ssl, 0);
  664. ck_assert_int_eq(ports[2].is_redirect, 0);
  665. test_sleep(1);
  666. memset(client_err, 0, sizeof(client_err));
  667. client_conn =
  668. mg_connect_client("127.0.0.1", 8443, 1, client_err, sizeof(client_err));
  669. /* We tried to connect without client certificate:
  670. * Depending on ???, either mg_conn_client failed entirely, returning NULL.
  671. * or we do get a connection but get an error when we try to use it.
  672. *
  673. * MacOS (Version ?), Ubuntu Bionic and Ububtu Eoan allow to connect,
  674. * while Ubuntu Xenial, Ubuntu Trusty and Windows test containers at
  675. * Travis CI do not. Maybe it is OpenSSL version specific.
  676. */
  677. #if defined(OPENSSL_API_1_1)
  678. if (client_conn) {
  679. /* Connect succeeds, but the connection is unusable. */
  680. mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
  681. client_res =
  682. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  683. ck_assert_int_lt(client_res, 0); /* response is "error" (-1) */
  684. ck_assert_str_ne(client_err, "");
  685. client_ri = mg_get_response_info(client_conn);
  686. if (client_ri) {
  687. /* client_ri == NULL is allowed. However, some versions seem to
  688. * return non-null, but all elements are NULL. */
  689. ck_assert_int_eq(client_ri->status_code, 0);
  690. ck_assert_int_eq(client_ri->num_headers, 0);
  691. ck_assert_ptr_eq(client_ri->http_version, NULL);
  692. client_ri = NULL;
  693. }
  694. ck_assert(client_ri == NULL);
  695. mg_close_connection(client_conn);
  696. client_conn = NULL;
  697. strcpy(client_err,
  698. "OpenSSL on MacOS allows to connect without a mandatory client "
  699. "certificate, but not data exchange");
  700. }
  701. #endif
  702. ck_assert(client_conn == NULL);
  703. ck_assert_str_ne(client_err, "");
  704. memset(client_err, 0, sizeof(client_err));
  705. memset(&client_options, 0, sizeof(client_options));
  706. client_options.host = "127.0.0.1";
  707. client_options.port = 8443;
  708. client_options.client_cert = client_cert;
  709. client_options.server_cert = server_cert;
  710. client_conn = mg_connect_client_secure(&client_options,
  711. client_err,
  712. sizeof(client_err));
  713. ck_assert_str_eq(client_err, "");
  714. ck_assert(client_conn != NULL);
  715. mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
  716. client_res =
  717. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  718. ck_assert_int_ge(client_res, 0);
  719. ck_assert_str_eq(client_err, "");
  720. client_ri = mg_get_response_info(client_conn);
  721. ck_assert(client_ri != NULL);
  722. #if defined(NO_FILES)
  723. ck_assert_int_eq(client_ri->status_code, 404);
  724. #else
  725. ck_assert_int_eq(client_ri->status_code, 200);
  726. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  727. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  728. ck_assert_int_gt(client_res, 0);
  729. ck_assert_int_le(client_res, sizeof(client_err));
  730. #endif
  731. mg_close_connection(client_conn);
  732. /* TODO: A client API using a client certificate is missing */
  733. test_sleep(1);
  734. test_mg_stop(ctx, __LINE__);
  735. #endif
  736. mark_point();
  737. }
  738. END_TEST
  739. static struct mg_context *g_ctx;
  740. static int
  741. request_test_handler(struct mg_connection *conn, void *cbdata)
  742. {
  743. int i;
  744. char chunk_data[32];
  745. const struct mg_request_info *ri;
  746. struct mg_context *ctx;
  747. void *ud, *cud, *ud2;
  748. void *dummy = malloc(1);
  749. ctx = mg_get_context(conn);
  750. ud = mg_get_user_data(ctx);
  751. ud2 = mg_get_user_context_data(conn);
  752. ri = mg_get_request_info(conn);
  753. ck_assert(ri != NULL);
  754. ck_assert(ctx == g_ctx);
  755. ck_assert(ud == &g_ctx);
  756. ck_assert(ud == ud2);
  757. ck_assert(dummy != NULL);
  758. mg_set_user_connection_data(conn, (void *)&dummy);
  759. cud = mg_get_user_connection_data(conn);
  760. ck_assert_ptr_eq((void *)cud, (void *)&dummy);
  761. mg_set_user_connection_data(conn, (void *)NULL);
  762. cud = mg_get_user_connection_data(conn);
  763. ck_assert_ptr_eq((void *)cud, (void *)NULL);
  764. free(dummy);
  765. ck_assert_ptr_eq((void *)cbdata, (void *)(ptrdiff_t)7);
  766. strcpy(chunk_data, "123456789A123456789B123456789C");
  767. mg_printf(conn,
  768. "HTTP/1.1 200 OK\r\n"
  769. "Transfer-Encoding: chunked\r\n"
  770. "Content-Type: text/plain\r\n\r\n");
  771. for (i = 1; i <= 10; i++) {
  772. mg_printf(conn, "%x\r\n", i);
  773. mg_write(conn, chunk_data, (unsigned)i);
  774. mg_printf(conn, "\r\n");
  775. }
  776. mg_printf(conn, "0\r\n\r\n");
  777. mark_point();
  778. return 1;
  779. }
  780. static char request_handler2_url_expected[128] = {0};
  781. /* Return the same as request_test_handler using new interfaces */
  782. static int
  783. request_test_handler2(struct mg_connection *conn, void *cbdata)
  784. {
  785. int i;
  786. const char *chunk_data = "123456789A123456789B123456789C";
  787. const struct mg_request_info *ri;
  788. struct mg_context *ctx;
  789. void *ud, *ud2;
  790. int err_ret;
  791. char url_buffer[128];
  792. (void)cbdata; /* unused */
  793. ctx = mg_get_context(conn);
  794. ud = mg_get_user_data(ctx);
  795. ud2 = mg_get_user_context_data(conn);
  796. ri = mg_get_request_info(conn);
  797. ck_assert(ri != NULL);
  798. ck_assert(ctx == g_ctx);
  799. ck_assert(ud == &g_ctx);
  800. ck_assert(ud == ud2);
  801. err_ret = mg_get_request_link(NULL,
  802. url_buffer,
  803. sizeof(url_buffer)); /* param error */
  804. ck_assert(err_ret < 0);
  805. err_ret =
  806. mg_get_request_link(conn, NULL, sizeof(url_buffer)); /* param error */
  807. ck_assert(err_ret < 0);
  808. err_ret = mg_get_request_link(conn, url_buffer, 0); /* param error */
  809. ck_assert(err_ret < 0);
  810. err_ret = mg_get_request_link(conn, url_buffer, 5); /* buffer too small */
  811. ck_assert(err_ret < 0);
  812. err_ret = mg_get_request_link(conn, url_buffer, sizeof(url_buffer));
  813. ck_assert(err_ret == 0);
  814. ck_assert_str_eq(url_buffer, request_handler2_url_expected);
  815. mg_send_http_ok(conn, "text/plain", -1);
  816. for (i = 1; i <= 10; i++) {
  817. mg_send_chunk(conn, chunk_data, (unsigned)i);
  818. }
  819. mg_send_chunk(conn, 0, 0);
  820. mark_point();
  821. return 200;
  822. }
  823. #if defined(USE_WEBSOCKET)
  824. /****************************************************************************/
  825. /* WEBSOCKET SERVER */
  826. /****************************************************************************/
  827. static const char *websocket_welcome_msg = "websocket welcome\n";
  828. static const size_t websocket_welcome_msg_len =
  829. 18 /* strlen(websocket_welcome_msg) */;
  830. static const char *websocket_goodbye_msg = "websocket bye\n";
  831. static const size_t websocket_goodbye_msg_len =
  832. 14 /* strlen(websocket_goodbye_msg) */;
  833. #if defined(WS_DEBUG_TRACE)
  834. static void
  835. WS_TEST_TRACE(const char *f, ...)
  836. {
  837. va_list l;
  838. va_start(l, f);
  839. vprintf(f, l);
  840. va_end(l);
  841. }
  842. #else
  843. #define WS_TEST_TRACE(...)
  844. #endif
  845. static int
  846. websock_server_connect(const struct mg_connection *conn, void *udata)
  847. {
  848. (void)conn;
  849. ck_assert_ptr_eq((void *)udata, (void *)(ptrdiff_t)7531);
  850. WS_TEST_TRACE("Server: Websocket connected\n");
  851. mark_point();
  852. return 0; /* return 0 to accept every connection */
  853. }
  854. static void
  855. websock_server_ready(struct mg_connection *conn, void *udata)
  856. {
  857. ck_assert_ptr_eq((void *)udata, (void *)(ptrdiff_t)7531);
  858. ck_assert_ptr_ne((void *)conn, (void *)NULL);
  859. WS_TEST_TRACE("Server: Websocket ready\n");
  860. /* Send websocket welcome message */
  861. mg_lock_connection(conn);
  862. mg_websocket_write(conn,
  863. MG_WEBSOCKET_OPCODE_TEXT,
  864. websocket_welcome_msg,
  865. websocket_welcome_msg_len);
  866. mg_unlock_connection(conn);
  867. WS_TEST_TRACE("Server: Websocket ready X\n");
  868. mark_point();
  869. }
  870. #define long_ws_buf_len_16 (500)
  871. #define long_ws_buf_len_64 (70000)
  872. static char long_ws_buf[long_ws_buf_len_64];
  873. static int
  874. websock_server_data(struct mg_connection *conn,
  875. int bits,
  876. char *data,
  877. size_t data_len,
  878. void *udata)
  879. {
  880. (void)bits;
  881. ck_assert_ptr_eq((void *)udata, (void *)(ptrdiff_t)7531);
  882. WS_TEST_TRACE("Server: Got %u bytes from the client\n", (unsigned)data_len);
  883. if (data_len == 3 && !memcmp(data, "bye", 3)) {
  884. /* Send websocket goodbye message */
  885. mg_lock_connection(conn);
  886. mg_websocket_write(conn,
  887. MG_WEBSOCKET_OPCODE_TEXT,
  888. websocket_goodbye_msg,
  889. websocket_goodbye_msg_len);
  890. mg_unlock_connection(conn);
  891. } else if (data_len == 5 && !memcmp(data, "data1", 5)) {
  892. mg_lock_connection(conn);
  893. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_TEXT, "ok1", 3);
  894. mg_unlock_connection(conn);
  895. } else if (data_len == 5 && !memcmp(data, "data2", 5)) {
  896. mg_lock_connection(conn);
  897. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_TEXT, "ok 2", 4);
  898. mg_unlock_connection(conn);
  899. } else if (data_len == 5 && !memcmp(data, "data3", 5)) {
  900. mg_lock_connection(conn);
  901. mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_TEXT, "ok - 3", 6);
  902. mg_unlock_connection(conn);
  903. } else if (data_len == long_ws_buf_len_16) {
  904. ck_assert(memcmp(data, long_ws_buf, long_ws_buf_len_16) == 0);
  905. mg_lock_connection(conn);
  906. mg_websocket_write(conn,
  907. MG_WEBSOCKET_OPCODE_BINARY,
  908. long_ws_buf,
  909. long_ws_buf_len_16);
  910. mg_unlock_connection(conn);
  911. } else if (data_len == long_ws_buf_len_64) {
  912. ck_assert(memcmp(data, long_ws_buf, long_ws_buf_len_64) == 0);
  913. mg_lock_connection(conn);
  914. mg_websocket_write(conn,
  915. MG_WEBSOCKET_OPCODE_BINARY,
  916. long_ws_buf,
  917. long_ws_buf_len_64);
  918. mg_unlock_connection(conn);
  919. } else {
  920. #if defined(__MINGW32__) || defined(__GNUC__)
  921. #pragma GCC diagnostic push
  922. #pragma GCC diagnostic ignored "-Wunreachable-code"
  923. #endif
  924. #ifdef __clang__
  925. #pragma clang diagnostic push
  926. #pragma clang diagnostic ignored "-Wunreachable-code"
  927. #endif
  928. ck_abort_msg("Got unexpected message from websocket client");
  929. return 0;
  930. #ifdef __clang__
  931. #pragma clang diagnostic pop
  932. #endif
  933. #if defined(__MINGW32__) || defined(__GNUC__)
  934. #pragma GCC diagnostic pop
  935. #endif
  936. }
  937. mark_point();
  938. return 1; /* return 1 to keep the connection open */
  939. }
  940. static void
  941. websock_server_close(const struct mg_connection *conn, void *udata)
  942. {
  943. #if !defined(__MACH__) && !defined(__APPLE__)
  944. ck_assert_ptr_eq((void *)udata, (void *)(ptrdiff_t)7531);
  945. WS_TEST_TRACE("Server: Close connection\n");
  946. /* Can not send a websocket goodbye message here -
  947. * the connection is already closed */
  948. mark_point();
  949. #endif
  950. (void)conn;
  951. (void)udata;
  952. }
  953. /****************************************************************************/
  954. /* WEBSOCKET CLIENT */
  955. /****************************************************************************/
  956. struct tclient_data {
  957. void *data;
  958. size_t len;
  959. int closed;
  960. int clientId;
  961. };
  962. static int
  963. websocket_client_data_handler(struct mg_connection *conn,
  964. int flags,
  965. char *data,
  966. size_t data_len,
  967. void *user_data)
  968. {
  969. struct mg_context *ctx = mg_get_context(conn);
  970. struct tclient_data *pclient_data =
  971. (struct tclient_data *)mg_get_user_data(ctx);
  972. ck_assert_ptr_eq(user_data, (void *)pclient_data);
  973. ck_assert(pclient_data != NULL);
  974. ck_assert_int_gt(flags, 128);
  975. ck_assert_int_lt(flags, 128 + 16);
  976. ck_assert((flags == (int)(128 | MG_WEBSOCKET_OPCODE_BINARY))
  977. || (flags == (int)(128 | MG_WEBSOCKET_OPCODE_TEXT)));
  978. if (flags == (int)(128 | MG_WEBSOCKET_OPCODE_TEXT)) {
  979. WS_TEST_TRACE(
  980. "Client %i received %lu bytes text data from server: %.*s\n",
  981. pclient_data->clientId,
  982. (unsigned long)data_len,
  983. (int)data_len,
  984. data);
  985. } else {
  986. WS_TEST_TRACE("Client %i received %lu bytes binary data from\n",
  987. pclient_data->clientId,
  988. (unsigned long)data_len);
  989. }
  990. pclient_data->data = malloc(data_len);
  991. ck_assert(pclient_data->data != NULL);
  992. memcpy(pclient_data->data, data, data_len);
  993. pclient_data->len = data_len;
  994. mark_point();
  995. return 1;
  996. }
  997. static void
  998. websocket_client_close_handler(const struct mg_connection *conn,
  999. void *user_data)
  1000. {
  1001. struct mg_context *ctx = mg_get_context(conn);
  1002. struct tclient_data *pclient_data =
  1003. (struct tclient_data *)mg_get_user_data(ctx);
  1004. #if !defined(__MACH__) && !defined(__APPLE__)
  1005. ck_assert_ptr_eq(user_data, (void *)pclient_data);
  1006. ck_assert(pclient_data != NULL);
  1007. WS_TEST_TRACE("Client %i: Close handler\n", pclient_data->clientId);
  1008. pclient_data->closed++;
  1009. mark_point();
  1010. #else
  1011. (void)user_data;
  1012. pclient_data->closed++;
  1013. #endif /* __MACH__ && __APPLE__ */
  1014. }
  1015. #endif /* USE_WEBSOCKET */
  1016. START_TEST(test_request_handlers)
  1017. {
  1018. char ebuf[1024];
  1019. struct mg_context *ctx;
  1020. struct mg_connection *client_conn;
  1021. const struct mg_response_info *client_ri;
  1022. char uri[64];
  1023. char buf[1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 8];
  1024. const char *expected =
  1025. "112123123412345123456123456712345678123456789123456789A";
  1026. int i;
  1027. const char *request = "GET /U7 HTTP/1.0\r\n\r\n";
  1028. #if defined(USE_IPV6) && defined(NO_SSL)
  1029. const char *HTTP_PORT = "8084,[::]:8086";
  1030. short ipv4_port = 8084;
  1031. short ipv6_port = 8086;
  1032. #elif !defined(USE_IPV6) && defined(NO_SSL)
  1033. const char *HTTP_PORT = "8084";
  1034. short ipv4_port = 8084;
  1035. #elif defined(USE_IPV6) && !defined(NO_SSL)
  1036. const char *HTTP_PORT = "8084,[::]:8086,8194r,[::]:8196r,8094s,[::]:8096s";
  1037. short ipv4_port = 8084;
  1038. short ipv4s_port = 8094;
  1039. short ipv4r_port = 8194;
  1040. short ipv6_port = 8086;
  1041. short ipv6s_port = 8096;
  1042. short ipv6r_port = 8196;
  1043. #elif !defined(USE_IPV6) && !defined(NO_SSL)
  1044. const char *HTTP_PORT = "8084,8194r,8094s";
  1045. short ipv4_port = 8084;
  1046. short ipv4s_port = 8094;
  1047. short ipv4r_port = 8194;
  1048. #endif
  1049. const char *OPTIONS[16];
  1050. const char *opt;
  1051. FILE *f;
  1052. const char *plain_file_content;
  1053. const char *cgi_script_content;
  1054. const char *expected_cgi_result;
  1055. int opt_idx = 0;
  1056. struct stat st;
  1057. const char encoded_file_content[] = "\x1f\x8b\x08\x08\xf8"
  1058. "\x9d\xcb\x55\x00\x00"
  1059. "test_gz.txt"
  1060. "\x00\x01\x11\x00\xee\xff"
  1061. "zipped text file"
  1062. "\x0a\x34\x5f\xcc\x49"
  1063. "\x11\x00\x00\x00";
  1064. size_t encoded_file_content_len = sizeof(encoded_file_content);
  1065. #if !defined(NO_SSL)
  1066. const char *ssl_cert = locate_ssl_cert();
  1067. #endif
  1068. #if defined(USE_WEBSOCKET)
  1069. struct tclient_data ws_client1_data = {NULL, 0, 0, 1};
  1070. struct tclient_data ws_client2_data = {NULL, 0, 0, 2};
  1071. struct tclient_data ws_client3_data = {NULL, 0, 0, 3};
  1072. struct tclient_data ws_client4_data = {NULL, 0, 0, 4};
  1073. struct mg_connection *ws_client1_conn = NULL;
  1074. struct mg_connection *ws_client2_conn = NULL;
  1075. struct mg_connection *ws_client3_conn = NULL;
  1076. struct mg_connection *ws_client4_conn = NULL;
  1077. #endif
  1078. char cmd_buf[1024];
  1079. char *cgi_env_opt;
  1080. const char *server_host = "test.domain";
  1081. mark_point();
  1082. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  1083. OPTIONS[opt_idx++] = "listening_ports";
  1084. OPTIONS[opt_idx++] = HTTP_PORT;
  1085. OPTIONS[opt_idx++] = "authentication_domain";
  1086. OPTIONS[opt_idx++] = server_host;
  1087. #if !defined(NO_FILES)
  1088. OPTIONS[opt_idx++] = "document_root";
  1089. OPTIONS[opt_idx++] = ".";
  1090. #endif
  1091. #ifndef NO_SSL
  1092. ck_assert(ssl_cert != NULL);
  1093. OPTIONS[opt_idx++] = "ssl_certificate";
  1094. OPTIONS[opt_idx++] = ssl_cert;
  1095. #endif
  1096. OPTIONS[opt_idx++] = "cgi_environment";
  1097. cgi_env_opt = (char *)calloc(1, 4096 /* CGI_ENVIRONMENT_SIZE */);
  1098. ck_assert(cgi_env_opt != NULL);
  1099. cgi_env_opt[0] = 'x';
  1100. cgi_env_opt[1] = '=';
  1101. memset(cgi_env_opt + 2, 'y', 4090); /* Add large env field, so the server
  1102. * must reallocate buffers. */
  1103. OPTIONS[opt_idx++] = cgi_env_opt;
  1104. OPTIONS[opt_idx++] = "num_threads";
  1105. OPTIONS[opt_idx++] = "2";
  1106. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  1107. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  1108. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  1109. ctx = test_mg_start(NULL, &g_ctx, OPTIONS, __LINE__);
  1110. ck_assert(ctx != NULL);
  1111. g_ctx = ctx;
  1112. opt = mg_get_option(ctx, "listening_ports");
  1113. ck_assert_str_eq(opt, HTTP_PORT);
  1114. opt = mg_get_option(ctx, "cgi_environment");
  1115. ck_assert_ptr_ne(opt, cgi_env_opt);
  1116. ck_assert_int_eq((int)opt[0], (int)cgi_env_opt[0]);
  1117. ck_assert_int_eq((int)opt[1], (int)cgi_env_opt[1]);
  1118. ck_assert_int_eq((int)opt[2], (int)cgi_env_opt[2]);
  1119. ck_assert_int_eq((int)opt[3], (int)cgi_env_opt[3]);
  1120. /* full length string compare will reach limit in the implementation
  1121. * of the check unit test framework */
  1122. {
  1123. size_t len_check_1 = strlen(opt);
  1124. size_t len_check_2 = strlen(cgi_env_opt);
  1125. ck_assert_uint_eq(len_check_1, len_check_2);
  1126. }
  1127. /* We don't need the original anymore, the server has a private copy */
  1128. free(cgi_env_opt);
  1129. opt = mg_get_option(ctx, "throttle");
  1130. ck_assert_str_eq(opt, "");
  1131. opt = mg_get_option(ctx, "unknown_option_name");
  1132. ck_assert(opt == NULL);
  1133. for (i = 0; i < 1000; i++) {
  1134. sprintf(uri, "/U%u", i);
  1135. mg_set_request_handler(ctx, uri, request_test_handler, NULL);
  1136. }
  1137. for (i = 500; i < 800; i++) {
  1138. sprintf(uri, "/U%u", i);
  1139. mg_set_request_handler(ctx, uri, NULL, (void *)(ptrdiff_t)1);
  1140. }
  1141. for (i = 600; i >= 0; i--) {
  1142. sprintf(uri, "/U%u", i);
  1143. mg_set_request_handler(ctx, uri, NULL, (void *)(ptrdiff_t)2);
  1144. }
  1145. for (i = 750; i <= 1000; i++) {
  1146. sprintf(uri, "/U%u", i);
  1147. mg_set_request_handler(ctx, uri, NULL, (void *)(ptrdiff_t)3);
  1148. }
  1149. for (i = 5; i < 9; i++) {
  1150. sprintf(uri, "/U%u", i);
  1151. mg_set_request_handler(ctx,
  1152. uri,
  1153. request_test_handler,
  1154. (void *)(ptrdiff_t)i);
  1155. }
  1156. sprintf(request_handler2_url_expected,
  1157. "http://%s:%u/handler2",
  1158. server_host,
  1159. ipv4_port);
  1160. mg_set_request_handler(ctx, "/handler2", request_test_handler2, NULL);
  1161. #if defined(USE_WEBSOCKET)
  1162. mg_set_websocket_handler(ctx,
  1163. "/websocket",
  1164. websock_server_connect,
  1165. websock_server_ready,
  1166. websock_server_data,
  1167. websock_server_close,
  1168. (void *)(ptrdiff_t)7531);
  1169. #endif
  1170. /* Try to load non existing file */
  1171. client_conn = mg_download("localhost",
  1172. ipv4_port,
  1173. 0,
  1174. ebuf,
  1175. sizeof(ebuf),
  1176. "%s",
  1177. "GET /file/not/found HTTP/1.0\r\n\r\n");
  1178. ck_assert(client_conn != NULL);
  1179. client_ri = mg_get_response_info(client_conn);
  1180. ck_assert(client_ri != NULL);
  1181. ck_assert_int_eq(client_ri->status_code, 404);
  1182. mg_close_connection(client_conn);
  1183. /* Get data from callback */
  1184. client_conn = mg_download(
  1185. "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  1186. ck_assert(client_conn != NULL);
  1187. client_ri = mg_get_response_info(client_conn);
  1188. ck_assert(client_ri != NULL);
  1189. ck_assert_int_eq(client_ri->status_code, 200);
  1190. i = mg_read(client_conn, buf, sizeof(buf));
  1191. ck_assert_int_eq(i, (int)strlen(expected));
  1192. buf[i] = 0;
  1193. ck_assert_str_eq(buf, expected);
  1194. mg_close_connection(client_conn);
  1195. /* Get data from callback using http://127.0.0.1 */
  1196. client_conn = mg_download(
  1197. "127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  1198. ck_assert(client_conn != NULL);
  1199. client_ri = mg_get_response_info(client_conn);
  1200. ck_assert(client_ri != NULL);
  1201. ck_assert_int_eq(client_ri->status_code, 200);
  1202. i = mg_read(client_conn, buf, sizeof(buf));
  1203. if ((i >= 0) && ((size_t)i < sizeof(buf))) {
  1204. buf[i] = 0;
  1205. } else {
  1206. ck_abort_msg(
  1207. "ERROR: test_request_handlers: read returned %i (>=0, <%i)",
  1208. (int)i,
  1209. (int)sizeof(buf));
  1210. }
  1211. ck_assert((int)i < (int)sizeof(buf));
  1212. ck_assert(i > 0);
  1213. ck_assert_int_eq(i, (int)strlen(expected));
  1214. buf[i] = 0;
  1215. ck_assert_str_eq(buf, expected);
  1216. mg_close_connection(client_conn);
  1217. #if defined(USE_IPV6)
  1218. /* Get data from callback using http://[::1] */
  1219. client_conn =
  1220. mg_download("[::1]", ipv6_port, 0, ebuf, sizeof(ebuf), "%s", request);
  1221. ck_assert(client_conn != NULL);
  1222. client_ri = mg_get_response_info(client_conn);
  1223. ck_assert(client_ri != NULL);
  1224. ck_assert_int_eq(client_ri->status_code, 200);
  1225. i = mg_read(client_conn, buf, sizeof(buf));
  1226. ck_assert_int_eq(i, (int)strlen(expected));
  1227. buf[i] = 0;
  1228. ck_assert_str_eq(buf, expected);
  1229. mg_close_connection(client_conn);
  1230. #endif
  1231. #if !defined(NO_SSL)
  1232. /* Get data from callback using https://127.0.0.1 */
  1233. client_conn = mg_download(
  1234. "127.0.0.1", ipv4s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  1235. ck_assert(client_conn != NULL);
  1236. client_ri = mg_get_response_info(client_conn);
  1237. ck_assert(client_ri != NULL);
  1238. ck_assert_int_eq(client_ri->status_code, 200);
  1239. i = mg_read(client_conn, buf, sizeof(buf));
  1240. ck_assert_int_eq(i, (int)strlen(expected));
  1241. buf[i] = 0;
  1242. ck_assert_str_eq(buf, expected);
  1243. mg_close_connection(client_conn);
  1244. /* Get redirect from callback using http://127.0.0.1 */
  1245. client_conn = mg_download(
  1246. "127.0.0.1", ipv4r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  1247. ck_assert(client_conn != NULL);
  1248. client_ri = mg_get_response_info(client_conn);
  1249. ck_assert(client_ri != NULL);
  1250. if ((client_ri->status_code != 301) && (client_ri->status_code != 302)
  1251. && (client_ri->status_code != 303) && (client_ri->status_code != 307)
  1252. && (client_ri->status_code != 308)) {
  1253. /* expect a 30x redirect code */
  1254. ck_abort_msg("Expected a redirect code, got %i",
  1255. client_ri->status_code);
  1256. }
  1257. /*
  1258. // A redirect may have a body, or not
  1259. i = mg_read(client_conn, buf, sizeof(buf));
  1260. ck_assert_int_eq(i, -1);
  1261. */
  1262. mg_close_connection(client_conn);
  1263. #endif
  1264. #if defined(USE_IPV6) && !defined(NO_SSL)
  1265. /* Get data from callback using https://[::1] */
  1266. client_conn =
  1267. mg_download("[::1]", ipv6s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  1268. ck_assert(client_conn != NULL);
  1269. client_ri = mg_get_response_info(client_conn);
  1270. ck_assert(client_ri != NULL);
  1271. ck_assert_int_eq(client_ri->status_code, 200);
  1272. i = mg_read(client_conn, buf, sizeof(buf));
  1273. ck_assert_int_eq(i, (int)strlen(expected));
  1274. buf[i] = 0;
  1275. ck_assert_str_eq(buf, expected);
  1276. mg_close_connection(client_conn);
  1277. /* Get redirect from callback using http://127.0.0.1 */
  1278. client_conn =
  1279. mg_download("[::1]", ipv6r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  1280. ck_assert(client_conn != NULL);
  1281. client_ri = mg_get_response_info(client_conn);
  1282. ck_assert(client_ri != NULL);
  1283. ck_assert((client_ri->status_code == 301) || (client_ri->status_code == 302)
  1284. || (client_ri->status_code == 303)
  1285. || (client_ri->status_code == 307)
  1286. || (client_ri->status_code == 308)); /* is a redirect code */
  1287. /*
  1288. // A redirect may have a body, or not
  1289. i = mg_read(client_conn, buf, sizeof(buf));
  1290. ck_assert_int_eq(i, -1);
  1291. */
  1292. mg_close_connection(client_conn);
  1293. #endif
  1294. /* It seems to be impossible to find out what the actual working
  1295. * directory of the CI test environment is. Before breaking another
  1296. * dozen of builds by trying blindly with different paths, just
  1297. * create the file here */
  1298. #ifdef _WIN32
  1299. f = fopen("test.txt", "wb");
  1300. #else
  1301. f = fopen("test.txt", "w");
  1302. #endif
  1303. ck_assert(f != NULL);
  1304. plain_file_content = "simple text file\n";
  1305. i = (int)strlen(plain_file_content);
  1306. ck_assert_int_eq(i, 17);
  1307. fwrite(plain_file_content, (size_t)i, 1, f);
  1308. fclose(f);
  1309. #ifdef _WIN32
  1310. f = fopen("test_gz.txt.gz", "wb");
  1311. #else
  1312. f = fopen("test_gz.txt.gz", "w");
  1313. #endif
  1314. ck_assert(f != NULL);
  1315. ck_assert_uint_ge(encoded_file_content_len, 52);
  1316. encoded_file_content_len = 52;
  1317. fwrite(encoded_file_content, 1, encoded_file_content_len, f);
  1318. fclose(f);
  1319. #ifdef _WIN32
  1320. f = fopen("test.cgi", "wb");
  1321. ck_assert(f != NULL);
  1322. cgi_script_content = "#!test.cgi.cmd\r\n";
  1323. fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
  1324. fclose(f);
  1325. f = fopen("test.cgi.cmd", "w");
  1326. cgi_script_content = "@echo off\r\n"
  1327. "echo Connection: close\r\n"
  1328. "echo Content-Type: text/plain\r\n"
  1329. "echo.\r\n"
  1330. "echo CGI test\r\n"
  1331. "\r\n";
  1332. fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
  1333. fclose(f);
  1334. #else
  1335. f = fopen("test.cgi", "w");
  1336. ck_assert(f != NULL);
  1337. cgi_script_content = "#!/bin/sh\n\n"
  1338. "printf \"Connection: close\\r\\n\"\n"
  1339. "printf \"Content-Type: text/plain\\r\\n\"\n"
  1340. "printf \"\\r\\n\"\n"
  1341. "printf \"CGI test\\r\\n\"\n"
  1342. "\n";
  1343. (void)fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
  1344. (void)fclose(f);
  1345. (void)system("chmod a+x test.cgi");
  1346. #endif
  1347. expected_cgi_result = "CGI test";
  1348. /* Get static data */
  1349. client_conn = mg_download("localhost",
  1350. ipv4_port,
  1351. 0,
  1352. ebuf,
  1353. sizeof(ebuf),
  1354. "%s",
  1355. "GET /test.txt HTTP/1.0\r\n\r\n");
  1356. ck_assert(client_conn != NULL);
  1357. client_ri = mg_get_response_info(client_conn);
  1358. ck_assert(client_ri != NULL);
  1359. #if defined(NO_FILES)
  1360. ck_assert_int_eq(client_ri->status_code, 404);
  1361. #else
  1362. ck_assert_int_eq(client_ri->status_code, 200);
  1363. i = mg_read(client_conn, buf, sizeof(buf));
  1364. ck_assert_int_eq(i, 17);
  1365. if ((i >= 0) && (i < (int)sizeof(buf))) {
  1366. buf[i] = 0;
  1367. }
  1368. ck_assert_str_eq(buf, plain_file_content);
  1369. #endif
  1370. mg_close_connection(client_conn);
  1371. /* Check if CGI test executable exists */
  1372. memset(&st, 0, sizeof(st));
  1373. #if defined(_WIN32)
  1374. /* TODO: not yet available */
  1375. sprintf(ebuf, "%scgi_test.cgi", locate_test_exes());
  1376. #else
  1377. sprintf(ebuf, "%scgi_test.cgi", locate_test_exes());
  1378. if (stat(ebuf, &st) != 0) {
  1379. char cwd[512];
  1380. getcwd(cwd, sizeof(cwd));
  1381. fprintf(stderr, "\nFile %s not found\n", ebuf);
  1382. fprintf(stderr, "Working directory is %s\n", cwd);
  1383. fprintf(stderr,
  1384. "This file needs to be compiled manually before "
  1385. "starting the test\n");
  1386. fprintf(stderr,
  1387. "e.g. by gcc unittest/cgi_test.c -o output/cgi_test.cgi\n\n");
  1388. /* Abort test with diagnostic message */
  1389. ck_abort_msg("Mandatory file %s must be built before starting the test "
  1390. "(cwd: %s)",
  1391. ebuf,
  1392. cwd);
  1393. }
  1394. #endif
  1395. /* Test with CGI test executable */
  1396. #if defined(_WIN32)
  1397. sprintf(cmd_buf, "copy %s\\cgi_test.cgi cgi_test.exe", locate_test_exes());
  1398. #else
  1399. sprintf(cmd_buf, "cp %s/cgi_test.cgi cgi_test.cgi", locate_test_exes());
  1400. #endif
  1401. (void)system(cmd_buf);
  1402. #if !defined(NO_CGI) && !defined(NO_FILES) && !defined(_WIN32)
  1403. /* TODO: add test for windows, check with POST */
  1404. client_conn = mg_download(
  1405. "localhost",
  1406. ipv4_port,
  1407. 0,
  1408. ebuf,
  1409. sizeof(ebuf),
  1410. "%s",
  1411. "POST /cgi_test.cgi/x/y.z HTTP/1.0\r\nContent-Length: 3\r\n\r\nABC");
  1412. ck_assert(client_conn != NULL);
  1413. client_ri = mg_get_response_info(client_conn);
  1414. ck_assert(client_ri != NULL);
  1415. ck_assert_int_eq(client_ri->status_code, 200);
  1416. mg_close_connection(client_conn);
  1417. #endif
  1418. /* Get zipped static data - will not work if Accept-Encoding is not set */
  1419. client_conn = mg_download("localhost",
  1420. ipv4_port,
  1421. 0,
  1422. ebuf,
  1423. sizeof(ebuf),
  1424. "%s",
  1425. "GET /test_gz.txt HTTP/1.0\r\n\r\n");
  1426. ck_assert(client_conn != NULL);
  1427. client_ri = mg_get_response_info(client_conn);
  1428. ck_assert(client_ri != NULL);
  1429. ck_assert_int_eq(client_ri->status_code, 404);
  1430. mg_close_connection(client_conn);
  1431. /* Get zipped static data - with Accept-Encoding */
  1432. client_conn = mg_download(
  1433. "localhost",
  1434. ipv4_port,
  1435. 0,
  1436. ebuf,
  1437. sizeof(ebuf),
  1438. "%s",
  1439. "GET /test_gz.txt HTTP/1.0\r\nAccept-Encoding: gzip\r\n\r\n");
  1440. ck_assert(client_conn != NULL);
  1441. client_ri = mg_get_response_info(client_conn);
  1442. ck_assert(client_ri != NULL);
  1443. #if defined(NO_FILES)
  1444. ck_assert_int_eq(client_ri->status_code, 404);
  1445. #else
  1446. ck_assert_int_eq(client_ri->status_code, 200);
  1447. i = mg_read(client_conn, buf, sizeof(buf));
  1448. ck_assert_int_eq(i, 52);
  1449. if ((i >= 0) && (i < (int)sizeof(buf))) {
  1450. buf[i] = 0;
  1451. }
  1452. ck_assert_int_eq(client_ri->content_length, 52);
  1453. ck_assert_str_eq(buf, encoded_file_content);
  1454. #endif
  1455. mg_close_connection(client_conn);
  1456. /* Get CGI generated data */
  1457. #if !defined(NO_CGI)
  1458. client_conn = mg_download("localhost",
  1459. ipv4_port,
  1460. 0,
  1461. ebuf,
  1462. sizeof(ebuf),
  1463. "%s",
  1464. "GET /test.cgi HTTP/1.0\r\n\r\n");
  1465. ck_assert(client_conn != NULL);
  1466. client_ri = mg_get_response_info(client_conn);
  1467. ck_assert(client_ri != NULL);
  1468. #if defined(NO_FILES)
  1469. ck_assert_int_eq(client_ri->status_code, 404);
  1470. (void)expected_cgi_result;
  1471. (void)cgi_script_content;
  1472. #else
  1473. i = mg_read(client_conn, buf, sizeof(buf));
  1474. if ((i >= 0) && (i < (int)sizeof(buf))) {
  1475. while ((i > 0) && ((buf[i - 1] == '\r') || (buf[i - 1] == '\n'))) {
  1476. i--;
  1477. }
  1478. buf[i] = 0;
  1479. }
  1480. /* ck_assert_int_eq(i, (int)strlen(expected_cgi_result)); */
  1481. ck_assert_str_eq(buf, expected_cgi_result);
  1482. ck_assert_int_eq(client_ri->status_code, 200);
  1483. mg_close_connection(client_conn);
  1484. #endif
  1485. #else
  1486. (void)expected_cgi_result;
  1487. (void)cgi_script_content;
  1488. #endif
  1489. /* Get directory listing */
  1490. client_conn = mg_download("localhost",
  1491. ipv4_port,
  1492. 0,
  1493. ebuf,
  1494. sizeof(ebuf),
  1495. "%s",
  1496. "GET / HTTP/1.0\r\n\r\n");
  1497. ck_assert(client_conn != NULL);
  1498. client_ri = mg_get_response_info(client_conn);
  1499. ck_assert(client_ri != NULL);
  1500. #if defined(NO_FILES)
  1501. ck_assert_int_eq(client_ri->status_code, 404);
  1502. #else
  1503. ck_assert_int_eq(client_ri->status_code, 200);
  1504. i = mg_read(client_conn, buf, sizeof(buf));
  1505. ck_assert(i > 6);
  1506. buf[6] = 0;
  1507. ck_assert_str_eq(buf, "<html>");
  1508. #endif
  1509. mg_close_connection(client_conn);
  1510. /* POST to static file (will not work) */
  1511. client_conn = mg_download("localhost",
  1512. ipv4_port,
  1513. 0,
  1514. ebuf,
  1515. sizeof(ebuf),
  1516. "%s",
  1517. "POST /test.txt HTTP/1.0\r\n\r\n");
  1518. ck_assert(client_conn != NULL);
  1519. client_ri = mg_get_response_info(client_conn);
  1520. ck_assert(client_ri != NULL);
  1521. #if defined(NO_FILES)
  1522. ck_assert_int_eq(client_ri->status_code, 404);
  1523. #else
  1524. ck_assert_int_eq(client_ri->status_code, 405);
  1525. i = mg_read(client_conn, buf, sizeof(buf));
  1526. ck_assert(i >= 29);
  1527. buf[29] = 0;
  1528. ck_assert_str_eq(buf, "Error 405: Method Not Allowed");
  1529. #endif
  1530. mg_close_connection(client_conn);
  1531. /* PUT to static file (will not work) */
  1532. client_conn = mg_download("localhost",
  1533. ipv4_port,
  1534. 0,
  1535. ebuf,
  1536. sizeof(ebuf),
  1537. "%s",
  1538. "PUT /test.txt HTTP/1.0\r\n\r\n");
  1539. ck_assert(client_conn != NULL);
  1540. client_ri = mg_get_response_info(client_conn);
  1541. ck_assert(client_ri != NULL);
  1542. /* Result must be an error code*/
  1543. ck_assert_int_gt(client_ri->status_code, 400);
  1544. ck_assert_int_lt(client_ri->status_code, 500);
  1545. #if defined(NO_FILES)
  1546. /* In case there is no filesystem, PUT is not a valid method */
  1547. ck_assert_int_eq(client_ri->status_code, 405); /* method not allowed */
  1548. #else
  1549. /* In case there is a filesystem but no auth file is provided,
  1550. * PUT is not a valid method */
  1551. if (client_ri->status_code != 405) {
  1552. /* 401: It would be possible in principle, but there client needs
  1553. * to send authentication data */
  1554. ck_assert_int_eq(client_ri->status_code, 401); /* not authorized */
  1555. }
  1556. #endif
  1557. mg_close_connection(client_conn);
  1558. /* Get data from callback using mg_connect_client instead of mg_download */
  1559. memset(ebuf, 0, sizeof(ebuf));
  1560. client_conn =
  1561. mg_connect_client("127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf));
  1562. ck_assert_str_eq(ebuf, "");
  1563. ck_assert(client_conn != NULL);
  1564. mg_printf(client_conn, "%s", request);
  1565. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1566. ck_assert_int_ge(i, 0);
  1567. ck_assert_str_eq(ebuf, "");
  1568. client_ri = mg_get_response_info(client_conn);
  1569. ck_assert(client_ri != NULL);
  1570. ck_assert_int_eq(client_ri->status_code, 200);
  1571. i = mg_read(client_conn, buf, sizeof(buf));
  1572. ck_assert_int_eq(i, (int)strlen(expected));
  1573. buf[i] = 0;
  1574. ck_assert_str_eq(buf, expected);
  1575. mg_close_connection(client_conn);
  1576. /* Get data from callback using mg_connect_client and absolute URI */
  1577. memset(ebuf, 0, sizeof(ebuf));
  1578. client_conn =
  1579. mg_connect_client("localhost", ipv4_port, 0, ebuf, sizeof(ebuf));
  1580. ck_assert_str_eq(ebuf, "");
  1581. ck_assert(client_conn != NULL);
  1582. mg_printf(client_conn,
  1583. "GET http://test.domain:%d/U7 HTTP/1.0\r\n\r\n",
  1584. ipv4_port);
  1585. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1586. ck_assert_int_ge(i, 0);
  1587. ck_assert_str_eq(ebuf, "");
  1588. client_ri = mg_get_response_info(client_conn);
  1589. ck_assert(client_ri != NULL);
  1590. ck_assert_int_eq(client_ri->status_code, 200);
  1591. i = mg_read(client_conn, buf, sizeof(buf));
  1592. ck_assert_int_eq(i, (int)strlen(expected));
  1593. buf[i] = 0;
  1594. ck_assert_str_eq(buf, expected);
  1595. mg_close_connection(client_conn);
  1596. /* Get data from callback using mg_connect_client and absolute URI with a
  1597. * sub-domain */
  1598. memset(ebuf, 0, sizeof(ebuf));
  1599. client_conn =
  1600. mg_connect_client("localhost", ipv4_port, 0, ebuf, sizeof(ebuf));
  1601. ck_assert_str_eq(ebuf, "");
  1602. ck_assert(client_conn != NULL);
  1603. mg_printf(client_conn,
  1604. "GET http://subdomain.test.domain:%d/U7 HTTP/1.0\r\n\r\n",
  1605. ipv4_port);
  1606. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1607. ck_assert_int_ge(i, 0);
  1608. ck_assert_str_eq(ebuf, "");
  1609. client_ri = mg_get_response_info(client_conn);
  1610. ck_assert(client_ri != NULL);
  1611. ck_assert_int_eq(client_ri->status_code, 200);
  1612. i = mg_read(client_conn, buf, sizeof(buf));
  1613. ck_assert_int_eq(i, (int)strlen(expected));
  1614. buf[i] = 0;
  1615. ck_assert_str_eq(buf, expected);
  1616. mg_close_connection(client_conn);
  1617. /* Get data from handler2 */
  1618. client_conn =
  1619. mg_connect_client("localhost", ipv4_port, 0, ebuf, sizeof(ebuf));
  1620. ck_assert_str_eq(ebuf, "");
  1621. ck_assert(client_conn != NULL);
  1622. mg_printf(client_conn,
  1623. "GET /handler2 HTTP/1.1\r\n"
  1624. "Host: %s\r\n"
  1625. "\r\n",
  1626. server_host);
  1627. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1628. ck_assert_int_ge(i, 0);
  1629. ck_assert_str_eq(ebuf, "");
  1630. client_ri = mg_get_response_info(client_conn);
  1631. ck_assert(client_ri != NULL);
  1632. ck_assert_int_eq(client_ri->status_code, 200);
  1633. i = mg_read(client_conn, buf, sizeof(buf));
  1634. ck_assert_int_eq(i, (int)strlen(expected));
  1635. buf[i] = 0;
  1636. ck_assert_str_eq(buf, expected);
  1637. mg_close_connection(client_conn);
  1638. /* Get data non existing handler (will return 404) */
  1639. client_conn =
  1640. mg_connect_client("localhost", ipv4_port, 0, ebuf, sizeof(ebuf));
  1641. ck_assert_str_eq(ebuf, "");
  1642. ck_assert(client_conn != NULL);
  1643. mg_printf(client_conn,
  1644. "GET /unknown_url HTTP/1.1\r\n"
  1645. "Host: localhost:%u\r\n"
  1646. "\r\n",
  1647. ipv4_port);
  1648. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1649. ck_assert_int_ge(i, 0);
  1650. ck_assert_str_eq(ebuf, "");
  1651. client_ri = mg_get_response_info(client_conn);
  1652. ck_assert(client_ri != NULL);
  1653. ck_assert_int_eq(client_ri->status_code, 404);
  1654. mg_close_connection(client_conn);
  1655. /* Get data from handler2, but only read a part of it */
  1656. client_conn =
  1657. mg_connect_client("localhost", ipv4_port, 0, ebuf, sizeof(ebuf));
  1658. ck_assert_str_eq(ebuf, "");
  1659. ck_assert(client_conn != NULL);
  1660. mg_printf(client_conn,
  1661. "GET /handler2 HTTP/1.1\r\n"
  1662. "Host: localhost:%u\r\n"
  1663. "\r\n",
  1664. ipv4_port);
  1665. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1666. ck_assert_int_ge(i, 0);
  1667. ck_assert_str_eq(ebuf, "");
  1668. client_ri = mg_get_response_info(client_conn);
  1669. ck_assert(client_ri != NULL);
  1670. ck_assert_int_eq(client_ri->status_code, 200);
  1671. i = mg_read(client_conn, buf, 7);
  1672. ck_assert_int_eq(i, 7);
  1673. ck_assert(0 == memcmp(buf, expected, 7));
  1674. mg_close_connection(client_conn);
  1675. /* Websocket test */
  1676. #if defined(USE_WEBSOCKET)
  1677. /* Then connect a first client */
  1678. ws_client1_conn =
  1679. mg_connect_websocket_client("localhost",
  1680. ipv4_port,
  1681. 0,
  1682. ebuf,
  1683. sizeof(ebuf),
  1684. "/websocket",
  1685. NULL,
  1686. websocket_client_data_handler,
  1687. websocket_client_close_handler,
  1688. &ws_client1_data);
  1689. ck_assert(ws_client1_conn != NULL);
  1690. wait_not_null(
  1691. &(ws_client1_data.data)); /* Wait for the websocket welcome message */
  1692. ck_assert_int_eq(ws_client1_data.closed, 0);
  1693. ck_assert_int_eq(ws_client2_data.closed, 0);
  1694. ck_assert_int_eq(ws_client3_data.closed, 0);
  1695. ck_assert(ws_client2_data.data == NULL);
  1696. ck_assert_uint_eq(ws_client2_data.len, 0);
  1697. ck_assert(ws_client1_data.data != NULL);
  1698. ck_assert_uint_eq(ws_client1_data.len, websocket_welcome_msg_len);
  1699. ck_assert(!memcmp(ws_client1_data.data,
  1700. websocket_welcome_msg,
  1701. websocket_welcome_msg_len));
  1702. free(ws_client1_data.data);
  1703. ws_client1_data.data = NULL;
  1704. ws_client1_data.len = 0;
  1705. mg_websocket_client_write(ws_client1_conn,
  1706. MG_WEBSOCKET_OPCODE_TEXT,
  1707. "data1",
  1708. 5);
  1709. wait_not_null(&(
  1710. ws_client1_data.data)); /* Wait for the websocket acknowledge message */
  1711. ck_assert_int_eq(ws_client1_data.closed, 0);
  1712. ck_assert_int_eq(ws_client2_data.closed, 0);
  1713. ck_assert(ws_client2_data.data == NULL);
  1714. ck_assert_uint_eq(ws_client2_data.len, 0);
  1715. ck_assert(ws_client1_data.data != NULL);
  1716. ck_assert_uint_eq(ws_client1_data.len, 3);
  1717. ck_assert(!memcmp(ws_client1_data.data, "ok1", 3));
  1718. free(ws_client1_data.data);
  1719. ws_client1_data.data = NULL;
  1720. ws_client1_data.len = 0;
  1721. /* Now connect a second client */
  1722. #if defined(USE_IPV6)
  1723. ws_client2_conn =
  1724. mg_connect_websocket_client("[::1]",
  1725. ipv6_port,
  1726. 0,
  1727. ebuf,
  1728. sizeof(ebuf),
  1729. "/websocket",
  1730. NULL,
  1731. websocket_client_data_handler,
  1732. websocket_client_close_handler,
  1733. &ws_client2_data);
  1734. #else
  1735. ws_client2_conn =
  1736. mg_connect_websocket_client("127.0.0.1",
  1737. ipv4_port,
  1738. 0,
  1739. ebuf,
  1740. sizeof(ebuf),
  1741. "/websocket",
  1742. NULL,
  1743. websocket_client_data_handler,
  1744. websocket_client_close_handler,
  1745. &ws_client2_data);
  1746. #endif
  1747. ck_assert(ws_client2_conn != NULL);
  1748. wait_not_null(
  1749. &(ws_client2_data.data)); /* Wait for the websocket welcome message */
  1750. ck_assert(ws_client1_data.closed == 0);
  1751. ck_assert(ws_client2_data.closed == 0);
  1752. ck_assert(ws_client1_data.data == NULL);
  1753. ck_assert(ws_client1_data.len == 0);
  1754. ck_assert(ws_client2_data.data != NULL);
  1755. ck_assert(ws_client2_data.len == websocket_welcome_msg_len);
  1756. ck_assert(!memcmp(ws_client2_data.data,
  1757. websocket_welcome_msg,
  1758. websocket_welcome_msg_len));
  1759. free(ws_client2_data.data);
  1760. ws_client2_data.data = NULL;
  1761. ws_client2_data.len = 0;
  1762. mg_websocket_client_write(ws_client1_conn,
  1763. MG_WEBSOCKET_OPCODE_TEXT,
  1764. "data2",
  1765. 5);
  1766. wait_not_null(&(
  1767. ws_client1_data.data)); /* Wait for the websocket acknowledge message */
  1768. ck_assert(ws_client1_data.closed == 0);
  1769. ck_assert(ws_client2_data.closed == 0);
  1770. ck_assert(ws_client2_data.data == NULL);
  1771. ck_assert(ws_client2_data.len == 0);
  1772. ck_assert(ws_client1_data.data != NULL);
  1773. ck_assert(ws_client1_data.len == 4);
  1774. ck_assert(!memcmp(ws_client1_data.data, "ok 2", 4));
  1775. free(ws_client1_data.data);
  1776. ws_client1_data.data = NULL;
  1777. ws_client1_data.len = 0;
  1778. mg_websocket_client_write(ws_client1_conn,
  1779. MG_WEBSOCKET_OPCODE_TEXT,
  1780. "bye",
  1781. 3);
  1782. wait_not_null(
  1783. &(ws_client1_data.data)); /* Wait for the websocket goodbye message */
  1784. ck_assert(ws_client1_data.closed == 0);
  1785. ck_assert(ws_client2_data.closed == 0);
  1786. ck_assert(ws_client2_data.data == NULL);
  1787. ck_assert(ws_client2_data.len == 0);
  1788. ck_assert(ws_client1_data.data != NULL);
  1789. ck_assert(ws_client1_data.len == websocket_goodbye_msg_len);
  1790. ck_assert(!memcmp(ws_client1_data.data,
  1791. websocket_goodbye_msg,
  1792. websocket_goodbye_msg_len));
  1793. free(ws_client1_data.data);
  1794. ws_client1_data.data = NULL;
  1795. ws_client1_data.len = 0;
  1796. ck_assert(ws_client1_data.closed == 0); /* Not closed */
  1797. mg_close_connection(ws_client1_conn);
  1798. test_sleep(3); /* Won't get any message */
  1799. ck_assert(ws_client1_data.closed == 1); /* Closed */
  1800. ck_assert(ws_client2_data.closed == 0);
  1801. ck_assert(ws_client1_data.data == NULL);
  1802. ck_assert(ws_client1_data.len == 0);
  1803. ck_assert(ws_client2_data.data == NULL);
  1804. ck_assert(ws_client2_data.len == 0);
  1805. mg_websocket_client_write(ws_client2_conn,
  1806. MG_WEBSOCKET_OPCODE_TEXT,
  1807. "bye",
  1808. 3);
  1809. wait_not_null(
  1810. &(ws_client2_data.data)); /* Wait for the websocket goodbye message */
  1811. ck_assert(ws_client1_data.closed == 1);
  1812. ck_assert(ws_client2_data.closed == 0);
  1813. ck_assert(ws_client1_data.data == NULL);
  1814. ck_assert(ws_client1_data.len == 0);
  1815. ck_assert(ws_client2_data.data != NULL);
  1816. ck_assert(ws_client2_data.len == websocket_goodbye_msg_len);
  1817. ck_assert(!memcmp(ws_client2_data.data,
  1818. websocket_goodbye_msg,
  1819. websocket_goodbye_msg_len));
  1820. free(ws_client2_data.data);
  1821. ws_client2_data.data = NULL;
  1822. ws_client2_data.len = 0;
  1823. mg_close_connection(ws_client2_conn);
  1824. test_sleep(3); /* Won't get any message */
  1825. ck_assert(ws_client1_data.closed == 1);
  1826. ck_assert(ws_client2_data.closed == 1);
  1827. ck_assert(ws_client1_data.data == NULL);
  1828. ck_assert(ws_client1_data.len == 0);
  1829. ck_assert(ws_client2_data.data == NULL);
  1830. ck_assert(ws_client2_data.len == 0);
  1831. /* Connect client 3 */
  1832. ws_client3_conn =
  1833. mg_connect_websocket_client("localhost",
  1834. #if defined(NO_SSL)
  1835. ipv4_port,
  1836. 0,
  1837. #else
  1838. ipv4s_port,
  1839. 1,
  1840. #endif
  1841. ebuf,
  1842. sizeof(ebuf),
  1843. "/websocket",
  1844. NULL,
  1845. websocket_client_data_handler,
  1846. websocket_client_close_handler,
  1847. &ws_client3_data);
  1848. ck_assert(ws_client3_conn != NULL);
  1849. wait_not_null(
  1850. &(ws_client3_data.data)); /* Wait for the websocket welcome message */
  1851. ck_assert(ws_client1_data.closed == 1);
  1852. ck_assert(ws_client2_data.closed == 1);
  1853. ck_assert(ws_client3_data.closed == 0);
  1854. ck_assert(ws_client1_data.data == NULL);
  1855. ck_assert(ws_client1_data.len == 0);
  1856. ck_assert(ws_client2_data.data == NULL);
  1857. ck_assert(ws_client2_data.len == 0);
  1858. ck_assert(ws_client3_data.data != NULL);
  1859. ck_assert(ws_client3_data.len == websocket_welcome_msg_len);
  1860. ck_assert(!memcmp(ws_client3_data.data,
  1861. websocket_welcome_msg,
  1862. websocket_welcome_msg_len));
  1863. free(ws_client3_data.data);
  1864. ws_client3_data.data = NULL;
  1865. ws_client3_data.len = 0;
  1866. /* Write long data (16 bit size header) */
  1867. mg_websocket_client_write(ws_client3_conn,
  1868. MG_WEBSOCKET_OPCODE_BINARY,
  1869. long_ws_buf,
  1870. long_ws_buf_len_16);
  1871. /* Wait for the response */
  1872. wait_not_null(&(ws_client3_data.data));
  1873. ck_assert_int_eq((int)ws_client3_data.len, (int)long_ws_buf_len_16);
  1874. ck_assert(!memcmp(ws_client3_data.data, long_ws_buf, long_ws_buf_len_16));
  1875. free(ws_client3_data.data);
  1876. ws_client3_data.data = NULL;
  1877. ws_client3_data.len = 0;
  1878. /* Write long data (64 bit size header) */
  1879. mg_websocket_client_write(ws_client3_conn,
  1880. MG_WEBSOCKET_OPCODE_BINARY,
  1881. long_ws_buf,
  1882. long_ws_buf_len_64);
  1883. /* Wait for the response */
  1884. wait_not_null(&(ws_client3_data.data));
  1885. ck_assert_int_eq((int)ws_client3_data.len, (int)long_ws_buf_len_64);
  1886. ck_assert(!memcmp(ws_client3_data.data, long_ws_buf, long_ws_buf_len_64));
  1887. free(ws_client3_data.data);
  1888. ws_client3_data.data = NULL;
  1889. ws_client3_data.len = 0;
  1890. /* Disconnect client 3 */
  1891. ck_assert(ws_client3_data.closed == 0);
  1892. mg_close_connection(ws_client3_conn);
  1893. ck_assert(ws_client3_data.closed == 1);
  1894. /* Connect client 4 */
  1895. ws_client4_conn =
  1896. mg_connect_websocket_client("localhost",
  1897. #if defined(NO_SSL)
  1898. ipv4_port,
  1899. 0,
  1900. #else
  1901. ipv4s_port,
  1902. 1,
  1903. #endif
  1904. ebuf,
  1905. sizeof(ebuf),
  1906. "/websocket",
  1907. NULL,
  1908. websocket_client_data_handler,
  1909. websocket_client_close_handler,
  1910. &ws_client4_data);
  1911. ck_assert(ws_client4_conn != NULL);
  1912. wait_not_null(
  1913. &(ws_client4_data.data)); /* Wait for the websocket welcome message */
  1914. ck_assert(ws_client1_data.closed == 1);
  1915. ck_assert(ws_client2_data.closed == 1);
  1916. ck_assert(ws_client3_data.closed == 1);
  1917. ck_assert(ws_client4_data.closed == 0);
  1918. ck_assert(ws_client4_data.data != NULL);
  1919. ck_assert(ws_client4_data.len == websocket_welcome_msg_len);
  1920. ck_assert(!memcmp(ws_client4_data.data,
  1921. websocket_welcome_msg,
  1922. websocket_welcome_msg_len));
  1923. free(ws_client4_data.data);
  1924. ws_client4_data.data = NULL;
  1925. ws_client4_data.len = 0;
  1926. /* stop the server without closing this connection */
  1927. #endif
  1928. /* Close the server */
  1929. g_ctx = NULL;
  1930. test_mg_stop(ctx, __LINE__);
  1931. mark_point();
  1932. #if defined(USE_WEBSOCKET)
  1933. for (i = 0; i < 100; i++) {
  1934. test_sleep(1);
  1935. if (ws_client3_data.closed != 0) {
  1936. mark_point();
  1937. break;
  1938. }
  1939. }
  1940. ck_assert_int_eq(ws_client4_data.closed, 1);
  1941. /* Free data in ws_client4_conn */
  1942. mg_close_connection(ws_client4_conn);
  1943. #endif
  1944. mark_point();
  1945. }
  1946. END_TEST
  1947. static int g_field_found_return = -999;
  1948. static int
  1949. field_found(const char *key,
  1950. const char *filename,
  1951. char *path,
  1952. size_t pathlen,
  1953. void *user_data)
  1954. {
  1955. ck_assert_ptr_ne(key, NULL);
  1956. ck_assert_ptr_ne(filename, NULL);
  1957. ck_assert_ptr_ne(path, NULL);
  1958. ck_assert_uint_gt(pathlen, 128);
  1959. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1960. ck_assert((g_field_found_return == MG_FORM_FIELD_STORAGE_GET)
  1961. || (g_field_found_return == MG_FORM_FIELD_STORAGE_STORE)
  1962. || (g_field_found_return == MG_FORM_FIELD_STORAGE_SKIP)
  1963. || (g_field_found_return == MG_FORM_FIELD_STORAGE_ABORT));
  1964. ck_assert_str_ne(key, "dontread");
  1965. if (!strcmp(key, "break_field_handler")) {
  1966. return MG_FORM_FIELD_STORAGE_ABORT;
  1967. }
  1968. if (!strcmp(key, "continue_field_handler")) {
  1969. return MG_FORM_FIELD_STORAGE_SKIP;
  1970. }
  1971. if (g_field_found_return == MG_FORM_FIELD_STORAGE_STORE) {
  1972. strncpy(path, key, pathlen - 8);
  1973. strcat(path, ".txt");
  1974. }
  1975. mark_point();
  1976. return g_field_found_return;
  1977. }
  1978. static int g_field_step;
  1979. static int
  1980. field_get(const char *key,
  1981. const char *value_untruncated,
  1982. size_t valuelen,
  1983. void *user_data)
  1984. {
  1985. /* Copy the untruncated value, so string compare functions can be used. */
  1986. /* The check unit test library does not have build in memcmp functions. */
  1987. char *value = (char *)malloc(valuelen + 1);
  1988. ck_assert(value != NULL);
  1989. memcpy(value, value_untruncated, valuelen);
  1990. value[valuelen] = 0;
  1991. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1992. ck_assert_int_ge(g_field_step, 0);
  1993. ++g_field_step;
  1994. switch (g_field_step) {
  1995. case 1:
  1996. ck_assert_str_eq(key, "textin");
  1997. ck_assert_uint_eq(valuelen, 4);
  1998. ck_assert_str_eq(value, "text");
  1999. break;
  2000. case 2:
  2001. ck_assert_str_eq(key, "passwordin");
  2002. ck_assert_uint_eq(valuelen, 0);
  2003. ck_assert_str_eq(value, "");
  2004. break;
  2005. case 3:
  2006. ck_assert_str_eq(key, "radio1");
  2007. ck_assert_uint_eq(valuelen, 4);
  2008. ck_assert_str_eq(value, "val1");
  2009. break;
  2010. case 4:
  2011. ck_assert_str_eq(key, "radio2");
  2012. ck_assert_uint_eq(valuelen, 4);
  2013. ck_assert_str_eq(value, "val1");
  2014. break;
  2015. case 5:
  2016. ck_assert_str_eq(key, "check1");
  2017. ck_assert_uint_eq(valuelen, 4);
  2018. ck_assert_str_eq(value, "val1");
  2019. break;
  2020. case 6:
  2021. ck_assert_str_eq(key, "numberin");
  2022. ck_assert_uint_eq(valuelen, 1);
  2023. ck_assert_str_eq(value, "1");
  2024. break;
  2025. case 7:
  2026. ck_assert_str_eq(key, "datein");
  2027. ck_assert_uint_eq(valuelen, 8);
  2028. ck_assert_str_eq(value, "1.1.2016");
  2029. break;
  2030. case 8:
  2031. ck_assert_str_eq(key, "colorin");
  2032. ck_assert_uint_eq(valuelen, 7);
  2033. ck_assert_str_eq(value, "#80ff00");
  2034. break;
  2035. case 9:
  2036. ck_assert_str_eq(key, "rangein");
  2037. ck_assert_uint_eq(valuelen, 1);
  2038. ck_assert_str_eq(value, "3");
  2039. break;
  2040. case 10:
  2041. ck_assert_str_eq(key, "monthin");
  2042. ck_assert_uint_eq(valuelen, 0);
  2043. ck_assert_str_eq(value, "");
  2044. break;
  2045. case 11:
  2046. ck_assert_str_eq(key, "weekin");
  2047. ck_assert_uint_eq(valuelen, 0);
  2048. ck_assert_str_eq(value, "");
  2049. break;
  2050. case 12:
  2051. ck_assert_str_eq(key, "timein");
  2052. ck_assert_uint_eq(valuelen, 0);
  2053. ck_assert_str_eq(value, "");
  2054. break;
  2055. case 13:
  2056. ck_assert_str_eq(key, "datetimen");
  2057. ck_assert_uint_eq(valuelen, 0);
  2058. ck_assert_str_eq(value, "");
  2059. break;
  2060. case 14:
  2061. ck_assert_str_eq(key, "datetimelocalin");
  2062. ck_assert_uint_eq(valuelen, 0);
  2063. ck_assert_str_eq(value, "");
  2064. break;
  2065. case 15:
  2066. ck_assert_str_eq(key, "emailin");
  2067. ck_assert_uint_eq(valuelen, 0);
  2068. ck_assert_str_eq(value, "");
  2069. break;
  2070. case 16:
  2071. ck_assert_str_eq(key, "searchin");
  2072. ck_assert_uint_eq(valuelen, 0);
  2073. ck_assert_str_eq(value, "");
  2074. break;
  2075. case 17:
  2076. ck_assert_str_eq(key, "telin");
  2077. ck_assert_uint_eq(valuelen, 0);
  2078. ck_assert_str_eq(value, "");
  2079. break;
  2080. case 18:
  2081. ck_assert_str_eq(key, "urlin");
  2082. ck_assert_uint_eq(valuelen, 0);
  2083. ck_assert_str_eq(value, "");
  2084. break;
  2085. case 19:
  2086. ck_assert_str_eq(key, "filein");
  2087. ck_assert_uint_eq(valuelen, 0);
  2088. ck_assert_str_eq(value, "");
  2089. break;
  2090. case 20:
  2091. ck_assert_str_eq(key, "filesin");
  2092. ck_assert_uint_eq(valuelen, 0);
  2093. ck_assert_str_eq(value, "");
  2094. break;
  2095. case 21:
  2096. ck_assert_str_eq(key, "selectin");
  2097. ck_assert_uint_eq(valuelen, 4);
  2098. ck_assert_str_eq(value, "opt1");
  2099. break;
  2100. case 22:
  2101. ck_assert_str_eq(key, "message");
  2102. ck_assert_uint_eq(valuelen, 23);
  2103. ck_assert_str_eq(value, "Text area default text.");
  2104. break;
  2105. default:
  2106. ck_abort_msg("field_get called with g_field_step == %i",
  2107. (int)g_field_step);
  2108. }
  2109. free(value);
  2110. mark_point();
  2111. return 0;
  2112. }
  2113. static const char *myfile_content = "Content of myfile.txt\r\n";
  2114. static const int myfile_content_rep = 500;
  2115. static int myfile_content_len = 23; /* (int)strlen(myfile_content); */
  2116. static int
  2117. field_store(const char *path, long long file_size, void *user_data)
  2118. {
  2119. FILE *f;
  2120. ck_assert_int_eq(myfile_content_len, 23);
  2121. ck_assert_int_eq(myfile_content_len, (int)strlen(myfile_content));
  2122. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  2123. ck_assert_int_ge(g_field_step, 100);
  2124. ++g_field_step;
  2125. switch (g_field_step) {
  2126. case 101:
  2127. ck_assert_str_eq(path, "storeme.txt");
  2128. ck_assert_int_eq(file_size, 9);
  2129. f = fopen(path, "r");
  2130. ck_assert_ptr_ne(f, NULL);
  2131. if (f) {
  2132. char buf[32] = {0};
  2133. int i = (int)fread(buf, 1, sizeof(buf) - 1, f);
  2134. ck_assert_int_eq(i, 9);
  2135. fclose(f);
  2136. ck_assert_str_eq(buf, "storetest");
  2137. }
  2138. break;
  2139. case 102:
  2140. ck_assert_str_eq(path, "file2store.txt");
  2141. ck_assert_int_eq(myfile_content_len, (int)strlen(myfile_content));
  2142. ck_assert_int_eq(file_size, myfile_content_len * myfile_content_rep);
  2143. #ifdef _WIN32
  2144. f = fopen(path, "rb");
  2145. #else
  2146. f = fopen(path, "r");
  2147. #endif
  2148. ck_assert_ptr_ne(f, NULL);
  2149. if (f) {
  2150. char buf[32] = {0};
  2151. int r, i;
  2152. for (r = 0; r < myfile_content_rep; r++) {
  2153. i = (int)fread(buf, 1, (size_t)myfile_content_len, f);
  2154. ck_assert_int_eq(i, myfile_content_len);
  2155. ck_assert_str_eq(buf, myfile_content);
  2156. }
  2157. i = (int)fread(buf, 1, (size_t)myfile_content_len, f);
  2158. ck_assert_int_eq(i, 0);
  2159. fclose(f);
  2160. }
  2161. break;
  2162. default:
  2163. ck_abort_msg("field_get called with g_field_step == %i",
  2164. (int)g_field_step);
  2165. }
  2166. mark_point();
  2167. return 0;
  2168. }
  2169. static int
  2170. FormGet(struct mg_connection *conn, void *cbdata)
  2171. {
  2172. const struct mg_request_info *req_info = mg_get_request_info(conn);
  2173. int ret;
  2174. struct mg_form_data_handler fdh = {field_found, field_get, NULL, NULL};
  2175. (void)cbdata;
  2176. ck_assert(req_info != NULL);
  2177. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  2178. fdh.user_data = (void *)&g_field_found_return;
  2179. /* Call the form handler */
  2180. g_field_step = 0;
  2181. g_field_found_return = MG_FORM_FIELD_STORAGE_GET;
  2182. ret = mg_handle_form_request(conn, &fdh);
  2183. g_field_found_return = -888;
  2184. ck_assert_int_eq(ret, 22);
  2185. ck_assert_int_eq(g_field_step, 22);
  2186. mg_printf(conn, "%i\r\n", ret);
  2187. g_field_step = 1000;
  2188. mark_point();
  2189. return 1;
  2190. }
  2191. static int
  2192. FormStore(struct mg_connection *conn,
  2193. void *cbdata,
  2194. int ret_expected,
  2195. int field_step_expected)
  2196. {
  2197. const struct mg_request_info *req_info = mg_get_request_info(conn);
  2198. int ret;
  2199. struct mg_form_data_handler fdh = {field_found,
  2200. field_get,
  2201. field_store,
  2202. NULL};
  2203. (void)cbdata;
  2204. ck_assert(req_info != NULL);
  2205. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  2206. fdh.user_data = (void *)&g_field_found_return;
  2207. /* Call the form handler */
  2208. g_field_step = 100;
  2209. g_field_found_return = MG_FORM_FIELD_STORAGE_STORE;
  2210. ret = mg_handle_form_request(conn, &fdh);
  2211. ck_assert_int_eq(ret, ret_expected);
  2212. ck_assert_int_eq(g_field_step, field_step_expected);
  2213. mg_printf(conn, "%i\r\n", ret);
  2214. g_field_step = 1000;
  2215. mark_point();
  2216. return 1;
  2217. }
  2218. static int
  2219. FormStore1(struct mg_connection *conn, void *cbdata)
  2220. {
  2221. mark_point();
  2222. return FormStore(conn, cbdata, 3, 101);
  2223. }
  2224. static int
  2225. FormStore2(struct mg_connection *conn, void *cbdata)
  2226. {
  2227. mark_point();
  2228. return FormStore(conn, cbdata, 4, 102);
  2229. }
  2230. static void
  2231. send_chunk_stringl(struct mg_connection *conn,
  2232. const char *chunk,
  2233. unsigned int chunk_len)
  2234. {
  2235. char lenbuf[16];
  2236. size_t lenbuf_len;
  2237. int ret;
  2238. mark_point();
  2239. /* First store the length information in a text buffer. */
  2240. sprintf(lenbuf, "%x\r\n", chunk_len);
  2241. lenbuf_len = strlen(lenbuf);
  2242. /* Then send length information, chunk and terminating \r\n. */
  2243. ret = mg_write(conn, lenbuf, lenbuf_len);
  2244. ck_assert_int_eq(ret, (int)lenbuf_len);
  2245. ret = mg_write(conn, chunk, chunk_len);
  2246. ck_assert_int_eq(ret, (int)chunk_len);
  2247. ret = mg_write(conn, "\r\n", 2);
  2248. ck_assert_int_eq(ret, 2);
  2249. }
  2250. static void
  2251. send_chunk_string(struct mg_connection *conn, const char *chunk)
  2252. {
  2253. mark_point();
  2254. send_chunk_stringl(conn, chunk, (unsigned int)strlen(chunk));
  2255. mark_point();
  2256. }
  2257. START_TEST(test_handle_form)
  2258. {
  2259. struct mg_context *ctx;
  2260. struct mg_connection *client_conn;
  2261. const struct mg_response_info *client_ri;
  2262. const char *OPTIONS[8];
  2263. const char *opt;
  2264. int opt_idx = 0;
  2265. char ebuf[1024];
  2266. const char *multipart_body;
  2267. const char *boundary;
  2268. size_t body_len, body_sent, chunk_len, bound_len;
  2269. int sleep_cnt;
  2270. mark_point();
  2271. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  2272. OPTIONS[opt_idx++] = "listening_ports";
  2273. OPTIONS[opt_idx++] = "8884";
  2274. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  2275. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  2276. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  2277. ctx = test_mg_start(NULL, &g_ctx, OPTIONS, __LINE__);
  2278. ck_assert(ctx != NULL);
  2279. g_ctx = ctx;
  2280. opt = mg_get_option(ctx, "listening_ports");
  2281. ck_assert_str_eq(opt, "8884");
  2282. mg_set_request_handler(ctx, "/handle_form", FormGet, NULL);
  2283. mg_set_request_handler(ctx, "/handle_form_store", FormStore1, NULL);
  2284. mg_set_request_handler(ctx, "/handle_form_store2", FormStore2, NULL);
  2285. test_sleep(1);
  2286. /* Handle form: "GET" */
  2287. client_conn = mg_download("localhost",
  2288. 8884,
  2289. 0,
  2290. ebuf,
  2291. sizeof(ebuf),
  2292. "%s",
  2293. "GET /handle_form"
  2294. "?textin=text&passwordin=&radio1=val1"
  2295. "&radio2=val1&check1=val1&numberin=1"
  2296. "&datein=1.1.2016&colorin=%2380ff00"
  2297. "&rangein=3&monthin=&weekin=&timein="
  2298. "&datetimen=&datetimelocalin=&emailin="
  2299. "&searchin=&telin=&urlin=&filein="
  2300. "&filesin=&selectin=opt1"
  2301. "&message=Text+area+default+text. "
  2302. "HTTP/1.0\r\n"
  2303. "Host: localhost:8884\r\n"
  2304. "Connection: close\r\n\r\n");
  2305. ck_assert(client_conn != NULL);
  2306. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2307. test_sleep(1);
  2308. if (g_field_step == 1000) {
  2309. break;
  2310. }
  2311. }
  2312. client_ri = mg_get_response_info(client_conn);
  2313. ck_assert(client_ri != NULL);
  2314. ck_assert_int_eq(client_ri->status_code, 200);
  2315. mg_close_connection(client_conn);
  2316. /* Handle form: "POST x-www-form-urlencoded" */
  2317. client_conn =
  2318. mg_download("localhost",
  2319. 8884,
  2320. 0,
  2321. ebuf,
  2322. sizeof(ebuf),
  2323. "%s",
  2324. "POST /handle_form HTTP/1.1\r\n"
  2325. "Host: localhost:8884\r\n"
  2326. "Connection: close\r\n"
  2327. "Content-Type: application/x-www-form-urlencoded\r\n"
  2328. "Content-Length: 263\r\n"
  2329. "\r\n"
  2330. "textin=text&passwordin=&radio1=val1&radio2=val1"
  2331. "&check1=val1&numberin=1&datein=1.1.2016"
  2332. "&colorin=%2380ff00&rangein=3&monthin=&weekin="
  2333. "&timein=&datetimen=&datetimelocalin=&emailin="
  2334. "&searchin=&telin=&urlin=&filein=&filesin="
  2335. "&selectin=opt1&message=Text+area+default+text.");
  2336. ck_assert(client_conn != NULL);
  2337. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2338. test_sleep(1);
  2339. if (g_field_step == 1000) {
  2340. break;
  2341. }
  2342. }
  2343. client_ri = mg_get_response_info(client_conn);
  2344. ck_assert(client_ri != NULL);
  2345. ck_assert_int_eq(client_ri->status_code, 200);
  2346. mg_close_connection(client_conn);
  2347. /* Handle form: "POST multipart/form-data" */
  2348. multipart_body =
  2349. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2350. "Content-Disposition: form-data; name=\"textin\"\r\n"
  2351. "\r\n"
  2352. "text\r\n"
  2353. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2354. "Content-Disposition: form-data; name=\"passwordin\"\r\n"
  2355. "\r\n"
  2356. "\r\n"
  2357. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2358. "Content-Disposition: form-data; name=\"radio1\"\r\n"
  2359. "\r\n"
  2360. "val1\r\n"
  2361. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2362. "Content-Disposition: form-data; name=radio2\r\n"
  2363. "\r\n"
  2364. "val1\r\n"
  2365. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2366. "Content-Disposition: form-data; name=\"check1\"\r\n"
  2367. "\r\n"
  2368. "val1\r\n"
  2369. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2370. "Content-Disposition: form-data; name=\"numberin\"\r\n"
  2371. "\r\n"
  2372. "1\r\n"
  2373. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2374. "Content-Disposition: form-data; name=\"datein\"\r\n"
  2375. "\r\n"
  2376. "1.1.2016\r\n"
  2377. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2378. "Content-Disposition: form-data; name=\"colorin\"\r\n"
  2379. "\r\n"
  2380. "#80ff00\r\n"
  2381. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2382. "Content-Disposition: form-data; name=\"rangein\"\r\n"
  2383. "\r\n"
  2384. "3\r\n"
  2385. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2386. "Content-Disposition: form-data; name=\"monthin\"\r\n"
  2387. "\r\n"
  2388. "\r\n"
  2389. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2390. "Content-Disposition: form-data; name=\"weekin\"\r\n"
  2391. "\r\n"
  2392. "\r\n"
  2393. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2394. "Content-Disposition: form-data; name=\"timein\"\r\n"
  2395. "\r\n"
  2396. "\r\n"
  2397. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2398. "Content-Disposition: form-data; name=\"datetimen\"\r\n"
  2399. "\r\n"
  2400. "\r\n"
  2401. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2402. "Content-Disposition: form-data; name=\"datetimelocalin\"\r\n"
  2403. "\r\n"
  2404. "\r\n"
  2405. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2406. "Content-Disposition: form-data; name=\"emailin\"\r\n"
  2407. "\r\n"
  2408. "\r\n"
  2409. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2410. "Content-Disposition: form-data; name=\"searchin\"\r\n"
  2411. "\r\n"
  2412. "\r\n"
  2413. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2414. "Content-Disposition: form-data; name=\"telin\"\r\n"
  2415. "\r\n"
  2416. "\r\n"
  2417. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2418. "Content-Disposition: form-data; name=\"urlin\"\r\n"
  2419. "\r\n"
  2420. "\r\n"
  2421. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2422. "Content-Disposition: form-data; name=\"filein\"; filename=\"\"\r\n"
  2423. "Content-Type: application/octet-stream\r\n"
  2424. "\r\n"
  2425. "\r\n"
  2426. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2427. "Content-Disposition: form-data; name=filesin; filename=\r\n"
  2428. "Content-Type: application/octet-stream\r\n"
  2429. "\r\n"
  2430. "\r\n"
  2431. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2432. "Content-Disposition: form-data; name=\"selectin\"\r\n"
  2433. "\r\n"
  2434. "opt1\r\n"
  2435. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2436. "Content-Disposition: form-data; name=\"message\"\r\n"
  2437. "\r\n"
  2438. "Text area default text.\r\n"
  2439. "--multipart-form-data-boundary--see-RFC-2388--\r\n";
  2440. body_len = strlen(multipart_body);
  2441. ck_assert_uint_eq(body_len, 2368); /* not required */
  2442. client_conn =
  2443. mg_download("localhost",
  2444. 8884,
  2445. 0,
  2446. ebuf,
  2447. sizeof(ebuf),
  2448. "POST /handle_form HTTP/1.1\r\n"
  2449. "Host: localhost:8884\r\n"
  2450. "Connection: close\r\n"
  2451. "Content-Type: multipart/form-data; "
  2452. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2453. "Content-Length: %u\r\n"
  2454. "\r\n%s",
  2455. (unsigned int)body_len,
  2456. multipart_body);
  2457. ck_assert(client_conn != NULL);
  2458. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2459. test_sleep(1);
  2460. if (g_field_step == 1000) {
  2461. break;
  2462. }
  2463. }
  2464. client_ri = mg_get_response_info(client_conn);
  2465. ck_assert(client_ri != NULL);
  2466. ck_assert_int_eq(client_ri->status_code, 200);
  2467. mg_close_connection(client_conn);
  2468. /* Handle form: "POST multipart/form-data" with chunked transfer encoding */
  2469. client_conn =
  2470. mg_download("localhost",
  2471. 8884,
  2472. 0,
  2473. ebuf,
  2474. sizeof(ebuf),
  2475. "%s",
  2476. "POST /handle_form HTTP/1.1\r\n"
  2477. "Host: localhost:8884\r\n"
  2478. "Connection: close\r\n"
  2479. "Content-Type: multipart/form-data; "
  2480. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2481. "Transfer-Encoding: chunked\r\n"
  2482. "\r\n");
  2483. ck_assert(client_conn != NULL);
  2484. body_len = strlen(multipart_body);
  2485. chunk_len = 1;
  2486. body_sent = 0;
  2487. while (body_len > body_sent) {
  2488. if (chunk_len > (body_len - body_sent)) {
  2489. chunk_len = body_len - body_sent;
  2490. }
  2491. ck_assert_int_gt((int)chunk_len, 0);
  2492. mg_printf(client_conn, "%x\r\n", (unsigned int)chunk_len);
  2493. mg_write(client_conn, multipart_body + body_sent, chunk_len);
  2494. mg_printf(client_conn, "\r\n");
  2495. body_sent += chunk_len;
  2496. chunk_len = (chunk_len % 40) + 1;
  2497. }
  2498. mg_printf(client_conn, "0\r\n\r\n");
  2499. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2500. test_sleep(1);
  2501. if (g_field_step == 1000) {
  2502. break;
  2503. }
  2504. }
  2505. client_ri = mg_get_response_info(client_conn);
  2506. ck_assert(client_ri != NULL);
  2507. ck_assert_int_eq(client_ri->status_code, 200);
  2508. mg_close_connection(client_conn);
  2509. /* Handle form: "POST multipart/form-data" with chunked transfer
  2510. * encoding, using a quoted boundary string */
  2511. client_conn = mg_download(
  2512. "localhost",
  2513. 8884,
  2514. 0,
  2515. ebuf,
  2516. sizeof(ebuf),
  2517. "%s",
  2518. "POST /handle_form HTTP/1.1\r\n"
  2519. "Host: localhost:8884\r\n"
  2520. "Connection: close\r\n"
  2521. "Content-Type: multipart/form-data; "
  2522. "boundary=\"multipart-form-data-boundary--see-RFC-2388\"\r\n"
  2523. "Transfer-Encoding: chunked\r\n"
  2524. "\r\n");
  2525. ck_assert(client_conn != NULL);
  2526. body_len = strlen(multipart_body);
  2527. chunk_len = 1;
  2528. body_sent = 0;
  2529. while (body_len > body_sent) {
  2530. if (chunk_len > (body_len - body_sent)) {
  2531. chunk_len = body_len - body_sent;
  2532. }
  2533. ck_assert_int_gt((int)chunk_len, 0);
  2534. mg_printf(client_conn, "%x\r\n", (unsigned int)chunk_len);
  2535. mg_write(client_conn, multipart_body + body_sent, chunk_len);
  2536. mg_printf(client_conn, "\r\n");
  2537. body_sent += chunk_len;
  2538. chunk_len = (chunk_len % 40) + 1;
  2539. }
  2540. mg_printf(client_conn, "0\r\n\r\n");
  2541. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2542. test_sleep(1);
  2543. if (g_field_step == 1000) {
  2544. break;
  2545. }
  2546. }
  2547. client_ri = mg_get_response_info(client_conn);
  2548. ck_assert(client_ri != NULL);
  2549. ck_assert_int_eq(client_ri->status_code, 200);
  2550. mg_close_connection(client_conn);
  2551. /* Now test form_store */
  2552. /* First test with GET */
  2553. client_conn = mg_download("localhost",
  2554. 8884,
  2555. 0,
  2556. ebuf,
  2557. sizeof(ebuf),
  2558. "%s",
  2559. "GET /handle_form_store"
  2560. "?storeme=storetest"
  2561. "&continue_field_handler=ignore"
  2562. "&break_field_handler=abort"
  2563. "&dontread=xyz "
  2564. "HTTP/1.0\r\n"
  2565. "Host: localhost:8884\r\n"
  2566. "Connection: close\r\n\r\n");
  2567. ck_assert(client_conn != NULL);
  2568. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2569. test_sleep(1);
  2570. if (g_field_step == 1000) {
  2571. break;
  2572. }
  2573. }
  2574. client_ri = mg_get_response_info(client_conn);
  2575. ck_assert(client_ri != NULL);
  2576. ck_assert_int_eq(client_ri->status_code, 200);
  2577. mg_close_connection(client_conn);
  2578. /* Handle form: "POST x-www-form-urlencoded", chunked, store */
  2579. client_conn =
  2580. mg_download("localhost",
  2581. 8884,
  2582. 0,
  2583. ebuf,
  2584. sizeof(ebuf),
  2585. "%s",
  2586. "POST /handle_form_store HTTP/1.0\r\n"
  2587. "Host: localhost:8884\r\n"
  2588. "Connection: close\r\n"
  2589. "Content-Type: application/x-www-form-urlencoded\r\n"
  2590. "Transfer-Encoding: chunked\r\n"
  2591. "\r\n");
  2592. ck_assert(client_conn != NULL);
  2593. send_chunk_string(client_conn, "storeme=store");
  2594. send_chunk_string(client_conn, "test&");
  2595. send_chunk_string(client_conn, "continue_field_handler=ignore");
  2596. send_chunk_string(client_conn, "&br");
  2597. test_sleep(1);
  2598. send_chunk_string(client_conn, "eak_field_handler=abort&");
  2599. send_chunk_string(client_conn, "dontread=xyz");
  2600. mg_printf(client_conn, "0\r\n\r\n");
  2601. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2602. test_sleep(1);
  2603. if (g_field_step == 1000) {
  2604. break;
  2605. }
  2606. }
  2607. client_ri = mg_get_response_info(client_conn);
  2608. ck_assert(client_ri != NULL);
  2609. ck_assert_int_eq(client_ri->status_code, 200);
  2610. mg_close_connection(client_conn);
  2611. /* Handle form: "POST multipart/form-data", chunked, store */
  2612. client_conn =
  2613. mg_download("localhost",
  2614. 8884,
  2615. 0,
  2616. ebuf,
  2617. sizeof(ebuf),
  2618. "%s",
  2619. "POST /handle_form_store HTTP/1.0\r\n"
  2620. "Host: localhost:8884\r\n"
  2621. "Connection: close\r\n"
  2622. "Content-Type: multipart/form-data; "
  2623. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2624. "Transfer-Encoding: chunked\r\n"
  2625. "\r\n");
  2626. ck_assert(client_conn != NULL);
  2627. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2628. send_chunk_string(client_conn, "--see-RFC-2388\r\n");
  2629. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2630. send_chunk_string(client_conn, "name=\"storeme\"\r\n");
  2631. send_chunk_string(client_conn, "\r\n");
  2632. send_chunk_string(client_conn, "storetest\r\n");
  2633. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2634. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2635. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2636. send_chunk_string(client_conn, "name=\"continue_field_handler\"\r\n");
  2637. send_chunk_string(client_conn, "\r\n");
  2638. send_chunk_string(client_conn, "ignore\r\n");
  2639. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2640. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2641. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2642. send_chunk_string(client_conn, "name=\"break_field_handler\"\r\n");
  2643. send_chunk_string(client_conn, "\r\n");
  2644. send_chunk_string(client_conn, "abort\r\n");
  2645. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2646. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2647. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2648. send_chunk_string(client_conn, "name=\"dontread\"\r\n");
  2649. send_chunk_string(client_conn, "\r\n");
  2650. send_chunk_string(client_conn, "xyz\r\n");
  2651. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2652. send_chunk_string(client_conn, "--see-RFC-2388--\r\n");
  2653. mg_printf(client_conn, "0\r\n\r\n");
  2654. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2655. test_sleep(1);
  2656. if (g_field_step == 1000) {
  2657. break;
  2658. }
  2659. }
  2660. client_ri = mg_get_response_info(client_conn);
  2661. ck_assert(client_ri != NULL);
  2662. ck_assert_int_eq(client_ri->status_code, 200);
  2663. mg_close_connection(client_conn);
  2664. /* Handle form: "POST multipart/form-data", chunked, store, with files */
  2665. client_conn =
  2666. mg_download("localhost",
  2667. 8884,
  2668. 0,
  2669. ebuf,
  2670. sizeof(ebuf),
  2671. "%s",
  2672. "POST /handle_form_store2 HTTP/1.0\r\n"
  2673. "Host: localhost:8884\r\n"
  2674. "Connection: close\r\n"
  2675. "Content-Type: multipart/form-data; "
  2676. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2677. "Transfer-Encoding: chunked\r\n"
  2678. "\r\n");
  2679. ck_assert(client_conn != NULL);
  2680. boundary = "--multipart-form-data-boundary--see-RFC-2388\r\n";
  2681. send_chunk_string(client_conn, boundary);
  2682. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2683. send_chunk_string(client_conn, "name=\"storeme\"\r\n");
  2684. send_chunk_string(client_conn, "\r\n");
  2685. send_chunk_string(client_conn, "storetest\r\n");
  2686. send_chunk_string(client_conn, boundary);
  2687. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2688. send_chunk_string(client_conn, "name=\"continue_field_handler\";");
  2689. send_chunk_string(client_conn, "filename=\"file_ignored.txt\"\r\n");
  2690. send_chunk_string(client_conn, "Content-Type: ");
  2691. send_chunk_string(client_conn, "application/octet-stream\r\n");
  2692. send_chunk_string(client_conn, "X-Ignored-Header: xyz\r\n");
  2693. send_chunk_string(client_conn, "\r\n");
  2694. /* send some kilobyte of data */
  2695. /* sending megabytes to localhost does not always work in CI test
  2696. * environments (depending on the network stack) */
  2697. body_sent = 0;
  2698. bound_len = strlen(boundary);
  2699. do {
  2700. send_chunk_string(client_conn, "ignore\r\n");
  2701. body_sent += 8;
  2702. /* send some strings that are almost boundaries */
  2703. for (chunk_len = 1; chunk_len < bound_len; chunk_len++) {
  2704. /* chunks from 1 byte to strlen(boundary)-1 */
  2705. send_chunk_stringl(client_conn, boundary, (unsigned int)chunk_len);
  2706. body_sent += chunk_len;
  2707. }
  2708. } while (body_sent < 8 * 1024);
  2709. send_chunk_string(client_conn, "\r\n");
  2710. send_chunk_string(client_conn, boundary);
  2711. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2712. send_chunk_string(client_conn, "name=\"file2store\";");
  2713. send_chunk_string(client_conn, "filename=\"myfile.txt\"\r\n");
  2714. send_chunk_string(client_conn, "Content-Type: ");
  2715. send_chunk_string(client_conn, "application/octet-stream\r\n");
  2716. send_chunk_string(client_conn, "X-Ignored-Header: xyz\r\n");
  2717. send_chunk_string(client_conn, "\r\n");
  2718. for (body_sent = 0; (int)body_sent < (int)myfile_content_rep; body_sent++) {
  2719. send_chunk_string(client_conn, myfile_content);
  2720. }
  2721. send_chunk_string(client_conn, "\r\n");
  2722. send_chunk_string(client_conn, boundary);
  2723. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2724. send_chunk_string(client_conn, "name=\"break_field_handler\"\r\n");
  2725. send_chunk_string(client_conn, "\r\n");
  2726. send_chunk_string(client_conn, "abort\r\n");
  2727. send_chunk_string(client_conn, boundary);
  2728. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2729. send_chunk_string(client_conn, "name=\"dontread\"\r\n");
  2730. send_chunk_string(client_conn, "\r\n");
  2731. send_chunk_string(client_conn, "xyz\r\n");
  2732. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2733. send_chunk_string(client_conn, "--see-RFC-2388--\r\n");
  2734. mg_printf(client_conn, "0\r\n\r\n");
  2735. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2736. test_sleep(1);
  2737. if (g_field_step == 1000) {
  2738. break;
  2739. }
  2740. }
  2741. client_ri = mg_get_response_info(client_conn);
  2742. ck_assert(client_ri != NULL);
  2743. ck_assert_int_eq(client_ri->status_code, 200);
  2744. mg_close_connection(client_conn);
  2745. /* Close the server */
  2746. g_ctx = NULL;
  2747. test_mg_stop(ctx, __LINE__);
  2748. mark_point();
  2749. }
  2750. END_TEST
  2751. START_TEST(test_http_auth)
  2752. {
  2753. #if !defined(NO_FILES)
  2754. const char *OPTIONS[] = {
  2755. "document_root",
  2756. ".",
  2757. "listening_ports",
  2758. "8080",
  2759. #if !defined(NO_CACHING)
  2760. "static_file_max_age",
  2761. "0",
  2762. #endif
  2763. "put_delete_auth_file",
  2764. "put_delete_auth_file.csv",
  2765. NULL,
  2766. };
  2767. struct mg_context *ctx;
  2768. struct mg_connection *client_conn;
  2769. char client_err[256], nonce[256];
  2770. const struct mg_response_info *client_ri;
  2771. int client_res;
  2772. FILE *f;
  2773. const char *passwd_file = ".htpasswd";
  2774. const char *test_file = "test_http_auth.test_file.txt";
  2775. const char *test_content = "test_http_auth test_file content";
  2776. const char *domain;
  2777. const char *doc_root;
  2778. const char *auth_request;
  2779. const char *str;
  2780. size_t len;
  2781. int i;
  2782. char HA1[256], HA2[256];
  2783. char HA1_md5_buf[33], HA2_md5_buf[33], HA_md5_buf[33];
  2784. char *HA1_md5_ret, *HA2_md5_ret, *HA_md5_ret;
  2785. const char *nc = "00000001";
  2786. const char *cnonce = "6789ABCD";
  2787. mark_point();
  2788. /* Start with default options */
  2789. ctx = test_mg_start(NULL, NULL, OPTIONS, __LINE__);
  2790. ck_assert(ctx != NULL);
  2791. domain = mg_get_option(ctx, "authentication_domain");
  2792. ck_assert(domain != NULL);
  2793. len = strlen(domain);
  2794. ck_assert_uint_gt(len, 0);
  2795. ck_assert_uint_lt(len, 64);
  2796. doc_root = mg_get_option(ctx, "document_root");
  2797. ck_assert_str_eq(doc_root, ".");
  2798. /* Create a default file in the document root */
  2799. f = fopen(test_file, "w");
  2800. if (f) {
  2801. fprintf(f, "%s", test_content);
  2802. fclose(f);
  2803. } else {
  2804. ck_abort_msg("Cannot create file %s", test_file);
  2805. }
  2806. (void)remove(passwd_file);
  2807. (void)remove("put_delete_auth_file.csv");
  2808. client_res = mg_modify_passwords_file("put_delete_auth_file.csv",
  2809. domain,
  2810. "admin",
  2811. "adminpass");
  2812. ck_assert_int_eq(client_res, 1);
  2813. /* Read file before a .htpasswd file has been created */
  2814. memset(client_err, 0, sizeof(client_err));
  2815. client_conn =
  2816. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2817. ck_assert_str_eq(client_err, "");
  2818. ck_assert(client_conn != NULL);
  2819. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2820. client_res =
  2821. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2822. ck_assert_int_ge(client_res, 0);
  2823. ck_assert_str_eq(client_err, "");
  2824. client_ri = mg_get_response_info(client_conn);
  2825. ck_assert(client_ri != NULL);
  2826. ck_assert_int_eq(client_ri->status_code, 200);
  2827. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2828. ck_assert_int_gt(client_res, 0);
  2829. ck_assert_int_le(client_res, sizeof(client_err));
  2830. ck_assert_str_eq(client_err, test_content);
  2831. mg_close_connection(client_conn);
  2832. test_sleep(1);
  2833. /* Create a .htpasswd file */
  2834. client_res = mg_modify_passwords_file(passwd_file, domain, "user", "pass");
  2835. ck_assert_int_eq(client_res, 1);
  2836. client_res = mg_modify_passwords_file(NULL, domain, "user", "pass");
  2837. ck_assert_int_eq(client_res, 0); /* Filename is required */
  2838. test_sleep(1);
  2839. /* Repeat test after .htpasswd is created */
  2840. memset(client_err, 0, sizeof(client_err));
  2841. client_conn =
  2842. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2843. ck_assert_str_eq(client_err, "");
  2844. ck_assert(client_conn != NULL);
  2845. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2846. client_res =
  2847. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2848. ck_assert_int_ge(client_res, 0);
  2849. ck_assert_str_eq(client_err, "");
  2850. client_ri = mg_get_response_info(client_conn);
  2851. ck_assert(client_ri != NULL);
  2852. ck_assert_int_eq(client_ri->status_code, 401);
  2853. auth_request = NULL;
  2854. for (i = 0; i < client_ri->num_headers; i++) {
  2855. if (!mg_strcasecmp(client_ri->http_headers[i].name,
  2856. "WWW-Authenticate")) {
  2857. ck_assert_ptr_eq(auth_request, NULL);
  2858. auth_request = client_ri->http_headers[i].value;
  2859. ck_assert_ptr_ne(auth_request, NULL);
  2860. }
  2861. }
  2862. ck_assert_ptr_ne(auth_request, NULL);
  2863. str = "Digest qop=\"auth\", realm=\"";
  2864. len = strlen(str);
  2865. ck_assert(!mg_strncasecmp(auth_request, str, len));
  2866. ck_assert(!strncmp(auth_request + len, domain, strlen(domain)));
  2867. len += strlen(domain);
  2868. str = "\", nonce=\"";
  2869. ck_assert(!strncmp(auth_request + len, str, strlen(str)));
  2870. len += strlen(str);
  2871. str = strchr(auth_request + len, '\"');
  2872. ck_assert_ptr_ne(str, NULL);
  2873. ck_assert_ptr_ne(str, auth_request + len);
  2874. /* nonce is from including (auth_request + len) to excluding (str) */
  2875. ck_assert_int_gt((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len), 0);
  2876. ck_assert_int_lt((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len),
  2877. (ptrdiff_t)sizeof(nonce));
  2878. memset(nonce, 0, sizeof(nonce));
  2879. memcpy(nonce,
  2880. auth_request + len,
  2881. (size_t)((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len)));
  2882. memset(HA1, 0, sizeof(HA1));
  2883. memset(HA2, 0, sizeof(HA2));
  2884. memset(HA1_md5_buf, 0, sizeof(HA1_md5_buf));
  2885. memset(HA2_md5_buf, 0, sizeof(HA2_md5_buf));
  2886. memset(HA_md5_buf, 0, sizeof(HA_md5_buf));
  2887. sprintf(HA1, "%s:%s:%s", "user", domain, "pass");
  2888. sprintf(HA2, "%s:/%s", "GET", test_file);
  2889. HA1_md5_ret = mg_md5(HA1_md5_buf, HA1, NULL);
  2890. HA2_md5_ret = mg_md5(HA2_md5_buf, HA2, NULL);
  2891. ck_assert_ptr_eq(HA1_md5_ret, HA1_md5_buf);
  2892. ck_assert_ptr_eq(HA2_md5_ret, HA2_md5_buf);
  2893. HA_md5_ret = mg_md5(HA_md5_buf, "user", ":", domain, ":", "pass", NULL);
  2894. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2895. ck_assert_str_eq(HA1_md5_ret, HA_md5_buf);
  2896. HA_md5_ret = mg_md5(HA_md5_buf, "GET", ":", "/", test_file, NULL);
  2897. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2898. ck_assert_str_eq(HA2_md5_ret, HA_md5_buf);
  2899. HA_md5_ret = mg_md5(HA_md5_buf,
  2900. HA1_md5_buf,
  2901. ":",
  2902. nonce,
  2903. ":",
  2904. nc,
  2905. ":",
  2906. cnonce,
  2907. ":",
  2908. "auth",
  2909. ":",
  2910. HA2_md5_buf,
  2911. NULL);
  2912. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2913. mg_close_connection(client_conn);
  2914. /* Retry with authorization */
  2915. memset(client_err, 0, sizeof(client_err));
  2916. client_conn =
  2917. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2918. ck_assert_str_eq(client_err, "");
  2919. ck_assert(client_conn != NULL);
  2920. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n", test_file);
  2921. mg_printf(client_conn,
  2922. "Authorization: Digest "
  2923. "username=\"%s\", "
  2924. "realm=\"%s\", "
  2925. "nonce=\"%s\", "
  2926. "uri=\"/%s\", "
  2927. "qop=auth, "
  2928. "nc=%s, "
  2929. "cnonce=\"%s\", "
  2930. "response=\"%s\"\r\n\r\n",
  2931. "user",
  2932. domain,
  2933. nonce,
  2934. test_file,
  2935. nc,
  2936. cnonce,
  2937. HA_md5_buf);
  2938. client_res =
  2939. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2940. ck_assert_int_ge(client_res, 0);
  2941. ck_assert_str_eq(client_err, "");
  2942. client_ri = mg_get_response_info(client_conn);
  2943. ck_assert(client_ri != NULL);
  2944. ck_assert_int_eq(client_ri->status_code, 200);
  2945. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2946. ck_assert_int_gt(client_res, 0);
  2947. ck_assert_int_le(client_res, sizeof(client_err));
  2948. ck_assert_str_eq(client_err, test_content);
  2949. mg_close_connection(client_conn);
  2950. test_sleep(1);
  2951. /* Retry DELETE with authorization of a user not authorized for DELETE */
  2952. memset(client_err, 0, sizeof(client_err));
  2953. client_conn =
  2954. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2955. ck_assert_str_eq(client_err, "");
  2956. ck_assert(client_conn != NULL);
  2957. mg_printf(client_conn, "DELETE /%s HTTP/1.0\r\n", test_file);
  2958. mg_printf(client_conn,
  2959. "Authorization: Digest "
  2960. "username=\"%s\", "
  2961. "realm=\"%s\", "
  2962. "nonce=\"%s\", "
  2963. "uri=\"/%s\", "
  2964. "qop=auth, "
  2965. "nc=%s, "
  2966. "cnonce=\"%s\", "
  2967. "response=\"%s\"\r\n\r\n",
  2968. "user",
  2969. domain,
  2970. nonce,
  2971. test_file,
  2972. nc,
  2973. cnonce,
  2974. HA_md5_buf);
  2975. client_res =
  2976. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2977. ck_assert_int_ge(client_res, 0);
  2978. ck_assert_str_eq(client_err, "");
  2979. client_ri = mg_get_response_info(client_conn);
  2980. ck_assert(client_ri != NULL);
  2981. ck_assert_int_eq(client_ri->status_code, 401);
  2982. mg_close_connection(client_conn);
  2983. test_sleep(1);
  2984. /* Remove the user from the .htpasswd file again */
  2985. client_res = mg_modify_passwords_file(passwd_file, domain, "user", NULL);
  2986. ck_assert_int_eq(client_res, 1);
  2987. test_sleep(1);
  2988. /* Try to access the file again. Expected: 401 error */
  2989. memset(client_err, 0, sizeof(client_err));
  2990. client_conn =
  2991. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2992. ck_assert_str_eq(client_err, "");
  2993. ck_assert(client_conn != NULL);
  2994. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2995. client_res =
  2996. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2997. ck_assert_int_ge(client_res, 0);
  2998. ck_assert_str_eq(client_err, "");
  2999. client_ri = mg_get_response_info(client_conn);
  3000. ck_assert(client_ri != NULL);
  3001. ck_assert_int_eq(client_ri->status_code, 401);
  3002. mg_close_connection(client_conn);
  3003. test_sleep(1);
  3004. /* Now remove the password file */
  3005. (void)remove(passwd_file);
  3006. test_sleep(1);
  3007. /* Access to the file must work like before */
  3008. memset(client_err, 0, sizeof(client_err));
  3009. client_conn =
  3010. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  3011. ck_assert_str_eq(client_err, "");
  3012. ck_assert(client_conn != NULL);
  3013. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  3014. client_res =
  3015. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  3016. ck_assert_int_ge(client_res, 0);
  3017. ck_assert_str_eq(client_err, "");
  3018. client_ri = mg_get_response_info(client_conn);
  3019. ck_assert(client_ri != NULL);
  3020. ck_assert_int_eq(client_ri->status_code, 200);
  3021. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  3022. ck_assert_int_gt(client_res, 0);
  3023. ck_assert_int_le(client_res, sizeof(client_err));
  3024. ck_assert_str_eq(client_err, test_content);
  3025. mg_close_connection(client_conn);
  3026. test_sleep(1);
  3027. /* Stop the server and clean up */
  3028. test_mg_stop(ctx, __LINE__);
  3029. (void)remove(test_file);
  3030. (void)remove(passwd_file);
  3031. (void)remove("put_delete_auth_file.csv");
  3032. #endif
  3033. mark_point();
  3034. }
  3035. END_TEST
  3036. START_TEST(test_keep_alive)
  3037. {
  3038. struct mg_context *ctx;
  3039. const char *OPTIONS[] =
  3040. { "listening_ports",
  3041. "8081",
  3042. "request_timeout_ms",
  3043. "10000",
  3044. "enable_keep_alive",
  3045. "yes",
  3046. #if !defined(NO_FILES)
  3047. "document_root",
  3048. ".",
  3049. "enable_directory_listing",
  3050. "no",
  3051. #endif
  3052. NULL };
  3053. struct mg_connection *client_conn;
  3054. char client_err[256];
  3055. const struct mg_response_info *client_ri;
  3056. int client_res, i;
  3057. const char *connection_header;
  3058. mark_point();
  3059. ctx = test_mg_start(NULL, NULL, OPTIONS, __LINE__);
  3060. ck_assert(ctx != NULL);
  3061. /* HTTP 1.1 GET request */
  3062. memset(client_err, 0, sizeof(client_err));
  3063. client_conn =
  3064. mg_connect_client("127.0.0.1", 8081, 0, client_err, sizeof(client_err));
  3065. ck_assert_str_eq(client_err, "");
  3066. ck_assert(client_conn != NULL);
  3067. mg_printf(client_conn,
  3068. "GET / HTTP/1.1\r\nHost: "
  3069. "localhost:8081\r\nConnection: keep-alive\r\n\r\n");
  3070. client_res =
  3071. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  3072. ck_assert_int_ge(client_res, 0);
  3073. ck_assert_str_eq(client_err, "");
  3074. client_ri = mg_get_response_info(client_conn);
  3075. ck_assert(client_ri != NULL);
  3076. #if defined(NO_FILES)
  3077. ck_assert_int_eq(client_ri->status_code, 404);
  3078. #else
  3079. ck_assert_int_eq(client_ri->status_code, 403);
  3080. #endif
  3081. connection_header = 0;
  3082. for (i = 0; i < client_ri->num_headers; i++) {
  3083. if (!mg_strcasecmp(client_ri->http_headers[i].name, "Connection")) {
  3084. ck_assert_ptr_eq(connection_header, NULL);
  3085. connection_header = client_ri->http_headers[i].value;
  3086. ck_assert_ptr_ne(connection_header, NULL);
  3087. }
  3088. }
  3089. /* Error replies will close the connection, even if keep-alive is set. */
  3090. ck_assert_ptr_ne(connection_header, NULL);
  3091. ck_assert_str_eq(connection_header, "close");
  3092. mg_close_connection(client_conn);
  3093. test_sleep(1);
  3094. /* TODO: request a file and keep alive
  3095. * (will only work if NO_FILES is not set). */
  3096. /* Stop the server and clean up */
  3097. test_mg_stop(ctx, __LINE__);
  3098. mark_point();
  3099. }
  3100. END_TEST
  3101. START_TEST(test_error_handling)
  3102. {
  3103. struct mg_context *ctx;
  3104. FILE *f;
  3105. char bad_thread_num[32] = "badnumber";
  3106. struct mg_callbacks callbacks;
  3107. char errmsg[256];
  3108. struct mg_connection *client_conn;
  3109. char client_err[256];
  3110. const struct mg_response_info *client_ri;
  3111. int client_res, i;
  3112. const char *OPTIONS[32];
  3113. int opt_cnt = 0;
  3114. mark_point();
  3115. #if !defined(NO_FILES)
  3116. OPTIONS[opt_cnt++] = "document_root";
  3117. OPTIONS[opt_cnt++] = ".";
  3118. #endif
  3119. OPTIONS[opt_cnt++] = "error_pages";
  3120. OPTIONS[opt_cnt++] = "./";
  3121. OPTIONS[opt_cnt++] = "listening_ports";
  3122. OPTIONS[opt_cnt++] = "8080";
  3123. OPTIONS[opt_cnt++] = "num_threads";
  3124. OPTIONS[opt_cnt++] = bad_thread_num;
  3125. OPTIONS[opt_cnt++] = "unknown_option";
  3126. OPTIONS[opt_cnt++] = "unknown_option_value";
  3127. OPTIONS[opt_cnt] = NULL;
  3128. memset(&callbacks, 0, sizeof(callbacks));
  3129. callbacks.log_message = log_msg_func;
  3130. /* test with unknown option */
  3131. memset(errmsg, 0, sizeof(errmsg));
  3132. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, 0);
  3133. /* Details of errmsg may vary, but it may not be empty */
  3134. ck_assert_str_ne(errmsg, "");
  3135. ck_assert(ctx == NULL);
  3136. ck_assert_str_eq(errmsg, "Invalid option: unknown_option");
  3137. /* Remove invalid option */
  3138. for (i = 0; OPTIONS[i]; i++) {
  3139. if (strstr(OPTIONS[i], "unknown_option")) {
  3140. OPTIONS[i] = 0;
  3141. }
  3142. }
  3143. /* Test with bad num_thread option */
  3144. memset(errmsg, 0, sizeof(errmsg));
  3145. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, 0);
  3146. /* Details of errmsg may vary, but it may not be empty */
  3147. ck_assert_str_ne(errmsg, "");
  3148. ck_assert(ctx == NULL);
  3149. ck_assert_str_eq(errmsg, "Invalid number of worker threads");
  3150. /* Set to a number - but use a number above the limit */
  3151. #if defined(MAX_WORKER_THREADS)
  3152. sprintf(bad_thread_num, "%u", MAX_WORKER_THREADS + 1);
  3153. #else
  3154. sprintf(bad_thread_num, "%lu", 1000000000lu);
  3155. #endif
  3156. /* Test with bad num_thread option */
  3157. memset(errmsg, 0, sizeof(errmsg));
  3158. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, 0);
  3159. /* Details of errmsg may vary, but it may not be empty */
  3160. ck_assert_str_ne(errmsg, "");
  3161. ck_assert(ctx == NULL);
  3162. ck_assert_str_eq(errmsg, "Too many worker threads");
  3163. /* HTTP 1.0 GET request - server is not running */
  3164. memset(client_err, 0, sizeof(client_err));
  3165. client_conn =
  3166. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  3167. ck_assert(client_conn == NULL);
  3168. /* Error message detail may vary - it may not be empty and should contain
  3169. * some information "connect" failed */
  3170. ck_assert_str_ne(client_err, "");
  3171. ck_assert(strstr(client_err, "connect"));
  3172. /* This time start the server with a valid configuration */
  3173. sprintf(bad_thread_num, "%i", 1);
  3174. memset(errmsg, 0, sizeof(errmsg));
  3175. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, __LINE__);
  3176. ck_assert_str_eq(errmsg, "");
  3177. ck_assert(ctx != NULL);
  3178. /* Server is running now */
  3179. test_sleep(1);
  3180. /* Remove error files (in case they exist) */
  3181. (void)remove("error.htm");
  3182. (void)remove("error4xx.htm");
  3183. (void)remove("error404.htm");
  3184. /* Ask for something not existing - should get default 404 */
  3185. memset(client_err, 0, sizeof(client_err));
  3186. client_conn =
  3187. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  3188. ck_assert_str_eq(client_err, "");
  3189. ck_assert(client_conn != NULL);
  3190. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  3191. client_res =
  3192. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  3193. ck_assert_int_ge(client_res, 0);
  3194. ck_assert_str_eq(client_err, "");
  3195. client_ri = mg_get_response_info(client_conn);
  3196. ck_assert(client_ri != NULL);
  3197. ck_assert_int_eq(client_ri->status_code, 404);
  3198. mg_close_connection(client_conn);
  3199. test_sleep(1);
  3200. /* Try DELETE when put_delete_auth_file is not configured */
  3201. memset(client_err, 0, sizeof(client_err));
  3202. client_conn =
  3203. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  3204. ck_assert_str_eq(client_err, "");
  3205. ck_assert(client_conn != NULL);
  3206. mg_printf(client_conn, "DELETE /something/not/existing HTTP/1.0\r\n\r\n");
  3207. client_res =
  3208. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  3209. ck_assert_int_ge(client_res, 0);
  3210. ck_assert_str_eq(client_err, "");
  3211. client_ri = mg_get_response_info(client_conn);
  3212. ck_assert(client_ri != NULL);
  3213. ck_assert_int_eq(client_ri->status_code, 405);
  3214. mg_close_connection(client_conn);
  3215. test_sleep(1);
  3216. /* Create an error.htm file */
  3217. f = fopen("error.htm", "wt");
  3218. ck_assert(f != NULL);
  3219. (void)fprintf(f, "err-all");
  3220. (void)fclose(f);
  3221. /* Ask for something not existing - should get error.htm */
  3222. memset(client_err, 0, sizeof(client_err));
  3223. client_conn =
  3224. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  3225. ck_assert_str_eq(client_err, "");
  3226. ck_assert(client_conn != NULL);
  3227. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  3228. client_res =
  3229. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  3230. ck_assert_int_ge(client_res, 0);
  3231. ck_assert_str_eq(client_err, "");
  3232. client_ri = mg_get_response_info(client_conn);
  3233. ck_assert(client_ri != NULL);
  3234. ck_assert_int_eq(client_ri->status_code, 200);
  3235. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  3236. mg_close_connection(client_conn);
  3237. ck_assert_int_eq(client_res, 7);
  3238. client_err[8] = 0;
  3239. ck_assert_str_eq(client_err, "err-all");
  3240. test_sleep(1);
  3241. /* Create an error4xx.htm file */
  3242. f = fopen("error4xx.htm", "wt");
  3243. ck_assert(f != NULL);
  3244. (void)fprintf(f, "err-4xx");
  3245. (void)fclose(f);
  3246. /* Ask for something not existing - should get error4xx.htm */
  3247. memset(client_err, 0, sizeof(client_err));
  3248. client_conn =
  3249. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  3250. ck_assert_str_eq(client_err, "");
  3251. ck_assert(client_conn != NULL);
  3252. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  3253. client_res =
  3254. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  3255. ck_assert_int_ge(client_res, 0);
  3256. ck_assert_str_eq(client_err, "");
  3257. client_ri = mg_get_response_info(client_conn);
  3258. ck_assert(client_ri != NULL);
  3259. ck_assert_int_eq(client_ri->status_code, 200);
  3260. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  3261. mg_close_connection(client_conn);
  3262. ck_assert_int_eq(client_res, 7);
  3263. client_err[8] = 0;
  3264. ck_assert_str_eq(client_err, "err-4xx");
  3265. test_sleep(1);
  3266. /* Create an error404.htm file */
  3267. f = fopen("error404.htm", "wt");
  3268. ck_assert(f != NULL);
  3269. (void)fprintf(f, "err-404");
  3270. (void)fclose(f);
  3271. /* Ask for something not existing - should get error404.htm */
  3272. memset(client_err, 0, sizeof(client_err));
  3273. client_conn =
  3274. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  3275. ck_assert_str_eq(client_err, "");
  3276. ck_assert(client_conn != NULL);
  3277. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  3278. client_res =
  3279. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  3280. ck_assert_int_ge(client_res, 0);
  3281. ck_assert_str_eq(client_err, "");
  3282. client_ri = mg_get_response_info(client_conn);
  3283. ck_assert(client_ri != NULL);
  3284. ck_assert_int_eq(client_ri->status_code, 200);
  3285. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  3286. mg_close_connection(client_conn);
  3287. ck_assert_int_eq(client_res, 7);
  3288. client_err[8] = 0;
  3289. ck_assert_str_eq(client_err, "err-404");
  3290. test_sleep(1);
  3291. /* Ask in a malformed way - should get error4xx.htm */
  3292. memset(client_err, 0, sizeof(client_err));
  3293. client_conn =
  3294. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  3295. ck_assert_str_eq(client_err, "");
  3296. ck_assert(client_conn != NULL);
  3297. mg_printf(client_conn, "Gimme some file!\r\n\r\n");
  3298. client_res =
  3299. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  3300. ck_assert_int_ge(client_res, 0);
  3301. ck_assert_str_eq(client_err, "");
  3302. client_ri = mg_get_response_info(client_conn);
  3303. ck_assert(client_ri != NULL);
  3304. ck_assert_int_eq(client_ri->status_code, 200);
  3305. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  3306. mg_close_connection(client_conn);
  3307. ck_assert_int_eq(client_res, 7);
  3308. client_err[8] = 0;
  3309. ck_assert_str_eq(client_err, "err-4xx");
  3310. test_sleep(1);
  3311. /* Remove all error files created by this test */
  3312. (void)remove("error.htm");
  3313. (void)remove("error4xx.htm");
  3314. (void)remove("error404.htm");
  3315. /* Stop the server */
  3316. test_mg_stop(ctx, __LINE__);
  3317. /* HTTP 1.1 GET request - must not work, since server is already stopped */
  3318. memset(client_err, 0, sizeof(client_err));
  3319. client_conn =
  3320. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  3321. ck_assert(client_conn == NULL);
  3322. ck_assert_str_ne(client_err, "");
  3323. test_sleep(1);
  3324. mark_point();
  3325. }
  3326. END_TEST
  3327. START_TEST(test_error_log_file)
  3328. {
  3329. /* Server var */
  3330. struct mg_context *ctx;
  3331. const char *OPTIONS[32];
  3332. int opt_cnt = 0;
  3333. /* Client var */
  3334. struct mg_connection *client;
  3335. char client_err_buf[256];
  3336. char client_data_buf[256];
  3337. const struct mg_response_info *client_ri;
  3338. /* File content check var */
  3339. FILE *f;
  3340. char buf[1024];
  3341. int len, ok;
  3342. mark_point();
  3343. /* Set options and start server */
  3344. OPTIONS[opt_cnt++] = "listening_ports";
  3345. OPTIONS[opt_cnt++] = "8080";
  3346. OPTIONS[opt_cnt++] = "error_log_file";
  3347. OPTIONS[opt_cnt++] = "error.log";
  3348. OPTIONS[opt_cnt++] = "access_log_file";
  3349. OPTIONS[opt_cnt++] = "access.log";
  3350. #if !defined(NO_FILES)
  3351. OPTIONS[opt_cnt++] = "document_root";
  3352. OPTIONS[opt_cnt++] = ".";
  3353. #endif
  3354. OPTIONS[opt_cnt] = NULL;
  3355. ctx = test_mg_start(NULL, 0, OPTIONS, __LINE__);
  3356. ck_assert(ctx != NULL);
  3357. /* Remove log files (they may exist from previous incomplete runs of
  3358. * this test) */
  3359. (void)remove("error.log");
  3360. (void)remove("access.log");
  3361. /* connect client */
  3362. memset(client_err_buf, 0, sizeof(client_err_buf));
  3363. memset(client_data_buf, 0, sizeof(client_data_buf));
  3364. client = mg_download("127.0.0.1",
  3365. 8080,
  3366. 0,
  3367. client_err_buf,
  3368. sizeof(client_err_buf),
  3369. "GET /not_existing_file.ext HTTP/1.0\r\n\r\n");
  3370. ck_assert(ctx != NULL);
  3371. ck_assert_str_eq(client_err_buf, "");
  3372. client_ri = mg_get_response_info(client);
  3373. /* Check status - should be 404 Not Found */
  3374. ck_assert(client_ri != NULL);
  3375. ck_assert_int_eq(client_ri->status_code, 404);
  3376. /* Get body data (could exist, but does not have to) */
  3377. len = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3378. ck_assert_int_ge(len, 0);
  3379. /* Close the client connection */
  3380. mg_close_connection(client);
  3381. /* Stop the server */
  3382. test_mg_stop(ctx, __LINE__);
  3383. /* Check access.log */
  3384. memset(buf, 0, sizeof(buf));
  3385. f = fopen("access.log", "r");
  3386. ck_assert_msg(f != NULL, "Cannot open access log file");
  3387. ok = (NULL != fgets(buf, sizeof(buf) - 1, f));
  3388. (void)fclose(f);
  3389. ck_assert_msg(ok, "Cannot read access log file");
  3390. len = (int)strlen(buf);
  3391. ck_assert_int_gt(len, 0);
  3392. ok = (NULL != strstr(buf, "not_existing_file.ext"));
  3393. ck_assert_msg(ok, "Did not find uri in access log file");
  3394. ok = (NULL != strstr(buf, "404"));
  3395. ck_assert_msg(ok, "Did not find HTTP status code in access log file");
  3396. /* Check error.log */
  3397. memset(buf, 0, sizeof(buf));
  3398. f = fopen("error.log", "r");
  3399. if (f) {
  3400. (void)fgets(buf, sizeof(buf) - 1, f);
  3401. fclose(f);
  3402. }
  3403. ck_assert_msg(f == NULL,
  3404. "Should not create error log file on 404, but got [%s]",
  3405. buf);
  3406. /* Remove log files */
  3407. (void)remove("error.log");
  3408. (void)remove("access.log");
  3409. /* Start server with bad options */
  3410. ck_assert_str_eq(OPTIONS[0], "listening_ports");
  3411. OPTIONS[1] = "bad !"; /* no r or s in string */
  3412. ctx = test_mg_start(NULL, 0, OPTIONS, 0);
  3413. ck_assert_msg(
  3414. ctx == NULL,
  3415. "Should not be able to start server with bad port configuration");
  3416. /* Check access.log */
  3417. memset(buf, 0, sizeof(buf));
  3418. f = fopen("access.log", "r");
  3419. if (f) {
  3420. (void)fgets(buf, sizeof(buf) - 1, f);
  3421. fclose(f);
  3422. }
  3423. ck_assert_msg(
  3424. f == NULL,
  3425. "Should not create access log file if start fails, but got [%s]",
  3426. buf);
  3427. /* Check error.log */
  3428. memset(buf, 0, sizeof(buf));
  3429. f = fopen("error.log", "r");
  3430. ck_assert_msg(f != NULL, "Cannot open access log file");
  3431. ok = (NULL != fgets(buf, sizeof(buf) - 1, f));
  3432. (void)fclose(f);
  3433. ck_assert_msg(ok, "Cannot read access log file");
  3434. len = (int)strlen(buf);
  3435. ck_assert_int_gt(len, 0);
  3436. ok = (NULL != strstr(buf, "port"));
  3437. ck_assert_msg(ok, "Did not find port as error reason in error log file");
  3438. /* Remove log files */
  3439. (void)remove("error.log");
  3440. (void)remove("access.log");
  3441. mark_point();
  3442. }
  3443. END_TEST
  3444. static int
  3445. test_throttle_begin_request(struct mg_connection *conn)
  3446. {
  3447. const struct mg_request_info *ri;
  3448. long unsigned len = 1024 * 10;
  3449. const char *block = "0123456789";
  3450. unsigned long i, blocklen;
  3451. ck_assert(conn != NULL);
  3452. ri = mg_get_request_info(conn);
  3453. ck_assert(ri != NULL);
  3454. ck_assert_str_eq(ri->request_method, "GET");
  3455. ck_assert_str_eq(ri->request_uri, "/throttle");
  3456. ck_assert_str_eq(ri->local_uri, "/throttle");
  3457. ck_assert_str_eq(ri->http_version, "1.0");
  3458. ck_assert_str_eq(ri->query_string, "q");
  3459. ck_assert_str_eq(ri->remote_addr, "127.0.0.1");
  3460. mg_printf(conn,
  3461. "HTTP/1.1 200 OK\r\n"
  3462. "Content-Length: %lu\r\n"
  3463. "Connection: close\r\n\r\n",
  3464. len);
  3465. blocklen = (unsigned long)strlen(block);
  3466. for (i = 0; i < len; i += blocklen) {
  3467. mg_write(conn, block, blocklen);
  3468. }
  3469. mark_point();
  3470. return 987; /* Not a valid HTTP response code,
  3471. * but it should be written to the log and passed to
  3472. * end_request. */
  3473. }
  3474. static void
  3475. test_throttle_end_request(const struct mg_connection *conn,
  3476. int reply_status_code)
  3477. {
  3478. const struct mg_request_info *ri;
  3479. ck_assert(conn != NULL);
  3480. ri = mg_get_request_info(conn);
  3481. ck_assert(ri != NULL);
  3482. ck_assert_str_eq(ri->request_method, "GET");
  3483. ck_assert_str_eq(ri->request_uri, "/throttle");
  3484. ck_assert_str_eq(ri->local_uri, "/throttle");
  3485. ck_assert_str_eq(ri->http_version, "1.0");
  3486. ck_assert_str_eq(ri->query_string, "q");
  3487. ck_assert_str_eq(ri->remote_addr, "127.0.0.1");
  3488. ck_assert_int_eq(reply_status_code, 987);
  3489. }
  3490. START_TEST(test_throttle)
  3491. {
  3492. /* Server var */
  3493. struct mg_context *ctx;
  3494. struct mg_callbacks callbacks;
  3495. const char *OPTIONS[32];
  3496. int opt_cnt = 0;
  3497. /* Client var */
  3498. struct mg_connection *client;
  3499. char client_err_buf[256];
  3500. char client_data_buf[256];
  3501. const struct mg_response_info *client_ri;
  3502. /* timing test */
  3503. int r, data_read;
  3504. time_t t0, t1;
  3505. double dt;
  3506. mark_point();
  3507. /* Set options and start server */
  3508. #if !defined(NO_FILES)
  3509. OPTIONS[opt_cnt++] = "document_root";
  3510. OPTIONS[opt_cnt++] = ".";
  3511. #endif
  3512. OPTIONS[opt_cnt++] = "listening_ports";
  3513. OPTIONS[opt_cnt++] = "8080";
  3514. OPTIONS[opt_cnt++] = "throttle";
  3515. OPTIONS[opt_cnt++] = "*=1k";
  3516. OPTIONS[opt_cnt] = NULL;
  3517. memset(&callbacks, 0, sizeof(callbacks));
  3518. callbacks.begin_request = test_throttle_begin_request;
  3519. callbacks.end_request = test_throttle_end_request;
  3520. ctx = test_mg_start(&callbacks, 0, OPTIONS, __LINE__);
  3521. ck_assert(ctx != NULL);
  3522. /* connect client */
  3523. memset(client_err_buf, 0, sizeof(client_err_buf));
  3524. memset(client_data_buf, 0, sizeof(client_data_buf));
  3525. strcpy(client_err_buf, "reset-content");
  3526. client = mg_download("127.0.0.1",
  3527. 8080,
  3528. 0,
  3529. client_err_buf,
  3530. sizeof(client_err_buf),
  3531. "GET /throttle?q HTTP/1.0\r\n\r\n");
  3532. ck_assert(ctx != NULL);
  3533. ck_assert_str_eq(client_err_buf, "");
  3534. client_ri = mg_get_response_info(client);
  3535. ck_assert(client_ri != NULL);
  3536. ck_assert_int_eq(client_ri->status_code, 200);
  3537. ck_assert_int_eq(client_ri->content_length, 1024 * 10);
  3538. data_read = 0;
  3539. t0 = time(NULL);
  3540. while (data_read < client_ri->content_length) {
  3541. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3542. ck_assert_int_ge(r, 0);
  3543. data_read += r;
  3544. }
  3545. t1 = time(NULL);
  3546. dt = difftime(t1, t0) * 1000.0; /* Elapsed time in ms - in most systems
  3547. * only with second resolution */
  3548. /* Time estimation: Data size is 10 kB, with 1 kB/s speed limit.
  3549. * The first block (1st kB) is transferred immediately, the second
  3550. * block (2nd kB) one second later, the third block (3rd kB) two
  3551. * seconds later, .. the last block (10th kB) nine seconds later.
  3552. * The resolution of time measurement using the "time" C library
  3553. * function is 1 second, so we should add +/- one second tolerance.
  3554. * Thus, download of 10 kB with 1 kB/s should not be faster than
  3555. * 8 seconds. */
  3556. /* Check if there are at least 8 seconds */
  3557. ck_assert_int_ge((int)dt, 8 * 1000);
  3558. /* Nothing left to read */
  3559. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3560. ck_assert_int_eq(r, 0);
  3561. /* Close the client connection */
  3562. mg_close_connection(client);
  3563. /* Stop the server */
  3564. test_mg_stop(ctx, __LINE__);
  3565. mark_point();
  3566. }
  3567. END_TEST
  3568. START_TEST(test_init_library)
  3569. {
  3570. unsigned f_avail, f_ret;
  3571. mark_point();
  3572. f_avail = mg_check_feature(0xFF);
  3573. f_ret = mg_init_library(f_avail);
  3574. ck_assert_uint_eq(f_ret, f_avail);
  3575. }
  3576. END_TEST
  3577. #define LARGE_FILE_SIZE (1024 * 1024 * 10)
  3578. static int
  3579. test_large_file_begin_request(struct mg_connection *conn)
  3580. {
  3581. const struct mg_request_info *ri;
  3582. long unsigned len = LARGE_FILE_SIZE;
  3583. const char *block = "0123456789";
  3584. uint64_t i;
  3585. size_t blocklen;
  3586. ck_assert(conn != NULL);
  3587. ri = mg_get_request_info(conn);
  3588. ck_assert(ri != NULL);
  3589. ck_assert_str_eq(ri->request_method, "GET");
  3590. ck_assert_str_eq(ri->http_version, "1.1");
  3591. ck_assert_str_eq(ri->remote_addr, "127.0.0.1");
  3592. ck_assert_ptr_eq(ri->query_string, NULL);
  3593. ck_assert_ptr_ne(ri->local_uri, NULL);
  3594. mg_printf(conn,
  3595. "HTTP/1.1 200 OK\r\n"
  3596. "Content-Length: %lu\r\n"
  3597. "Connection: close\r\n\r\n",
  3598. len);
  3599. blocklen = strlen(block);
  3600. for (i = 0; i < len; i += blocklen) {
  3601. mg_write(conn, block, blocklen);
  3602. }
  3603. mark_point();
  3604. return 200;
  3605. }
  3606. START_TEST(test_large_file)
  3607. {
  3608. /* Server var */
  3609. struct mg_context *ctx;
  3610. struct mg_callbacks callbacks;
  3611. const char *OPTIONS[32];
  3612. int opt_cnt = 0;
  3613. #if !defined(NO_SSL)
  3614. const char *ssl_cert = locate_ssl_cert();
  3615. #endif
  3616. char errmsg[256] = {0};
  3617. /* Client var */
  3618. struct mg_connection *client;
  3619. char client_err_buf[256];
  3620. char client_data_buf[256];
  3621. const struct mg_response_info *client_ri;
  3622. int64_t data_read;
  3623. int r;
  3624. int retry, retry_ok_cnt, retry_fail_cnt;
  3625. mark_point();
  3626. /* Set options and start server */
  3627. #if !defined(NO_FILES)
  3628. OPTIONS[opt_cnt++] = "document_root";
  3629. OPTIONS[opt_cnt++] = ".";
  3630. #endif
  3631. #if defined(NO_SSL)
  3632. OPTIONS[opt_cnt++] = "listening_ports";
  3633. OPTIONS[opt_cnt++] = "8080";
  3634. #else
  3635. OPTIONS[opt_cnt++] = "listening_ports";
  3636. OPTIONS[opt_cnt++] = "8443s";
  3637. OPTIONS[opt_cnt++] = "ssl_certificate";
  3638. OPTIONS[opt_cnt++] = ssl_cert;
  3639. #if defined(__MACH__) && defined(__APPLE__)
  3640. /* The Apple builds on Travis CI seem to have problems with TLS1.x
  3641. * Allow SSLv3 and TLS */
  3642. OPTIONS[opt_cnt++] = "ssl_protocol_version";
  3643. OPTIONS[opt_cnt++] = "2";
  3644. #else
  3645. /* The Linux builds on Travis CI work fine with TLS1.2 */
  3646. OPTIONS[opt_cnt++] = "ssl_protocol_version";
  3647. OPTIONS[opt_cnt++] = "4";
  3648. #endif
  3649. ck_assert(ssl_cert != NULL);
  3650. #endif
  3651. OPTIONS[opt_cnt] = NULL;
  3652. memset(&callbacks, 0, sizeof(callbacks));
  3653. callbacks.begin_request = test_large_file_begin_request;
  3654. callbacks.log_message = log_msg_func;
  3655. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, __LINE__);
  3656. ck_assert_str_eq(errmsg, "");
  3657. ck_assert(ctx != NULL);
  3658. /* Try downloading several times */
  3659. retry_ok_cnt = 0;
  3660. retry_fail_cnt = 0;
  3661. for (retry = 0; retry < 3; retry++) {
  3662. int fail = 0;
  3663. /* connect client */
  3664. memset(client_err_buf, 0, sizeof(client_err_buf));
  3665. memset(client_data_buf, 0, sizeof(client_data_buf));
  3666. client =
  3667. mg_download("127.0.0.1",
  3668. #if defined(NO_SSL)
  3669. 8080,
  3670. 0,
  3671. #else
  3672. 8443,
  3673. 1,
  3674. #endif
  3675. client_err_buf,
  3676. sizeof(client_err_buf),
  3677. "GET /large.file HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
  3678. ck_assert(client != NULL);
  3679. ck_assert_str_eq(client_err_buf, "");
  3680. client_ri = mg_get_response_info(client);
  3681. ck_assert(client_ri != NULL);
  3682. ck_assert_int_eq(client_ri->status_code, 200);
  3683. ck_assert_int_eq(client_ri->content_length, LARGE_FILE_SIZE);
  3684. data_read = 0;
  3685. while (data_read < client_ri->content_length) {
  3686. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3687. if (r < 0) {
  3688. fail = 1;
  3689. break;
  3690. };
  3691. data_read += r;
  3692. }
  3693. /* Nothing left to read */
  3694. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3695. if (fail) {
  3696. ck_assert_int_eq(r, -1);
  3697. retry_fail_cnt++;
  3698. } else {
  3699. ck_assert_int_eq(r, 0);
  3700. retry_ok_cnt++;
  3701. }
  3702. /* Close the client connection */
  3703. mg_close_connection(client);
  3704. }
  3705. #if defined(_WIN32)
  3706. // TODO: Check this problem on AppVeyor
  3707. // ck_assert_int_le(retry_fail_cnt, 2);
  3708. // ck_assert_int_ge(retry_ok_cnt, 1);
  3709. #else
  3710. ck_assert_int_eq(retry_fail_cnt, 0);
  3711. ck_assert_int_eq(retry_ok_cnt, 3);
  3712. #endif
  3713. /* Stop the server */
  3714. test_mg_stop(ctx, __LINE__);
  3715. mark_point();
  3716. }
  3717. END_TEST
  3718. static int test_mg_store_body_con_len = 20000;
  3719. static int
  3720. test_mg_store_body_put_delete_handler(struct mg_connection *conn, void *ignored)
  3721. {
  3722. char path[4096] = {0};
  3723. const struct mg_request_info *info = mg_get_request_info(conn);
  3724. int64_t rc;
  3725. (void)ignored;
  3726. mark_point();
  3727. sprintf(path, "./%s", info->local_uri);
  3728. rc = mg_store_body(conn, path);
  3729. ck_assert_int_eq(test_mg_store_body_con_len, rc);
  3730. if (rc < 0) {
  3731. mg_printf(conn,
  3732. "HTTP/1.1 500 Internal Server Error\r\n"
  3733. "Content-Type:text/plain;charset=UTF-8\r\n"
  3734. "Connection:close\r\n\r\n"
  3735. "%s (ret: %ld)\n",
  3736. path,
  3737. (long)rc);
  3738. mg_close_connection(conn);
  3739. /* Debug output for tests */
  3740. printf("mg_store_body(%s) failed (ret: %ld)\n", path, (long)rc);
  3741. return 500;
  3742. }
  3743. mg_printf(conn,
  3744. "HTTP/1.1 200 OK\r\n"
  3745. "Content-Type:text/plain;charset=UTF-8\r\n"
  3746. "Connection:close\r\n\r\n"
  3747. "%s OK (%ld bytes saved)\n",
  3748. path,
  3749. (long)rc);
  3750. mg_close_connection(conn);
  3751. /* Debug output for tests */
  3752. printf("mg_store_body(%s) OK (%ld bytes)\n", path, (long)rc);
  3753. mark_point();
  3754. return 200;
  3755. }
  3756. static int
  3757. test_mg_store_body_begin_request_callback(struct mg_connection *conn)
  3758. {
  3759. const struct mg_request_info *info = mg_get_request_info(conn);
  3760. mark_point();
  3761. /* Debug output for tests */
  3762. printf("test_mg_store_body_begin_request_callback called (%s)\n",
  3763. info->request_method);
  3764. if ((strcmp(info->request_method, "PUT") == 0)
  3765. || (strcmp(info->request_method, "DELETE") == 0)) {
  3766. return test_mg_store_body_put_delete_handler(conn, NULL);
  3767. }
  3768. mark_point();
  3769. return 0;
  3770. }
  3771. START_TEST(test_mg_store_body)
  3772. {
  3773. /* Client data */
  3774. char client_err_buf[256];
  3775. char client_data_buf[1024];
  3776. struct mg_connection *client;
  3777. const struct mg_response_info *client_ri;
  3778. int r;
  3779. char check_data[256];
  3780. char *check_ptr;
  3781. char errmsg[256] = {0};
  3782. /* Server context handle */
  3783. struct mg_context *ctx;
  3784. struct mg_callbacks callbacks;
  3785. const char *options[] = {
  3786. #if !defined(NO_FILES)
  3787. "document_root",
  3788. ".",
  3789. #endif
  3790. #if !defined(NO_CACHING)
  3791. "static_file_max_age",
  3792. "0",
  3793. #endif
  3794. "listening_ports",
  3795. "127.0.0.1:8082",
  3796. "num_threads",
  3797. "1",
  3798. NULL
  3799. };
  3800. mark_point();
  3801. memset(&callbacks, 0, sizeof(callbacks));
  3802. callbacks.begin_request = test_mg_store_body_begin_request_callback;
  3803. callbacks.log_message = log_msg_func;
  3804. /* Initialize the library */
  3805. mg_init_library(0);
  3806. /* Start the server */
  3807. ctx = mg_start(&callbacks, (void *)errmsg, options);
  3808. ck_assert_str_eq(errmsg, "");
  3809. ck_assert(ctx != NULL);
  3810. /* Run the server for 15 seconds */
  3811. test_sleep(15);
  3812. /* Call a test client */
  3813. client = mg_connect_client(
  3814. "127.0.0.1", 8082, 0, client_err_buf, sizeof(client_err_buf));
  3815. ck_assert_str_eq(client_err_buf, "");
  3816. ck_assert(client != NULL);
  3817. mg_printf(client,
  3818. "PUT /%s HTTP/1.0\r\nContent-Length: %i\r\n\r\n",
  3819. "test_file_name.txt",
  3820. test_mg_store_body_con_len);
  3821. r = 0;
  3822. while (r < test_mg_store_body_con_len) {
  3823. int l = mg_write(client, "1234567890", 10);
  3824. ck_assert_int_eq(l, 10);
  3825. r += 10;
  3826. }
  3827. r = mg_get_response(client, client_err_buf, sizeof(client_err_buf), 10000);
  3828. ck_assert_int_ge(r, 0);
  3829. ck_assert_str_eq(client_err_buf, "");
  3830. client_ri = mg_get_response_info(client);
  3831. ck_assert(client_ri != NULL);
  3832. /* Response must be 200 OK */
  3833. ck_assert_int_eq(client_ri->status_code, 200);
  3834. /* Read PUT response */
  3835. r = mg_read(client, client_data_buf, sizeof(client_data_buf) - 1);
  3836. ck_assert_int_gt(r, 0);
  3837. client_data_buf[r] = 0;
  3838. sprintf(check_data, "(%i bytes saved)", test_mg_store_body_con_len);
  3839. check_ptr = strstr(client_data_buf, check_data);
  3840. ck_assert_ptr_ne(check_ptr, NULL);
  3841. mg_close_connection(client);
  3842. /* Run the server for 5 seconds */
  3843. test_sleep(5);
  3844. /* Stop the server */
  3845. test_mg_stop(ctx, __LINE__);
  3846. /* Un-initialize the library */
  3847. mg_exit_library();
  3848. mark_point();
  3849. }
  3850. END_TEST
  3851. #if defined(MG_USE_OPEN_FILE) && !defined(NO_FILES)
  3852. #define FILE_IN_MEM_SIZE (1024 * 100)
  3853. static char *file_in_mem_data;
  3854. static const char *
  3855. test_file_in_memory_open_file(const struct mg_connection *conn,
  3856. const char *file_path,
  3857. size_t *file_size)
  3858. {
  3859. (void)conn;
  3860. if (strcmp(file_path, "./file_in_mem") == 0) {
  3861. /* File is in memory */
  3862. *file_size = FILE_IN_MEM_SIZE;
  3863. return file_in_mem_data;
  3864. } else {
  3865. /* File is not in memory */
  3866. return NULL;
  3867. }
  3868. }
  3869. START_TEST(test_file_in_memory)
  3870. {
  3871. /* Server var */
  3872. struct mg_context *ctx;
  3873. struct mg_callbacks callbacks;
  3874. const char *OPTIONS[32];
  3875. int opt_cnt = 0;
  3876. #if !defined(NO_SSL)
  3877. const char *ssl_cert = locate_ssl_cert();
  3878. #endif
  3879. /* Client var */
  3880. struct mg_connection *client;
  3881. char client_err_buf[256];
  3882. char client_data_buf[256];
  3883. const struct mg_request_info *client_ri;
  3884. int64_t data_read;
  3885. int r, i;
  3886. /* Prepare test data */
  3887. file_in_mem_data = (char *)malloc(FILE_IN_MEM_SIZE);
  3888. ck_assert_ptr_ne(file_in_mem_data, NULL);
  3889. for (r = 0; r < FILE_IN_MEM_SIZE; r++) {
  3890. file_in_mem_data[r] = (char)(r);
  3891. }
  3892. /* Set options and start server */
  3893. OPTIONS[opt_cnt++] = "document_root";
  3894. OPTIONS[opt_cnt++] = ".";
  3895. #if defined(NO_SSL)
  3896. OPTIONS[opt_cnt++] = "listening_ports";
  3897. OPTIONS[opt_cnt++] = "8080";
  3898. #else
  3899. OPTIONS[opt_cnt++] = "listening_ports";
  3900. OPTIONS[opt_cnt++] = "8443s";
  3901. OPTIONS[opt_cnt++] = "ssl_certificate";
  3902. OPTIONS[opt_cnt++] = ssl_cert;
  3903. ck_assert(ssl_cert != NULL);
  3904. #endif
  3905. OPTIONS[opt_cnt] = NULL;
  3906. memset(&callbacks, 0, sizeof(callbacks));
  3907. callbacks.open_file = test_file_in_memory_open_file;
  3908. ctx = test_mg_start(&callbacks, 0, OPTIONS, __LINE__);
  3909. ck_assert(ctx != NULL);
  3910. /* connect client */
  3911. memset(client_err_buf, 0, sizeof(client_err_buf));
  3912. memset(client_data_buf, 0, sizeof(client_data_buf));
  3913. client =
  3914. mg_download("127.0.0.1",
  3915. #if defined(NO_SSL)
  3916. 8080,
  3917. 0,
  3918. #else
  3919. 8443,
  3920. 1,
  3921. #endif
  3922. client_err_buf,
  3923. sizeof(client_err_buf),
  3924. "GET /file_in_mem HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
  3925. ck_assert(client != NULL);
  3926. ck_assert_str_eq(client_err_buf, "");
  3927. client_ri = mg_get_response_info(client);
  3928. ck_assert(client_ri != NULL);
  3929. ck_assert_int_eq(client_ri->status_code, 200);
  3930. ck_assert_int_eq(client_ri->content_length, FILE_IN_MEM_SIZE);
  3931. data_read = 0;
  3932. while (data_read < client_ri->content_length) {
  3933. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3934. if (r > 0) {
  3935. for (i = 0; i < r; i++) {
  3936. ck_assert_int_eq((int)client_data_buf[i],
  3937. (int)file_in_mem_data[data_read + i]);
  3938. }
  3939. data_read += r;
  3940. }
  3941. }
  3942. /* Nothing left to read */
  3943. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3944. ck_assert_int_eq(r, 0);
  3945. /* Close the client connection */
  3946. mg_close_connection(client);
  3947. /* Stop the server */
  3948. test_mg_stop(ctx, __LINE__);
  3949. /* Free test data */
  3950. free(file_in_mem_data);
  3951. file_in_mem_data = NULL;
  3952. }
  3953. END_TEST
  3954. #else /* defined(MG_USE_OPEN_FILE) */
  3955. START_TEST(test_file_in_memory)
  3956. {
  3957. mark_point();
  3958. }
  3959. END_TEST
  3960. #endif
  3961. static void
  3962. minimal_http_https_client_impl(const char *server,
  3963. uint16_t port,
  3964. int use_ssl,
  3965. const char *uri,
  3966. const char *expected)
  3967. {
  3968. /* Client var */
  3969. struct mg_connection *client;
  3970. char client_err_buf[256];
  3971. char client_data_buf[4096];
  3972. const struct mg_response_info *client_ri;
  3973. int64_t data_read;
  3974. int r;
  3975. mark_point();
  3976. client = mg_connect_client(
  3977. server, port, use_ssl, client_err_buf, sizeof(client_err_buf));
  3978. if ((client == NULL) || (0 != strcmp(client_err_buf, ""))) {
  3979. ck_abort_msg("%s connection to server [%s] port [%u] failed: [%s]",
  3980. use_ssl ? "HTTPS" : "HTTP",
  3981. server,
  3982. port,
  3983. client_err_buf);
  3984. }
  3985. mg_printf(client, "GET %s HTTP/1.0\r\n\r\n", uri);
  3986. r = mg_get_response(client, client_err_buf, sizeof(client_err_buf), 10000);
  3987. if ((r < 0) || (0 != strcmp(client_err_buf, ""))) {
  3988. ck_abort_msg(
  3989. "%s connection to server [%s] port [%u] did not respond: [%s]",
  3990. use_ssl ? "HTTPS" : "HTTP",
  3991. server,
  3992. port,
  3993. client_err_buf);
  3994. }
  3995. client_ri = mg_get_response_info(client);
  3996. ck_assert(client_ri != NULL);
  3997. /* Check for status code 200 OK or 30? moved */
  3998. if ((client_ri->status_code != 200)
  3999. && (client_ri->status_code / 10 != 30)) {
  4000. ck_abort_msg("Request to %s://%s:%u/%s: Status %u",
  4001. use_ssl ? "HTTPS" : "HTTP",
  4002. server,
  4003. port,
  4004. uri,
  4005. client_ri->status_code);
  4006. }
  4007. data_read = 0;
  4008. while (data_read < client_ri->content_length) {
  4009. r = mg_read(client,
  4010. client_data_buf + data_read,
  4011. sizeof(client_data_buf) - (size_t)data_read);
  4012. if (r > 0) {
  4013. data_read += r;
  4014. ck_assert_int_lt(data_read, sizeof(client_data_buf));
  4015. }
  4016. }
  4017. /* Nothing left to read */
  4018. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  4019. ck_assert_int_eq(r, 0);
  4020. mark_point();
  4021. if (expected) {
  4022. ck_assert_str_eq(client_data_buf, expected);
  4023. }
  4024. mark_point();
  4025. mg_close_connection(client);
  4026. mark_point();
  4027. }
  4028. static void
  4029. minimal_http_client_check(const char *server,
  4030. uint16_t port,
  4031. const char *uri,
  4032. const char *expected)
  4033. {
  4034. minimal_http_https_client_impl(server, port, 0, uri, expected);
  4035. }
  4036. #if !defined(NO_SSL)
  4037. static void
  4038. minimal_https_client_check(const char *server,
  4039. uint16_t port,
  4040. const char *uri,
  4041. const char *expected)
  4042. {
  4043. minimal_http_https_client_impl(server, port, 1, uri, expected);
  4044. }
  4045. #endif
  4046. START_TEST(test_minimal_client)
  4047. {
  4048. const char *external_server_ip;
  4049. mark_point();
  4050. external_server_ip = get_external_server_ip();
  4051. mark_point();
  4052. /* Initialize the library */
  4053. mg_init_library(0);
  4054. mark_point();
  4055. /* Call a test client */
  4056. minimal_http_client_check(external_server_ip,
  4057. 80,
  4058. "/civetweb/civetweb/",
  4059. NULL /* no check */);
  4060. mark_point();
  4061. /* Un-initialize the library */
  4062. mg_exit_library();
  4063. mark_point();
  4064. }
  4065. END_TEST
  4066. START_TEST(test_minimal_tls_client)
  4067. {
  4068. const char *external_server_ip;
  4069. mark_point();
  4070. external_server_ip = get_external_server_ip();
  4071. (void)external_server_ip; /* unused in some cases */
  4072. mark_point();
  4073. #if !defined(NO_SSL) /* dont run https test if SSL is not enabled */
  4074. #if (!defined(__MACH__) || defined(LOCAL_TEST)) && !defined(OPENSSL_API_1_1)
  4075. /* dont run on Travis OSX worker with OpenSSL 1.0 */
  4076. /* Initialize the library */
  4077. mg_init_library(2);
  4078. mark_point();
  4079. /* Call a test client */
  4080. minimal_https_client_check(external_server_ip,
  4081. 443,
  4082. "/civetweb/civetweb/",
  4083. NULL /* no check */);
  4084. mark_point();
  4085. /* Un-initialize the library */
  4086. mg_exit_library();
  4087. #endif
  4088. #endif
  4089. mark_point();
  4090. }
  4091. END_TEST
  4092. static int
  4093. minimal_test_request_handler(struct mg_connection *conn, void *cbdata)
  4094. {
  4095. const char *msg = (const char *)cbdata;
  4096. unsigned long len = (unsigned long)strlen(msg) + 1;
  4097. const struct mg_request_info *ri = mg_get_request_info(conn);
  4098. ck_assert(conn != NULL);
  4099. ck_assert(ri != NULL);
  4100. ck_assert(len > 0);
  4101. ck_assert_str_eq(ri->request_method, "GET");
  4102. ck_assert(ri->request_uri[0] == '/');
  4103. ck_assert(ri->local_uri[0] == '/');
  4104. ck_assert(ri->http_version[0] == '1');
  4105. ck_assert(ri->http_version[1] == '.');
  4106. ck_assert(ri->http_version[3] == 0);
  4107. ck_assert(ri->num_headers >= 0);
  4108. if (ri->query_string != NULL) {
  4109. msg = ri->query_string;
  4110. len = (unsigned long)strlen(msg) + 1;
  4111. }
  4112. mg_printf(conn,
  4113. "HTTP/1.1 200 OK\r\n"
  4114. "Content-Length: %lu\r\n"
  4115. "Content-Type: text/plain\r\n"
  4116. "Connection: close\r\n\r\n",
  4117. len);
  4118. mark_point();
  4119. mg_write(conn, msg, len);
  4120. mark_point();
  4121. return 200;
  4122. }
  4123. START_TEST(test_minimal_http_server_callback)
  4124. {
  4125. /* This test should ensure the minimum server example in
  4126. * docs/Embedding.md is still running. */
  4127. /* Server context handle */
  4128. struct mg_context *ctx;
  4129. mark_point();
  4130. /* Initialize the library */
  4131. mg_init_library(0);
  4132. /* Start the server */
  4133. ctx = test_mg_start(NULL, 0, NULL, __LINE__);
  4134. ck_assert(ctx != NULL);
  4135. /* Add some handler */
  4136. mg_set_request_handler(ctx,
  4137. "/hello",
  4138. minimal_test_request_handler,
  4139. (void *)"Hello world");
  4140. mg_set_request_handler(ctx,
  4141. "/8",
  4142. minimal_test_request_handler,
  4143. (void *)"Number eight");
  4144. /* Run the server for 15 seconds */
  4145. test_sleep(10);
  4146. /* Call a test client */
  4147. minimal_http_client_check("127.0.0.1", 8080, "/hello", "Hello world");
  4148. /* Run the server for 1 second */
  4149. test_sleep(1);
  4150. /* Call a test client */
  4151. minimal_http_client_check("127.0.0.1", 8080, "/8", "Number eight");
  4152. /* Run the server for 1 second */
  4153. test_sleep(1);
  4154. /* Call a test client */
  4155. minimal_http_client_check("127.0.0.1",
  4156. 8080,
  4157. "/8?Alternative=Response",
  4158. "Alternative=Response");
  4159. /* Run the server for 5 seconds */
  4160. test_sleep(5);
  4161. /* Stop the server */
  4162. test_mg_stop(ctx, __LINE__);
  4163. /* Un-initialize the library */
  4164. mg_exit_library();
  4165. mark_point();
  4166. }
  4167. END_TEST
  4168. START_TEST(test_minimal_https_server_callback)
  4169. {
  4170. #if !defined(NO_SSL)
  4171. /* This test should show a HTTPS server with enhanced
  4172. * security settings.
  4173. *
  4174. * Articles:
  4175. * https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
  4176. *
  4177. * Scanners:
  4178. * https://securityheaders.io/
  4179. * https://www.htbridge.com/ssl/
  4180. * https://www.htbridge.com/websec/
  4181. * https://www.ssllabs.com/ssltest/
  4182. * https://www.qualys.com/forms/freescan/
  4183. */
  4184. /* Server context handle */
  4185. struct mg_context *ctx;
  4186. /* Server start parameters for HTTPS */
  4187. const char *OPTIONS[32];
  4188. int opt_idx = 0;
  4189. /* HTTPS port - required */
  4190. OPTIONS[opt_idx++] = "listening_ports";
  4191. OPTIONS[opt_idx++] = "8443s";
  4192. /* path to certificate file - required */
  4193. OPTIONS[opt_idx++] = "ssl_certificate";
  4194. OPTIONS[opt_idx++] = locate_ssl_cert();
  4195. #if defined(LOCAL_TEST) || defined(_WIN32)
  4196. /* Do not set this on Travis CI, since the build containers
  4197. * contain older SSL libraries */
  4198. /* set minimum SSL version to TLS 1.2 - recommended */
  4199. OPTIONS[opt_idx++] = "ssl_protocol_version";
  4200. OPTIONS[opt_idx++] = "4";
  4201. /* set some modern ciphers - recommended */
  4202. OPTIONS[opt_idx++] = "ssl_cipher_list";
  4203. OPTIONS[opt_idx++] = "ECDH+AESGCM+AES256:!aNULL:!MD5:!DSS";
  4204. #endif
  4205. /* set "HTTPS only" header - recommended */
  4206. OPTIONS[opt_idx++] = "strict_transport_security_max_age";
  4207. OPTIONS[opt_idx++] = "31622400";
  4208. /* end of options - required */
  4209. OPTIONS[opt_idx] = NULL;
  4210. mark_point();
  4211. /* Initialize the library */
  4212. mg_init_library(0);
  4213. /* Start the server */
  4214. ctx = test_mg_start(NULL, 0, OPTIONS, __LINE__);
  4215. ck_assert(ctx != NULL);
  4216. /* Add some handler */
  4217. mg_set_request_handler(ctx,
  4218. "/hello",
  4219. minimal_test_request_handler,
  4220. (void *)"Hello world");
  4221. mg_set_request_handler(ctx,
  4222. "/8",
  4223. minimal_test_request_handler,
  4224. (void *)"Number eight");
  4225. /* Run the server for 15 seconds */
  4226. test_sleep(10);
  4227. /* Call a test client */
  4228. minimal_https_client_check("127.0.0.1", 8443, "/hello", "Hello world");
  4229. /* Run the server for 15 seconds */
  4230. test_sleep(1);
  4231. /* Call a test client */
  4232. minimal_https_client_check("127.0.0.1", 8443, "/8", "Number eight");
  4233. /* Run the server for 1 second */
  4234. test_sleep(1);
  4235. /* Call a test client */
  4236. minimal_https_client_check("127.0.0.1",
  4237. 8443,
  4238. "/8?Alternative=Response",
  4239. "Alternative=Response");
  4240. /* Run the server for 5 seconds */
  4241. test_sleep(5);
  4242. /* Stop the server */
  4243. test_mg_stop(ctx, __LINE__);
  4244. /* Un-initialize the library */
  4245. mg_exit_library();
  4246. #endif
  4247. mark_point();
  4248. }
  4249. END_TEST
  4250. #if !defined(REPLACE_CHECK_FOR_LOCAL_DEBUGGING)
  4251. Suite *
  4252. make_public_server_suite(void)
  4253. {
  4254. Suite *const suite = suite_create("PublicServer");
  4255. TCase *const tcase_checktestenv = tcase_create("Check test environment");
  4256. TCase *const tcase_initlib = tcase_create("Init library");
  4257. TCase *const tcase_startthreads = tcase_create("Start threads");
  4258. TCase *const tcase_minimal_http_svr = tcase_create("Minimal HTTP Server");
  4259. TCase *const tcase_minimal_https_svr = tcase_create("Minimal HTTPS Server");
  4260. TCase *const tcase_minimal_http_cli = tcase_create("Minimal HTTP Client");
  4261. TCase *const tcase_minimal_https_cli = tcase_create("Minimal HTTPS Client");
  4262. TCase *const tcase_startstophttp = tcase_create("Start Stop HTTP Server");
  4263. TCase *const tcase_startstophttp_ipv6 =
  4264. tcase_create("Start Stop HTTP Server IPv6");
  4265. TCase *const tcase_startstophttps = tcase_create("Start Stop HTTPS Server");
  4266. TCase *const tcase_serverandclienttls = tcase_create("TLS Server Client");
  4267. TCase *const tcase_serverrequests = tcase_create("Server Requests");
  4268. TCase *const tcase_storebody = tcase_create("Store Body");
  4269. TCase *const tcase_handle_form = tcase_create("Handle Form");
  4270. TCase *const tcase_http_auth = tcase_create("HTTP Authentication");
  4271. TCase *const tcase_keep_alive = tcase_create("HTTP Keep Alive");
  4272. TCase *const tcase_error_handling = tcase_create("Error handling");
  4273. TCase *const tcase_error_log = tcase_create("Error logging");
  4274. TCase *const tcase_throttle = tcase_create("Limit speed");
  4275. TCase *const tcase_large_file = tcase_create("Large file");
  4276. TCase *const tcase_file_in_mem = tcase_create("File in memory");
  4277. tcase_add_test(tcase_checktestenv, test_the_test_environment);
  4278. tcase_set_timeout(tcase_checktestenv, civetweb_min_test_timeout);
  4279. suite_add_tcase(suite, tcase_checktestenv);
  4280. tcase_add_test(tcase_initlib, test_init_library);
  4281. tcase_set_timeout(tcase_initlib, civetweb_min_test_timeout);
  4282. suite_add_tcase(suite, tcase_initlib);
  4283. tcase_add_test(tcase_startthreads, test_threading);
  4284. tcase_set_timeout(tcase_startthreads, civetweb_min_test_timeout);
  4285. suite_add_tcase(suite, tcase_startthreads);
  4286. tcase_add_test(tcase_minimal_http_svr, test_minimal_http_server_callback);
  4287. tcase_set_timeout(tcase_minimal_http_svr, civetweb_min_server_test_timeout);
  4288. suite_add_tcase(suite, tcase_minimal_http_svr);
  4289. tcase_add_test(tcase_minimal_https_svr, test_minimal_https_server_callback);
  4290. tcase_set_timeout(tcase_minimal_https_svr,
  4291. civetweb_min_server_test_timeout);
  4292. suite_add_tcase(suite, tcase_minimal_https_svr);
  4293. tcase_add_test(tcase_minimal_http_cli, test_minimal_client);
  4294. tcase_set_timeout(tcase_minimal_http_cli, civetweb_min_server_test_timeout);
  4295. suite_add_tcase(suite, tcase_minimal_http_cli);
  4296. tcase_add_test(tcase_minimal_https_cli, test_minimal_tls_client);
  4297. tcase_set_timeout(tcase_minimal_https_cli,
  4298. civetweb_min_server_test_timeout);
  4299. suite_add_tcase(suite, tcase_minimal_https_cli);
  4300. tcase_add_test(tcase_startstophttp, test_mg_start_stop_http_server);
  4301. tcase_set_timeout(tcase_startstophttp, civetweb_min_server_test_timeout);
  4302. suite_add_tcase(suite, tcase_startstophttp);
  4303. tcase_add_test(tcase_startstophttp_ipv6,
  4304. test_mg_start_stop_http_server_ipv6);
  4305. tcase_set_timeout(tcase_startstophttp_ipv6,
  4306. civetweb_min_server_test_timeout);
  4307. suite_add_tcase(suite, tcase_startstophttp_ipv6);
  4308. tcase_add_test(tcase_startstophttps, test_mg_start_stop_https_server);
  4309. tcase_set_timeout(tcase_startstophttps, civetweb_min_server_test_timeout);
  4310. suite_add_tcase(suite, tcase_startstophttps);
  4311. tcase_add_test(tcase_serverandclienttls, test_mg_server_and_client_tls);
  4312. tcase_set_timeout(tcase_serverandclienttls,
  4313. civetweb_min_server_test_timeout);
  4314. suite_add_tcase(suite, tcase_serverandclienttls);
  4315. tcase_add_test(tcase_serverrequests, test_request_handlers);
  4316. tcase_set_timeout(tcase_serverrequests, civetweb_mid_server_test_timeout);
  4317. suite_add_tcase(suite, tcase_serverrequests);
  4318. tcase_add_test(tcase_storebody, test_mg_store_body);
  4319. tcase_set_timeout(tcase_storebody, civetweb_mid_server_test_timeout);
  4320. suite_add_tcase(suite, tcase_storebody);
  4321. tcase_add_test(tcase_handle_form, test_handle_form);
  4322. tcase_set_timeout(tcase_handle_form, civetweb_mid_server_test_timeout);
  4323. suite_add_tcase(suite, tcase_handle_form);
  4324. tcase_add_test(tcase_http_auth, test_http_auth);
  4325. tcase_set_timeout(tcase_http_auth, civetweb_min_server_test_timeout);
  4326. suite_add_tcase(suite, tcase_http_auth);
  4327. tcase_add_test(tcase_keep_alive, test_keep_alive);
  4328. tcase_set_timeout(tcase_keep_alive, civetweb_mid_server_test_timeout);
  4329. suite_add_tcase(suite, tcase_keep_alive);
  4330. tcase_add_test(tcase_error_handling, test_error_handling);
  4331. tcase_set_timeout(tcase_error_handling, civetweb_mid_server_test_timeout);
  4332. suite_add_tcase(suite, tcase_error_handling);
  4333. tcase_add_test(tcase_error_log, test_error_log_file);
  4334. tcase_set_timeout(tcase_error_log, civetweb_mid_server_test_timeout);
  4335. suite_add_tcase(suite, tcase_error_log);
  4336. tcase_add_test(tcase_throttle, test_throttle);
  4337. tcase_set_timeout(tcase_throttle, civetweb_mid_server_test_timeout);
  4338. suite_add_tcase(suite, tcase_throttle);
  4339. tcase_add_test(tcase_large_file, test_large_file);
  4340. tcase_set_timeout(tcase_large_file, civetweb_mid_server_test_timeout);
  4341. suite_add_tcase(suite, tcase_large_file);
  4342. tcase_add_test(tcase_file_in_mem, test_file_in_memory);
  4343. tcase_set_timeout(tcase_file_in_mem, civetweb_mid_server_test_timeout);
  4344. suite_add_tcase(suite, tcase_file_in_mem);
  4345. return suite;
  4346. }
  4347. #endif
  4348. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  4349. /* Used to debug test cases without using the check framework */
  4350. /* Build command for Linux:
  4351. gcc test/public_server.c src/civetweb.c -I include/ -I test/ -l pthread -l dl -D
  4352. LOCAL_TEST -D REPLACE_CHECK_FOR_LOCAL_DEBUGGING -D MAIN_PUBLIC_SERVER=main
  4353. */
  4354. static int chk_ok = 0;
  4355. static int chk_failed = 0;
  4356. void
  4357. MAIN_PUBLIC_SERVER(void)
  4358. {
  4359. unsigned f_avail = mg_check_feature(0xFF);
  4360. unsigned f_ret = mg_init_library(f_avail);
  4361. ck_assert_uint_eq(f_ret, f_avail);
  4362. test_handle_form(0);
  4363. test_the_test_environment(0);
  4364. test_threading(0);
  4365. test_minimal_client(0);
  4366. test_mg_start_stop_http_server(0);
  4367. test_mg_start_stop_https_server(0);
  4368. test_request_handlers(0);
  4369. test_mg_store_body(0);
  4370. test_mg_server_and_client_tls(0);
  4371. test_handle_form(0);
  4372. test_http_auth(0);
  4373. test_keep_alive(0);
  4374. test_error_handling(0);
  4375. test_error_log_file(0);
  4376. test_throttle(0);
  4377. test_large_file(0);
  4378. test_file_in_memory(0);
  4379. mg_exit_library();
  4380. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  4381. }
  4382. void
  4383. _ck_assert_failed(const char *file, int line, const char *expr, ...)
  4384. {
  4385. va_list va;
  4386. va_start(va, expr);
  4387. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  4388. vfprintf(stderr, expr, va);
  4389. fprintf(stderr, "\n\n");
  4390. va_end(va);
  4391. chk_failed++;
  4392. }
  4393. void
  4394. _ck_assert_msg(int cond, const char *file, int line, const char *expr, ...)
  4395. {
  4396. va_list va;
  4397. if (cond) {
  4398. chk_ok++;
  4399. return;
  4400. }
  4401. va_start(va, expr);
  4402. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  4403. vfprintf(stderr, expr, va);
  4404. fprintf(stderr, "\n\n");
  4405. va_end(va);
  4406. chk_failed++;
  4407. }
  4408. void
  4409. _mark_point(const char *file, int line)
  4410. {
  4411. chk_ok++;
  4412. }
  4413. void
  4414. tcase_fn_start(const char *fname, const char *file, int line)
  4415. {
  4416. }
  4417. void suite_add_tcase(Suite *s, TCase *tc){};
  4418. void _tcase_add_test(TCase *tc,
  4419. TFun tf,
  4420. const char *fname,
  4421. int _signal,
  4422. int allowed_exit_value,
  4423. int start,
  4424. int end){};
  4425. TCase *
  4426. tcase_create(const char *name)
  4427. {
  4428. return NULL;
  4429. };
  4430. Suite *
  4431. suite_create(const char *name)
  4432. {
  4433. return NULL;
  4434. };
  4435. void tcase_set_timeout(TCase *tc, double timeout){};
  4436. #endif