public_server.c 130 KB

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