test_config.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * Mark Michelson <mmichelson@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 Configuration unit tests
  21. *
  22. * \author Mark Michelson <mmichelson@digium.com>
  23. *
  24. */
  25. /*** MODULEINFO
  26. <depend>TEST_FRAMEWORK</depend>
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
  31. #include "asterisk/config.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/test.h"
  34. #include "asterisk/paths.h"
  35. #define CONFIG_FILE "test_config.conf"
  36. /*
  37. * This builds the folowing config:
  38. * [Capitals]
  39. * Germany = Berlin
  40. * China = Beijing
  41. * Canada = Ottawa
  42. *
  43. * [Protagonists]
  44. * 1984 = Winston Smith
  45. * Green Eggs And Ham = Sam I Am
  46. * The Kalevala = Vainamoinen
  47. *
  48. * This config is used for all tests below.
  49. */
  50. const char cat1[] = "Capitals";
  51. const char cat1varname1[] = "Germany";
  52. const char cat1varvalue1[] = "Berlin";
  53. const char cat1varname2[] = "China";
  54. const char cat1varvalue2[] = "Beijing";
  55. const char cat1varname3[] = "Canada";
  56. const char cat1varvalue3[] = "Ottawa";
  57. const char cat2[] = "Protagonists";
  58. const char cat2varname1[] = "1984";
  59. const char cat2varvalue1[] = "Winston Smith";
  60. const char cat2varname2[] = "Green Eggs And Ham";
  61. const char cat2varvalue2[] = "Sam I Am";
  62. const char cat2varname3[] = "The Kalevala";
  63. const char cat2varvalue3[] = "Vainamoinen";
  64. struct pair {
  65. const char *name;
  66. const char *val;
  67. };
  68. struct association {
  69. const char *category;
  70. struct pair vars[3];
  71. } categories [] = {
  72. { cat1,
  73. {
  74. { cat1varname1, cat1varvalue1 },
  75. { cat1varname2, cat1varvalue2 },
  76. { cat1varname3, cat1varvalue3 },
  77. }
  78. },
  79. { cat2,
  80. {
  81. { cat2varname1, cat2varvalue1 },
  82. { cat2varname2, cat2varvalue2 },
  83. { cat2varname3, cat2varvalue3 },
  84. }
  85. },
  86. };
  87. /*!
  88. * \brief Build ast_config struct from above definitions
  89. *
  90. * \retval NULL Failed to build the config
  91. * \retval non-NULL An ast_config struct populated with data
  92. */
  93. static struct ast_config *build_cfg(void)
  94. {
  95. struct ast_config *cfg;
  96. struct association *cat_iter;
  97. struct pair *var_iter;
  98. size_t i;
  99. size_t j;
  100. cfg = ast_config_new();
  101. if (!cfg) {
  102. goto fail;
  103. }
  104. for (i = 0; i < ARRAY_LEN(categories); ++i) {
  105. struct ast_category *cat;
  106. cat_iter = &categories[i];
  107. cat = ast_category_new(cat_iter->category, "", 999999);
  108. if (!cat) {
  109. goto fail;
  110. }
  111. ast_category_append(cfg, cat);
  112. for (j = 0; j < ARRAY_LEN(cat_iter->vars); ++j) {
  113. struct ast_variable *var;
  114. var_iter = &cat_iter->vars[j];
  115. var = ast_variable_new(var_iter->name, var_iter->val, "");
  116. if (!var) {
  117. goto fail;
  118. }
  119. ast_variable_append(cat, var);
  120. }
  121. }
  122. return cfg;
  123. fail:
  124. ast_config_destroy(cfg);
  125. return NULL;
  126. }
  127. /*!
  128. * \brief Tests that the contents of an ast_config is what is expected
  129. *
  130. * \param cfg Config to test
  131. * \retval -1 Failed to pass a test
  132. * \retval 0 Config passes checks
  133. */
  134. static int test_config_validity(struct ast_config *cfg)
  135. {
  136. int i;
  137. const char *cat_iter = NULL;
  138. /* Okay, let's see if the correct content is there */
  139. for (i = 0; i < ARRAY_LEN(categories); ++i) {
  140. struct ast_variable *var = NULL;
  141. size_t j;
  142. cat_iter = ast_category_browse(cfg, cat_iter);
  143. if (strcmp(cat_iter, categories[i].category)) {
  144. ast_log(LOG_ERROR, "Category name mismatch, %s does not match %s\n", cat_iter, categories[i].category);
  145. return -1;
  146. }
  147. for (j = 0; j < ARRAY_LEN(categories[i].vars); ++j) {
  148. var = var ? var->next : ast_variable_browse(cfg, cat_iter);
  149. if (strcmp(var->name, categories[i].vars[j].name)) {
  150. ast_log(LOG_ERROR, "Variable name mismatch, %s does not match %s\n", var->name, categories[i].vars[j].name);
  151. return -1;
  152. }
  153. if (strcmp(var->value, categories[i].vars[j].val)) {
  154. ast_log(LOG_ERROR, "Variable value mismatch, %s does not match %s\n", var->value, categories[i].vars[j].val);
  155. return -1;
  156. }
  157. }
  158. }
  159. return 0;
  160. }
  161. AST_TEST_DEFINE(copy_config)
  162. {
  163. enum ast_test_result_state res = AST_TEST_FAIL;
  164. struct ast_config *cfg = NULL;
  165. struct ast_config *copy = NULL;
  166. switch (cmd) {
  167. case TEST_INIT:
  168. info->name = "copy_config";
  169. info->category = "/main/config/";
  170. info->summary = "Test copying configuration";
  171. info->description =
  172. "Ensure that variables and categories are copied correctly";
  173. return AST_TEST_NOT_RUN;
  174. case TEST_EXECUTE:
  175. break;
  176. }
  177. cfg = build_cfg();
  178. if (!cfg) {
  179. goto out;
  180. }
  181. copy = ast_config_copy(cfg);
  182. if (!copy) {
  183. goto out;
  184. }
  185. if (test_config_validity(copy) != 0) {
  186. goto out;
  187. }
  188. res = AST_TEST_PASS;
  189. out:
  190. ast_config_destroy(cfg);
  191. ast_config_destroy(copy);
  192. return res;
  193. }
  194. /*!
  195. * \brief Write the config file to disk
  196. *
  197. * This is necessary for testing config hooks since
  198. * they are only triggered when a config is read from
  199. * its intended storage medium
  200. */
  201. static int write_config_file(void)
  202. {
  203. int i;
  204. FILE *config_file;
  205. char filename[PATH_MAX];
  206. snprintf(filename, sizeof(filename), "%s/%s",
  207. ast_config_AST_CONFIG_DIR, CONFIG_FILE);
  208. config_file = fopen(filename, "w");
  209. if (!config_file) {
  210. return -1;
  211. }
  212. for (i = 0; i < ARRAY_LEN(categories); ++i) {
  213. int j;
  214. fprintf(config_file, "[%s]\n", categories[i].category);
  215. for (j = 0; j < ARRAY_LEN(categories[i].vars); ++j) {
  216. fprintf(config_file, "%s = %s\n",
  217. categories[i].vars[j].name,
  218. categories[i].vars[j].val);
  219. }
  220. }
  221. fclose(config_file);
  222. return 0;
  223. }
  224. /*!
  225. * \brief Delete config file created by write_config_file
  226. */
  227. static void delete_config_file(void)
  228. {
  229. char filename[PATH_MAX];
  230. snprintf(filename, sizeof(filename), "%s/%s",
  231. ast_config_AST_CONFIG_DIR, CONFIG_FILE);
  232. unlink(filename);
  233. }
  234. /*
  235. * Boolean to indicate if the config hook has run
  236. */
  237. static int hook_run;
  238. /*
  239. * Boolean to indicate if, when the hook runs, the
  240. * data passed to it is what is expected
  241. */
  242. static int hook_config_sane;
  243. static int hook_cb(struct ast_config *cfg)
  244. {
  245. hook_run = 1;
  246. if (test_config_validity(cfg) == 0) {
  247. hook_config_sane = 1;
  248. }
  249. ast_config_destroy(cfg);
  250. return 0;
  251. }
  252. AST_TEST_DEFINE(config_hook)
  253. {
  254. enum ast_test_result_state res = AST_TEST_FAIL;
  255. enum config_hook_flags hook_flags = { 0, };
  256. struct ast_flags config_flags = { CONFIG_FLAG_FILEUNCHANGED };
  257. struct ast_config *cfg;
  258. switch (cmd) {
  259. case TEST_INIT:
  260. info->name = "config_hook";
  261. info->category = "/main/config/";
  262. info->summary = "Test config hooks";
  263. info->description =
  264. "Ensure that config hooks are called at approriate times,"
  265. "not called at inappropriate times, and that all information"
  266. "that should be present is present.";
  267. return AST_TEST_NOT_RUN;
  268. case TEST_EXECUTE:
  269. break;
  270. }
  271. write_config_file();
  272. /*
  273. * Register a config hook to run when CONFIG_FILE is loaded by this module
  274. */
  275. ast_config_hook_register("test_hook",
  276. CONFIG_FILE,
  277. AST_MODULE,
  278. hook_flags,
  279. hook_cb);
  280. /*
  281. * Try loading the config file. This should result in the hook
  282. * being called
  283. */
  284. cfg = ast_config_load(CONFIG_FILE, config_flags);
  285. ast_config_destroy(cfg);
  286. if (!hook_run || !hook_config_sane) {
  287. ast_test_status_update(test, "Config hook either did not run or was given bad data!\n");
  288. goto out;
  289. }
  290. /*
  291. * Now try loading the wrong config file but from the right module.
  292. * Hook should not run
  293. */
  294. hook_run = 0;
  295. cfg = ast_config_load("asterisk.conf", config_flags);
  296. ast_config_destroy(cfg);
  297. if (hook_run) {
  298. ast_test_status_update(test, "Config hook ran even though an incorrect file was specified.\n");
  299. goto out;
  300. }
  301. /*
  302. * Now try loading the correct config file but from the wrong module.
  303. * Hook should not run
  304. */
  305. hook_run = 0;
  306. cfg = ast_config_load2(CONFIG_FILE, "fake_module.so", config_flags);
  307. ast_config_destroy(cfg);
  308. if (hook_run) {
  309. ast_test_status_update(test, "Config hook ran even though an incorrect module was specified.\n");
  310. goto out;
  311. }
  312. /*
  313. * Now try loading the file correctly, but without any changes to the file.
  314. * Hook should not run
  315. */
  316. hook_run = 0;
  317. cfg = ast_config_load(CONFIG_FILE, config_flags);
  318. /* Only destroy this cfg conditionally. Otherwise a crash happens. */
  319. if (cfg != CONFIG_STATUS_FILEUNCHANGED) {
  320. ast_config_destroy(cfg);
  321. }
  322. if (hook_run) {
  323. ast_test_status_update(test, "Config hook ran even though file contents had not changed\n");
  324. goto out;
  325. }
  326. res = AST_TEST_PASS;
  327. out:
  328. delete_config_file();
  329. return res;
  330. }
  331. static int unload_module(void)
  332. {
  333. AST_TEST_UNREGISTER(copy_config);
  334. AST_TEST_UNREGISTER(config_hook);
  335. return 0;
  336. }
  337. static int load_module(void)
  338. {
  339. AST_TEST_REGISTER(copy_config);
  340. AST_TEST_REGISTER(config_hook);
  341. return AST_MODULE_LOAD_SUCCESS;
  342. }
  343. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Config test module");