public_server.c 130 KB

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