public_server.c 134 KB

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