test-gobex-transfer.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * OBEX library with GLib integration
  5. *
  6. * Copyright (C) 2011 Intel Corporation. All rights reserved.
  7. *
  8. */
  9. #ifdef HAVE_CONFIG_H
  10. #include <config.h>
  11. #endif
  12. #include <stdlib.h>
  13. #include <sys/types.h>
  14. #include <sys/socket.h>
  15. #include <sys/stat.h>
  16. #include <errno.h>
  17. #include <unistd.h>
  18. #include <string.h>
  19. #include <stdint.h>
  20. #include <fcntl.h>
  21. #include "gobex/gobex.h"
  22. #include "util.h"
  23. #define FINAL_BIT 0x80
  24. #define RANDOM_PACKETS 4
  25. static guint8 put_req_first[] = { G_OBEX_OP_PUT, 0x00, 0x30,
  26. G_OBEX_HDR_TYPE, 0x00, 0x0b,
  27. 'f', 'o', 'o', '/', 'b', 'a', 'r', '\0',
  28. G_OBEX_HDR_NAME, 0x00, 0x15,
  29. 0, 'f', 0, 'i', 0, 'l', 0, 'e', 0, '.', 0, 't', 0, 'x', 0, 't', 0, 0,
  30. G_OBEX_HDR_BODY, 0x00, 0x0d,
  31. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  32. static guint8 put_req_first_srm[] = { G_OBEX_OP_PUT, 0x00, 0x32,
  33. G_OBEX_HDR_TYPE, 0x00, 0x0b,
  34. 'f', 'o', 'o', '/', 'b', 'a', 'r', '\0',
  35. G_OBEX_HDR_NAME, 0x00, 0x15,
  36. 0, 'f', 0, 'i', 0, 'l', 0, 'e', 0, '.', 0, 't', 0, 'x', 0, 't', 0, 0,
  37. G_OBEX_HDR_SRM, G_OBEX_SRM_ENABLE,
  38. G_OBEX_HDR_BODY, 0x00, 0x0d,
  39. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  40. static guint8 put_req_zero[255] = { G_OBEX_OP_PUT, 0x00, 0xff,
  41. G_OBEX_HDR_BODY, 0x00, 0xfc };
  42. static guint8 put_req_last[] = { G_OBEX_OP_PUT | FINAL_BIT, 0x00, 0x06,
  43. G_OBEX_HDR_BODY_END, 0x00, 0x03 };
  44. static guint8 abort_req[] = { G_OBEX_OP_ABORT | FINAL_BIT, 0x00, 0x03 };
  45. static guint8 put_rsp_first[] = { G_OBEX_RSP_CONTINUE | FINAL_BIT,
  46. 0x00, 0x03 };
  47. static guint8 put_rsp_first_srm[] = { G_OBEX_RSP_CONTINUE | FINAL_BIT,
  48. 0x00, 0x05,
  49. G_OBEX_HDR_SRM, 0x01 };
  50. static guint8 put_rsp_first_srm_wait[] = { G_OBEX_RSP_CONTINUE | FINAL_BIT,
  51. 0x00, 0x07,
  52. G_OBEX_HDR_SRM, 0x01,
  53. G_OBEX_HDR_SRMP, 0x01 };
  54. static guint8 put_rsp_last[] = { G_OBEX_RSP_SUCCESS | FINAL_BIT, 0x00, 0x03 };
  55. static guint8 get_req_first[] = { G_OBEX_OP_GET | FINAL_BIT, 0x00, 0x23,
  56. G_OBEX_HDR_TYPE, 0x00, 0x0b,
  57. 'f', 'o', 'o', '/', 'b', 'a', 'r', '\0',
  58. G_OBEX_HDR_NAME, 0x00, 0x15,
  59. 0, 'f', 0, 'i', 0, 'l', 0, 'e', 0, '.', 0, 't', 0, 'x', 0, 't', 0, 0 };
  60. static guint8 get_req_first_app[] = { G_OBEX_OP_GET | FINAL_BIT, 0x00, 0x2a,
  61. G_OBEX_HDR_TYPE, 0x00, 0x0b,
  62. 'f', 'o', 'o', '/', 'b', 'a', 'r', '\0',
  63. G_OBEX_HDR_NAME, 0x00, 0x15,
  64. 0, 'f', 0, 'i', 0, 'l', 0, 'e', 0, '.', 0, 't', 0, 'x', 0, 't', 0, 0,
  65. G_OBEX_HDR_APPARAM, 0x00, 0x07,
  66. 0, 1, 2, 3 };
  67. static guint8 get_req_first_srm[] = { G_OBEX_OP_GET | FINAL_BIT, 0x00, 0x25,
  68. G_OBEX_HDR_SRM, 0x01,
  69. G_OBEX_HDR_TYPE, 0x00, 0x0b,
  70. 'f', 'o', 'o', '/', 'b', 'a', 'r', '\0',
  71. G_OBEX_HDR_NAME, 0x00, 0x15,
  72. 0, 'f', 0, 'i', 0, 'l', 0, 'e', 0, '.', 0, 't', 0, 'x', 0, 't', 0, 0 };
  73. static guint8 get_req_first_srm_wait[] = { G_OBEX_OP_GET | FINAL_BIT, 0x00, 0x27,
  74. G_OBEX_HDR_SRM, 0x01,
  75. G_OBEX_HDR_TYPE, 0x00, 0x0b,
  76. 'f', 'o', 'o', '/', 'b', 'a', 'r', '\0',
  77. G_OBEX_HDR_NAME, 0x00, 0x15,
  78. 0, 'f', 0, 'i', 0, 'l', 0, 'e', 0, '.', 0, 't', 0, 'x', 0, 't', 0, 0,
  79. G_OBEX_HDR_SRMP, 0x01 };
  80. static guint8 get_req_srm_wait[] = { G_OBEX_OP_GET | FINAL_BIT, 0x00, 0x05,
  81. G_OBEX_HDR_SRMP, 0x01 };
  82. static guint8 get_req_last[] = { G_OBEX_OP_GET | FINAL_BIT, 0x00, 0x03, };
  83. static guint8 get_rsp_first_app[] = { G_OBEX_RSP_CONTINUE | FINAL_BIT, 0x00, 0x0A,
  84. G_OBEX_HDR_APPARAM, 0x00, 0x07,
  85. 0, 1, 2, 3 };
  86. static guint8 get_rsp_first[] = { G_OBEX_RSP_CONTINUE | FINAL_BIT, 0x00, 0x10,
  87. G_OBEX_HDR_BODY, 0x00, 0x0d,
  88. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  89. static guint8 get_rsp_first_srm[] = { G_OBEX_RSP_CONTINUE | FINAL_BIT, 0x00, 0x12,
  90. G_OBEX_HDR_SRM, 0x01,
  91. G_OBEX_HDR_BODY, 0x00, 0x0d,
  92. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  93. static guint8 get_rsp_first_srm_wait_next[] = { G_OBEX_RSP_CONTINUE | FINAL_BIT,
  94. 0x00, 0x14,
  95. G_OBEX_HDR_SRM, 0x01,
  96. G_OBEX_HDR_SRMP, 0x02,
  97. G_OBEX_HDR_BODY, 0x00, 0x0d,
  98. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  99. static guint8 get_rsp_srm_wait[] = { G_OBEX_RSP_CONTINUE | FINAL_BIT,
  100. 0x00, 0x03 };
  101. static guint8 get_rsp_zero[255] = { G_OBEX_RSP_CONTINUE | FINAL_BIT, 0x00, 0xff,
  102. G_OBEX_HDR_BODY, 0x00, 0xfc };
  103. static guint8 get_rsp_zero_wait_next[255] = { G_OBEX_RSP_CONTINUE | FINAL_BIT,
  104. 0x00, 0xff,
  105. G_OBEX_HDR_SRMP, 0x02,
  106. G_OBEX_HDR_BODY, 0x00, 0xfa };
  107. static guint8 get_rsp_last[] = { G_OBEX_RSP_SUCCESS | FINAL_BIT, 0x00, 0x06,
  108. G_OBEX_HDR_BODY_END, 0x00, 0x03 };
  109. static guint8 conn_req[] = { G_OBEX_OP_CONNECT | FINAL_BIT, 0x00, 0x07,
  110. 0x10, 0x00, 0x10, 0x00 };
  111. static guint8 conn_rsp[] = { G_OBEX_RSP_SUCCESS | FINAL_BIT, 0x00, 0x0c,
  112. 0x10, 0x00, 0x10, 0x00,
  113. G_OBEX_HDR_CONNECTION, 0x00, 0x00,
  114. 0x00, 0x01 };
  115. static guint8 conn_req_srm[] = { G_OBEX_OP_CONNECT | FINAL_BIT, 0x00, 0x09,
  116. 0x10, 0x00, 0x10, 0x00,
  117. G_OBEX_HDR_SRM, 0x02 };
  118. static guint8 conn_rsp_srm[] = { G_OBEX_RSP_SUCCESS | FINAL_BIT, 0x00, 0x0e,
  119. 0x10, 0x00, 0x10, 0x00,
  120. G_OBEX_HDR_CONNECTION, 0x00, 0x00,
  121. 0x00, 0x01,
  122. G_OBEX_HDR_SRM, 0x01 };
  123. static guint8 unavailable_rsp[] = { G_OBEX_RSP_SERVICE_UNAVAILABLE | FINAL_BIT,
  124. 0x00, 0x03 };
  125. static guint8 conn_get_req_first[] = { G_OBEX_OP_GET | FINAL_BIT, 0x00, 0x28,
  126. G_OBEX_HDR_CONNECTION, 0x00, 0x00, 0x00, 0x01,
  127. G_OBEX_HDR_TYPE, 0x00, 0x0b,
  128. 'f', 'o', 'o', '/', 'b', 'a', 'r', '\0',
  129. G_OBEX_HDR_NAME, 0x00, 0x15,
  130. 0, 'f', 0, 'i', 0, 'l', 0, 'e', 0, '.', 0, 't', 0, 'x', 0, 't', 0, 0 };
  131. static guint8 conn_get_req_wrg[] = { G_OBEX_OP_GET | FINAL_BIT, 0x00, 0x28,
  132. G_OBEX_HDR_CONNECTION, 0x00, 0x00, 0x00, 0xFF,
  133. G_OBEX_HDR_TYPE, 0x00, 0x0b,
  134. 'f', 'o', 'o', '/', 'b', 'a', 'r', '\0',
  135. G_OBEX_HDR_NAME, 0x00, 0x15,
  136. 0, 'f', 0, 'i', 0, 'l', 0, 'e', 0, '.', 0, 't', 0, 'x', 0, 't', 0, 0 };
  137. static guint8 conn_put_req_first[] = { G_OBEX_OP_PUT, 0x00, 0x35,
  138. G_OBEX_HDR_CONNECTION, 0x00, 0x00, 0x00, 0x01,
  139. G_OBEX_HDR_TYPE, 0x00, 0x0b,
  140. 'f', 'o', 'o', '/', 'b', 'a', 'r', '\0',
  141. G_OBEX_HDR_NAME, 0x00, 0x15,
  142. 0, 'f', 0, 'i', 0, 'l', 0, 'e', 0, '.', 0, 't', 0, 'x', 0, 't', 0, 0,
  143. G_OBEX_HDR_BODY, 0x00, 0x0d,
  144. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  145. static guint8 hdr_type[] = "foo/bar";
  146. static guint8 hdr_app[] = { 0, 1, 2, 3 };
  147. static guint8 body_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  148. static void transfer_complete(GObex *obex, GError *err, gpointer user_data)
  149. {
  150. struct test_data *d = user_data;
  151. if (err != NULL)
  152. d->err = g_error_copy(err);
  153. g_main_loop_quit(d->mainloop);
  154. }
  155. static gboolean resume_obex(gpointer user_data)
  156. {
  157. struct test_data *d = user_data;
  158. if (!g_main_loop_is_running(d->mainloop))
  159. return FALSE;
  160. g_obex_resume(d->obex);
  161. return FALSE;
  162. }
  163. static gssize provide_seq(void *buf, gsize len, gpointer user_data)
  164. {
  165. struct test_data *d = user_data;
  166. int fd;
  167. gssize ret;
  168. if (d->count == RANDOM_PACKETS - 1)
  169. return 0;
  170. fd = open("/dev/urandom", O_RDONLY | O_NOCTTY, 0);
  171. if (fd < 0) {
  172. g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
  173. "open(/dev/urandom): %s", strerror(errno));
  174. g_main_loop_quit(d->mainloop);
  175. return -1;
  176. }
  177. ret = read(fd, buf, len);
  178. close(fd);
  179. return ret;
  180. }
  181. static gssize provide_eagain(void *buf, gsize len, gpointer user_data)
  182. {
  183. struct test_data *d = user_data;
  184. if (d->count > 0)
  185. return 0;
  186. if (len < sizeof(body_data)) {
  187. g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
  188. "Got data request for only %zu bytes", len);
  189. g_main_loop_quit(d->mainloop);
  190. return -1;
  191. }
  192. if (d->provide_delay > 0) {
  193. g_timeout_add(d->provide_delay, resume_obex, d);
  194. d->provide_delay = 0;
  195. return -EAGAIN;
  196. }
  197. memcpy(buf, body_data, sizeof(body_data));
  198. return sizeof(body_data);
  199. }
  200. static gssize provide_data(void *buf, gsize len, gpointer user_data)
  201. {
  202. struct test_data *d = user_data;
  203. if (d->total > 0)
  204. return 0;
  205. if (len < sizeof(body_data)) {
  206. g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
  207. "Got data request for only %zu bytes", len);
  208. g_main_loop_quit(d->mainloop);
  209. return -1;
  210. }
  211. memcpy(buf, body_data, sizeof(body_data));
  212. if (d->provide_delay > 0) {
  213. g_obex_suspend(d->obex);
  214. g_timeout_add(d->provide_delay, resume_obex, d);
  215. }
  216. d->total += sizeof(body_data);
  217. return sizeof(body_data);
  218. }
  219. static void test_put_req(void)
  220. {
  221. GIOChannel *io;
  222. GIOCondition cond;
  223. GObex *obex;
  224. struct test_data d = { 0, NULL, {
  225. { put_req_first, sizeof(put_req_first) },
  226. { put_req_last, sizeof(put_req_last) } }, {
  227. { put_rsp_first, sizeof(put_rsp_first) },
  228. { put_rsp_last, sizeof(put_rsp_last) } } };
  229. create_endpoints(&obex, &io, SOCK_STREAM);
  230. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  231. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  232. d.mainloop = g_main_loop_new(NULL, FALSE);
  233. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  234. g_obex_put_req(obex, provide_data, transfer_complete, &d, &d.err,
  235. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  236. G_OBEX_HDR_NAME, "file.txt",
  237. G_OBEX_HDR_INVALID);
  238. g_assert_no_error(d.err);
  239. g_main_loop_run(d.mainloop);
  240. g_assert_cmpuint(d.count, ==, 2);
  241. g_main_loop_unref(d.mainloop);
  242. if (d.timer_id > 0)
  243. g_source_remove(d.timer_id);
  244. if (d.io_id > 0)
  245. g_source_remove(d.io_id);
  246. g_io_channel_unref(io);
  247. g_obex_unref(obex);
  248. g_assert_no_error(d.err);
  249. }
  250. static gboolean rcv_data(const void *buf, gsize len, gpointer user_data)
  251. {
  252. struct test_data *d = user_data;
  253. if (len != sizeof(body_data))
  254. d->err = g_error_new(TEST_ERROR, TEST_ERROR_UNEXPECTED,
  255. "Unexpected byte count %zu", len);
  256. if (memcmp(buf, body_data, sizeof(body_data)) != 0) {
  257. dump_bufs(body_data, sizeof(body_data), buf, len);
  258. d->err = g_error_new(TEST_ERROR, TEST_ERROR_UNEXPECTED,
  259. "Unexpected byte count %zu", len);
  260. }
  261. return TRUE;
  262. }
  263. static void handle_put(GObex *obex, GObexPacket *req, gpointer user_data)
  264. {
  265. struct test_data *d = user_data;
  266. guint8 op = g_obex_packet_get_operation(req, NULL);
  267. guint id;
  268. if (op != G_OBEX_OP_PUT) {
  269. d->err = g_error_new(TEST_ERROR, TEST_ERROR_UNEXPECTED,
  270. "Unexpected opcode 0x%02x", op);
  271. g_main_loop_quit(d->mainloop);
  272. return;
  273. }
  274. id = g_obex_put_rsp(obex, req, rcv_data, transfer_complete, d, &d->err,
  275. G_OBEX_HDR_INVALID);
  276. if (id == 0)
  277. g_main_loop_quit(d->mainloop);
  278. }
  279. static void test_put_rsp(void)
  280. {
  281. GIOChannel *io;
  282. GIOCondition cond;
  283. GObex *obex;
  284. struct test_data d = { 0, NULL, {
  285. { put_rsp_first, sizeof(put_rsp_first) },
  286. { put_rsp_last, sizeof(put_rsp_last) } }, {
  287. { put_req_last, sizeof(put_req_last) },
  288. { NULL, 0 } } };
  289. create_endpoints(&obex, &io, SOCK_STREAM);
  290. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  291. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  292. d.mainloop = g_main_loop_new(NULL, FALSE);
  293. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  294. g_obex_add_request_function(obex, G_OBEX_OP_PUT, handle_put, &d);
  295. g_io_channel_write_chars(io, (char *) put_req_first,
  296. sizeof(put_req_first), NULL, &d.err);
  297. g_assert_no_error(d.err);
  298. g_main_loop_run(d.mainloop);
  299. g_assert_cmpuint(d.count, ==, 1);
  300. g_main_loop_unref(d.mainloop);
  301. if (d.timer_id > 0)
  302. g_source_remove(d.timer_id);
  303. if (d.io_id > 0)
  304. g_source_remove(d.io_id);
  305. g_io_channel_unref(io);
  306. g_obex_unref(obex);
  307. g_assert_no_error(d.err);
  308. }
  309. static gboolean rcv_seq(const void *buf, gsize len, gpointer user_data)
  310. {
  311. return TRUE;
  312. }
  313. static void handle_put_seq(GObex *obex, GObexPacket *req,
  314. gpointer user_data)
  315. {
  316. struct test_data *d = user_data;
  317. guint8 op = g_obex_packet_get_operation(req, NULL);
  318. guint id;
  319. if (op != G_OBEX_OP_PUT) {
  320. d->err = g_error_new(TEST_ERROR, TEST_ERROR_UNEXPECTED,
  321. "Unexpected opcode 0x%02x", op);
  322. g_main_loop_quit(d->mainloop);
  323. return;
  324. }
  325. id = g_obex_put_rsp(obex, req, rcv_seq, transfer_complete, d,
  326. &d->err, G_OBEX_HDR_INVALID);
  327. if (id == 0)
  328. g_main_loop_quit(d->mainloop);
  329. }
  330. static void test_stream_put_rsp(void)
  331. {
  332. GIOChannel *io;
  333. GIOCondition cond;
  334. GObex *obex;
  335. struct test_data d = { 0, NULL, {
  336. { put_rsp_first, sizeof(put_rsp_first) },
  337. { put_rsp_first, sizeof(put_rsp_first) },
  338. { put_rsp_first, sizeof(put_rsp_first) },
  339. { put_rsp_last, sizeof(put_rsp_last) } }, {
  340. { put_req_zero, sizeof(put_req_zero) },
  341. { put_req_zero, sizeof(put_req_zero) },
  342. { put_req_last, sizeof(put_req_last) },
  343. { NULL, -1 } } };
  344. create_endpoints(&obex, &io, SOCK_STREAM);
  345. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  346. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  347. d.mainloop = g_main_loop_new(NULL, FALSE);
  348. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  349. g_obex_add_request_function(obex, G_OBEX_OP_PUT, handle_put_seq,
  350. &d);
  351. g_io_channel_write_chars(io, (char *) put_req_first,
  352. sizeof(put_req_first), NULL, &d.err);
  353. g_assert_no_error(d.err);
  354. g_main_loop_run(d.mainloop);
  355. g_assert_cmpuint(d.count, ==, RANDOM_PACKETS - 1);
  356. g_main_loop_unref(d.mainloop);
  357. if (d.timer_id > 0)
  358. g_source_remove(d.timer_id);
  359. if (d.io_id > 0)
  360. g_source_remove(d.io_id);
  361. g_io_channel_unref(io);
  362. g_obex_unref(obex);
  363. g_assert_no_error(d.err);
  364. }
  365. static gboolean cancel_transfer(gpointer user_data)
  366. {
  367. struct test_data *d = user_data;
  368. if (d->id > 0)
  369. g_obex_cancel_transfer(d->id, transfer_complete, user_data);
  370. return FALSE;
  371. }
  372. static gssize abort_data(void *buf, gsize len, gpointer user_data)
  373. {
  374. g_idle_add_full(G_PRIORITY_HIGH, cancel_transfer, user_data, NULL);
  375. return provide_data(buf, len, user_data);
  376. }
  377. static void test_stream_put_req_abort(void)
  378. {
  379. GIOChannel *io;
  380. GIOCondition cond;
  381. GObex *obex;
  382. struct test_data d = { 0, NULL, {
  383. { put_req_first, sizeof(put_req_first) },
  384. { abort_req, sizeof(abort_req) } }, {
  385. { put_rsp_last, sizeof(put_rsp_last) } } };
  386. create_endpoints(&obex, &io, SOCK_STREAM);
  387. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  388. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  389. d.mainloop = g_main_loop_new(NULL, FALSE);
  390. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  391. d.id = g_obex_put_req(obex, abort_data, transfer_complete, &d, &d.err,
  392. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  393. G_OBEX_HDR_NAME, "file.txt",
  394. G_OBEX_HDR_INVALID);
  395. g_assert_no_error(d.err);
  396. g_main_loop_run(d.mainloop);
  397. g_assert_cmpuint(d.count, ==, 2);
  398. g_main_loop_unref(d.mainloop);
  399. if (d.timer_id > 0)
  400. g_source_remove(d.timer_id);
  401. if (d.io_id > 0)
  402. g_source_remove(d.io_id);
  403. g_io_channel_unref(io);
  404. g_obex_unref(obex);
  405. g_assert_error(d.err, G_OBEX_ERROR, G_OBEX_ERROR_CANCELLED);
  406. g_error_free(d.err);
  407. }
  408. static void test_stream_put_rsp_abort(void)
  409. {
  410. GIOChannel *io;
  411. GIOCondition cond;
  412. GObex *obex;
  413. struct test_data d = { 0, NULL, {
  414. { put_rsp_first, sizeof(put_rsp_first) },
  415. { put_rsp_first, sizeof(put_rsp_first) },
  416. { put_rsp_first, sizeof(put_rsp_first) },
  417. { put_rsp_last, sizeof(put_rsp_last) } }, {
  418. { put_req_zero, sizeof(put_req_zero) },
  419. { abort_req, sizeof(abort_req) },
  420. { NULL, -1 },
  421. { NULL, -1 } } };
  422. create_endpoints(&obex, &io, SOCK_STREAM);
  423. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  424. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  425. d.mainloop = g_main_loop_new(NULL, FALSE);
  426. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  427. g_obex_add_request_function(obex, G_OBEX_OP_PUT, handle_put_seq, &d);
  428. g_io_channel_write_chars(io, (char *) put_req_first,
  429. sizeof(put_req_first), NULL, &d.err);
  430. g_assert_no_error(d.err);
  431. g_main_loop_run(d.mainloop);
  432. g_assert_cmpuint(d.count, ==, RANDOM_PACKETS - 2);
  433. g_main_loop_unref(d.mainloop);
  434. if (d.timer_id > 0)
  435. g_source_remove(d.timer_id);
  436. if (d.io_id > 0)
  437. g_source_remove(d.io_id);
  438. g_io_channel_unref(io);
  439. g_obex_unref(obex);
  440. g_assert_error(d.err, G_OBEX_ERROR, G_OBEX_ERROR_CANCELLED);
  441. g_error_free(d.err);
  442. }
  443. static void handle_put_seq_wait(GObex *obex, GObexPacket *req,
  444. gpointer user_data)
  445. {
  446. struct test_data *d = user_data;
  447. guint8 op = g_obex_packet_get_operation(req, NULL);
  448. guint id;
  449. if (op != G_OBEX_OP_PUT) {
  450. d->err = g_error_new(TEST_ERROR, TEST_ERROR_UNEXPECTED,
  451. "Unexpected opcode 0x%02x", op);
  452. g_main_loop_quit(d->mainloop);
  453. return;
  454. }
  455. id = g_obex_put_rsp(obex, req, rcv_seq, transfer_complete, d,
  456. &d->err,
  457. G_OBEX_HDR_SRMP, G_OBEX_SRMP_WAIT,
  458. G_OBEX_HDR_INVALID);
  459. if (id == 0)
  460. g_main_loop_quit(d->mainloop);
  461. }
  462. static void test_packet_put_rsp_wait(void)
  463. {
  464. GIOChannel *io;
  465. GIOCondition cond;
  466. GObex *obex;
  467. struct test_data d = { 0, NULL, {
  468. { put_rsp_first_srm_wait, sizeof(put_rsp_first_srm_wait) },
  469. { put_rsp_first, sizeof(put_rsp_first) },
  470. { NULL, -1 },
  471. { put_rsp_last, sizeof(put_rsp_last) } }, {
  472. { put_req_zero, sizeof(put_req_zero) },
  473. { put_req_zero, sizeof(put_req_zero) },
  474. { put_req_last, sizeof(put_req_last) },
  475. { NULL, 0 } } };
  476. create_endpoints(&obex, &io, SOCK_SEQPACKET);
  477. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  478. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  479. d.mainloop = g_main_loop_new(NULL, FALSE);
  480. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  481. g_obex_add_request_function(obex, G_OBEX_OP_PUT,
  482. handle_put_seq_wait, &d);
  483. g_io_channel_write_chars(io, (char *) put_req_first_srm,
  484. sizeof(put_req_first_srm), NULL,
  485. &d.err);
  486. g_assert_no_error(d.err);
  487. g_main_loop_run(d.mainloop);
  488. g_assert_cmpuint(d.count, ==, RANDOM_PACKETS);
  489. g_main_loop_unref(d.mainloop);
  490. if (d.timer_id > 0)
  491. g_source_remove(d.timer_id);
  492. if (d.io_id > 0)
  493. g_source_remove(d.io_id);
  494. g_io_channel_unref(io);
  495. g_obex_unref(obex);
  496. g_assert_no_error(d.err);
  497. }
  498. static void test_packet_put_rsp(void)
  499. {
  500. GIOChannel *io;
  501. GIOCondition cond;
  502. GObex *obex;
  503. struct test_data d = { 0, NULL, {
  504. { put_rsp_first_srm, sizeof(put_rsp_first_srm) },
  505. { NULL, -1 },
  506. { NULL, -1 },
  507. { put_rsp_last, sizeof(put_rsp_last) } }, {
  508. { put_req_zero, sizeof(put_req_zero) },
  509. { put_req_zero, sizeof(put_req_zero) },
  510. { put_req_last, sizeof(put_req_last) },
  511. { NULL, 0 } } };
  512. create_endpoints(&obex, &io, SOCK_SEQPACKET);
  513. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  514. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  515. d.mainloop = g_main_loop_new(NULL, FALSE);
  516. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  517. g_obex_add_request_function(obex, G_OBEX_OP_PUT, handle_put_seq, &d);
  518. g_io_channel_write_chars(io, (char *) put_req_first_srm,
  519. sizeof(put_req_first_srm), NULL,
  520. &d.err);
  521. g_assert_no_error(d.err);
  522. g_main_loop_run(d.mainloop);
  523. g_assert_cmpuint(d.count, ==, RANDOM_PACKETS);
  524. g_main_loop_unref(d.mainloop);
  525. if (d.timer_id > 0)
  526. g_source_remove(d.timer_id);
  527. if (d.io_id > 0)
  528. g_source_remove(d.io_id);
  529. g_io_channel_unref(io);
  530. g_obex_unref(obex);
  531. g_assert_no_error(d.err);
  532. }
  533. static void test_get_req(void)
  534. {
  535. GIOChannel *io;
  536. GIOCondition cond;
  537. GObex *obex;
  538. struct test_data d = { 0, NULL, {
  539. { get_req_first, sizeof(get_req_first) },
  540. { get_req_last, sizeof(get_req_last) } }, {
  541. { get_rsp_first, sizeof(get_rsp_first) },
  542. { get_rsp_last, sizeof(get_rsp_last) } } };
  543. create_endpoints(&obex, &io, SOCK_STREAM);
  544. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  545. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  546. d.mainloop = g_main_loop_new(NULL, FALSE);
  547. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  548. g_obex_get_req(obex, rcv_data, transfer_complete, &d, &d.err,
  549. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  550. G_OBEX_HDR_NAME, "file.txt",
  551. G_OBEX_HDR_INVALID);
  552. g_assert_no_error(d.err);
  553. g_main_loop_run(d.mainloop);
  554. g_assert_cmpuint(d.count, ==, 2);
  555. g_main_loop_unref(d.mainloop);
  556. if (d.timer_id > 0)
  557. g_source_remove(d.timer_id);
  558. if (d.io_id > 0)
  559. g_source_remove(d.io_id);
  560. g_io_channel_unref(io);
  561. g_obex_unref(obex);
  562. g_assert_no_error(d.err);
  563. }
  564. static void test_stream_get_req(void)
  565. {
  566. GIOChannel *io;
  567. GIOCondition cond;
  568. GObex *obex;
  569. struct test_data d = { 0, NULL, {
  570. { get_req_first, sizeof(get_req_first) },
  571. { NULL, 0 },
  572. { NULL, 0 },
  573. { get_req_last, sizeof(get_req_last) } }, {
  574. { get_rsp_first, sizeof(get_rsp_first) },
  575. { get_rsp_zero, sizeof(get_rsp_zero) },
  576. { get_rsp_zero, sizeof(get_rsp_zero) },
  577. { get_rsp_last, sizeof(get_rsp_last) } } };
  578. create_endpoints(&obex, &io, SOCK_STREAM);
  579. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  580. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  581. d.mainloop = g_main_loop_new(NULL, FALSE);
  582. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  583. g_obex_get_req(obex, rcv_seq, transfer_complete, &d, &d.err,
  584. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  585. G_OBEX_HDR_NAME, "file.txt",
  586. G_OBEX_HDR_INVALID);
  587. g_assert_no_error(d.err);
  588. g_main_loop_run(d.mainloop);
  589. g_assert_cmpuint(d.count, ==, RANDOM_PACKETS);
  590. g_main_loop_unref(d.mainloop);
  591. if (d.timer_id > 0)
  592. g_source_remove(d.timer_id);
  593. if (d.io_id > 0)
  594. g_source_remove(d.io_id);
  595. g_io_channel_unref(io);
  596. g_obex_unref(obex);
  597. g_assert_no_error(d.err);
  598. }
  599. static void test_packet_get_req(void)
  600. {
  601. GIOChannel *io;
  602. GIOCondition cond;
  603. GObex *obex;
  604. struct test_data d = { 0, NULL, {
  605. { get_req_first_srm, sizeof(get_req_first_srm) },
  606. { NULL, -1 },
  607. { NULL, -1 },
  608. { get_req_last, sizeof(get_req_last) } }, {
  609. { get_rsp_first_srm, sizeof(get_rsp_first_srm) },
  610. { get_rsp_zero, sizeof(get_rsp_zero) },
  611. { get_rsp_zero, sizeof(get_rsp_zero) },
  612. { get_rsp_last, sizeof(get_rsp_last) } } };
  613. create_endpoints(&obex, &io, SOCK_SEQPACKET);
  614. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  615. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  616. d.mainloop = g_main_loop_new(NULL, FALSE);
  617. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  618. g_obex_get_req(obex, rcv_seq, transfer_complete, &d, &d.err,
  619. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  620. G_OBEX_HDR_NAME, "file.txt",
  621. G_OBEX_HDR_INVALID);
  622. g_assert_no_error(d.err);
  623. g_main_loop_run(d.mainloop);
  624. g_assert_cmpuint(d.count, ==, RANDOM_PACKETS);
  625. g_main_loop_unref(d.mainloop);
  626. if (d.timer_id > 0)
  627. g_source_remove(d.timer_id);
  628. if (d.io_id > 0)
  629. g_source_remove(d.io_id);
  630. g_io_channel_unref(io);
  631. g_obex_unref(obex);
  632. g_assert_no_error(d.err);
  633. }
  634. static void test_packet_get_req_wait(void)
  635. {
  636. GIOChannel *io;
  637. GIOCondition cond;
  638. GObex *obex;
  639. struct test_data d = { 0, NULL, {
  640. { get_req_first_srm_wait, sizeof(get_req_first_srm_wait) },
  641. { get_req_last, sizeof(get_req_last) },
  642. { NULL, -1 },
  643. { get_req_last, sizeof(get_req_last) } }, {
  644. { get_rsp_first_srm, sizeof(get_rsp_first_srm) },
  645. { get_rsp_zero, sizeof(get_rsp_zero) },
  646. { get_rsp_zero, sizeof(get_rsp_zero) },
  647. { get_rsp_last, sizeof(get_rsp_last) } } };
  648. create_endpoints(&obex, &io, SOCK_SEQPACKET);
  649. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  650. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  651. d.mainloop = g_main_loop_new(NULL, FALSE);
  652. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  653. g_obex_get_req(obex, rcv_seq, transfer_complete, &d, &d.err,
  654. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  655. G_OBEX_HDR_NAME, "file.txt",
  656. G_OBEX_HDR_SRMP, G_OBEX_SRMP_WAIT,
  657. G_OBEX_HDR_INVALID);
  658. g_assert_no_error(d.err);
  659. g_main_loop_run(d.mainloop);
  660. g_assert_cmpuint(d.count, ==, RANDOM_PACKETS);
  661. g_main_loop_unref(d.mainloop);
  662. if (d.timer_id > 0)
  663. g_source_remove(d.timer_id);
  664. if (d.io_id > 0)
  665. g_source_remove(d.io_id);
  666. g_io_channel_unref(io);
  667. g_obex_unref(obex);
  668. g_assert_no_error(d.err);
  669. }
  670. static gboolean rcv_seq_delay(const void *buf, gsize len, gpointer user_data)
  671. {
  672. struct test_data *d = user_data;
  673. if (d->provide_delay > 0) {
  674. g_obex_suspend(d->obex);
  675. g_timeout_add_seconds(d->provide_delay, resume_obex, d);
  676. d->provide_delay = 0;
  677. }
  678. return TRUE;
  679. }
  680. static void test_packet_get_req_suspend_resume(void)
  681. {
  682. GIOChannel *io;
  683. GIOCondition cond;
  684. GObex *obex;
  685. struct test_data d = { 0, NULL, {
  686. { get_req_first_srm, sizeof(get_req_first_srm) },
  687. { get_req_srm_wait, sizeof(get_req_srm_wait) },
  688. { get_req_srm_wait, sizeof(get_req_srm_wait) },
  689. { get_req_last, sizeof(get_req_last) } }, {
  690. { get_rsp_first_srm, sizeof(get_rsp_first_srm) },
  691. { get_rsp_srm_wait, sizeof(get_rsp_srm_wait) },
  692. { get_rsp_srm_wait, sizeof(get_rsp_srm_wait) },
  693. { get_rsp_last, sizeof(get_rsp_last) } } };
  694. create_endpoints(&obex, &io, SOCK_SEQPACKET);
  695. d.obex = obex;
  696. d.provide_delay = 1;
  697. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  698. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  699. d.mainloop = g_main_loop_new(NULL, FALSE);
  700. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  701. g_obex_get_req(obex, rcv_seq_delay, transfer_complete, &d, &d.err,
  702. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  703. G_OBEX_HDR_NAME, "file.txt",
  704. G_OBEX_HDR_INVALID);
  705. g_assert_no_error(d.err);
  706. g_main_loop_run(d.mainloop);
  707. g_assert_cmpuint(d.count, ==, 4);
  708. g_main_loop_unref(d.mainloop);
  709. if (d.timer_id > 0)
  710. g_source_remove(d.timer_id);
  711. if (d.io_id > 0)
  712. g_source_remove(d.io_id);
  713. g_io_channel_unref(io);
  714. g_obex_unref(obex);
  715. g_assert_no_error(d.err);
  716. }
  717. static void test_packet_get_req_wait_next(void)
  718. {
  719. GIOChannel *io;
  720. GIOCondition cond;
  721. GObex *obex;
  722. struct test_data d = { 0, NULL, {
  723. { get_req_first_srm, sizeof(get_req_first_srm) },
  724. { get_req_last, sizeof(get_req_last) },
  725. { get_req_last, sizeof(get_req_last) },
  726. { get_req_last, sizeof(get_req_last) } }, {
  727. { get_rsp_first_srm_wait_next,
  728. sizeof(get_rsp_first_srm_wait_next) },
  729. { get_rsp_zero_wait_next, sizeof(get_rsp_zero_wait_next) },
  730. { get_rsp_zero_wait_next, sizeof(get_rsp_zero_wait_next) },
  731. { get_rsp_last, sizeof(get_rsp_last) } } };
  732. create_endpoints(&obex, &io, SOCK_SEQPACKET);
  733. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  734. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  735. d.mainloop = g_main_loop_new(NULL, FALSE);
  736. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  737. g_obex_get_req(obex, rcv_seq, transfer_complete, &d, &d.err,
  738. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  739. G_OBEX_HDR_NAME, "file.txt",
  740. G_OBEX_HDR_INVALID);
  741. g_assert_no_error(d.err);
  742. g_main_loop_run(d.mainloop);
  743. g_assert_cmpuint(d.count, ==, RANDOM_PACKETS);
  744. g_main_loop_unref(d.mainloop);
  745. if (d.timer_id > 0)
  746. g_source_remove(d.timer_id);
  747. if (d.io_id > 0)
  748. g_source_remove(d.io_id);
  749. g_io_channel_unref(io);
  750. g_obex_unref(obex);
  751. g_assert_no_error(d.err);
  752. }
  753. static void test_get_req_app(void)
  754. {
  755. GIOChannel *io;
  756. GIOCondition cond;
  757. GObex *obex;
  758. struct test_data d = { 0, NULL, {
  759. { get_req_first_app, sizeof(get_req_first_app) },
  760. { get_req_last, sizeof(get_req_last) },
  761. { get_req_last, sizeof(get_req_last) } }, {
  762. { get_rsp_first_app, sizeof(get_rsp_first_app) },
  763. { get_rsp_first, sizeof(get_rsp_first) },
  764. { get_rsp_last, sizeof(get_rsp_last) } } };
  765. create_endpoints(&obex, &io, SOCK_STREAM);
  766. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  767. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  768. d.mainloop = g_main_loop_new(NULL, FALSE);
  769. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  770. g_obex_get_req(obex, rcv_data, transfer_complete, &d, &d.err,
  771. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  772. G_OBEX_HDR_NAME, "file.txt",
  773. G_OBEX_HDR_APPARAM, hdr_app, sizeof(hdr_app),
  774. G_OBEX_HDR_INVALID);
  775. g_assert_no_error(d.err);
  776. g_main_loop_run(d.mainloop);
  777. g_assert_cmpuint(d.count, ==, 3);
  778. g_main_loop_unref(d.mainloop);
  779. if (d.timer_id > 0)
  780. g_source_remove(d.timer_id);
  781. if (d.io_id > 0)
  782. g_source_remove(d.io_id);
  783. g_io_channel_unref(io);
  784. g_obex_unref(obex);
  785. g_assert_no_error(d.err);
  786. }
  787. static void handle_get_eagain(GObex *obex, GObexPacket *req,
  788. gpointer user_data)
  789. {
  790. struct test_data *d = user_data;
  791. guint8 op = g_obex_packet_get_operation(req, NULL);
  792. guint id;
  793. if (op != G_OBEX_OP_GET) {
  794. d->err = g_error_new(TEST_ERROR, TEST_ERROR_UNEXPECTED,
  795. "Unexpected opcode 0x%02x", op);
  796. g_main_loop_quit(d->mainloop);
  797. return;
  798. }
  799. id = g_obex_get_rsp(obex, provide_eagain, transfer_complete, d,
  800. &d->err, G_OBEX_HDR_INVALID);
  801. if (id == 0)
  802. g_main_loop_quit(d->mainloop);
  803. }
  804. static void handle_get(GObex *obex, GObexPacket *req, gpointer user_data)
  805. {
  806. struct test_data *d = user_data;
  807. guint8 op = g_obex_packet_get_operation(req, NULL);
  808. guint id;
  809. if (op != G_OBEX_OP_GET) {
  810. d->err = g_error_new(TEST_ERROR, TEST_ERROR_UNEXPECTED,
  811. "Unexpected opcode 0x%02x", op);
  812. g_main_loop_quit(d->mainloop);
  813. return;
  814. }
  815. id = g_obex_get_rsp(obex, provide_data, transfer_complete, d, &d->err,
  816. G_OBEX_HDR_INVALID);
  817. if (id == 0)
  818. g_main_loop_quit(d->mainloop);
  819. }
  820. static void test_stream_put_req(void)
  821. {
  822. GIOChannel *io;
  823. GIOCondition cond;
  824. GObex *obex;
  825. struct test_data d = { 0, NULL, {
  826. { NULL, 0 },
  827. { NULL, 0 },
  828. { NULL, 0 },
  829. { put_req_last, sizeof(put_req_last) } }, {
  830. { put_rsp_first, sizeof(put_rsp_first) },
  831. { put_rsp_first, sizeof(put_rsp_first) },
  832. { put_rsp_first, sizeof(put_rsp_first) },
  833. { put_rsp_last, sizeof(put_rsp_last) } } };
  834. create_endpoints(&obex, &io, SOCK_STREAM);
  835. d.obex = obex;
  836. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  837. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  838. d.mainloop = g_main_loop_new(NULL, FALSE);
  839. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  840. g_obex_put_req(obex, provide_seq, transfer_complete, &d, &d.err,
  841. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  842. G_OBEX_HDR_NAME, "random.bin",
  843. G_OBEX_HDR_INVALID);
  844. g_assert_no_error(d.err);
  845. g_main_loop_run(d.mainloop);
  846. g_assert_cmpuint(d.count, ==, RANDOM_PACKETS);
  847. g_main_loop_unref(d.mainloop);
  848. if (d.timer_id > 0)
  849. g_source_remove(d.timer_id);
  850. if (d.io_id > 0)
  851. g_source_remove(d.io_id);
  852. g_io_channel_unref(io);
  853. g_obex_unref(obex);
  854. g_assert_no_error(d.err);
  855. }
  856. static gssize provide_seq_delay(void *buf, gsize len, gpointer user_data)
  857. {
  858. struct test_data *d = user_data;
  859. int fd;
  860. gssize ret;
  861. if (d->provide_delay > 0) {
  862. g_obex_suspend(d->obex);
  863. g_timeout_add_seconds(d->provide_delay, resume_obex, d);
  864. d->provide_delay = 0;
  865. }
  866. if (d->count == RANDOM_PACKETS - 1)
  867. return 0;
  868. fd = open("/dev/urandom", O_RDONLY | O_NOCTTY, 0);
  869. if (fd < 0) {
  870. g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
  871. "open(/dev/urandom): %s", strerror(errno));
  872. g_main_loop_quit(d->mainloop);
  873. return -1;
  874. }
  875. ret = read(fd, buf, len);
  876. close(fd);
  877. return ret;
  878. }
  879. static void test_packet_put_req_suspend_resume(void)
  880. {
  881. GIOChannel *io;
  882. GIOCondition cond;
  883. GObex *obex;
  884. struct test_data d = { 0, NULL, {
  885. { NULL, 0 },
  886. { NULL, 0 },
  887. { NULL, 0 },
  888. { put_req_last, sizeof(put_req_last) } }, {
  889. { put_rsp_first_srm, sizeof(put_rsp_first_srm) },
  890. { NULL, 0 },
  891. { NULL, 0 },
  892. { put_rsp_last, sizeof(put_rsp_last) } } };
  893. create_endpoints(&obex, &io, SOCK_SEQPACKET);
  894. d.obex = obex;
  895. d.provide_delay = 1;
  896. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  897. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  898. d.mainloop = g_main_loop_new(NULL, FALSE);
  899. g_obex_put_req(obex, provide_seq_delay, transfer_complete, &d, &d.err,
  900. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  901. G_OBEX_HDR_NAME, "random.bin",
  902. G_OBEX_HDR_INVALID);
  903. g_assert_no_error(d.err);
  904. g_main_loop_run(d.mainloop);
  905. g_assert_cmpuint(d.count, ==, RANDOM_PACKETS);
  906. g_main_loop_unref(d.mainloop);
  907. if (d.io_id > 0)
  908. g_source_remove(d.io_id);
  909. g_io_channel_unref(io);
  910. g_obex_unref(obex);
  911. g_assert_no_error(d.err);
  912. }
  913. static void test_packet_put_req_wait(void)
  914. {
  915. GIOChannel *io;
  916. GIOCondition cond;
  917. GObex *obex;
  918. struct test_data d = { 0, NULL, {
  919. { NULL, 0 },
  920. { NULL, 0 },
  921. { NULL, 0 },
  922. { put_req_last, sizeof(put_req_last) } }, {
  923. { put_rsp_first_srm_wait, sizeof(put_rsp_first_srm_wait) },
  924. { put_rsp_first, sizeof(put_rsp_first) },
  925. { NULL, 0 },
  926. { put_rsp_last, sizeof(put_rsp_last) } } };
  927. create_endpoints(&obex, &io, SOCK_SEQPACKET);
  928. d.obex = obex;
  929. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  930. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  931. d.mainloop = g_main_loop_new(NULL, FALSE);
  932. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  933. g_obex_put_req(obex, provide_seq, transfer_complete, &d, &d.err,
  934. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  935. G_OBEX_HDR_NAME, "random.bin",
  936. G_OBEX_HDR_INVALID);
  937. g_assert_no_error(d.err);
  938. g_main_loop_run(d.mainloop);
  939. g_assert_cmpuint(d.count, ==, RANDOM_PACKETS);
  940. g_main_loop_unref(d.mainloop);
  941. if (d.timer_id > 0)
  942. g_source_remove(d.timer_id);
  943. if (d.io_id > 0)
  944. g_source_remove(d.io_id);
  945. g_io_channel_unref(io);
  946. g_obex_unref(obex);
  947. g_assert_no_error(d.err);
  948. }
  949. static void test_packet_put_req(void)
  950. {
  951. GIOChannel *io;
  952. GIOCondition cond;
  953. GObex *obex;
  954. struct test_data d = { 0, NULL, {
  955. { NULL, 0 },
  956. { NULL, 0 },
  957. { NULL, 0 },
  958. { put_req_last, sizeof(put_req_last) } }, {
  959. { put_rsp_first_srm, sizeof(put_rsp_first_srm) },
  960. { NULL, 0 },
  961. { NULL, 0 },
  962. { put_rsp_last, sizeof(put_rsp_last) } } };
  963. create_endpoints(&obex, &io, SOCK_SEQPACKET);
  964. d.obex = obex;
  965. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  966. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  967. d.mainloop = g_main_loop_new(NULL, FALSE);
  968. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  969. g_obex_put_req(obex, provide_seq, transfer_complete, &d, &d.err,
  970. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  971. G_OBEX_HDR_NAME, "random.bin",
  972. G_OBEX_HDR_INVALID);
  973. g_assert_no_error(d.err);
  974. g_main_loop_run(d.mainloop);
  975. g_assert_cmpuint(d.count, ==, RANDOM_PACKETS);
  976. g_main_loop_unref(d.mainloop);
  977. if (d.timer_id > 0)
  978. g_source_remove(d.timer_id);
  979. if (d.io_id > 0)
  980. g_source_remove(d.io_id);
  981. g_io_channel_unref(io);
  982. g_obex_unref(obex);
  983. g_assert_no_error(d.err);
  984. }
  985. static void test_put_req_eagain(void)
  986. {
  987. GIOChannel *io;
  988. GIOCondition cond;
  989. GObex *obex;
  990. struct test_data d = { 0, NULL, {
  991. { put_req_first, sizeof(put_req_first) },
  992. { put_req_last, sizeof(put_req_last) } }, {
  993. { put_rsp_first, sizeof(put_rsp_first) },
  994. { put_rsp_last, sizeof(put_rsp_last) } } };
  995. create_endpoints(&obex, &io, SOCK_STREAM);
  996. d.obex = obex;
  997. d.provide_delay = 200;
  998. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  999. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1000. d.mainloop = g_main_loop_new(NULL, FALSE);
  1001. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1002. g_obex_put_req(obex, provide_eagain, transfer_complete, &d, &d.err,
  1003. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  1004. G_OBEX_HDR_NAME, "file.txt",
  1005. G_OBEX_HDR_INVALID);
  1006. g_assert_no_error(d.err);
  1007. g_main_loop_run(d.mainloop);
  1008. g_assert_cmpuint(d.count, ==, 2);
  1009. g_main_loop_unref(d.mainloop);
  1010. if (d.timer_id > 0)
  1011. g_source_remove(d.timer_id);
  1012. if (d.io_id > 0)
  1013. g_source_remove(d.io_id);
  1014. g_io_channel_unref(io);
  1015. g_obex_unref(obex);
  1016. g_assert_no_error(d.err);
  1017. }
  1018. static void test_get_rsp(void)
  1019. {
  1020. GIOChannel *io;
  1021. GIOCondition cond;
  1022. GObex *obex;
  1023. struct test_data d = { 0, NULL, {
  1024. { get_rsp_first, sizeof(get_rsp_first) },
  1025. { get_rsp_last, sizeof(get_rsp_last) } }, {
  1026. { get_req_last, sizeof(get_req_last) },
  1027. { NULL, 0 } } };
  1028. create_endpoints(&obex, &io, SOCK_STREAM);
  1029. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1030. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1031. d.mainloop = g_main_loop_new(NULL, FALSE);
  1032. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1033. g_obex_add_request_function(obex, G_OBEX_OP_GET, handle_get, &d);
  1034. g_io_channel_write_chars(io, (char *) get_req_first,
  1035. sizeof(get_req_first), NULL, &d.err);
  1036. g_assert_no_error(d.err);
  1037. g_main_loop_run(d.mainloop);
  1038. g_assert_cmpuint(d.count, ==, 1);
  1039. g_main_loop_unref(d.mainloop);
  1040. if (d.timer_id > 0)
  1041. g_source_remove(d.timer_id);
  1042. if (d.io_id > 0)
  1043. g_source_remove(d.io_id);
  1044. g_io_channel_unref(io);
  1045. g_obex_unref(obex);
  1046. g_assert_no_error(d.err);
  1047. }
  1048. static void handle_get_seq(GObex *obex, GObexPacket *req,
  1049. gpointer user_data)
  1050. {
  1051. struct test_data *d = user_data;
  1052. guint8 op = g_obex_packet_get_operation(req, NULL);
  1053. guint id;
  1054. if (op != G_OBEX_OP_GET) {
  1055. d->err = g_error_new(TEST_ERROR, TEST_ERROR_UNEXPECTED,
  1056. "Unexpected opcode 0x%02x", op);
  1057. g_main_loop_quit(d->mainloop);
  1058. return;
  1059. }
  1060. id = g_obex_get_rsp(obex, provide_seq, transfer_complete, d,
  1061. &d->err, G_OBEX_HDR_INVALID);
  1062. if (id == 0)
  1063. g_main_loop_quit(d->mainloop);
  1064. }
  1065. static void test_stream_get_rsp(void)
  1066. {
  1067. GIOChannel *io;
  1068. GIOCondition cond;
  1069. GObex *obex;
  1070. struct test_data d = { 0, NULL, {
  1071. { NULL, 0 },
  1072. { NULL, 0 },
  1073. { NULL, 0 },
  1074. { get_rsp_last, sizeof(get_rsp_last) } }, {
  1075. { get_req_last, sizeof(get_req_last) },
  1076. { get_req_last, sizeof(get_req_last) },
  1077. { get_req_last, sizeof(get_req_last) } } };
  1078. create_endpoints(&obex, &io, SOCK_STREAM);
  1079. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1080. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1081. d.mainloop = g_main_loop_new(NULL, FALSE);
  1082. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1083. g_obex_add_request_function(obex, G_OBEX_OP_GET, handle_get_seq, &d);
  1084. g_io_channel_write_chars(io, (char *) get_req_first,
  1085. sizeof(get_req_first), NULL, &d.err);
  1086. g_assert_no_error(d.err);
  1087. g_main_loop_run(d.mainloop);
  1088. g_assert_cmpuint(d.count, ==, RANDOM_PACKETS - 1);
  1089. g_main_loop_unref(d.mainloop);
  1090. if (d.timer_id > 0)
  1091. g_source_remove(d.timer_id);
  1092. if (d.io_id > 0)
  1093. g_source_remove(d.io_id);
  1094. g_io_channel_unref(io);
  1095. g_obex_unref(obex);
  1096. g_assert_no_error(d.err);
  1097. }
  1098. static void test_packet_get_rsp(void)
  1099. {
  1100. GIOChannel *io;
  1101. GIOCondition cond;
  1102. GObex *obex;
  1103. struct test_data d = { 0, NULL, {
  1104. { NULL, 0 },
  1105. { NULL, 0 },
  1106. { NULL, 0 },
  1107. { get_rsp_last, sizeof(get_rsp_last) } }, {
  1108. { NULL, 0 },
  1109. { NULL, 0 },
  1110. { get_req_last, sizeof(get_req_last) } } };
  1111. create_endpoints(&obex, &io, SOCK_SEQPACKET);
  1112. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1113. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1114. d.mainloop = g_main_loop_new(NULL, FALSE);
  1115. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1116. g_obex_add_request_function(obex, G_OBEX_OP_GET, handle_get_seq, &d);
  1117. g_io_channel_write_chars(io, (char *) get_req_first_srm,
  1118. sizeof(get_req_first_srm), NULL,
  1119. &d.err);
  1120. g_assert_no_error(d.err);
  1121. g_main_loop_run(d.mainloop);
  1122. g_assert_cmpuint(d.count, ==, RANDOM_PACKETS - 1);
  1123. g_main_loop_unref(d.mainloop);
  1124. if (d.timer_id > 0)
  1125. g_source_remove(d.timer_id);
  1126. if (d.io_id > 0)
  1127. g_source_remove(d.io_id);
  1128. g_io_channel_unref(io);
  1129. g_obex_unref(obex);
  1130. g_assert_no_error(d.err);
  1131. }
  1132. static void handle_get_seq_srm_wait(GObex *obex, GObexPacket *req,
  1133. gpointer user_data)
  1134. {
  1135. struct test_data *d = user_data;
  1136. guint8 op = g_obex_packet_get_operation(req, NULL);
  1137. guint id;
  1138. if (op != G_OBEX_OP_GET) {
  1139. d->err = g_error_new(TEST_ERROR, TEST_ERROR_UNEXPECTED,
  1140. "Unexpected opcode 0x%02x", op);
  1141. g_main_loop_quit(d->mainloop);
  1142. return;
  1143. }
  1144. id = g_obex_get_rsp(obex, provide_seq, transfer_complete, d,
  1145. &d->err,
  1146. G_OBEX_HDR_SRMP, G_OBEX_SRMP_WAIT,
  1147. G_OBEX_HDR_INVALID);
  1148. if (id == 0)
  1149. g_main_loop_quit(d->mainloop);
  1150. }
  1151. static void test_packet_get_rsp_wait(void)
  1152. {
  1153. GIOChannel *io;
  1154. GIOCondition cond;
  1155. GObex *obex;
  1156. struct test_data d = { 0, NULL, {
  1157. { NULL, 0 },
  1158. { NULL, 0 },
  1159. { NULL, 0 },
  1160. { get_rsp_last, sizeof(get_rsp_last) } }, {
  1161. { get_req_last, sizeof(get_req_last) },
  1162. { NULL, 0 },
  1163. { get_req_last, sizeof(get_req_last) } } };
  1164. create_endpoints(&obex, &io, SOCK_SEQPACKET);
  1165. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1166. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1167. d.mainloop = g_main_loop_new(NULL, FALSE);
  1168. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1169. g_obex_add_request_function(obex, G_OBEX_OP_GET,
  1170. handle_get_seq_srm_wait, &d);
  1171. g_io_channel_write_chars(io, (char *) get_req_first_srm,
  1172. sizeof(get_req_first_srm), NULL,
  1173. &d.err);
  1174. g_assert_no_error(d.err);
  1175. g_main_loop_run(d.mainloop);
  1176. g_assert_cmpuint(d.count, ==, RANDOM_PACKETS - 1);
  1177. g_main_loop_unref(d.mainloop);
  1178. if (d.timer_id > 0)
  1179. g_source_remove(d.timer_id);
  1180. if (d.io_id > 0)
  1181. g_source_remove(d.io_id);
  1182. g_io_channel_unref(io);
  1183. g_obex_unref(obex);
  1184. g_assert_no_error(d.err);
  1185. }
  1186. static void handle_get_app(GObex *obex, GObexPacket *req, gpointer user_data)
  1187. {
  1188. struct test_data *d = user_data;
  1189. guint8 op = g_obex_packet_get_operation(req, NULL);
  1190. GObexPacket *rsp;
  1191. if (op != G_OBEX_OP_GET) {
  1192. d->err = g_error_new(TEST_ERROR, TEST_ERROR_UNEXPECTED,
  1193. "Unexpected opcode 0x%02x", op);
  1194. g_main_loop_quit(d->mainloop);
  1195. return;
  1196. }
  1197. g_obex_add_request_function(d->obex, G_OBEX_OP_GET, handle_get, d);
  1198. rsp = g_obex_packet_new(G_OBEX_RSP_CONTINUE, TRUE,
  1199. G_OBEX_HDR_APPARAM, hdr_app, sizeof(hdr_app),
  1200. G_OBEX_HDR_INVALID);
  1201. if (g_obex_send(d->obex, rsp, NULL) == FALSE)
  1202. g_main_loop_quit(d->mainloop);
  1203. }
  1204. static void test_get_rsp_app(void)
  1205. {
  1206. GIOChannel *io;
  1207. GIOCondition cond;
  1208. GObex *obex;
  1209. struct test_data d = { 0, NULL, {
  1210. { get_rsp_first_app, sizeof(get_rsp_first_app) },
  1211. { get_rsp_first, sizeof(get_rsp_first) },
  1212. { get_rsp_last, sizeof(get_rsp_last) } }, {
  1213. { get_req_first, sizeof(get_req_first) },
  1214. { get_req_last, sizeof(get_req_last) },
  1215. { NULL, 0 } } };
  1216. create_endpoints(&obex, &io, SOCK_STREAM);
  1217. d.obex = obex;
  1218. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1219. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1220. d.mainloop = g_main_loop_new(NULL, FALSE);
  1221. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1222. g_obex_add_request_function(obex, G_OBEX_OP_GET, handle_get_app, &d);
  1223. g_io_channel_write_chars(io, (char *) get_req_first_app,
  1224. sizeof(get_req_first_app), NULL, &d.err);
  1225. g_assert_no_error(d.err);
  1226. g_main_loop_run(d.mainloop);
  1227. g_assert_cmpuint(d.count, ==, 2);
  1228. g_main_loop_unref(d.mainloop);
  1229. if (d.timer_id > 0)
  1230. g_source_remove(d.timer_id);
  1231. if (d.io_id > 0)
  1232. g_source_remove(d.io_id);
  1233. g_io_channel_unref(io);
  1234. g_obex_unref(obex);
  1235. g_assert_no_error(d.err);
  1236. }
  1237. static void test_put_req_delay(void)
  1238. {
  1239. GIOChannel *io;
  1240. GIOCondition cond;
  1241. GObex *obex;
  1242. struct test_data d = { 0, NULL, {
  1243. { put_req_first, sizeof(put_req_first) },
  1244. { put_req_last, sizeof(put_req_last) } }, {
  1245. { put_rsp_first, sizeof(put_rsp_first) },
  1246. { put_rsp_last, sizeof(put_rsp_last) } } };
  1247. create_endpoints(&obex, &io, SOCK_STREAM);
  1248. d.obex = obex;
  1249. d.provide_delay = 200;
  1250. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1251. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1252. d.mainloop = g_main_loop_new(NULL, FALSE);
  1253. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1254. g_obex_put_req(obex, provide_data, transfer_complete, &d, &d.err,
  1255. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  1256. G_OBEX_HDR_NAME, "file.txt",
  1257. G_OBEX_HDR_INVALID);
  1258. g_assert_no_error(d.err);
  1259. g_main_loop_run(d.mainloop);
  1260. g_assert_cmpuint(d.count, ==, 2);
  1261. g_main_loop_unref(d.mainloop);
  1262. if (d.timer_id > 0)
  1263. g_source_remove(d.timer_id);
  1264. if (d.io_id > 0)
  1265. g_source_remove(d.io_id);
  1266. g_io_channel_unref(io);
  1267. g_obex_unref(obex);
  1268. g_assert_no_error(d.err);
  1269. }
  1270. static void test_get_rsp_delay(void)
  1271. {
  1272. GIOChannel *io;
  1273. GIOCondition cond;
  1274. GObex *obex;
  1275. struct test_data d = { 0, NULL, {
  1276. { get_rsp_first, sizeof(get_rsp_first) },
  1277. { get_rsp_last, sizeof(get_rsp_last) } }, {
  1278. { get_req_last, sizeof(get_req_last) },
  1279. { NULL, 0 } } };
  1280. create_endpoints(&obex, &io, SOCK_STREAM);
  1281. d.obex = obex;
  1282. d.provide_delay = 200;
  1283. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1284. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1285. d.mainloop = g_main_loop_new(NULL, FALSE);
  1286. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1287. g_obex_add_request_function(obex, G_OBEX_OP_GET, handle_get, &d);
  1288. g_io_channel_write_chars(io, (char *) get_req_first,
  1289. sizeof(get_req_first), NULL, &d.err);
  1290. g_assert_no_error(d.err);
  1291. g_main_loop_run(d.mainloop);
  1292. g_assert_cmpuint(d.count, ==, 1);
  1293. g_main_loop_unref(d.mainloop);
  1294. if (d.timer_id > 0)
  1295. g_source_remove(d.timer_id);
  1296. if (d.io_id > 0)
  1297. g_source_remove(d.io_id);
  1298. g_io_channel_unref(io);
  1299. g_obex_unref(obex);
  1300. g_assert_no_error(d.err);
  1301. }
  1302. static gboolean rcv_data_delay(const void *buf, gsize len, gpointer user_data)
  1303. {
  1304. struct test_data *d = user_data;
  1305. if (len != sizeof(body_data))
  1306. d->err = g_error_new(TEST_ERROR, TEST_ERROR_UNEXPECTED,
  1307. "Unexpected byte count %zu", len);
  1308. if (memcmp(buf, body_data, sizeof(body_data)) != 0) {
  1309. dump_bufs(body_data, sizeof(body_data), buf, len);
  1310. d->err = g_error_new(TEST_ERROR, TEST_ERROR_UNEXPECTED,
  1311. "Unexpected byte count %zu", len);
  1312. }
  1313. if (d->provide_delay > 0) {
  1314. g_obex_suspend(d->obex);
  1315. g_timeout_add(d->provide_delay, resume_obex, d);
  1316. }
  1317. return TRUE;
  1318. }
  1319. static void handle_put_delay(GObex *obex, GObexPacket *req, gpointer user_data)
  1320. {
  1321. struct test_data *d = user_data;
  1322. guint8 op = g_obex_packet_get_operation(req, NULL);
  1323. guint id;
  1324. if (op != G_OBEX_OP_PUT) {
  1325. d->err = g_error_new(TEST_ERROR, TEST_ERROR_UNEXPECTED,
  1326. "Unexpected opcode 0x%02x", op);
  1327. g_main_loop_quit(d->mainloop);
  1328. return;
  1329. }
  1330. id = g_obex_put_rsp(obex, req, rcv_data_delay, transfer_complete, d,
  1331. &d->err, G_OBEX_HDR_INVALID);
  1332. if (id == 0)
  1333. g_main_loop_quit(d->mainloop);
  1334. }
  1335. static void test_put_rsp_delay(void)
  1336. {
  1337. GIOChannel *io;
  1338. GIOCondition cond;
  1339. GObex *obex;
  1340. struct test_data d = { 0, NULL, {
  1341. { put_rsp_first, sizeof(put_rsp_first) },
  1342. { put_rsp_last, sizeof(put_rsp_last) } }, {
  1343. { put_req_last, sizeof(put_req_last) },
  1344. { NULL, 0 } } };
  1345. create_endpoints(&obex, &io, SOCK_STREAM);
  1346. d.obex = obex;
  1347. d.provide_delay = 200;
  1348. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1349. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1350. d.mainloop = g_main_loop_new(NULL, FALSE);
  1351. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1352. g_obex_add_request_function(obex, G_OBEX_OP_PUT, handle_put_delay, &d);
  1353. g_io_channel_write_chars(io, (char *) put_req_first,
  1354. sizeof(put_req_first), NULL, &d.err);
  1355. g_assert_no_error(d.err);
  1356. g_main_loop_run(d.mainloop);
  1357. g_assert_cmpuint(d.count, ==, 1);
  1358. g_main_loop_unref(d.mainloop);
  1359. if (d.timer_id > 0)
  1360. g_source_remove(d.timer_id);
  1361. if (d.io_id > 0)
  1362. g_source_remove(d.io_id);
  1363. g_io_channel_unref(io);
  1364. g_obex_unref(obex);
  1365. g_assert_no_error(d.err);
  1366. }
  1367. static void test_get_req_delay(void)
  1368. {
  1369. GIOChannel *io;
  1370. GIOCondition cond;
  1371. GObex *obex;
  1372. struct test_data d = { 0, NULL, {
  1373. { get_req_first, sizeof(get_req_first) },
  1374. { get_req_last, sizeof(get_req_last) } }, {
  1375. { get_rsp_first, sizeof(get_rsp_first) },
  1376. { get_rsp_last, sizeof(get_rsp_last) } } };
  1377. create_endpoints(&obex, &io, SOCK_STREAM);
  1378. d.obex = obex;
  1379. d.provide_delay = 200;
  1380. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1381. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1382. d.mainloop = g_main_loop_new(NULL, FALSE);
  1383. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1384. g_obex_get_req(obex, rcv_data_delay, transfer_complete, &d, &d.err,
  1385. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  1386. G_OBEX_HDR_NAME, "file.txt",
  1387. G_OBEX_HDR_INVALID);
  1388. g_assert_no_error(d.err);
  1389. g_main_loop_run(d.mainloop);
  1390. g_assert_cmpuint(d.count, ==, 2);
  1391. g_main_loop_unref(d.mainloop);
  1392. if (d.timer_id > 0)
  1393. g_source_remove(d.timer_id);
  1394. if (d.io_id > 0)
  1395. g_source_remove(d.io_id);
  1396. g_io_channel_unref(io);
  1397. g_obex_unref(obex);
  1398. g_assert_no_error(d.err);
  1399. }
  1400. static void test_get_rsp_eagain(void)
  1401. {
  1402. GIOChannel *io;
  1403. GIOCondition cond;
  1404. GObex *obex;
  1405. struct test_data d = { 0, NULL, {
  1406. { get_rsp_first, sizeof(get_rsp_first) },
  1407. { get_rsp_last, sizeof(get_rsp_last) } }, {
  1408. { get_req_last, sizeof(get_req_last) },
  1409. { NULL, 0 } } };
  1410. create_endpoints(&obex, &io, SOCK_STREAM);
  1411. d.obex = obex;
  1412. d.provide_delay = 200;
  1413. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1414. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1415. d.mainloop = g_main_loop_new(NULL, FALSE);
  1416. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1417. g_obex_add_request_function(obex, G_OBEX_OP_GET, handle_get_eagain,
  1418. &d);
  1419. g_io_channel_write_chars(io, (char *) get_req_first,
  1420. sizeof(get_req_first), NULL, &d.err);
  1421. g_assert_no_error(d.err);
  1422. g_main_loop_run(d.mainloop);
  1423. g_assert_cmpuint(d.count, ==, 1);
  1424. g_main_loop_unref(d.mainloop);
  1425. if (d.timer_id > 0)
  1426. g_source_remove(d.timer_id);
  1427. if (d.io_id > 0)
  1428. g_source_remove(d.io_id);
  1429. g_io_channel_unref(io);
  1430. g_obex_unref(obex);
  1431. g_assert_no_error(d.err);
  1432. }
  1433. static void conn_complete(GObex *obex, GError *err, GObexPacket *rsp,
  1434. gpointer user_data)
  1435. {
  1436. struct test_data *d = user_data;
  1437. if (err != NULL)
  1438. d->err = g_error_copy(err);
  1439. g_main_loop_quit(d->mainloop);
  1440. }
  1441. static void test_conn_req(void)
  1442. {
  1443. GIOChannel *io;
  1444. GIOCondition cond;
  1445. GObex *obex;
  1446. struct test_data d = { 0, NULL, {
  1447. { conn_req, sizeof(conn_req) } }, {
  1448. { conn_rsp, sizeof(conn_rsp) } } };
  1449. create_endpoints(&obex, &io, SOCK_STREAM);
  1450. d.obex = obex;
  1451. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1452. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1453. d.mainloop = g_main_loop_new(NULL, FALSE);
  1454. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1455. g_obex_connect(obex, conn_complete, &d, &d.err, G_OBEX_HDR_INVALID);
  1456. g_assert_no_error(d.err);
  1457. g_main_loop_run(d.mainloop);
  1458. g_assert_cmpuint(d.count, ==, 1);
  1459. g_main_loop_unref(d.mainloop);
  1460. if (d.timer_id > 0)
  1461. g_source_remove(d.timer_id);
  1462. if (d.io_id > 0)
  1463. g_source_remove(d.io_id);
  1464. g_io_channel_unref(io);
  1465. g_obex_unref(obex);
  1466. g_assert_no_error(d.err);
  1467. }
  1468. static void handle_conn_rsp(GObex *obex, GObexPacket *req,
  1469. gpointer user_data)
  1470. {
  1471. struct test_data *d = user_data;
  1472. guint8 op = g_obex_packet_get_operation(req, NULL);
  1473. GObexPacket *rsp;
  1474. if (op != G_OBEX_OP_CONNECT) {
  1475. d->err = g_error_new(TEST_ERROR, TEST_ERROR_UNEXPECTED,
  1476. "Unexpected opcode 0x%02x", op);
  1477. g_main_loop_quit(d->mainloop);
  1478. return;
  1479. }
  1480. rsp = g_obex_packet_new(G_OBEX_RSP_SUCCESS, TRUE,
  1481. G_OBEX_HDR_CONNECTION, 1,
  1482. G_OBEX_HDR_INVALID);
  1483. g_obex_send(obex, rsp, &d->err);
  1484. }
  1485. static void test_conn_rsp(void)
  1486. {
  1487. GIOChannel *io;
  1488. GIOCondition cond;
  1489. GObex *obex;
  1490. struct test_data d = { 0, NULL, {
  1491. { conn_rsp, sizeof(conn_rsp) } }, {
  1492. { NULL, -1 } } };
  1493. create_endpoints(&obex, &io, SOCK_STREAM);
  1494. d.obex = obex;
  1495. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1496. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1497. d.mainloop = g_main_loop_new(NULL, FALSE);
  1498. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1499. g_obex_add_request_function(obex, G_OBEX_OP_CONNECT,
  1500. handle_conn_rsp, &d);
  1501. g_io_channel_write_chars(io, (char *) conn_req, sizeof(conn_req),
  1502. NULL, &d.err);
  1503. g_assert_no_error(d.err);
  1504. g_main_loop_run(d.mainloop);
  1505. g_assert_cmpuint(d.count, ==, 1);
  1506. g_main_loop_unref(d.mainloop);
  1507. if (d.timer_id > 0)
  1508. g_source_remove(d.timer_id);
  1509. if (d.io_id > 0)
  1510. g_source_remove(d.io_id);
  1511. g_io_channel_unref(io);
  1512. g_obex_unref(obex);
  1513. g_assert_no_error(d.err);
  1514. }
  1515. static void conn_complete_get_req(GObex *obex, GError *err, GObexPacket *rsp,
  1516. gpointer user_data)
  1517. {
  1518. struct test_data *d = user_data;
  1519. if (err != NULL) {
  1520. d->err = g_error_copy(err);
  1521. g_main_loop_quit(d->mainloop);
  1522. }
  1523. g_obex_get_req(obex, rcv_data, transfer_complete, d, &d->err,
  1524. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  1525. G_OBEX_HDR_NAME, "file.txt",
  1526. G_OBEX_HDR_INVALID);
  1527. }
  1528. static void test_conn_get_req(void)
  1529. {
  1530. GIOChannel *io;
  1531. GIOCondition cond;
  1532. GObex *obex;
  1533. struct test_data d = { 0, NULL, {
  1534. { conn_req, sizeof(conn_req) },
  1535. { conn_get_req_first, sizeof(conn_get_req_first) },
  1536. { get_req_last, sizeof(get_req_last) }}, {
  1537. { conn_rsp, sizeof(conn_rsp) } ,
  1538. { get_rsp_first, sizeof(get_rsp_first) },
  1539. { get_rsp_last, sizeof(get_rsp_last) } } };
  1540. create_endpoints(&obex, &io, SOCK_STREAM);
  1541. d.obex = obex;
  1542. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1543. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1544. d.mainloop = g_main_loop_new(NULL, FALSE);
  1545. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1546. g_obex_connect(obex, conn_complete_get_req, &d, &d.err,
  1547. G_OBEX_HDR_INVALID);
  1548. g_assert_no_error(d.err);
  1549. g_main_loop_run(d.mainloop);
  1550. g_assert_cmpuint(d.count, ==, 3);
  1551. g_main_loop_unref(d.mainloop);
  1552. if (d.timer_id > 0)
  1553. g_source_remove(d.timer_id);
  1554. if (d.io_id > 0)
  1555. g_source_remove(d.io_id);
  1556. g_io_channel_unref(io);
  1557. g_obex_unref(obex);
  1558. g_assert_no_error(d.err);
  1559. }
  1560. static void test_conn_get_rsp(void)
  1561. {
  1562. GIOChannel *io;
  1563. GIOCondition cond;
  1564. GObex *obex;
  1565. struct test_data d = { 0, NULL, {
  1566. { conn_rsp, sizeof(conn_rsp) },
  1567. { get_rsp_first, sizeof(get_rsp_first) },
  1568. { get_rsp_last, sizeof(get_rsp_last) } }, {
  1569. { conn_get_req_first, sizeof(conn_get_req_first) },
  1570. { get_req_last, sizeof(get_req_last) },
  1571. { NULL, 0 } } };
  1572. create_endpoints(&obex, &io, SOCK_STREAM);
  1573. d.obex = obex;
  1574. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1575. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1576. d.mainloop = g_main_loop_new(NULL, FALSE);
  1577. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1578. g_obex_add_request_function(obex, G_OBEX_OP_CONNECT,
  1579. handle_conn_rsp, &d);
  1580. g_obex_add_request_function(obex, G_OBEX_OP_GET,
  1581. handle_get, &d);
  1582. g_io_channel_write_chars(io, (char *) conn_req, sizeof(conn_req),
  1583. NULL, &d.err);
  1584. g_assert_no_error(d.err);
  1585. g_main_loop_run(d.mainloop);
  1586. g_assert_cmpuint(d.count, ==, 2);
  1587. g_main_loop_unref(d.mainloop);
  1588. if (d.timer_id > 0)
  1589. g_source_remove(d.timer_id);
  1590. if (d.io_id > 0)
  1591. g_source_remove(d.io_id);
  1592. g_io_channel_unref(io);
  1593. g_obex_unref(obex);
  1594. g_assert_no_error(d.err);
  1595. }
  1596. static void conn_complete_put_req(GObex *obex, GError *err, GObexPacket *rsp,
  1597. gpointer user_data)
  1598. {
  1599. struct test_data *d = user_data;
  1600. if (err != NULL) {
  1601. d->err = g_error_copy(err);
  1602. g_main_loop_quit(d->mainloop);
  1603. }
  1604. g_obex_put_req(obex, provide_data, transfer_complete, d, &d->err,
  1605. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  1606. G_OBEX_HDR_NAME, "file.txt",
  1607. G_OBEX_HDR_INVALID);
  1608. }
  1609. static void test_conn_put_req(void)
  1610. {
  1611. GIOChannel *io;
  1612. GIOCondition cond;
  1613. GObex *obex;
  1614. struct test_data d = { 0, NULL, {
  1615. { conn_req, sizeof(conn_req) },
  1616. { conn_put_req_first, sizeof(conn_put_req_first) },
  1617. { put_req_last, sizeof(put_req_last) }}, {
  1618. { conn_rsp, sizeof(conn_rsp) } ,
  1619. { put_rsp_first, sizeof(put_rsp_first) },
  1620. { put_rsp_last, sizeof(put_rsp_last) } } };
  1621. create_endpoints(&obex, &io, SOCK_STREAM);
  1622. d.obex = obex;
  1623. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1624. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1625. d.mainloop = g_main_loop_new(NULL, FALSE);
  1626. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1627. g_obex_connect(obex, conn_complete_put_req, &d, &d.err,
  1628. G_OBEX_HDR_INVALID);
  1629. g_assert_no_error(d.err);
  1630. g_main_loop_run(d.mainloop);
  1631. g_assert_cmpuint(d.count, ==, 3);
  1632. g_main_loop_unref(d.mainloop);
  1633. if (d.timer_id > 0)
  1634. g_source_remove(d.timer_id);
  1635. if (d.io_id > 0)
  1636. g_source_remove(d.io_id);
  1637. g_io_channel_unref(io);
  1638. g_obex_unref(obex);
  1639. g_assert_no_error(d.err);
  1640. }
  1641. static void test_conn_put_rsp(void)
  1642. {
  1643. GIOChannel *io;
  1644. GIOCondition cond;
  1645. GObex *obex;
  1646. struct test_data d = { 0, NULL, {
  1647. { conn_rsp, sizeof(conn_rsp) },
  1648. { put_rsp_first, sizeof(put_rsp_first) },
  1649. { put_rsp_last, sizeof(put_rsp_last) } }, {
  1650. { conn_put_req_first, sizeof(conn_put_req_first) },
  1651. { put_req_last, sizeof(put_req_last) },
  1652. { NULL, 0 } } };
  1653. create_endpoints(&obex, &io, SOCK_STREAM);
  1654. d.obex = obex;
  1655. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1656. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1657. d.mainloop = g_main_loop_new(NULL, FALSE);
  1658. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1659. g_obex_add_request_function(obex, G_OBEX_OP_CONNECT,
  1660. handle_conn_rsp, &d);
  1661. g_obex_add_request_function(obex, G_OBEX_OP_PUT,
  1662. handle_put, &d);
  1663. g_io_channel_write_chars(io, (char *) conn_req, sizeof(conn_req),
  1664. NULL, &d.err);
  1665. g_assert_no_error(d.err);
  1666. g_main_loop_run(d.mainloop);
  1667. g_assert_cmpuint(d.count, ==, 2);
  1668. g_main_loop_unref(d.mainloop);
  1669. if (d.timer_id > 0)
  1670. g_source_remove(d.timer_id);
  1671. if (d.io_id > 0)
  1672. g_source_remove(d.io_id);
  1673. g_io_channel_unref(io);
  1674. g_obex_unref(obex);
  1675. g_assert_no_error(d.err);
  1676. }
  1677. static void test_conn_get_wrg_rsp(void)
  1678. {
  1679. GIOChannel *io;
  1680. GIOCondition cond;
  1681. GObex *obex;
  1682. struct test_data d = { 0, NULL, {
  1683. { conn_rsp, sizeof(conn_rsp) },
  1684. { unavailable_rsp, sizeof(unavailable_rsp) } }, {
  1685. { conn_get_req_wrg, sizeof(conn_get_req_wrg) },
  1686. { NULL, -1 } } };
  1687. create_endpoints(&obex, &io, SOCK_STREAM);
  1688. d.obex = obex;
  1689. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1690. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1691. d.mainloop = g_main_loop_new(NULL, FALSE);
  1692. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1693. g_obex_add_request_function(obex, G_OBEX_OP_CONNECT,
  1694. handle_conn_rsp, &d);
  1695. g_io_channel_write_chars(io, (char *) conn_req, sizeof(conn_req),
  1696. NULL, &d.err);
  1697. g_assert_no_error(d.err);
  1698. g_main_loop_run(d.mainloop);
  1699. g_assert_cmpuint(d.count, ==, 2);
  1700. g_main_loop_unref(d.mainloop);
  1701. if (d.timer_id > 0)
  1702. g_source_remove(d.timer_id);
  1703. if (d.io_id > 0)
  1704. g_source_remove(d.io_id);
  1705. g_io_channel_unref(io);
  1706. g_obex_unref(obex);
  1707. g_assert_no_error(d.err);
  1708. }
  1709. static void conn_complete_put_req_seq(GObex *obex, GError *err,
  1710. GObexPacket *rsp, gpointer user_data)
  1711. {
  1712. struct test_data *d = user_data;
  1713. if (err != NULL) {
  1714. d->err = g_error_copy(err);
  1715. g_main_loop_quit(d->mainloop);
  1716. }
  1717. g_obex_put_req(obex, provide_seq, transfer_complete, d, &d->err,
  1718. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  1719. G_OBEX_HDR_NAME, "random.bin",
  1720. G_OBEX_HDR_INVALID);
  1721. }
  1722. static void test_conn_put_req_seq(void)
  1723. {
  1724. GIOChannel *io;
  1725. GIOCondition cond;
  1726. GObex *obex;
  1727. struct test_data d = { 0, NULL, {
  1728. { conn_req, sizeof(conn_req) } ,
  1729. { NULL, 0 },
  1730. { NULL, 0 },
  1731. { put_req_last, sizeof(put_req_last) } }, {
  1732. { conn_rsp, sizeof(conn_rsp) } ,
  1733. { put_rsp_first, sizeof(put_rsp_first) },
  1734. { put_rsp_first, sizeof(put_rsp_first) },
  1735. { put_rsp_last, sizeof(put_rsp_last) } } };
  1736. create_endpoints(&obex, &io, SOCK_STREAM);
  1737. d.obex = obex;
  1738. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1739. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1740. d.mainloop = g_main_loop_new(NULL, FALSE);
  1741. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1742. g_obex_connect(obex, conn_complete_put_req_seq, &d, &d.err,
  1743. G_OBEX_HDR_INVALID);
  1744. g_assert_no_error(d.err);
  1745. g_main_loop_run(d.mainloop);
  1746. g_assert_cmpuint(d.count, ==, RANDOM_PACKETS);
  1747. g_main_loop_unref(d.mainloop);
  1748. if (d.timer_id > 0)
  1749. g_source_remove(d.timer_id);
  1750. if (d.io_id > 0)
  1751. g_source_remove(d.io_id);
  1752. g_io_channel_unref(io);
  1753. g_obex_unref(obex);
  1754. g_assert_no_error(d.err);
  1755. }
  1756. static void conn_complete_put_req_seq_srm(GObex *obex, GError *err,
  1757. GObexPacket *rsp, gpointer user_data)
  1758. {
  1759. struct test_data *d = user_data;
  1760. if (err != NULL) {
  1761. d->err = g_error_copy(err);
  1762. g_main_loop_quit(d->mainloop);
  1763. }
  1764. g_obex_put_req(obex, provide_seq, transfer_complete, d, &d->err,
  1765. G_OBEX_HDR_TYPE, hdr_type, sizeof(hdr_type),
  1766. G_OBEX_HDR_NAME, "random.bin",
  1767. G_OBEX_HDR_INVALID);
  1768. }
  1769. static void test_conn_put_req_seq_srm(void)
  1770. {
  1771. GIOChannel *io;
  1772. GIOCondition cond;
  1773. GObex *obex;
  1774. struct test_data d = { 0, NULL, {
  1775. { conn_req_srm, sizeof(conn_req_srm) } ,
  1776. { NULL, 0 },
  1777. { NULL, 0 },
  1778. { put_req_last, sizeof(put_req_last) } }, {
  1779. { conn_rsp_srm, sizeof(conn_rsp_srm) } ,
  1780. { NULL, 0 },
  1781. { NULL, 0 },
  1782. { put_rsp_last, sizeof(put_rsp_last) } } };
  1783. create_endpoints(&obex, &io, SOCK_SEQPACKET);
  1784. d.obex = obex;
  1785. cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
  1786. d.io_id = g_io_add_watch(io, cond, test_io_cb, &d);
  1787. d.mainloop = g_main_loop_new(NULL, FALSE);
  1788. d.timer_id = g_timeout_add_seconds(1, test_timeout, &d);
  1789. g_obex_connect(obex, conn_complete_put_req_seq_srm, &d, &d.err,
  1790. G_OBEX_HDR_SRM, G_OBEX_SRM_INDICATE,
  1791. G_OBEX_HDR_INVALID);
  1792. g_assert_no_error(d.err);
  1793. g_main_loop_run(d.mainloop);
  1794. g_assert_cmpuint(d.count, ==, RANDOM_PACKETS);
  1795. g_main_loop_unref(d.mainloop);
  1796. if (d.timer_id > 0)
  1797. g_source_remove(d.timer_id);
  1798. if (d.io_id > 0)
  1799. g_source_remove(d.io_id);
  1800. g_io_channel_unref(io);
  1801. g_obex_unref(obex);
  1802. g_assert_no_error(d.err);
  1803. }
  1804. int main(int argc, char *argv[])
  1805. {
  1806. g_test_init(&argc, &argv, NULL);
  1807. g_test_add_func("/gobex/test_conn_req", test_conn_req);
  1808. g_test_add_func("/gobex/test_conn_rsp", test_conn_rsp);
  1809. g_test_add_func("/gobex/test_put_req", test_put_req);
  1810. g_test_add_func("/gobex/test_put_rsp", test_put_rsp);
  1811. g_test_add_func("/gobex/test_get_req", test_get_req);
  1812. g_test_add_func("/gobex/test_get_rsp", test_get_rsp);
  1813. g_test_add_func("/gobex/test_get_req_app", test_get_req_app);
  1814. g_test_add_func("/gobex/test_get_rsp_app", test_get_rsp_app);
  1815. g_test_add_func("/gobex/test_put_req_delay", test_put_req_delay);
  1816. g_test_add_func("/gobex/test_put_rsp_delay", test_put_rsp_delay);
  1817. g_test_add_func("/gobex/test_get_req_delay", test_get_req_delay);
  1818. g_test_add_func("/gobex/test_get_rsp_delay", test_get_rsp_delay);
  1819. g_test_add_func("/gobex/test_put_req_eagain", test_put_req_eagain);
  1820. g_test_add_func("/gobex/test_get_req_eagain", test_get_rsp_eagain);
  1821. g_test_add_func("/gobex/test_stream_put_req", test_stream_put_req);
  1822. g_test_add_func("/gobex/test_stream_put_rsp", test_stream_put_rsp);
  1823. g_test_add_func("/gobex/test_stream_put_req_abort",
  1824. test_stream_put_req_abort);
  1825. g_test_add_func("/gobex/test_stream_put_rsp_abort",
  1826. test_stream_put_rsp_abort);
  1827. g_test_add_func("/gobex/test_stream_get_req", test_stream_get_req);
  1828. g_test_add_func("/gobex/test_stream_get_rsp", test_stream_get_rsp);
  1829. g_test_add_func("/gobex/test_conn_get_req", test_conn_get_req);
  1830. g_test_add_func("/gobex/test_conn_get_rsp", test_conn_get_rsp);
  1831. g_test_add_func("/gobex/test_conn_put_req", test_conn_put_req);
  1832. g_test_add_func("/gobex/test_conn_put_rsp", test_conn_put_rsp);
  1833. g_test_add_func("/gobex/test_conn_get_wrg_rsp", test_conn_get_wrg_rsp);
  1834. g_test_add_func("/gobex/test_conn_put_req_seq",
  1835. test_conn_put_req_seq);
  1836. g_test_add_func("/gobex/test_packet_put_req", test_packet_put_req);
  1837. g_test_add_func("/gobex/test_packet_put_req_wait",
  1838. test_packet_put_req_wait);
  1839. g_test_add_func("/gobex/test_packet_put_req_suspend_resume",
  1840. test_packet_put_req_suspend_resume);
  1841. g_test_add_func("/gobex/test_packet_put_rsp", test_packet_put_rsp);
  1842. g_test_add_func("/gobex/test_packet_put_rsp_wait",
  1843. test_packet_put_rsp_wait);
  1844. g_test_add_func("/gobex/test_packet_get_rsp", test_packet_get_rsp);
  1845. g_test_add_func("/gobex/test_packet_get_rsp_wait",
  1846. test_packet_get_rsp_wait);
  1847. g_test_add_func("/gobex/test_packet_get_req", test_packet_get_req);
  1848. g_test_add_func("/gobex/test_packet_get_req_wait",
  1849. test_packet_get_req_wait);
  1850. g_test_add_func("/gobex/test_packet_get_req_suspend_resume",
  1851. test_packet_get_req_suspend_resume);
  1852. g_test_add_func("/gobex/test_packet_get_req_wait_next",
  1853. test_packet_get_req_wait_next);
  1854. g_test_add_func("/gobex/test_conn_put_req_seq_srm",
  1855. test_conn_put_req_seq_srm);
  1856. return g_test_run();
  1857. }