test_sorcery.c 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012 - 2013, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. * \brief Sorcery Unit Tests
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. *
  24. */
  25. /*** MODULEINFO
  26. <depend>TEST_FRAMEWORK</depend>
  27. <depend>func_sorcery</depend>
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "")
  32. #include "asterisk/test.h"
  33. #include "asterisk/module.h"
  34. #include "asterisk/astobj2.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/sorcery.h"
  37. #include "asterisk/logger.h"
  38. #include "asterisk/json.h"
  39. /*! \brief Dummy sorcery object */
  40. struct test_sorcery_object {
  41. SORCERY_OBJECT(details);
  42. unsigned int bob;
  43. unsigned int joe;
  44. struct ast_variable *jim;
  45. struct ast_variable *jack;
  46. };
  47. /*! \brief Internal function to destroy a test object */
  48. static void test_sorcery_object_destroy(void *obj)
  49. {
  50. struct test_sorcery_object *tobj = obj;
  51. ast_variables_destroy(tobj->jim);
  52. ast_variables_destroy(tobj->jack);
  53. }
  54. /*! \brief Internal function to allocate a test object */
  55. static void *test_sorcery_object_alloc(const char *id)
  56. {
  57. return ast_sorcery_generic_alloc(sizeof(struct test_sorcery_object), test_sorcery_object_destroy);
  58. }
  59. /*! \brief Internal function for object set transformation */
  60. static struct ast_variable *test_sorcery_transform(struct ast_variable *set)
  61. {
  62. struct ast_variable *field, *transformed = NULL;
  63. for (field = set; field; field = field->next) {
  64. struct ast_variable *transformed_field;
  65. if (!strcmp(field->name, "joe")) {
  66. transformed_field = ast_variable_new(field->name, "5000", "");
  67. } else {
  68. transformed_field = ast_variable_new(field->name, field->value, "");
  69. }
  70. if (!transformed_field) {
  71. ast_variables_destroy(transformed);
  72. return NULL;
  73. }
  74. transformed_field->next = transformed;
  75. transformed = transformed_field;
  76. }
  77. return transformed;
  78. }
  79. /*! \brief Internal function which copies pre-defined data into an object, natively */
  80. static int test_sorcery_copy(const void *src, void *dst)
  81. {
  82. struct test_sorcery_object *obj = dst;
  83. obj->bob = 10;
  84. obj->joe = 20;
  85. obj->jim = ast_variable_new("jim", "444", "");
  86. obj->jack = ast_variable_new("jack", "999,000", "");
  87. return 0;
  88. }
  89. /*! \brief Internal function which creates a pre-defined diff natively */
  90. static int test_sorcery_diff(const void *original, const void *modified, struct ast_variable **changes)
  91. {
  92. *changes = ast_variable_new("yes", "itworks", "");
  93. return 0;
  94. }
  95. /*! \brief Internal function which sets some values */
  96. static int test_sorcery_regex_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  97. {
  98. struct test_sorcery_object *test = obj;
  99. test->bob = 256;
  100. return 0;
  101. }
  102. /*! \brief Internal function which creates some ast_variable structures */
  103. static int test_sorcery_regex_fields(const void *obj, struct ast_variable **fields)
  104. {
  105. *fields = ast_variable_new("toast-bob", "10", "");
  106. return 0;
  107. }
  108. /*! \brief Test structure for caching */
  109. struct sorcery_test_caching {
  110. /*! \brief Whether the object has been created in the cache or not */
  111. unsigned int created:1;
  112. /*! \brief Whether the object has been updated in the cache or not */
  113. unsigned int updated:1;
  114. /*! \brief Whether the object has been deleted from the cache or not */
  115. unsigned int deleted:1;
  116. /*! \brief Object to return when asked */
  117. struct test_sorcery_object object;
  118. };
  119. /*! \brief Test structure for observer */
  120. struct sorcery_test_observer {
  121. /*! \brief Lock for notification */
  122. ast_mutex_t lock;
  123. /*! \brief Condition for notification */
  124. ast_cond_t cond;
  125. /*! \brief Pointer to the created object */
  126. const void *created;
  127. /*! \brief Pointer to the update object */
  128. const void *updated;
  129. /*! \brief Pointer to the deleted object */
  130. const void *deleted;
  131. /*! \brief Whether the type has been loaded */
  132. unsigned int loaded:1;
  133. };
  134. /*! \brief Global scope apply handler integer to make sure it executed */
  135. static int apply_handler_called;
  136. /*! \brief Simple apply handler which sets global scope integer to 1 if called */
  137. static int test_apply_handler(const struct ast_sorcery *sorcery, void *obj)
  138. {
  139. apply_handler_called = 1;
  140. return 0;
  141. }
  142. /*! \brief Global scope caching structure for testing */
  143. static struct sorcery_test_caching cache = { 0, };
  144. /*! \brief Global scope observer structure for testing */
  145. static struct sorcery_test_observer observer;
  146. static void *wizard2_data;
  147. static void *sorcery_test_open(const char *data)
  148. {
  149. wizard2_data = (void *)data;
  150. return wizard2_data;
  151. }
  152. static void sorcery_test_close(void *data)
  153. {
  154. }
  155. static int sorcery_test_create(const struct ast_sorcery *sorcery, void *data, void *object)
  156. {
  157. cache.created = 1;
  158. cache.updated = 0;
  159. cache.deleted = 0;
  160. return 0;
  161. }
  162. static void *sorcery_test_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
  163. {
  164. return (cache.created && !cache.deleted) ? ast_sorcery_alloc(sorcery, type, id) : NULL;
  165. }
  166. static int sorcery_test_update(const struct ast_sorcery *sorcery, void *data, void *object)
  167. {
  168. cache.updated = 1;
  169. return 0;
  170. }
  171. static int sorcery_test_delete(const struct ast_sorcery *sorcery, void *data, void *object)
  172. {
  173. cache.deleted = 1;
  174. return 0;
  175. }
  176. /*! \brief Dummy sorcery wizards, not actually used so we only populate the name and nothing else */
  177. static struct ast_sorcery_wizard test_wizard = {
  178. .name = "test",
  179. .create = sorcery_test_create,
  180. .retrieve_id = sorcery_test_retrieve_id,
  181. .update = sorcery_test_update,
  182. .delete = sorcery_test_delete,
  183. };
  184. static struct ast_sorcery_wizard test_wizard2 = {
  185. .name = "test2",
  186. .open = sorcery_test_open,
  187. .close = sorcery_test_close,
  188. .create = sorcery_test_create,
  189. .retrieve_id = sorcery_test_retrieve_id,
  190. .update = sorcery_test_update,
  191. .delete = sorcery_test_delete,
  192. };
  193. static void sorcery_observer_created(const void *object)
  194. {
  195. SCOPED_MUTEX(lock, &observer.lock);
  196. observer.created = object;
  197. ast_cond_signal(&observer.cond);
  198. }
  199. static void sorcery_observer_updated(const void *object)
  200. {
  201. SCOPED_MUTEX(lock, &observer.lock);
  202. observer.updated = object;
  203. ast_cond_signal(&observer.cond);
  204. }
  205. static void sorcery_observer_deleted(const void *object)
  206. {
  207. SCOPED_MUTEX(lock, &observer.lock);
  208. observer.deleted = object;
  209. ast_cond_signal(&observer.cond);
  210. }
  211. static void sorcery_observer_loaded(const char *object_type)
  212. {
  213. SCOPED_MUTEX(lock, &observer.lock);
  214. observer.loaded = 1;
  215. ast_cond_signal(&observer.cond);
  216. }
  217. /*! \brief Test sorcery observer implementation */
  218. static const struct ast_sorcery_observer test_observer = {
  219. .created = sorcery_observer_created,
  220. .updated = sorcery_observer_updated,
  221. .deleted = sorcery_observer_deleted,
  222. .loaded = sorcery_observer_loaded,
  223. };
  224. /* This handler takes a simple value and creates new list entry for it*/
  225. static int jim_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  226. {
  227. struct test_sorcery_object *tobj = obj;
  228. ast_variable_list_append(&tobj->jim, ast_variables_dup(var));
  229. return 0;
  230. }
  231. /* This handler takes a CSV string and creates new a new list entry for each value */
  232. static int jack_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  233. {
  234. struct test_sorcery_object *tobj = obj;
  235. char *jacks = ast_strdupa(var->value);
  236. char *val;
  237. while ((val = strsep(&jacks, ","))) {
  238. ast_variable_list_append(&tobj->jack, ast_variable_new("jack", val, ""));
  239. }
  240. return 0;
  241. }
  242. static int jim_vl(const void *obj, struct ast_variable **fields)
  243. {
  244. const struct test_sorcery_object *tobj = obj;
  245. if (tobj->jim) {
  246. *fields = ast_variables_dup(tobj->jim);
  247. }
  248. return 0;
  249. }
  250. static int jack_str(const void *obj, const intptr_t *args, char **buf)
  251. {
  252. const struct test_sorcery_object *tobj = obj;
  253. struct ast_variable *curr = tobj->jack;
  254. RAII_VAR(struct ast_str *, str, ast_str_create(128), ast_free);
  255. while(curr) {
  256. ast_str_append(&str, 0, "%s,", curr->value);
  257. curr = curr->next;
  258. }
  259. ast_str_truncate(str, -1);
  260. *buf = ast_strdup(ast_str_buffer(str));
  261. str = NULL;
  262. return 0;
  263. }
  264. static struct ast_sorcery *alloc_and_initialize_sorcery(void)
  265. {
  266. struct ast_sorcery *sorcery;
  267. if (!(sorcery = ast_sorcery_open())) {
  268. return NULL;
  269. }
  270. if ((ast_sorcery_apply_default(sorcery, "test", "memory", NULL) != AST_SORCERY_APPLY_SUCCESS) ||
  271. ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
  272. ast_sorcery_unref(sorcery);
  273. return NULL;
  274. }
  275. ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
  276. ast_sorcery_object_field_register_nodoc(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
  277. ast_sorcery_object_field_register_custom_nodoc(sorcery, "test", "jim", "444", jim_handler, NULL, jim_vl, 0, 0);
  278. ast_sorcery_object_field_register_custom_nodoc(sorcery, "test", "jack", "888,999", jack_handler, jack_str, NULL, 0, 0);
  279. return sorcery;
  280. }
  281. AST_TEST_DEFINE(wizard_registration)
  282. {
  283. switch (cmd) {
  284. case TEST_INIT:
  285. info->name = "wizard_registration";
  286. info->category = "/main/sorcery/";
  287. info->summary = "sorcery wizard registration and unregistration unit test";
  288. info->description =
  289. "Test registration and unregistration of a sorcery wizard";
  290. return AST_TEST_NOT_RUN;
  291. case TEST_EXECUTE:
  292. break;
  293. }
  294. if (ast_sorcery_wizard_register(&test_wizard)) {
  295. ast_test_status_update(test, "Failed to register a perfectly valid sorcery wizard\n");
  296. return AST_TEST_FAIL;
  297. }
  298. if (!ast_sorcery_wizard_register(&test_wizard)) {
  299. ast_test_status_update(test, "Successfully registered a sorcery wizard twice, which is bad\n");
  300. return AST_TEST_FAIL;
  301. }
  302. if (ast_sorcery_wizard_unregister(&test_wizard)) {
  303. ast_test_status_update(test, "Failed to unregister a perfectly valid sorcery wizard\n");
  304. return AST_TEST_FAIL;
  305. }
  306. if (!ast_sorcery_wizard_unregister(&test_wizard)) {
  307. ast_test_status_update(test, "Successfully unregistered a sorcery wizard twice, which is bad\n");
  308. return AST_TEST_FAIL;
  309. }
  310. return AST_TEST_PASS;
  311. }
  312. AST_TEST_DEFINE(sorcery_open)
  313. {
  314. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  315. RAII_VAR(struct ast_sorcery *, sorcery2, NULL, ast_sorcery_unref);
  316. int refcount;
  317. switch (cmd) {
  318. case TEST_INIT:
  319. info->name = "open";
  320. info->category = "/main/sorcery/";
  321. info->summary = "sorcery open/close unit test";
  322. info->description =
  323. "Test opening of sorcery and registry operations";
  324. return AST_TEST_NOT_RUN;
  325. case TEST_EXECUTE:
  326. break;
  327. }
  328. if ((sorcery = ast_sorcery_retrieve_by_module_name(AST_MODULE))) {
  329. ast_test_status_update(test, "There should NOT have been an existing sorcery instance\n");
  330. return AST_TEST_FAIL;
  331. }
  332. if (!(sorcery = ast_sorcery_open())) {
  333. ast_test_status_update(test, "Failed to open new sorcery structure\n");
  334. return AST_TEST_FAIL;
  335. }
  336. if (!(sorcery2 = ast_sorcery_retrieve_by_module_name(AST_MODULE))) {
  337. ast_test_status_update(test, "Failed to find sorcery structure in registry\n");
  338. return AST_TEST_FAIL;
  339. }
  340. if (sorcery2 != sorcery) {
  341. ast_test_status_update(test, "Should have gotten same sorcery on retrieve\n");
  342. return AST_TEST_FAIL;
  343. }
  344. ast_sorcery_unref(sorcery2);
  345. if ((refcount = ao2_ref(sorcery, 0)) != 2) {
  346. ast_test_status_update(test, "Should have been 2 references to sorcery instead of %d\n", refcount);
  347. return AST_TEST_FAIL;
  348. }
  349. if (!(sorcery2 = ast_sorcery_open())) {
  350. ast_test_status_update(test, "Failed to open second sorcery structure\n");
  351. return AST_TEST_FAIL;
  352. }
  353. if (sorcery2 != sorcery) {
  354. ast_test_status_update(test, "Should have gotten same sorcery on 2nd open\n");
  355. return AST_TEST_FAIL;
  356. }
  357. if ((refcount = ao2_ref(sorcery, 0)) != 3) {
  358. ast_test_status_update(test, "Should have been 3 references to sorcery instead of %d\n", refcount);
  359. return AST_TEST_FAIL;
  360. }
  361. ast_sorcery_unref(sorcery);
  362. ast_sorcery_unref(sorcery2);
  363. sorcery2 = NULL;
  364. if ((sorcery = ast_sorcery_retrieve_by_module_name(AST_MODULE))) {
  365. ast_sorcery_unref(sorcery);
  366. sorcery = NULL;
  367. ast_test_status_update(test, "Should NOT have found sorcery structure in registry\n");
  368. return AST_TEST_FAIL;
  369. }
  370. return AST_TEST_PASS;
  371. }
  372. AST_TEST_DEFINE(apply_default)
  373. {
  374. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  375. switch (cmd) {
  376. case TEST_INIT:
  377. info->name = "apply_default";
  378. info->category = "/main/sorcery/";
  379. info->summary = "sorcery default wizard unit test";
  380. info->description =
  381. "Test setting default type wizard in sorcery";
  382. return AST_TEST_NOT_RUN;
  383. case TEST_EXECUTE:
  384. break;
  385. }
  386. if (!(sorcery = ast_sorcery_open())) {
  387. ast_test_status_update(test, "Failed to open sorcery structure\n");
  388. return AST_TEST_FAIL;
  389. }
  390. if (ast_sorcery_apply_default(sorcery, "test", "dummy", NULL) != AST_SORCERY_APPLY_FAIL) {
  391. ast_test_status_update(test, "Successfully set a default wizard that doesn't exist\n");
  392. return AST_TEST_FAIL;
  393. }
  394. if (ast_sorcery_apply_default(sorcery, "test", "memory", NULL) != AST_SORCERY_APPLY_SUCCESS) {
  395. ast_test_status_update(test, "Failed to set a known wizard as a default\n");
  396. return AST_TEST_FAIL;
  397. }
  398. if (ast_sorcery_apply_default(sorcery, "test", "memory", NULL) != AST_SORCERY_APPLY_DEFAULT_UNNECESSARY) {
  399. ast_test_status_update(test, "Successfully set a default wizard on a type twice\n");
  400. return AST_TEST_FAIL;
  401. }
  402. return AST_TEST_PASS;
  403. }
  404. AST_TEST_DEFINE(apply_config)
  405. {
  406. struct ast_flags flags = { CONFIG_FLAG_NOCACHE };
  407. struct ast_config *config;
  408. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  409. switch (cmd) {
  410. case TEST_INIT:
  411. info->name = "apply_config";
  412. info->category = "/main/sorcery/";
  413. info->summary = "sorcery object mapping configuration unit test";
  414. info->description =
  415. "Test configured object mapping in sorcery";
  416. return AST_TEST_NOT_RUN;
  417. case TEST_EXECUTE:
  418. break;
  419. }
  420. if (!(config = ast_config_load2("sorcery.conf", "test_sorcery", flags))) {
  421. ast_test_status_update(test, "Sorcery configuration file not present - skipping apply_config test\n");
  422. return AST_TEST_NOT_RUN;
  423. }
  424. if (!ast_category_get(config, "test_sorcery_section", NULL)) {
  425. ast_test_status_update(test, "Sorcery configuration file does not have test_sorcery section\n");
  426. ast_config_destroy(config);
  427. return AST_TEST_NOT_RUN;
  428. }
  429. ast_config_destroy(config);
  430. if (!(sorcery = ast_sorcery_open())) {
  431. ast_test_status_update(test, "Failed to open sorcery structure\n");
  432. return AST_TEST_FAIL;
  433. }
  434. if (ast_sorcery_apply_config(sorcery, "test_sorcery_section") != AST_SORCERY_APPLY_SUCCESS) {
  435. ast_test_status_update(test, "Failed to apply configured object mappings\n");
  436. return AST_TEST_FAIL;
  437. }
  438. return AST_TEST_PASS;
  439. }
  440. AST_TEST_DEFINE(object_register)
  441. {
  442. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  443. switch (cmd) {
  444. case TEST_INIT:
  445. info->name = "object_register";
  446. info->category = "/main/sorcery/";
  447. info->summary = "sorcery object type registration unit test";
  448. info->description =
  449. "Test object type registration in sorcery";
  450. return AST_TEST_NOT_RUN;
  451. case TEST_EXECUTE:
  452. break;
  453. }
  454. if (!(sorcery = ast_sorcery_open())) {
  455. ast_test_status_update(test, "Failed to open structure\n");
  456. return AST_TEST_FAIL;
  457. }
  458. if (ast_sorcery_apply_default(sorcery, "test", "memory", NULL) != AST_SORCERY_APPLY_SUCCESS) {
  459. ast_test_status_update(test, "Failed to set a known wizard as a default\n");
  460. return AST_TEST_FAIL;
  461. }
  462. if (ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
  463. ast_test_status_update(test, "Failed to register object type\n");
  464. return AST_TEST_FAIL;
  465. }
  466. if (!ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
  467. ast_test_status_update(test, "Registered object type a second time, despite it being registered already\n");
  468. return AST_TEST_FAIL;
  469. }
  470. return AST_TEST_PASS;
  471. }
  472. AST_TEST_DEFINE(object_register_without_mapping)
  473. {
  474. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  475. switch (cmd) {
  476. case TEST_INIT:
  477. info->name = "object_register_without_mapping";
  478. info->category = "/main/sorcery/";
  479. info->summary = "sorcery object type registration (without mapping) unit test";
  480. info->description =
  481. "Test object type registration when no mapping exists in sorcery";
  482. return AST_TEST_NOT_RUN;
  483. case TEST_EXECUTE:
  484. break;
  485. }
  486. if (!(sorcery = ast_sorcery_open())) {
  487. ast_test_status_update(test, "Failed to open sorcery structure\n");
  488. return AST_TEST_FAIL;
  489. }
  490. if (!ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
  491. ast_test_status_update(test, "Registered object type when no object mapping exists\n");
  492. return AST_TEST_FAIL;
  493. }
  494. return AST_TEST_PASS;
  495. }
  496. AST_TEST_DEFINE(object_field_register)
  497. {
  498. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  499. switch (cmd) {
  500. case TEST_INIT:
  501. info->name = "object_field_register";
  502. info->category = "/main/sorcery/";
  503. info->summary = "sorcery object field registration unit test";
  504. info->description =
  505. "Test object field registration in sorcery with a provided id";
  506. return AST_TEST_NOT_RUN;
  507. case TEST_EXECUTE:
  508. break;
  509. }
  510. if (!(sorcery = ast_sorcery_open())) {
  511. ast_test_status_update(test, "Failed to open sorcery structure\n");
  512. return AST_TEST_FAIL;
  513. }
  514. if (!ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob))) {
  515. ast_test_status_update(test, "Registered an object field successfully when no mappings or object types exist\n");
  516. return AST_TEST_FAIL;
  517. }
  518. if (ast_sorcery_apply_default(sorcery, "test", "memory", NULL) != AST_SORCERY_APPLY_SUCCESS) {
  519. ast_test_status_update(test, "Failed to set a known wizard as a default\n");
  520. return AST_TEST_FAIL;
  521. }
  522. if (!ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob))) {
  523. ast_test_status_update(test, "Registered an object field successfully when object type does not exist\n");
  524. return AST_TEST_FAIL;
  525. }
  526. if (ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
  527. ast_test_status_update(test, "Failed to register object type\n");
  528. return AST_TEST_FAIL;
  529. }
  530. if (ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob))) {
  531. ast_test_status_update(test, "Could not successfully register object field when mapping and object type exists\n");
  532. return AST_TEST_FAIL;
  533. }
  534. return AST_TEST_PASS;
  535. }
  536. AST_TEST_DEFINE(object_fields_register)
  537. {
  538. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  539. switch (cmd) {
  540. case TEST_INIT:
  541. info->name = "object_fields_register";
  542. info->category = "/main/sorcery/";
  543. info->summary = "sorcery object regex fields registration unit test";
  544. info->description =
  545. "Test object regex fields registration in sorcery with a provided id";
  546. return AST_TEST_NOT_RUN;
  547. case TEST_EXECUTE:
  548. break;
  549. }
  550. if (!(sorcery = ast_sorcery_open())) {
  551. ast_test_status_update(test, "Failed to open sorcery structure\n");
  552. return AST_TEST_FAIL;
  553. }
  554. if (!ast_sorcery_object_fields_register(sorcery, "test", "^toast-", test_sorcery_regex_handler, test_sorcery_regex_fields)) {
  555. ast_test_status_update(test, "Registered a regex object field successfully when no mappings or object types exist\n");
  556. return AST_TEST_FAIL;
  557. }
  558. if (ast_sorcery_apply_default(sorcery, "test", "memory", NULL) != AST_SORCERY_APPLY_SUCCESS) {
  559. ast_test_status_update(test, "Failed to set a known wizard as a default\n");
  560. return AST_TEST_FAIL;
  561. }
  562. if (!ast_sorcery_object_fields_register(sorcery, "test", "^toast-", test_sorcery_regex_handler, test_sorcery_regex_fields)) {
  563. ast_test_status_update(test, "Registered a regex object field successfully when object type does not exist\n");
  564. return AST_TEST_FAIL;
  565. }
  566. if (ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
  567. ast_test_status_update(test, "Failed to register object type\n");
  568. return AST_TEST_FAIL;
  569. }
  570. if (ast_sorcery_object_fields_register(sorcery, "test", "^toast-", test_sorcery_regex_handler, test_sorcery_regex_fields)) {
  571. ast_test_status_update(test, "Registered a regex object field successfully when no mappings or object types exist\n");
  572. return AST_TEST_FAIL;
  573. }
  574. return AST_TEST_PASS;
  575. }
  576. AST_TEST_DEFINE(object_alloc_with_id)
  577. {
  578. int res = AST_TEST_PASS;
  579. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  580. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  581. switch (cmd) {
  582. case TEST_INIT:
  583. info->name = "object_alloc_with_id";
  584. info->category = "/main/sorcery/";
  585. info->summary = "sorcery object allocation (with id) unit test";
  586. info->description =
  587. "Test object allocation in sorcery with a provided id";
  588. return AST_TEST_NOT_RUN;
  589. case TEST_EXECUTE:
  590. break;
  591. }
  592. if (!(sorcery = alloc_and_initialize_sorcery())) {
  593. ast_test_status_update(test, "Failed to open sorcery structure\n");
  594. return AST_TEST_FAIL;
  595. }
  596. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  597. ast_test_status_update(test, "Failed to allocate a known object type\n");
  598. res = AST_TEST_FAIL;
  599. } else if (ast_strlen_zero(ast_sorcery_object_get_id(obj))) {
  600. ast_test_status_update(test, "Allocated object has empty id when it should not\n");
  601. res = AST_TEST_FAIL;
  602. } else if (strcmp(ast_sorcery_object_get_id(obj), "blah")) {
  603. ast_test_status_update(test, "Allocated object does not have correct id\n");
  604. res = AST_TEST_FAIL;
  605. } else if (ast_strlen_zero(ast_sorcery_object_get_type(obj))) {
  606. ast_test_status_update(test, "Allocated object has empty type when it should not\n");
  607. res = AST_TEST_FAIL;
  608. } else if (strcmp(ast_sorcery_object_get_type(obj), "test")) {
  609. ast_test_status_update(test, "Allocated object does not have correct type\n");
  610. res = AST_TEST_FAIL;
  611. } else if ((obj->bob != 5) || (obj->joe != 10)) {
  612. ast_test_status_update(test, "Allocated object does not have defaults set as it should\n");
  613. res = AST_TEST_FAIL;
  614. }
  615. return res;
  616. }
  617. AST_TEST_DEFINE(object_alloc_without_id)
  618. {
  619. int res = AST_TEST_PASS;
  620. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  621. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  622. switch (cmd) {
  623. case TEST_INIT:
  624. info->name = "object_alloc_without_id";
  625. info->category = "/main/sorcery/";
  626. info->summary = "sorcery object allocation (without id) unit test";
  627. info->description =
  628. "Test object allocation in sorcery with no provided id";
  629. return AST_TEST_NOT_RUN;
  630. case TEST_EXECUTE:
  631. break;
  632. }
  633. if (!(sorcery = alloc_and_initialize_sorcery())) {
  634. ast_test_status_update(test, "Failed to open sorcery structure\n");
  635. return AST_TEST_FAIL;
  636. }
  637. if (!(obj = ast_sorcery_alloc(sorcery, "test", NULL))) {
  638. ast_test_status_update(test, "Failed to allocate a known object type\n");
  639. res = AST_TEST_FAIL;
  640. } else if (ast_strlen_zero(ast_sorcery_object_get_id(obj))) {
  641. ast_test_status_update(test, "Allocated object has empty id when it should not\n");
  642. res = AST_TEST_FAIL;
  643. }
  644. return res;
  645. }
  646. AST_TEST_DEFINE(object_copy)
  647. {
  648. int res = AST_TEST_PASS;
  649. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  650. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  651. RAII_VAR(struct test_sorcery_object *, copy, NULL, ao2_cleanup);
  652. switch (cmd) {
  653. case TEST_INIT:
  654. info->name = "object_copy";
  655. info->category = "/main/sorcery/";
  656. info->summary = "sorcery object copy unit test";
  657. info->description =
  658. "Test object copy in sorcery";
  659. return AST_TEST_NOT_RUN;
  660. case TEST_EXECUTE:
  661. break;
  662. }
  663. if (!(sorcery = alloc_and_initialize_sorcery())) {
  664. ast_test_status_update(test, "Failed to open sorcery structure\n");
  665. return AST_TEST_FAIL;
  666. }
  667. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  668. ast_test_status_update(test, "Failed to allocate a known object type\n");
  669. return AST_TEST_FAIL;
  670. }
  671. obj->bob = 50;
  672. obj->joe = 100;
  673. jim_handler(NULL, ast_variable_new("jim", "444", ""), obj);
  674. jim_handler(NULL, ast_variable_new("jim", "555", ""), obj);
  675. if (!(copy = ast_sorcery_copy(sorcery, obj))) {
  676. ast_test_status_update(test, "Failed to create a copy of a known valid object\n");
  677. res = AST_TEST_FAIL;
  678. } else if (copy == obj) {
  679. ast_test_status_update(test, "Created copy is actually the original object\n");
  680. res = AST_TEST_FAIL;
  681. } else if (copy->bob != obj->bob) {
  682. ast_test_status_update(test, "Value of 'bob' on newly created copy is not the same as original\n");
  683. res = AST_TEST_FAIL;
  684. } else if (copy->joe != obj->joe) {
  685. ast_test_status_update(test, "Value of 'joe' on newly created copy is not the same as original\n");
  686. res = AST_TEST_FAIL;
  687. } else if (!copy->jim) {
  688. ast_test_status_update(test, "A new ast_variable was not created for 'jim'\n");
  689. res = AST_TEST_FAIL;
  690. } else if (copy->jim == obj->jim) {
  691. ast_test_status_update(test, "Created copy of 'jim' is actually the ogirinal 'jim'\n");
  692. res = AST_TEST_FAIL;
  693. } else if (strcmp(copy->jim->value, obj->jim->value)) {
  694. ast_test_status_update(test, "Value of 1st 'jim' on newly created copy is not the same as original\n");
  695. res = AST_TEST_FAIL;
  696. } else if (!copy->jim->next) {
  697. ast_test_status_update(test, "A new ast_variable was not created for 2nd 'jim'\n");
  698. res = AST_TEST_FAIL;
  699. } else if (strcmp(copy->jim->next->value, obj->jim->next->value)) {
  700. ast_test_status_update(test, "Value of 2nd 'jim' (%s %s) on newly created copy is not the same as original (%s %s)\n",
  701. copy->jim->value, copy->jim->next->value, obj->jim->value, obj->jim->next->value);
  702. res = AST_TEST_FAIL;
  703. }
  704. return res;
  705. }
  706. AST_TEST_DEFINE(object_copy_native)
  707. {
  708. int res = AST_TEST_PASS;
  709. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  710. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  711. RAII_VAR(struct test_sorcery_object *, copy, NULL, ao2_cleanup);
  712. switch (cmd) {
  713. case TEST_INIT:
  714. info->name = "object_copy_native";
  715. info->category = "/main/sorcery/";
  716. info->summary = "sorcery object native copy unit test";
  717. info->description =
  718. "Test object native copy in sorcery";
  719. return AST_TEST_NOT_RUN;
  720. case TEST_EXECUTE:
  721. break;
  722. }
  723. if (!(sorcery = alloc_and_initialize_sorcery())) {
  724. ast_test_status_update(test, "Failed to open sorcery structure\n");
  725. return AST_TEST_FAIL;
  726. }
  727. ast_sorcery_object_set_copy_handler(sorcery, "test", test_sorcery_copy);
  728. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  729. ast_test_status_update(test, "Failed to allocate a known object type\n");
  730. return AST_TEST_FAIL;
  731. }
  732. obj->bob = 50;
  733. obj->joe = 100;
  734. if (!(copy = ast_sorcery_copy(sorcery, obj))) {
  735. ast_test_status_update(test, "Failed to create a copy of a known valid object\n");
  736. res = AST_TEST_FAIL;
  737. } else if (copy == obj) {
  738. ast_test_status_update(test, "Created copy is actually the original object\n");
  739. res = AST_TEST_FAIL;
  740. } else if (copy->bob != 10) {
  741. ast_test_status_update(test, "Value of 'bob' on newly created copy is not the predefined native copy value\n");
  742. res = AST_TEST_FAIL;
  743. } else if (copy->joe != 20) {
  744. ast_test_status_update(test, "Value of 'joe' on newly created copy is not the predefined native copy value\n");
  745. res = AST_TEST_FAIL;
  746. } else if (!copy->jim) {
  747. ast_test_status_update(test, "A new ast_variable was not created for 'jim'\n");
  748. res = AST_TEST_FAIL;
  749. } else if (strcmp(copy->jim->value, "444")) {
  750. ast_test_status_update(test, "Value of 'jim' on newly created copy is not the predefined native copy value\n");
  751. res = AST_TEST_FAIL;
  752. }
  753. return res;
  754. }
  755. AST_TEST_DEFINE(object_diff)
  756. {
  757. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  758. RAII_VAR(struct test_sorcery_object *, obj1, NULL, ao2_cleanup);
  759. RAII_VAR(struct test_sorcery_object *, obj2, NULL, ao2_cleanup);
  760. RAII_VAR(struct ast_variable *, changes, NULL, ast_variables_destroy);
  761. struct ast_variable *field;
  762. int res = AST_TEST_PASS;
  763. int jims = 0;
  764. switch (cmd) {
  765. case TEST_INIT:
  766. info->name = "object_diff";
  767. info->category = "/main/sorcery/";
  768. info->summary = "sorcery object diff unit test";
  769. info->description =
  770. "Test object diffing in sorcery";
  771. return AST_TEST_NOT_RUN;
  772. case TEST_EXECUTE:
  773. break;
  774. }
  775. if (!(sorcery = alloc_and_initialize_sorcery())) {
  776. ast_test_status_update(test, "Failed to open sorcery structure\n");
  777. return AST_TEST_FAIL;
  778. }
  779. if (!(obj1 = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  780. ast_test_status_update(test, "Failed to allocate a known object type\n");
  781. return AST_TEST_FAIL;
  782. }
  783. obj1->bob = 99;
  784. obj1->joe = 55;
  785. jim_handler(NULL, ast_variable_new("jim", "444", ""), obj1);
  786. jim_handler(NULL, ast_variable_new("jim", "555", ""), obj1);
  787. if (!(obj2 = ast_sorcery_alloc(sorcery, "test", "blah2"))) {
  788. ast_test_status_update(test, "Failed to allocate a second known object type\n");
  789. return AST_TEST_FAIL;
  790. }
  791. obj2->bob = 99;
  792. obj2->joe = 42;
  793. jim_handler(NULL, ast_variable_new("jim", "444", ""), obj2);
  794. jim_handler(NULL, ast_variable_new("jim", "666", ""), obj2);
  795. jim_handler(NULL, ast_variable_new("jim", "777", ""), obj2);
  796. if (ast_sorcery_diff(sorcery, obj1, obj2, &changes)) {
  797. ast_test_status_update(test, "Failed to diff obj1 and obj2\n");
  798. } else if (!changes) {
  799. ast_test_status_update(test, "Failed to produce a diff of two objects, despite there being differences\n");
  800. return AST_TEST_FAIL;
  801. }
  802. for (field = changes; field; field = field->next) {
  803. if (!strcmp(field->name, "joe")) {
  804. if (strcmp(field->value, "42")) {
  805. ast_test_status_update(test,
  806. "Object diff produced unexpected value '%s' for joe\n", field->value);
  807. res = AST_TEST_FAIL;
  808. }
  809. } else if (!strcmp(field->name, "jim")) {
  810. jims++;
  811. if (!strcmp(field->value, "555")) {
  812. ast_test_status_update(test,
  813. "Object diff produced unexpected value '%s' for jim\n", field->value);
  814. res = AST_TEST_FAIL;
  815. }
  816. } else {
  817. ast_test_status_update(test, "Object diff produced unexpected field '%s'\n",
  818. field->name);
  819. res = AST_TEST_FAIL;
  820. }
  821. }
  822. if (jims != 2) {
  823. ast_test_status_update(test, "Object diff didn't produce 2 jims\n");
  824. res = AST_TEST_FAIL;
  825. }
  826. return res;
  827. }
  828. AST_TEST_DEFINE(object_diff_native)
  829. {
  830. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  831. RAII_VAR(struct test_sorcery_object *, obj1, NULL, ao2_cleanup);
  832. RAII_VAR(struct test_sorcery_object *, obj2, NULL, ao2_cleanup);
  833. RAII_VAR(struct ast_variable *, changes, NULL, ast_variables_destroy);
  834. struct ast_variable *field;
  835. int res = AST_TEST_PASS;
  836. switch (cmd) {
  837. case TEST_INIT:
  838. info->name = "object_diff_native";
  839. info->category = "/main/sorcery/";
  840. info->summary = "sorcery object native diff unit test";
  841. info->description =
  842. "Test native object diffing in sorcery";
  843. return AST_TEST_NOT_RUN;
  844. case TEST_EXECUTE:
  845. break;
  846. }
  847. if (!(sorcery = alloc_and_initialize_sorcery())) {
  848. ast_test_status_update(test, "Failed to open sorcery structure\n");
  849. return AST_TEST_FAIL;
  850. }
  851. ast_sorcery_object_set_diff_handler(sorcery, "test", test_sorcery_diff);
  852. if (!(obj1 = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  853. ast_test_status_update(test, "Failed to allocate a known object type\n");
  854. return AST_TEST_FAIL;
  855. }
  856. obj1->bob = 99;
  857. obj1->joe = 55;
  858. if (!(obj2 = ast_sorcery_alloc(sorcery, "test", "blah2"))) {
  859. ast_test_status_update(test, "Failed to allocate a second known object type\n");
  860. return AST_TEST_FAIL;
  861. }
  862. obj2->bob = 99;
  863. obj2->joe = 42;
  864. if (ast_sorcery_diff(sorcery, obj1, obj2, &changes)) {
  865. ast_test_status_update(test, "Failed to diff obj1 and obj2\n");
  866. } else if (!changes) {
  867. ast_test_status_update(test, "Failed to produce a diff of two objects, despite there being differences\n");
  868. return AST_TEST_FAIL;
  869. }
  870. for (field = changes; field; field = field->next) {
  871. if (!strcmp(field->name, "yes")) {
  872. if (strcmp(field->value, "itworks")) {
  873. ast_test_status_update(test, "Object diff produced unexpected value '%s' for joe\n", field->value);
  874. res = AST_TEST_FAIL;
  875. }
  876. } else {
  877. ast_test_status_update(test, "Object diff produced unexpected field '%s'\n", field->name);
  878. res = AST_TEST_FAIL;
  879. }
  880. }
  881. return res;
  882. }
  883. AST_TEST_DEFINE(objectset_create)
  884. {
  885. int res = AST_TEST_PASS;
  886. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  887. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  888. RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
  889. struct ast_variable *field;
  890. switch (cmd) {
  891. case TEST_INIT:
  892. info->name = "objectset_create";
  893. info->category = "/main/sorcery/";
  894. info->summary = "sorcery object set creation unit test";
  895. info->description =
  896. "Test object set creation in sorcery";
  897. return AST_TEST_NOT_RUN;
  898. case TEST_EXECUTE:
  899. break;
  900. }
  901. if (!(sorcery = alloc_and_initialize_sorcery())) {
  902. ast_test_status_update(test, "Failed to open sorcery structure\n");
  903. return AST_TEST_FAIL;
  904. }
  905. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  906. ast_test_status_update(test, "Failed to allocate a known object type\n");
  907. return AST_TEST_FAIL;
  908. }
  909. if (!(objset = ast_sorcery_objectset_create(sorcery, obj))) {
  910. ast_test_status_update(test, "Failed to create an object set for a known sane object\n");
  911. return AST_TEST_FAIL;
  912. }
  913. for (field = objset; field; field = field->next) {
  914. if (!strcmp(field->name, "bob")) {
  915. if (strcmp(field->value, "5")) {
  916. ast_test_status_update(test, "Object set failed to create proper value for 'bob'\n");
  917. res = AST_TEST_FAIL;
  918. }
  919. } else if (!strcmp(field->name, "joe")) {
  920. if (strcmp(field->value, "10")) {
  921. ast_test_status_update(test, "Object set failed to create proper value for 'joe'\n");
  922. res = AST_TEST_FAIL;
  923. }
  924. } else if (!strcmp(field->name, "jim")) {
  925. if (strcmp(field->value, "444")) {
  926. ast_test_status_update(test, "Object set failed to create proper value for 'jim'\n");
  927. res = AST_TEST_FAIL;
  928. }
  929. } else if (!strcmp(field->name, "jack")) {
  930. if (strcmp(field->value, "888,999")) {
  931. ast_test_status_update(test, "Object set failed to create proper value (%s) for 'jack'\n", field->value);
  932. res = AST_TEST_FAIL;
  933. }
  934. } else {
  935. ast_test_status_update(test, "Object set created field '%s' which is unknown\n", field->name);
  936. res = AST_TEST_FAIL;
  937. }
  938. }
  939. return res;
  940. }
  941. AST_TEST_DEFINE(objectset_json_create)
  942. {
  943. int res = AST_TEST_PASS;
  944. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  945. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  946. RAII_VAR(struct ast_json *, objset, NULL, ast_json_unref);
  947. struct ast_json_iter *field;
  948. switch (cmd) {
  949. case TEST_INIT:
  950. info->name = "objectset_json_create";
  951. info->category = "/main/sorcery/";
  952. info->summary = "sorcery json object set creation unit test";
  953. info->description =
  954. "Test object set creation (for JSON format) in sorcery";
  955. return AST_TEST_NOT_RUN;
  956. case TEST_EXECUTE:
  957. break;
  958. }
  959. if (!(sorcery = alloc_and_initialize_sorcery())) {
  960. ast_test_status_update(test, "Failed to open sorcery structure\n");
  961. return AST_TEST_FAIL;
  962. }
  963. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  964. ast_test_status_update(test, "Failed to allocate a known object type\n");
  965. return AST_TEST_FAIL;
  966. }
  967. if (!(objset = ast_sorcery_objectset_json_create(sorcery, obj))) {
  968. ast_test_status_update(test, "Failed to create an object set for a known sane object\n");
  969. return AST_TEST_FAIL;
  970. }
  971. for (field = ast_json_object_iter(objset); field; field = ast_json_object_iter_next(objset, field)) {
  972. struct ast_json *value = ast_json_object_iter_value(field);
  973. if (!strcmp(ast_json_object_iter_key(field), "bob")) {
  974. if (strcmp(ast_json_string_get(value), "5")) {
  975. ast_test_status_update(test, "Object set failed to create proper value for 'bob'\n");
  976. res = AST_TEST_FAIL;
  977. }
  978. } else if (!strcmp(ast_json_object_iter_key(field), "joe")) {
  979. if (strcmp(ast_json_string_get(value), "10")) {
  980. ast_test_status_update(test, "Object set failed to create proper value for 'joe'\n");
  981. res = AST_TEST_FAIL;
  982. }
  983. } else if (!strcmp(ast_json_object_iter_key(field), "jim")) {
  984. if (strcmp(ast_json_string_get(value), "444")) {
  985. ast_test_status_update(test, "Object set failed to create proper value for 'jim'\n");
  986. res = AST_TEST_FAIL;
  987. }
  988. } else if (!strcmp(ast_json_object_iter_key(field), "jack")) {
  989. if (strcmp(ast_json_string_get(value), "888,999")) {
  990. ast_test_status_update(test, "Object set failed to create proper value for 'jack'\n");
  991. res = AST_TEST_FAIL;
  992. }
  993. } else {
  994. ast_test_status_update(test, "Object set created field '%s' which is unknown\n", ast_json_object_iter_key(field));
  995. res = AST_TEST_FAIL;
  996. }
  997. }
  998. return res;
  999. }
  1000. AST_TEST_DEFINE(objectset_create_regex)
  1001. {
  1002. int res = AST_TEST_PASS;
  1003. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  1004. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  1005. RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
  1006. struct ast_variable *field;
  1007. switch (cmd) {
  1008. case TEST_INIT:
  1009. info->name = "objectset_create_regex";
  1010. info->category = "/main/sorcery/";
  1011. info->summary = "sorcery object set creation with regex fields unit test";
  1012. info->description =
  1013. "Test object set creation with regex fields in sorcery";
  1014. return AST_TEST_NOT_RUN;
  1015. case TEST_EXECUTE:
  1016. break;
  1017. }
  1018. if (!(sorcery = ast_sorcery_open())) {
  1019. ast_test_status_update(test, "Failed to open sorcery structure\n");
  1020. return AST_TEST_FAIL;
  1021. }
  1022. if ((ast_sorcery_apply_default(sorcery, "test", "memory", NULL) != AST_SORCERY_APPLY_SUCCESS) ||
  1023. ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, test_apply_handler)) {
  1024. ast_test_status_update(test, "Failed to register 'test' object type\n");
  1025. return AST_TEST_FAIL;
  1026. }
  1027. ast_sorcery_object_fields_register(sorcery, "test", "^toast-", test_sorcery_regex_handler, test_sorcery_regex_fields);
  1028. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  1029. ast_test_status_update(test, "Failed to allocate a known object type\n");
  1030. return AST_TEST_FAIL;
  1031. }
  1032. if (!(objset = ast_sorcery_objectset_create(sorcery, obj))) {
  1033. ast_test_status_update(test, "Failed to create an object set for a known sane object\n");
  1034. return AST_TEST_FAIL;
  1035. }
  1036. for (field = objset; field; field = field->next) {
  1037. if (!strcmp(field->name, "toast-bob")) {
  1038. if (strcmp(field->value, "10")) {
  1039. ast_test_status_update(test, "Object set failed to create proper value for 'bob'\n");
  1040. res = AST_TEST_FAIL;
  1041. }
  1042. } else {
  1043. ast_test_status_update(test, "Object set created field '%s' which is unknown\n", field->name);
  1044. res = AST_TEST_FAIL;
  1045. }
  1046. }
  1047. return res;
  1048. }
  1049. AST_TEST_DEFINE(objectset_apply)
  1050. {
  1051. int res = AST_TEST_PASS;
  1052. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  1053. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  1054. RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
  1055. switch (cmd) {
  1056. case TEST_INIT:
  1057. info->name = "objectset_apply";
  1058. info->category = "/main/sorcery/";
  1059. info->summary = "sorcery object apply unit test";
  1060. info->description =
  1061. "Test object set applying in sorcery";
  1062. return AST_TEST_NOT_RUN;
  1063. case TEST_EXECUTE:
  1064. break;
  1065. }
  1066. if (!(sorcery = alloc_and_initialize_sorcery())) {
  1067. ast_test_status_update(test, "Failed to open sorcery structure\n");
  1068. return AST_TEST_FAIL;
  1069. }
  1070. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  1071. ast_test_status_update(test, "Failed to allocate a known object type\n");
  1072. return AST_TEST_FAIL;
  1073. }
  1074. if (!(objset = ast_variable_new("joe", "25", ""))) {
  1075. ast_test_status_update(test, "Failed to create an object set, test could not occur\n");
  1076. res = AST_TEST_FAIL;
  1077. } else if (ast_sorcery_objectset_apply(sorcery, obj, objset)) {
  1078. ast_test_status_update(test, "Failed to apply valid object set to object\n");
  1079. res = AST_TEST_FAIL;
  1080. } else if (obj->joe != 25) {
  1081. ast_test_status_update(test, "Object set was not actually applied to object despite it returning success\n");
  1082. res = AST_TEST_FAIL;
  1083. }
  1084. return res;
  1085. }
  1086. AST_TEST_DEFINE(objectset_apply_handler)
  1087. {
  1088. int res = AST_TEST_PASS;
  1089. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  1090. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  1091. RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
  1092. switch (cmd) {
  1093. case TEST_INIT:
  1094. info->name = "objectset_apply_handler";
  1095. info->category = "/main/sorcery/";
  1096. info->summary = "sorcery object apply handler unit test";
  1097. info->description =
  1098. "Test object set apply handler call in sorcery";
  1099. return AST_TEST_NOT_RUN;
  1100. case TEST_EXECUTE:
  1101. break;
  1102. }
  1103. if (!(sorcery = ast_sorcery_open())) {
  1104. ast_test_status_update(test, "Failed to open sorcery structure\n");
  1105. return AST_TEST_FAIL;
  1106. }
  1107. if ((ast_sorcery_apply_default(sorcery, "test", "memory", NULL) != AST_SORCERY_APPLY_SUCCESS) ||
  1108. ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, test_apply_handler)) {
  1109. ast_test_status_update(test, "Failed to register 'test' object type\n");
  1110. return AST_TEST_FAIL;
  1111. }
  1112. ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
  1113. ast_sorcery_object_field_register_nodoc(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
  1114. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  1115. ast_test_status_update(test, "Failed to allocate a known object type\n");
  1116. return AST_TEST_FAIL;
  1117. }
  1118. apply_handler_called = 0;
  1119. if (!(objset = ast_variable_new("joe", "25", ""))) {
  1120. ast_test_status_update(test, "Failed to create an object set, test could not occur\n");
  1121. res = AST_TEST_FAIL;
  1122. } else if (ast_sorcery_objectset_apply(sorcery, obj, objset)) {
  1123. ast_test_status_update(test, "Failed to apply valid object set to object\n");
  1124. res = AST_TEST_FAIL;
  1125. } else if (!apply_handler_called) {
  1126. ast_test_status_update(test, "Apply handler was not called when it should have been\n");
  1127. res = AST_TEST_FAIL;
  1128. }
  1129. return res;
  1130. }
  1131. AST_TEST_DEFINE(objectset_apply_invalid)
  1132. {
  1133. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  1134. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  1135. RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
  1136. switch (cmd) {
  1137. case TEST_INIT:
  1138. info->name = "objectset_apply_invalid";
  1139. info->category = "/main/sorcery/";
  1140. info->summary = "sorcery object invalid apply unit test";
  1141. info->description =
  1142. "Test object set applying of an invalid set in sorcery";
  1143. return AST_TEST_NOT_RUN;
  1144. case TEST_EXECUTE:
  1145. break;
  1146. }
  1147. if (!(sorcery = alloc_and_initialize_sorcery())) {
  1148. ast_test_status_update(test, "Failed to open sorcery structure\n");
  1149. return AST_TEST_FAIL;
  1150. }
  1151. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  1152. ast_test_status_update(test, "Failed to allocate a known object type\n");
  1153. return AST_TEST_FAIL;
  1154. }
  1155. if (!(objset = ast_variable_new("fred", "99", ""))) {
  1156. ast_test_status_update(test, "Failed to create an object set, test could not occur\n");
  1157. return AST_TEST_FAIL;
  1158. } else if (!ast_sorcery_objectset_apply(sorcery, obj, objset)) {
  1159. ast_test_status_update(test, "Successfully applied an invalid object set\n");
  1160. return AST_TEST_FAIL;
  1161. } else if ((obj->bob != 5) || (obj->joe != 10)) {
  1162. ast_test_status_update(test, "Object set modified object fields when it should not have\n");
  1163. return AST_TEST_FAIL;
  1164. }
  1165. return AST_TEST_PASS;
  1166. }
  1167. AST_TEST_DEFINE(objectset_transform)
  1168. {
  1169. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  1170. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  1171. RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
  1172. switch (cmd) {
  1173. case TEST_INIT:
  1174. info->name = "objectset_transform";
  1175. info->category = "/main/sorcery/";
  1176. info->summary = "sorcery object set transformation unit test";
  1177. info->description =
  1178. "Test object set transformation in sorcery";
  1179. return AST_TEST_NOT_RUN;
  1180. case TEST_EXECUTE:
  1181. break;
  1182. }
  1183. if (!(sorcery = ast_sorcery_open())) {
  1184. ast_test_status_update(test, "Failed to open sorcery structure\n");
  1185. return AST_TEST_FAIL;
  1186. }
  1187. if (ast_sorcery_apply_default(sorcery, "test", "memory", NULL) != AST_SORCERY_APPLY_SUCCESS) {
  1188. ast_test_status_update(test, "Failed to set a known wizard as a default\n");
  1189. return AST_TEST_FAIL;
  1190. }
  1191. if (ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, test_sorcery_transform, NULL)) {
  1192. ast_test_status_update(test, "Failed to register object type\n");
  1193. return AST_TEST_FAIL;
  1194. }
  1195. ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
  1196. ast_sorcery_object_field_register_nodoc(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
  1197. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  1198. ast_test_status_update(test, "Failed to allocate a known object type\n");
  1199. return AST_TEST_FAIL;
  1200. }
  1201. if (!(objset = ast_sorcery_objectset_create(sorcery, obj))) {
  1202. ast_test_status_update(test, "Failed to create an object set for a known sane object\n");
  1203. return AST_TEST_FAIL;
  1204. }
  1205. if (ast_sorcery_objectset_apply(sorcery, obj, objset)) {
  1206. ast_test_status_update(test, "Failed to apply properly created object set against object\n");
  1207. return AST_TEST_FAIL;
  1208. }
  1209. if (obj->bob != 5) {
  1210. ast_test_status_update(test, "Application of object set produced incorrect value on 'bob'\n");
  1211. return AST_TEST_FAIL;
  1212. } else if (obj->joe == 10) {
  1213. ast_test_status_update(test, "Transformation callback did not change value of 'joe' from provided value\n");
  1214. return AST_TEST_FAIL;
  1215. } else if (obj->joe != 5000) {
  1216. ast_test_status_update(test, "Value of 'joe' differs from default AND from transformation value\n");
  1217. return AST_TEST_FAIL;
  1218. }
  1219. return AST_TEST_PASS;
  1220. }
  1221. AST_TEST_DEFINE(objectset_apply_fields)
  1222. {
  1223. int res = AST_TEST_PASS;
  1224. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  1225. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  1226. RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
  1227. switch (cmd) {
  1228. case TEST_INIT:
  1229. info->name = "objectset_apply_fields";
  1230. info->category = "/main/sorcery/";
  1231. info->summary = "sorcery object apply regex fields unit test";
  1232. info->description =
  1233. "Test object set apply with regex fields in sorcery";
  1234. return AST_TEST_NOT_RUN;
  1235. case TEST_EXECUTE:
  1236. break;
  1237. }
  1238. if (!(sorcery = ast_sorcery_open())) {
  1239. ast_test_status_update(test, "Failed to open sorcery structure\n");
  1240. return AST_TEST_FAIL;
  1241. }
  1242. if ((ast_sorcery_apply_default(sorcery, "test", "memory", NULL) != AST_SORCERY_APPLY_SUCCESS) ||
  1243. ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, test_apply_handler)) {
  1244. ast_test_status_update(test, "Failed to register 'test' object type\n");
  1245. return AST_TEST_FAIL;
  1246. }
  1247. ast_sorcery_object_fields_register(sorcery, "test", "^toast-", test_sorcery_regex_handler, test_sorcery_regex_fields);
  1248. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  1249. ast_test_status_update(test, "Failed to allocate a known object type\n");
  1250. return AST_TEST_FAIL;
  1251. }
  1252. if (!(objset = ast_variable_new("toast-bob", "20", ""))) {
  1253. ast_test_status_update(test, "Failed to create an object set, test could not occur\n");
  1254. res = AST_TEST_FAIL;
  1255. } else if (ast_sorcery_objectset_apply(sorcery, obj, objset)) {
  1256. ast_test_status_update(test, "Failed to apply valid object set to object\n");
  1257. res = AST_TEST_FAIL;
  1258. } else if (obj->bob != 256) {
  1259. ast_test_status_update(test, "Regex field handler was not called when it should have been\n");
  1260. res = AST_TEST_FAIL;
  1261. }
  1262. return res;
  1263. }
  1264. AST_TEST_DEFINE(extended_fields)
  1265. {
  1266. int res = AST_TEST_PASS;
  1267. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  1268. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  1269. RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
  1270. const char *value;
  1271. switch (cmd) {
  1272. case TEST_INIT:
  1273. info->name = "extended_fields";
  1274. info->category = "/main/sorcery/";
  1275. info->summary = "sorcery object extended fields unit test";
  1276. info->description =
  1277. "Test extended fields support in sorcery";
  1278. return AST_TEST_NOT_RUN;
  1279. case TEST_EXECUTE:
  1280. break;
  1281. }
  1282. if (!(sorcery = alloc_and_initialize_sorcery())) {
  1283. ast_test_status_update(test, "Failed to open sorcery structure\n");
  1284. return AST_TEST_FAIL;
  1285. }
  1286. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  1287. ast_test_status_update(test, "Failed to allocate a known object type\n");
  1288. return AST_TEST_FAIL;
  1289. }
  1290. if (!(objset = ast_variable_new("@testing", "toast", ""))) {
  1291. ast_test_status_update(test, "Failed to create an object set, test could not occur\n");
  1292. res = AST_TEST_FAIL;
  1293. } else if (ast_sorcery_objectset_apply(sorcery, obj, objset)) {
  1294. ast_test_status_update(test, "Failed to apply valid object set to object\n");
  1295. res = AST_TEST_FAIL;
  1296. } else if (!(value = ast_sorcery_object_get_extended(obj, "testing"))) {
  1297. ast_test_status_update(test, "Extended field, which was set using object set, could not be found\n");
  1298. res = AST_TEST_FAIL;
  1299. } else if (strcmp(value, "toast")) {
  1300. ast_test_status_update(test, "Extended field does not contain expected value\n");
  1301. res = AST_TEST_FAIL;
  1302. } else if (ast_sorcery_object_set_extended(obj, "@tacos", "supreme")) {
  1303. ast_test_status_update(test, "Extended field could not be set\n");
  1304. res = AST_TEST_FAIL;
  1305. } else if (!(value = ast_sorcery_object_get_extended(obj, "tacos"))) {
  1306. ast_test_status_update(test, "Extended field, which was set using the API, could not be found\n");
  1307. res = AST_TEST_FAIL;
  1308. } else if (strcmp(value, "supreme")) {
  1309. ast_test_status_update(test, "Extended field does not contain expected value\n");
  1310. res = AST_TEST_FAIL;
  1311. } else if (ast_sorcery_object_set_extended(obj, "@tacos", "canadian")) {
  1312. ast_test_status_update(test, "Extended field could not be set a second time\n");
  1313. res = AST_TEST_FAIL;
  1314. } else if (!(value = ast_sorcery_object_get_extended(obj, "tacos"))) {
  1315. ast_test_status_update(test, "Extended field, which was set using the API, could not be found\n");
  1316. res = AST_TEST_FAIL;
  1317. } else if (strcmp(value, "canadian")) {
  1318. ast_test_status_update(test, "Extended field does not contain expected value\n");
  1319. res = AST_TEST_FAIL;
  1320. }
  1321. return res;
  1322. }
  1323. AST_TEST_DEFINE(changeset_create)
  1324. {
  1325. int res = AST_TEST_PASS;
  1326. RAII_VAR(struct ast_variable *, original, NULL, ast_variables_destroy);
  1327. RAII_VAR(struct ast_variable *, modified, NULL, ast_variables_destroy);
  1328. RAII_VAR(struct ast_variable *, changes, NULL, ast_variables_destroy);
  1329. struct ast_variable *tmp;
  1330. switch (cmd) {
  1331. case TEST_INIT:
  1332. info->name = "changeset_create";
  1333. info->category = "/main/sorcery/";
  1334. info->summary = "sorcery changeset creation unit test";
  1335. info->description =
  1336. "Test changeset creation in sorcery";
  1337. return AST_TEST_NOT_RUN;
  1338. case TEST_EXECUTE:
  1339. break;
  1340. }
  1341. if (!(tmp = ast_variable_new("bananas", "purple", ""))) {
  1342. ast_test_status_update(test, "Failed to create first field for original objectset\n");
  1343. return AST_TEST_FAIL;
  1344. }
  1345. tmp->next = original;
  1346. original = tmp;
  1347. if (!(tmp = ast_variable_new("apples", "orange", ""))) {
  1348. ast_test_status_update(test, "Failed to create second field for original objectset\n");
  1349. return AST_TEST_FAIL;
  1350. }
  1351. tmp->next = original;
  1352. original = tmp;
  1353. if (!(tmp = ast_variable_new("bananas", "green", ""))) {
  1354. ast_test_status_update(test, "Failed to create first field for modified objectset\n");
  1355. return AST_TEST_FAIL;
  1356. }
  1357. tmp->next = modified;
  1358. modified = tmp;
  1359. if (!(tmp = ast_variable_new("apples", "orange", ""))) {
  1360. ast_test_status_update(test, "Failed to create second field for modified objectset\n");
  1361. return AST_TEST_FAIL;
  1362. }
  1363. tmp->next = modified;
  1364. modified = tmp;
  1365. if (ast_sorcery_changeset_create(original, modified, &changes)) {
  1366. ast_test_status_update(test, "Failed to create a changeset due to an error\n");
  1367. return AST_TEST_FAIL;
  1368. } else if (!changes) {
  1369. ast_test_status_update(test, "Failed to produce a changeset when there should be one\n");
  1370. return AST_TEST_FAIL;
  1371. }
  1372. for (tmp = changes; tmp; tmp = tmp->next) {
  1373. if (!strcmp(tmp->name, "bananas")) {
  1374. if (strcmp(tmp->value, "green")) {
  1375. ast_test_status_update(test, "Changeset produced had unexpected value '%s' for bananas\n", tmp->value);
  1376. res = AST_TEST_FAIL;
  1377. }
  1378. } else {
  1379. ast_test_status_update(test, "Changeset produced had unexpected field '%s'\n", tmp->name);
  1380. res = AST_TEST_FAIL;
  1381. }
  1382. }
  1383. return res;
  1384. }
  1385. AST_TEST_DEFINE(changeset_create_unchanged)
  1386. {
  1387. RAII_VAR(struct ast_variable *, original, NULL, ast_variables_destroy);
  1388. RAII_VAR(struct ast_variable *, changes, NULL, ast_variables_destroy);
  1389. RAII_VAR(struct ast_variable *, same, NULL, ast_variables_destroy);
  1390. struct ast_variable *tmp;
  1391. switch (cmd) {
  1392. case TEST_INIT:
  1393. info->name = "changeset_create_unchanged";
  1394. info->category = "/main/sorcery/";
  1395. info->summary = "sorcery changeset creation unit test when no changes exist";
  1396. info->description =
  1397. "Test changeset creation in sorcery when no changes actually exist";
  1398. return AST_TEST_NOT_RUN;
  1399. case TEST_EXECUTE:
  1400. break;
  1401. }
  1402. if (!(tmp = ast_variable_new("bananas", "purple", ""))) {
  1403. ast_test_status_update(test, "Failed to create first field for original objectset\n");
  1404. return AST_TEST_FAIL;
  1405. }
  1406. tmp->next = original;
  1407. original = tmp;
  1408. if (!(tmp = ast_variable_new("apples", "orange", ""))) {
  1409. ast_test_status_update(test, "Failed to create second field for original objectset\n");
  1410. return AST_TEST_FAIL;
  1411. }
  1412. tmp->next = original;
  1413. original = tmp;
  1414. if (ast_sorcery_changeset_create(original, original, &changes)) {
  1415. ast_test_status_update(test, "Failed to create a changeset due to an error\n");
  1416. return AST_TEST_FAIL;
  1417. } else if (changes) {
  1418. ast_test_status_update(test, "Created a changeset when no changes actually exist\n");
  1419. return AST_TEST_FAIL;
  1420. }
  1421. if (!(tmp = ast_variable_new("bananas", "purple", ""))) {
  1422. ast_test_status_update(test, "Failed to create first field for same objectset\n");
  1423. return AST_TEST_FAIL;
  1424. }
  1425. tmp->next = same;
  1426. same = tmp;
  1427. if (!(tmp = ast_variable_new("apples", "orange", ""))) {
  1428. ast_test_status_update(test, "Failed to create second field for same objectset\n");
  1429. return AST_TEST_FAIL;
  1430. }
  1431. tmp->next = same;
  1432. same = tmp;
  1433. if (ast_sorcery_changeset_create(original, same, &changes)) {
  1434. ast_test_status_update(test, "Failed to create a changeset due to an error\n");
  1435. return AST_TEST_FAIL;
  1436. } else if (changes) {
  1437. ast_test_status_update(test, "Created a changeset between two different objectsets when no changes actually exist\n");
  1438. return AST_TEST_FAIL;
  1439. }
  1440. return AST_TEST_PASS;
  1441. }
  1442. AST_TEST_DEFINE(object_create)
  1443. {
  1444. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  1445. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  1446. switch (cmd) {
  1447. case TEST_INIT:
  1448. info->name = "object_create";
  1449. info->category = "/main/sorcery/";
  1450. info->summary = "sorcery object creation unit test";
  1451. info->description =
  1452. "Test object creation in sorcery";
  1453. return AST_TEST_NOT_RUN;
  1454. case TEST_EXECUTE:
  1455. break;
  1456. }
  1457. if (!(sorcery = alloc_and_initialize_sorcery())) {
  1458. ast_test_status_update(test, "Failed to open sorcery structure\n");
  1459. return AST_TEST_FAIL;
  1460. }
  1461. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  1462. ast_test_status_update(test, "Failed to allocate a known object type\n");
  1463. return AST_TEST_FAIL;
  1464. }
  1465. if (ast_sorcery_create(sorcery, obj)) {
  1466. ast_test_status_update(test, "Failed to create object using in-memory wizard\n");
  1467. return AST_TEST_FAIL;
  1468. }
  1469. return AST_TEST_PASS;
  1470. }
  1471. AST_TEST_DEFINE(object_retrieve_id)
  1472. {
  1473. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  1474. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  1475. switch (cmd) {
  1476. case TEST_INIT:
  1477. info->name = "object_retrieve_id";
  1478. info->category = "/main/sorcery/";
  1479. info->summary = "sorcery object retrieval using id unit test";
  1480. info->description =
  1481. "Test object retrieval using id in sorcery";
  1482. return AST_TEST_NOT_RUN;
  1483. case TEST_EXECUTE:
  1484. break;
  1485. }
  1486. if (!(sorcery = alloc_and_initialize_sorcery())) {
  1487. ast_test_status_update(test, "Failed to open sorcery structure\n");
  1488. return AST_TEST_FAIL;
  1489. }
  1490. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  1491. ast_test_status_update(test, "Failed to allocate a known object type\n");
  1492. return AST_TEST_FAIL;
  1493. }
  1494. if (ast_sorcery_create(sorcery, obj)) {
  1495. ast_test_status_update(test, "Failed to create object using in-memory wizard\n");
  1496. return AST_TEST_FAIL;
  1497. }
  1498. ao2_cleanup(obj);
  1499. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah2"))) {
  1500. ast_test_status_update(test, "Failed to allocate second instance of a known object type\n");
  1501. return AST_TEST_FAIL;
  1502. }
  1503. if (ast_sorcery_create(sorcery, obj)) {
  1504. ast_test_status_update(test, "Failed to create second object using in-memory wizard\n");
  1505. return AST_TEST_FAIL;
  1506. }
  1507. ao2_cleanup(obj);
  1508. if (!(obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
  1509. ast_test_status_update(test, "Failed to retrieve properly created object using id of 'blah'\n");
  1510. return AST_TEST_FAIL;
  1511. } else if (strcmp(ast_sorcery_object_get_id(obj), "blah")) {
  1512. ast_test_status_update(test, "Retrieved object does not have correct id\n");
  1513. return AST_TEST_FAIL;
  1514. }
  1515. return AST_TEST_PASS;
  1516. }
  1517. AST_TEST_DEFINE(object_retrieve_field)
  1518. {
  1519. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  1520. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  1521. RAII_VAR(struct ast_variable *, fields, ast_variable_new("joe", "42", ""), ast_variables_destroy);
  1522. switch (cmd) {
  1523. case TEST_INIT:
  1524. info->name = "object_retrieve_field";
  1525. info->category = "/main/sorcery/";
  1526. info->summary = "sorcery object retrieval using a specific field unit test";
  1527. info->description =
  1528. "Test object retrieval using a specific field in sorcery";
  1529. return AST_TEST_NOT_RUN;
  1530. case TEST_EXECUTE:
  1531. break;
  1532. }
  1533. if (!fields) {
  1534. ast_test_status_update(test, "Failed to create fields for object retrieval attempt\n");
  1535. return AST_TEST_FAIL;
  1536. }
  1537. if (!(sorcery = alloc_and_initialize_sorcery())) {
  1538. ast_test_status_update(test, "Failed to open sorcery structure\n");
  1539. return AST_TEST_FAIL;
  1540. }
  1541. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  1542. ast_test_status_update(test, "Failed to allocate a known object type\n");
  1543. return AST_TEST_FAIL;
  1544. }
  1545. obj->joe = 42;
  1546. if (ast_sorcery_create(sorcery, obj)) {
  1547. ast_test_status_update(test, "Failed to create object using in-memory wizard\n");
  1548. return AST_TEST_FAIL;
  1549. }
  1550. ao2_cleanup(obj);
  1551. if (!(obj = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_DEFAULT, fields))) {
  1552. ast_test_status_update(test, "Failed to retrieve properly created object using 'joe' field\n");
  1553. return AST_TEST_FAIL;
  1554. }
  1555. ao2_cleanup(obj);
  1556. ast_variables_destroy(fields);
  1557. if (!(fields = ast_variable_new("joe", "49", ""))) {
  1558. ast_test_status_update(test, "Failed to create fields for object retrieval attempt\n");
  1559. return AST_TEST_FAIL;
  1560. }
  1561. if ((obj = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_DEFAULT, fields))) {
  1562. ast_test_status_update(test, "Retrieved an object using a field with an in-correct value... that should not happen\n");
  1563. return AST_TEST_FAIL;
  1564. }
  1565. return AST_TEST_PASS;
  1566. }
  1567. AST_TEST_DEFINE(object_retrieve_multiple_all)
  1568. {
  1569. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  1570. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  1571. RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
  1572. switch (cmd) {
  1573. case TEST_INIT:
  1574. info->name = "object_retrieve_multiple_all";
  1575. info->category = "/main/sorcery/";
  1576. info->summary = "sorcery multiple object retrieval unit test";
  1577. info->description =
  1578. "Test multiple object retrieval in sorcery";
  1579. return AST_TEST_NOT_RUN;
  1580. case TEST_EXECUTE:
  1581. break;
  1582. }
  1583. if (!(sorcery = alloc_and_initialize_sorcery())) {
  1584. ast_test_status_update(test, "Failed to open sorcery structure\n");
  1585. return AST_TEST_FAIL;
  1586. }
  1587. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  1588. ast_test_status_update(test, "Failed to allocate a known object type\n");
  1589. return AST_TEST_FAIL;
  1590. }
  1591. if (ast_sorcery_create(sorcery, obj)) {
  1592. ast_test_status_update(test, "Failed to create object using in-memory wizard\n");
  1593. return AST_TEST_FAIL;
  1594. }
  1595. ao2_cleanup(obj);
  1596. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah2"))) {
  1597. ast_test_status_update(test, "Failed to allocate second instance of a known object type\n");
  1598. return AST_TEST_FAIL;
  1599. }
  1600. if (ast_sorcery_create(sorcery, obj)) {
  1601. ast_test_status_update(test, "Failed to create second object using in-memory wizard\n");
  1602. return AST_TEST_FAIL;
  1603. }
  1604. if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL))) {
  1605. ast_test_status_update(test, "Failed to retrieve a container of all objects\n");
  1606. return AST_TEST_FAIL;
  1607. } else if (ao2_container_count(objects) != 2) {
  1608. ast_test_status_update(test, "Received a container with no objects in it when there should be some\n");
  1609. return AST_TEST_FAIL;
  1610. }
  1611. return AST_TEST_PASS;
  1612. }
  1613. AST_TEST_DEFINE(object_retrieve_multiple_field)
  1614. {
  1615. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  1616. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  1617. RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
  1618. RAII_VAR(struct ast_variable *, fields, ast_variable_new("joe", "6", ""), ast_variables_destroy);
  1619. switch (cmd) {
  1620. case TEST_INIT:
  1621. info->name = "object_retrieve_multiple_field";
  1622. info->category = "/main/sorcery/";
  1623. info->summary = "sorcery multiple object retrieval unit test";
  1624. info->description =
  1625. "Test multiple object retrieval in sorcery using fields";
  1626. return AST_TEST_NOT_RUN;
  1627. case TEST_EXECUTE:
  1628. break;
  1629. }
  1630. if (!fields) {
  1631. ast_test_status_update(test, "Failed to create fields for multiple retrieve\n");
  1632. return AST_TEST_FAIL;
  1633. }
  1634. if (!(sorcery = alloc_and_initialize_sorcery())) {
  1635. ast_test_status_update(test, "Failed to open sorcery structure\n");
  1636. return AST_TEST_FAIL;
  1637. }
  1638. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  1639. ast_test_status_update(test, "Failed to allocate a known object type\n");
  1640. return AST_TEST_FAIL;
  1641. }
  1642. obj->joe = 6;
  1643. if (ast_sorcery_create(sorcery, obj)) {
  1644. ast_test_status_update(test, "Failed to create object using in-memory wizard\n");
  1645. return AST_TEST_FAIL;
  1646. }
  1647. if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE, fields))) {
  1648. ast_test_status_update(test, "Failed to retrieve a container of all objects\n");
  1649. return AST_TEST_FAIL;
  1650. } else if (ao2_container_count(objects) != 1) {
  1651. ast_test_status_update(test, "Received a container with no objects in it when there should be some\n");
  1652. return AST_TEST_FAIL;
  1653. }
  1654. ao2_cleanup(objects);
  1655. ast_variables_destroy(fields);
  1656. if (!(fields = ast_variable_new("joe", "7", ""))) {
  1657. ast_test_status_update(test, "Failed to create fields for multiple retrieval\n");
  1658. return AST_TEST_FAIL;
  1659. } else if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE, fields))) {
  1660. ast_test_status_update(test, "Failed to retrieve an empty container when retrieving multiple\n");
  1661. return AST_TEST_FAIL;
  1662. } else if (ao2_container_count(objects)) {
  1663. ast_test_status_update(test, "Received a container with objects when there should be none in it\n");
  1664. return AST_TEST_FAIL;
  1665. }
  1666. return AST_TEST_PASS;
  1667. }
  1668. AST_TEST_DEFINE(object_retrieve_regex)
  1669. {
  1670. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  1671. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  1672. RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
  1673. switch (cmd) {
  1674. case TEST_INIT:
  1675. info->name = "object_retrieve_regex";
  1676. info->category = "/main/sorcery/";
  1677. info->summary = "sorcery multiple object retrieval using regex unit test";
  1678. info->description =
  1679. "Test multiple object retrieval in sorcery using regular expression for matching";
  1680. return AST_TEST_NOT_RUN;
  1681. case TEST_EXECUTE:
  1682. break;
  1683. }
  1684. if (!(sorcery = alloc_and_initialize_sorcery())) {
  1685. ast_test_status_update(test, "Failed to open sorcery structure\n");
  1686. return AST_TEST_FAIL;
  1687. }
  1688. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah-98joe"))) {
  1689. ast_test_status_update(test, "Failed to allocate a known object type\n");
  1690. return AST_TEST_FAIL;
  1691. }
  1692. if (ast_sorcery_create(sorcery, obj)) {
  1693. ast_test_status_update(test, "Failed to create object using in-memory wizard\n");
  1694. return AST_TEST_FAIL;
  1695. }
  1696. ao2_cleanup(obj);
  1697. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah-93joe"))) {
  1698. ast_test_status_update(test, "Failed to allocate second instance of a known object type\n");
  1699. return AST_TEST_FAIL;
  1700. }
  1701. if (ast_sorcery_create(sorcery, obj)) {
  1702. ast_test_status_update(test, "Failed to create second object using in-memory wizard\n");
  1703. return AST_TEST_FAIL;
  1704. }
  1705. ao2_cleanup(obj);
  1706. if (!(obj = ast_sorcery_alloc(sorcery, "test", "neener-93joe"))) {
  1707. ast_test_status_update(test, "Failed to allocate third instance of a known object type\n");
  1708. return AST_TEST_FAIL;
  1709. }
  1710. if (ast_sorcery_create(sorcery, obj)) {
  1711. ast_test_status_update(test, "Failed to create third object using in-memory wizard\n");
  1712. return AST_TEST_FAIL;
  1713. }
  1714. if (!(objects = ast_sorcery_retrieve_by_regex(sorcery, "test", "^blah-"))) {
  1715. ast_test_status_update(test, "Failed to retrieve a container of objects\n");
  1716. return AST_TEST_FAIL;
  1717. } else if (ao2_container_count(objects) != 2) {
  1718. ast_test_status_update(test, "Received a container with incorrect number of objects in it\n");
  1719. return AST_TEST_FAIL;
  1720. }
  1721. return AST_TEST_PASS;
  1722. }
  1723. AST_TEST_DEFINE(object_update)
  1724. {
  1725. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  1726. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  1727. RAII_VAR(struct test_sorcery_object *, obj2, NULL, ao2_cleanup);
  1728. switch (cmd) {
  1729. case TEST_INIT:
  1730. info->name = "object_update";
  1731. info->category = "/main/sorcery/";
  1732. info->summary = "sorcery object update unit test";
  1733. info->description =
  1734. "Test object updating in sorcery";
  1735. return AST_TEST_NOT_RUN;
  1736. case TEST_EXECUTE:
  1737. break;
  1738. }
  1739. if (!(sorcery = alloc_and_initialize_sorcery())) {
  1740. ast_test_status_update(test, "Failed to open sorcery structure\n");
  1741. return AST_TEST_FAIL;
  1742. }
  1743. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  1744. ast_test_status_update(test, "Failed to allocate a known object type\n");
  1745. return AST_TEST_FAIL;
  1746. }
  1747. if (ast_sorcery_create(sorcery, obj)) {
  1748. ast_test_status_update(test, "Failed to create object using in-memory wizard\n");
  1749. return AST_TEST_FAIL;
  1750. }
  1751. if (!(obj2 = ast_sorcery_copy(sorcery, obj))) {
  1752. ast_test_status_update(test, "Failed to allocate a known object type for updating\n");
  1753. return AST_TEST_FAIL;
  1754. }
  1755. ao2_cleanup(obj);
  1756. if (ast_sorcery_update(sorcery, obj2)) {
  1757. ast_test_status_update(test, "Failed to update sorcery with new object\n");
  1758. return AST_TEST_FAIL;
  1759. }
  1760. if (!(obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
  1761. ast_test_status_update(test, "Failed to retrieve properly updated object\n");
  1762. return AST_TEST_FAIL;
  1763. } else if (obj != obj2) {
  1764. ast_test_status_update(test, "Object retrieved is not the updated object\n");
  1765. return AST_TEST_FAIL;
  1766. }
  1767. return AST_TEST_PASS;
  1768. }
  1769. AST_TEST_DEFINE(object_update_uncreated)
  1770. {
  1771. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  1772. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  1773. switch (cmd) {
  1774. case TEST_INIT:
  1775. info->name = "object_update_uncreated";
  1776. info->category = "/main/sorcery/";
  1777. info->summary = "sorcery object update unit test";
  1778. info->description =
  1779. "Test updating of an uncreated object in sorcery";
  1780. return AST_TEST_NOT_RUN;
  1781. case TEST_EXECUTE:
  1782. break;
  1783. }
  1784. if (!(sorcery = alloc_and_initialize_sorcery())) {
  1785. ast_test_status_update(test, "Failed to open sorcery structure\n");
  1786. return AST_TEST_FAIL;
  1787. }
  1788. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  1789. ast_test_status_update(test, "Failed to allocate a known object type\n");
  1790. return AST_TEST_FAIL;
  1791. }
  1792. if (!ast_sorcery_update(sorcery, obj)) {
  1793. ast_test_status_update(test, "Successfully updated an object which has not been created yet\n");
  1794. return AST_TEST_FAIL;
  1795. }
  1796. return AST_TEST_PASS;
  1797. }
  1798. AST_TEST_DEFINE(object_delete)
  1799. {
  1800. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  1801. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  1802. switch (cmd) {
  1803. case TEST_INIT:
  1804. info->name = "object_delete";
  1805. info->category = "/main/sorcery/";
  1806. info->summary = "sorcery object deletion unit test";
  1807. info->description =
  1808. "Test object deletion in sorcery";
  1809. return AST_TEST_NOT_RUN;
  1810. case TEST_EXECUTE:
  1811. break;
  1812. }
  1813. if (!(sorcery = alloc_and_initialize_sorcery())) {
  1814. ast_test_status_update(test, "Failed to open sorcery structure\n");
  1815. return AST_TEST_FAIL;
  1816. }
  1817. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  1818. ast_test_status_update(test, "Failed to allocate a known object type\n");
  1819. return AST_TEST_FAIL;
  1820. }
  1821. if (ast_sorcery_create(sorcery, obj)) {
  1822. ast_test_status_update(test, "Failed to create object using in-memory wizard\n");
  1823. return AST_TEST_FAIL;
  1824. }
  1825. if (ast_sorcery_delete(sorcery, obj)) {
  1826. ast_test_status_update(test, "Failed to delete object using in-memory wizard\n");
  1827. return AST_TEST_FAIL;
  1828. }
  1829. ao2_cleanup(obj);
  1830. if ((obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
  1831. ast_test_status_update(test, "Retrieved deleted object that should not be there\n");
  1832. return AST_TEST_FAIL;
  1833. }
  1834. return AST_TEST_PASS;
  1835. }
  1836. AST_TEST_DEFINE(object_delete_uncreated)
  1837. {
  1838. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  1839. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  1840. switch (cmd) {
  1841. case TEST_INIT:
  1842. info->name = "object_delete_uncreated";
  1843. info->category = "/main/sorcery/";
  1844. info->summary = "sorcery object deletion unit test";
  1845. info->description =
  1846. "Test object deletion of an uncreated object in sorcery";
  1847. return AST_TEST_NOT_RUN;
  1848. case TEST_EXECUTE:
  1849. break;
  1850. }
  1851. if (!(sorcery = alloc_and_initialize_sorcery())) {
  1852. ast_test_status_update(test, "Failed to open sorcery structure\n");
  1853. return AST_TEST_FAIL;
  1854. }
  1855. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  1856. ast_test_status_update(test, "Failed to allocate a known object type\n");
  1857. return AST_TEST_FAIL;
  1858. }
  1859. if (!ast_sorcery_delete(sorcery, obj)) {
  1860. ast_test_status_update(test, "Successfully deleted an object which was never created\n");
  1861. return AST_TEST_FAIL;
  1862. }
  1863. return AST_TEST_PASS;
  1864. }
  1865. AST_TEST_DEFINE(caching_wizard_behavior)
  1866. {
  1867. struct ast_flags flags = { CONFIG_FLAG_NOCACHE };
  1868. struct ast_config *config;
  1869. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  1870. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  1871. RAII_VAR(struct test_sorcery_object *, obj2, NULL, ao2_cleanup);
  1872. int res = AST_TEST_FAIL;
  1873. switch (cmd) {
  1874. case TEST_INIT:
  1875. info->name = "caching_wizard_behavior";
  1876. info->category = "/main/sorcery/";
  1877. info->summary = "sorcery caching wizard behavior unit test";
  1878. info->description =
  1879. "Test internal behavior of caching wizards";
  1880. return AST_TEST_NOT_RUN;
  1881. case TEST_EXECUTE:
  1882. break;
  1883. }
  1884. if (!(config = ast_config_load2("sorcery.conf", "test_sorcery_cache", flags))) {
  1885. ast_test_status_update(test, "Sorcery configuration file not present - skipping caching_wizard_behavior test\n");
  1886. return AST_TEST_NOT_RUN;
  1887. }
  1888. if (!ast_category_get(config, "test_sorcery_cache", NULL)) {
  1889. ast_test_status_update(test, "Sorcery configuration file does not contain 'test_sorcery_cache' section\n");
  1890. ast_config_destroy(config);
  1891. return AST_TEST_NOT_RUN;
  1892. }
  1893. ast_config_destroy(config);
  1894. if (ast_sorcery_wizard_register(&test_wizard)) {
  1895. ast_test_status_update(test, "Failed to register a perfectly valid sorcery wizard\n");
  1896. return AST_TEST_FAIL;
  1897. }
  1898. if (!(sorcery = ast_sorcery_open())) {
  1899. ast_test_status_update(test, "Failed to open sorcery structure\n");
  1900. goto end;
  1901. }
  1902. if (ast_sorcery_apply_config(sorcery, "test_sorcery_cache") != AST_SORCERY_APPLY_SUCCESS) {
  1903. ast_test_status_update(test, "Failed to apply configured object mappings\n");
  1904. goto end;
  1905. }
  1906. if (ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
  1907. ast_test_status_update(test, "Failed to register object type\n");
  1908. goto end;
  1909. }
  1910. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  1911. ast_test_status_update(test, "Failed to allocate a known object type\n");
  1912. goto end;
  1913. }
  1914. if (ast_sorcery_create(sorcery, obj)) {
  1915. ast_test_status_update(test, "Failed to create object using in-memory wizard\n");
  1916. goto end;
  1917. }
  1918. ao2_cleanup(obj);
  1919. if (!(obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
  1920. ast_test_status_update(test, "Failed to retrieve just created object\n");
  1921. goto end;
  1922. } else if (!cache.created) {
  1923. ast_test_status_update(test, "Caching wizard was not told to cache just created object\n");
  1924. goto end;
  1925. } else if (!(obj2 = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
  1926. ast_test_status_update(test, "Failed to retrieve just cached object\n");
  1927. goto end;
  1928. } else if (obj == obj2) {
  1929. ast_test_status_update(test, "Returned object is *NOT* a cached object\n");
  1930. goto end;
  1931. } else if (ast_sorcery_update(sorcery, obj)) {
  1932. ast_test_status_update(test, "Failed to update a known stored object\n");
  1933. goto end;
  1934. } else if (!cache.updated) {
  1935. ast_test_status_update(test, "Caching wizard was not told to update object\n");
  1936. goto end;
  1937. } else if (ast_sorcery_delete(sorcery, obj)) {
  1938. ast_test_status_update(test, "Failed to delete a known stored object\n");
  1939. goto end;
  1940. } else if (!cache.deleted) {
  1941. ast_test_status_update(test, "Caching wizard was not told to delete object\n");
  1942. goto end;
  1943. }
  1944. ao2_cleanup(obj2);
  1945. if ((obj2 = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
  1946. ast_test_status_update(test, "Retrieved an object that should have been deleted\n");
  1947. goto end;
  1948. }
  1949. res = AST_TEST_PASS;
  1950. end:
  1951. ast_sorcery_unref(sorcery);
  1952. sorcery = NULL;
  1953. if (ast_sorcery_wizard_unregister(&test_wizard)) {
  1954. ast_test_status_update(test, "Failed to unregister test sorcery wizard\n");
  1955. return AST_TEST_FAIL;
  1956. }
  1957. return res;
  1958. }
  1959. AST_TEST_DEFINE(object_type_observer)
  1960. {
  1961. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  1962. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  1963. int res = AST_TEST_FAIL;
  1964. switch (cmd) {
  1965. case TEST_INIT:
  1966. info->name = "object_type_observer";
  1967. info->category = "/main/sorcery/";
  1968. info->summary = "sorcery object type observer unit test";
  1969. info->description =
  1970. "Test that object type observers get called when they should";
  1971. return AST_TEST_NOT_RUN;
  1972. case TEST_EXECUTE:
  1973. break;
  1974. }
  1975. if (!(sorcery = alloc_and_initialize_sorcery())) {
  1976. ast_test_status_update(test, "Failed to open sorcery structure\n");
  1977. return AST_TEST_FAIL;
  1978. }
  1979. if (!ast_sorcery_observer_add(sorcery, "test", NULL)) {
  1980. ast_test_status_update(test, "Successfully added a NULL observer when it should not be possible\n");
  1981. return AST_TEST_FAIL;
  1982. }
  1983. if (ast_sorcery_observer_add(sorcery, "test", &test_observer)) {
  1984. ast_test_status_update(test, "Failed to add a proper observer\n");
  1985. return AST_TEST_FAIL;
  1986. }
  1987. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  1988. ast_test_status_update(test, "Failed to allocate a known object type\n");
  1989. goto end;
  1990. }
  1991. ast_mutex_init(&observer.lock);
  1992. ast_cond_init(&observer.cond, NULL);
  1993. observer.created = NULL;
  1994. observer.updated = NULL;
  1995. observer.deleted = NULL;
  1996. if (ast_sorcery_create(sorcery, obj)) {
  1997. ast_test_status_update(test, "Failed to create object using in-memory wizard\n");
  1998. goto end;
  1999. }
  2000. ast_mutex_lock(&observer.lock);
  2001. while (!observer.created) {
  2002. struct timeval start = ast_tvnow();
  2003. struct timespec end = {
  2004. .tv_sec = start.tv_sec + 10,
  2005. .tv_nsec = start.tv_usec * 1000,
  2006. };
  2007. if (ast_cond_timedwait(&observer.cond, &observer.lock, &end) == ETIMEDOUT) {
  2008. break;
  2009. }
  2010. }
  2011. ast_mutex_unlock(&observer.lock);
  2012. if (!observer.created) {
  2013. ast_test_status_update(test, "Failed to receive observer notification for object creation within suitable timeframe\n");
  2014. goto end;
  2015. }
  2016. if (ast_sorcery_update(sorcery, obj)) {
  2017. ast_test_status_update(test, "Failed to update object using in-memory wizard\n");
  2018. goto end;
  2019. }
  2020. ast_mutex_lock(&observer.lock);
  2021. while (!observer.updated) {
  2022. struct timeval start = ast_tvnow();
  2023. struct timespec end = {
  2024. .tv_sec = start.tv_sec + 10,
  2025. .tv_nsec = start.tv_usec * 1000,
  2026. };
  2027. if (ast_cond_timedwait(&observer.cond, &observer.lock, &end) == ETIMEDOUT) {
  2028. break;
  2029. }
  2030. }
  2031. ast_mutex_unlock(&observer.lock);
  2032. if (!observer.updated) {
  2033. ast_test_status_update(test, "Failed to receive observer notification for object updating within suitable timeframe\n");
  2034. goto end;
  2035. }
  2036. if (ast_sorcery_delete(sorcery, obj)) {
  2037. ast_test_status_update(test, "Failed to delete object using in-memory wizard\n");
  2038. goto end;
  2039. }
  2040. ast_mutex_lock(&observer.lock);
  2041. while (!observer.deleted) {
  2042. struct timeval start = ast_tvnow();
  2043. struct timespec end = {
  2044. .tv_sec = start.tv_sec + 10,
  2045. .tv_nsec = start.tv_usec * 1000,
  2046. };
  2047. if (ast_cond_timedwait(&observer.cond, &observer.lock, &end) == ETIMEDOUT) {
  2048. break;
  2049. }
  2050. }
  2051. ast_mutex_unlock(&observer.lock);
  2052. if (!observer.deleted) {
  2053. ast_test_status_update(test, "Failed to receive observer notification for object deletion within suitable timeframe\n");
  2054. goto end;
  2055. }
  2056. ast_sorcery_reload(sorcery);
  2057. ast_mutex_lock(&observer.lock);
  2058. while (!observer.loaded) {
  2059. struct timeval start = ast_tvnow();
  2060. struct timespec end = {
  2061. .tv_sec = start.tv_sec + 10,
  2062. .tv_nsec = start.tv_usec * 1000,
  2063. };
  2064. if (ast_cond_timedwait(&observer.cond, &observer.lock, &end) == ETIMEDOUT) {
  2065. break;
  2066. }
  2067. }
  2068. ast_mutex_unlock(&observer.lock);
  2069. if (!observer.loaded) {
  2070. ast_test_status_update(test, "Failed to receive observer notification for object type load within suitable timeframe\n");
  2071. goto end;
  2072. }
  2073. res = AST_TEST_PASS;
  2074. end:
  2075. observer.created = NULL;
  2076. observer.updated = NULL;
  2077. observer.deleted = NULL;
  2078. ast_mutex_destroy(&observer.lock);
  2079. ast_cond_destroy(&observer.cond);
  2080. return res;
  2081. }
  2082. AST_TEST_DEFINE(configuration_file_wizard)
  2083. {
  2084. struct ast_flags flags = { CONFIG_FLAG_NOCACHE };
  2085. struct ast_config *config;
  2086. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  2087. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  2088. switch (cmd) {
  2089. case TEST_INIT:
  2090. info->name = "configuration_file_wizard";
  2091. info->category = "/main/sorcery/";
  2092. info->summary = "sorcery configuration file wizard unit test";
  2093. info->description =
  2094. "Test the configuration file wizard in sorcery";
  2095. return AST_TEST_NOT_RUN;
  2096. case TEST_EXECUTE:
  2097. break;
  2098. }
  2099. if (!(config = ast_config_load2("test_sorcery.conf", "test_sorcery", flags))) {
  2100. ast_test_status_update(test, "Test sorcery configuration file wizard file not present - skipping configuration_file_wizard test\n");
  2101. return AST_TEST_NOT_RUN;
  2102. }
  2103. ast_config_destroy(config);
  2104. if (!(sorcery = ast_sorcery_open())) {
  2105. ast_test_status_update(test, "Failed to open sorcery structure\n");
  2106. return AST_TEST_FAIL;
  2107. }
  2108. if (ast_sorcery_apply_default(sorcery, "test", "config", "test_sorcery.conf") != AST_SORCERY_APPLY_SUCCESS) {
  2109. ast_test_status_update(test, "Could not set a default wizard of the 'config' type, so skipping since it may not be loaded\n");
  2110. return AST_TEST_NOT_RUN;
  2111. }
  2112. if (ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
  2113. ast_test_status_update(test, "Failed to register object type\n");
  2114. return AST_TEST_FAIL;
  2115. }
  2116. ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
  2117. ast_sorcery_object_field_register_nodoc(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
  2118. ast_sorcery_load(sorcery);
  2119. if ((obj = ast_sorcery_retrieve_by_id(sorcery, "test", "hey2"))) {
  2120. ast_test_status_update(test, "Retrieved object which has an unknown field\n");
  2121. return AST_TEST_FAIL;
  2122. } else if (!(obj = ast_sorcery_retrieve_by_id(sorcery, "test", "hey"))) {
  2123. ast_test_status_update(test, "Failed to retrieve a known object that has been configured in the configuration file\n");
  2124. return AST_TEST_FAIL;
  2125. } else if (obj->bob != 98) {
  2126. ast_test_status_update(test, "Value of 'bob' on object is not what is configured in configuration file\n");
  2127. return AST_TEST_FAIL;
  2128. } else if (obj->joe != 41) {
  2129. ast_test_status_update(test, "Value of 'joe' on object is not what is configured in configuration file\n");
  2130. return AST_TEST_FAIL;
  2131. }
  2132. return AST_TEST_PASS;
  2133. }
  2134. AST_TEST_DEFINE(configuration_file_wizard_with_file_integrity)
  2135. {
  2136. struct ast_flags flags = { CONFIG_FLAG_NOCACHE };
  2137. struct ast_config *config;
  2138. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  2139. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  2140. switch (cmd) {
  2141. case TEST_INIT:
  2142. info->name = "configuration_file_wizard_with_file_integrity";
  2143. info->category = "/main/sorcery/";
  2144. info->summary = "sorcery configuration file wizard file integrity unit test";
  2145. info->description =
  2146. "Test the configuration file wizard with file integrity turned on in sorcery";
  2147. return AST_TEST_NOT_RUN;
  2148. case TEST_EXECUTE:
  2149. break;
  2150. }
  2151. if (!(config = ast_config_load2("test_sorcery.conf", "test_sorcery", flags))) {
  2152. ast_test_status_update(test, "Test sorcery configuration file wizard file not present - skipping configuration_file_wizard_with_file_integrity test\n");
  2153. return AST_TEST_NOT_RUN;
  2154. }
  2155. ast_config_destroy(config);
  2156. if (!(sorcery = ast_sorcery_open())) {
  2157. ast_test_status_update(test, "Failed to open sorcery structure\n");
  2158. return AST_TEST_FAIL;
  2159. }
  2160. if (ast_sorcery_apply_default(sorcery, "test", "config", "test_sorcery.conf,integrity=file") != AST_SORCERY_APPLY_SUCCESS) {
  2161. ast_test_status_update(test, "Could not set a default wizard of the 'config' type, so skipping since it may not be loaded\n");
  2162. return AST_TEST_NOT_RUN;
  2163. }
  2164. if (ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
  2165. ast_test_status_update(test, "Failed to register object type\n");
  2166. return AST_TEST_FAIL;
  2167. }
  2168. ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
  2169. ast_sorcery_object_field_register_nodoc(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
  2170. ast_sorcery_load(sorcery);
  2171. if ((obj = ast_sorcery_retrieve_by_id(sorcery, "test", "hey"))) {
  2172. ast_test_status_update(test, "Retrieved object which has an unknown field\n");
  2173. return AST_TEST_FAIL;
  2174. }
  2175. return AST_TEST_PASS;
  2176. }
  2177. AST_TEST_DEFINE(configuration_file_wizard_with_criteria)
  2178. {
  2179. struct ast_flags flags = { CONFIG_FLAG_NOCACHE };
  2180. struct ast_config *config;
  2181. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  2182. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  2183. switch (cmd) {
  2184. case TEST_INIT:
  2185. info->name = "configuration_file_wizard_with_criteria";
  2186. info->category = "/main/sorcery/";
  2187. info->summary = "sorcery configuration file wizard with criteria unit test";
  2188. info->description =
  2189. "Test the configuration file wizard with criteria matching in sorcery";
  2190. return AST_TEST_NOT_RUN;
  2191. case TEST_EXECUTE:
  2192. break;
  2193. }
  2194. if (!(config = ast_config_load2("test_sorcery.conf", "test_sorcery", flags))) {
  2195. ast_test_status_update(test, "Test sorcery configuration file wizard file not present - skipping configuration_file_wizard_with_criteria test\n");
  2196. return AST_TEST_NOT_RUN;
  2197. }
  2198. ast_config_destroy(config);
  2199. if (!(sorcery = ast_sorcery_open())) {
  2200. ast_test_status_update(test, "Failed to open sorcery structure\n");
  2201. return AST_TEST_FAIL;
  2202. }
  2203. if (ast_sorcery_apply_default(sorcery, "test", "config", "test_sorcery.conf,criteria=type=zombies") != AST_SORCERY_APPLY_SUCCESS) {
  2204. ast_test_status_update(test, "Could not set a default wizard of the 'config' type, so skipping since it may not be loaded\n");
  2205. return AST_TEST_NOT_RUN;
  2206. }
  2207. if (ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
  2208. ast_test_status_update(test, "Failed to register object type\n");
  2209. return AST_TEST_FAIL;
  2210. }
  2211. ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
  2212. ast_sorcery_object_field_register_nodoc(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
  2213. ast_sorcery_object_field_register_nodoc(sorcery, "test", "type", NULL, OPT_NOOP_T, 0, NULL);
  2214. ast_sorcery_load(sorcery);
  2215. if ((obj = ast_sorcery_retrieve_by_id(sorcery, "test", "hey"))) {
  2216. ast_test_status_update(test, "Retrieved object which did not match criteria\n");
  2217. return AST_TEST_FAIL;
  2218. } else if (!(obj = ast_sorcery_retrieve_by_id(sorcery, "test", "hey2"))) {
  2219. ast_test_status_update(test, "Failed to retrieve a known object which matches criteria\n");
  2220. return AST_TEST_FAIL;
  2221. }
  2222. return AST_TEST_PASS;
  2223. }
  2224. AST_TEST_DEFINE(configuration_file_wizard_retrieve_field)
  2225. {
  2226. struct ast_flags flags = { CONFIG_FLAG_NOCACHE };
  2227. struct ast_config *config;
  2228. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  2229. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  2230. RAII_VAR(struct ast_variable *, fields, ast_variable_new("joe", "41", ""), ast_variables_destroy);
  2231. switch (cmd) {
  2232. case TEST_INIT:
  2233. info->name = "configuration_file_wizard_retrieve_field";
  2234. info->category = "/main/sorcery/";
  2235. info->summary = "sorcery configuration file wizard field retrieval unit test";
  2236. info->description =
  2237. "Test the configuration file wizard retrieval using field in sorcery";
  2238. return AST_TEST_NOT_RUN;
  2239. case TEST_EXECUTE:
  2240. break;
  2241. }
  2242. if (!(config = ast_config_load2("test_sorcery.conf", "test_sorcery", flags))) {
  2243. ast_test_status_update(test, "Test sorcery configuration file wizard file not present - skipping configuration_file_wizard_retrieve_field test\n");
  2244. return AST_TEST_NOT_RUN;
  2245. }
  2246. ast_config_destroy(config);
  2247. if (!(sorcery = ast_sorcery_open())) {
  2248. ast_test_status_update(test, "Failed to open sorcery structure\n");
  2249. return AST_TEST_FAIL;
  2250. }
  2251. if (ast_sorcery_apply_default(sorcery, "test", "config", "test_sorcery.conf") != AST_SORCERY_APPLY_SUCCESS) {
  2252. ast_test_status_update(test, "Could not set a default wizard of the 'config' type, so skipping since it may not be loaded\n");
  2253. return AST_TEST_NOT_RUN;
  2254. }
  2255. if (ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
  2256. ast_test_status_update(test, "Failed to register object type\n");
  2257. return AST_TEST_FAIL;
  2258. }
  2259. ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
  2260. ast_sorcery_object_field_register_nodoc(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
  2261. ast_sorcery_load(sorcery);
  2262. if (!(obj = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_DEFAULT, fields))) {
  2263. ast_test_status_update(test, "Failed to retrieve a known object that has been configured with the correct field\n");
  2264. return AST_TEST_FAIL;
  2265. } else if (strcmp(ast_sorcery_object_get_id(obj), "hey")) {
  2266. ast_test_status_update(test, "Retrieved object has incorrect object id of '%s'\n", ast_sorcery_object_get_id(obj));
  2267. return AST_TEST_FAIL;
  2268. }
  2269. return AST_TEST_PASS;
  2270. }
  2271. AST_TEST_DEFINE(configuration_file_wizard_retrieve_multiple)
  2272. {
  2273. struct ast_flags flags = { CONFIG_FLAG_NOCACHE };
  2274. struct ast_config *config;
  2275. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  2276. RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
  2277. RAII_VAR(struct ast_variable *, fields, ast_variable_new("joe", "99", ""), ast_variables_destroy);
  2278. switch (cmd) {
  2279. case TEST_INIT:
  2280. info->name = "configuration_file_wizard_retrieve_multiple";
  2281. info->category = "/main/sorcery/";
  2282. info->summary = "sorcery configuration file wizard multiple retrieval unit test";
  2283. info->description =
  2284. "Test the configuration file wizard multiple retrieval in sorcery";
  2285. return AST_TEST_NOT_RUN;
  2286. case TEST_EXECUTE:
  2287. break;
  2288. }
  2289. if (!(config = ast_config_load2("test_sorcery.conf", "test_sorcery", flags))) {
  2290. ast_test_status_update(test, "Test sorcery configuration file wizard file not present - skipping configuration_file_wizard_retrieve_multiple test\n");
  2291. return AST_TEST_NOT_RUN;
  2292. }
  2293. ast_config_destroy(config);
  2294. if (!fields) {
  2295. ast_test_status_update(test, "Failed to create fields for multiple retrieve\n");
  2296. return AST_TEST_FAIL;
  2297. }
  2298. if (!(sorcery = ast_sorcery_open())) {
  2299. ast_test_status_update(test, "Failed to open sorcery structure\n");
  2300. return AST_TEST_FAIL;
  2301. }
  2302. if (ast_sorcery_apply_default(sorcery, "test", "config", "test_sorcery.conf") != AST_SORCERY_APPLY_SUCCESS) {
  2303. ast_test_status_update(test, "Could not set a default wizard of the 'config' type, so skipping since it may not be loaded\n");
  2304. return AST_TEST_NOT_RUN;
  2305. }
  2306. if (ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
  2307. ast_test_status_update(test, "Failed to register object type\n");
  2308. return AST_TEST_FAIL;
  2309. }
  2310. ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
  2311. ast_sorcery_object_field_register_nodoc(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
  2312. ast_sorcery_load(sorcery);
  2313. if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE, fields))) {
  2314. ast_test_status_update(test, "Failed to retrieve an empty container when retrieving multiple\n");
  2315. return AST_TEST_FAIL;
  2316. } else if (ao2_container_count(objects)) {
  2317. ast_test_status_update(test, "Received a container with objects when there should be none in it\n");
  2318. return AST_TEST_FAIL;
  2319. }
  2320. ao2_cleanup(objects);
  2321. ast_variables_destroy(fields);
  2322. if (!(fields = ast_variable_new("joe", "41", ""))) {
  2323. ast_test_status_update(test, "Failed to create fields for multiple retrieve\n");
  2324. return AST_TEST_FAIL;
  2325. } else if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE, fields))) {
  2326. ast_test_status_update(test, "Failed to retrieve a container when retrieving multiple\n");
  2327. return AST_TEST_FAIL;
  2328. } else if (ao2_container_count(objects) != 1) {
  2329. ast_test_status_update(test, "Received a container with no objects in it when there should be\n");
  2330. return AST_TEST_FAIL;
  2331. }
  2332. return AST_TEST_PASS;
  2333. }
  2334. AST_TEST_DEFINE(configuration_file_wizard_retrieve_multiple_all)
  2335. {
  2336. struct ast_flags flags = { CONFIG_FLAG_NOCACHE };
  2337. struct ast_config *config;
  2338. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  2339. RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
  2340. switch (cmd) {
  2341. case TEST_INIT:
  2342. info->name = "configuration_file_wizard_retrieve_multiple_all";
  2343. info->category = "/main/sorcery/";
  2344. info->summary = "sorcery configuration file wizard multiple retrieve all unit test";
  2345. info->description =
  2346. "Test the configuration file wizard multiple retrieve all in sorcery";
  2347. return AST_TEST_NOT_RUN;
  2348. case TEST_EXECUTE:
  2349. break;
  2350. }
  2351. if (!(config = ast_config_load2("test_sorcery.conf", "test_sorcery", flags))) {
  2352. ast_test_status_update(test, "Test sorcery configuration file wizard file not present - skipping configuration_file_wizard_retrieve_multiple_all test\n");
  2353. return AST_TEST_NOT_RUN;
  2354. }
  2355. ast_config_destroy(config);
  2356. if (!(sorcery = ast_sorcery_open())) {
  2357. ast_test_status_update(test, "Failed to open sorcery structure\n");
  2358. return AST_TEST_FAIL;
  2359. }
  2360. if (ast_sorcery_apply_default(sorcery, "test", "config", "test_sorcery.conf") != AST_SORCERY_APPLY_SUCCESS) {
  2361. ast_test_status_update(test, "Could not set a default wizard of the 'config' type, so skipping since it may not be loaded\n");
  2362. return AST_TEST_NOT_RUN;
  2363. }
  2364. if (ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
  2365. ast_test_status_update(test, "Failed to register object type\n");
  2366. return AST_TEST_FAIL;
  2367. }
  2368. ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
  2369. ast_sorcery_object_field_register_nodoc(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
  2370. ast_sorcery_load(sorcery);
  2371. if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL))) {
  2372. ast_test_status_update(test, "Failed to retrieve a container with all objects when there should be one\n");
  2373. return AST_TEST_FAIL;
  2374. } else if (ao2_container_count(objects) != 2) {
  2375. ast_test_status_update(test, "Returned container does not have the correct number of objects in it\n");
  2376. return AST_TEST_FAIL;
  2377. }
  2378. return AST_TEST_PASS;
  2379. }
  2380. AST_TEST_DEFINE(dialplan_function)
  2381. {
  2382. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  2383. RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
  2384. RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
  2385. struct ast_str *buf;
  2386. char expression[256];
  2387. switch (cmd) {
  2388. case TEST_INIT:
  2389. info->name = "dialplan_function";
  2390. info->category = "/main/sorcery/";
  2391. info->summary = "AST_SORCERY dialplan function";
  2392. info->description =
  2393. "Test the AST_SORCERY dialplan function";
  2394. return AST_TEST_NOT_RUN;
  2395. case TEST_EXECUTE:
  2396. break;
  2397. }
  2398. if (!(sorcery = alloc_and_initialize_sorcery())) {
  2399. ast_test_status_update(test, "Failed to open sorcery structure\n");
  2400. return AST_TEST_FAIL;
  2401. }
  2402. if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
  2403. ast_test_status_update(test, "Failed to allocate a known object type\n");
  2404. return AST_TEST_FAIL;
  2405. }
  2406. if (ast_sorcery_create(sorcery, obj)) {
  2407. ast_test_status_update(test, "Failed to create a known object type\n");
  2408. return AST_TEST_FAIL;
  2409. }
  2410. if (!(buf = ast_str_create(16))) {
  2411. ast_test_status_update(test, "Failed to allocate return buffer\n");
  2412. return AST_TEST_FAIL;
  2413. }
  2414. ast_str_reset(buf);
  2415. snprintf(expression, sizeof(expression), "AST_SORCERY(%s,%s,%s,%s)", "notest_sorcery", "test", "blah", "bob");
  2416. if (!ast_func_read2(NULL, expression, &buf, 16)) {
  2417. ast_free(buf);
  2418. ast_test_status_update(test, "Retrieved a non-existent module\n");
  2419. return AST_TEST_FAIL;
  2420. }
  2421. ast_str_reset(buf);
  2422. snprintf(expression, sizeof(expression), "AST_SORCERY(%s,%s,%s,%s)", "test_sorcery", "notest", "blah", "bob");
  2423. if (!ast_func_read2(NULL, expression, &buf, 16)) {
  2424. ast_free(buf);
  2425. ast_test_status_update(test, "Retrieved a non-existent type\n");
  2426. return AST_TEST_FAIL;
  2427. }
  2428. ast_str_reset(buf);
  2429. snprintf(expression, sizeof(expression), "AST_SORCERY(%s,%s,%s,%s)", "test_sorcery", "test", "noid", "bob");
  2430. if (!ast_func_read2(NULL, expression, &buf, 16)) {
  2431. ast_free(buf);
  2432. ast_test_status_update(test, "Retrieved a non-existent id\n");
  2433. return AST_TEST_FAIL;
  2434. }
  2435. ast_str_reset(buf);
  2436. snprintf(expression, sizeof(expression), "AST_SORCERY(%s,%s,%s,%s)", "test_sorcery", "test", "blah", "nobob");
  2437. if (!ast_func_read2(NULL, expression, &buf, 16)) {
  2438. ast_free(buf);
  2439. ast_test_status_update(test, "Retrieved a non-existent field\n");
  2440. return AST_TEST_FAIL;
  2441. }
  2442. ast_str_reset(buf);
  2443. snprintf(expression, sizeof(expression), "AST_SORCERY(%s,%s,%s,%s)", "test_sorcery", "test", "blah", "bob");
  2444. if (ast_func_read2(NULL, expression, &buf, 16)) {
  2445. ast_free(buf);
  2446. ast_test_status_update(test, "Failed retrieve field 'bob'\n");
  2447. return AST_TEST_FAIL;
  2448. }
  2449. if (strcmp(ast_str_buffer(buf), "5")) {
  2450. ast_free(buf);
  2451. ast_test_status_update(test, "Failed retrieve field. Got '%u', should be '5'\n", obj->bob);
  2452. return AST_TEST_FAIL;
  2453. }
  2454. ast_str_reset(buf);
  2455. snprintf(expression, sizeof(expression), "AST_SORCERY(%s,%s,%s,%s,single,1)", "test_sorcery", "test", "blah", "bob");
  2456. if (ast_func_read2(NULL, expression, &buf, 16)) {
  2457. ast_free(buf);
  2458. ast_test_status_update(test, "Failed retrieve field 'bob'\n");
  2459. return AST_TEST_FAIL;
  2460. }
  2461. if (strcmp(ast_str_buffer(buf), "5")) {
  2462. ast_free(buf);
  2463. ast_test_status_update(test, "Failed retrieve field. Got '%u', should be '5'\n", obj->bob);
  2464. return AST_TEST_FAIL;
  2465. }
  2466. ast_str_reset(buf);
  2467. snprintf(expression, sizeof(expression), "AST_SORCERY(%s,%s,%s,%s,single,2)", "test_sorcery", "test", "blah", "bob");
  2468. if (!ast_func_read2(NULL, expression, &buf, 16)) {
  2469. ast_free(buf);
  2470. ast_test_status_update(test, "Got a second 'bob' and shouldn't have\n");
  2471. return AST_TEST_FAIL;
  2472. }
  2473. /* 444 is already the first item in the list */
  2474. jim_handler(NULL, ast_variable_new("jim", "555", ""), obj);
  2475. jim_handler(NULL, ast_variable_new("jim", "666", ""), obj);
  2476. ast_str_reset(buf);
  2477. snprintf(expression, sizeof(expression), "AST_SORCERY(%s,%s,%s,%s)", "test_sorcery", "test", "blah", "jim");
  2478. if (ast_func_read2(NULL, expression, &buf, 16)) {
  2479. ast_free(buf);
  2480. ast_test_status_update(test, "Couldn't retrieve 'jim'\n");
  2481. return AST_TEST_FAIL;
  2482. }
  2483. if (strcmp(ast_str_buffer(buf), "444,555,666")) {
  2484. ast_free(buf);
  2485. ast_test_status_update(test, "Failed retrieve jim. Got '%s', should be '444,555,666'\n", ast_str_buffer(buf));
  2486. return AST_TEST_FAIL;
  2487. }
  2488. ast_str_reset(buf);
  2489. snprintf(expression, sizeof(expression), "AST_SORCERY(%s,%s,%s,%s,single,2)", "test_sorcery", "test", "blah", "jim");
  2490. if (ast_func_read2(NULL, expression, &buf, 16)) {
  2491. ast_free(buf);
  2492. ast_test_status_update(test, "Couldn't retrieve 2nd jim\n");
  2493. return AST_TEST_FAIL;
  2494. }
  2495. if (strcmp(ast_str_buffer(buf), "555")) {
  2496. ast_free(buf);
  2497. ast_test_status_update(test, "Failed retrieve 2nd jim. Got '%s', should be '555'\n", ast_str_buffer(buf));
  2498. return AST_TEST_FAIL;
  2499. }
  2500. ast_str_reset(buf);
  2501. snprintf(expression, sizeof(expression), "AST_SORCERY(%s,%s,%s,%s,concat,|)", "test_sorcery", "test", "blah", "jim");
  2502. if (ast_func_read2(NULL, expression, &buf, 16)) {
  2503. ast_free(buf);
  2504. ast_test_status_update(test, "Couldn't retrieve any 'jim'\n");
  2505. return AST_TEST_FAIL;
  2506. }
  2507. if (strcmp(ast_str_buffer(buf), "444|555|666")) {
  2508. ast_free(buf);
  2509. ast_test_status_update(test, "Failed retrieve 'jim'. Got '%s', should be '444|555|666'\n", ast_str_buffer(buf));
  2510. return AST_TEST_FAIL;
  2511. }
  2512. ast_str_reset(buf);
  2513. snprintf(expression, sizeof(expression), "AST_SORCERY(%s,%s,%s,%s,noconcat,3)", "test_sorcery", "test", "blah", "jim");
  2514. if (!ast_func_read2(NULL, expression, &buf, 16)) {
  2515. ast_free(buf);
  2516. ast_test_status_update(test, "Should have failed with invalid retrieval_type\n");
  2517. return AST_TEST_FAIL;
  2518. }
  2519. ast_str_reset(buf);
  2520. snprintf(expression, sizeof(expression), "AST_SORCERY(%s,%s,%s,%s,single,|)", "test_sorcery", "test", "blah", "jim");
  2521. if (!ast_func_read2(NULL, expression, &buf, 16)) {
  2522. ast_free(buf);
  2523. ast_test_status_update(test, "Should have failed with invalid occurrence_number\n");
  2524. return AST_TEST_FAIL;
  2525. }
  2526. ast_free(buf);
  2527. return AST_TEST_PASS;
  2528. }
  2529. AST_TEST_DEFINE(object_field_registered)
  2530. {
  2531. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  2532. RAII_VAR(struct ast_sorcery_object_type *, object_type, NULL, ao2_cleanup);
  2533. switch (cmd) {
  2534. case TEST_INIT:
  2535. info->name = "object_field_registered";
  2536. info->category = "/main/sorcery/";
  2537. info->summary = "ast_sorcery_is_object_field_registered unit test";
  2538. info->description =
  2539. "Test ast_sorcery_is_object_field_registered in sorcery";
  2540. return AST_TEST_NOT_RUN;
  2541. case TEST_EXECUTE:
  2542. break;
  2543. }
  2544. if (!(sorcery = alloc_and_initialize_sorcery())) {
  2545. ast_test_status_update(test, "Failed to open sorcery structure\n");
  2546. return AST_TEST_FAIL;
  2547. }
  2548. object_type = ast_sorcery_get_object_type(sorcery, "test");
  2549. ast_sorcery_object_fields_register(sorcery, "test", "^prefix/.", test_sorcery_regex_handler, test_sorcery_regex_fields);
  2550. ast_test_validate(test, ast_sorcery_is_object_field_registered(object_type, "joe"));
  2551. ast_test_validate(test, ast_sorcery_is_object_field_registered(object_type, "bob"));
  2552. ast_test_validate(test, ast_sorcery_is_object_field_registered(object_type, "@joebob"));
  2553. ast_test_validate(test, ast_sorcery_is_object_field_registered(object_type, "prefix/goober"));
  2554. ast_test_validate(test, !ast_sorcery_is_object_field_registered(object_type, "joebob"));
  2555. ast_test_validate(test, !ast_sorcery_is_object_field_registered(object_type, "prefix/"));
  2556. ast_test_validate(test, !ast_sorcery_is_object_field_registered(object_type, "goober"));
  2557. ast_sorcery_object_fields_register(sorcery, "test", "^", test_sorcery_regex_handler, test_sorcery_regex_fields);
  2558. ast_test_validate(test, ast_sorcery_is_object_field_registered(object_type, "goober"));
  2559. return AST_TEST_PASS;
  2560. }
  2561. static int event_observed;
  2562. static void wizard_observer(const char *name, const struct ast_sorcery_wizard *wizard)
  2563. {
  2564. if (!strcmp(wizard->name, "test")) {
  2565. event_observed = 1;
  2566. }
  2567. }
  2568. static void instance_observer(const char *name, struct ast_sorcery *sorcery)
  2569. {
  2570. if (!strcmp(name, "test_sorcery")) {
  2571. event_observed = 1;
  2572. }
  2573. }
  2574. AST_TEST_DEFINE(global_observation)
  2575. {
  2576. RAII_VAR(struct ast_sorcery_wizard *, wizard, &test_wizard, ast_sorcery_wizard_unregister);
  2577. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  2578. const struct ast_sorcery_global_observer observer = {
  2579. .wizard_registered = wizard_observer,
  2580. .instance_created = instance_observer,
  2581. .wizard_unregistering = wizard_observer,
  2582. .instance_destroying = instance_observer,
  2583. };
  2584. switch (cmd) {
  2585. case TEST_INIT:
  2586. info->name = "global_observation";
  2587. info->category = "/main/sorcery/";
  2588. info->summary = "global sorcery observation test";
  2589. info->description =
  2590. "Test observation of sorcery (global)";
  2591. return AST_TEST_NOT_RUN;
  2592. case TEST_EXECUTE:
  2593. break;
  2594. }
  2595. ast_sorcery_global_observer_add(&observer);
  2596. event_observed = 0;
  2597. ast_sorcery_wizard_register(wizard);
  2598. ast_test_validate(test, (event_observed == 1), "Wizard registered failed");
  2599. event_observed = 0;
  2600. ast_sorcery_wizard_unregister(wizard);
  2601. ast_test_validate(test, (event_observed == 1), "Wizard unregistered failed");
  2602. event_observed = 0;
  2603. sorcery = ast_sorcery_open();
  2604. ast_test_validate(test, (event_observed == 1), "Instance created failed");
  2605. event_observed = 0;
  2606. ast_sorcery_unref(sorcery);
  2607. sorcery = NULL;
  2608. ast_test_validate(test, (event_observed == 1), "Instance destroyed failed");
  2609. ast_sorcery_global_observer_remove(&observer);
  2610. event_observed = 0;
  2611. ast_sorcery_wizard_register(&test_wizard);
  2612. ast_test_validate(test, (event_observed == 0), "Observer removed failed");
  2613. return AST_TEST_PASS;
  2614. }
  2615. static void instance_loaded_observer(const char *name, const struct ast_sorcery *sorcery,
  2616. int reloaded)
  2617. {
  2618. if (!strcmp(name, "test_sorcery") && !reloaded) {
  2619. event_observed++;
  2620. }
  2621. }
  2622. static void instance_reloaded_observer(const char *name,
  2623. const struct ast_sorcery *sorcery, int reloaded)
  2624. {
  2625. if (!strcmp(name, "test_sorcery") && reloaded) {
  2626. event_observed++;
  2627. }
  2628. }
  2629. static void wizard_mapped_observer(const char *name, struct ast_sorcery *sorcery,
  2630. const char *object_type, struct ast_sorcery_wizard *wizard,
  2631. const char *wizard_args, void *wizard_data)
  2632. {
  2633. if (!strcmp(name, "test_sorcery") && !strcmp(object_type, "test_object_type")
  2634. && !strcmp(wizard->name, "memory") && !strcmp(wizard_args, "memwiz")) {
  2635. event_observed++;
  2636. }
  2637. }
  2638. static void object_type_registered_observer(const char *name,
  2639. struct ast_sorcery *sorcery, const char *object_type)
  2640. {
  2641. if (!strcmp(name, "test_sorcery") && !strcmp(object_type, "test_object_type")) {
  2642. event_observed++;
  2643. }
  2644. }
  2645. static void object_type_loaded_observer(const char *name,
  2646. const struct ast_sorcery *sorcery, const char *object_type, int reloaded)
  2647. {
  2648. if (!strcmp(name, "test_sorcery") && !strcmp(object_type, "test_object_type")
  2649. && !reloaded) {
  2650. event_observed++;
  2651. }
  2652. }
  2653. static void object_type_reloaded_observer(const char *name,
  2654. const struct ast_sorcery *sorcery, const char *object_type, int reloaded)
  2655. {
  2656. if (!strcmp(name, "test_sorcery") && !strcmp(object_type, "test_object_type")
  2657. && reloaded) {
  2658. event_observed++;
  2659. }
  2660. }
  2661. AST_TEST_DEFINE(instance_observation)
  2662. {
  2663. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  2664. struct ast_sorcery_instance_observer observer = {
  2665. .wizard_mapped = wizard_mapped_observer,
  2666. .object_type_registered = object_type_registered_observer,
  2667. };
  2668. switch (cmd) {
  2669. case TEST_INIT:
  2670. info->name = "instance_observation";
  2671. info->category = "/main/sorcery/";
  2672. info->summary = "sorcery instance observation test";
  2673. info->description =
  2674. "Test observation of sorcery (instance)";
  2675. return AST_TEST_NOT_RUN;
  2676. case TEST_EXECUTE:
  2677. break;
  2678. }
  2679. /* Test instance load */
  2680. if (!(sorcery = ast_sorcery_open())) {
  2681. ast_test_status_update(test, "Failed to open a sorcery instance\n");
  2682. return AST_TEST_FAIL;
  2683. }
  2684. observer.instance_loading = instance_loaded_observer;
  2685. observer.instance_loaded = instance_loaded_observer;
  2686. ast_sorcery_instance_observer_add(sorcery, &observer);
  2687. event_observed = 0;
  2688. ast_sorcery_load(sorcery);
  2689. ast_test_validate(test, (event_observed == 2), "Instance loaded failed");
  2690. event_observed = 0;
  2691. ast_sorcery_reload(sorcery);
  2692. ast_test_validate(test, (event_observed == 0), "Instance reloaded failed");
  2693. /* Test instance reload */
  2694. ast_sorcery_instance_observer_remove(sorcery, &observer);
  2695. observer.instance_loading = instance_reloaded_observer;
  2696. observer.instance_loaded = instance_reloaded_observer;
  2697. ast_sorcery_instance_observer_add(sorcery, &observer);
  2698. event_observed = 0;
  2699. ast_sorcery_load(sorcery);
  2700. ast_test_validate(test, (event_observed == 0), "Instance loaded failed");
  2701. event_observed = 0;
  2702. ast_sorcery_reload(sorcery);
  2703. ast_test_validate(test, (event_observed == 2), "Instance reloaded failed");
  2704. /* Test wizard mapping */
  2705. event_observed = 0;
  2706. ast_sorcery_apply_default(sorcery, "test_object_type", "memory", "memwiz");
  2707. ast_test_validate(test, (event_observed == 1), "Wizard mapping failed");
  2708. /* Test object type register */
  2709. event_observed = 0;
  2710. ast_sorcery_internal_object_register(sorcery, "test_object_type",
  2711. test_sorcery_object_alloc, NULL, NULL);
  2712. ast_test_validate(test, (event_observed == 1), "Object type registered failed");
  2713. /* Test object type load */
  2714. ast_sorcery_instance_observer_remove(sorcery, &observer);
  2715. observer.object_type_loading = object_type_loaded_observer;
  2716. observer.object_type_loaded = object_type_loaded_observer;
  2717. ast_sorcery_instance_observer_add(sorcery, &observer);
  2718. event_observed = 0;
  2719. ast_sorcery_load_object(sorcery, "test_object_type");
  2720. ast_test_validate(test, (event_observed == 2), "Object type loaded failed");
  2721. event_observed = 0;
  2722. ast_sorcery_reload_object(sorcery, "test_object_type");
  2723. ast_test_validate(test, (event_observed == 0), "Object type reloaded failed");
  2724. /* Test object type reload */
  2725. ast_sorcery_instance_observer_remove(sorcery, &observer);
  2726. observer.object_type_loading = object_type_reloaded_observer;
  2727. observer.object_type_loaded = object_type_reloaded_observer;
  2728. ast_sorcery_instance_observer_add(sorcery, &observer);
  2729. event_observed = 0;
  2730. ast_sorcery_load_object(sorcery, "test_object_type");
  2731. ast_test_validate(test, (event_observed == 0), "Object type loaded failed");
  2732. event_observed = 0;
  2733. ast_sorcery_reload_object(sorcery, "test_object_type");
  2734. ast_test_validate(test, (event_observed == 2), "Object type reloaded failed");
  2735. ast_sorcery_instance_observer_remove(sorcery, &observer);
  2736. event_observed = 0;
  2737. ast_sorcery_apply_default(sorcery, "test_object_type", "memory", "memwiz");
  2738. ast_test_validate(test, (event_observed == 0), "Observer remove failed");
  2739. return AST_TEST_PASS;
  2740. }
  2741. static void wizard_loaded_observer(const char *name,
  2742. const struct ast_sorcery_wizard *wizard, const char *object_type, int reloaded)
  2743. {
  2744. if (!strcmp(name, "test") && !strcmp(object_type, "test_object_type")
  2745. && !reloaded) {
  2746. event_observed++;
  2747. }
  2748. }
  2749. static void sorcery_test_load(void *data, const struct ast_sorcery *sorcery, const char *type)
  2750. {
  2751. return;
  2752. }
  2753. static void wizard_reloaded_observer(const char *name,
  2754. const struct ast_sorcery_wizard *wizard, const char *object_type, int reloaded)
  2755. {
  2756. if (!strcmp(name, "test") && !strcmp(object_type, "test_object_type")
  2757. && reloaded) {
  2758. event_observed++;
  2759. }
  2760. }
  2761. AST_TEST_DEFINE(wizard_observation)
  2762. {
  2763. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  2764. RAII_VAR(struct ast_sorcery_wizard *, wizard, &test_wizard, ast_sorcery_wizard_unregister);
  2765. struct ast_sorcery_wizard_observer observer = {
  2766. .wizard_loading = wizard_loaded_observer,
  2767. .wizard_loaded = wizard_loaded_observer,
  2768. };
  2769. switch (cmd) {
  2770. case TEST_INIT:
  2771. info->name = "wizard_observation";
  2772. info->category = "/main/sorcery/";
  2773. info->summary = "sorcery wizard observation test";
  2774. info->description =
  2775. "Test observation of sorcery (wizard)";
  2776. return AST_TEST_NOT_RUN;
  2777. case TEST_EXECUTE:
  2778. break;
  2779. }
  2780. wizard->load = sorcery_test_load;
  2781. wizard->reload = sorcery_test_load;
  2782. /* Test wizard observer remove and wizard unregister */
  2783. ast_sorcery_wizard_register(wizard);
  2784. ast_sorcery_wizard_observer_add(wizard, &observer);
  2785. ast_sorcery_wizard_observer_remove(wizard, &observer);
  2786. event_observed = 0;
  2787. ast_sorcery_wizard_unregister(wizard);
  2788. ast_test_validate(test, (event_observed == 0), "Wizard observer removed failed");
  2789. /* Setup for test loaded and reloaded */
  2790. if (!(sorcery = ast_sorcery_open())) {
  2791. ast_test_status_update(test, "Failed to open a sorcery instance\n");
  2792. return AST_TEST_FAIL;
  2793. }
  2794. ast_sorcery_wizard_register(wizard);
  2795. ast_sorcery_apply_default(sorcery, "test_object_type", "test", NULL);
  2796. ast_sorcery_internal_object_register(sorcery, "test_object_type",
  2797. test_sorcery_object_alloc, NULL, NULL);
  2798. /* Test wizard loading and loaded */
  2799. ast_sorcery_wizard_observer_add(wizard, &observer);
  2800. event_observed = 0;
  2801. ast_sorcery_load_object(sorcery, "test_object_type");
  2802. ast_test_validate(test, (event_observed == 2), "Wizard loaded failed");
  2803. event_observed = 0;
  2804. ast_sorcery_reload_object(sorcery, "test_object_type");
  2805. ast_sorcery_wizard_observer_remove(wizard, &observer);
  2806. ast_test_validate(test, (event_observed == 0), "Wizard reloaded failed");
  2807. /* Test wizard reloading and reloaded */
  2808. observer.wizard_loading = wizard_reloaded_observer;
  2809. observer.wizard_loaded = wizard_reloaded_observer;
  2810. ast_sorcery_wizard_observer_add(wizard, &observer);
  2811. event_observed = 0;
  2812. ast_sorcery_load_object(sorcery, "test_object_type");
  2813. ast_test_validate(test, (event_observed == 0), "Wizard loaded failed");
  2814. event_observed = 0;
  2815. ast_sorcery_reload_object(sorcery, "test_object_type");
  2816. ast_sorcery_wizard_observer_remove(wizard, &observer);
  2817. ast_test_validate(test, (event_observed == 2), "Wizard reloaded failed");
  2818. return AST_TEST_PASS;
  2819. }
  2820. AST_TEST_DEFINE(wizard_apply_and_insert)
  2821. {
  2822. RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
  2823. RAII_VAR(struct ast_sorcery_wizard *, wizard1, &test_wizard, ast_sorcery_wizard_unregister);
  2824. RAII_VAR(struct ast_sorcery_wizard *, wizard2, &test_wizard2, ast_sorcery_wizard_unregister);
  2825. RAII_VAR(struct ast_sorcery_wizard *, wizard, NULL, ao2_cleanup);
  2826. void *data;
  2827. switch (cmd) {
  2828. case TEST_INIT:
  2829. info->name = "wizard_apply_and_insert";
  2830. info->category = "/main/sorcery/";
  2831. info->summary = "sorcery wizard apply and insert test";
  2832. info->description =
  2833. "sorcery wizard apply and insert test";
  2834. return AST_TEST_NOT_RUN;
  2835. case TEST_EXECUTE:
  2836. break;
  2837. }
  2838. wizard1->load = sorcery_test_load;
  2839. wizard1->reload = sorcery_test_load;
  2840. wizard2->load = sorcery_test_load;
  2841. wizard2->reload = sorcery_test_load;
  2842. if (!(sorcery = ast_sorcery_open())) {
  2843. ast_test_status_update(test, "Failed to open a sorcery instance\n");
  2844. return AST_TEST_FAIL;
  2845. }
  2846. ast_sorcery_wizard_register(wizard1);
  2847. ast_sorcery_wizard_register(wizard2);
  2848. /* test_object_type isn't registered yet so count should return error */
  2849. ast_test_validate(test,
  2850. ast_sorcery_get_wizard_mapping_count(sorcery, "test_object_type") == -1);
  2851. ast_sorcery_apply_default(sorcery, "test_object_type", "test", NULL);
  2852. ast_test_validate(test,
  2853. ast_sorcery_get_wizard_mapping_count(sorcery, "test_object_type") == 1);
  2854. ast_test_validate(test,
  2855. ast_sorcery_get_wizard_mapping(sorcery, "test_object_type", 0, &wizard, NULL) == 0);
  2856. ast_test_validate(test, strcmp("test", wizard->name) == 0);
  2857. ao2_ref(wizard, -1);
  2858. wizard = NULL;
  2859. ast_test_validate(test,
  2860. ast_sorcery_insert_wizard_mapping(sorcery, "test_object_type", "test2", "test2data", 0, 0) == 0);
  2861. ast_test_validate(test,
  2862. ast_sorcery_insert_wizard_mapping(sorcery, "test_object_type", "test2", "test2data", 0, 0) != 0);
  2863. ast_test_validate(test,
  2864. ast_sorcery_get_wizard_mapping(sorcery, "test_object_type", 0, &wizard, &data) == 0);
  2865. ast_test_validate(test, strcmp("test2", wizard->name) == 0);
  2866. ast_test_validate(test, strcmp("test2data", data) == 0);
  2867. ao2_ref(wizard, -1);
  2868. wizard = NULL;
  2869. ast_test_validate(test,
  2870. ast_sorcery_get_wizard_mapping(sorcery, "test_object_type", 1, &wizard, NULL) == 0);
  2871. ast_test_validate(test, strcmp("test", wizard->name) == 0);
  2872. ao2_ref(wizard, -1);
  2873. wizard = NULL;
  2874. /* Test failures */
  2875. ast_test_validate(test,
  2876. ast_sorcery_get_wizard_mapping(sorcery, "non-existent-type", 0, &wizard, NULL) != 0);
  2877. ast_test_validate(test,
  2878. ast_sorcery_get_wizard_mapping(sorcery, "test_object_type", -1, &wizard, &data) != 0);
  2879. ast_test_validate(test,
  2880. ast_sorcery_get_wizard_mapping(sorcery, "test_object_type", 2, &wizard, NULL) != 0);
  2881. ast_test_validate(test,
  2882. ast_sorcery_get_wizard_mapping(sorcery, "test_object_type", 2, NULL, NULL) != 0);
  2883. /* Test remove */
  2884. /* Should fail */
  2885. ast_test_validate(test,
  2886. ast_sorcery_remove_wizard_mapping(sorcery, "non-existent-type", "somewizard") != 0);
  2887. ast_test_validate(test,
  2888. ast_sorcery_remove_wizard_mapping(sorcery, "test_object_type", "somewizard") != 0);
  2889. /* should work */
  2890. ast_test_validate(test,
  2891. ast_sorcery_remove_wizard_mapping(sorcery, "test_object_type", "test") == 0);
  2892. ast_test_validate(test,
  2893. ast_sorcery_get_wizard_mapping_count(sorcery, "test_object_type") == 1);
  2894. ast_test_validate(test,
  2895. ast_sorcery_get_wizard_mapping(sorcery, "test_object_type", 0, &wizard, &data) == 0);
  2896. ast_test_validate(test, strcmp("test2", wizard->name) == 0);
  2897. ast_test_validate(test, strcmp("test2data", data) == 0);
  2898. ao2_ref(wizard, -1);
  2899. wizard = NULL;
  2900. return AST_TEST_PASS;
  2901. }
  2902. static int unload_module(void)
  2903. {
  2904. AST_TEST_UNREGISTER(wizard_registration);
  2905. AST_TEST_UNREGISTER(sorcery_open);
  2906. AST_TEST_UNREGISTER(apply_default);
  2907. AST_TEST_UNREGISTER(apply_config);
  2908. AST_TEST_UNREGISTER(object_register);
  2909. AST_TEST_UNREGISTER(object_register_without_mapping);
  2910. AST_TEST_UNREGISTER(object_field_register);
  2911. AST_TEST_UNREGISTER(object_fields_register);
  2912. AST_TEST_UNREGISTER(object_alloc_with_id);
  2913. AST_TEST_UNREGISTER(object_alloc_without_id);
  2914. AST_TEST_UNREGISTER(object_copy);
  2915. AST_TEST_UNREGISTER(object_copy_native);
  2916. AST_TEST_UNREGISTER(object_diff);
  2917. AST_TEST_UNREGISTER(object_diff_native);
  2918. AST_TEST_UNREGISTER(objectset_create);
  2919. AST_TEST_UNREGISTER(objectset_json_create);
  2920. AST_TEST_UNREGISTER(objectset_create_regex);
  2921. AST_TEST_UNREGISTER(objectset_apply);
  2922. AST_TEST_UNREGISTER(objectset_apply_handler);
  2923. AST_TEST_UNREGISTER(objectset_apply_invalid);
  2924. AST_TEST_UNREGISTER(objectset_transform);
  2925. AST_TEST_UNREGISTER(objectset_apply_fields);
  2926. AST_TEST_UNREGISTER(extended_fields);
  2927. AST_TEST_UNREGISTER(changeset_create);
  2928. AST_TEST_UNREGISTER(changeset_create_unchanged);
  2929. AST_TEST_UNREGISTER(object_create);
  2930. AST_TEST_UNREGISTER(object_retrieve_id);
  2931. AST_TEST_UNREGISTER(object_retrieve_field);
  2932. AST_TEST_UNREGISTER(object_retrieve_multiple_all);
  2933. AST_TEST_UNREGISTER(object_retrieve_multiple_field);
  2934. AST_TEST_UNREGISTER(object_retrieve_regex);
  2935. AST_TEST_UNREGISTER(object_update);
  2936. AST_TEST_UNREGISTER(object_update_uncreated);
  2937. AST_TEST_UNREGISTER(object_delete);
  2938. AST_TEST_UNREGISTER(object_delete_uncreated);
  2939. AST_TEST_UNREGISTER(caching_wizard_behavior);
  2940. AST_TEST_UNREGISTER(object_type_observer);
  2941. AST_TEST_UNREGISTER(configuration_file_wizard);
  2942. AST_TEST_UNREGISTER(configuration_file_wizard_with_file_integrity);
  2943. AST_TEST_UNREGISTER(configuration_file_wizard_with_criteria);
  2944. AST_TEST_UNREGISTER(configuration_file_wizard_retrieve_field);
  2945. AST_TEST_UNREGISTER(configuration_file_wizard_retrieve_multiple);
  2946. AST_TEST_UNREGISTER(configuration_file_wizard_retrieve_multiple_all);
  2947. AST_TEST_UNREGISTER(dialplan_function);
  2948. AST_TEST_UNREGISTER(object_field_registered);
  2949. AST_TEST_UNREGISTER(global_observation);
  2950. AST_TEST_UNREGISTER(instance_observation);
  2951. AST_TEST_UNREGISTER(wizard_observation);
  2952. AST_TEST_UNREGISTER(wizard_apply_and_insert);
  2953. return 0;
  2954. }
  2955. static int load_module(void)
  2956. {
  2957. AST_TEST_REGISTER(wizard_apply_and_insert);
  2958. AST_TEST_REGISTER(wizard_registration);
  2959. AST_TEST_REGISTER(sorcery_open);
  2960. AST_TEST_REGISTER(apply_default);
  2961. AST_TEST_REGISTER(apply_config);
  2962. AST_TEST_REGISTER(object_register);
  2963. AST_TEST_REGISTER(object_register_without_mapping);
  2964. AST_TEST_REGISTER(object_field_register);
  2965. AST_TEST_REGISTER(object_fields_register);
  2966. AST_TEST_REGISTER(object_alloc_with_id);
  2967. AST_TEST_REGISTER(object_alloc_without_id);
  2968. AST_TEST_REGISTER(object_copy);
  2969. AST_TEST_REGISTER(object_copy_native);
  2970. AST_TEST_REGISTER(object_diff);
  2971. AST_TEST_REGISTER(object_diff_native);
  2972. AST_TEST_REGISTER(objectset_create);
  2973. AST_TEST_REGISTER(objectset_json_create);
  2974. AST_TEST_REGISTER(objectset_create_regex);
  2975. AST_TEST_REGISTER(objectset_apply);
  2976. AST_TEST_REGISTER(objectset_apply_handler);
  2977. AST_TEST_REGISTER(objectset_apply_invalid);
  2978. AST_TEST_REGISTER(objectset_transform);
  2979. AST_TEST_REGISTER(objectset_apply_fields);
  2980. AST_TEST_REGISTER(extended_fields);
  2981. AST_TEST_REGISTER(changeset_create);
  2982. AST_TEST_REGISTER(changeset_create_unchanged);
  2983. AST_TEST_REGISTER(object_create);
  2984. AST_TEST_REGISTER(object_retrieve_id);
  2985. AST_TEST_REGISTER(object_retrieve_field);
  2986. AST_TEST_REGISTER(object_retrieve_multiple_all);
  2987. AST_TEST_REGISTER(object_retrieve_multiple_field);
  2988. AST_TEST_REGISTER(object_retrieve_regex);
  2989. AST_TEST_REGISTER(object_update);
  2990. AST_TEST_REGISTER(object_update_uncreated);
  2991. AST_TEST_REGISTER(object_delete);
  2992. AST_TEST_REGISTER(object_delete_uncreated);
  2993. AST_TEST_REGISTER(caching_wizard_behavior);
  2994. AST_TEST_REGISTER(object_type_observer);
  2995. AST_TEST_REGISTER(configuration_file_wizard);
  2996. AST_TEST_REGISTER(configuration_file_wizard_with_file_integrity);
  2997. AST_TEST_REGISTER(configuration_file_wizard_with_criteria);
  2998. AST_TEST_REGISTER(configuration_file_wizard_retrieve_field);
  2999. AST_TEST_REGISTER(configuration_file_wizard_retrieve_multiple);
  3000. AST_TEST_REGISTER(configuration_file_wizard_retrieve_multiple_all);
  3001. AST_TEST_REGISTER(dialplan_function);
  3002. AST_TEST_REGISTER(object_field_registered);
  3003. AST_TEST_REGISTER(global_observation);
  3004. AST_TEST_REGISTER(instance_observation);
  3005. AST_TEST_REGISTER(wizard_observation);
  3006. return AST_MODULE_LOAD_SUCCESS;
  3007. }
  3008. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Sorcery test module");