public_server.c 117 KB

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