Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Macros | Enumerations | Functions | Variables
test_config.c File Reference

Configuration unit tests. More...

#include "asterisk.h"
#include <math.h>
#include <sys/stat.h>
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/test.h"
#include "asterisk/paths.h"
#include "asterisk/config_options.h"
#include "asterisk/netsock2.h"
#include "asterisk/acl.h"
#include "asterisk/app.h"
#include "asterisk/pbx.h"
#include "asterisk/frame.h"
#include "asterisk/utils.h"
#include "asterisk/logger.h"
#include "asterisk/format_cap.h"

Go to the source code of this file.

Data Structures

struct  association
 
struct  pair
 
struct  test_config
 
struct  test_item
 

Macros

#define ACL_CONFIG_DENY   "0.0.0.0/0"
 
#define ACL_CONFIG_PERMIT   "1.2.3.4/32"
 
#define ACL_DEFAULT   NULL
 
#define BOOL_CONFIG   "true"
 
#define BOOL_DEFAULT   "false"
 
#define BOOLFLAG1   1 << 0
 
#define BOOLFLAG1_CONFIG   "true"
 
#define BOOLFLAG1_DEFAULT   "false"
 
#define BOOLFLAG2   1 << 1
 
#define BOOLFLAG2_CONFIG   "false"
 
#define BOOLFLAG2_DEFAULT   "false"
 
#define BOOLFLAG3   1 << 2
 
#define BOOLFLAG3_CONFIG   "true"
 
#define BOOLFLAG3_DEFAULT   "false"
 
#define CODEC_CONFIG   "!all,ulaw,g729"
 
#define CODEC_DEFAULT   "!all,alaw"
 
#define CONFIG_FILE   "test_config.conf"
 
#define CONFIG_INCLUDE_FILE   "test_config_include.conf"
 
#define CUSTOM_CONFIG   "yes"
 
#define CUSTOM_DEFAULT   "no"
 
#define DEFAULTVAL   42
 
#define DOUBLE_CONFIG   "0.1"
 
#define DOUBLE_DEFAULT   "1.1"
 
#define EPSILON   0.001
 
#define INT_CONFIG   "-1"
 
#define INT_DEFAULT   "-2"
 
#define NOT_EQUAL_FAIL(field, format)
 
#define SOCKADDR_CONFIG   "1.2.3.4:1234"
 
#define SOCKADDR_DEFAULT   "4.3.2.1:4321"
 
#define STR_CONFIG   "test"
 
#define STR_DEFAULT   "default"
 
#define TEST_PARSE(input, should_succeed, expected_result, flags, result, ...)
 
#define TIMELEN_CONFIG   "1"
 
#define TIMELEN_DEFAULT   "2"
 
#define TOOBIG_I32   "2147483649"
 
#define TOOBIG_U32   "4294967297"
 
#define TOOSMALL_I32   "-2147483649"
 
#define TOOSMALL_U32   "-4294967297"
 
#define UINT_CONFIG   "1"
 
#define UINT_DEFAULT   "2"
 

Enumerations

enum  { EXPECT_FAIL = 0, EXPECT_SUCCEED }
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static AO2_GLOBAL_OBJ_STATIC (global_obj)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (copy_config)
 
 AST_TEST_DEFINE (config_basic_ops)
 
 AST_TEST_DEFINE (config_filtered_ops)
 
 AST_TEST_DEFINE (config_template_ops)
 
 AST_TEST_DEFINE (config_save)
 
 AST_TEST_DEFINE (config_hook)
 
 AST_TEST_DEFINE (ast_parse_arg_test)
 
 AST_TEST_DEFINE (config_options_test)
 
 AST_TEST_DEFINE (config_dialplan_function)
 
 AST_TEST_DEFINE (variable_lists_match)
 
 AST_TEST_DEFINE (variable_list_join_replace)
 
 AST_TEST_DEFINE (variable_list_from_string)
 
static struct ast_configbuild_cfg (void)
 Build ast_config struct from above definitions. More...
 
 CONFIG_INFO_TEST (cfg_info, global_obj, test_config_alloc,.files=ACO_FILES(&config_test_conf),)
 
static int customopt_handler (const struct aco_option *opt, struct ast_variable *var, void *obj)
 
static void delete_config_file (void)
 Delete config file created by write_config_file.
 
static int hook_cb (struct ast_config *cfg)
 
static int load_module (void)
 
static void * test_config_alloc (void)
 
static void test_config_destructor (void *obj)
 
static int test_config_validity (struct ast_config *cfg)
 Tests that the contents of an ast_config is what is expected. More...
 
static void * test_item_alloc (const char *cat)
 
static int test_item_cmp (void *obj, void *arg, int flags)
 
static void test_item_destructor (void *obj)
 
static void * test_item_find (struct ao2_container *container, const char *cat)
 
static int unload_module (void)
 
static int write_config_file (void)
 Write the config file to disk. More...
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Config test module" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
const char cat1 [] = "Capitals"
 
const char cat1varname1 [] = "Germany"
 
const char cat1varname2 [] = "China"
 
const char cat1varname3 [] = "Canada"
 
const char cat1varvalue1 [] = "Berlin"
 
const char cat1varvalue2 [] = "Beijing"
 
const char cat1varvalue3 [] = "Ottawa"
 
const char cat2 [] = "Protagonists"
 
const char cat2varname1 [] = "1984"
 
const char cat2varname2 [] = "Green Eggs And Ham"
 
const char cat2varname3 [] = "The Kalevala"
 
const char cat2varvalue1 [] = "Winston Smith"
 
const char cat2varvalue2 [] = "Sam I Am"
 
const char cat2varvalue3 [] = "Vainamoinen"
 
struct association categories []
 
struct aco_file config_test_conf
 
static struct aco_type global
 
