public_server.c 113 KB

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