test_sorcery.c 123 KB

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