static struct aco_type global_defaults
 
static int hook_config_sane
 
static int hook_run
 
static struct aco_type item
 
static const char * item_blacklist []
 

Detailed Description

Configuration unit tests.

Author
Mark Michelson mmich.nosp@m.elso.nosp@m.n@dig.nosp@m.ium..nosp@m.com

Definition in file test_config.c.

Macro Definition Documentation

#define NOT_EQUAL_FAIL (   field,
  format 
)
Value:
if (arr[x]->field != control->field) { \
ast_test_status_update(test, "%s did not match: " format " != " format " with x = %d\n", #field, arr[x]->field, control->field, x); \
res = AST_TEST_FAIL; \
}

Function Documentation

static struct ast_config* build_cfg ( void  )
static

Build ast_config struct from above definitions.

Return values
NULLFailed to build the config
non-NULLAn ast_config struct populated with data

Definition at line 115 of file test_config.c.

References ast_category_append(), ast_category_new(), ast_config_destroy(), and ast_config_new().

116 {
117  struct ast_config *cfg;
118  struct association *cat_iter;
119  struct pair *var_iter;
120  size_t i;
121  size_t j;
122 
123  cfg = ast_config_new();
124  if (!cfg) {
125  goto fail;
126  }
127 
128  for (i = 0; i < ARRAY_LEN(categories); ++i) {
129  struct ast_category *cat;
130  cat_iter = &categories[i];
131 
132  cat = ast_category_new(cat_iter->category, "", 999999);
133  if (!cat) {
134  goto fail;
135  }
136  ast_category_append(cfg, cat);
137 
138  for (j = 0; j < ARRAY_LEN(cat_iter->vars); ++j) {
139  struct ast_variable *var;
140  var_iter = &cat_iter->vars[j];
141 
142  var = ast_variable_new(var_iter->name, var_iter->val, "");
143  if (!var) {
144  goto fail;
145  }
146  ast_variable_append(cat, var);
147  }
148  }
149 
150  return cfg;
151 
152 fail:
153  ast_config_destroy(cfg);
154  return NULL;
155 }
struct ast_category * ast_category_new(const char *name, const char *in_file, int lineno)
Create a category.
Definition: extconf.c:2788
Structure for variables, used for configurations and for channel variables.
void ast_category_append(struct ast_config *config, struct ast_category *category)
Appends a category to a config.
Definition: extconf.c:2833
struct ast_config * ast_config_new(void)
Create a new base configuration structure.
Definition: extconf.c:3274
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition: extconf.c:1289
static int test_config_validity ( struct ast_config cfg)
static

Tests that the contents of an ast_config is what is expected.

Parameters
cfgConfig to test
Return values
-1Failed to pass a test
0Config passes checks

Definition at line 164 of file test_config.c.

References ast_category_browse(), ast_variable::name, ast_variable::next, and ast_variable::value.

165 {
166  int i;
167  const char *cat_iter = NULL;
168  /* Okay, let's see if the correct content is there */
169  for (i = 0; i < ARRAY_LEN(categories); ++i) {
170  struct ast_variable *var = NULL;
171  size_t j;
172  cat_iter = ast_category_browse(cfg, cat_iter);
173  if (strcmp(cat_iter, categories[i].category)) {
174  ast_log(LOG_ERROR, "Category name mismatch, %s does not match %s\n", cat_iter, categories[i].category);
175  return -1;
176  }
177  for (j = 0; j < ARRAY_LEN(categories[i].vars); ++j) {
178  var = var ? var->next : ast_variable_browse(cfg, cat_iter);
179  if (strcmp(var->name, categories[i].vars[j].name)) {
180  ast_log(LOG_ERROR, "Variable name mismatch, %s does not match %s\n", var->name, categories[i].vars[j].name);
181  return -1;
182  }
183  if (strcmp(var->value, categories[i].vars[j].val)) {
184  ast_log(LOG_ERROR, "Variable value mismatch, %s does not match %s\n", var->value, categories[i].vars[j].val);
185  return -1;
186  }
187  }
188  }
189  return 0;
190 }
struct ast_variable * next
Structure for variables, used for configurations and for channel variables.
char * ast_category_browse(struct ast_config *config, const char *prev_name)
Browse categories.
Definition: extconf.c:3326
static int write_config_file ( void  )
static

Write the config file to disk.

This is necessary for testing config hooks since they are only triggered when a config is read from its intended storage medium

Definition at line 825 of file test_config.c.

References config_file.

826 {
827  int i;
828  FILE *config_file;
829  char filename[PATH_MAX];
830 
831  snprintf(filename, sizeof(filename), "%s/%s",
832  ast_config_AST_CONFIG_DIR, CONFIG_FILE);
833  config_file = fopen(filename, "w");
834 
835  if (!config_file) {
836  return -1;
837  }
838 
839  for (i = 0; i < ARRAY_LEN(categories); ++i) {
840  int j;
841  fprintf(config_file, "[%s]\n", categories[i].category);
842  for (j = 0; j < ARRAY_LEN(categories[i].vars); ++j) {
843  fprintf(config_file, "%s = %s\n",
844  categories[i].vars[j].name,
845  categories[i].vars[j].val);
846  }
847  }
848 
849  fclose(config_file);
850  return 0;
851 }
static const char config_file[]

Variable Documentation

struct aco_file config_test_conf
Initial value:
= {
.filename = "config_test.conf",
.types = ACO_TYPES(&global, &global_defaults, &item),
}
#define ACO_TYPES(...)
A helper macro to ensure that aco_info types always have a sentinel.

Definition at line 1472 of file test_config.c.

const char* item_blacklist[]
static
Initial value:
= {
"global",
"global_defaults",
NULL,
}

Definition at line 1457 of file test_config.c.