public_server.c 130 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632
  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, "%scgi_test.cgi", locate_test_exes());
  1182. #else
  1183. sprintf(cmd_buf, "%scgi_test.cgi", locate_test_exes());
  1184. #endif
  1185. memset(&st, 0, sizeof(st));
  1186. if (stat(cmd_buf, &st) != 0) {
  1187. fprintf(stderr, "\nFile %s not found\n", cmd_buf);
  1188. fprintf(stderr,
  1189. "This file needs to be compiled manually before "
  1190. "starting the test\n");
  1191. fprintf(stderr,
  1192. "e.g. by gcc test/cgi_test.c -o output/cgi_test.cgi\n\n");
  1193. /* Print working directory, if file is not found */
  1194. #if defined(_WIN32)
  1195. strcpy(cmd_buf, "cd && dir");
  1196. #else
  1197. strcpy(cmd_buf, "pwd && ls -la");
  1198. #endif
  1199. (void)system(cmd_buf);
  1200. /* Abort test with diagnostic message */
  1201. ck_abort_msg("Mandatory file %s must be built before starting the test",
  1202. cmd_buf);
  1203. }
  1204. /* Test with CGI test executable */
  1205. #if defined(_WIN32)
  1206. sprintf(cmd_buf, "copy %s\\cgi_test.cgi cgi_test.exe", locate_test_exes());
  1207. #else
  1208. sprintf(cmd_buf, "cp %s/cgi_test.cgi cgi_test.cgi", locate_test_exes());
  1209. #endif
  1210. (void)system(cmd_buf);
  1211. #if !defined(NO_CGI) && !defined(NO_FILES) && !defined(_WIN32)
  1212. /* TODO: add test for windows, check with POST */
  1213. client_conn = mg_download(
  1214. "localhost",
  1215. ipv4_port,
  1216. 0,
  1217. ebuf,
  1218. sizeof(ebuf),
  1219. "%s",
  1220. "POST /cgi_test.cgi/x/y.z HTTP/1.0\r\nContent-Length: 3\r\n\r\nABC");
  1221. ck_assert(client_conn != NULL);
  1222. ri = mg_get_request_info(client_conn);
  1223. ck_assert(ri != NULL);
  1224. ck_assert_str_eq(ri->local_uri, "200");
  1225. mg_close_connection(client_conn);
  1226. #endif
  1227. /* Get zipped static data - will not work if Accept-Encoding is not set */
  1228. client_conn = mg_download("localhost",
  1229. ipv4_port,
  1230. 0,
  1231. ebuf,
  1232. sizeof(ebuf),
  1233. "%s",
  1234. "GET /test_gz.txt HTTP/1.0\r\n\r\n");
  1235. ck_assert(client_conn != NULL);
  1236. ri = mg_get_request_info(client_conn);
  1237. ck_assert(ri != NULL);
  1238. ck_assert_str_eq(ri->local_uri, "404");
  1239. mg_close_connection(client_conn);
  1240. /* Get zipped static data - with Accept-Encoding */
  1241. client_conn = mg_download(
  1242. "localhost",
  1243. ipv4_port,
  1244. 0,
  1245. ebuf,
  1246. sizeof(ebuf),
  1247. "%s",
  1248. "GET /test_gz.txt HTTP/1.0\r\nAccept-Encoding: gzip\r\n\r\n");
  1249. ck_assert(client_conn != NULL);
  1250. ri = mg_get_request_info(client_conn);
  1251. ck_assert(ri != NULL);
  1252. #if defined(NO_FILES)
  1253. ck_assert_str_eq(ri->local_uri, "404");
  1254. #else
  1255. ck_assert_str_eq(ri->local_uri, "200");
  1256. i = mg_read(client_conn, buf, sizeof(buf));
  1257. ck_assert_int_eq(i, 52);
  1258. if ((i >= 0) && (i < (int)sizeof(buf))) {
  1259. buf[i] = 0;
  1260. }
  1261. ck_assert_int_eq(ri->content_length, 52);
  1262. ck_assert_str_eq(buf, encoded_file_content);
  1263. #endif
  1264. mg_close_connection(client_conn);
  1265. /* Get CGI generated data */
  1266. #if !defined(NO_CGI)
  1267. client_conn = mg_download("localhost",
  1268. ipv4_port,
  1269. 0,
  1270. ebuf,
  1271. sizeof(ebuf),
  1272. "%s",
  1273. "GET /test.cgi HTTP/1.0\r\n\r\n");
  1274. ck_assert(client_conn != NULL);
  1275. ri = mg_get_request_info(client_conn);
  1276. ck_assert(ri != NULL);
  1277. #if defined(NO_FILES)
  1278. ck_assert_str_eq(ri->local_uri, "404");
  1279. (void)expected_cgi_result;
  1280. (void)cgi_script_content;
  1281. #else
  1282. i = mg_read(client_conn, buf, sizeof(buf));
  1283. if ((i >= 0) && (i < (int)sizeof(buf))) {
  1284. while ((i > 0) && ((buf[i - 1] == '\r') || (buf[i - 1] == '\n'))) {
  1285. i--;
  1286. }
  1287. buf[i] = 0;
  1288. }
  1289. /* ck_assert_int_eq(i, (int)strlen(expected_cgi_result)); */
  1290. ck_assert_str_eq(buf, expected_cgi_result);
  1291. ck_assert_str_eq(ri->local_uri, "200");
  1292. mg_close_connection(client_conn);
  1293. #endif
  1294. #else
  1295. (void)expected_cgi_result;
  1296. (void)cgi_script_content;
  1297. #endif
  1298. /* Get directory listing */
  1299. client_conn = mg_download("localhost",
  1300. ipv4_port,
  1301. 0,
  1302. ebuf,
  1303. sizeof(ebuf),
  1304. "%s",
  1305. "GET / HTTP/1.0\r\n\r\n");
  1306. ck_assert(client_conn != NULL);
  1307. ri = mg_get_request_info(client_conn);
  1308. ck_assert(ri != NULL);
  1309. #if defined(NO_FILES)
  1310. ck_assert_str_eq(ri->local_uri, "404");
  1311. #else
  1312. ck_assert_str_eq(ri->local_uri, "200");
  1313. i = mg_read(client_conn, buf, sizeof(buf));
  1314. ck_assert(i > 6);
  1315. buf[6] = 0;
  1316. ck_assert_str_eq(buf, "<html>");
  1317. #endif
  1318. mg_close_connection(client_conn);
  1319. /* POST to static file (will not work) */
  1320. client_conn = mg_download("localhost",
  1321. ipv4_port,
  1322. 0,
  1323. ebuf,
  1324. sizeof(ebuf),
  1325. "%s",
  1326. "POST /test.txt HTTP/1.0\r\n\r\n");
  1327. ck_assert(client_conn != NULL);
  1328. ri = mg_get_request_info(client_conn);
  1329. ck_assert(ri != NULL);
  1330. #if defined(NO_FILES)
  1331. ck_assert_str_eq(ri->local_uri, "404");
  1332. #else
  1333. ck_assert_str_eq(ri->local_uri, "405");
  1334. i = mg_read(client_conn, buf, sizeof(buf));
  1335. ck_assert(i >= 29);
  1336. buf[29] = 0;
  1337. ck_assert_str_eq(buf, "Error 405: Method Not Allowed");
  1338. #endif
  1339. mg_close_connection(client_conn);
  1340. /* PUT to static file (will not work) */
  1341. client_conn = mg_download("localhost",
  1342. ipv4_port,
  1343. 0,
  1344. ebuf,
  1345. sizeof(ebuf),
  1346. "%s",
  1347. "PUT /test.txt HTTP/1.0\r\n\r\n");
  1348. ck_assert(client_conn != NULL);
  1349. ri = mg_get_request_info(client_conn);
  1350. ck_assert(ri != NULL);
  1351. #if defined(NO_FILES)
  1352. ck_assert_str_eq(ri->local_uri, "405"); /* method not allowed */
  1353. #else
  1354. ck_assert_str_eq(ri->local_uri, "401"); /* not authorized */
  1355. #endif
  1356. mg_close_connection(client_conn);
  1357. /* Get data from callback using mg_connect_client instead of mg_download */
  1358. memset(ebuf, 0, sizeof(ebuf));
  1359. client_conn =
  1360. mg_connect_client("127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf));
  1361. ck_assert(client_conn != NULL);
  1362. ck_assert_str_eq(ebuf, "");
  1363. mg_printf(client_conn, "%s", request);
  1364. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1365. ck_assert_int_ge(i, 0);
  1366. ck_assert_str_eq(ebuf, "");
  1367. ri = mg_get_request_info(client_conn);
  1368. ck_assert(ri != NULL);
  1369. ck_assert_str_eq(ri->local_uri, "200");
  1370. i = mg_read(client_conn, buf, sizeof(buf));
  1371. ck_assert_int_eq(i, (int)strlen(expected));
  1372. buf[i] = 0;
  1373. ck_assert_str_eq(buf, expected);
  1374. mg_close_connection(client_conn);
  1375. /* Get data from callback using mg_connect_client and absolute URI */
  1376. memset(ebuf, 0, sizeof(ebuf));
  1377. client_conn =
  1378. mg_connect_client("localhost", ipv4_port, 0, ebuf, sizeof(ebuf));
  1379. ck_assert(client_conn != NULL);
  1380. ck_assert_str_eq(ebuf, "");
  1381. mg_printf(client_conn,
  1382. "GET http://test.domain:%d/U7 HTTP/1.0\r\n\r\n",
  1383. ipv4_port);
  1384. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1385. ck_assert_int_ge(i, 0);
  1386. ck_assert_str_eq(ebuf, "");
  1387. ri = mg_get_request_info(client_conn);
  1388. ck_assert(ri != NULL);
  1389. ck_assert_str_eq(ri->local_uri, "200");
  1390. i = mg_read(client_conn, buf, sizeof(buf));
  1391. ck_assert_int_eq(i, (int)strlen(expected));
  1392. buf[i] = 0;
  1393. ck_assert_str_eq(buf, expected);
  1394. mg_close_connection(client_conn);
  1395. /* Get data from callback using mg_connect_client and absolute URI with a
  1396. * sub-domain */
  1397. memset(ebuf, 0, sizeof(ebuf));
  1398. client_conn =
  1399. mg_connect_client("localhost", ipv4_port, 0, ebuf, sizeof(ebuf));
  1400. ck_assert(client_conn != NULL);
  1401. ck_assert_str_eq(ebuf, "");
  1402. mg_printf(client_conn,
  1403. "GET http://subdomain.test.domain:%d/U7 HTTP/1.0\r\n\r\n",
  1404. ipv4_port);
  1405. i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
  1406. ck_assert_int_ge(i, 0);
  1407. ck_assert_str_eq(ebuf, "");
  1408. ri = mg_get_request_info(client_conn);
  1409. ck_assert(ri != NULL);
  1410. ck_assert_str_eq(ri->local_uri, "200");
  1411. i = mg_read(client_conn, buf, sizeof(buf));
  1412. ck_assert_int_eq(i, (int)strlen(expected));
  1413. buf[i] = 0;
  1414. ck_assert_str_eq(buf, expected);
  1415. mg_close_connection(client_conn);
  1416. /* Websocket test */
  1417. #ifdef USE_WEBSOCKET
  1418. /* Then connect a first client */
  1419. ws_client1_conn =
  1420. mg_connect_websocket_client("localhost",
  1421. ipv4_port,
  1422. 0,
  1423. ebuf,
  1424. sizeof(ebuf),
  1425. "/websocket",
  1426. NULL,
  1427. websocket_client_data_handler,
  1428. websocket_client_close_handler,
  1429. &ws_client1_data);
  1430. ck_assert(ws_client1_conn != NULL);
  1431. wait_not_null(
  1432. &(ws_client1_data.data)); /* Wait for the websocket welcome message */
  1433. ck_assert_int_eq(ws_client1_data.closed, 0);
  1434. ck_assert_int_eq(ws_client2_data.closed, 0);
  1435. ck_assert_int_eq(ws_client3_data.closed, 0);
  1436. ck_assert(ws_client2_data.data == NULL);
  1437. ck_assert_uint_eq(ws_client2_data.len, 0);
  1438. ck_assert(ws_client1_data.data != NULL);
  1439. ck_assert_uint_eq(ws_client1_data.len, websocket_welcome_msg_len);
  1440. ck_assert(!memcmp(ws_client1_data.data,
  1441. websocket_welcome_msg,
  1442. websocket_welcome_msg_len));
  1443. free(ws_client1_data.data);
  1444. ws_client1_data.data = NULL;
  1445. ws_client1_data.len = 0;
  1446. mg_websocket_client_write(ws_client1_conn,
  1447. WEBSOCKET_OPCODE_TEXT,
  1448. "data1",
  1449. 5);
  1450. wait_not_null(
  1451. &(ws_client1_data
  1452. .data)); /* Wait for the websocket acknowledge message */
  1453. ck_assert_int_eq(ws_client1_data.closed, 0);
  1454. ck_assert_int_eq(ws_client2_data.closed, 0);
  1455. ck_assert(ws_client2_data.data == NULL);
  1456. ck_assert_uint_eq(ws_client2_data.len, 0);
  1457. ck_assert(ws_client1_data.data != NULL);
  1458. ck_assert_uint_eq(ws_client1_data.len, 3);
  1459. ck_assert(!memcmp(ws_client1_data.data, "ok1", 3));
  1460. free(ws_client1_data.data);
  1461. ws_client1_data.data = NULL;
  1462. ws_client1_data.len = 0;
  1463. /* Now connect a second client */
  1464. #ifdef USE_IPV6
  1465. ws_client2_conn =
  1466. mg_connect_websocket_client("[::1]",
  1467. ipv6_port,
  1468. 0,
  1469. ebuf,
  1470. sizeof(ebuf),
  1471. "/websocket",
  1472. NULL,
  1473. websocket_client_data_handler,
  1474. websocket_client_close_handler,
  1475. &ws_client2_data);
  1476. #else
  1477. ws_client2_conn =
  1478. mg_connect_websocket_client("127.0.0.1",
  1479. ipv4_port,
  1480. 0,
  1481. ebuf,
  1482. sizeof(ebuf),
  1483. "/websocket",
  1484. NULL,
  1485. websocket_client_data_handler,
  1486. websocket_client_close_handler,
  1487. &ws_client2_data);
  1488. #endif
  1489. ck_assert(ws_client2_conn != NULL);
  1490. wait_not_null(
  1491. &(ws_client2_data.data)); /* Wait for the websocket welcome message */
  1492. ck_assert(ws_client1_data.closed == 0);
  1493. ck_assert(ws_client2_data.closed == 0);
  1494. ck_assert(ws_client1_data.data == NULL);
  1495. ck_assert(ws_client1_data.len == 0);
  1496. ck_assert(ws_client2_data.data != NULL);
  1497. ck_assert(ws_client2_data.len == websocket_welcome_msg_len);
  1498. ck_assert(!memcmp(ws_client2_data.data,
  1499. websocket_welcome_msg,
  1500. websocket_welcome_msg_len));
  1501. free(ws_client2_data.data);
  1502. ws_client2_data.data = NULL;
  1503. ws_client2_data.len = 0;
  1504. mg_websocket_client_write(ws_client1_conn,
  1505. WEBSOCKET_OPCODE_TEXT,
  1506. "data2",
  1507. 5);
  1508. wait_not_null(
  1509. &(ws_client1_data
  1510. .data)); /* Wait for the websocket acknowledge message */
  1511. ck_assert(ws_client1_data.closed == 0);
  1512. ck_assert(ws_client2_data.closed == 0);
  1513. ck_assert(ws_client2_data.data == NULL);
  1514. ck_assert(ws_client2_data.len == 0);
  1515. ck_assert(ws_client1_data.data != NULL);
  1516. ck_assert(ws_client1_data.len == 4);
  1517. ck_assert(!memcmp(ws_client1_data.data, "ok 2", 4));
  1518. free(ws_client1_data.data);
  1519. ws_client1_data.data = NULL;
  1520. ws_client1_data.len = 0;
  1521. mg_websocket_client_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  1522. wait_not_null(
  1523. &(ws_client1_data.data)); /* Wait for the websocket goodbye message */
  1524. ck_assert(ws_client1_data.closed == 0);
  1525. ck_assert(ws_client2_data.closed == 0);
  1526. ck_assert(ws_client2_data.data == NULL);
  1527. ck_assert(ws_client2_data.len == 0);
  1528. ck_assert(ws_client1_data.data != NULL);
  1529. ck_assert(ws_client1_data.len == websocket_goodbye_msg_len);
  1530. ck_assert(!memcmp(ws_client1_data.data,
  1531. websocket_goodbye_msg,
  1532. websocket_goodbye_msg_len));
  1533. free(ws_client1_data.data);
  1534. ws_client1_data.data = NULL;
  1535. ws_client1_data.len = 0;
  1536. mg_close_connection(ws_client1_conn);
  1537. test_sleep(3); /* Won't get any 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 == 0);
  1544. mg_websocket_client_write(ws_client2_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  1545. wait_not_null(
  1546. &(ws_client2_data.data)); /* Wait for the websocket goodbye message */
  1547. ck_assert(ws_client1_data.closed == 1);
  1548. ck_assert(ws_client2_data.closed == 0);
  1549. ck_assert(ws_client1_data.data == NULL);
  1550. ck_assert(ws_client1_data.len == 0);
  1551. ck_assert(ws_client2_data.data != NULL);
  1552. ck_assert(ws_client2_data.len == websocket_goodbye_msg_len);
  1553. ck_assert(!memcmp(ws_client2_data.data,
  1554. websocket_goodbye_msg,
  1555. websocket_goodbye_msg_len));
  1556. free(ws_client2_data.data);
  1557. ws_client2_data.data = NULL;
  1558. ws_client2_data.len = 0;
  1559. mg_close_connection(ws_client2_conn);
  1560. test_sleep(3); /* Won't get any message */
  1561. ck_assert(ws_client1_data.closed == 1);
  1562. ck_assert(ws_client2_data.closed == 1);
  1563. ck_assert(ws_client1_data.data == NULL);
  1564. ck_assert(ws_client1_data.len == 0);
  1565. ck_assert(ws_client2_data.data == NULL);
  1566. ck_assert(ws_client2_data.len == 0);
  1567. /* Connect client 3 */
  1568. ws_client3_conn =
  1569. mg_connect_websocket_client("localhost",
  1570. #if defined(NO_SSL)
  1571. ipv4_port,
  1572. 0,
  1573. #else
  1574. ipv4s_port,
  1575. 1,
  1576. #endif
  1577. ebuf,
  1578. sizeof(ebuf),
  1579. "/websocket",
  1580. NULL,
  1581. websocket_client_data_handler,
  1582. websocket_client_close_handler,
  1583. &ws_client3_data);
  1584. ck_assert(ws_client3_conn != NULL);
  1585. wait_not_null(
  1586. &(ws_client3_data.data)); /* Wait for the websocket welcome message */
  1587. ck_assert(ws_client1_data.closed == 1);
  1588. ck_assert(ws_client2_data.closed == 1);
  1589. ck_assert(ws_client3_data.closed == 0);
  1590. ck_assert(ws_client1_data.data == NULL);
  1591. ck_assert(ws_client1_data.len == 0);
  1592. ck_assert(ws_client2_data.data == NULL);
  1593. ck_assert(ws_client2_data.len == 0);
  1594. ck_assert(ws_client3_data.data != NULL);
  1595. ck_assert(ws_client3_data.len == websocket_welcome_msg_len);
  1596. ck_assert(!memcmp(ws_client3_data.data,
  1597. websocket_welcome_msg,
  1598. websocket_welcome_msg_len));
  1599. free(ws_client3_data.data);
  1600. ws_client3_data.data = NULL;
  1601. ws_client3_data.len = 0;
  1602. /* Write long data (16 bit size header) */
  1603. mg_websocket_client_write(ws_client3_conn,
  1604. WEBSOCKET_OPCODE_BINARY,
  1605. long_ws_buf,
  1606. long_ws_buf_len_16);
  1607. /* Wait for the response */
  1608. wait_not_null(&(ws_client3_data.data));
  1609. ck_assert_int_eq((int)ws_client3_data.len, (int)long_ws_buf_len_16);
  1610. ck_assert(!memcmp(ws_client3_data.data, long_ws_buf, long_ws_buf_len_16));
  1611. free(ws_client3_data.data);
  1612. ws_client3_data.data = NULL;
  1613. ws_client3_data.len = 0;
  1614. /* Write long data (64 bit size header) */
  1615. mg_websocket_client_write(ws_client3_conn,
  1616. WEBSOCKET_OPCODE_BINARY,
  1617. long_ws_buf,
  1618. long_ws_buf_len_64);
  1619. /* Wait for the response */
  1620. wait_not_null(&(ws_client3_data.data));
  1621. ck_assert_int_eq((int)ws_client3_data.len, (int)long_ws_buf_len_64);
  1622. ck_assert(!memcmp(ws_client3_data.data, long_ws_buf, long_ws_buf_len_64));
  1623. free(ws_client3_data.data);
  1624. ws_client3_data.data = NULL;
  1625. ws_client3_data.len = 0;
  1626. /* Disconnect client 3 */
  1627. ck_assert(ws_client3_data.closed == 0);
  1628. mg_close_connection(ws_client3_conn);
  1629. ck_assert(ws_client3_data.closed == 1);
  1630. /* Connect client 4 */
  1631. ws_client4_conn =
  1632. mg_connect_websocket_client("localhost",
  1633. #if defined(NO_SSL)
  1634. ipv4_port,
  1635. 0,
  1636. #else
  1637. ipv4s_port,
  1638. 1,
  1639. #endif
  1640. ebuf,
  1641. sizeof(ebuf),
  1642. "/websocket",
  1643. NULL,
  1644. websocket_client_data_handler,
  1645. websocket_client_close_handler,
  1646. &ws_client4_data);
  1647. ck_assert(ws_client4_conn != NULL);
  1648. wait_not_null(
  1649. &(ws_client4_data.data)); /* Wait for the websocket welcome message */
  1650. ck_assert(ws_client1_data.closed == 1);
  1651. ck_assert(ws_client2_data.closed == 1);
  1652. ck_assert(ws_client3_data.closed == 1);
  1653. ck_assert(ws_client4_data.closed == 0);
  1654. ck_assert(ws_client4_data.data != NULL);
  1655. ck_assert(ws_client4_data.len == websocket_welcome_msg_len);
  1656. ck_assert(!memcmp(ws_client4_data.data,
  1657. websocket_welcome_msg,
  1658. websocket_welcome_msg_len));
  1659. free(ws_client4_data.data);
  1660. ws_client4_data.data = NULL;
  1661. ws_client4_data.len = 0;
  1662. /* stop the server without closing this connection */
  1663. #endif
  1664. /* Close the server */
  1665. g_ctx = NULL;
  1666. test_mg_stop(ctx);
  1667. mark_point();
  1668. #ifdef USE_WEBSOCKET
  1669. for (i = 0; i < 100; i++) {
  1670. test_sleep(1);
  1671. if (ws_client3_data.closed != 0) {
  1672. mark_point();
  1673. break;
  1674. }
  1675. }
  1676. ck_assert_int_eq(ws_client4_data.closed, 1);
  1677. #endif
  1678. }
  1679. END_TEST
  1680. static int g_field_found_return = -999;
  1681. static int
  1682. field_found(const char *key,
  1683. const char *filename,
  1684. char *path,
  1685. size_t pathlen,
  1686. void *user_data)
  1687. {
  1688. ck_assert_ptr_ne(key, NULL);
  1689. ck_assert_ptr_ne(filename, NULL);
  1690. ck_assert_ptr_ne(path, NULL);
  1691. ck_assert_uint_gt(pathlen, 128);
  1692. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1693. ck_assert((g_field_found_return == FORM_FIELD_STORAGE_GET)
  1694. || (g_field_found_return == FORM_FIELD_STORAGE_STORE)
  1695. || (g_field_found_return == FORM_FIELD_STORAGE_SKIP)
  1696. || (g_field_found_return == FORM_FIELD_STORAGE_ABORT));
  1697. ck_assert_str_ne(key, "dontread");
  1698. if (!strcmp(key, "break_field_handler")) {
  1699. return FORM_FIELD_STORAGE_ABORT;
  1700. }
  1701. if (!strcmp(key, "continue_field_handler")) {
  1702. return FORM_FIELD_STORAGE_SKIP;
  1703. }
  1704. if (g_field_found_return == FORM_FIELD_STORAGE_STORE) {
  1705. strncpy(path, key, pathlen - 8);
  1706. strcat(path, ".txt");
  1707. }
  1708. return g_field_found_return;
  1709. }
  1710. static int g_field_step;
  1711. static int
  1712. field_get(const char *key,
  1713. const char *value_untruncated,
  1714. size_t valuelen,
  1715. void *user_data)
  1716. {
  1717. /* Copy the untruncated value, so string compare functions can be used. */
  1718. /* The check unit test library does not have build in memcmp functions. */
  1719. char *value = (char *)malloc(valuelen + 1);
  1720. ck_assert(value != NULL);
  1721. memcpy(value, value_untruncated, valuelen);
  1722. value[valuelen] = 0;
  1723. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1724. ck_assert_int_ge(g_field_step, 0);
  1725. ++g_field_step;
  1726. switch (g_field_step) {
  1727. case 1:
  1728. ck_assert_str_eq(key, "textin");
  1729. ck_assert_uint_eq(valuelen, 4);
  1730. ck_assert_str_eq(value, "text");
  1731. break;
  1732. case 2:
  1733. ck_assert_str_eq(key, "passwordin");
  1734. ck_assert_uint_eq(valuelen, 0);
  1735. ck_assert_str_eq(value, "");
  1736. break;
  1737. case 3:
  1738. ck_assert_str_eq(key, "radio1");
  1739. ck_assert_uint_eq(valuelen, 4);
  1740. ck_assert_str_eq(value, "val1");
  1741. break;
  1742. case 4:
  1743. ck_assert_str_eq(key, "radio2");
  1744. ck_assert_uint_eq(valuelen, 4);
  1745. ck_assert_str_eq(value, "val1");
  1746. break;
  1747. case 5:
  1748. ck_assert_str_eq(key, "check1");
  1749. ck_assert_uint_eq(valuelen, 4);
  1750. ck_assert_str_eq(value, "val1");
  1751. break;
  1752. case 6:
  1753. ck_assert_str_eq(key, "numberin");
  1754. ck_assert_uint_eq(valuelen, 1);
  1755. ck_assert_str_eq(value, "1");
  1756. break;
  1757. case 7:
  1758. ck_assert_str_eq(key, "datein");
  1759. ck_assert_uint_eq(valuelen, 8);
  1760. ck_assert_str_eq(value, "1.1.2016");
  1761. break;
  1762. case 8:
  1763. ck_assert_str_eq(key, "colorin");
  1764. ck_assert_uint_eq(valuelen, 7);
  1765. ck_assert_str_eq(value, "#80ff00");
  1766. break;
  1767. case 9:
  1768. ck_assert_str_eq(key, "rangein");
  1769. ck_assert_uint_eq(valuelen, 1);
  1770. ck_assert_str_eq(value, "3");
  1771. break;
  1772. case 10:
  1773. ck_assert_str_eq(key, "monthin");
  1774. ck_assert_uint_eq(valuelen, 0);
  1775. ck_assert_str_eq(value, "");
  1776. break;
  1777. case 11:
  1778. ck_assert_str_eq(key, "weekin");
  1779. ck_assert_uint_eq(valuelen, 0);
  1780. ck_assert_str_eq(value, "");
  1781. break;
  1782. case 12:
  1783. ck_assert_str_eq(key, "timein");
  1784. ck_assert_uint_eq(valuelen, 0);
  1785. ck_assert_str_eq(value, "");
  1786. break;
  1787. case 13:
  1788. ck_assert_str_eq(key, "datetimen");
  1789. ck_assert_uint_eq(valuelen, 0);
  1790. ck_assert_str_eq(value, "");
  1791. break;
  1792. case 14:
  1793. ck_assert_str_eq(key, "datetimelocalin");
  1794. ck_assert_uint_eq(valuelen, 0);
  1795. ck_assert_str_eq(value, "");
  1796. break;
  1797. case 15:
  1798. ck_assert_str_eq(key, "emailin");
  1799. ck_assert_uint_eq(valuelen, 0);
  1800. ck_assert_str_eq(value, "");
  1801. break;
  1802. case 16:
  1803. ck_assert_str_eq(key, "searchin");
  1804. ck_assert_uint_eq(valuelen, 0);
  1805. ck_assert_str_eq(value, "");
  1806. break;
  1807. case 17:
  1808. ck_assert_str_eq(key, "telin");
  1809. ck_assert_uint_eq(valuelen, 0);
  1810. ck_assert_str_eq(value, "");
  1811. break;
  1812. case 18:
  1813. ck_assert_str_eq(key, "urlin");
  1814. ck_assert_uint_eq(valuelen, 0);
  1815. ck_assert_str_eq(value, "");
  1816. break;
  1817. case 19:
  1818. ck_assert_str_eq(key, "filein");
  1819. ck_assert_uint_eq(valuelen, 0);
  1820. ck_assert_str_eq(value, "");
  1821. break;
  1822. case 20:
  1823. ck_assert_str_eq(key, "filesin");
  1824. ck_assert_uint_eq(valuelen, 0);
  1825. ck_assert_str_eq(value, "");
  1826. break;
  1827. case 21:
  1828. ck_assert_str_eq(key, "selectin");
  1829. ck_assert_uint_eq(valuelen, 4);
  1830. ck_assert_str_eq(value, "opt1");
  1831. break;
  1832. case 22:
  1833. ck_assert_str_eq(key, "message");
  1834. ck_assert_uint_eq(valuelen, 23);
  1835. ck_assert_str_eq(value, "Text area default text.");
  1836. break;
  1837. default:
  1838. ck_abort_msg("field_get called with g_field_step == %i",
  1839. (int)g_field_step);
  1840. }
  1841. free(value);
  1842. return 0;
  1843. }
  1844. static const char *myfile_content = "Content of myfile.txt\r\n";
  1845. static const int myfile_content_rep = 500;
  1846. static int
  1847. field_store(const char *path, long long file_size, void *user_data)
  1848. {
  1849. FILE *f;
  1850. ck_assert_ptr_eq(user_data, (void *)&g_field_found_return);
  1851. ck_assert_int_ge(g_field_step, 100);
  1852. ++g_field_step;
  1853. switch (g_field_step) {
  1854. case 101:
  1855. ck_assert_str_eq(path, "storeme.txt");
  1856. ck_assert_int_eq(file_size, 9);
  1857. f = fopen(path, "r");
  1858. ck_assert_ptr_ne(f, NULL);
  1859. if (f) {
  1860. char buf[32] = {0};
  1861. int i = (int)fread(buf, 1, 31, f);
  1862. ck_assert_int_eq(i, 9);
  1863. fclose(f);
  1864. ck_assert_str_eq(buf, "storetest");
  1865. }
  1866. break;
  1867. case 102:
  1868. ck_assert_str_eq(path, "file2store.txt");
  1869. ck_assert_uint_eq(23, strlen(myfile_content));
  1870. ck_assert_int_eq(file_size, 23 * myfile_content_rep);
  1871. #ifdef _WIN32
  1872. f = fopen(path, "rb");
  1873. #else
  1874. f = fopen(path, "r");
  1875. #endif
  1876. ck_assert_ptr_ne(f, NULL);
  1877. if (f) {
  1878. char buf[32] = {0};
  1879. int r, i;
  1880. for (r = 0; r < myfile_content_rep; r++) {
  1881. i = (int)fread(buf, 1, 23, f);
  1882. ck_assert_int_eq(i, 23);
  1883. ck_assert_str_eq(buf, myfile_content);
  1884. }
  1885. i = (int)fread(buf, 1, 23, f);
  1886. ck_assert_int_eq(i, 0);
  1887. fclose(f);
  1888. }
  1889. break;
  1890. default:
  1891. ck_abort_msg("field_get called with g_field_step == %i",
  1892. (int)g_field_step);
  1893. }
  1894. return 0;
  1895. }
  1896. static int
  1897. FormGet(struct mg_connection *conn, void *cbdata)
  1898. {
  1899. const struct mg_request_info *req_info = mg_get_request_info(conn);
  1900. int ret;
  1901. struct mg_form_data_handler fdh = {field_found, field_get, NULL, NULL};
  1902. (void)cbdata;
  1903. ck_assert(req_info != NULL);
  1904. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  1905. fdh.user_data = (void *)&g_field_found_return;
  1906. /* Call the form handler */
  1907. g_field_step = 0;
  1908. g_field_found_return = FORM_FIELD_STORAGE_GET;
  1909. ret = mg_handle_form_request(conn, &fdh);
  1910. g_field_found_return = -888;
  1911. ck_assert_int_eq(ret, 22);
  1912. ck_assert_int_eq(g_field_step, 22);
  1913. mg_printf(conn, "%i\r\n", ret);
  1914. g_field_step = 1000;
  1915. return 1;
  1916. }
  1917. static int
  1918. FormStore(struct mg_connection *conn,
  1919. void *cbdata,
  1920. int ret_expected,
  1921. int field_step_expected)
  1922. {
  1923. const struct mg_request_info *req_info = mg_get_request_info(conn);
  1924. int ret;
  1925. struct mg_form_data_handler fdh = {field_found,
  1926. field_get,
  1927. field_store,
  1928. NULL};
  1929. (void)cbdata;
  1930. ck_assert(req_info != NULL);
  1931. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  1932. fdh.user_data = (void *)&g_field_found_return;
  1933. /* Call the form handler */
  1934. g_field_step = 100;
  1935. g_field_found_return = FORM_FIELD_STORAGE_STORE;
  1936. ret = mg_handle_form_request(conn, &fdh);
  1937. ck_assert_int_eq(ret, ret_expected);
  1938. ck_assert_int_eq(g_field_step, field_step_expected);
  1939. mg_printf(conn, "%i\r\n", ret);
  1940. g_field_step = 1000;
  1941. return 1;
  1942. }
  1943. static int
  1944. FormStore1(struct mg_connection *conn, void *cbdata)
  1945. {
  1946. return FormStore(conn, cbdata, 3, 101);
  1947. }
  1948. static int
  1949. FormStore2(struct mg_connection *conn, void *cbdata)
  1950. {
  1951. return FormStore(conn, cbdata, 4, 102);
  1952. }
  1953. static void
  1954. send_chunk_stringl(struct mg_connection *conn,
  1955. const char *chunk,
  1956. unsigned int chunk_len)
  1957. {
  1958. char lenbuf[16];
  1959. size_t lenbuf_len;
  1960. int ret;
  1961. /* First store the length information in a text buffer. */
  1962. sprintf(lenbuf, "%x\r\n", chunk_len);
  1963. lenbuf_len = strlen(lenbuf);
  1964. /* Then send length information, chunk and terminating \r\n. */
  1965. ret = mg_write(conn, lenbuf, lenbuf_len);
  1966. ck_assert_int_eq(ret, (int)lenbuf_len);
  1967. ret = mg_write(conn, chunk, chunk_len);
  1968. ck_assert_int_eq(ret, (int)chunk_len);
  1969. ret = mg_write(conn, "\r\n", 2);
  1970. ck_assert_int_eq(ret, 2);
  1971. }
  1972. static void
  1973. send_chunk_string(struct mg_connection *conn, const char *chunk)
  1974. {
  1975. send_chunk_stringl(conn, chunk, (unsigned int)strlen(chunk));
  1976. }
  1977. START_TEST(test_handle_form)
  1978. {
  1979. struct mg_context *ctx;
  1980. struct mg_connection *client_conn;
  1981. const struct mg_request_info *ri;
  1982. const char *OPTIONS[8];
  1983. const char *opt;
  1984. int opt_idx = 0;
  1985. char ebuf[100];
  1986. const char *multipart_body;
  1987. const char *boundary;
  1988. size_t body_len, body_sent, chunk_len;
  1989. int sleep_cnt;
  1990. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  1991. OPTIONS[opt_idx++] = "listening_ports";
  1992. OPTIONS[opt_idx++] = "8884";
  1993. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  1994. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  1995. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  1996. ctx = test_mg_start(NULL, &g_ctx, OPTIONS);
  1997. ck_assert(ctx != NULL);
  1998. g_ctx = ctx;
  1999. opt = mg_get_option(ctx, "listening_ports");
  2000. ck_assert_str_eq(opt, "8884");
  2001. mg_set_request_handler(ctx, "/handle_form", FormGet, (void *)0);
  2002. mg_set_request_handler(ctx, "/handle_form_store", FormStore1, (void *)0);
  2003. mg_set_request_handler(ctx, "/handle_form_store2", FormStore2, (void *)0);
  2004. test_sleep(1);
  2005. /* Handle form: "GET" */
  2006. client_conn = mg_download("localhost",
  2007. 8884,
  2008. 0,
  2009. ebuf,
  2010. sizeof(ebuf),
  2011. "%s",
  2012. "GET /handle_form"
  2013. "?textin=text&passwordin=&radio1=val1"
  2014. "&radio2=val1&check1=val1&numberin=1"
  2015. "&datein=1.1.2016&colorin=%2380ff00"
  2016. "&rangein=3&monthin=&weekin=&timein="
  2017. "&datetimen=&datetimelocalin=&emailin="
  2018. "&searchin=&telin=&urlin=&filein="
  2019. "&filesin=&selectin=opt1"
  2020. "&message=Text+area+default+text. "
  2021. "HTTP/1.0\r\n"
  2022. "Host: localhost:8884\r\n"
  2023. "Connection: close\r\n\r\n");
  2024. ck_assert(client_conn != NULL);
  2025. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2026. test_sleep(1);
  2027. if (g_field_step == 1000) {
  2028. break;
  2029. }
  2030. }
  2031. ri = mg_get_request_info(client_conn);
  2032. ck_assert(ri != NULL);
  2033. ck_assert_str_eq(ri->local_uri, "200");
  2034. mg_close_connection(client_conn);
  2035. /* Handle form: "POST x-www-form-urlencoded" */
  2036. client_conn =
  2037. mg_download("localhost",
  2038. 8884,
  2039. 0,
  2040. ebuf,
  2041. sizeof(ebuf),
  2042. "%s",
  2043. "POST /handle_form HTTP/1.1\r\n"
  2044. "Host: localhost:8884\r\n"
  2045. "Connection: close\r\n"
  2046. "Content-Type: application/x-www-form-urlencoded\r\n"
  2047. "Content-Length: 263\r\n"
  2048. "\r\n"
  2049. "textin=text&passwordin=&radio1=val1&radio2=val1"
  2050. "&check1=val1&numberin=1&datein=1.1.2016"
  2051. "&colorin=%2380ff00&rangein=3&monthin=&weekin="
  2052. "&timein=&datetimen=&datetimelocalin=&emailin="
  2053. "&searchin=&telin=&urlin=&filein=&filesin="
  2054. "&selectin=opt1&message=Text+area+default+text.");
  2055. ck_assert(client_conn != NULL);
  2056. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2057. test_sleep(1);
  2058. if (g_field_step == 1000) {
  2059. break;
  2060. }
  2061. }
  2062. ri = mg_get_request_info(client_conn);
  2063. ck_assert(ri != NULL);
  2064. ck_assert_str_eq(ri->local_uri, "200");
  2065. mg_close_connection(client_conn);
  2066. /* Handle form: "POST multipart/form-data" */
  2067. multipart_body =
  2068. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2069. "Content-Disposition: form-data; name=\"textin\"\r\n"
  2070. "\r\n"
  2071. "text\r\n"
  2072. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2073. "Content-Disposition: form-data; name=\"passwordin\"\r\n"
  2074. "\r\n"
  2075. "\r\n"
  2076. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2077. "Content-Disposition: form-data; name=\"radio1\"\r\n"
  2078. "\r\n"
  2079. "val1\r\n"
  2080. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2081. "Content-Disposition: form-data; name=radio2\r\n"
  2082. "\r\n"
  2083. "val1\r\n"
  2084. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2085. "Content-Disposition: form-data; name=\"check1\"\r\n"
  2086. "\r\n"
  2087. "val1\r\n"
  2088. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2089. "Content-Disposition: form-data; name=\"numberin\"\r\n"
  2090. "\r\n"
  2091. "1\r\n"
  2092. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2093. "Content-Disposition: form-data; name=\"datein\"\r\n"
  2094. "\r\n"
  2095. "1.1.2016\r\n"
  2096. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2097. "Content-Disposition: form-data; name=\"colorin\"\r\n"
  2098. "\r\n"
  2099. "#80ff00\r\n"
  2100. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2101. "Content-Disposition: form-data; name=\"rangein\"\r\n"
  2102. "\r\n"
  2103. "3\r\n"
  2104. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2105. "Content-Disposition: form-data; name=\"monthin\"\r\n"
  2106. "\r\n"
  2107. "\r\n"
  2108. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2109. "Content-Disposition: form-data; name=\"weekin\"\r\n"
  2110. "\r\n"
  2111. "\r\n"
  2112. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2113. "Content-Disposition: form-data; name=\"timein\"\r\n"
  2114. "\r\n"
  2115. "\r\n"
  2116. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2117. "Content-Disposition: form-data; name=\"datetimen\"\r\n"
  2118. "\r\n"
  2119. "\r\n"
  2120. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2121. "Content-Disposition: form-data; name=\"datetimelocalin\"\r\n"
  2122. "\r\n"
  2123. "\r\n"
  2124. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2125. "Content-Disposition: form-data; name=\"emailin\"\r\n"
  2126. "\r\n"
  2127. "\r\n"
  2128. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2129. "Content-Disposition: form-data; name=\"searchin\"\r\n"
  2130. "\r\n"
  2131. "\r\n"
  2132. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2133. "Content-Disposition: form-data; name=\"telin\"\r\n"
  2134. "\r\n"
  2135. "\r\n"
  2136. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2137. "Content-Disposition: form-data; name=\"urlin\"\r\n"
  2138. "\r\n"
  2139. "\r\n"
  2140. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2141. "Content-Disposition: form-data; name=\"filein\"; filename=\"\"\r\n"
  2142. "Content-Type: application/octet-stream\r\n"
  2143. "\r\n"
  2144. "\r\n"
  2145. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2146. "Content-Disposition: form-data; name=filesin; filename=\r\n"
  2147. "Content-Type: application/octet-stream\r\n"
  2148. "\r\n"
  2149. "\r\n"
  2150. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2151. "Content-Disposition: form-data; name=\"selectin\"\r\n"
  2152. "\r\n"
  2153. "opt1\r\n"
  2154. "--multipart-form-data-boundary--see-RFC-2388\r\n"
  2155. "Content-Disposition: form-data; name=\"message\"\r\n"
  2156. "\r\n"
  2157. "Text area default text.\r\n"
  2158. "--multipart-form-data-boundary--see-RFC-2388--\r\n";
  2159. body_len = strlen(multipart_body);
  2160. ck_assert_uint_eq(body_len, 2368); /* not required */
  2161. client_conn =
  2162. mg_download("localhost",
  2163. 8884,
  2164. 0,
  2165. ebuf,
  2166. sizeof(ebuf),
  2167. "POST /handle_form HTTP/1.1\r\n"
  2168. "Host: localhost:8884\r\n"
  2169. "Connection: close\r\n"
  2170. "Content-Type: multipart/form-data; "
  2171. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2172. "Content-Length: %u\r\n"
  2173. "\r\n%s",
  2174. (unsigned int)body_len,
  2175. multipart_body);
  2176. ck_assert(client_conn != NULL);
  2177. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2178. test_sleep(1);
  2179. if (g_field_step == 1000) {
  2180. break;
  2181. }
  2182. }
  2183. ri = mg_get_request_info(client_conn);
  2184. ck_assert(ri != NULL);
  2185. ck_assert_str_eq(ri->local_uri, "200");
  2186. mg_close_connection(client_conn);
  2187. /* Handle form: "POST multipart/form-data" with chunked transfer encoding */
  2188. client_conn =
  2189. mg_download("localhost",
  2190. 8884,
  2191. 0,
  2192. ebuf,
  2193. sizeof(ebuf),
  2194. "%s",
  2195. "POST /handle_form HTTP/1.1\r\n"
  2196. "Host: localhost:8884\r\n"
  2197. "Connection: close\r\n"
  2198. "Content-Type: multipart/form-data; "
  2199. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2200. "Transfer-Encoding: chunked\r\n"
  2201. "\r\n");
  2202. ck_assert(client_conn != NULL);
  2203. body_len = strlen(multipart_body);
  2204. chunk_len = 1;
  2205. body_sent = 0;
  2206. while (body_len > body_sent) {
  2207. if (chunk_len > (body_len - body_sent)) {
  2208. chunk_len = body_len - body_sent;
  2209. }
  2210. ck_assert_int_gt((int)chunk_len, 0);
  2211. mg_printf(client_conn, "%x\r\n", (unsigned int)chunk_len);
  2212. mg_write(client_conn, multipart_body + body_sent, chunk_len);
  2213. mg_printf(client_conn, "\r\n");
  2214. body_sent += chunk_len;
  2215. chunk_len = (chunk_len % 40) + 1;
  2216. }
  2217. mg_printf(client_conn, "0\r\n");
  2218. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2219. test_sleep(1);
  2220. if (g_field_step == 1000) {
  2221. break;
  2222. }
  2223. }
  2224. ri = mg_get_request_info(client_conn);
  2225. ck_assert(ri != NULL);
  2226. ck_assert_str_eq(ri->local_uri, "200");
  2227. mg_close_connection(client_conn);
  2228. /* Handle form: "POST multipart/form-data" with chunked transfer
  2229. * encoding, using a quoted boundary string */
  2230. client_conn = mg_download(
  2231. "localhost",
  2232. 8884,
  2233. 0,
  2234. ebuf,
  2235. sizeof(ebuf),
  2236. "%s",
  2237. "POST /handle_form HTTP/1.1\r\n"
  2238. "Host: localhost:8884\r\n"
  2239. "Connection: close\r\n"
  2240. "Content-Type: multipart/form-data; "
  2241. "boundary=\"multipart-form-data-boundary--see-RFC-2388\"\r\n"
  2242. "Transfer-Encoding: chunked\r\n"
  2243. "\r\n");
  2244. ck_assert(client_conn != NULL);
  2245. body_len = strlen(multipart_body);
  2246. chunk_len = 1;
  2247. body_sent = 0;
  2248. while (body_len > body_sent) {
  2249. if (chunk_len > (body_len - body_sent)) {
  2250. chunk_len = body_len - body_sent;
  2251. }
  2252. ck_assert_int_gt((int)chunk_len, 0);
  2253. mg_printf(client_conn, "%x\r\n", (unsigned int)chunk_len);
  2254. mg_write(client_conn, multipart_body + body_sent, chunk_len);
  2255. mg_printf(client_conn, "\r\n");
  2256. body_sent += chunk_len;
  2257. chunk_len = (chunk_len % 40) + 1;
  2258. }
  2259. mg_printf(client_conn, "0\r\n");
  2260. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2261. test_sleep(1);
  2262. if (g_field_step == 1000) {
  2263. break;
  2264. }
  2265. }
  2266. ri = mg_get_request_info(client_conn);
  2267. ck_assert(ri != NULL);
  2268. ck_assert_str_eq(ri->local_uri, "200");
  2269. mg_close_connection(client_conn);
  2270. /* Now test form_store */
  2271. /* First test with GET */
  2272. client_conn = mg_download("localhost",
  2273. 8884,
  2274. 0,
  2275. ebuf,
  2276. sizeof(ebuf),
  2277. "%s",
  2278. "GET /handle_form_store"
  2279. "?storeme=storetest"
  2280. "&continue_field_handler=ignore"
  2281. "&break_field_handler=abort"
  2282. "&dontread=xyz "
  2283. "HTTP/1.0\r\n"
  2284. "Host: localhost:8884\r\n"
  2285. "Connection: close\r\n\r\n");
  2286. ck_assert(client_conn != NULL);
  2287. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2288. test_sleep(1);
  2289. if (g_field_step == 1000) {
  2290. break;
  2291. }
  2292. }
  2293. ri = mg_get_request_info(client_conn);
  2294. ck_assert(ri != NULL);
  2295. ck_assert_str_eq(ri->local_uri, "200");
  2296. mg_close_connection(client_conn);
  2297. /* Handle form: "POST x-www-form-urlencoded", chunked, store */
  2298. client_conn =
  2299. mg_download("localhost",
  2300. 8884,
  2301. 0,
  2302. ebuf,
  2303. sizeof(ebuf),
  2304. "%s",
  2305. "POST /handle_form_store HTTP/1.0\r\n"
  2306. "Host: localhost:8884\r\n"
  2307. "Connection: close\r\n"
  2308. "Content-Type: application/x-www-form-urlencoded\r\n"
  2309. "Transfer-Encoding: chunked\r\n"
  2310. "\r\n");
  2311. ck_assert(client_conn != NULL);
  2312. send_chunk_string(client_conn, "storeme=store");
  2313. send_chunk_string(client_conn, "test&");
  2314. send_chunk_string(client_conn, "continue_field_handler=ignore");
  2315. send_chunk_string(client_conn, "&br");
  2316. test_sleep(1);
  2317. send_chunk_string(client_conn, "eak_field_handler=abort&");
  2318. send_chunk_string(client_conn, "dontread=xyz");
  2319. mg_printf(client_conn, "0\r\n");
  2320. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2321. test_sleep(1);
  2322. if (g_field_step == 1000) {
  2323. break;
  2324. }
  2325. }
  2326. ri = mg_get_request_info(client_conn);
  2327. ck_assert(ri != NULL);
  2328. ck_assert_str_eq(ri->local_uri, "200");
  2329. mg_close_connection(client_conn);
  2330. /* Handle form: "POST multipart/form-data", chunked, store */
  2331. client_conn =
  2332. mg_download("localhost",
  2333. 8884,
  2334. 0,
  2335. ebuf,
  2336. sizeof(ebuf),
  2337. "%s",
  2338. "POST /handle_form_store HTTP/1.0\r\n"
  2339. "Host: localhost:8884\r\n"
  2340. "Connection: close\r\n"
  2341. "Content-Type: multipart/form-data; "
  2342. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2343. "Transfer-Encoding: chunked\r\n"
  2344. "\r\n");
  2345. ck_assert(client_conn != NULL);
  2346. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2347. send_chunk_string(client_conn, "--see-RFC-2388\r\n");
  2348. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2349. send_chunk_string(client_conn, "name=\"storeme\"\r\n");
  2350. send_chunk_string(client_conn, "\r\n");
  2351. send_chunk_string(client_conn, "storetest\r\n");
  2352. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2353. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2354. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2355. send_chunk_string(client_conn, "name=\"continue_field_handler\"\r\n");
  2356. send_chunk_string(client_conn, "\r\n");
  2357. send_chunk_string(client_conn, "ignore\r\n");
  2358. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2359. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2360. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2361. send_chunk_string(client_conn, "name=\"break_field_handler\"\r\n");
  2362. send_chunk_string(client_conn, "\r\n");
  2363. send_chunk_string(client_conn, "abort\r\n");
  2364. send_chunk_string(client_conn, "--multipart-form-data-boundary-");
  2365. send_chunk_string(client_conn, "-see-RFC-2388\r\n");
  2366. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2367. send_chunk_string(client_conn, "name=\"dontread\"\r\n");
  2368. send_chunk_string(client_conn, "\r\n");
  2369. send_chunk_string(client_conn, "xyz\r\n");
  2370. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2371. send_chunk_string(client_conn, "--see-RFC-2388--\r\n");
  2372. mg_printf(client_conn, "0\r\n");
  2373. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2374. test_sleep(1);
  2375. if (g_field_step == 1000) {
  2376. break;
  2377. }
  2378. }
  2379. ri = mg_get_request_info(client_conn);
  2380. ck_assert(ri != NULL);
  2381. ck_assert_str_eq(ri->local_uri, "200");
  2382. mg_close_connection(client_conn);
  2383. /* Handle form: "POST multipart/form-data", chunked, store, with files */
  2384. client_conn =
  2385. mg_download("localhost",
  2386. 8884,
  2387. 0,
  2388. ebuf,
  2389. sizeof(ebuf),
  2390. "%s",
  2391. "POST /handle_form_store2 HTTP/1.0\r\n"
  2392. "Host: localhost:8884\r\n"
  2393. "Connection: close\r\n"
  2394. "Content-Type: multipart/form-data; "
  2395. "boundary=multipart-form-data-boundary--see-RFC-2388\r\n"
  2396. "Transfer-Encoding: chunked\r\n"
  2397. "\r\n");
  2398. ck_assert(client_conn != NULL);
  2399. boundary = "--multipart-form-data-boundary--see-RFC-2388\r\n";
  2400. send_chunk_string(client_conn, boundary);
  2401. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2402. send_chunk_string(client_conn, "name=\"storeme\"\r\n");
  2403. send_chunk_string(client_conn, "\r\n");
  2404. send_chunk_string(client_conn, "storetest\r\n");
  2405. send_chunk_string(client_conn, boundary);
  2406. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2407. send_chunk_string(client_conn, "name=\"continue_field_handler\";");
  2408. send_chunk_string(client_conn, "filename=\"file_ignored.txt\"\r\n");
  2409. send_chunk_string(client_conn, "Content-Type: ");
  2410. send_chunk_string(client_conn, "application/octet-stream\r\n");
  2411. send_chunk_string(client_conn, "X-Ignored-Header: xyz\r\n");
  2412. send_chunk_string(client_conn, "\r\n");
  2413. /* send some kilobyte of data */
  2414. /* sending megabytes to localhost does not allways work in CI test
  2415. * environments (depending on the network stack) */
  2416. body_sent = 0;
  2417. do {
  2418. send_chunk_string(client_conn, "ignore\r\n");
  2419. body_sent += 8;
  2420. /* send some strings that are almost boundaries */
  2421. for (chunk_len = 1; chunk_len < strlen(boundary); chunk_len++) {
  2422. /* chunks from 1 byte to strlen(boundary)-1 */
  2423. send_chunk_stringl(client_conn, boundary, (unsigned int)chunk_len);
  2424. body_sent += chunk_len;
  2425. }
  2426. } while (body_sent < 8 * 1024);
  2427. send_chunk_string(client_conn, "\r\n");
  2428. send_chunk_string(client_conn, boundary);
  2429. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2430. send_chunk_string(client_conn, "name=\"file2store\";");
  2431. send_chunk_string(client_conn, "filename=\"myfile.txt\"\r\n");
  2432. send_chunk_string(client_conn, "Content-Type: ");
  2433. send_chunk_string(client_conn, "application/octet-stream\r\n");
  2434. send_chunk_string(client_conn, "X-Ignored-Header: xyz\r\n");
  2435. send_chunk_string(client_conn, "\r\n");
  2436. for (body_sent = 0; (int)body_sent < (int)myfile_content_rep; body_sent++) {
  2437. send_chunk_string(client_conn, myfile_content);
  2438. }
  2439. send_chunk_string(client_conn, "\r\n");
  2440. send_chunk_string(client_conn, boundary);
  2441. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2442. send_chunk_string(client_conn, "name=\"break_field_handler\"\r\n");
  2443. send_chunk_string(client_conn, "\r\n");
  2444. send_chunk_string(client_conn, "abort\r\n");
  2445. send_chunk_string(client_conn, boundary);
  2446. send_chunk_string(client_conn, "Content-Disposition: form-data; ");
  2447. send_chunk_string(client_conn, "name=\"dontread\"\r\n");
  2448. send_chunk_string(client_conn, "\r\n");
  2449. send_chunk_string(client_conn, "xyz\r\n");
  2450. send_chunk_string(client_conn, "--multipart-form-data-boundary");
  2451. send_chunk_string(client_conn, "--see-RFC-2388--\r\n");
  2452. mg_printf(client_conn, "0\r\n");
  2453. for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) {
  2454. test_sleep(1);
  2455. if (g_field_step == 1000) {
  2456. break;
  2457. }
  2458. }
  2459. ri = mg_get_request_info(client_conn);
  2460. ck_assert(ri != NULL);
  2461. ck_assert_str_eq(ri->local_uri, "200");
  2462. mg_close_connection(client_conn);
  2463. /* Close the server */
  2464. g_ctx = NULL;
  2465. test_mg_stop(ctx);
  2466. mark_point();
  2467. }
  2468. END_TEST
  2469. START_TEST(test_http_auth)
  2470. {
  2471. #if !defined(NO_FILES)
  2472. const char *OPTIONS[] = {
  2473. "document_root",
  2474. ".",
  2475. "listening_ports",
  2476. "8080",
  2477. #if !defined(NO_CACHING)
  2478. "static_file_max_age",
  2479. "0",
  2480. #endif
  2481. "put_delete_auth_file",
  2482. "put_delete_auth_file.csv",
  2483. NULL,
  2484. };
  2485. struct mg_context *ctx;
  2486. struct mg_connection *client_conn;
  2487. char client_err[256], nonce[256];
  2488. const struct mg_request_info *client_ri;
  2489. int client_res;
  2490. FILE *f;
  2491. const char *passwd_file = ".htpasswd";
  2492. const char *test_file = "test_http_auth.test_file.txt";
  2493. const char *test_content = "test_http_auth test_file content";
  2494. const char *domain;
  2495. const char *doc_root;
  2496. const char *auth_request;
  2497. const char *str;
  2498. size_t len;
  2499. int i;
  2500. char HA1[256], HA2[256], HA[256];
  2501. char HA1_md5_buf[33], HA2_md5_buf[33], HA_md5_buf[33];
  2502. char *HA1_md5_ret, *HA2_md5_ret, *HA_md5_ret;
  2503. const char *nc = "00000001";
  2504. const char *cnonce = "6789ABCD";
  2505. /* Start with default options */
  2506. ctx = test_mg_start(NULL, NULL, OPTIONS);
  2507. ck_assert(ctx != NULL);
  2508. domain = mg_get_option(ctx, "authentication_domain");
  2509. ck_assert(domain != NULL);
  2510. len = strlen(domain);
  2511. ck_assert_uint_gt(len, 0);
  2512. ck_assert_uint_lt(len, 64);
  2513. doc_root = mg_get_option(ctx, "document_root");
  2514. ck_assert_str_eq(doc_root, ".");
  2515. /* Create a default file in the document root */
  2516. f = fopen(test_file, "w");
  2517. if (f) {
  2518. fprintf(f, "%s", test_content);
  2519. fclose(f);
  2520. } else {
  2521. ck_abort_msg("Cannot create file %s", test_file);
  2522. }
  2523. (void)remove(passwd_file);
  2524. (void)remove("put_delete_auth_file.csv");
  2525. client_res = mg_modify_passwords_file("put_delete_auth_file.csv",
  2526. domain,
  2527. "admin",
  2528. "adminpass");
  2529. ck_assert_int_eq(client_res, 1);
  2530. /* Read file before a .htpasswd file has been created */
  2531. memset(client_err, 0, sizeof(client_err));
  2532. client_conn =
  2533. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2534. ck_assert(client_conn != NULL);
  2535. ck_assert_str_eq(client_err, "");
  2536. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2537. client_res =
  2538. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2539. ck_assert_int_ge(client_res, 0);
  2540. ck_assert_str_eq(client_err, "");
  2541. client_ri = mg_get_request_info(client_conn);
  2542. ck_assert(client_ri != NULL);
  2543. ck_assert_str_eq(client_ri->local_uri, "200");
  2544. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2545. ck_assert_int_gt(client_res, 0);
  2546. ck_assert_int_le(client_res, sizeof(client_err));
  2547. ck_assert_str_eq(client_err, test_content);
  2548. mg_close_connection(client_conn);
  2549. test_sleep(1);
  2550. /* Create a .htpasswd file */
  2551. client_res = mg_modify_passwords_file(passwd_file, domain, "user", "pass");
  2552. ck_assert_int_eq(client_res, 1);
  2553. client_res = mg_modify_passwords_file(NULL, domain, "user", "pass");
  2554. ck_assert_int_eq(client_res, 0); /* Filename is required */
  2555. test_sleep(1);
  2556. /* Repeat test after .htpasswd is created */
  2557. memset(client_err, 0, sizeof(client_err));
  2558. client_conn =
  2559. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2560. ck_assert(client_conn != NULL);
  2561. ck_assert_str_eq(client_err, "");
  2562. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2563. client_res =
  2564. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2565. ck_assert_int_ge(client_res, 0);
  2566. ck_assert_str_eq(client_err, "");
  2567. client_ri = mg_get_request_info(client_conn);
  2568. ck_assert(client_ri != NULL);
  2569. ck_assert_str_eq(client_ri->local_uri, "401");
  2570. auth_request = NULL;
  2571. for (i = 0; i < client_ri->num_headers; i++) {
  2572. if (!mg_strcasecmp(client_ri->http_headers[i].name,
  2573. "WWW-Authenticate")) {
  2574. ck_assert_ptr_eq(auth_request, NULL);
  2575. auth_request = client_ri->http_headers[i].value;
  2576. ck_assert_ptr_ne(auth_request, NULL);
  2577. }
  2578. }
  2579. ck_assert_ptr_ne(auth_request, NULL);
  2580. str = "Digest qop=\"auth\", realm=\"";
  2581. len = strlen(str);
  2582. ck_assert(!mg_strncasecmp(auth_request, str, len));
  2583. ck_assert(!strncmp(auth_request + len, domain, strlen(domain)));
  2584. len += strlen(domain);
  2585. str = "\", nonce=\"";
  2586. ck_assert(!strncmp(auth_request + len, str, strlen(str)));
  2587. len += strlen(str);
  2588. str = strchr(auth_request + len, '\"');
  2589. ck_assert_ptr_ne(str, NULL);
  2590. ck_assert_ptr_ne(str, auth_request + len);
  2591. /* nonce is from including (auth_request + len) to excluding (str) */
  2592. ck_assert_int_gt((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len), 0);
  2593. ck_assert_int_lt((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len),
  2594. (ptrdiff_t)sizeof(nonce));
  2595. memset(nonce, 0, sizeof(nonce));
  2596. memcpy(nonce,
  2597. auth_request + len,
  2598. (size_t)((ptrdiff_t)(str) - (ptrdiff_t)(auth_request + len)));
  2599. memset(HA1, 0, sizeof(HA1));
  2600. memset(HA2, 0, sizeof(HA2));
  2601. memset(HA, 0, sizeof(HA));
  2602. memset(HA1_md5_buf, 0, sizeof(HA1_md5_buf));
  2603. memset(HA2_md5_buf, 0, sizeof(HA2_md5_buf));
  2604. memset(HA_md5_buf, 0, sizeof(HA_md5_buf));
  2605. sprintf(HA1, "%s:%s:%s", "user", domain, "pass");
  2606. sprintf(HA2, "%s:/%s", "GET", test_file);
  2607. HA1_md5_ret = mg_md5(HA1_md5_buf, HA1, NULL);
  2608. HA2_md5_ret = mg_md5(HA2_md5_buf, HA2, NULL);
  2609. ck_assert_ptr_eq(HA1_md5_ret, HA1_md5_buf);
  2610. ck_assert_ptr_eq(HA2_md5_ret, HA2_md5_buf);
  2611. HA_md5_ret = mg_md5(HA_md5_buf, "user", ":", domain, ":", "pass", NULL);
  2612. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2613. ck_assert_str_eq(HA1_md5_ret, HA_md5_buf);
  2614. HA_md5_ret = mg_md5(HA_md5_buf, "GET", ":", "/", test_file, NULL);
  2615. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2616. ck_assert_str_eq(HA2_md5_ret, HA_md5_buf);
  2617. HA_md5_ret = mg_md5(HA_md5_buf,
  2618. HA1_md5_buf,
  2619. ":",
  2620. nonce,
  2621. ":",
  2622. nc,
  2623. ":",
  2624. cnonce,
  2625. ":",
  2626. "auth",
  2627. ":",
  2628. HA2_md5_buf,
  2629. NULL);
  2630. ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf);
  2631. /* Retry with authorization */
  2632. memset(client_err, 0, sizeof(client_err));
  2633. client_conn =
  2634. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2635. ck_assert(client_conn != NULL);
  2636. ck_assert_str_eq(client_err, "");
  2637. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n", test_file);
  2638. mg_printf(client_conn,
  2639. "Authorization: Digest "
  2640. "username=\"%s\", "
  2641. "realm=\"%s\", "
  2642. "nonce=\"%s\", "
  2643. "uri=\"/%s\", "
  2644. "qop=auth, "
  2645. "nc=%s, "
  2646. "cnonce=\"%s\", "
  2647. "response=\"%s\"\r\n\r\n",
  2648. "user",
  2649. domain,
  2650. nonce,
  2651. test_file,
  2652. nc,
  2653. cnonce,
  2654. HA_md5_buf);
  2655. client_res =
  2656. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2657. ck_assert_int_ge(client_res, 0);
  2658. ck_assert_str_eq(client_err, "");
  2659. client_ri = mg_get_request_info(client_conn);
  2660. ck_assert(client_ri != NULL);
  2661. ck_assert_str_eq(client_ri->local_uri, "200");
  2662. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2663. ck_assert_int_gt(client_res, 0);
  2664. ck_assert_int_le(client_res, sizeof(client_err));
  2665. ck_assert_str_eq(client_err, test_content);
  2666. mg_close_connection(client_conn);
  2667. test_sleep(1);
  2668. /* Retry DELETE with authorization of a user not authorized for DELETE */
  2669. memset(client_err, 0, sizeof(client_err));
  2670. client_conn =
  2671. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2672. ck_assert(client_conn != NULL);
  2673. ck_assert_str_eq(client_err, "");
  2674. mg_printf(client_conn, "DELETE /%s HTTP/1.0\r\n", test_file);
  2675. mg_printf(client_conn,
  2676. "Authorization: Digest "
  2677. "username=\"%s\", "
  2678. "realm=\"%s\", "
  2679. "nonce=\"%s\", "
  2680. "uri=\"/%s\", "
  2681. "qop=auth, "
  2682. "nc=%s, "
  2683. "cnonce=\"%s\", "
  2684. "response=\"%s\"\r\n\r\n",
  2685. "user",
  2686. domain,
  2687. nonce,
  2688. test_file,
  2689. nc,
  2690. cnonce,
  2691. HA_md5_buf);
  2692. client_res =
  2693. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2694. ck_assert_int_ge(client_res, 0);
  2695. ck_assert_str_eq(client_err, "");
  2696. client_ri = mg_get_request_info(client_conn);
  2697. ck_assert(client_ri != NULL);
  2698. ck_assert_str_eq(client_ri->local_uri, "401");
  2699. mg_close_connection(client_conn);
  2700. test_sleep(1);
  2701. /* Remove the user from the .htpasswd file again */
  2702. client_res = mg_modify_passwords_file(passwd_file, domain, "user", NULL);
  2703. ck_assert_int_eq(client_res, 1);
  2704. test_sleep(1);
  2705. /* Try to access the file again. Expected: 401 error */
  2706. memset(client_err, 0, sizeof(client_err));
  2707. client_conn =
  2708. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2709. ck_assert(client_conn != NULL);
  2710. ck_assert_str_eq(client_err, "");
  2711. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2712. client_res =
  2713. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2714. ck_assert_int_ge(client_res, 0);
  2715. ck_assert_str_eq(client_err, "");
  2716. client_ri = mg_get_request_info(client_conn);
  2717. ck_assert(client_ri != NULL);
  2718. ck_assert_str_eq(client_ri->local_uri, "401");
  2719. mg_close_connection(client_conn);
  2720. test_sleep(1);
  2721. /* Now remove the password file */
  2722. (void)remove(passwd_file);
  2723. test_sleep(1);
  2724. /* Access to the file must work like before */
  2725. memset(client_err, 0, sizeof(client_err));
  2726. client_conn =
  2727. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2728. ck_assert(client_conn != NULL);
  2729. ck_assert_str_eq(client_err, "");
  2730. mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
  2731. client_res =
  2732. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2733. ck_assert_int_ge(client_res, 0);
  2734. ck_assert_str_eq(client_err, "");
  2735. client_ri = mg_get_request_info(client_conn);
  2736. ck_assert(client_ri != NULL);
  2737. ck_assert_str_eq(client_ri->local_uri, "200");
  2738. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2739. ck_assert_int_gt(client_res, 0);
  2740. ck_assert_int_le(client_res, sizeof(client_err));
  2741. ck_assert_str_eq(client_err, test_content);
  2742. mg_close_connection(client_conn);
  2743. test_sleep(1);
  2744. /* Stop the server and clean up */
  2745. test_mg_stop(ctx);
  2746. (void)remove(test_file);
  2747. (void)remove(passwd_file);
  2748. (void)remove("put_delete_auth_file.csv");
  2749. #endif
  2750. }
  2751. END_TEST
  2752. START_TEST(test_keep_alive)
  2753. {
  2754. struct mg_context *ctx;
  2755. const char *OPTIONS[] =
  2756. { "listening_ports",
  2757. "8081",
  2758. "request_timeout_ms",
  2759. "10000",
  2760. "enable_keep_alive",
  2761. "yes",
  2762. #if !defined(NO_FILES)
  2763. "document_root",
  2764. ".",
  2765. "enable_directory_listing",
  2766. "no",
  2767. #endif
  2768. NULL };
  2769. struct mg_connection *client_conn;
  2770. char client_err[256];
  2771. const struct mg_request_info *client_ri;
  2772. int client_res, i;
  2773. const char *connection_header;
  2774. ctx = test_mg_start(NULL, NULL, OPTIONS);
  2775. ck_assert(ctx != NULL);
  2776. /* HTTP 1.1 GET request */
  2777. memset(client_err, 0, sizeof(client_err));
  2778. client_conn =
  2779. mg_connect_client("127.0.0.1", 8081, 0, client_err, sizeof(client_err));
  2780. ck_assert(client_conn != NULL);
  2781. ck_assert_str_eq(client_err, "");
  2782. mg_printf(client_conn,
  2783. "GET / HTTP/1.1\r\nHost: "
  2784. "localhost:8081\r\nConnection: keep-alive\r\n\r\n");
  2785. client_res =
  2786. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2787. ck_assert_int_ge(client_res, 0);
  2788. ck_assert_str_eq(client_err, "");
  2789. client_ri = mg_get_request_info(client_conn);
  2790. ck_assert(client_ri != NULL);
  2791. #if defined(NO_FILES)
  2792. ck_assert_str_eq(client_ri->local_uri, "404");
  2793. #else
  2794. ck_assert_str_eq(client_ri->local_uri, "403");
  2795. #endif
  2796. connection_header = 0;
  2797. for (i = 0; i < client_ri->num_headers; i++) {
  2798. if (!mg_strcasecmp(client_ri->http_headers[i].name, "Connection")) {
  2799. ck_assert_ptr_eq(connection_header, NULL);
  2800. connection_header = client_ri->http_headers[i].value;
  2801. ck_assert_ptr_ne(connection_header, NULL);
  2802. }
  2803. }
  2804. /* Error replies will close the connection, even if keep-alive is set. */
  2805. ck_assert_ptr_ne(connection_header, NULL);
  2806. ck_assert_str_eq(connection_header, "close");
  2807. mg_close_connection(client_conn);
  2808. test_sleep(1);
  2809. /* TODO: request a file and keep alive
  2810. * (will only work if NO_FILES is not set). */
  2811. /* Stop the server and clean up */
  2812. test_mg_stop(ctx);
  2813. }
  2814. END_TEST
  2815. START_TEST(test_error_handling)
  2816. {
  2817. struct mg_context *ctx;
  2818. FILE *f;
  2819. char bad_thread_num[32] = "badnumber";
  2820. struct mg_callbacks callbacks;
  2821. char errmsg[256];
  2822. struct mg_connection *client_conn;
  2823. char client_err[256];
  2824. const struct mg_request_info *client_ri;
  2825. int client_res, i;
  2826. const char *OPTIONS[32];
  2827. int opt_cnt = 0;
  2828. #if !defined(NO_FILES)
  2829. OPTIONS[opt_cnt++] = "document_root";
  2830. OPTIONS[opt_cnt++] = ".";
  2831. #endif
  2832. OPTIONS[opt_cnt++] = "error_pages";
  2833. OPTIONS[opt_cnt++] = "./";
  2834. OPTIONS[opt_cnt++] = "listening_ports";
  2835. OPTIONS[opt_cnt++] = "8080";
  2836. OPTIONS[opt_cnt++] = "num_threads";
  2837. OPTIONS[opt_cnt++] = bad_thread_num;
  2838. OPTIONS[opt_cnt++] = "unknown_option";
  2839. OPTIONS[opt_cnt++] = "unknown_option_value";
  2840. OPTIONS[opt_cnt] = NULL;
  2841. memset(&callbacks, 0, sizeof(callbacks));
  2842. callbacks.log_message = log_msg_func;
  2843. /* test with unknown option */
  2844. memset(errmsg, 0, sizeof(errmsg));
  2845. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  2846. /* Details of errmsg may vary, but it may not be empty */
  2847. ck_assert_str_ne(errmsg, "");
  2848. ck_assert(ctx == NULL);
  2849. ck_assert_str_eq(errmsg, "Invalid option: unknown_option");
  2850. /* Remove invalid option */
  2851. for (i = 0; OPTIONS[i]; i++) {
  2852. if (strstr(OPTIONS[i], "unknown_option")) {
  2853. OPTIONS[i] = 0;
  2854. }
  2855. }
  2856. /* Test with bad num_thread option */
  2857. memset(errmsg, 0, sizeof(errmsg));
  2858. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  2859. /* Details of errmsg may vary, but it may not be empty */
  2860. ck_assert_str_ne(errmsg, "");
  2861. ck_assert(ctx == NULL);
  2862. ck_assert_str_eq(errmsg, "Invalid number of worker threads");
  2863. /* Set to a number - but use a number above the limit */
  2864. #ifdef MAX_WORKER_THREADS
  2865. sprintf(bad_thread_num, "%u", MAX_WORKER_THREADS + 1);
  2866. #else
  2867. sprintf(bad_thread_num, "%lu", 1000000000lu);
  2868. #endif
  2869. /* Test with bad num_thread option */
  2870. memset(errmsg, 0, sizeof(errmsg));
  2871. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  2872. /* Details of errmsg may vary, but it may not be empty */
  2873. ck_assert_str_ne(errmsg, "");
  2874. ck_assert(ctx == NULL);
  2875. ck_assert_str_eq(errmsg, "Too many worker threads");
  2876. /* HTTP 1.0 GET request - server is not running */
  2877. memset(client_err, 0, sizeof(client_err));
  2878. client_conn =
  2879. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2880. ck_assert(client_conn == NULL);
  2881. /* Error message detail may vary - it may not be empty ans should contain
  2882. * some information "connect" failed */
  2883. ck_assert_str_ne(client_err, "");
  2884. ck_assert(strstr(client_err, "connect"));
  2885. /* This time start the server with a valid configuration */
  2886. sprintf(bad_thread_num, "%i", 1);
  2887. memset(errmsg, 0, sizeof(errmsg));
  2888. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  2889. ck_assert_str_eq(errmsg, "");
  2890. ck_assert(ctx != NULL);
  2891. /* Server is running now */
  2892. test_sleep(1);
  2893. /* Remove error files (in case they exist) */
  2894. (void)remove("error.htm");
  2895. (void)remove("error4xx.htm");
  2896. (void)remove("error404.htm");
  2897. /* Ask for something not existing - should get default 404 */
  2898. memset(client_err, 0, sizeof(client_err));
  2899. client_conn =
  2900. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2901. ck_assert(client_conn != NULL);
  2902. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  2903. client_res =
  2904. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2905. ck_assert_int_ge(client_res, 0);
  2906. ck_assert_str_eq(client_err, "");
  2907. client_ri = mg_get_request_info(client_conn);
  2908. ck_assert(client_ri != NULL);
  2909. ck_assert_str_eq(client_ri->local_uri, "404");
  2910. mg_close_connection(client_conn);
  2911. test_sleep(1);
  2912. /* Create an error.htm file */
  2913. f = fopen("error.htm", "wt");
  2914. ck_assert(f != NULL);
  2915. (void)fprintf(f, "err-all");
  2916. (void)fclose(f);
  2917. /* Ask for something not existing - should get error.htm */
  2918. memset(client_err, 0, sizeof(client_err));
  2919. client_conn =
  2920. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2921. ck_assert(client_conn != NULL);
  2922. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  2923. client_res =
  2924. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2925. ck_assert_int_ge(client_res, 0);
  2926. ck_assert_str_eq(client_err, "");
  2927. client_ri = mg_get_request_info(client_conn);
  2928. ck_assert(client_ri != NULL);
  2929. ck_assert_str_eq(client_ri->local_uri, "200");
  2930. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2931. mg_close_connection(client_conn);
  2932. ck_assert_int_eq(client_res, 7);
  2933. client_err[8] = 0;
  2934. ck_assert_str_eq(client_err, "err-all");
  2935. test_sleep(1);
  2936. /* Create an error4xx.htm file */
  2937. f = fopen("error4xx.htm", "wt");
  2938. ck_assert(f != NULL);
  2939. (void)fprintf(f, "err-4xx");
  2940. (void)fclose(f);
  2941. /* Ask for something not existing - should get error4xx.htm */
  2942. memset(client_err, 0, sizeof(client_err));
  2943. client_conn =
  2944. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2945. ck_assert(client_conn != NULL);
  2946. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  2947. client_res =
  2948. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2949. ck_assert_int_ge(client_res, 0);
  2950. ck_assert_str_eq(client_err, "");
  2951. client_ri = mg_get_request_info(client_conn);
  2952. ck_assert(client_ri != NULL);
  2953. ck_assert_str_eq(client_ri->local_uri, "200");
  2954. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2955. mg_close_connection(client_conn);
  2956. ck_assert_int_eq(client_res, 7);
  2957. client_err[8] = 0;
  2958. ck_assert_str_eq(client_err, "err-4xx");
  2959. test_sleep(1);
  2960. /* Create an error404.htm file */
  2961. f = fopen("error404.htm", "wt");
  2962. ck_assert(f != NULL);
  2963. (void)fprintf(f, "err-404");
  2964. (void)fclose(f);
  2965. /* Ask for something not existing - should get error404.htm */
  2966. memset(client_err, 0, sizeof(client_err));
  2967. client_conn =
  2968. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2969. ck_assert(client_conn != NULL);
  2970. mg_printf(client_conn, "GET /something/not/existing HTTP/1.0\r\n\r\n");
  2971. client_res =
  2972. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2973. ck_assert_int_ge(client_res, 0);
  2974. ck_assert_str_eq(client_err, "");
  2975. client_ri = mg_get_request_info(client_conn);
  2976. ck_assert(client_ri != NULL);
  2977. ck_assert_str_eq(client_ri->local_uri, "200");
  2978. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2979. mg_close_connection(client_conn);
  2980. ck_assert_int_eq(client_res, 7);
  2981. client_err[8] = 0;
  2982. ck_assert_str_eq(client_err, "err-404");
  2983. test_sleep(1);
  2984. /* Ask in a malformed way - should get error4xx.htm */
  2985. memset(client_err, 0, sizeof(client_err));
  2986. client_conn =
  2987. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  2988. ck_assert(client_conn != NULL);
  2989. mg_printf(client_conn, "Gimme some file!\r\n\r\n");
  2990. client_res =
  2991. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  2992. ck_assert_int_ge(client_res, 0);
  2993. ck_assert_str_eq(client_err, "");
  2994. client_ri = mg_get_request_info(client_conn);
  2995. ck_assert(client_ri != NULL);
  2996. ck_assert_str_eq(client_ri->local_uri, "200");
  2997. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  2998. mg_close_connection(client_conn);
  2999. ck_assert_int_eq(client_res, 7);
  3000. client_err[8] = 0;
  3001. ck_assert_str_eq(client_err, "err-4xx");
  3002. test_sleep(1);
  3003. /* Remove all error files created by this test */
  3004. (void)remove("error.htm");
  3005. (void)remove("error4xx.htm");
  3006. (void)remove("error404.htm");
  3007. /* Stop the server */
  3008. test_mg_stop(ctx);
  3009. /* HTTP 1.1 GET request - must not work, since server is already stopped */
  3010. memset(client_err, 0, sizeof(client_err));
  3011. client_conn =
  3012. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  3013. ck_assert(client_conn == NULL);
  3014. ck_assert_str_ne(client_err, "");
  3015. test_sleep(1);
  3016. }
  3017. END_TEST
  3018. START_TEST(test_error_log_file)
  3019. {
  3020. /* Server var */
  3021. struct mg_context *ctx;
  3022. const char *OPTIONS[32];
  3023. int opt_cnt = 0;
  3024. /* Client var */
  3025. struct mg_connection *client;
  3026. char client_err_buf[256];
  3027. char client_data_buf[256];
  3028. const struct mg_request_info *client_ri;
  3029. /* File content check var */
  3030. FILE *f;
  3031. char buf[1024];
  3032. int len, ok;
  3033. /* Set options and start server */
  3034. OPTIONS[opt_cnt++] = "listening_ports";
  3035. OPTIONS[opt_cnt++] = "8080";
  3036. OPTIONS[opt_cnt++] = "error_log_file";
  3037. OPTIONS[opt_cnt++] = "error.log";
  3038. OPTIONS[opt_cnt++] = "access_log_file";
  3039. OPTIONS[opt_cnt++] = "access.log";
  3040. #if !defined(NO_FILES)
  3041. OPTIONS[opt_cnt++] = "document_root";
  3042. OPTIONS[opt_cnt++] = ".";
  3043. #endif
  3044. OPTIONS[opt_cnt] = NULL;
  3045. ctx = test_mg_start(NULL, 0, OPTIONS);
  3046. ck_assert(ctx != NULL);
  3047. /* Remove log files (they may exist from previous incomplete runs of
  3048. * this test) */
  3049. (void)remove("error.log");
  3050. (void)remove("access.log");
  3051. /* connect client */
  3052. memset(client_err_buf, 0, sizeof(client_err_buf));
  3053. memset(client_data_buf, 0, sizeof(client_data_buf));
  3054. client = mg_download("127.0.0.1",
  3055. 8080,
  3056. 0,
  3057. client_err_buf,
  3058. sizeof(client_err_buf),
  3059. "GET /not_existing_file.ext HTTP/1.0\r\n\r\n");
  3060. ck_assert(ctx != NULL);
  3061. ck_assert_str_eq(client_err_buf, "");
  3062. client_ri = mg_get_request_info(client);
  3063. ck_assert(client_ri != NULL);
  3064. ck_assert_str_eq(client_ri->local_uri, "404");
  3065. /* Close the client connection */
  3066. mg_close_connection(client);
  3067. /* Stop the server */
  3068. test_mg_stop(ctx);
  3069. /* Check access.log */
  3070. memset(buf, 0, sizeof(buf));
  3071. f = fopen("access.log", "r");
  3072. ck_assert_msg(f != NULL, "Cannot open access log file");
  3073. ok = (NULL != fgets(buf, sizeof(buf) - 1, f));
  3074. (void)fclose(f);
  3075. ck_assert_msg(ok, "Cannot read access log file");
  3076. len = (int)strlen(buf);
  3077. ck_assert_int_gt(len, 0);
  3078. ok = (NULL != strstr(buf, "not_existing_file.ext"));
  3079. ck_assert_msg(ok, "Did not find uri in access log file");
  3080. ok = (NULL != strstr(buf, "404"));
  3081. ck_assert_msg(ok, "Did not find HTTP status code in access log file");
  3082. /* Check error.log */
  3083. memset(buf, 0, sizeof(buf));
  3084. f = fopen("error.log", "r");
  3085. if (f) {
  3086. (void)fgets(buf, sizeof(buf) - 1, f);
  3087. fclose(f);
  3088. }
  3089. ck_assert_msg(f == NULL,
  3090. "Should not create error log file on 404, but got [%s]",
  3091. buf);
  3092. /* Remove log files */
  3093. (void)remove("error.log");
  3094. (void)remove("access.log");
  3095. /* Start server with bad options */
  3096. ck_assert_str_eq(OPTIONS[0], "listening_ports");
  3097. OPTIONS[1] = "bad port syntax";
  3098. ctx = test_mg_start(NULL, 0, OPTIONS);
  3099. ck_assert_msg(
  3100. ctx == NULL,
  3101. "Should not be able to start server with bad port configuration");
  3102. /* Check access.log */
  3103. memset(buf, 0, sizeof(buf));
  3104. f = fopen("access.log", "r");
  3105. if (f) {
  3106. (void)fgets(buf, sizeof(buf) - 1, f);
  3107. fclose(f);
  3108. }
  3109. ck_assert_msg(
  3110. f == NULL,
  3111. "Should not create access log file if start fails, but got [%s]",
  3112. buf);
  3113. /* Check error.log */
  3114. memset(buf, 0, sizeof(buf));
  3115. f = fopen("error.log", "r");
  3116. ck_assert_msg(f != NULL, "Cannot open access log file");
  3117. ok = (NULL != fgets(buf, sizeof(buf) - 1, f));
  3118. (void)fclose(f);
  3119. ck_assert_msg(ok, "Cannot read access log file");
  3120. len = (int)strlen(buf);
  3121. ck_assert_int_gt(len, 0);
  3122. ok = (NULL != strstr(buf, "port"));
  3123. ck_assert_msg(ok, "Did not find port as error reason in error log file");
  3124. /* Remove log files */
  3125. (void)remove("error.log");
  3126. (void)remove("access.log");
  3127. }
  3128. END_TEST
  3129. static int
  3130. test_throttle_begin_request(struct mg_connection *conn)
  3131. {
  3132. const struct mg_request_info *ri;
  3133. long unsigned len = 1024 * 10;
  3134. const char *block = "0123456789";
  3135. unsigned long i, blocklen;
  3136. ck_assert(conn != NULL);
  3137. ri = mg_get_request_info(conn);
  3138. ck_assert(ri != NULL);
  3139. ck_assert_str_eq(ri->request_method, "GET");
  3140. ck_assert_str_eq(ri->request_uri, "/throttle");
  3141. ck_assert_str_eq(ri->local_uri, "/throttle");
  3142. ck_assert_str_eq(ri->http_version, "1.0");
  3143. ck_assert_str_eq(ri->query_string, "q");
  3144. ck_assert_str_eq(ri->remote_addr, "127.0.0.1");
  3145. mg_printf(conn,
  3146. "HTTP/1.1 200 OK\r\n"
  3147. "Content-Length: %lu\r\n"
  3148. "Connection: close\r\n\r\n",
  3149. len);
  3150. blocklen = (unsigned long)strlen(block);
  3151. for (i = 0; i < len; i += blocklen) {
  3152. mg_write(conn, block, blocklen);
  3153. }
  3154. return 987; /* Not a valid HTTP response code,
  3155. * but it should be written to the log and passed to
  3156. * end_request. */
  3157. }
  3158. static void
  3159. test_throttle_end_request(const struct mg_connection *conn,
  3160. int reply_status_code)
  3161. {
  3162. const struct mg_request_info *ri;
  3163. ck_assert(conn != NULL);
  3164. ri = mg_get_request_info(conn);
  3165. ck_assert(ri != NULL);
  3166. ck_assert_str_eq(ri->request_method, "GET");
  3167. ck_assert_str_eq(ri->request_uri, "/throttle");
  3168. ck_assert_str_eq(ri->local_uri, "/throttle");
  3169. ck_assert_str_eq(ri->http_version, "1.0");
  3170. ck_assert_str_eq(ri->query_string, "q");
  3171. ck_assert_str_eq(ri->remote_addr, "127.0.0.1");
  3172. ck_assert_int_eq(reply_status_code, 987);
  3173. }
  3174. START_TEST(test_throttle)
  3175. {
  3176. /* Server var */
  3177. struct mg_context *ctx;
  3178. struct mg_callbacks callbacks;
  3179. const char *OPTIONS[32];
  3180. int opt_cnt = 0;
  3181. /* Client var */
  3182. struct mg_connection *client;
  3183. char client_err_buf[256];
  3184. char client_data_buf[256];
  3185. const struct mg_request_info *client_ri;
  3186. /* timing test */
  3187. int r, data_read;
  3188. time_t t0, t1;
  3189. double dt;
  3190. /* Set options and start server */
  3191. #if !defined(NO_FILES)
  3192. OPTIONS[opt_cnt++] = "document_root";
  3193. OPTIONS[opt_cnt++] = ".";
  3194. #endif
  3195. OPTIONS[opt_cnt++] = "listening_ports";
  3196. OPTIONS[opt_cnt++] = "8080";
  3197. OPTIONS[opt_cnt++] = "throttle";
  3198. OPTIONS[opt_cnt++] = "*=1k";
  3199. OPTIONS[opt_cnt] = NULL;
  3200. memset(&callbacks, 0, sizeof(callbacks));
  3201. callbacks.begin_request = test_throttle_begin_request;
  3202. callbacks.end_request = test_throttle_end_request;
  3203. ctx = test_mg_start(&callbacks, 0, OPTIONS);
  3204. ck_assert(ctx != NULL);
  3205. /* connect client */
  3206. memset(client_err_buf, 0, sizeof(client_err_buf));
  3207. memset(client_data_buf, 0, sizeof(client_data_buf));
  3208. strcpy(client_err_buf, "reset-content");
  3209. client = mg_download("127.0.0.1",
  3210. 8080,
  3211. 0,
  3212. client_err_buf,
  3213. sizeof(client_err_buf),
  3214. "GET /throttle?q HTTP/1.0\r\n\r\n");
  3215. ck_assert(ctx != NULL);
  3216. ck_assert_str_eq(client_err_buf, "");
  3217. client_ri = mg_get_request_info(client);
  3218. ck_assert(client_ri != NULL);
  3219. ck_assert_str_eq(client_ri->local_uri, "200");
  3220. ck_assert_int_eq(client_ri->content_length, 1024 * 10);
  3221. data_read = 0;
  3222. t0 = time(NULL);
  3223. while (data_read < client_ri->content_length) {
  3224. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3225. ck_assert_int_ge(r, 0);
  3226. data_read += r;
  3227. }
  3228. t1 = time(NULL);
  3229. dt = difftime(t1, t0) * 1000.0; /* Elapsed time in ms - in most systems
  3230. * only with second resolution */
  3231. /* Time estimation: Data size is 10 kB, with 1 kB/s speed limit.
  3232. * The first block (1st kB) is transferred immediately, the second
  3233. * block (2nd kB) one second later, the third block (3rd kB) two
  3234. * seconds later, .. the last block (10th kB) nine seconds later.
  3235. * The resolution of time measurement using the "time" C library
  3236. * function is 1 second, so we should add +/- one second tolerance.
  3237. * Thus, download of 10 kB with 1 kB/s should not be faster than
  3238. * 8 seconds. */
  3239. /* Check if there are at least 8 seconds */
  3240. ck_assert_int_ge((int)dt, 8 * 1000);
  3241. /* Nothing left to read */
  3242. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3243. ck_assert_int_eq(r, 0);
  3244. /* Close the client connection */
  3245. mg_close_connection(client);
  3246. /* Stop the server */
  3247. test_mg_stop(ctx);
  3248. }
  3249. END_TEST
  3250. START_TEST(test_init_library)
  3251. {
  3252. unsigned f_avail = mg_check_feature(0xFF);
  3253. unsigned f_ret = mg_init_library(f_avail);
  3254. ck_assert_uint_eq(f_ret, f_avail);
  3255. }
  3256. END_TEST
  3257. #define LARGE_FILE_SIZE (1024 * 1024 * 10)
  3258. static int
  3259. test_large_file_begin_request(struct mg_connection *conn)
  3260. {
  3261. const struct mg_request_info *ri;
  3262. long unsigned len = LARGE_FILE_SIZE;
  3263. const char *block = "0123456789";
  3264. uint64_t i;
  3265. size_t blocklen;
  3266. ck_assert(conn != NULL);
  3267. ri = mg_get_request_info(conn);
  3268. ck_assert(ri != NULL);
  3269. ck_assert_str_eq(ri->request_method, "GET");
  3270. ck_assert_str_eq(ri->http_version, "1.1");
  3271. ck_assert_str_eq(ri->remote_addr, "127.0.0.1");
  3272. ck_assert_ptr_eq(ri->query_string, NULL);
  3273. ck_assert_ptr_ne(ri->local_uri, NULL);
  3274. mg_printf(conn,
  3275. "HTTP/1.1 200 OK\r\n"
  3276. "Content-Length: %lu\r\n"
  3277. "Connection: close\r\n\r\n",
  3278. len);
  3279. blocklen = strlen(block);
  3280. for (i = 0; i < len; i += blocklen) {
  3281. mg_write(conn, block, blocklen);
  3282. }
  3283. return 200;
  3284. }
  3285. START_TEST(test_large_file)
  3286. {
  3287. /* Server var */
  3288. struct mg_context *ctx;
  3289. struct mg_callbacks callbacks;
  3290. const char *OPTIONS[32];
  3291. int opt_cnt = 0;
  3292. #if !defined(NO_SSL)
  3293. const char *ssl_cert = locate_ssl_cert();
  3294. #endif
  3295. char errmsg[256] = {0};
  3296. /* Client var */
  3297. struct mg_connection *client;
  3298. char client_err_buf[256];
  3299. char client_data_buf[256];
  3300. const struct mg_request_info *client_ri;
  3301. int64_t data_read;
  3302. int r;
  3303. int retry, retry_ok_cnt, retry_fail_cnt;
  3304. /* Set options and start server */
  3305. #if !defined(NO_FILES)
  3306. OPTIONS[opt_cnt++] = "document_root";
  3307. OPTIONS[opt_cnt++] = ".";
  3308. #endif
  3309. #if defined(NO_SSL)
  3310. OPTIONS[opt_cnt++] = "listening_ports";
  3311. OPTIONS[opt_cnt++] = "8080";
  3312. #else
  3313. OPTIONS[opt_cnt++] = "listening_ports";
  3314. OPTIONS[opt_cnt++] = "8443s";
  3315. OPTIONS[opt_cnt++] = "ssl_certificate";
  3316. OPTIONS[opt_cnt++] = ssl_cert;
  3317. #ifdef __MACH__
  3318. /* The Apple builds on Travis CI seem to have problems with TLS1.x
  3319. * Allow SSLv3 and TLS */
  3320. OPTIONS[opt_cnt++] = "ssl_protocol_version";
  3321. OPTIONS[opt_cnt++] = "2";
  3322. #else
  3323. /* The Linux builds on Travis CI work fine with TLS1.2 */
  3324. OPTIONS[opt_cnt++] = "ssl_protocol_version";
  3325. OPTIONS[opt_cnt++] = "4";
  3326. #endif
  3327. ck_assert(ssl_cert != NULL);
  3328. #endif
  3329. OPTIONS[opt_cnt] = NULL;
  3330. memset(&callbacks, 0, sizeof(callbacks));
  3331. callbacks.begin_request = test_large_file_begin_request;
  3332. callbacks.log_message = log_msg_func;
  3333. ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
  3334. ck_assert_str_eq(errmsg, "");
  3335. ck_assert(ctx != NULL);
  3336. /* Try downloading several times */
  3337. retry_ok_cnt = 0;
  3338. retry_fail_cnt = 0;
  3339. for (retry = 0; retry < 3; retry++) {
  3340. int fail = 0;
  3341. /* connect client */
  3342. memset(client_err_buf, 0, sizeof(client_err_buf));
  3343. memset(client_data_buf, 0, sizeof(client_data_buf));
  3344. client =
  3345. mg_download("127.0.0.1",
  3346. #if defined(NO_SSL)
  3347. 8080,
  3348. 0,
  3349. #else
  3350. 8443,
  3351. 1,
  3352. #endif
  3353. client_err_buf,
  3354. sizeof(client_err_buf),
  3355. "GET /large.file HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
  3356. ck_assert(client != NULL);
  3357. ck_assert_str_eq(client_err_buf, "");
  3358. client_ri = mg_get_request_info(client);
  3359. ck_assert(client_ri != NULL);
  3360. ck_assert_str_eq(client_ri->local_uri, "200");
  3361. ck_assert_int_eq(client_ri->content_length, LARGE_FILE_SIZE);
  3362. data_read = 0;
  3363. while (data_read < client_ri->content_length) {
  3364. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3365. if (r < 0) {
  3366. fail = 1;
  3367. break;
  3368. };
  3369. data_read += r;
  3370. }
  3371. /* Nothing left to read */
  3372. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3373. if (fail) {
  3374. ck_assert_int_eq(r, -1);
  3375. retry_fail_cnt++;
  3376. } else {
  3377. ck_assert_int_eq(r, 0);
  3378. retry_ok_cnt++;
  3379. }
  3380. /* Close the client connection */
  3381. mg_close_connection(client);
  3382. }
  3383. #if defined(_WIN32)
  3384. // TODO: Check this problem on AppVeyor
  3385. // ck_assert_int_le(retry_fail_cnt, 2);
  3386. // ck_assert_int_ge(retry_ok_cnt, 1);
  3387. #else
  3388. ck_assert_int_eq(retry_fail_cnt, 0);
  3389. ck_assert_int_eq(retry_ok_cnt, 3);
  3390. #endif
  3391. /* Stop the server */
  3392. test_mg_stop(ctx);
  3393. }
  3394. END_TEST
  3395. static int test_mg_store_body_con_len = 20000;
  3396. static int
  3397. test_mg_store_body_put_delete_handler(struct mg_connection *conn, void *ignored)
  3398. {
  3399. char path[4096] = {0};
  3400. const struct mg_request_info *info = mg_get_request_info(conn);
  3401. int64_t rc;
  3402. (void)ignored;
  3403. sprintf(path, "./%s", info->local_uri);
  3404. rc = mg_store_body(conn, path);
  3405. ck_assert_int_eq(test_mg_store_body_con_len, rc);
  3406. if (rc < 0) {
  3407. mg_printf(conn,
  3408. "HTTP/1.1 500 Internal Server Error\r\n"
  3409. "Content-Type:text/plain;charset=UTF-8\r\n"
  3410. "Connection:close\r\n\r\n"
  3411. "%s (ret: %ld)\n",
  3412. path,
  3413. (long)rc);
  3414. mg_close_connection(conn);
  3415. /* Debug output for tests */
  3416. printf("mg_store_body(%s) failed (ret: %ld)\n", path, (long)rc);
  3417. return 500;
  3418. }
  3419. mg_printf(conn,
  3420. "HTTP/1.1 200 OK\r\n"
  3421. "Content-Type:text/plain;charset=UTF-8\r\n"
  3422. "Connection:close\r\n\r\n"
  3423. "%s OK (%ld bytes saved)\n",
  3424. path,
  3425. (long)rc);
  3426. mg_close_connection(conn);
  3427. /* Debug output for tests */
  3428. printf("mg_store_body(%s) OK (%ld bytes)\n", path, (long)rc);
  3429. return 200;
  3430. }
  3431. static int
  3432. test_mg_store_body_begin_request_callback(struct mg_connection *conn)
  3433. {
  3434. const struct mg_request_info *info = mg_get_request_info(conn);
  3435. /* Debug output for tests */
  3436. printf("test_mg_store_body_begin_request_callback called (%s)\n",
  3437. info->request_method);
  3438. if ((strcmp(info->request_method, "PUT") == 0)
  3439. || (strcmp(info->request_method, "DELETE") == 0)) {
  3440. return test_mg_store_body_put_delete_handler(conn, NULL);
  3441. }
  3442. return 0;
  3443. }
  3444. START_TEST(test_mg_store_body)
  3445. {
  3446. /* Client data */
  3447. char client_err_buf[256];
  3448. char client_data_buf[1024];
  3449. struct mg_connection *client;
  3450. const struct mg_request_info *client_ri;
  3451. int r;
  3452. char check_data[256];
  3453. char *check_ptr;
  3454. char errmsg[256] = {0};
  3455. /* Server context handle */
  3456. struct mg_context *ctx;
  3457. struct mg_callbacks callbacks;
  3458. const char *options[] = {
  3459. #if !defined(NO_FILES)
  3460. "document_root",
  3461. ".",
  3462. #endif
  3463. #if !defined(NO_CACHING)
  3464. "static_file_max_age",
  3465. "0",
  3466. #endif
  3467. "listening_ports",
  3468. "127.0.0.1:8082",
  3469. "num_threads",
  3470. "1",
  3471. NULL
  3472. };
  3473. memset(&callbacks, 0, sizeof(callbacks));
  3474. callbacks.begin_request = test_mg_store_body_begin_request_callback;
  3475. callbacks.log_message = log_msg_func;
  3476. /* Initialize the library */
  3477. mg_init_library(0);
  3478. /* Start the server */
  3479. ctx = mg_start(&callbacks, (void *)errmsg, options);
  3480. ck_assert_str_eq(errmsg, "");
  3481. ck_assert(ctx != NULL);
  3482. /* Run the server for 15 seconds */
  3483. test_sleep(15);
  3484. /* Call a test client */
  3485. client = mg_connect_client(
  3486. "127.0.0.1", 8082, 0, client_err_buf, sizeof(client_err_buf));
  3487. ck_assert(client != NULL);
  3488. ck_assert_str_eq(client_err_buf, "");
  3489. mg_printf(client,
  3490. "PUT /%s HTTP/1.0\r\nContent-Length: %i\r\n\r\n",
  3491. "test_file_name.txt",
  3492. test_mg_store_body_con_len);
  3493. r = 0;
  3494. while (r < test_mg_store_body_con_len) {
  3495. int l = mg_write(client, "1234567890", 10);
  3496. ck_assert_int_eq(l, 10);
  3497. r += 10;
  3498. }
  3499. r = mg_get_response(client, client_err_buf, sizeof(client_err_buf), 10000);
  3500. ck_assert_int_ge(r, 0);
  3501. ck_assert_str_eq(client_err_buf, "");
  3502. client_ri = mg_get_request_info(client);
  3503. ck_assert(client_ri != NULL);
  3504. /* Response must be 200 OK */
  3505. ck_assert_ptr_ne(client_ri->request_uri, NULL);
  3506. ck_assert_str_eq(client_ri->request_uri, "200");
  3507. /* Read PUT response */
  3508. r = mg_read(client, client_data_buf, sizeof(client_data_buf) - 1);
  3509. ck_assert_int_gt(r, 0);
  3510. client_data_buf[r] = 0;
  3511. sprintf(check_data, "(%i bytes saved)", test_mg_store_body_con_len);
  3512. check_ptr = strstr(client_data_buf, check_data);
  3513. ck_assert_ptr_ne(check_ptr, NULL);
  3514. mg_close_connection(client);
  3515. /* Run the server for 5 seconds */
  3516. test_sleep(5);
  3517. /* Stop the server */
  3518. test_mg_stop(ctx);
  3519. /* Un-initialize the library */
  3520. mg_exit_library();
  3521. }
  3522. END_TEST
  3523. #if defined(MG_USE_OPEN_FILE) && !defined(NO_FILES)
  3524. #define FILE_IN_MEM_SIZE (1024 * 100)
  3525. static char *file_in_mem_data;
  3526. static const char *
  3527. test_file_in_memory_open_file(const struct mg_connection *conn,
  3528. const char *file_path,
  3529. size_t *file_size)
  3530. {
  3531. (void)conn;
  3532. if (strcmp(file_path, "./file_in_mem") == 0) {
  3533. /* File is in memory */
  3534. *file_size = FILE_IN_MEM_SIZE;
  3535. return file_in_mem_data;
  3536. } else {
  3537. /* File is not in memory */
  3538. return NULL;
  3539. }
  3540. }
  3541. START_TEST(test_file_in_memory)
  3542. {
  3543. /* Server var */
  3544. struct mg_context *ctx;
  3545. struct mg_callbacks callbacks;
  3546. const char *OPTIONS[32];
  3547. int opt_cnt = 0;
  3548. #if !defined(NO_SSL)
  3549. const char *ssl_cert = locate_ssl_cert();
  3550. #endif
  3551. /* Client var */
  3552. struct mg_connection *client;
  3553. char client_err_buf[256];
  3554. char client_data_buf[256];
  3555. const struct mg_request_info *client_ri;
  3556. int64_t data_read;
  3557. int r, i;
  3558. /* Prepare test data */
  3559. file_in_mem_data = (char *)malloc(FILE_IN_MEM_SIZE);
  3560. ck_assert_ptr_ne(file_in_mem_data, NULL);
  3561. for (r = 0; r < FILE_IN_MEM_SIZE; r++) {
  3562. file_in_mem_data[r] = (char)(r);
  3563. }
  3564. /* Set options and start server */
  3565. OPTIONS[opt_cnt++] = "document_root";
  3566. OPTIONS[opt_cnt++] = ".";
  3567. #if defined(NO_SSL)
  3568. OPTIONS[opt_cnt++] = "listening_ports";
  3569. OPTIONS[opt_cnt++] = "8080";
  3570. #else
  3571. OPTIONS[opt_cnt++] = "listening_ports";
  3572. OPTIONS[opt_cnt++] = "8443s";
  3573. OPTIONS[opt_cnt++] = "ssl_certificate";
  3574. OPTIONS[opt_cnt++] = ssl_cert;
  3575. ck_assert(ssl_cert != NULL);
  3576. #endif
  3577. OPTIONS[opt_cnt] = NULL;
  3578. memset(&callbacks, 0, sizeof(callbacks));
  3579. callbacks.open_file = test_file_in_memory_open_file;
  3580. ctx = test_mg_start(&callbacks, 0, OPTIONS);
  3581. ck_assert(ctx != NULL);
  3582. /* connect client */
  3583. memset(client_err_buf, 0, sizeof(client_err_buf));
  3584. memset(client_data_buf, 0, sizeof(client_data_buf));
  3585. client =
  3586. mg_download("127.0.0.1",
  3587. #if defined(NO_SSL)
  3588. 8080,
  3589. 0,
  3590. #else
  3591. 8443,
  3592. 1,
  3593. #endif
  3594. client_err_buf,
  3595. sizeof(client_err_buf),
  3596. "GET /file_in_mem HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
  3597. ck_assert(client != NULL);
  3598. ck_assert_str_eq(client_err_buf, "");
  3599. client_ri = mg_get_request_info(client);
  3600. ck_assert(client_ri != NULL);
  3601. ck_assert_str_eq(client_ri->local_uri, "200");
  3602. ck_assert_int_eq(client_ri->content_length, FILE_IN_MEM_SIZE);
  3603. data_read = 0;
  3604. while (data_read < client_ri->content_length) {
  3605. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3606. if (r > 0) {
  3607. for (i = 0; i < r; i++) {
  3608. ck_assert_int_eq((int)client_data_buf[i],
  3609. (int)file_in_mem_data[data_read + i]);
  3610. }
  3611. data_read += r;
  3612. }
  3613. }
  3614. /* Nothing left to read */
  3615. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3616. ck_assert_int_eq(r, 0);
  3617. /* Close the client connection */
  3618. mg_close_connection(client);
  3619. /* Stop the server */
  3620. test_mg_stop(ctx);
  3621. /* Free test data */
  3622. free(file_in_mem_data);
  3623. file_in_mem_data = NULL;
  3624. }
  3625. END_TEST
  3626. #else /* defined(MG_USE_OPEN_FILE) */
  3627. START_TEST(test_file_in_memory)
  3628. {
  3629. mark_point();
  3630. }
  3631. END_TEST
  3632. #endif
  3633. static void
  3634. minimal_http_client_impl(const char *server, uint16_t port, const char *uri)
  3635. {
  3636. /* Client var */
  3637. struct mg_connection *client;
  3638. char client_err_buf[256];
  3639. char client_data_buf[256];
  3640. const struct mg_request_info *client_ri;
  3641. int64_t data_read;
  3642. int r;
  3643. client = mg_connect_client(
  3644. server, port, 0, client_err_buf, sizeof(client_err_buf));
  3645. ck_assert(client != NULL);
  3646. ck_assert_str_eq(client_err_buf, "");
  3647. mg_printf(client, "GET /%s HTTP/1.0\r\n\r\n", uri);
  3648. r = mg_get_response(client, client_err_buf, sizeof(client_err_buf), 10000);
  3649. ck_assert_int_ge(r, 0);
  3650. ck_assert_str_eq(client_err_buf, "");
  3651. client_ri = mg_get_request_info(client);
  3652. ck_assert(client_ri != NULL);
  3653. /* e.g.: ck_assert_str_eq(client_ri->request_uri, "200"); */
  3654. ck_assert_ptr_ne(client_ri->request_uri, NULL);
  3655. r = (int)strlen(client_ri->request_uri);
  3656. ck_assert_int_eq(r, 3);
  3657. data_read = 0;
  3658. while (data_read < client_ri->content_length) {
  3659. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3660. if (r > 0) {
  3661. data_read += r;
  3662. }
  3663. }
  3664. /* Nothing left to read */
  3665. r = mg_read(client, client_data_buf, sizeof(client_data_buf));
  3666. ck_assert_int_eq(r, 0);
  3667. mg_close_connection(client);
  3668. }
  3669. START_TEST(test_minimal_client)
  3670. {
  3671. /* Initialize the library */
  3672. mg_init_library(0);
  3673. /* Call a test client */
  3674. minimal_http_client_impl("192.30.253.113" /* www.github.com */,
  3675. 80,
  3676. "civetweb/civetweb/");
  3677. /* Un-initialize the library */
  3678. mg_exit_library();
  3679. }
  3680. END_TEST
  3681. static int
  3682. minimal_test_request_handler(struct mg_connection *conn, void *cbdata)
  3683. {
  3684. const char *msg = (const char *)cbdata;
  3685. unsigned long len = (unsigned long)strlen(msg);
  3686. mg_printf(conn,
  3687. "HTTP/1.1 200 OK\r\n"
  3688. "Content-Length: %lu\r\n"
  3689. "Content-Type: text/plain\r\n"
  3690. "Connection: close\r\n\r\n",
  3691. len);
  3692. mg_write(conn, msg, len);
  3693. return 200;
  3694. }
  3695. START_TEST(test_minimal_server_callback)
  3696. {
  3697. /* This test should ensure the minimum server example in
  3698. * docs/Embedding.md is still running. */
  3699. /* Server context handle */
  3700. struct mg_context *ctx;
  3701. /* Initialize the library */
  3702. mg_init_library(0);
  3703. /* Start the server */
  3704. ctx = test_mg_start(NULL, 0, NULL);
  3705. ck_assert(ctx != NULL);
  3706. /* Add some handler */
  3707. mg_set_request_handler(ctx,
  3708. "/hello",
  3709. minimal_test_request_handler,
  3710. (void *)"Hello world");
  3711. mg_set_request_handler(ctx,
  3712. "/8",
  3713. minimal_test_request_handler,
  3714. (void *)"Number eight");
  3715. /* Run the server for 15 seconds */
  3716. test_sleep(10);
  3717. /* Call a test client */
  3718. minimal_http_client_impl("127.0.0.1", 8080, "/hello");
  3719. /* Run the server for 15 seconds */
  3720. test_sleep(5);
  3721. /* Stop the server */
  3722. test_mg_stop(ctx);
  3723. /* Un-initialize the library */
  3724. mg_exit_library();
  3725. }
  3726. END_TEST
  3727. Suite *
  3728. make_public_server_suite(void)
  3729. {
  3730. Suite *const suite = suite_create("PublicServer");
  3731. TCase *const tcase_checktestenv = tcase_create("Check test environment");
  3732. TCase *const tcase_initlib = tcase_create("Init library");
  3733. TCase *const tcase_startthreads = tcase_create("Start threads");
  3734. TCase *const tcase_minimal = tcase_create("Minimal");
  3735. TCase *const tcase_startstophttp = tcase_create("Start Stop HTTP Server");
  3736. TCase *const tcase_startstophttp_ipv6 =
  3737. tcase_create("Start Stop HTTP Server IPv6");
  3738. TCase *const tcase_startstophttps = tcase_create("Start Stop HTTPS Server");
  3739. TCase *const tcase_serverandclienttls = tcase_create("TLS Server Client");
  3740. TCase *const tcase_serverrequests = tcase_create("Server Requests");
  3741. TCase *const tcase_handle_form = tcase_create("Handle Form");
  3742. TCase *const tcase_http_auth = tcase_create("HTTP Authentication");
  3743. TCase *const tcase_keep_alive = tcase_create("HTTP Keep Alive");
  3744. TCase *const tcase_error_handling = tcase_create("Error handling");
  3745. TCase *const tcase_throttle = tcase_create("Limit speed");
  3746. TCase *const tcase_large_file = tcase_create("Large file");
  3747. TCase *const tcase_file_in_mem = tcase_create("File in memory");
  3748. tcase_add_test(tcase_checktestenv, test_the_test_environment);
  3749. tcase_set_timeout(tcase_checktestenv, civetweb_min_test_timeout);
  3750. suite_add_tcase(suite, tcase_checktestenv);
  3751. tcase_add_test(tcase_initlib, test_init_library);
  3752. tcase_set_timeout(tcase_initlib, civetweb_min_test_timeout);
  3753. suite_add_tcase(suite, tcase_initlib);
  3754. tcase_add_test(tcase_startthreads, test_threading);
  3755. tcase_set_timeout(tcase_startthreads, civetweb_min_test_timeout);
  3756. suite_add_tcase(suite, tcase_startthreads);
  3757. tcase_add_test(tcase_minimal, test_minimal_client);
  3758. tcase_add_test(tcase_minimal, test_minimal_server_callback);
  3759. tcase_set_timeout(tcase_minimal, civetweb_min_test_timeout);
  3760. suite_add_tcase(suite, tcase_minimal);
  3761. tcase_add_test(tcase_startstophttp, test_mg_start_stop_http_server);
  3762. tcase_set_timeout(tcase_startstophttp, civetweb_min_test_timeout);
  3763. suite_add_tcase(suite, tcase_startstophttp);
  3764. tcase_add_test(tcase_startstophttp_ipv6,
  3765. test_mg_start_stop_http_server_ipv6);
  3766. tcase_set_timeout(tcase_startstophttp_ipv6, civetweb_min_test_timeout);
  3767. suite_add_tcase(suite, tcase_startstophttp_ipv6);
  3768. tcase_add_test(tcase_startstophttps, test_mg_start_stop_https_server);
  3769. tcase_set_timeout(tcase_startstophttps, civetweb_min_test_timeout);
  3770. suite_add_tcase(suite, tcase_startstophttps);
  3771. tcase_add_test(tcase_serverandclienttls, test_mg_server_and_client_tls);
  3772. tcase_set_timeout(tcase_serverandclienttls, civetweb_min_test_timeout);
  3773. suite_add_tcase(suite, tcase_serverandclienttls);
  3774. tcase_add_test(tcase_serverrequests, test_request_handlers);
  3775. tcase_add_test(tcase_serverrequests, test_mg_store_body);
  3776. tcase_set_timeout(tcase_serverrequests, 180);
  3777. suite_add_tcase(suite, tcase_serverrequests);
  3778. tcase_add_test(tcase_handle_form, test_handle_form);
  3779. tcase_set_timeout(tcase_handle_form, 300);
  3780. suite_add_tcase(suite, tcase_handle_form);
  3781. tcase_add_test(tcase_http_auth, test_http_auth);
  3782. tcase_set_timeout(tcase_http_auth, 60);
  3783. suite_add_tcase(suite, tcase_http_auth);
  3784. tcase_add_test(tcase_keep_alive, test_keep_alive);
  3785. tcase_set_timeout(tcase_keep_alive, 300);
  3786. suite_add_tcase(suite, tcase_keep_alive);
  3787. tcase_add_test(tcase_error_handling, test_error_handling);
  3788. tcase_add_test(tcase_error_handling, test_error_log_file);
  3789. tcase_set_timeout(tcase_error_handling, 300);
  3790. suite_add_tcase(suite, tcase_error_handling);
  3791. tcase_add_test(tcase_throttle, test_throttle);
  3792. tcase_set_timeout(tcase_throttle, 300);
  3793. suite_add_tcase(suite, tcase_throttle);
  3794. tcase_add_test(tcase_large_file, test_large_file);
  3795. tcase_set_timeout(tcase_large_file, 600);
  3796. suite_add_tcase(suite, tcase_large_file);
  3797. tcase_add_test(tcase_file_in_mem, test_file_in_memory);
  3798. tcase_set_timeout(tcase_file_in_mem, 300);
  3799. suite_add_tcase(suite, tcase_file_in_mem);
  3800. return suite;
  3801. }
  3802. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  3803. /* Used to debug test cases without using the check framework */
  3804. /* Build command for Linux:
  3805. gcc test/public_server.c src/civetweb.c -I include/ -I test/ -l pthread -l dl -D
  3806. LOCAL_TEST -D REPLACE_CHECK_FOR_LOCAL_DEBUGGING -D MAIN_PUBLIC_SERVER=main
  3807. */
  3808. static int chk_ok = 0;
  3809. static int chk_failed = 0;
  3810. void
  3811. MAIN_PUBLIC_SERVER(void)
  3812. {
  3813. unsigned f_avail = mg_check_feature(0xFF);
  3814. unsigned f_ret = mg_init_library(f_avail);
  3815. ck_assert_uint_eq(f_ret, f_avail);
  3816. test_the_test_environment(0);
  3817. test_threading(0);
  3818. test_minimal_client(0);
  3819. test_minimal_server_callback(0);
  3820. test_mg_start_stop_http_server(0);
  3821. test_mg_start_stop_https_server(0);
  3822. test_request_handlers(0);
  3823. test_mg_store_body(0);
  3824. test_mg_server_and_client_tls(0);
  3825. test_handle_form(0);
  3826. test_http_auth(0);
  3827. test_keep_alive(0);
  3828. test_error_handling(0);
  3829. test_error_log_file(0);
  3830. test_throttle(0);
  3831. test_large_file(0);
  3832. test_file_in_memory(0);
  3833. mg_exit_library();
  3834. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  3835. }
  3836. void
  3837. _ck_assert_failed(const char *file, int line, const char *expr, ...)
  3838. {
  3839. va_list va;
  3840. va_start(va, expr);
  3841. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  3842. vfprintf(stderr, expr, va);
  3843. fprintf(stderr, "\n\n");
  3844. va_end(va);
  3845. chk_failed++;
  3846. }
  3847. void
  3848. _ck_assert_msg(int cond, const char *file, int line, const char *expr, ...)
  3849. {
  3850. va_list va;
  3851. if (cond) {
  3852. chk_ok++;
  3853. return;
  3854. }
  3855. va_start(va, expr);
  3856. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  3857. vfprintf(stderr, expr, va);
  3858. fprintf(stderr, "\n\n");
  3859. va_end(va);
  3860. chk_failed++;
  3861. }
  3862. void
  3863. _mark_point(const char *file, int line)
  3864. {
  3865. chk_ok++;
  3866. }
  3867. void
  3868. tcase_fn_start(const char *fname, const char *file, int line)
  3869. {
  3870. }
  3871. void suite_add_tcase(Suite *s, TCase *tc){};
  3872. void _tcase_add_test(TCase *tc,
  3873. TFun tf,
  3874. const char *fname,
  3875. int _signal,
  3876. int allowed_exit_value,
  3877. int start,
  3878. int end){};
  3879. TCase *
  3880. tcase_create(const char *name)
  3881. {
  3882. return NULL;
  3883. };
  3884. Suite *
  3885. suite_create(const char *name)
  3886. {
  3887. return NULL;
  3888. };
  3889. void tcase_set_timeout(TCase *tc, double timeout){};
  3890. #endif